1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 09:57:35 +00:00

Userland: Use Font::pixel_size_rounded_up() instead of glyph_height()

The only remaining clients of this API are specific to bitmap fonts and
editing thereof.
This commit is contained in:
Andreas Kling 2023-03-03 19:32:19 +01:00
parent 93c9344e35
commit b71c7a6e44
32 changed files with 63 additions and 65 deletions

View file

@ -24,7 +24,7 @@ public:
class AbstractTableView : public AbstractView {
public:
int row_height() const { return font().glyph_height() + vertical_padding(); }
int row_height() const { return font().pixel_size_rounded_up() + vertical_padding(); }
virtual int horizontal_padding() const { return m_horizontal_padding; }
void set_horizontal_padding(int padding) { m_horizontal_padding = padding; }
@ -122,7 +122,7 @@ private:
bool m_highlight_selected_rows { true };
int m_vertical_padding { 8 };
int m_horizontal_padding { font().glyph_height() / 2 };
int m_horizontal_padding { font().pixel_size_rounded_up() / 2 };
int m_tab_moves { 0 };
};

View file

@ -30,8 +30,8 @@ public:
m_label->set_text(Gfx::parse_ampersand_string(tooltip));
int tooltip_width = m_label->effective_min_size().width().as_int() + 10;
int line_count = m_label->text().count("\n"sv);
int glyph_height = m_label->font().glyph_height();
int tooltip_height = glyph_height * (1 + line_count) + ((glyph_height + 1) / 2) * line_count + 8;
int font_size = m_label->font().pixel_size_rounded_up();
int tooltip_height = font_size * (1 + line_count) + ((font_size + 1) / 2) * line_count + 8;
Gfx::IntRect desktop_rect = Desktop::the().rect();
if (tooltip_width > desktop_rect.width())

View file

@ -105,7 +105,7 @@ void Button::paint_event(PaintEvent& event)
content_rect.set_width(content_rect.width() - m_icon->width() - icon_spacing());
}
Gfx::IntRect text_rect { 0, 0, static_cast<int>(ceilf(font.width(text()))), font.glyph_height() };
Gfx::IntRect text_rect { 0, 0, static_cast<int>(ceilf(font.width(text()))), font.pixel_size_rounded_up() };
if (text_rect.width() > content_rect.width())
text_rect.set_width(content_rect.width());
text_rect.align_within(content_rect, text_alignment());

View file

