mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 03:17:35 +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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue