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

LibGfx: Make Font::width() return a float

This commit is contained in:
Andreas Kling 2023-01-03 14:43:07 +01:00
parent b9d2b8f7b2
commit 3407ab0fd1
25 changed files with 40 additions and 40 deletions

View file

@ -196,7 +196,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, font().width(title) + 32, height() };
Gfx::IntRect rect { width, 0, static_cast<int>(ceilf(font().width(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().starts_with('\0'))
return;
Gfx::IntRect text_rect { 0, 0, font.width(text()), font.glyph_height() };
Gfx::IntRect text_rect { 0, 0, static_cast<int>(ceilf(font.width(text()))), font.glyph_height() };
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

@ -205,7 +205,7 @@ void TextTool::on_second_paint(Layer const* layer, GUI::PaintEvent& event)
painter.draw_scaled_bitmap(scaled_rect, text_bitmap, text_bitmap->rect(), 1.0);
// marching ants box
auto right_padding = m_selected_font->width(" "sv);
auto right_padding = static_cast<int>(ceilf(m_selected_font->width(" "sv)));
m_ants_rect = Gfx::IntRect(text_location.translated(-4, -2), { scaled_rect.width() + 4 + right_padding, scaled_rect.height() + 4 });
m_editor->draw_marching_ants(painter, m_ants_rect);
@ -241,7 +241,7 @@ void TextTool::apply_text_to_layer()
auto text_height = m_selected_font->preferred_line_height() * static_cast<int>(m_text_editor->line_count());
painter.set_font(*m_selected_font);
auto text_rect = Gfx::Rect<int>(m_add_text_position, { text_width, text_height });
auto text_rect = Gfx::Rect<int>(m_add_text_position, { static_cast<int>(ceilf(text_width)), text_height });
painter.draw_text(text_rect, demo_text, Gfx::TextAlignment::TopLeft, m_text_color);
m_editor->did_complete_action(tool_name());
layer->did_modify_bitmap(text_rect);

View file

@ -110,6 +110,6 @@ void WelcomeWidget::paint_event(GUI::PaintEvent& event)
static auto font = Gfx::BitmapFont::load_from_file("/res/fonts/MarietaRegular24.font"sv);
painter.draw_text({ 12, 4, 1, 30 }, "Welcome to "sv, *font, Gfx::TextAlignment::CenterLeft, palette().base_text());
painter.draw_text({ 12 + font->width("Welcome to "sv), 4, 1, 30 }, "Serenity"sv, font->bold_variant(), Gfx::TextAlignment::CenterLeft, palette().base_text());
painter.draw_text({ 12 + font->width("Welcome to "sv) + font->bold_variant().width("Serenity"sv), 4, 1, 30 }, "OS"sv, font->bold_variant(), Gfx::TextAlignment::CenterLeft, palette().base() == palette().window() ? palette().base_text() : palette().base());
painter.draw_text({ 12 + static_cast<int>(ceilf(font->width("Welcome to "sv))), 4, 1, 30 }, "Serenity"sv, font->bold_variant(), Gfx::TextAlignment::CenterLeft, palette().base_text());
painter.draw_text({ 12 + static_cast<int>(ceilf(font->width("Welcome to "sv))) + static_cast<int>(ceilf(font->bold_variant().width("Serenity"sv))), 4, 1, 30 }, "OS"sv, font->bold_variant(), Gfx::TextAlignment::CenterLeft, palette().base() == palette().window() ? palette().base_text() : palette().base());
}

View file

@ -499,7 +499,7 @@ void BrickGame::paint_cell(GUI::Painter& painter, Gfx::IntRect rect, bool is_on)
void BrickGame::paint_text(GUI::Painter& painter, int row, DeprecatedString const& text)
{
auto const text_width { font().width(text) };
auto const text_width = static_cast<int>(ceilf(font().width(text)));
auto const entire_area_rect { frame_inner_rect() };
auto const margin = 4;
auto const glyph_height = font().glyph_height();

View file

@ -211,7 +211,7 @@ void ColorLines::paint_event(GUI::PaintEvent& event)
// Draw score
auto const score_text = MUST(String::formatted("{:05}"sv, m_score));
auto text_width { m_score_font->width(score_text) };
auto text_width = static_cast<int>(ceilf(m_score_font->width(score_text)));
auto const glyph_height = m_score_font->glyph_height();
auto const score_text_rect = Gfx::IntRect {
frame_inner_rect().top_left().translated(text_margin),

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 = font.width(score_text);
auto score_text_width = static_cast<int>(ceilf(font.width(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.glyph_height() / 2 },

View file

@ -175,7 +175,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, font.width("10"sv), font.glyph_height() };
auto text_rect = Gfx::IntRect { 4, 6, static_cast<int>(ceilf(font.width("10"sv))), font.glyph_height() };
painter.draw_text(text_rect, card_rank_label(rank), font, Gfx::TextAlignment::Center, suit_color);
painter.draw_bitmap(

View file

@ -109,10 +109,10 @@ void Breadcrumbbar::append_segment(DeprecatedString text, Gfx::Bitmap const* ico
auto button_width = min(button_text_width + icon_width + icon_padding + 16, max_button_width);
auto shrunken_width = icon_width + icon_padding + (icon ? 4 : 16);
button.set_max_size(button_width, 16 + 8);
button.set_max_size(static_cast<int>(ceilf(button_width)), 16 + 8);
button.set_min_size(shrunken_width, 16 + 8);
Segment segment { icon, text, data, button_width, shrunken_width, button.make_weak_ptr<GUI::Button>() };
Segment segment { icon, text, data, static_cast<int>(ceilf(button_width)), shrunken_width, button.make_weak_ptr<GUI::Button>() };
m_segments.append(move(segment));
relayout();

View file

@ -108,7 +108,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, font.width(text()), font.glyph_height() };
Gfx::IntRect text_rect { 0, 0, static_cast<int>(ceilf(font.width(text()))), font.glyph_height() };
if (text_rect.width() > content_rect.width())
text_rect.set_width(content_rect.width());
text_rect.align_within(content_rect, text_alignment());

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, font().width(m_title) + 6, font().glyph_height() };
Gfx::IntRect text_rect { 6, 1, static_cast<int>(ceilf(font().width(m_title) + 6)), font().glyph_height() };
painter.fill_rect(text_rect, palette().button());
painter.draw_text(text_rect, m_title, Gfx::TextAlignment::Center, 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() + font().width(text) + sorting_arrow_offset, sorting_arrow_offset };
Gfx::IntPoint offset { text_rect.x() + static_cast<int>(ceilf(font().width(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

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

View file

@ -46,7 +46,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() + 7, 0, font().width(text()), font().glyph_height() };
Gfx::IntRect text_rect { circle_rect.right() + 7, 0, static_cast<int>(ceilf(font().width(text()))), font().glyph_height() };
text_rect.center_vertically_within(rect());
paint_text(painter, text_rect, font(), Gfx::TextAlignment::TopLeft);

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 + font.width(title) + (icon ? (16 + 4) : 0);
auto width = 16 + static_cast<int>(ceilf(font.width(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

@ -188,7 +188,7 @@ void TreeView::traverse_in_paint_order(Callback callback) const
auto node_text = index.data().to_deprecated_string();
Gfx::IntRect rect = {
x_offset, y_offset,
icon_size() + icon_spacing() + text_padding() + font_for_index(index)->width(node_text) + text_padding(), row_height()
static_cast<int>(ceilf(icon_size() + icon_spacing() + text_padding() + font_for_index(index)->width(node_text) + text_padding())), row_height()
};
Gfx::IntRect toggle_rect;
if (row_count_at_index > 0) {

View file

@ -335,9 +335,9 @@ int BitmapFont::glyph_or_emoji_width_for_variable_width_font(u32 code_point) con
return glyph_height() * emoji->width() / emoji->height();
}
int BitmapFont::width(StringView view) const { return unicode_view_width(Utf8View(view)); }
int BitmapFont::width(Utf8View const& view) const { return unicode_view_width(view); }
int BitmapFont::width(Utf32View const& view) const { return unicode_view_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); }
template<typename T>
ALWAYS_INLINE int BitmapFont::unicode_view_width(T const& view) const

View file

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

View file

@ -140,9 +140,9 @@ public:
virtual u8 baseline() const = 0;
virtual u8 mean_line() const = 0;
virtual int width(StringView) const = 0;
virtual int width(Utf8View const&) const = 0;
virtual int width(Utf32View const&) const = 0;
virtual float width(StringView) const = 0;
virtual float width(Utf8View const&) const = 0;
virtual float width(Utf32View const&) const = 0;
virtual DeprecatedString name() const = 0;

View file

@ -10,12 +10,12 @@
namespace Gfx {
int ScaledFont::width(StringView view) const { return unicode_view_width(Utf8View(view)); }
int ScaledFont::width(Utf8View const& view) const { return unicode_view_width(view); }
int ScaledFont::width(Utf32View const& view) const { return unicode_view_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); }
template<typename T>
ALWAYS_INLINE int ScaledFont::unicode_view_width(T const& view) const
ALWAYS_INLINE float ScaledFont::unicode_view_width(T const& view) const
{
if (view.is_empty())
return 0;

View file

@ -54,9 +54,9 @@ public:
virtual u8 glyph_fixed_width() const override;
virtual u8 baseline() const override { return m_point_height; } // FIXME: Read from font
virtual u8 mean_line() const override { return m_point_height; } // FIXME: Read from font
virtual int width(StringView) const override;
virtual int width(Utf8View const&) const override;
virtual int width(Utf32View const&) const override;
virtual float width(StringView) const override;
virtual float width(Utf8View const&) const override;
virtual float width(Utf32View const&) 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; }
@ -75,7 +75,7 @@ private:
mutable HashMap<u32, RefPtr<Gfx::Bitmap>> m_cached_glyph_bitmaps;
template<typename T>
int unicode_view_width(T const& view) const;
float unicode_view_width(T const& view) const;
};
}

View file

@ -2443,7 +2443,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, font.width(name_to_draw), static_cast<int>(ceilf(font.pixel_size())) };
Gfx::IntRect text_rect { 0, 0, static_cast<int>(ceilf(font.width(name_to_draw))), static_cast<int>(ceilf(font.pixel_size())) };
text_rect.align_within(rect, text_alignment);
draw_text(text_rect, name_to_draw, font, text_alignment, color);

View file

@ -205,7 +205,7 @@ void CanvasRenderingContext2D::fill_text(DeprecatedString const& text, float x,
auto& drawing_state = this->drawing_state();
// FIXME: painter only supports integer rects for text right now, so this effectively chops off any fractional position
auto text_rect = Gfx::IntRect(x, y, max_width.has_value() ? max_width.value() : painter->font().width(text), painter->font().pixel_size());
auto text_rect = Gfx::IntRect(x, y, max_width.has_value() ? static_cast<float>(max_width.value()) : painter->font().width(text), painter->font().pixel_size());
auto transformed_rect = drawing_state.transform.map(text_rect);
painter->draw_text(transformed_rect, text, Gfx::TextAlignment::TopLeft, drawing_state.fill_style);
did_draw(transformed_rect.to_type<float>());

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, font.width(text()), font.glyph_height() };
Gfx::IntRect text_rect { 0, 0, static_cast<int>(ceilf(font.width(text()))), font.glyph_height() };
if (text_rect.width() > content_rect.width())
text_rect.set_width(content_rect.width());
text_rect.align_within(content_rect, text_alignment());

View file

@ -243,7 +243,7 @@ void WindowGeometryOverlay::update_rect()
} else {
m_label = window->rect().to_deprecated_string();
}
m_label_rect = Gfx::IntRect { 0, 0, wm.font().width(m_label) + 16, wm.font().glyph_height() + 10 };
m_label_rect = Gfx::IntRect { 0, 0, static_cast<int>(ceilf(wm.font().width(m_label))) + 16, wm.font().glyph_height() + 10 };
auto rect = calculate_frame_rect(m_label_rect).centered_within(window->frame().rect());
auto desktop_rect = wm.desktop_rect(ScreenInput::the().cursor_location_screen());