summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2022-03-26 23:54:07 +0100
committerAndreas Kling <kling@serenityos.org>2022-03-27 01:14:56 +0100
commitee883372f69170312215b0cc8374aac303a02223 (patch)
tree74a1ed7266897592fa6db00158a6a2bcbe839a7c
parenteeeaf410fbe4735a3495f1cec05b22cc25b2a904 (diff)
downloadserenity-ee883372f69170312215b0cc8374aac303a02223.zip
LibGfx: Make FontDatabase lookups take font (point) sizes as float
This will allow web content to ask for fractional sizes, which becomes important when converting between px/pt.
-rw-r--r--Userland/Libraries/LibGfx/FontDatabase.cpp8
-rw-r--r--Userland/Libraries/LibGfx/FontDatabase.h4
-rw-r--r--Userland/Libraries/LibGfx/Typeface.cpp11
-rw-r--r--Userland/Libraries/LibGfx/Typeface.h2
4 files changed, 13 insertions, 12 deletions
diff --git a/Userland/Libraries/LibGfx/FontDatabase.cpp b/Userland/Libraries/LibGfx/FontDatabase.cpp
index 30c1afaf33..e761f5d89b 100644
--- a/Userland/Libraries/LibGfx/FontDatabase.cpp
+++ b/Userland/Libraries/LibGfx/FontDatabase.cpp
@@ -161,20 +161,20 @@ RefPtr<Gfx::Font> FontDatabase::get_by_name(StringView name)
return it->value;
}
-RefPtr<Gfx::Font> FontDatabase::get(FlyString const& family, unsigned size, unsigned weight, unsigned slope, Font::AllowInexactSizeMatch allow_inexact_size_match)
+RefPtr<Gfx::Font> FontDatabase::get(FlyString const& family, float point_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, allow_inexact_size_match);
+ return typeface->get_font(point_size, allow_inexact_size_match);
}
return nullptr;
}
-RefPtr<Gfx::Font> FontDatabase::get(FlyString const& family, FlyString const& variant, unsigned size, Font::AllowInexactSizeMatch allow_inexact_size_match)
+RefPtr<Gfx::Font> FontDatabase::get(FlyString const& family, FlyString const& variant, float point_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, allow_inexact_size_match);
+ return typeface->get_font(point_size, allow_inexact_size_match);
}
return nullptr;
}
diff --git a/Userland/Libraries/LibGfx/FontDatabase.h b/Userland/Libraries/LibGfx/FontDatabase.h
index 32b12c7dfc..effc8259ec 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(FlyString const& family, unsigned size, unsigned weight, unsigned slope, Font::AllowInexactSizeMatch = Font::AllowInexactSizeMatch::No);
- RefPtr<Gfx::Font> get(FlyString const& family, FlyString const& variant, unsigned size, Font::AllowInexactSizeMatch = Font::AllowInexactSizeMatch::No);
+ RefPtr<Gfx::Font> get(FlyString const& family, float point_size, unsigned weight, unsigned slope, Font::AllowInexactSizeMatch = Font::AllowInexactSizeMatch::No);
+ RefPtr<Gfx::Font> get(FlyString const& family, FlyString const& variant, float point_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 42681889be..87ba7eaf40 100644
--- a/Userland/Libraries/LibGfx/Typeface.cpp
+++ b/Userland/Libraries/LibGfx/Typeface.cpp
@@ -48,11 +48,15 @@ void Typeface::set_ttf_font(RefPtr<TTF::Font> font)
m_ttf_font = move(font);
}
-RefPtr<Font> Typeface::get_font(unsigned size, Font::AllowInexactSizeMatch allow_inexact_size_match) const
+RefPtr<Font> Typeface::get_font(float point_size, Font::AllowInexactSizeMatch allow_inexact_size_match) const
{
- VERIFY(size < NumericLimits<int>::max());
+ VERIFY(point_size > 0);
+
+ if (m_ttf_font)
+ return adopt_ref(*new TTF::ScaledFont(*m_ttf_font, point_size, point_size));
RefPtr<BitmapFont> best_match;
+ int size = roundf(point_size);
int best_delta = NumericLimits<int>::max();
for (auto font : m_bitmap_fonts) {
@@ -70,9 +74,6 @@ RefPtr<Font> Typeface::get_font(unsigned size, Font::AllowInexactSizeMatch allow
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));
-
return {};
}
diff --git a/Userland/Libraries/LibGfx/Typeface.h b/Userland/Libraries/LibGfx/Typeface.h
index b4f0de23f6..10b865d8bc 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, Font::AllowInexactSizeMatch = Font::AllowInexactSizeMatch::No) const;
+ RefPtr<Font> get_font(float point_size, Font::AllowInexactSizeMatch = Font::AllowInexactSizeMatch::No) const;
private:
FlyString m_family;