LibWeb: Use Gfx::Font::pixel_size() when we want pixel metrics

This gives us consistent results with both bitmap and scalable fonts.
This commit is contained in:
Andreas Kling 2022-03-27 00:58:52 +01:00
parent 65629d26fe
commit b0208f38f6
Notes: sideshowbarker 2024-07-17 16:42:05 +09:00
3 changed files with 6 additions and 6 deletions

View file

@ -240,7 +240,7 @@ void CanvasRenderingContext2D::fill_text(const String& text, float x, float y, O
return; return;
// FIXME: painter only supports integer rects for text right now, so this effectively chops off any fractional position // 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().glyph_height()); auto text_rect = Gfx::IntRect(x, y, max_width.has_value() ? max_width.value() : painter->font().width(text), painter->font().pixel_size());
auto transformed_rect = m_drawing_state.transform.map(text_rect); auto transformed_rect = m_drawing_state.transform.map(text_rect);
painter->draw_text(transformed_rect, text, Gfx::TextAlignment::TopLeft, m_drawing_state.fill_style); painter->draw_text(transformed_rect, text, Gfx::TextAlignment::TopLeft, m_drawing_state.fill_style);
did_draw(transformed_rect.to_type<float>()); did_draw(transformed_rect.to_type<float>());
@ -586,7 +586,7 @@ CanvasRenderingContext2D::PreparedText CanvasRenderingContext2D::prepare_text(St
// FIXME: Once we have CanvasTextDrawingStyles, add the CSS attributes. // FIXME: Once we have CanvasTextDrawingStyles, add the CSS attributes.
auto& font = Gfx::FontDatabase::default_font(); auto& font = Gfx::FontDatabase::default_font();
size_t width = 0; size_t width = 0;
size_t height = font.glyph_height(); size_t height = font.pixel_size();
for (auto c : Utf8View { replaced_text }) { for (auto c : Utf8View { replaced_text }) {
width += font.glyph_or_emoji_width(c); width += font.glyph_or_emoji_width(c);
} }

View file

@ -61,7 +61,7 @@ void ImageBox::prepare_for_replaced_layout()
if (alt.is_empty()) if (alt.is_empty())
alt = image_element.src(); alt = image_element.src();
set_intrinsic_width(font.width(alt) + 16); set_intrinsic_width(font.width(alt) + 16);
set_intrinsic_height(font.glyph_height() + 16); set_intrinsic_height(font.pixel_size() + 16);
} }
if (!has_intrinsic_width() && !has_intrinsic_height()) { if (!has_intrinsic_width() && !has_intrinsic_height()) {

View file

@ -147,7 +147,7 @@ void PaintableBox::paint(PaintContext& context, PaintPhase phase) const
size_text_rect.set_y(border_rect.y() + border_rect.height()); size_text_rect.set_y(border_rect.y() + border_rect.height());
size_text_rect.set_top(size_text_rect.top()); size_text_rect.set_top(size_text_rect.top());
size_text_rect.set_width((float)context.painter().font().width(size_text) + 4); size_text_rect.set_width((float)context.painter().font().width(size_text) + 4);
size_text_rect.set_height(context.painter().font().glyph_height() + 4); size_text_rect.set_height(context.painter().font().pixel_size() + 4);
context.painter().fill_rect(enclosing_int_rect(size_text_rect), context.palette().color(Gfx::ColorRole::Tooltip)); context.painter().fill_rect(enclosing_int_rect(size_text_rect), context.palette().color(Gfx::ColorRole::Tooltip));
context.painter().draw_rect(enclosing_int_rect(size_text_rect), context.palette().threed_shadow1()); context.painter().draw_rect(enclosing_int_rect(size_text_rect), context.palette().threed_shadow1());
context.painter().draw_text(enclosing_int_rect(size_text_rect), size_text, Gfx::TextAlignment::Center, context.palette().color(Gfx::ColorRole::TooltipText)); context.painter().draw_text(enclosing_int_rect(size_text_rect), size_text, Gfx::TextAlignment::Center, context.palette().color(Gfx::ColorRole::TooltipText));
@ -285,7 +285,7 @@ static void paint_text_decoration(Gfx::Painter& painter, Layout::Node const& tex
auto& font = fragment.layout_node().font(); auto& font = fragment.layout_node().font();
auto fragment_box = enclosing_int_rect(fragment.absolute_rect()); auto fragment_box = enclosing_int_rect(fragment.absolute_rect());
auto glyph_height = font.glyph_height(); auto glyph_height = font.pixel_size();
auto baseline = fragment_box.height() / 2 - (glyph_height + 4) / 2 + glyph_height; auto baseline = fragment_box.height() / 2 - (glyph_height + 4) / 2 + glyph_height;
switch (text_node.computed_values().text_decoration_line()) { switch (text_node.computed_values().text_decoration_line()) {
@ -377,7 +377,7 @@ static void paint_text_fragment(PaintContext& context, Layout::TextNode const& t
// FIXME: This is a hack to prevent text clipping when painting a bitmap font into a too-small box. // FIXME: This is a hack to prevent text clipping when painting a bitmap font into a too-small box.
auto draw_rect = enclosing_int_rect(fragment_absolute_rect); auto draw_rect = enclosing_int_rect(fragment_absolute_rect);
draw_rect.set_height(max(draw_rect.height(), text_node.font().glyph_height())); draw_rect.set_height(max(draw_rect.height(), text_node.font().pixel_size()));
painter.draw_text(draw_rect, text.substring_view(fragment.start(), fragment.length()), Gfx::TextAlignment::CenterLeft, text_node.computed_values().color()); painter.draw_text(draw_rect, text.substring_view(fragment.start(), fragment.length()), Gfx::TextAlignment::CenterLeft, text_node.computed_values().color());
auto selection_rect = fragment.selection_rect(text_node.font()); auto selection_rect = fragment.selection_rect(text_node.font());