1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 03:37:43 +00:00

LibGfx: Cache vector fonts by family name

Instead of just keeping them in an unsorted Vector, which led to
increasingly noticeable O(n) lookups, we now cache a list of Typefaces
per family name.
This commit is contained in:
Andreas Kling 2022-09-15 12:03:41 +02:00
parent a192095594
commit 4910cc1879

View file

@ -119,7 +119,7 @@ Font& FontDatabase::default_fixed_width_font()
struct FontDatabase::Private { struct FontDatabase::Private {
HashMap<String, NonnullRefPtr<Gfx::Font>> full_name_to_font_map; HashMap<String, NonnullRefPtr<Gfx::Font>> full_name_to_font_map;
Vector<RefPtr<Typeface>> typefaces; HashMap<FlyString, Vector<NonnullRefPtr<Typeface>>> typefaces;
}; };
void FontDatabase::load_all_fonts_from_path(String const& root) void FontDatabase::load_all_fonts_from_path(String const& root)
@ -217,8 +217,11 @@ RefPtr<Gfx::Font> FontDatabase::get_by_name(StringView name)
RefPtr<Gfx::Font> FontDatabase::get(FlyString const& family, float point_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) { auto it = m_private->typefaces.find(family);
if (typeface->family() == family && typeface->weight() == weight && typeface->slope() == slope) if (it == m_private->typefaces.end())
return nullptr;
for (auto const& typeface : it->value) {
if (typeface->weight() == weight && typeface->slope() == slope)
return typeface->get_font(point_size, allow_inexact_size_match); return typeface->get_font(point_size, allow_inexact_size_match);
} }
return nullptr; return nullptr;
@ -226,8 +229,11 @@ RefPtr<Gfx::Font> FontDatabase::get(FlyString const& family, float point_size, u
RefPtr<Gfx::Font> FontDatabase::get(FlyString const& family, FlyString const& variant, float point_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) { auto it = m_private->typefaces.find(family);
if (typeface->family() == family && typeface->variant() == variant) if (it == m_private->typefaces.end())
return nullptr;
for (auto const& typeface : it->value) {
if (typeface->variant() == variant)
return typeface->get_font(point_size, allow_inexact_size_match); return typeface->get_font(point_size, allow_inexact_size_match);
} }
return nullptr; return nullptr;
@ -235,19 +241,24 @@ RefPtr<Gfx::Font> FontDatabase::get(FlyString const& family, FlyString const& va
RefPtr<Typeface> FontDatabase::get_or_create_typeface(String const& family, String const& variant) RefPtr<Typeface> FontDatabase::get_or_create_typeface(String const& family, String const& variant)
{ {
for (auto typeface : m_private->typefaces) { auto it = m_private->typefaces.find(family);
if (typeface->family() == family && typeface->variant() == variant) if (it != m_private->typefaces.end()) {
return typeface; for (auto const& typeface : it->value) {
if (typeface->variant() == variant)
return typeface;
}
} }
auto typeface = adopt_ref(*new Typeface(family, variant)); auto typeface = adopt_ref(*new Typeface(family, variant));
m_private->typefaces.append(typeface); m_private->typefaces.ensure(family).append(typeface);
return typeface; return typeface;
} }
void FontDatabase::for_each_typeface(Function<void(Typeface const&)> callback) void FontDatabase::for_each_typeface(Function<void(Typeface const&)> callback)
{ {
for (auto typeface : m_private->typefaces) { for (auto const& it : m_private->typefaces) {
callback(*typeface); for (auto const& jt : it.value) {
callback(*jt);
}
} }
} }