1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 17:07:46 +00:00

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.
This commit is contained in:
Shane Murphy 2023-10-18 21:55:47 -07:00 committed by Andreas Kling
parent 1915e603c9
commit 0e05614710
2 changed files with 11 additions and 3 deletions

View file

@ -148,6 +148,8 @@ public:
enum class AllowInexactSizeMatch {
No,
Yes,
Larger,
Smaller,
};
virtual NonnullRefPtr<Font> clone() const = 0;

View file

@ -73,16 +73,22 @@ RefPtr<Font> 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<int>(font->presentation_size()) - static_cast<int>(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 {};