LibGfx+Userland: Add width_rounded_up() helper

This commit is contained in:
thankyouverycool 2023-04-14 08:52:47 -04:00 committed by Andreas Kling
parent c9404c3a63
commit 55423b4ed0
21 changed files with 34 additions and 19 deletions

View file

@ -201,7 +201,7 @@ void BookmarksBarWidget::model_did_update(unsigned)
auto title = model()->index(item_index, 0).data().to_deprecated_string();
auto url = model()->index(item_index, 1).data().to_deprecated_string();
Gfx::IntRect rect { width, 0, static_cast<int>(ceilf(font().width(title))) + 32, height() };
Gfx::IntRect rect { width, 0, font().width_rounded_up(title) + 32, height() };
auto& button = add<GUI::Button>();
m_bookmarks.append(button);

View file

@ -41,7 +41,7 @@ void KeyButton::paint_event(GUI::PaintEvent& event)
if (text().is_empty() || text().bytes_as_string_view().starts_with('\0'))
return;
Gfx::IntRect text_rect { 0, 0, static_cast<int>(ceilf(font.width(text()))), font.pixel_size_rounded_up() };
Gfx::IntRect text_rect { 0, 0, font.width_rounded_up(text()), font.pixel_size_rounded_up() };
text_rect.align_within(key_cap_face_rect, Gfx::TextAlignment::Center);
painter.draw_text(text_rect, text(), font, Gfx::TextAlignment::Center, Color::Black, Gfx::TextElision::Right);

View file

