mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:27:35 +00:00
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. :^)
This commit is contained in:
parent
5c2e3d1637
commit
3dc82d2fa5
2 changed files with 49 additions and 27 deletions
|
@ -6,8 +6,10 @@
|
||||||
|
|
||||||
#include <AK/FlyString.h>
|
#include <AK/FlyString.h>
|
||||||
#include <AK/NonnullRefPtrVector.h>
|
#include <AK/NonnullRefPtrVector.h>
|
||||||
|
#include <AK/Queue.h>
|
||||||
#include <AK/QuickSort.h>
|
#include <AK/QuickSort.h>
|
||||||
#include <LibCore/DirIterator.h>
|
#include <LibCore/DirIterator.h>
|
||||||
|
#include <LibCore/File.h>
|
||||||
#include <LibGfx/Font/Font.h>
|
#include <LibGfx/Font/Font.h>
|
||||||
#include <LibGfx/Font/FontDatabase.h>
|
#include <LibGfx/Font/FontDatabase.h>
|
||||||
#include <LibGfx/Font/TrueType/Font.h>
|
#include <LibGfx/Font/TrueType/Font.h>
|
||||||
|
@ -120,41 +122,57 @@ struct FontDatabase::Private {
|
||||||
Vector<RefPtr<Typeface>> typefaces;
|
Vector<RefPtr<Typeface>> typefaces;
|
||||||
};
|
};
|
||||||
|
|
||||||
FontDatabase::FontDatabase()
|
void FontDatabase::load_all_fonts_from_path(String const& root)
|
||||||
: m_private(make<Private>())
|
|
||||||
{
|
{
|
||||||
Core::DirIterator dir_iterator(s_default_fonts_lookup_path, Core::DirIterator::SkipDots);
|
Queue<String> path_queue;
|
||||||
if (dir_iterator.has_error()) {
|
path_queue.enqueue(root);
|
||||||
warnln("DirIterator: {}", dir_iterator.error_string());
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
while (dir_iterator.has_next()) {
|
|
||||||
auto path = dir_iterator.next_full_path();
|
|
||||||
|
|
||||||
if (path.ends_with(".font"sv)) {
|
while (!path_queue.is_empty()) {
|
||||||
if (auto font_or_error = Gfx::BitmapFont::try_load_from_file(path); !font_or_error.is_error()) {
|
auto current_directory = path_queue.dequeue();
|
||||||
auto font = font_or_error.release_value();
|
Core::DirIterator dir_iterator(current_directory, Core::DirIterator::SkipParentAndBaseDir);
|
||||||
m_private->full_name_to_font_map.set(font->qualified_name(), *font);
|
if (dir_iterator.has_error()) {
|
||||||
auto typeface = get_or_create_typeface(font->family(), font->variant());
|
dbgln("FontDatabase::load_fonts: {}", dir_iterator.error_string());
|
||||||
typeface->add_bitmap_font(font);
|
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 (path.ends_with(".font"sv)) {
|
||||||
if (auto font_or_error = TTF::Font::try_load_from_file(path); !font_or_error.is_error()) {
|
if (auto font_or_error = Gfx::BitmapFont::try_load_from_file(path); !font_or_error.is_error()) {
|
||||||
auto font = font_or_error.release_value();
|
auto font = font_or_error.release_value();
|
||||||
auto typeface = get_or_create_typeface(font->family(), font->variant());
|
m_private->full_name_to_font_map.set(font->qualified_name(), *font);
|
||||||
typeface->set_vector_font(move(font));
|
auto typeface = get_or_create_typeface(font->family(), font->variant());
|
||||||
}
|
typeface->add_bitmap_font(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()) {
|
} else if (path.ends_with(".ttf"sv)) {
|
||||||
auto font = font_or_error.release_value();
|
// FIXME: What about .otf
|
||||||
auto typeface = get_or_create_typeface(font->family(), font->variant());
|
if (auto font_or_error = TTF::Font::try_load_from_file(path); !font_or_error.is_error()) {
|
||||||
typeface->set_vector_font(move(font));
|
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<Private>())
|
||||||
|
{
|
||||||
|
load_all_fonts_from_path(s_default_fonts_lookup_path);
|
||||||
|
}
|
||||||
|
|
||||||
void FontDatabase::for_each_font(Function<void(Gfx::Font const&)> callback)
|
void FontDatabase::for_each_font(Function<void(Gfx::Font const&)> callback)
|
||||||
{
|
{
|
||||||
Vector<RefPtr<Gfx::Font>> fonts;
|
Vector<RefPtr<Gfx::Font>> fonts;
|
||||||
|
|
|
@ -56,10 +56,14 @@ public:
|
||||||
|
|
||||||
void for_each_typeface(Function<void(Typeface const&)>);
|
void for_each_typeface(Function<void(Typeface const&)>);
|
||||||
|
|
||||||
|
void load_all_fonts_from_path(String const&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
FontDatabase();
|
FontDatabase();
|
||||||
~FontDatabase() = default;
|
~FontDatabase() = default;
|
||||||
|
|
||||||
|
void load_fonts();
|
||||||
|
|
||||||
RefPtr<Typeface> get_or_create_typeface(String const& family, String const& variant);
|
RefPtr<Typeface> get_or_create_typeface(String const& family, String const& variant);
|
||||||
|
|
||||||
struct Private;
|
struct Private;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue