FontDatabase.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/NonnullRefPtrVector.h>
  7. #include <AK/QuickSort.h>
  8. #include <LibCore/DirIterator.h>
  9. #include <LibGfx/Font.h>
  10. #include <LibGfx/FontDatabase.h>
  11. #include <LibGfx/Typeface.h>
  12. #include <LibTTF/Font.h>
  13. #include <stdlib.h>
  14. namespace Gfx {
  15. static FontDatabase* s_the;
  16. FontDatabase& FontDatabase::the()
  17. {
  18. if (!s_the)
  19. s_the = new FontDatabase;
  20. return *s_the;
  21. }
  22. static RefPtr<Font> s_default_font;
  23. static String s_default_font_query;
  24. static RefPtr<Font> s_fixed_width_font;
  25. static String s_fixed_width_font_query;
  26. void FontDatabase::set_default_font_query(String query)
  27. {
  28. if (s_default_font_query == query)
  29. return;
  30. s_default_font_query = move(query);
  31. s_default_font = nullptr;
  32. }
  33. String FontDatabase::default_font_query()
  34. {
  35. return s_default_font_query;
  36. }
  37. Font& FontDatabase::default_font()
  38. {
  39. if (!s_default_font) {
  40. VERIFY(!s_default_font_query.is_empty());
  41. s_default_font = FontDatabase::the().get_by_name(s_default_font_query);
  42. VERIFY(s_default_font);
  43. }
  44. return *s_default_font;
  45. }
  46. void FontDatabase::set_fixed_width_font_query(String query)
  47. {
  48. if (s_fixed_width_font_query == query)
  49. return;
  50. s_fixed_width_font_query = move(query);
  51. s_fixed_width_font = nullptr;
  52. }
  53. String FontDatabase::fixed_width_font_query()
  54. {
  55. return s_fixed_width_font_query;
  56. }
  57. Font& FontDatabase::default_fixed_width_font()
  58. {
  59. if (!s_fixed_width_font) {
  60. VERIFY(!s_fixed_width_font_query.is_empty());
  61. s_fixed_width_font = FontDatabase::the().get_by_name(s_fixed_width_font_query);
  62. VERIFY(s_fixed_width_font);
  63. }
  64. return *s_fixed_width_font;
  65. }
  66. struct FontDatabase::Private {
  67. HashMap<String, RefPtr<Gfx::Font>> full_name_to_font_map;
  68. Vector<RefPtr<Typeface>> typefaces;
  69. };
  70. FontDatabase::FontDatabase()
  71. : m_private(make<Private>())
  72. {
  73. Core::DirIterator dir_iterator("/res/fonts", Core::DirIterator::SkipDots);
  74. if (dir_iterator.has_error()) {
  75. warnln("DirIterator: {}", dir_iterator.error_string());
  76. exit(1);
  77. }
  78. while (dir_iterator.has_next()) {
  79. auto path = dir_iterator.next_full_path();
  80. if (path.ends_with(".font"sv)) {
  81. if (auto font = Gfx::BitmapFont::load_from_file(path)) {
  82. m_private->full_name_to_font_map.set(font->qualified_name(), font);
  83. auto typeface = get_or_create_typeface(font->family(), font->variant());
  84. typeface->add_bitmap_font(font);
  85. }
  86. } else if (path.ends_with(".ttf"sv)) {
  87. // FIXME: What about .otf and .woff
  88. if (auto font_or_error = TTF::Font::try_load_from_file(path); !font_or_error.is_error()) {
  89. auto font = font_or_error.release_value();
  90. auto typeface = get_or_create_typeface(font->family(), font->variant());
  91. typeface->set_ttf_font(move(font));
  92. }
  93. }
  94. }
  95. }
  96. FontDatabase::~FontDatabase()
  97. {
  98. }
  99. void FontDatabase::for_each_font(Function<void(const Gfx::Font&)> callback)
  100. {
  101. Vector<RefPtr<Gfx::Font>> fonts;
  102. fonts.ensure_capacity(m_private->full_name_to_font_map.size());
  103. for (auto& it : m_private->full_name_to_font_map)
  104. fonts.append(it.value);
  105. quick_sort(fonts, [](auto& a, auto& b) { return a->qualified_name() < b->qualified_name(); });
  106. for (auto& font : fonts)
  107. callback(*font);
  108. }
  109. void FontDatabase::for_each_fixed_width_font(Function<void(const Gfx::Font&)> callback)
  110. {
  111. Vector<RefPtr<Gfx::Font>> fonts;
  112. fonts.ensure_capacity(m_private->full_name_to_font_map.size());
  113. for (auto& it : m_private->full_name_to_font_map) {
  114. if (it.value->is_fixed_width())
  115. fonts.append(it.value);
  116. }
  117. quick_sort(fonts, [](auto& a, auto& b) { return a->qualified_name() < b->qualified_name(); });
  118. for (auto& font : fonts)
  119. callback(*font);
  120. }
  121. RefPtr<Gfx::Font> FontDatabase::get_by_name(const StringView& name)
  122. {
  123. auto it = m_private->full_name_to_font_map.find(name);
  124. if (it == m_private->full_name_to_font_map.end()) {
  125. auto parts = name.split_view(" "sv);
  126. if (parts.size() >= 3) {
  127. auto weight = parts.take_last().to_int().value_or(0);
  128. auto size = parts.take_last().to_int().value_or(0);
  129. auto family = String::join(' ', parts);
  130. return get(family, size, weight);
  131. }
  132. dbgln("Font lookup failed: '{}'", name);
  133. return nullptr;
  134. }
  135. return it->value;
  136. }
  137. RefPtr<Gfx::Font> FontDatabase::get(const String& family, unsigned size, unsigned weight)
  138. {
  139. for (auto typeface : m_private->typefaces) {
  140. if (typeface->family() == family && typeface->weight() == weight)
  141. return typeface->get_font(size);
  142. }
  143. dbgln("Failed to get font: '{} {} {}'", family, size, weight);
  144. return nullptr;
  145. }
  146. RefPtr<Gfx::Font> FontDatabase::get(const String& family, const String& variant, unsigned size)
  147. {
  148. for (auto typeface : m_private->typefaces) {
  149. if (typeface->family() == family && typeface->variant() == variant)
  150. return typeface->get_font(size);
  151. }
  152. return nullptr;
  153. }
  154. RefPtr<Typeface> FontDatabase::get_or_create_typeface(const String& family, const String& variant)
  155. {
  156. for (auto typeface : m_private->typefaces) {
  157. if (typeface->family() == family && typeface->variant() == variant)
  158. return typeface;
  159. }
  160. auto typeface = adopt_ref(*new Typeface(family, variant));
  161. m_private->typefaces.append(typeface);
  162. return typeface;
  163. }
  164. void FontDatabase::for_each_typeface(Function<void(const Typeface&)> callback)
  165. {
  166. for (auto typeface : m_private->typefaces) {
  167. callback(*typeface);
  168. }
  169. }
  170. }