1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 14:07:45 +00:00

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.
This commit is contained in:
thankyouverycool 2021-09-23 19:34:15 -04:00 committed by Andreas Kling
parent fde48f1a7a
commit 39484fc02c
2 changed files with 18 additions and 22 deletions

View file

@ -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) constexpr u16 bold_bit { 1 };
auto variant_name = variant(); if (m_os2.weight_class())
return m_os2.weight_class();
if (variant_name == "Thin") if (m_head.style() & bold_bit)
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")
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));

View file

@ -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;