@ -585,7 +585,7 @@ void BrickGame::paint_cell(GUI::Painter& painter, Gfx::IntRect rect, BrickGame::
void BrickGame::paint_sidebar_text(GUI::Painter& painter, int row, StringView text)
{
auto const text_width = static_cast<int>(ceilf(font().width(text)));
auto const text_width = font().width_rounded_up(text);
auto const entire_area_rect { frame_inner_rect() };
auto const margin = 4;
auto const rect { Gfx::IntRect { entire_area_rect.x() + entire_area_rect.width() - 116,
@ -597,7 +597,7 @@ void BrickGame::paint_sidebar_text(GUI::Painter& painter, int row, StringView te
void BrickGame::paint_paused_text(GUI::Painter& painter)
{
auto const paused_text = "Paused"sv;
auto const paused_text_width = static_cast<int>(ceilf(font().width(paused_text)));
auto const paused_text_width = font().width_rounded_up(paused_text);
auto const more_or_less_font_height = static_cast<int>(font().pixel_size_rounded_up());
auto const entire_area_rect { frame_inner_rect() };
auto const margin = more_or_less_font_height * 2;

View file

@ -71,7 +71,7 @@ void ScoreCard::paint_event(GUI::PaintEvent& event)
for (int score_index = 0; score_index < (int)player.scores.size(); score_index++) {
auto text_rect = cell_rect(player_index, 1 + score_index);
auto score_text = DeprecatedString::formatted("{}", player.scores[score_index]);
auto score_text_width = static_cast<int>(ceilf(font.width(score_text)));
auto score_text_width = font.width_rounded_up(score_text);
if (score_index != (int)player.scores.size() - 1) {
painter.draw_line(
{ text_rect.left() + text_rect.width() / 2 - score_text_width / 2 - 3, text_rect.top() + font.pixel_size_rounded_up() / 2 },

View file

@ -201,7 +201,7 @@ void CardPainter::paint_card_front(Gfx::Bitmap& bitmap, Cards::Suit suit, Cards:
paint_rect.set_height(paint_rect.height() / 2);
paint_rect.shrink(10, 6);
auto text_rect = Gfx::IntRect { 4, 6, static_cast<int>(ceilf(font.width("10"sv))), font.pixel_size_rounded_up() };
auto text_rect = Gfx::IntRect { 4, 6, font.width_rounded_up("10"sv), font.pixel_size_rounded_up() };
painter.draw_text(text_rect, card_rank_label(rank), font, Gfx::TextAlignment::Center, suit_color);
painter.draw_bitmap(

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.pixel_size_rounded_up() };
Gfx::IntRect text_rect { 0, 0, font.width_rounded_up(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

@ -65,7 +65,7 @@ void CheckBox::paint_event(PaintEvent& event)
auto text_rect = rect();
if (m_checkbox_position == CheckBoxPosition::Left)
text_rect.set_left(box_rect.right() + 1 + gap_between_box_and_rect());
text_rect.set_width(static_cast<int>(ceilf(font().width(text()))));
text_rect.set_width(font().width_rounded_up(text()));
text_rect.set_top(height() / 2 - font().pixel_size_rounded_up() / 2);
text_rect.set_height(font().pixel_size_rounded_up());
@ -101,7 +101,7 @@ void CheckBox::set_autosize(bool autosize)
void CheckBox::size_to_fit()
{
set_fixed_width(box_rect().width() + gap_between_box_and_rect() + static_cast<int>(ceilf(font().width(text()))) + horizontal_padding() * 2);
set_fixed_width(box_rect().width() + gap_between_box_and_rect() + font().width_rounded_up(text()) + horizontal_padding() * 2);
}
Optional<UISize> CheckBox::calculated_min_size() const

View file

@ -43,7 +43,7 @@ void GroupBox::paint_event(PaintEvent& event)
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().pixel_size_rounded_up() };
Gfx::IntRect text_rect { 6, 1, font().width_rounded_up(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

@ -272,7 +272,7 @@ void HeaderView::paint_horizontal(Painter& painter)
painter.draw_text(text_rect, text, font(), section_data.alignment, palette().button_text());
if (is_key_column && (m_table_view.sort_order() != SortOrder::None)) {
Gfx::IntPoint offset { text_rect.x() + static_cast<int>(ceilf(font().width(text))) + sorting_arrow_offset, sorting_arrow_offset };
Gfx::IntPoint offset { text_rect.x() + font().width_rounded_up(text) + sorting_arrow_offset, sorting_arrow_offset };
auto coordinates = m_table_view.sort_order() == SortOrder::Ascending
? ascending_arrow_coordinates.span()
: descending_arrow_coordinates.span();

View file

@ -428,7 +428,7 @@ void IconView::get_item_rects(int item_index, ItemData& item_data, Gfx::Font con
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 unwrapped_text_width = font.width_rounded_up(item_data.text);
int available_width = item_rect.width() - 6;
item_data.text_rect = { 0, item_data.icon_rect.bottom() + 6 + 1, 0, font.pixel_size_rounded_up() };

View file

@ -116,7 +116,7 @@ void Label::size_to_fit()
int Label::text_calculated_preferred_width() const
{
return static_cast<int>(ceilf(font().width(m_text))) + m_autosize_padding * 2;
return font().width_rounded_up(m_text) + m_autosize_padding * 2;
}
int Label::text_calculated_preferred_height() const

View file

@ -81,7 +81,7 @@ void LinkLabel::paint_event(PaintEvent& event)
GUI::Painter painter(*this);
if (m_hovered)
painter.draw_line({ 0, rect().bottom() }, { static_cast<int>(ceilf(font().width(text()))), rect().bottom() }, palette().link());
painter.draw_line({ 0, rect().bottom() }, { font().width_rounded_up(text()), rect().bottom() }, palette().link());
if (is_focused())
painter.draw_focus_rect(text_rect(), palette().focus_outline());

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().pixel_size_rounded_up() };
Gfx::IntRect text_rect { circle_rect.right() + 5 + horizontal_padding(), 0, font().width_rounded_up(text()), font().pixel_size_rounded_up() };
text_rect.center_vertically_within(rect());
paint_text(painter, text_rect, font(), Gfx::TextAlignment::TopLeft);
@ -69,7 +69,7 @@ void RadioButton::click(unsigned)
Optional<UISize> RadioButton::calculated_min_size() const
{
auto const& font = this->font();
int width = horizontal_padding() * 2 + circle_size().width() + static_cast<int>(ceilf(font.width(text())));
int width = horizontal_padding() * 2 + circle_size().width() + font.width_rounded_up(text());
int height = max(22, max(font.pixel_size_rounded_up() + 8, circle_size().height()));
return UISize(width, height);
}

View file

@ -452,7 +452,7 @@ Gfx::IntRect TabWidget::close_button_rect(size_t index) const
int TabWidget::TabData::width(Gfx::Font const& font) const
{
auto width = 16 + static_cast<int>(ceilf(font.width(title))) + (icon ? (16 + 4) : 0);
auto width = 16 + font.width_rounded_up(title) + (icon ? (16 + 4) : 0);
// NOTE: This needs to always be an odd number, because the button rect
// includes 3px of light and shadow on the left and right edges. If
// the button rect width is not an odd number, the area left for the

View file

@ -336,6 +336,11 @@ float BitmapFont::glyph_or_emoji_width(Utf32CodePointIterator& it) const
return glyph_or_emoji_width_impl(*this, it);
}
int BitmapFont::width_rounded_up(StringView view) const
{
return static_cast<int>(ceilf(width(view)));
}
float BitmapFont::width(StringView view) const { return unicode_view_width(Utf8View(view)); }
float BitmapFont::width(Utf8View const& view) const { return unicode_view_width(view); }
float BitmapFont::width(Utf32View const& view) const { return unicode_view_width(view); }

View file

@ -97,6 +97,8 @@ public:
virtual float width(Utf8View const&) const override;
virtual float width(Utf32View const&) const override;
virtual int width_rounded_up(StringView) const override;
DeprecatedString name() const override { return m_name; }
void set_name(DeprecatedString name) { m_name = move(name); }

View file

@ -196,6 +196,8 @@ public:
virtual float width(Utf8View const&) const = 0;
virtual float width(Utf32View const&) const = 0;
virtual int width_rounded_up(StringView) const = 0;
virtual DeprecatedString name() const = 0;
virtual bool is_fixed_width() const = 0;

View file

@ -36,6 +36,11 @@ ScaledFont::ScaledFont(NonnullRefPtr<VectorFont> font, float point_width, float
};
}
int ScaledFont::width_rounded_up(StringView view) const
{
return static_cast<int>(ceilf(width(view)));
}
float ScaledFont::width(StringView view) const { return unicode_view_width(Utf8View(view)); }
float ScaledFont::width(Utf8View const& view) const { return unicode_view_width(view); }
float ScaledFont::width(Utf32View const& view) const { return unicode_view_width(view); }

View file

@ -61,6 +61,7 @@ public:
virtual float width(StringView) const override;
virtual float width(Utf8View const&) const override;
virtual float width(Utf32View const&) const override;
virtual int width_rounded_up(StringView) const override;
virtual DeprecatedString name() const override { return DeprecatedString::formatted("{} {}", family(), variant()); }
virtual bool is_fixed_width() const override { return m_font->is_fixed_width(); }
virtual u8 glyph_spacing() const override { return 0; }

View file

@ -2453,7 +2453,7 @@ void Gfx::Painter::draw_ui_text(Gfx::IntRect const& rect, StringView text, Gfx::
Optional<size_t> underline_offset;
auto name_to_draw = parse_ampersand_string(text, &underline_offset);
Gfx::IntRect text_rect { 0, 0, static_cast<int>(ceilf(font.width(name_to_draw))), font.pixel_size_rounded_up() };
Gfx::IntRect text_rect { 0, 0, font.width_rounded_up(name_to_draw), font.pixel_size_rounded_up() };
text_rect.align_within(rect, text_alignment);
draw_text(text_rect, name_to_draw, font, text_alignment, color);

View file

@ -106,7 +106,7 @@ void TaskbarButton::paint_event(GUI::PaintEvent& event)
content_rect.set_width(content_rect.width() - icon.width() - 4);
}
Gfx::IntRect text_rect { 0, 0, static_cast<int>(ceilf(font.width(text()))), font.pixel_size_rounded_up() };
Gfx::IntRect text_rect { 0, 0, font.width_rounded_up(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());