@ -484,7 +484,7 @@ void Calendar::paint_event(GUI::PaintEvent& event)
x_offset,
y_offset + 4,
m_tiles[0][i].width - 4,
font().glyph_height() + 4);
font().pixel_size_rounded_up() + 4);
if (width > 150 && height > 150) {
set_font(extra_large_font);

View file

@ -102,9 +102,9 @@ Gfx::IntRect GlyphMapWidget::get_outer_rect(int glyph) const
int column = glyph % columns();
return Gfx::IntRect {
column * (font().max_glyph_width() + m_horizontal_spacing),
row * (font().glyph_height() + m_vertical_spacing),
row * (font().pixel_size_rounded_up() + m_vertical_spacing),
font().max_glyph_width() + m_horizontal_spacing,
font().glyph_height() + m_vertical_spacing
font().pixel_size_rounded_up() + m_vertical_spacing
}
.translated(frame_thickness() - horizontal_scrollbar().value(), frame_thickness() - vertical_scrollbar().value());
}
@ -136,7 +136,7 @@ void GlyphMapWidget::paint_event(PaintEvent& event)
outer_rect.x() + m_horizontal_spacing / 2,
outer_rect.y() + m_vertical_spacing / 2,
font().max_glyph_width(),
font().glyph_height());
font().pixel_size_rounded_up());
if (m_selection.contains(glyph)) {
painter.fill_rect(outer_rect, is_focused() ? palette().selection() : palette().inactive_selection());
if (font().contains_glyph(glyph))
@ -184,7 +184,7 @@ Optional<int> GlyphMapWidget::glyph_at_position(Gfx::IntPoint position) const
Gfx::IntPoint map_offset { frame_thickness() - horizontal_scrollbar().value(), frame_thickness() - vertical_scrollbar().value() };
auto map_position = position - map_offset;
auto col = (map_position.x() - 1) / ((font().max_glyph_width() + m_horizontal_spacing));
auto row = (map_position.y() - 1) / ((font().glyph_height() + m_vertical_spacing));
auto row = (map_position.y() - 1) / ((font().pixel_size_rounded_up() + m_vertical_spacing));
auto glyph = row * columns() + col + m_active_range.first;
if (row >= 0 && row < rows() && col >= 0 && col < columns() && glyph < m_glyph_count + m_active_range.first)
return glyph;
@ -197,7 +197,7 @@ int GlyphMapWidget::glyph_at_position_clamped(Gfx::IntPoint position) const
Gfx::IntPoint map_offset { frame_thickness() - horizontal_scrollbar().value(), frame_thickness() - vertical_scrollbar().value() };
auto map_position = position - map_offset;
auto col = clamp((map_position.x() - 1) / ((font().max_glyph_width() + m_horizontal_spacing)), 0, columns() - 1);
auto row = clamp((map_position.y() - 1) / ((font().glyph_height() + m_vertical_spacing)), 0, rows() - 1);
auto row = clamp((map_position.y() - 1) / ((font().pixel_size_rounded_up() + m_vertical_spacing)), 0, rows() - 1);
auto glyph = row * columns() + col + m_active_range.first;
if (row == rows() - 1)
glyph = min(glyph, m_glyph_count + m_active_range.first - 1);
@ -448,7 +448,7 @@ void GlyphMapWidget::keydown_event(KeyEvent& event)
void GlyphMapWidget::did_change_font()
{
recalculate_content_size();
vertical_scrollbar().set_step(font().glyph_height() + m_vertical_spacing);
vertical_scrollbar().set_step(font().pixel_size_rounded_up() + m_vertical_spacing);
}
void GlyphMapWidget::scroll_to_glyph(int glyph)
@ -458,9 +458,9 @@ void GlyphMapWidget::scroll_to_glyph(int glyph)
int column = glyph % columns();
auto scroll_rect = Gfx::IntRect {
column * (font().max_glyph_width() + m_horizontal_spacing),
row * (font().glyph_height() + m_vertical_spacing),
row * (font().pixel_size_rounded_up() + m_vertical_spacing),
font().max_glyph_width() + m_horizontal_spacing,
font().glyph_height() + m_vertical_spacing
font().pixel_size_rounded_up() + m_vertical_spacing
};
scroll_into_view(scroll_rect, true, true);
}
@ -515,12 +515,12 @@ void GlyphMapWidget::recalculate_content_size()
m_rows = ceil_div(m_glyph_count, m_columns);
constexpr auto overdraw_margins = 2;
auto max_visible_rows = event_height / (font().glyph_height() + m_vertical_spacing);
auto max_visible_rows = event_height / (font().pixel_size_rounded_up() + m_vertical_spacing);
m_visible_rows = min(max_visible_rows, m_rows);
m_visible_glyphs = (m_visible_rows + overdraw_margins) * m_columns;
int content_width = columns() * (font().max_glyph_width() + m_horizontal_spacing);
int content_height = rows() * (font().glyph_height() + m_vertical_spacing);
int content_height = rows() * (font().pixel_size_rounded_up() + m_vertical_spacing);
set_content_size({ content_width, content_height });
scroll_to_glyph(m_active_glyph);
@ -590,7 +590,7 @@ void GlyphMapWidget::leave_event(Core::Event&)
Optional<UISize> GlyphMapWidget::calculated_min_size() const
{
auto scrollbar = vertical_scrollbar().effective_min_size().height().as_int();
auto min_height = max(font().glyph_height() + m_vertical_spacing, scrollbar);
auto min_height = max(font().pixel_size_rounded_up() + m_vertical_spacing, scrollbar);
auto min_width = font().max_glyph_width() + m_horizontal_spacing + width_occupied_by_vertical_scrollbar();
return { { min_width + frame_thickness() * 2, min_height + frame_thickness() * 2 } };
}

View file

