diff options
author | Andreas Kling <kling@serenityos.org> | 2023-01-04 19:27:25 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-01-06 12:02:20 +0100 |
commit | bb0c5f8ab47ed64a2caee3287ce9509f238f8434 (patch) | |
tree | 0f322b21385b6b8222471540d273a25d9c841e40 /Userland/Libraries | |
parent | c3018f8beba19fa44967578099e1c3d1732ce830 (diff) | |
download | serenity-bb0c5f8ab47ed64a2caee3287ce9509f238f8434.zip |
LibGfx/OpenType: Use typographic metrics when asked to by the OS/2 table
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibGfx/Font/OpenType/Font.cpp | 41 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Font/OpenType/Tables.h | 2 |
2 files changed, 36 insertions, 7 deletions
diff --git a/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp b/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp index 7535d735bc..fbf01b04c8 100644 --- a/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp +++ b/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp @@ -518,14 +518,24 @@ ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_offset(ReadonlyBytes buffer, u3 Gfx::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; + i16 raw_ascender; + i16 raw_descender; + i16 raw_line_gap; + + if (m_os2.has_value() && m_os2->use_typographic_metrics()) { + raw_ascender = m_os2->typographic_ascender(); + raw_descender = m_os2->typographic_descender(); + raw_line_gap = m_os2->typographic_line_gap(); + } else { + raw_ascender = m_hhea.ascender(); + raw_descender = m_hhea.descender(); + raw_line_gap = m_hhea.line_gap(); + } return Gfx::ScaledFontMetrics { - .ascender = ascender, - .descender = descender, - .line_gap = line_gap, + .ascender = static_cast<float>(raw_ascender) * y_scale, + .descender = static_cast<float>(raw_descender) * y_scale, + .line_gap = static_cast<float>(raw_line_gap) * y_scale, }; } @@ -561,7 +571,19 @@ RefPtr<Gfx::Bitmap> Font::rasterize_glyph(u32 glyph_id, float x_scale, float y_s } auto glyph_offset = m_loca.get_glyph_offset(glyph_id); auto glyph = m_glyf.glyph(glyph_offset); - return glyph.rasterize(m_hhea.ascender(), m_hhea.descender(), x_scale, y_scale, subpixel_offset, [&](u16 glyph_id) { + + i16 ascender = 0; + i16 descender = 0; + + if (m_os2.has_value() && m_os2->use_typographic_metrics()) { + ascender = m_os2->typographic_ascender(); + descender = m_os2->typographic_descender(); + } else { + ascender = m_hhea.ascender(); + descender = m_hhea.descender(); + } + + return glyph.rasterize(ascender, descender, x_scale, y_scale, subpixel_offset, [&](u16 glyph_id) { if (glyph_id >= glyph_count()) { glyph_id = 0; } @@ -657,4 +679,9 @@ i16 OS2::typographic_line_gap() const return header().s_typo_line_gap; } +bool OS2::use_typographic_metrics() const +{ + return header().fs_selection & 0x80; +} + } diff --git a/Userland/Libraries/LibGfx/Font/OpenType/Tables.h b/Userland/Libraries/LibGfx/Font/OpenType/Tables.h index fadb0636d5..30e29c0d1e 100644 --- a/Userland/Libraries/LibGfx/Font/OpenType/Tables.h +++ b/Userland/Libraries/LibGfx/Font/OpenType/Tables.h @@ -206,6 +206,8 @@ public: i16 typographic_descender() const; i16 typographic_line_gap() const; + bool use_typographic_metrics() const; + explicit OS2(ReadonlyBytes slice) : m_slice(slice) { |