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

LibGfx: Pass font width to FontDatabase::get()

Width need to be passed to `FontDatabase::get()` to resolve font name
unambiguously.
This commit is contained in:
Aliaksandr Kalenik 2023-02-04 23:00:34 +03:00 committed by Sam Atkins
parent 79006c03b4
commit 1f4106842d
21 changed files with 71 additions and 21 deletions

View file

@ -385,7 +385,7 @@ Font const& Font::bold_variant() const
{
if (m_bold_variant)
return *m_bold_variant;
m_bold_variant = Gfx::FontDatabase::the().get(family(), presentation_size(), 700, 0);
m_bold_variant = Gfx::FontDatabase::the().get(family(), presentation_size(), 700, Gfx::FontWidth::Normal, 0);
if (!m_bold_variant)
m_bold_variant = this;
return *m_bold_variant;

View file

@ -44,6 +44,8 @@ public:
virtual float pixel_size() const override { return m_glyph_height; }
u16 width() const override { return FontWidth::Normal; }
u16 weight() const override { return m_weight; }
void set_weight(u16 weight) { m_weight = weight; }

View file

@ -127,6 +127,19 @@ struct FontPixelMetrics {
float line_spacing() const { return ascent + descent + line_gap; }
};
// https://learn.microsoft.com/en-us/typography/opentype/spec/os2#uswidthclass
enum FontWidth {
UltraCondensed = 1,
ExtraCondensed = 2,
Condensed = 3,
SemiCondensed = 4,
Normal = 5,
SemiExpanded = 6,
Expanded = 7,
ExtraExpanded = 8,
UltraExpanded = 9
};
class Font : public RefCounted<Font> {
public:
enum class AllowInexactSizeMatch {
@ -144,6 +157,8 @@ public:
virtual float pixel_size() const = 0;
virtual u8 slope() const = 0;
virtual u16 width() const = 0;
virtual u16 weight() const = 0;
virtual Glyph glyph(u32 code_point) const = 0;
virtual Glyph glyph(u32 code_point, GlyphSubpixelOffset) const = 0;

View file

@ -206,7 +206,7 @@ RefPtr<Gfx::Font> FontDatabase::get_by_name(StringView name)
auto weight = parts.take_last().to_int().value_or(0);
auto size = parts.take_last().to_int().value_or(0);
auto family = DeprecatedString::join(' ', parts);
return get(family, size, weight, slope);
return get(family, size, weight, Gfx::FontWidth::Normal, slope);
}
dbgln("Font lookup failed: '{}'", name);
return nullptr;
@ -214,13 +214,13 @@ RefPtr<Gfx::Font> FontDatabase::get_by_name(StringView name)
return it->value;
}
RefPtr<Gfx::Font> FontDatabase::get(DeprecatedFlyString const& family, float point_size, unsigned weight, unsigned slope, Font::AllowInexactSizeMatch allow_inexact_size_match)
RefPtr<Gfx::Font> FontDatabase::get(DeprecatedFlyString const& family, float point_size, unsigned weight, unsigned width, unsigned slope, Font::AllowInexactSizeMatch allow_inexact_size_match)
{
auto it = m_private->typefaces.find(family);
if (it == m_private->typefaces.end())
return nullptr;
for (auto const& typeface : it->value) {
if (typeface->weight() == weight && typeface->slope() == slope)
if (typeface->weight() == weight && typeface->width() == width && typeface->slope() == slope)
return typeface->get_font(point_size, allow_inexact_size_match);
}
return nullptr;

View file

@ -48,7 +48,7 @@ public:
static void set_fixed_width_font_query(DeprecatedString);
static void set_default_fonts_lookup_path(DeprecatedString);
RefPtr<Gfx::Font> get(DeprecatedFlyString const& family, float point_size, unsigned weight, unsigned slope, Font::AllowInexactSizeMatch = Font::AllowInexactSizeMatch::No);
RefPtr<Gfx::Font> get(DeprecatedFlyString const& family, float point_size, unsigned weight, unsigned width, unsigned slope, Font::AllowInexactSizeMatch = Font::AllowInexactSizeMatch::No);
RefPtr<Gfx::Font> get(DeprecatedFlyString const& family, DeprecatedFlyString const& variant, float point_size, Font::AllowInexactSizeMatch = Font::AllowInexactSizeMatch::No);
RefPtr<Gfx::Font> get_by_name(StringView);
void for_each_font(Function<void(Gfx::Font const&)>);

View file

@ -650,6 +650,15 @@ u16 Font::weight() const
return 400;
}
u16 Font::width() const
{
if (m_os2.has_value()) {
return m_os2->width_class();
}
return Gfx::FontWidth::Normal;
}
u8 Font::slope() const
{
// https://docs.microsoft.com/en-us/typography/opentype/spec/os2
@ -680,6 +689,11 @@ u16 OS2::weight_class() const
return header().us_weight_class;
}
u16 OS2::width_class() const
{
return header().us_width_class;
}
u16 OS2::selection() const
{
return header().fs_selection;

View file

@ -37,6 +37,7 @@ public:
virtual DeprecatedString family() const override;
virtual DeprecatedString variant() const override;
virtual u16 weight() const override;
virtual u16 width() const override;
virtual u8 slope() const override;
virtual bool is_fixed_width() const override;

View file

@ -231,6 +231,7 @@ private:
class OS2 {
public:
u16 weight_class() const;
u16 width_class() const;
u16 selection() const;
i16 typographic_ascender() const;
i16 typographic_descender() const;

View file

@ -38,6 +38,7 @@ public:
virtual float pixel_size() const override { return m_point_height * 1.33333333f; }
virtual Gfx::FontPixelMetrics pixel_metrics() const override;
virtual u8 slope() const override { return m_font->slope(); }
virtual u16 width() const override { return m_font->width(); }
virtual u16 weight() const override { return m_font->weight(); }
virtual Gfx::Glyph glyph(u32 code_point) const override;
virtual float glyph_left_bearing(u32 code_point) const override;

View file

@ -19,6 +19,16 @@ unsigned Typeface::weight() const
return m_vector_font->weight();
}
unsigned Typeface::width() const
{
VERIFY(m_vector_font || m_bitmap_fonts.size() > 0);
if (is_fixed_size())
return Gfx::FontWidth::Normal;
return m_vector_font->width();
}
u8 Typeface::slope() const
{
VERIFY(m_vector_font || m_bitmap_fonts.size() > 0);

View file

@ -27,6 +27,7 @@ public:
DeprecatedFlyString const& family() const { return m_family; }
DeprecatedFlyString const& variant() const { return m_variant; }
unsigned weight() const;
unsigned width() const;
u8 slope() const;
bool is_fixed_width() const;

View file

@ -44,6 +44,7 @@ public:
virtual DeprecatedString family() const = 0;
virtual DeprecatedString variant() const = 0;
virtual u16 weight() const = 0;
virtual u16 width() const = 0;
virtual u8 slope() const = 0;
virtual bool is_fixed_width() const = 0;
};

View file

@ -33,6 +33,7 @@ public:
virtual DeprecatedString family() const override { return m_input_font->family(); }
virtual DeprecatedString variant() const override { return m_input_font->variant(); }
virtual u16 weight() const override { return m_input_font->weight(); }
virtual u16 width() const override { return m_input_font->width(); }
virtual u8 slope() const override { return m_input_font->slope(); }
virtual bool is_fixed_width() const override { return m_input_font->is_fixed_width(); }