mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 06:07: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:
parent
83488cd102
commit
850b4a03e6
2 changed files with 26 additions and 21 deletions
|
@ -10,6 +10,28 @@
|
|||
|
||||
namespace Gfx {
|
||||
|
||||
ScaledFont::ScaledFont(NonnullRefPtr<VectorFont> font, float point_width, float point_height, unsigned dpi_x, unsigned dpi_y)
|
||||
: 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);
|
||||
|
||||
auto metrics = m_font->metrics(m_x_scale, m_y_scale);
|
||||
|
||||
m_pixel_metrics = Gfx::FontPixelMetrics {
|
||||
.size = (float)pixel_size(),
|
||||
.x_height = (float)x_height(),
|
||||
.advance_of_ascii_zero = (float)glyph_width('0'),
|
||||
.glyph_spacing = (float)glyph_spacing(),
|
||||
.ascent = metrics.ascender,
|
||||
.descent = metrics.descender,
|
||||
.line_gap = metrics.line_gap,
|
||||
};
|
||||
}
|
||||
|
||||
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); }
|
||||
|
@ -103,17 +125,7 @@ u8 ScaledFont::glyph_fixed_width() const
|
|||
|
||||
Gfx::FontPixelMetrics ScaledFont::pixel_metrics() const
|
||||
{
|
||||
auto metrics = m_font->metrics(m_x_scale, m_y_scale);
|
||||
|
||||
return Gfx::FontPixelMetrics {
|
||||
.size = (float)pixel_size(),
|
||||
.x_height = (float)x_height(),
|
||||
.advance_of_ascii_zero = (float)glyph_width('0'),
|
||||
.glyph_spacing = (float)glyph_spacing(),
|
||||
.ascent = metrics.ascender,
|
||||
.descent = metrics.descender,
|
||||
.line_gap = metrics.line_gap,
|
||||
};
|
||||
return m_pixel_metrics;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue