소스 검색

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.
thankyouverycool 3 년 전
부모
커밋
39484fc02c
2개의 변경된 파일18개의 추가작업 그리고 22개의 파일을 삭제
  1. 14 22
      Userland/Libraries/LibGfx/TrueTypeFont/Font.cpp
  2. 4 0
      Userland/Libraries/LibGfx/TrueTypeFont/Tables.h

+ 14 - 22
Userland/Libraries/LibGfx/TrueTypeFont/Font.cpp

@@ -84,6 +84,11 @@ i16 Head::ymax() const
     return be_i16(m_slice.offset_pointer((u32)Offsets::YMax));
     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
 u16 Head::lowest_recommended_ppem() const
 {
 {
     return be_u16(m_slice.offset_pointer((u32)Offsets::LowestRecPPEM));
     return be_u16(m_slice.offset_pointer((u32)Offsets::LowestRecPPEM));
@@ -462,29 +467,11 @@ String Font::variant() const
 
 
 u16 Font::weight() 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;
         return 700;
-    if (variant_name == "Extra Bold")
-        return 800;
-    if (variant_name == "Black")
-        return 900;
-    if (variant_name == "Extra Black")
-        return 950;
 
 
     return 400;
     return 400;
 }
 }
@@ -559,6 +546,11 @@ u8 ScaledFont::glyph_fixed_width() const
     return glyph_metrics(glyph_id_for_code_point(' ')).advance_width;
     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
 i16 OS2::typographic_ascender() const
 {
 {
     return be_i16(m_slice.offset_pointer((u32)Offsets::TypographicAscender));
     return be_i16(m_slice.offset_pointer((u32)Offsets::TypographicAscender));

+ 4 - 0
Userland/Libraries/LibGfx/TrueTypeFont/Tables.h

@@ -23,6 +23,7 @@ public:
     i16 ymin() const;
     i16 ymin() const;
     i16 xmax() const;
     i16 xmax() const;
     i16 ymax() const;
     i16 ymax() const;
+    u16 style() const;
     u16 lowest_recommended_ppem() const;
     u16 lowest_recommended_ppem() const;
     IndexToLocFormat index_to_loc_format() const;
     IndexToLocFormat index_to_loc_format() const;
 
 
@@ -33,6 +34,7 @@ private:
         YMin = 38,
         YMin = 38,
         XMax = 40,
         XMax = 40,
         YMax = 42,
         YMax = 42,
+        Style = 44,
         LowestRecPPEM = 46,
         LowestRecPPEM = 46,
         IndexToLocFormat = 50,
         IndexToLocFormat = 50,
     };
     };
@@ -129,11 +131,13 @@ private:
 class OS2 {
 class OS2 {
 public:
 public:
     enum class Offsets {
     enum class Offsets {
+        WeightClass = 4,
         TypographicAscender = 68,
         TypographicAscender = 68,
         TypographicDescender = 70,
         TypographicDescender = 70,
         TypographicLineGap = 72,
         TypographicLineGap = 72,
     };
     };
 
 
+    u16 weight_class() const;
     i16 typographic_ascender() const;
     i16 typographic_ascender() const;
     i16 typographic_descender() const;
     i16 typographic_descender() const;
     i16 typographic_line_gap() const;
     i16 typographic_line_gap() const;