FontDatabase.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/DeprecatedFlyString.h>
  7. #include <AK/FlyString.h>
  8. #include <AK/LexicalPath.h>
  9. #include <AK/Queue.h>
  10. #include <AK/QuickSort.h>
  11. #include <LibCore/Resource.h>
  12. #include <LibFileSystem/FileSystem.h>
  13. #include <LibGfx/Font/Font.h>
  14. #include <LibGfx/Font/FontDatabase.h>
  15. #include <LibGfx/Font/OpenType/Font.h>
  16. #include <LibGfx/Font/Typeface.h>
  17. #include <LibGfx/Font/WOFF/Font.h>
  18. namespace Gfx {
  19. FontDatabase& FontDatabase::the()
  20. {
  21. static FontDatabase s_the;
  22. return s_the;
  23. }
  24. static RefPtr<Font> s_default_font;
  25. static ByteString s_default_font_query;
  26. static RefPtr<Font> s_window_title_font;
  27. static ByteString s_window_title_font_query;
  28. static RefPtr<Font> s_fixed_width_font;
  29. static ByteString s_fixed_width_font_query;
  30. void FontDatabase::set_default_font_query(ByteString query)
  31. {
  32. if (s_default_font_query == query)
  33. return;
  34. s_default_font_query = move(query);
  35. s_default_font = nullptr;
  36. }
  37. ByteString FontDatabase::default_font_query()
  38. {
  39. return s_default_font_query;
  40. }
  41. void FontDatabase::set_window_title_font_query(ByteString query)
  42. {
  43. if (s_window_title_font_query == query)
  44. return;
  45. s_window_title_font_query = move(query);
  46. s_window_title_font = nullptr;
  47. }
  48. ByteString FontDatabase::window_title_font_query()
  49. {
  50. return s_window_title_font_query;
  51. }
  52. Font& FontDatabase::default_font()
  53. {
  54. if (!s_default_font) {
  55. VERIFY(!s_default_font_query.is_empty());
  56. s_default_font = FontDatabase::the().get_by_name(s_default_font_query);
  57. VERIFY(s_default_font);
  58. }
  59. return *s_default_font;
  60. }
  61. Font& FontDatabase::window_title_font()
  62. {
  63. if (!s_window_title_font) {
  64. VERIFY(!s_window_title_font_query.is_empty());
  65. s_window_title_font = FontDatabase::the().get_by_name(s_window_title_font_query);
  66. VERIFY(s_window_title_font);
  67. }
  68. return *s_window_title_font;
  69. }
  70. void FontDatabase::set_fixed_width_font_query(ByteString query)
  71. {
  72. if (s_fixed_width_font_query == query)
  73. return;
  74. s_fixed_width_font_query = move(query);
  75. s_fixed_width_font = nullptr;
  76. }
  77. ByteString FontDatabase::fixed_width_font_query()
  78. {
  79. return s_fixed_width_font_query;
  80. }
  81. Font& FontDatabase::default_fixed_width_font()
  82. {
  83. if (!s_fixed_width_font) {
  84. VERIFY(!s_fixed_width_font_query.is_empty());
  85. s_fixed_width_font = FontDatabase::the().get_by_name(s_fixed_width_font_query);
  86. VERIFY(s_fixed_width_font);
  87. }
  88. return *s_fixed_width_font;
  89. }
  90. struct FontDatabase::Private {
  91. HashMap<ByteString, NonnullRefPtr<Gfx::Font>, CaseInsensitiveStringTraits> full_name_to_font_map;
  92. HashMap<FlyString, Vector<NonnullRefPtr<Typeface>>, AK::ASCIICaseInsensitiveFlyStringTraits> typefaces;
  93. };
  94. void FontDatabase::load_all_fonts_from_uri(StringView uri)
  95. {
  96. auto root_or_error = Core::Resource::load_from_uri(uri);
  97. if (root_or_error.is_error()) {
  98. dbgln("FontDatabase::load_all_fonts_from_uri('{}'): {}", uri, root_or_error.error());
  99. return;
  100. }
  101. auto root = root_or_error.release_value();
  102. root->for_each_descendant_file([this](Core::Resource const& resource) -> IterationDecision {
  103. auto path = LexicalPath(resource.uri().bytes_as_string_view());
  104. if (path.has_extension(".font"sv)) {
  105. if (auto font_or_error = Gfx::BitmapFont::try_load_from_resource(resource); !font_or_error.is_error()) {
  106. auto font = font_or_error.release_value();
  107. m_private->full_name_to_font_map.set(font->qualified_name().to_byte_string(), *font);
  108. auto typeface = get_or_create_typeface(font->family(), font->variant());
  109. typeface->add_bitmap_font(font);
  110. }
  111. } else if (path.has_extension(".ttf"sv)) {
  112. // FIXME: What about .otf
  113. if (auto font_or_error = OpenType::Font::try_load_from_resource(resource); !font_or_error.is_error()) {
  114. auto font = font_or_error.release_value();
  115. auto typeface = get_or_create_typeface(font->family(), font->variant());
  116. typeface->set_vector_font(move(font));
  117. }
  118. } else if (path.has_extension(".woff"sv)) {
  119. if (auto font_or_error = WOFF::Font::try_load_from_resource(resource); !font_or_error.is_error()) {
  120. auto font = font_or_error.release_value();
  121. auto typeface = get_or_create_typeface(font->family(), font->variant());
  122. typeface->set_vector_font(move(font));
  123. }
  124. }
  125. return IterationDecision::Continue;
  126. });
  127. }
  128. FontDatabase::FontDatabase()
  129. : m_private(make<Private>())
  130. {
  131. load_all_fonts_from_uri("resource://fonts"sv);
  132. }
  133. void FontDatabase::for_each_font(Function<void(Gfx::Font const&)> callback)
  134. {
  135. Vector<RefPtr<Gfx::Font>> fonts;
  136. fonts.ensure_capacity(m_private->full_name_to_font_map.size());
  137. for (auto& it : m_private->full_name_to_font_map)
  138. fonts.append(it.value);
  139. quick_sort(fonts, [](auto& a, auto& b) { return a->qualified_name() < b->qualified_name(); });
  140. for (auto& font : fonts)
  141. callback(*font);
  142. }
  143. void FontDatabase::for_each_fixed_width_font(Function<void(Gfx::Font const&)> callback)
  144. {
  145. Vector<RefPtr<Gfx::Font>> fonts;
  146. fonts.ensure_capacity(m_private->full_name_to_font_map.size());
  147. for (auto& it : m_private->full_name_to_font_map) {
  148. if (it.value->is_fixed_width())
  149. fonts.append(it.value);
  150. }
  151. quick_sort(fonts, [](auto& a, auto& b) { return a->qualified_name() < b->qualified_name(); });
  152. for (auto& font : fonts)
  153. callback(*font);
  154. }
  155. RefPtr<Gfx::Font> FontDatabase::get_by_name(StringView name)
  156. {
  157. auto it = m_private->full_name_to_font_map.find(name);
  158. if (it == m_private->full_name_to_font_map.end()) {
  159. auto parts = name.split_view(" "sv);
  160. if (parts.size() >= 4) {
  161. auto slope = parts.take_last().to_number<int>().value_or(0);
  162. auto weight = parts.take_last().to_number<int>().value_or(0);
  163. auto size = parts.take_last().to_number<int>().value_or(0);
  164. auto family = MUST(String::join(' ', parts));
  165. return get(family, size, weight, Gfx::FontWidth::Normal, slope);
  166. }
  167. dbgln("Font lookup failed: '{}'", name);
  168. return nullptr;
  169. }
  170. return it->value;
  171. }
  172. RefPtr<Gfx::Font> FontDatabase::get(FlyString const& family, float point_size, unsigned weight, unsigned width, unsigned slope, Font::AllowInexactSizeMatch allow_inexact_size_match)
  173. {
  174. auto it = m_private->typefaces.find(family);
  175. if (it == m_private->typefaces.end())
  176. return nullptr;
  177. for (auto const& typeface : it->value) {
  178. if (typeface->weight() == weight && typeface->width() == width && typeface->slope() == slope)
  179. return typeface->get_font(point_size, allow_inexact_size_match);
  180. }
  181. return nullptr;
  182. }
  183. RefPtr<Gfx::Font> FontDatabase::get(FlyString const& family, FlyString const& variant, float point_size, Font::AllowInexactSizeMatch allow_inexact_size_match)
  184. {
  185. auto it = m_private->typefaces.find(family);
  186. if (it == m_private->typefaces.end())
  187. return nullptr;
  188. for (auto const& typeface : it->value) {
  189. if (typeface->variant() == variant)
  190. return typeface->get_font(point_size, allow_inexact_size_match);
  191. }
  192. return nullptr;
  193. }
  194. RefPtr<Typeface> FontDatabase::get_or_create_typeface(FlyString const& family, FlyString const& variant)
  195. {
  196. auto it = m_private->typefaces.find(family);
  197. if (it != m_private->typefaces.end()) {
  198. for (auto const& typeface : it->value) {
  199. if (typeface->variant() == variant)
  200. return typeface;
  201. }
  202. }
  203. auto typeface = adopt_ref(*new Typeface(family, variant));
  204. m_private->typefaces.ensure(family).append(typeface);
  205. return typeface;
  206. }
  207. void FontDatabase::for_each_typeface(Function<void(Typeface const&)> callback)
  208. {
  209. for (auto const& it : m_private->typefaces) {
  210. for (auto const& jt : it.value) {
  211. callback(*jt);
  212. }
  213. }
  214. }
  215. void FontDatabase::for_each_typeface_with_family_name(FlyString const& family_name, Function<void(Typeface const&)> callback)
  216. {
  217. auto it = m_private->typefaces.find(family_name);
  218. if (it == m_private->typefaces.end())
  219. return;
  220. for (auto const& typeface : it->value)
  221. callback(*typeface);
  222. }
  223. }