From 0e056147107ee42818da2b853bd7edde83683425 Mon Sep 17 00:00:00 2001 From: Shane Murphy Date: Wed, 18 Oct 2023 21:55:47 -0700 Subject: [PATCH] LibGfx: Improve tie-breaking in Typeface::get_font Add two new members to Font::AllowInexactMatch: Larger and Smaller. This allows callers of FontDatabase::get to specify if they want to prefer a larger or smaller font in the event of a tie. --- Userland/Libraries/LibGfx/Font/Font.h | 2 ++ Userland/Libraries/LibGfx/Font/Typeface.cpp | 12 +++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibGfx/Font/Font.h b/Userland/Libraries/LibGfx/Font/Font.h index 21e3c37812..7049e80658 100644 --- a/Userland/Libraries/LibGfx/Font/Font.h +++ b/Userland/Libraries/LibGfx/Font/Font.h @@ -148,6 +148,8 @@ public: enum class AllowInexactSizeMatch { No, Yes, + Larger, + Smaller, }; virtual NonnullRefPtr clone() const = 0; diff --git a/Userland/Libraries/LibGfx/Font/Typeface.cpp b/Userland/Libraries/LibGfx/Font/Typeface.cpp index 922749bb82..56a1a47762 100644 --- a/Userland/Libraries/LibGfx/Font/Typeface.cpp +++ b/Userland/Libraries/LibGfx/Font/Typeface.cpp @@ -73,16 +73,22 @@ RefPtr Typeface::get_font(float point_size, Font::AllowInexactSizeMatch al for (auto font : m_bitmap_fonts) { if (font->presentation_size() == size) return font; - if (allow_inexact_size_match == Font::AllowInexactSizeMatch::Yes) { + if (allow_inexact_size_match != Font::AllowInexactSizeMatch::No) { int delta = static_cast(font->presentation_size()) - static_cast(size); - if (abs(delta) < best_delta) { + if (abs(delta) == best_delta) { + if (allow_inexact_size_match == Font::AllowInexactSizeMatch::Larger && delta > 0) { + best_match = font; + } else if (allow_inexact_size_match == Font::AllowInexactSizeMatch::Smaller && delta < 0) { + best_match = font; + } + } else if (abs(delta) < best_delta) { best_match = font; best_delta = abs(delta); } } } - if (allow_inexact_size_match == Font::AllowInexactSizeMatch::Yes && best_match) + if (allow_inexact_size_match != Font::AllowInexactSizeMatch::No && best_match) return best_match; return {};