diff options
author | Andreas Kling <kling@serenityos.org> | 2022-03-28 12:17:18 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-03-30 00:57:15 +0200 |
commit | 56a284713dbabfc59a1cbb02f5546aa67f64c3b1 (patch) | |
tree | 699981f8ede39754dd6f5a6751195d2056b5a027 /Userland/Libraries/LibGfx | |
parent | 7850628ff1029a4a6eeb0b4f1db3eb15e4e26f97 (diff) | |
download | serenity-56a284713dbabfc59a1cbb02f5546aa67f64c3b1.zip |
LibGfx: Add ascent, descent and line gap to FontPixelMetrics
These are the main values we need in LibWeb to perform inline layout.
Diffstat (limited to 'Userland/Libraries/LibGfx')
-rw-r--r-- | Userland/Libraries/LibGfx/BitmapFont.cpp | 3 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Font.h | 11 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/TrueTypeFont/Font.cpp | 16 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/TrueTypeFont/Font.h | 7 |
4 files changed, 27 insertions, 10 deletions
diff --git a/Userland/Libraries/LibGfx/BitmapFont.cpp b/Userland/Libraries/LibGfx/BitmapFont.cpp index 166e94caa8..3a4d3b416e 100644 --- a/Userland/Libraries/LibGfx/BitmapFont.cpp +++ b/Userland/Libraries/LibGfx/BitmapFont.cpp @@ -379,6 +379,9 @@ FontPixelMetrics BitmapFont::pixel_metrics() const .x_height = (float)x_height(), .advance_of_ascii_zero = (float)glyph_width('0'), .glyph_spacing = (float)glyph_spacing(), + .ascent = (float)m_baseline, + .descent = (float)(m_glyph_height - m_baseline), + .line_gap = (float)pixel_size() * 0.4f, // FIXME: Do something nicer here. }; } diff --git a/Userland/Libraries/LibGfx/Font.h b/Userland/Libraries/LibGfx/Font.h index 7e4a9a2f2e..1f24f22bde 100644 --- a/Userland/Libraries/LibGfx/Font.h +++ b/Userland/Libraries/LibGfx/Font.h @@ -91,6 +91,17 @@ struct FontPixelMetrics { float x_height { 0 }; float advance_of_ascii_zero { 0 }; float glyph_spacing { 0 }; + + // Number of pixels the font extends above the baseline. + float ascent { 0 }; + + // Number of pixels the font descends below the baseline. + float descent { 0 }; + + // Line gap specified by font. + float line_gap { 0 }; + + float line_spacing() const { return roundf(ascent) + roundf(descent) + roundf(line_gap); } }; class Font : public RefCounted<Font> { diff --git a/Userland/Libraries/LibGfx/TrueTypeFont/Font.cpp b/Userland/Libraries/LibGfx/TrueTypeFont/Font.cpp index e4caca2d47..cc4bf48a85 100644 --- a/Userland/Libraries/LibGfx/TrueTypeFont/Font.cpp +++ b/Userland/Libraries/LibGfx/TrueTypeFont/Font.cpp @@ -526,17 +526,16 @@ ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_offset(ReadonlyBytes buffer, u3 return adopt_ref(*new Font(move(buffer), move(head), move(name), move(hhea), move(maxp), move(hmtx), move(cmap), move(loca), move(glyf), move(os2), move(kern))); } -ScaledFontMetrics Font::metrics(float x_scale, float y_scale) const +ScaledFontMetrics Font::metrics([[maybe_unused]] float x_scale, float y_scale) const { auto ascender = m_hhea.ascender() * y_scale; auto descender = m_hhea.descender() * y_scale; auto line_gap = m_hhea.line_gap() * y_scale; - auto advance_width_max = m_hhea.advance_width_max() * x_scale; + return ScaledFontMetrics { - .ascender = (int)roundf(ascender), - .descender = (int)roundf(descender), - .line_gap = (int)roundf(line_gap), - .advance_width_max = (int)roundf(advance_width_max), + .ascender = ascender, + .descender = descender, + .line_gap = line_gap, }; } @@ -726,11 +725,16 @@ 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, }; } diff --git a/Userland/Libraries/LibGfx/TrueTypeFont/Font.h b/Userland/Libraries/LibGfx/TrueTypeFont/Font.h index 2e9212d93a..1b63ffe07b 100644 --- a/Userland/Libraries/LibGfx/TrueTypeFont/Font.h +++ b/Userland/Libraries/LibGfx/TrueTypeFont/Font.h @@ -23,10 +23,9 @@ namespace TTF { struct ScaledFontMetrics { - int ascender; - int descender; - int line_gap; - int advance_width_max; + float ascender { 0 }; + float descender { 0 }; + float line_gap { 0 }; int height() const { |