1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 08:28:11 +00:00

LibGUI: Add a GFontDatabase class that lets you enumerate fonts and more.

"More" in this case being also giving you the ability to load a font by name.
Use this as backend for Terminal's font menu. :^)
This commit is contained in:
Andreas Kling 2019-02-12 14:35:33 +01:00
parent 7df7e5e2a6
commit d6326d6c2e
4 changed files with 79 additions and 8 deletions

50
LibGUI/GFontDatabase.cpp Normal file
View file

@ -0,0 +1,50 @@
#include <LibGUI/GFontDatabase.h>
#include <SharedGraphics/Font.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
static GFontDatabase* s_the;
GFontDatabase& GFontDatabase::the()
{
if (!s_the)
s_the = new GFontDatabase;
return *s_the;
}
GFontDatabase::GFontDatabase()
{
DIR* dirp = opendir("/res/fonts");
if (!dirp) {
perror("opendir");
exit(1);
}
while (auto* de = readdir(dirp)) {
if (de->d_name[0] == '.')
continue;
auto path = String::format("/res/fonts/%s", de->d_name);
if (auto font = Font::load_from_file(path))
m_name_to_path.set(font->name(), path);
}
closedir(dirp);
}
GFontDatabase::~GFontDatabase()
{
}
void GFontDatabase::for_each_font(Function<void(const String&)> callback)
{
for (auto& it : m_name_to_path) {
callback(it.key);
}
}
RetainPtr<Font> GFontDatabase::get_by_name(const String& name)
{
auto it = m_name_to_path.find(name);
if (it == m_name_to_path.end())
return nullptr;
return Font::load_from_file((*it).value);
}

21
LibGUI/GFontDatabase.h Normal file
View file

@ -0,0 +1,21 @@
#pragma once
#include <AK/AKString.h>
#include <AK/HashMap.h>
#include <AK/Function.h>
class Font;
class GFontDatabase {
public:
static GFontDatabase& the();
RetainPtr<Font> get_by_name(const String&);
void for_each_font(Function<void(const String&)>);
private:
GFontDatabase();
~GFontDatabase();
HashMap<String, String> m_name_to_path;
};

View file

@ -26,6 +26,7 @@ LIBGUI_OBJS = \
GMenuItem.o \
GApplication.o \
GAction.o \
GFontDatabase.o \
GWindow.o
OBJS = $(SHAREDGRAPHICS_OBJS) $(LIBGUI_OBJS)