@ -24,7 +24,7 @@ GroupBox::GroupBox(StringView title)
Margins GroupBox::content_margins() const
{
return {
(!m_title.is_empty() ? static_cast<int>(ceilf(font().glyph_height())) + 1 /*room for the focus rect*/ : 2),
(!m_title.is_empty() ? font().pixel_size_rounded_up() + 1 /*room for the focus rect*/ : 2),
2,
2,
2
@ -37,13 +37,13 @@ void GroupBox::paint_event(PaintEvent& event)
painter.add_clip_rect(event.rect());
Gfx::IntRect frame_rect {
0, (!m_title.is_empty() ? font().glyph_height() / 2 : 0),
width(), height() - (!m_title.is_empty() ? static_cast<int>(ceilf(font().glyph_height())) / 2 : 0)
0, (!m_title.is_empty() ? font().pixel_size_rounded_up() / 2 : 0),
width(), height() - (!m_title.is_empty() ? font().pixel_size_rounded_up() / 2 : 0)
};
Gfx::StylePainter::paint_frame(painter, frame_rect, palette(), Gfx::FrameShape::Box, Gfx::FrameShadow::Sunken, 2);
if (!m_title.is_empty()) {
Gfx::IntRect text_rect { 6, 1, static_cast<int>(ceilf(font().width(m_title) + 6)), font().glyph_height() };
Gfx::IntRect text_rect { 6, 1, static_cast<int>(ceilf(font().width(m_title) + 6)), font().pixel_size_rounded_up() };
painter.fill_rect(text_rect, palette().button());
painter.draw_text(text_rect, m_title, Gfx::TextAlignment::CenterLeft, palette().button_text());
}

View file

@ -379,7 +379,7 @@ Gfx::IntRect IconView::editing_rect(ModelIndex const& index) const
return {};
auto& item_data = get_item_data(index.row());
auto editing_rect = item_data.text_rect;
editing_rect.set_height(font_for_index(index)->glyph_height() + 8);
editing_rect.set_height(font_for_index(index)->pixel_size_rounded_up() + 8);
editing_rect.set_y(item_data.text_rect.y() - 2);
return editing_rect;
}
@ -425,13 +425,13 @@ void IconView::get_item_rects(int item_index, ItemData& item_data, Gfx::Font con
{
auto item_rect = this->item_rect(item_index);
item_data.icon_rect = Gfx::IntRect(0, 0, 32, 32).centered_within(item_rect);
item_data.icon_offset_y = -font.glyph_height() - 6;
item_data.icon_offset_y = -font.pixel_size_rounded_up() - 6;
item_data.icon_rect.translate_by(0, item_data.icon_offset_y);
int unwrapped_text_width = static_cast<int>(ceilf(font.width(item_data.text)));
int available_width = item_rect.width() - 6;
item_data.text_rect = { 0, item_data.icon_rect.bottom() + 6 + 1, 0, font.glyph_height() };
item_data.text_rect = { 0, item_data.icon_rect.bottom() + 6 + 1, 0, font.pixel_size_rounded_up() };
item_data.wrapped_text_lines.clear();
if ((unwrapped_text_width > available_width) && (item_data.selected || m_hovered_index == item_data.index || cursor_index() == item_data.index || m_always_wrap_item_labels)) {
@ -458,7 +458,7 @@ void IconView::get_item_rects(int item_index, ItemData& item_data, Gfx::Font con
item_data.text_rect.set_width(widest_line_width);
item_data.text_rect.center_horizontally_within(item_rect);
item_data.text_rect.intersect(item_rect);
item_data.text_rect.set_height(font.glyph_height() * item_data.wrapped_text_lines.size());
item_data.text_rect.set_height(font.pixel_size_rounded_up() * item_data.wrapped_text_lines.size());
item_data.text_rect.inflate(6, 6);
item_data.text_rect_wrapped = item_data.text_rect;
} else {
@ -551,14 +551,14 @@ void IconView::paint_event(PaintEvent& event)
// Item text would not fit in the item text rect, let's break it up into lines..
const auto& lines = item_data.wrapped_text_lines;
size_t number_of_text_lines = min((size_t)text_rect.height() / font->glyph_height(), lines.size());
size_t number_of_text_lines = min((size_t)text_rect.height() / font->pixel_size_rounded_up(), lines.size());
size_t previous_line_lengths = 0;
for (size_t line_index = 0; line_index < number_of_text_lines; ++line_index) {
Gfx::IntRect line_rect;
line_rect.set_width(text_rect.width());
line_rect.set_height(font->glyph_height());
line_rect.set_height(font->pixel_size_rounded_up());
line_rect.center_horizontally_within(item_data.text_rect);
line_rect.set_y(3 + item_data.text_rect.y() + line_index * font->glyph_height());
line_rect.set_y(3 + item_data.text_rect.y() + line_index * font->pixel_size_rounded_up());
line_rect.inflate(6, 0);
// Shrink the line_rect on the last line to apply elision if there are more lines.

View file

@ -117,7 +117,7 @@ void MessageBox::build()
int text_width = widget->font().width(m_text);
auto number_of_lines = m_text.split('\n').size();
int padded_text_height = widget->font().glyph_height() * 1.6;
int padded_text_height = widget->font().pixel_size_rounded_up() * 1.6;
int total_text_height = number_of_lines * padded_text_height;
int icon_width = 0;

View file

@ -51,7 +51,7 @@ void RadioButton::paint_event(PaintEvent& event)
Gfx::StylePainter::paint_radio_button(painter, circle_rect, palette(), is_checked(), is_being_pressed());
Gfx::IntRect text_rect { circle_rect.right() + 5 + horizontal_padding(), 0, static_cast<int>(ceilf(font().width(text()))), font().glyph_height() };
Gfx::IntRect text_rect { circle_rect.right() + 5 + horizontal_padding(), 0, static_cast<int>(ceilf(font().width(text()))), font().pixel_size_rounded_up() };
text_rect.center_vertically_within(rect());
paint_text(painter, text_rect, font(), Gfx::TextAlignment::TopLeft);

View file

@ -321,7 +321,7 @@ void TabWidget::paint_event(PaintEvent& event)
painter.draw_text(tab_button_content_rect, m_tabs[i].title, m_text_alignment, palette().button_text(), Gfx::TextElision::Right);
if (is_focused()) {
Gfx::IntRect focus_rect { 0, 0, min(tab_button_content_rect.width(), font().width(m_tabs[i].title)), font().glyph_height() };
Gfx::IntRect focus_rect { 0, 0, min(tab_button_content_rect.width(), font().width(m_tabs[i].title)), font().pixel_size_rounded_up() };
focus_rect.align_within(tab_button_content_rect, m_text_alignment);
focus_rect.inflate(6, 4);

View file

@ -1437,7 +1437,7 @@ Gfx::IntRect TextEditor::line_content_rect(size_t line_index) const
{
auto& line = this->line(line_index);
if (is_single_line()) {
Gfx::IntRect line_rect = { content_x_for_position({ line_index, 0 }), 0, text_width_for_font(line.view(), font()), font().glyph_height() + 4 };
Gfx::IntRect line_rect = { content_x_for_position({ line_index, 0 }), 0, text_width_for_font(line.view(), font()), font().pixel_size_rounded_up() + 4 };
line_rect.center_vertically_within({ {}, frame_inner_rect().size() });
return line_rect;
}

View file

@ -27,11 +27,11 @@ WizardPage::WizardPage(DeprecatedString const& title_text, DeprecatedString cons
header_widget.set_layout<VerticalBoxLayout>(GUI::Margins { 15, 30, 0 });
m_title_label = header_widget.add<Label>(title_text);
m_title_label->set_font(Gfx::FontDatabase::default_font().bold_variant());
m_title_label->set_fixed_height(m_title_label->font().glyph_height() + 2);
m_title_label->set_fixed_height(m_title_label->font().pixel_size_rounded_up() + 2);
m_title_label->set_text_alignment(Gfx::TextAlignment::TopLeft);
m_subtitle_label = header_widget.add<Label>(subtitle_text);
m_subtitle_label->set_text_alignment(Gfx::TextAlignment::TopLeft);
m_subtitle_label->set_fixed_height(m_subtitle_label->font().glyph_height());
m_subtitle_label->set_fixed_height(m_subtitle_label->font().pixel_size_rounded_up());
header_widget.add_spacer().release_value_but_fixme_should_propagate_errors();
auto& separator = add<SeparatorWidget>(Gfx::Orientation::Horizontal);