From 39484fc02ced5804d3d5625fba8955ae819dd1f9 Mon Sep 17 00:00:00 2001 From: thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> Date: Thu, 23 Sep 2021 19:34:15 -0400 Subject: [PATCH] LibGfx: Get weight from tables for TrueTypeFonts First, try to find detailed weight metrics in the OS/2 table; barring that, fall back to the font header table. --- .../Libraries/LibGfx/TrueTypeFont/Font.cpp | 36 ++++++++----------- .../Libraries/LibGfx/TrueTypeFont/Tables.h | 4 +++ 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/Userland/Libraries/LibGfx/TrueTypeFont/Font.cpp b/Userland/Libraries/LibGfx/TrueTypeFont/Font.cpp index 087cb85093..cc89ffbfde 100644 --- a/Userland/Libraries/LibGfx/TrueTypeFont/Font.cpp +++ b/Userland/Libraries/LibGfx/TrueTypeFont/Font.cpp @@ -84,6 +84,11 @@ i16 Head::ymax() const return be_i16(m_slice.offset_pointer((u32)Offsets::YMax)); } +u16 Head::style() const +{ + return be_u16(m_slice.offset_pointer((u32)Offsets::Style)); +} + u16 Head::lowest_recommended_ppem() const { return be_u16(m_slice.offset_pointer((u32)Offsets::LowestRecPPEM)); @@ -462,29 +467,11 @@ String Font::variant() const u16 Font::weight() const { - // FIXME: This is pretty naive, read weight from the actual font table(s) - auto variant_name = variant(); - - if (variant_name == "Thin") - return 100; - if (variant_name == "Extra Light") - return 200; - if (variant_name == "Light") - return 300; - if (variant_name == "Regular") - return 400; - if (variant_name == "Medium") - return 500; - if (variant_name == "Semi Bold") - return 600; - if (variant_name == "Bold") + constexpr u16 bold_bit { 1 }; + if (m_os2.weight_class()) + return m_os2.weight_class(); + if (m_head.style() & bold_bit) return 700; - if (variant_name == "Extra Bold") - return 800; - if (variant_name == "Black") - return 900; - if (variant_name == "Extra Black") - return 950; return 400; } @@ -559,6 +546,11 @@ u8 ScaledFont::glyph_fixed_width() const return glyph_metrics(glyph_id_for_code_point(' ')).advance_width; } +u16 OS2::weight_class() const +{ + return be_u16(m_slice.offset_pointer((u32)Offsets::WeightClass)); +} + i16 OS2::typographic_ascender() const { return be_i16(m_slice.offset_pointer((u32)Offsets::TypographicAscender)); diff --git a/Userland/Libraries/LibGfx/TrueTypeFont/Tables.h b/Userland/Libraries/LibGfx/TrueTypeFont/Tables.h index 2c7a5a7d29..f60b83a5ff 100644 --- a/Userland/Libraries/LibGfx/TrueTypeFont/Tables.h +++ b/Userland/Libraries/LibGfx/TrueTypeFont/Tables.h @@ -23,6 +23,7 @@ public: i16 ymin() const; i16 xmax() const; i16 ymax() const; + u16 style() const; u16 lowest_recommended_ppem() const; IndexToLocFormat index_to_loc_format() const; @@ -33,6 +34,7 @@ private: YMin = 38, XMax = 40, YMax = 42, + Style = 44, LowestRecPPEM = 46, IndexToLocFormat = 50, }; @@ -129,11 +131,13 @@ private: class OS2 { public: enum class Offsets { + WeightClass = 4, TypographicAscender = 68, TypographicDescender = 70, TypographicLineGap = 72, }; + u16 weight_class() const; i16 typographic_ascender() const; i16 typographic_descender() const; i16 typographic_line_gap() const;