From 87a149c9a42696c9df1bcbaf9fd4477db5df17d5 Mon Sep 17 00:00:00 2001 From: thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> Date: Mon, 31 Jan 2022 20:10:30 -0500 Subject: [PATCH] LibGfx: Determine slope attributes for TrueTypeFonts from tables First checks the OS/2 table for an oblique selection flag before falling back on italic flags. --- Userland/Libraries/LibGfx/BitmapFont.h | 2 +- Userland/Libraries/LibGfx/Font.h | 1 + .../Libraries/LibGfx/TrueTypeFont/Font.cpp | 23 +++++++++++++++++++ Userland/Libraries/LibGfx/TrueTypeFont/Font.h | 2 ++ .../Libraries/LibGfx/TrueTypeFont/Tables.h | 2 ++ 5 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibGfx/BitmapFont.h b/Userland/Libraries/LibGfx/BitmapFont.h index 632ddd2f32..b41c7def6e 100644 --- a/Userland/Libraries/LibGfx/BitmapFont.h +++ b/Userland/Libraries/LibGfx/BitmapFont.h @@ -37,7 +37,7 @@ public: u16 weight() const override { return m_weight; } void set_weight(u16 weight) { m_weight = weight; } - u8 slope() const { return m_slope; } + virtual u8 slope() const override { return m_slope; } void set_slope(u8 slope) { m_slope = slope; } Glyph glyph(u32 code_point) const override; diff --git a/Userland/Libraries/LibGfx/Font.h b/Userland/Libraries/LibGfx/Font.h index ab2974de52..da9fec975b 100644 --- a/Userland/Libraries/LibGfx/Font.h +++ b/Userland/Libraries/LibGfx/Font.h @@ -101,6 +101,7 @@ public: FontMetrics metrics(u32 code_point) const; virtual u8 presentation_size() const = 0; + virtual u8 slope() const = 0; virtual u16 weight() const = 0; virtual Glyph glyph(u32 code_point) const = 0; diff --git a/Userland/Libraries/LibGfx/TrueTypeFont/Font.cpp b/Userland/Libraries/LibGfx/TrueTypeFont/Font.cpp index dc5949da58..8cdb37d0dd 100644 --- a/Userland/Libraries/LibGfx/TrueTypeFont/Font.cpp +++ b/Userland/Libraries/LibGfx/TrueTypeFont/Font.cpp @@ -474,6 +474,24 @@ u16 Font::weight() const return 400; } +u8 Font::slope() const +{ + // https://docs.microsoft.com/en-us/typography/opentype/spec/os2 + constexpr u16 italic_selection_bit { 1 }; + constexpr u16 oblique_selection_bit { 512 }; + // https://docs.microsoft.com/en-us/typography/opentype/spec/head + constexpr u16 italic_style_bit { 2 }; + + if (m_os2.selection() & oblique_selection_bit) + return 2; + if (m_os2.selection() & italic_selection_bit) + return 1; + if (m_head.style() & italic_style_bit) + return 1; + + return 0; +} + bool Font::is_fixed_width() const { // FIXME: Read this information from the font file itself. @@ -549,6 +567,11 @@ u16 OS2::weight_class() const return be_u16(m_slice.offset_pointer((u32)Offsets::WeightClass)); } +u16 OS2::selection() const +{ + return be_u16(m_slice.offset_pointer((u32)Offsets::Selection)); +} + i16 OS2::typographic_ascender() const { return be_i16(m_slice.offset_pointer((u32)Offsets::TypographicAscender)); diff --git a/Userland/Libraries/LibGfx/TrueTypeFont/Font.h b/Userland/Libraries/LibGfx/TrueTypeFont/Font.h index 3f4341d850..7bba4765ad 100644 --- a/Userland/Libraries/LibGfx/TrueTypeFont/Font.h +++ b/Userland/Libraries/LibGfx/TrueTypeFont/Font.h @@ -57,6 +57,7 @@ public: String family() const; String variant() const; u16 weight() const; + u8 slope() const; bool is_fixed_width() const; private: @@ -122,6 +123,7 @@ public: // Gfx::Font implementation virtual NonnullRefPtr clone() const override { return *this; } // FIXME: clone() should not need to be implemented virtual u8 presentation_size() const override { return m_point_height; } + virtual u8 slope() const override { return m_font->slope(); } virtual u16 weight() const override { return m_font->weight(); } virtual Gfx::Glyph glyph(u32 code_point) const override; virtual bool contains_glyph(u32 code_point) const override { return m_font->glyph_id_for_code_point(code_point) > 0; } diff --git a/Userland/Libraries/LibGfx/TrueTypeFont/Tables.h b/Userland/Libraries/LibGfx/TrueTypeFont/Tables.h index 7165743f64..182e2493ca 100644 --- a/Userland/Libraries/LibGfx/TrueTypeFont/Tables.h +++ b/Userland/Libraries/LibGfx/TrueTypeFont/Tables.h @@ -133,12 +133,14 @@ class OS2 { public: enum class Offsets { WeightClass = 4, + Selection = 62, TypographicAscender = 68, TypographicDescender = 70, TypographicLineGap = 72, }; u16 weight_class() const; + u16 selection() const; i16 typographic_ascender() const; i16 typographic_descender() const; i16 typographic_line_gap() const;