summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2022-02-25 13:30:08 +0100
committerAndreas Kling <kling@serenityos.org>2022-02-25 19:38:31 +0100
commit4dd9e2df7846ab3a5656b78665d3806c31985653 (patch)
tree5814f7d79d439ad30dba25067b32b3c44bc07e23 /Userland
parent95715f0c8f066b2bb43b9dbb4dec993bb627fd9f (diff)
downloadserenity-4dd9e2df7846ab3a5656b78665d3806c31985653.zip
LibGfx: Add Font::AllowInexactSizeMatch parameter to font lookup
This allows bitmap font lookup to return the best matching size instead of failing completely. The previous behavior (exact matches only) remains the default.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibGfx/Font.h5
-rw-r--r--Userland/Libraries/LibGfx/FontDatabase.cpp8
-rw-r--r--Userland/Libraries/LibGfx/FontDatabase.h4
-rw-r--r--Userland/Libraries/LibGfx/Typeface.cpp17
-rw-r--r--Userland/Libraries/LibGfx/Typeface.h2
5 files changed, 28 insertions, 8 deletions
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<Font> {
public:
+ enum class AllowInexactSizeMatch {
+ No,
+ Yes,
+ };
+
virtual NonnullRefPtr<Font> 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<Gfx::Font> FontDatabase::get_by_name(StringView name)
return it->value;
}
-RefPtr<Gfx::Font> FontDatabase::get(const String& family, unsigned size, unsigned weight, unsigned slope)
+RefPtr<Gfx::Font> 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<Gfx::Font> FontDatabase::get(const String& family, const String& variant, unsigned size)
+RefPtr<Gfx::Font> 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<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(const String& family, unsigned size, unsigned weight, unsigned slope, Font::AllowInexactSizeMatch = Font::AllowInexactSizeMatch::No);
+ RefPtr<Gfx::Font> get(const String& family, const String& variant, unsigned size, Font::AllowInexactSizeMatch = Font::AllowInexactSizeMatch::No);
RefPtr<Gfx::Font> get_by_name(StringView);
void for_each_font(Function<void(const Gfx::Font&)>);
void for_each_fixed_width_font(Function<void(const Gfx::Font&)>);
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<TTF::Font> font)
m_ttf_font = move(font);
}
-RefPtr<Font> Typeface::get_font(unsigned size) const
+RefPtr<Font> Typeface::get_font(unsigned size, Font::AllowInexactSizeMatch allow_inexact_size_match) const
{
+ VERIFY(size < NumericLimits<int>::max());
+
+ RefPtr<BitmapFont> best_match;
+ int best_delta = NumericLimits<int>::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<int>(font->presentation_size()) - static_cast<int>(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<BitmapFont>);
void set_ttf_font(RefPtr<TTF::Font>);
- RefPtr<Font> get_font(unsigned size) const;
+ RefPtr<Font> get_font(unsigned size, Font::AllowInexactSizeMatch = Font::AllowInexactSizeMatch::No) const;
private:
String m_family;