2019-05-27 09:26:54 +02:00
|
|
|
#include <LibCore/CDirIterator.h>
|
2019-06-07 11:46:02 +02:00
|
|
|
#include <LibGUI/GFontDatabase.h>
|
2019-02-12 14:35:33 +01:00
|
|
|
#include <SharedGraphics/Font.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2019-04-05 05:10:18 +02:00
|
|
|
static GFontDatabase* s_the;
|
|
|
|
|
2019-02-12 14:35:33 +01:00
|
|
|
GFontDatabase& GFontDatabase::the()
|
|
|
|
{
|
2019-04-05 05:10:18 +02:00
|
|
|
if (!s_the)
|
|
|
|
s_the = new GFontDatabase;
|
|
|
|
return *s_the;
|
2019-02-12 14:35:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GFontDatabase::GFontDatabase()
|
|
|
|
{
|
2019-05-27 09:26:54 +02:00
|
|
|
CDirIterator di("/res/fonts", CDirIterator::SkipDots);
|
|
|
|
if (di.has_error()) {
|
|
|
|
fprintf(stderr, "CDirIterator: %s\n", di.error_string());
|
2019-02-12 14:35:33 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
2019-05-27 09:26:54 +02:00
|
|
|
while (di.has_next()) {
|
|
|
|
String name = di.next_path();
|
|
|
|
auto path = String::format("/res/fonts/%s", name.characters());
|
2019-03-06 14:06:40 +01:00
|
|
|
if (auto font = Font::load_from_file(path)) {
|
|
|
|
Metadata metadata;
|
|
|
|
metadata.path = path;
|
|
|
|
metadata.glyph_height = font->glyph_height();
|
|
|
|
metadata.is_fixed_width = font->is_fixed_width();
|
|
|
|
m_name_to_metadata.set(font->name(), move(metadata));
|
|
|
|
}
|
2019-02-12 14:35:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GFontDatabase::~GFontDatabase()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-02 14:58:02 +02:00
|
|
|
void GFontDatabase::for_each_font(Function<void(const StringView&)> callback)
|
2019-02-12 14:35:33 +01:00
|
|
|
{
|
2019-03-06 14:06:40 +01:00
|
|
|
for (auto& it : m_name_to_metadata) {
|
2019-02-12 14:35:33 +01:00
|
|
|
callback(it.key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-02 14:58:02 +02:00
|
|
|
void GFontDatabase::for_each_fixed_width_font(Function<void(const StringView&)> callback)
|
2019-03-06 14:06:40 +01:00
|
|
|
{
|
|
|
|
for (auto& it : m_name_to_metadata) {
|
|
|
|
if (it.value.is_fixed_width)
|
|
|
|
callback(it.key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
RefPtr<Font> GFontDatabase::get_by_name(const StringView& name)
|
2019-02-12 14:35:33 +01:00
|
|
|
{
|
2019-03-06 14:06:40 +01:00
|
|
|
auto it = m_name_to_metadata.find(name);
|
|
|
|
if (it == m_name_to_metadata.end())
|
2019-02-12 14:35:33 +01:00
|
|
|
return nullptr;
|
2019-03-06 14:06:40 +01:00
|
|
|
return Font::load_from_file((*it).value.path);
|
2019-02-12 14:35:33 +01:00
|
|
|
}
|