1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:47:34 +00:00

LibGfx: Cache font pixel metrics in ScaledFont

Instead of recomputing the pixel metrics over and over, we can just
cache them with the font and avoid a bunch of expensive computation.
This commit is contained in:
Andreas Kling 2023-01-16 09:26:01 +01:00
parent 83488cd102
commit 850b4a03e6
2 changed files with 26 additions and 21 deletions

View file

@ -23,17 +23,9 @@ struct GlyphIndexWithSubpixelOffset {
bool operator==(GlyphIndexWithSubpixelOffset const&) const = default;
};
class ScaledFont : public Gfx::Font {
class ScaledFont final : public Gfx::Font {
public:
ScaledFont(NonnullRefPtr<VectorFont> font, float point_width, float point_height, unsigned dpi_x = DEFAULT_DPI, unsigned dpi_y = DEFAULT_DPI)
: m_font(move(font))
, m_point_width(point_width)
, m_point_height(point_height)
{
float units_per_em = m_font->units_per_em();
m_x_scale = (point_width * dpi_x) / (POINTS_PER_INCH * units_per_em);
m_y_scale = (point_height * dpi_y) / (POINTS_PER_INCH * units_per_em);
}
ScaledFont(NonnullRefPtr<VectorFont>, float point_width, float point_height, unsigned dpi_x = DEFAULT_DPI, unsigned dpi_y = DEFAULT_DPI);
u32 glyph_id_for_code_point(u32 code_point) const { return m_font->glyph_id_for_code_point(code_point); }
ScaledFontMetrics metrics() const { return m_font->metrics(m_x_scale, m_y_scale); }
ScaledGlyphMetrics glyph_metrics(u32 glyph_id) const { return m_font->glyph_metrics(glyph_id, m_x_scale, m_y_scale); }
@ -81,6 +73,7 @@ private:
float m_point_width { 0.0f };
float m_point_height { 0.0f };
mutable HashMap<GlyphIndexWithSubpixelOffset, RefPtr<Gfx::Bitmap>> m_cached_glyph_bitmaps;
Gfx::FontPixelMetrics m_pixel_metrics;
template<typename T>
float unicode_view_width(T const& view) const;