From ee883372f69170312215b0cc8374aac303a02223 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 26 Mar 2022 23:54:07 +0100 Subject: [PATCH] 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. --- Userland/Libraries/LibGfx/FontDatabase.cpp | 8 ++++---- Userland/Libraries/LibGfx/FontDatabase.h | 4 ++-- Userland/Libraries/LibGfx/Typeface.cpp | 11 ++++++----- Userland/Libraries/LibGfx/Typeface.h | 2 +- 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 FontDatabase::get_by_name(StringView name) return it->value; } -RefPtr FontDatabase::get(FlyString const& family, unsigned size, unsigned weight, unsigned slope, Font::AllowInexactSizeMatch allow_inexact_size_match) +RefPtr 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 FontDatabase::get(FlyString const& family, FlyString const& variant, unsigned size, Font::AllowInexactSizeMatch allow_inexact_size_match) +RefPtr 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 get(FlyString const& family, unsigned size, unsigned weight, unsigned slope, Font::AllowInexactSizeMatch = Font::AllowInexactSizeMatch::No); - RefPtr get(FlyString const& family, FlyString const& variant, unsigned size, Font::AllowInexactSizeMatch = Font::AllowInexactSizeMatch::No); + RefPtr get(FlyString const& family, float point_size, unsigned weight, unsigned slope, Font::AllowInexactSizeMatch = Font::AllowInexactSizeMatch::No); + RefPtr get(FlyString const& family, FlyString const& variant, float point_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 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 font) m_ttf_font = move(font); } -RefPtr Typeface::get_font(unsigned size, Font::AllowInexactSizeMatch allow_inexact_size_match) const +RefPtr Typeface::get_font(float point_size, Font::AllowInexactSizeMatch allow_inexact_size_match) const { - VERIFY(size < NumericLimits::max()); + VERIFY(point_size > 0); + + if (m_ttf_font) + return adopt_ref(*new TTF::ScaledFont(*m_ttf_font, point_size, point_size)); RefPtr best_match; + int size = roundf(point_size); int best_delta = NumericLimits::max(); for (auto font : m_bitmap_fonts) { @@ -70,9 +74,6 @@ RefPtr 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); void set_ttf_font(RefPtr); - RefPtr get_font(unsigned size, Font::AllowInexactSizeMatch = Font::AllowInexactSizeMatch::No) const; + RefPtr get_font(float point_size, Font::AllowInexactSizeMatch = Font::AllowInexactSizeMatch::No) const; private: FlyString m_family;