diff --git a/Userland/Libraries/LibGfx/Font.h b/Userland/Libraries/LibGfx/Font.h index e9264de823..441e9dbad9 100644 --- a/Userland/Libraries/LibGfx/Font.h +++ b/Userland/Libraries/LibGfx/Font.h @@ -95,6 +95,11 @@ struct FontMetrics { class Font : public RefCounted { public: + enum class AllowInexactSizeMatch { + No, + Yes, + }; + virtual NonnullRefPtr clone() const = 0; virtual ~Font() {}; diff --git a/Userland/Libraries/LibGfx/FontDatabase.cpp b/Userland/Libraries/LibGfx/FontDatabase.cpp index 6232b38825..51da859afc 100644 --- a/Userland/Libraries/LibGfx/FontDatabase.cpp +++ b/Userland/Libraries/LibGfx/FontDatabase.cpp @@ -164,20 +164,20 @@ RefPtr FontDatabase::get_by_name(StringView name) return it->value; } -RefPtr FontDatabase::get(const String& family, unsigned size, unsigned weight, unsigned slope) +RefPtr FontDatabase::get(String const& family, unsigned size, unsigned weight, unsigned slope, Font::AllowInexactSizeMatch allow_inexact_size_match) { for (auto typeface : m_private->typefaces) { if (typeface->family() == family && typeface->weight() == weight && typeface->slope() == slope) - return typeface->get_font(size); + return typeface->get_font(size, allow_inexact_size_match); } return nullptr; } -RefPtr FontDatabase::get(const String& family, const String& variant, unsigned size) +RefPtr FontDatabase::get(String const& family, const String& variant, unsigned size, Font::AllowInexactSizeMatch allow_inexact_size_match) { for (auto typeface : m_private->typefaces) { if (typeface->family() == family && typeface->variant() == variant) - return typeface->get_font(size); + return typeface->get_font(size, allow_inexact_size_match); } return nullptr; } diff --git a/Userland/Libraries/LibGfx/FontDatabase.h b/Userland/Libraries/LibGfx/FontDatabase.h index 29f1a1bf7b..f475fec169 100644 --- a/Userland/Libraries/LibGfx/FontDatabase.h +++ b/Userland/Libraries/LibGfx/FontDatabase.h @@ -44,8 +44,8 @@ public: static void set_fixed_width_font_query(String); static void set_default_fonts_lookup_path(String); - RefPtr get(const String& family, unsigned size, unsigned weight, unsigned slope); - RefPtr get(const String& family, const String& variant, unsigned size); + RefPtr get(const String& family, unsigned size, unsigned weight, unsigned slope, Font::AllowInexactSizeMatch = Font::AllowInexactSizeMatch::No); + RefPtr get(const String& family, const String& variant, unsigned size, Font::AllowInexactSizeMatch = Font::AllowInexactSizeMatch::No); RefPtr get_by_name(StringView); void for_each_font(Function); void for_each_fixed_width_font(Function); diff --git a/Userland/Libraries/LibGfx/Typeface.cpp b/Userland/Libraries/LibGfx/Typeface.cpp index f69225ec02..42681889be 100644 --- a/Userland/Libraries/LibGfx/Typeface.cpp +++ b/Userland/Libraries/LibGfx/Typeface.cpp @@ -48,13 +48,28 @@ void Typeface::set_ttf_font(RefPtr font) m_ttf_font = move(font); } -RefPtr Typeface::get_font(unsigned size) const +RefPtr Typeface::get_font(unsigned size, Font::AllowInexactSizeMatch allow_inexact_size_match) const { + VERIFY(size < NumericLimits::max()); + + RefPtr best_match; + int best_delta = NumericLimits::max(); + for (auto font : m_bitmap_fonts) { if (font->presentation_size() == size) return font; + if (allow_inexact_size_match == Font::AllowInexactSizeMatch::Yes) { + int delta = static_cast(font->presentation_size()) - static_cast(size); + if (abs(delta) < best_delta) { + best_match = font; + best_delta = abs(delta); + } + } } + if (allow_inexact_size_match == Font::AllowInexactSizeMatch::Yes && best_match) + return best_match; + if (m_ttf_font) return adopt_ref(*new TTF::ScaledFont(*m_ttf_font, size, size)); diff --git a/Userland/Libraries/LibGfx/Typeface.h b/Userland/Libraries/LibGfx/Typeface.h index 631c306580..3e8cb27c0c 100644 --- a/Userland/Libraries/LibGfx/Typeface.h +++ b/Userland/Libraries/LibGfx/Typeface.h @@ -36,7 +36,7 @@ public: void add_bitmap_font(RefPtr); void set_ttf_font(RefPtr); - RefPtr get_font(unsigned size) const; + RefPtr get_font(unsigned size, Font::AllowInexactSizeMatch = Font::AllowInexactSizeMatch::No) const; private: String m_family;