Everywhere: Fully qualify font names by including their slope

Fixes typefaces of the same weight but different slopes being
incorrectly returned for each other by FontDatabase.
This commit is contained in:
thankyouverycool 2022-01-31 20:18:15 -05:00 committed by Andreas Kling
parent 87a149c9a4
commit 96895cd22c
Notes: sideshowbarker 2024-07-17 19:53:40 +09:00
15 changed files with 39 additions and 25 deletions

View file

@ -10,8 +10,8 @@ Height=768
ScaleFactor=1
[Fonts]
Default=Katica 10 400
FixedWidth=Csilla 10 400
Default=Katica 10 400 0
FixedWidth=Csilla 10 400 0
[Theme]
Name=Default

View file

@ -16,7 +16,7 @@
static struct FontDatabaseSpoofer {
FontDatabaseSpoofer()
{
Gfx::FontDatabase::the().set_default_font_query("Katica 10 400"sv);
Gfx::FontDatabase::the().set_default_font_query("Katica 10 400 0"sv);
}
} g_spoof;

View file

@ -14,7 +14,7 @@
TEST_CASE(test_fontdatabase_get_by_name)
{
const char* name = "Liza 10 400";
const char* name = "Liza 10 400 0";
auto& font_database = Gfx::FontDatabase::the();
EXPECT(!font_database.get_by_name(name)->name().is_null());
}
@ -22,7 +22,7 @@ TEST_CASE(test_fontdatabase_get_by_name)
TEST_CASE(test_fontdatabase_get)
{
auto& font_database = Gfx::FontDatabase::the();
EXPECT(!font_database.get("Liza", 10, 400)->name().is_null());
EXPECT(!font_database.get("Liza", 10, 400, 0)->name().is_null());
}
TEST_CASE(test_fontdatabase_for_each_font)

View file

@ -609,7 +609,7 @@ void TextEditor::paint_event(PaintEvent& event)
}
auto font = unspanned_font;
if (span.attributes.bold) {
if (auto bold_font = Gfx::FontDatabase::the().get(font->family(), font->presentation_size(), 700))
if (auto bold_font = Gfx::FontDatabase::the().get(font->family(), font->presentation_size(), 700, 0))
font = bold_font;
}
draw_text_helper(span_start, span_end, font, span.attributes);

View file

@ -753,25 +753,25 @@ void Widget::set_font(const Gfx::Font* font)
void Widget::set_font_family(const String& family)
{
set_font(Gfx::FontDatabase::the().get(family, m_font->presentation_size(), m_font->weight()));
set_font(Gfx::FontDatabase::the().get(family, m_font->presentation_size(), m_font->weight(), m_font->slope()));
}
void Widget::set_font_size(unsigned size)
{
set_font(Gfx::FontDatabase::the().get(m_font->family(), size, m_font->weight()));
set_font(Gfx::FontDatabase::the().get(m_font->family(), size, m_font->weight(), m_font->slope()));
}
void Widget::set_font_weight(unsigned weight)
{
set_font(Gfx::FontDatabase::the().get(m_font->family(), m_font->presentation_size(), weight));
set_font(Gfx::FontDatabase::the().get(m_font->family(), m_font->presentation_size(), weight, m_font->slope()));
}
void Widget::set_font_fixed_width(bool fixed_width)
{
if (fixed_width)
set_font(Gfx::FontDatabase::the().get(Gfx::FontDatabase::the().default_fixed_width_font().family(), m_font->presentation_size(), m_font->weight()));
set_font(Gfx::FontDatabase::the().get(Gfx::FontDatabase::the().default_fixed_width_font().family(), m_font->presentation_size(), m_font->weight(), m_font->slope()));
else
set_font(Gfx::FontDatabase::the().get(Gfx::FontDatabase::the().default_font().family(), m_font->presentation_size(), m_font->weight()));
set_font(Gfx::FontDatabase::the().get(Gfx::FontDatabase::the().default_font().family(), m_font->presentation_size(), m_font->weight(), m_font->slope()));
}
void Widget::set_min_size(const Gfx::IntSize& size)

View file

@ -28,7 +28,7 @@ CoverWizardPage::CoverWizardPage()
m_content_widget->layout()->set_margins(20);
m_header_label = m_content_widget->add<Label>();
m_header_label->set_font(Gfx::FontDatabase::the().get("Pebbleton", 14, 700));
m_header_label->set_font(Gfx::FontDatabase::the().get("Pebbleton", 14, 700, 0));
m_header_label->set_text_alignment(Gfx::TextAlignment::TopLeft);
m_header_label->set_fixed_height(48);

View file

@ -345,7 +345,7 @@ ALWAYS_INLINE int BitmapFont::unicode_view_width(T const& view) const
String BitmapFont::qualified_name() const
{
return String::formatted("{} {} {}", family(), presentation_size(), weight());
return String::formatted("{} {} {} {}", family(), presentation_size(), weight(), slope());
}
String BitmapFont::variant() const
@ -366,7 +366,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);
m_bold_variant = Gfx::FontDatabase::the().get(family(), presentation_size(), 700, 0);
if (!m_bold_variant)
m_bold_variant = this;
return *m_bold_variant;

