From 3dc82d2fa5f3b3441cb180f992ad0b25882e4e51 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 8 Sep 2022 12:30:25 +0200 Subject: [PATCH] LibGfx: Add API for loading more directories into Gfx::FontDatabase Also make the font discovery algorithm search subdirectories as well. This will be used by Ladybird to discover more fonts on non-Serenity systems. :^) --- .../Libraries/LibGfx/Font/FontDatabase.cpp | 72 ++++++++++++------- Userland/Libraries/LibGfx/Font/FontDatabase.h | 4 ++ 2 files changed, 49 insertions(+), 27 deletions(-) diff --git a/Userland/Libraries/LibGfx/Font/FontDatabase.cpp b/Userland/Libraries/LibGfx/Font/FontDatabase.cpp index 4be0a1acfa..ba18d5290c 100644 --- a/Userland/Libraries/LibGfx/Font/FontDatabase.cpp +++ b/Userland/Libraries/LibGfx/Font/FontDatabase.cpp @@ -6,8 +6,10 @@ #include #include +#include #include #include +#include #include #include #include @@ -120,41 +122,57 @@ struct FontDatabase::Private { Vector> typefaces; }; -FontDatabase::FontDatabase() - : m_private(make()) +void FontDatabase::load_all_fonts_from_path(String const& root) { - Core::DirIterator dir_iterator(s_default_fonts_lookup_path, Core::DirIterator::SkipDots); - if (dir_iterator.has_error()) { - warnln("DirIterator: {}", dir_iterator.error_string()); - exit(1); - } - while (dir_iterator.has_next()) { - auto path = dir_iterator.next_full_path(); + Queue path_queue; + path_queue.enqueue(root); - if (path.ends_with(".font"sv)) { - if (auto font_or_error = Gfx::BitmapFont::try_load_from_file(path); !font_or_error.is_error()) { - auto font = font_or_error.release_value(); - m_private->full_name_to_font_map.set(font->qualified_name(), *font); - auto typeface = get_or_create_typeface(font->family(), font->variant()); - typeface->add_bitmap_font(font); + while (!path_queue.is_empty()) { + auto current_directory = path_queue.dequeue(); + Core::DirIterator dir_iterator(current_directory, Core::DirIterator::SkipParentAndBaseDir); + if (dir_iterator.has_error()) { + dbgln("FontDatabase::load_fonts: {}", dir_iterator.error_string()); + continue; + } + while (dir_iterator.has_next()) { + auto path = dir_iterator.next_full_path(); + + if (Core::File::is_directory(path)) { + path_queue.enqueue(path); + continue; } - } else if (path.ends_with(".ttf"sv)) { - // FIXME: What about .otf - if (auto font_or_error = TTF::Font::try_load_from_file(path); !font_or_error.is_error()) { - auto font = font_or_error.release_value(); - auto typeface = get_or_create_typeface(font->family(), font->variant()); - typeface->set_vector_font(move(font)); - } - } else if (path.ends_with(".woff"sv)) { - if (auto font_or_error = WOFF::Font::try_load_from_file(path); !font_or_error.is_error()) { - auto font = font_or_error.release_value(); - auto typeface = get_or_create_typeface(font->family(), font->variant()); - typeface->set_vector_font(move(font)); + + if (path.ends_with(".font"sv)) { + if (auto font_or_error = Gfx::BitmapFont::try_load_from_file(path); !font_or_error.is_error()) { + auto font = font_or_error.release_value(); + m_private->full_name_to_font_map.set(font->qualified_name(), *font); + auto typeface = get_or_create_typeface(font->family(), font->variant()); + typeface->add_bitmap_font(font); + } + } else if (path.ends_with(".ttf"sv)) { + // FIXME: What about .otf + if (auto font_or_error = TTF::Font::try_load_from_file(path); !font_or_error.is_error()) { + auto font = font_or_error.release_value(); + auto typeface = get_or_create_typeface(font->family(), font->variant()); + typeface->set_vector_font(move(font)); + } + } else if (path.ends_with(".woff"sv)) { + if (auto font_or_error = WOFF::Font::try_load_from_file(path); !font_or_error.is_error()) { + auto font = font_or_error.release_value(); + auto typeface = get_or_create_typeface(font->family(), font->variant()); + typeface->set_vector_font(move(font)); + } } } } } +FontDatabase::FontDatabase() + : m_private(make()) +{ + load_all_fonts_from_path(s_default_fonts_lookup_path); +} + void FontDatabase::for_each_font(Function callback) { Vector> fonts; diff --git a/Userland/Libraries/LibGfx/Font/FontDatabase.h b/Userland/Libraries/LibGfx/Font/FontDatabase.h index d75432b517..5307de1f00 100644 --- a/Userland/Libraries/LibGfx/Font/FontDatabase.h +++ b/Userland/Libraries/LibGfx/Font/FontDatabase.h @@ -56,10 +56,14 @@ public: void for_each_typeface(Function); + void load_all_fonts_from_path(String const&); + private: FontDatabase(); ~FontDatabase() = default; + void load_fonts(); + RefPtr get_or_create_typeface(String const& family, String const& variant); struct Private;