GFontDatabase.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <LibGUI/GFontDatabase.h>
  2. #include <SharedGraphics/Font.h>
  3. #include <dirent.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. static GFontDatabase* s_the;
  7. GFontDatabase& GFontDatabase::the()
  8. {
  9. if (!s_the)
  10. s_the = new GFontDatabase;
  11. return *s_the;
  12. }
  13. GFontDatabase::GFontDatabase()
  14. {
  15. DIR* dirp = opendir("/res/fonts");
  16. if (!dirp) {
  17. perror("opendir");
  18. exit(1);
  19. }
  20. while (auto* de = readdir(dirp)) {
  21. if (de->d_name[0] == '.')
  22. continue;
  23. auto path = String::format("/res/fonts/%s", de->d_name);
  24. if (auto font = Font::load_from_file(path))
  25. m_name_to_path.set(font->name(), path);
  26. }
  27. closedir(dirp);
  28. }
  29. GFontDatabase::~GFontDatabase()
  30. {
  31. }
  32. void GFontDatabase::for_each_font(Function<void(const String&)> callback)
  33. {
  34. for (auto& it : m_name_to_path) {
  35. callback(it.key);
  36. }
  37. }
  38. RetainPtr<Font> GFontDatabase::get_by_name(const String& name)
  39. {
  40. auto it = m_name_to_path.find(name);
  41. if (it == m_name_to_path.end())
  42. return nullptr;
  43. return Font::load_from_file((*it).value);
  44. }