View file

@ -138,11 +138,12 @@ RefPtr<Gfx::Font> FontDatabase::get_by_name(StringView name)
auto it = m_private->full_name_to_font_map.find(name);
if (it == m_private->full_name_to_font_map.end()) {
auto parts = name.split_view(" "sv);
if (parts.size() >= 3) {
if (parts.size() >= 4) {
auto slope = parts.take_last().to_int().value_or(0);
auto weight = parts.take_last().to_int().value_or(0);
auto size = parts.take_last().to_int().value_or(0);
auto family = String::join(' ', parts);
return get(family, size, weight);
return get(family, size, weight, slope);
}
dbgln("Font lookup failed: '{}'", name);
return nullptr;
@ -150,10 +151,10 @@ RefPtr<Gfx::Font> FontDatabase::get_by_name(StringView name)
return it->value;
}
RefPtr<Gfx::Font> FontDatabase::get(const String& family, unsigned size, unsigned weight)
RefPtr<Gfx::Font> FontDatabase::get(const String& family, unsigned size, unsigned weight, unsigned slope)
{
for (auto typeface : m_private->typefaces) {
if (typeface->family() == family && typeface->weight() == weight)
if (typeface->family() == family && typeface->weight() == weight && typeface->slope() == slope)
return typeface->get_font(size);
}
return nullptr;

View file

@ -42,7 +42,7 @@ public:
static void set_default_font_query(String);
static void set_fixed_width_font_query(String);
RefPtr<Gfx::Font> get(const String& family, unsigned size, unsigned weight);
RefPtr<Gfx::Font> get(const String& family, unsigned size, unsigned weight, unsigned slope);
RefPtr<Gfx::Font> get(const String& family, const String& variant, unsigned size);
RefPtr<Gfx::Font> get_by_name(StringView);
void for_each_font(Function<void(const Gfx::Font&)>);

View file

@ -145,7 +145,7 @@ public:
virtual size_t glyph_count() const override { return m_font->glyph_count(); }
virtual String family() const override { return m_font->family(); }
virtual String variant() const override { return m_font->variant(); }
virtual String qualified_name() const override { return String::formatted("{} {} {}", family(), presentation_size(), weight()); }
virtual String qualified_name() const override { return String::formatted("{} {} {} {}", family(), presentation_size(), weight(), slope()); }
private:
NonnullRefPtr<TTF::Font> m_font;

View file

@ -18,6 +18,16 @@ unsigned Typeface::weight() const
return m_ttf_font->weight();
}
u8 Typeface::slope() const
{
VERIFY(m_ttf_font || m_bitmap_fonts.size() > 0);
if (is_fixed_size())
return m_bitmap_fonts[0]->slope();
return m_ttf_font->slope();
}
bool Typeface::is_fixed_width() const
{
VERIFY(m_ttf_font || m_bitmap_fonts.size() > 0);

View file

@ -27,6 +27,7 @@ public:
String family() const { return m_family; }
String variant() const { return m_variant; }
unsigned weight() const;
u8 slope() const;
bool is_fixed_width() const;
bool is_fixed_size() const { return !m_bitmap_fonts.is_empty(); }

View file

@ -793,13 +793,14 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele
FontSelector font_selector;
bool monospace = false;
// FIXME: Implement font slope style. All found fonts are currently hard-coded as regular.
auto find_font = [&](String const& family) -> RefPtr<Gfx::Font> {
font_selector = { family, size, weight };
font_selector = { family, size, weight, 0 };
if (auto found_font = FontCache::the().get(font_selector))
return found_font;
if (auto found_font = Gfx::FontDatabase::the().get(family, size, weight))
if (auto found_font = Gfx::FontDatabase::the().get(family, size, weight, 0))
return found_font;
return {};

View file

@ -16,10 +16,11 @@ struct FontSelector {
FlyString family;
int size { 0 };
int weight { 0 };
int slope { 0 };
bool operator==(const FontSelector& other) const
{
return family == other.family && size == other.size && weight == other.weight;
return family == other.family && size == other.size && weight == other.weight && slope == other.slope;
}
};

View file

@ -41,8 +41,8 @@ ErrorOr<int> serenity_main(Main::Arguments)
Gfx::set_system_theme(theme);
auto palette = Gfx::PaletteImpl::create_with_anonymous_buffer(theme);
auto default_font_query = wm_config->read_entry("Fonts", "Default", "Katica 10 400");
auto fixed_width_font_query = wm_config->read_entry("Fonts", "FixedWidth", "Csilla 10 400");
auto default_font_query = wm_config->read_entry("Fonts", "Default", "Katica 10 400 0");
auto fixed_width_font_query = wm_config->read_entry("Fonts", "FixedWidth", "Csilla 10 400 0");
Gfx::FontDatabase::set_default_font_query(default_font_query);
Gfx::FontDatabase::set_fixed_width_font_query(fixed_width_font_query);