LibGfx/OpenType: Make glyph_width() only fetch the glyph advance

Instead of fetching a generic set of metrics for each glyph, only fetch
the advance when that's all we need.

This is extremely hot in LibWeb text layout, where it makes a nice dent.
This commit is contained in:
Andreas Kling 2024-03-24 16:49:13 +01:00
parent 2b8a920a7c
commit f48024c2d1
Notes: sideshowbarker 2024-07-17 17:06:59 +09:00
6 changed files with 20 additions and 2 deletions

View file

@ -413,6 +413,21 @@ Font::EmbeddedBitmapData Font::embedded_bitmap_data_for_glyph(u32 glyph_id) cons
return Empty {};
}
float Font::glyph_advance(u32 glyph_id, float x_scale, float y_scale, float point_width, float point_height) const
{
if (has_color_bitmaps())
return glyph_metrics(glyph_id, x_scale, y_scale, point_width, point_height).advance_width;
if (!m_hmtx.has_value())
return 0;
if (glyph_id >= glyph_count())
glyph_id = 0;
auto horizontal_metrics = m_hmtx->get_glyph_horizontal_metrics(glyph_id);
return static_cast<float>(horizontal_metrics.advance_width) * x_scale;
}
Gfx::ScaledGlyphMetrics Font::glyph_metrics(u32 glyph_id, float x_scale, float y_scale, float point_width, float point_height) const
{
auto embedded_bitmap_metrics = embedded_bitmap_data_for_glyph(glyph_id).visit(

View file

@ -54,6 +54,7 @@ public:
virtual Gfx::ScaledFontMetrics metrics(float x_scale, float y_scale) const override;
virtual Gfx::ScaledGlyphMetrics glyph_metrics(u32 glyph_id, float x_scale, float y_scale, float point_width, float point_height) const override;
virtual float glyph_advance(u32 glyph_id, float x_scale, float y_scale, float point_width, float point_height) const override;
virtual float glyphs_horizontal_kerning(u32 left_glyph_id, u32 right_glyph_id, float x_scale) const override;
virtual RefPtr<Gfx::Bitmap> rasterize_glyph(u32 glyph_id, float x_scale, float y_scale, Gfx::GlyphSubpixelOffset) const override;
virtual bool append_glyph_path_to(Gfx::Path&, u32 glyph_id, float x_scale, float y_scale) const override;

View file

@ -110,8 +110,7 @@ float ScaledFont::glyph_left_bearing(u32 code_point) const
float ScaledFont::glyph_width(u32 code_point) const
{
auto id = glyph_id_for_code_point(code_point);
auto metrics = glyph_metrics(id);
return metrics.advance_width;
return m_font->glyph_advance(id, m_x_scale, m_y_scale, m_point_width, m_point_height);
}
template<typename CodePointIterator>

View file

@ -44,6 +44,7 @@ public:
virtual ~VectorFont();
virtual ScaledFontMetrics metrics(float x_scale, float y_scale) const = 0;
virtual ScaledGlyphMetrics glyph_metrics(u32 glyph_id, float x_scale, float y_scale, float point_width, float point_height) const = 0;
virtual float glyph_advance(u32 glyph_id, float x_scale, float y_scale, float point_width, float point_height) const = 0;
virtual float glyphs_horizontal_kerning(u32 left_glyph_id, u32 right_glyph_id, float x_scale) const = 0;
virtual RefPtr<Gfx::Bitmap> rasterize_glyph(u32 glyph_id, float x_scale, float y_scale, GlyphSubpixelOffset) const = 0;
virtual bool append_glyph_path_to(Gfx::Path&, u32 glyph_id, float x_scale, float y_scale) const = 0;

View file

@ -26,6 +26,7 @@ public:
virtual Gfx::ScaledFontMetrics metrics(float x_scale, float y_scale) const override { return m_input_font->metrics(x_scale, y_scale); }
virtual Gfx::ScaledGlyphMetrics glyph_metrics(u32 glyph_id, float x_scale, float y_scale, float point_width, float point_height) const override { return m_input_font->glyph_metrics(glyph_id, x_scale, y_scale, point_width, point_height); }
virtual float glyph_advance(u32 glyph_id, float x_scale, float y_scale, float point_width, float point_height) const override { return m_input_font->glyph_advance(glyph_id, x_scale, y_scale, point_width, point_height); }
virtual float glyphs_horizontal_kerning(u32 left_glyph_id, u32 right_glyph_id, float x_scale) const override { return m_input_font->glyphs_horizontal_kerning(left_glyph_id, right_glyph_id, x_scale); }
virtual RefPtr<Gfx::Bitmap> rasterize_glyph(u32 glyph_id, float x_scale, float y_scale, Gfx::GlyphSubpixelOffset subpixel_offset) const override { return m_input_font->rasterize_glyph(glyph_id, x_scale, y_scale, subpixel_offset); }
virtual bool append_glyph_path_to(Gfx::Path& path, u32 glyph_id, float x_scale, float y_scale) const override { return m_input_font->append_glyph_path_to(path, glyph_id, x_scale, y_scale); }

View file

@ -27,6 +27,7 @@ public:
virtual Gfx::ScaledFontMetrics metrics(float x_scale, float y_scale) const override { return m_input_font->metrics(x_scale, y_scale); }
virtual Gfx::ScaledGlyphMetrics glyph_metrics(u32 glyph_id, float x_scale, float y_scale, float point_width, float point_height) const override { return m_input_font->glyph_metrics(glyph_id, x_scale, y_scale, point_width, point_height); }
virtual float glyph_advance(u32 glyph_id, float x_scale, float y_scale, float point_width, float point_height) const override { return m_input_font->glyph_advance(glyph_id, x_scale, y_scale, point_width, point_height); }
virtual float glyphs_horizontal_kerning(u32 left_glyph_id, u32 right_glyph_id, float x_scale) const override { return m_input_font->glyphs_horizontal_kerning(left_glyph_id, right_glyph_id, x_scale); }
virtual RefPtr<Gfx::Bitmap> rasterize_glyph(u32 glyph_id, float x_scale, float y_scale, Gfx::GlyphSubpixelOffset subpixel_offset) const override
{