From 55423b4ed06b4db1b61f862250d17f3407671117 Mon Sep 17 00:00:00 2001 From: thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> Date: Fri, 14 Apr 2023 08:52:47 -0400 Subject: [PATCH] LibGfx+Userland: Add width_rounded_up() helper --- Userland/Applications/Browser/BookmarksBarWidget.cpp | 2 +- Userland/Applications/KeyboardMapper/KeyButton.cpp | 2 +- Userland/Games/BrickGame/BrickGame.cpp | 4 ++-- Userland/Games/Hearts/ScoreCard.cpp | 2 +- Userland/Libraries/LibCards/CardPainter.cpp | 2 +- Userland/Libraries/LibGUI/Button.cpp | 2 +- Userland/Libraries/LibGUI/CheckBox.cpp | 4 ++-- Userland/Libraries/LibGUI/GroupBox.cpp | 2 +- Userland/Libraries/LibGUI/HeaderView.cpp | 2 +- Userland/Libraries/LibGUI/IconView.cpp | 2 +- Userland/Libraries/LibGUI/Label.cpp | 2 +- Userland/Libraries/LibGUI/LinkLabel.cpp | 2 +- Userland/Libraries/LibGUI/RadioButton.cpp | 4 ++-- Userland/Libraries/LibGUI/TabWidget.cpp | 2 +- Userland/Libraries/LibGfx/Font/BitmapFont.cpp | 5 +++++ Userland/Libraries/LibGfx/Font/BitmapFont.h | 2 ++ Userland/Libraries/LibGfx/Font/Font.h | 2 ++ Userland/Libraries/LibGfx/Font/ScaledFont.cpp | 5 +++++ Userland/Libraries/LibGfx/Font/ScaledFont.h | 1 + Userland/Libraries/LibGfx/Painter.cpp | 2 +- Userland/Services/Taskbar/TaskbarButton.cpp | 2 +- 21 files changed, 34 insertions(+), 19 deletions(-) diff --git a/Userland/Applications/Browser/BookmarksBarWidget.cpp b/Userland/Applications/Browser/BookmarksBarWidget.cpp index d51b88ae5f..56dd74a367 100644 --- a/Userland/Applications/Browser/BookmarksBarWidget.cpp +++ b/Userland/Applications/Browser/BookmarksBarWidget.cpp @@ -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(ceilf(font().width(title))) + 32, height() }; + Gfx::IntRect rect { width, 0, font().width_rounded_up(title) + 32, height() }; auto& button = add(); m_bookmarks.append(button); diff --git a/Userland/Applications/KeyboardMapper/KeyButton.cpp b/Userland/Applications/KeyboardMapper/KeyButton.cpp index c7d7f74e72..075fb35836 100644 --- a/Userland/Applications/KeyboardMapper/KeyButton.cpp +++ b/Userland/Applications/KeyboardMapper/KeyButton.cpp @@ -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(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); diff --git a/Userland/Games/BrickGame/BrickGame.cpp b/Userland/Games/BrickGame/BrickGame.cpp index 630579ccc2..625eab2178 100644 --- a/Userland/Games/BrickGame/BrickGame.cpp +++ b/Userland/Games/BrickGame/BrickGame.cpp @@ -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(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(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(font().pixel_size_rounded_up()); auto const entire_area_rect { frame_inner_rect() }; auto const margin = more_or_less_font_height * 2; diff --git a/Userland/Games/Hearts/ScoreCard.cpp b/Userland/Games/Hearts/ScoreCard.cpp index f8b934c80c..1f34aa4081 100644 --- a/Userland/Games/Hearts/ScoreCard.cpp +++ b/Userland/Games/Hearts/ScoreCard.cpp @@ -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(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 }, diff --git a/Userland/Libraries/LibCards/CardPainter.cpp b/Userland/Libraries/LibCards/CardPainter.cpp index a95f7f3f5a..b8cc7b2fbc 100644 --- a/Userland/Libraries/LibCards/CardPainter.cpp +++ b/Userland/Libraries/LibCards/CardPainter.cpp @@ -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(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( diff --git a/Userland/Libraries/LibGUI/Button.cpp b/Userland/Libraries/LibGUI/Button.cpp index 30da20f9f7..88b999cdbb 100644 --- a/Userland/Libraries/LibGUI/Button.cpp +++ b/Userland/Libraries/LibGUI/Button.cpp @@ -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(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()); diff --git a/Userland/Libraries/LibGUI/CheckBox.cpp b/Userland/Libraries/LibGUI/CheckBox.cpp index fe4f12fabe..693505d446 100644 --- a/Userland/Libraries/LibGUI/CheckBox.cpp +++ b/Userland/Libraries/LibGUI/CheckBox.cpp @@ -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(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(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 CheckBox::calculated_min_size() const diff --git a/Userland/Libraries/LibGUI/GroupBox.cpp b/Userland/Libraries/LibGUI/GroupBox.cpp index 01e3dfe18a..d45ae0c0fa 100644 --- a/Userland/Libraries/LibGUI/GroupBox.cpp +++ b/Userland/Libraries/LibGUI/GroupBox.cpp @@ -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(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()); } diff --git a/Userland/Libraries/LibGUI/HeaderView.cpp b/Userland/Libraries/LibGUI/HeaderView.cpp index c29586dd0c..1998a60c4e 100644 --- a/Userland/Libraries/LibGUI/HeaderView.cpp +++ b/Userland/Libraries/LibGUI/HeaderView.cpp @@ -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(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(); diff --git a/Userland/Libraries/LibGUI/IconView.cpp b/Userland/Libraries/LibGUI/IconView.cpp index 3445145af4..521f97bbac 100644 --- a/Userland/Libraries/LibGUI/IconView.cpp +++ b/Userland/Libraries/LibGUI/IconView.cpp @@ -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(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() }; diff --git a/Userland/Libraries/LibGUI/Label.cpp b/Userland/Libraries/LibGUI/Label.cpp index 79ca24f2f1..37b106b58a 100644 --- a/Userland/Libraries/LibGUI/Label.cpp +++ b/Userland/Libraries/LibGUI/Label.cpp @@ -116,7 +116,7 @@ void Label::size_to_fit() int Label::text_calculated_preferred_width() const { - return static_cast(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 diff --git a/Userland/Libraries/LibGUI/LinkLabel.cpp b/Userland/Libraries/LibGUI/LinkLabel.cpp index bd1e6240df..fff58244fb 100644 --- a/Userland/Libraries/LibGUI/LinkLabel.cpp +++ b/Userland/Libraries/LibGUI/LinkLabel.cpp @@ -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(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()); diff --git a/Userland/Libraries/LibGUI/RadioButton.cpp b/Userland/Libraries/LibGUI/RadioButton.cpp index bd92ed1e8e..11e84bebee 100644 --- a/Userland/Libraries/LibGUI/RadioButton.cpp +++ b/Userland/Libraries/LibGUI/RadioButton.cpp @@ -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(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 RadioButton::calculated_min_size() const { auto const& font = this->font(); - int width = horizontal_padding() * 2 + circle_size().width() + static_cast(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); } diff --git a/Userland/Libraries/LibGUI/TabWidget.cpp b/Userland/Libraries/LibGUI/TabWidget.cpp index 6cc20289af..91b82301b8 100644 --- a/Userland/Libraries/LibGUI/TabWidget.cpp +++ b/Userland/Libraries/LibGUI/TabWidget.cpp @@ -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(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 diff --git a/Userland/Libraries/LibGfx/Font/BitmapFont.cpp b/Userland/Libraries/LibGfx/Font/BitmapFont.cpp index c165b24864..6c011d4df4 100644 --- a/Userland/Libraries/LibGfx/Font/BitmapFont.cpp +++ b/Userland/Libraries/LibGfx/Font/BitmapFont.cpp @@ -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(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); } diff --git a/Userland/Libraries/LibGfx/Font/BitmapFont.h b/Userland/Libraries/LibGfx/Font/BitmapFont.h index dc174136e8..514b358a7e 100644 --- a/Userland/Libraries/LibGfx/Font/BitmapFont.h +++ b/Userland/Libraries/LibGfx/Font/BitmapFont.h @@ -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); } diff --git a/Userland/Libraries/LibGfx/Font/Font.h b/Userland/Libraries/LibGfx/Font/Font.h index 5df49042bf..f0633eb557 100644 --- a/Userland/Libraries/LibGfx/Font/Font.h +++ b/Userland/Libraries/LibGfx/Font/Font.h @@ -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; diff --git a/Userland/Libraries/LibGfx/Font/ScaledFont.cpp b/Userland/Libraries/LibGfx/Font/ScaledFont.cpp index 6a9891fb01..441639bc97 100644 --- a/Userland/Libraries/LibGfx/Font/ScaledFont.cpp +++ b/Userland/Libraries/LibGfx/Font/ScaledFont.cpp @@ -36,6 +36,11 @@ ScaledFont::ScaledFont(NonnullRefPtr font, float point_width, float }; } +int ScaledFont::width_rounded_up(StringView view) const +{ + return static_cast(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); } diff --git a/Userland/Libraries/LibGfx/Font/ScaledFont.h b/Userland/Libraries/LibGfx/Font/ScaledFont.h index d968afcc95..ada2754dbb 100644 --- a/Userland/Libraries/LibGfx/Font/ScaledFont.h +++ b/Userland/Libraries/LibGfx/Font/ScaledFont.h @@ -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; } diff --git a/Userland/Libraries/LibGfx/Painter.cpp b/Userland/Libraries/LibGfx/Painter.cpp index b5be252aa0..edf9271c25 100644 --- a/Userland/Libraries/LibGfx/Painter.cpp +++ b/Userland/Libraries/LibGfx/Painter.cpp @@ -2453,7 +2453,7 @@ void Gfx::Painter::draw_ui_text(Gfx::IntRect const& rect, StringView text, Gfx:: Optional underline_offset; auto name_to_draw = parse_ampersand_string(text, &underline_offset); - Gfx::IntRect text_rect { 0, 0, static_cast(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); diff --git a/Userland/Services/Taskbar/TaskbarButton.cpp b/Userland/Services/Taskbar/TaskbarButton.cpp index d731d0000f..4902d558b8 100644 --- a/Userland/Services/Taskbar/TaskbarButton.cpp +++ b/Userland/Services/Taskbar/TaskbarButton.cpp @@ -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(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());