FontDatabase.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 uri = resource.uri();
  104. auto path = LexicalPath(uri.bytes_as_string_view());
  105. if (path.has_extension(".font"sv)) {
  106. if (auto font_or_error = Gfx::BitmapFont::try_load_from_resource(resource); !font_or_error.is_error()) {
  107. auto font = font_or_error.release_value();
  108. m_private->full_name_to_font_map.set(font->qualified_name().to_byte_string(), *font);
  109. auto typeface = get_or_create_typeface(font->family(), font->variant());
  110. typeface->add_bitmap_font(font);
  111. }
  112. } else if (path.has_extension(".ttf"sv)) {
  113. // FIXME: What about .otf
  114. if (auto font_or_error = OpenType::Font::try_load_from_resource(resource); !font_or_error.is_error()) {
  115. auto font = font_or_error.release_value();
  116. auto typeface = get_or_create_typeface(font->family(), font->variant());
  117. typeface->set_vector_font(move(font));
  118. }
  119. } else if (path.has_extension(".woff"sv)) {
  120. if (auto font_or_error = WOFF::Font::try_load_from_resource(resource); !font_or_error.is_error()) {
  121. auto font = font_or_error.release_value();
  122. auto typeface = get_or_create_typeface(font->family(), font->variant());
  123. typeface->set_vector_font(move(font));
  124. }
  125. }
  126. return IterationDecision::Continue;
  127. });
  128. }
  129. FontDatabase::FontDatabase()
  130. : m_private(make<Private>())
  131. {
  132. load_all_fonts_from_uri("resource://fonts"sv);
  133. }
  134. void FontDatabase::for_each_font(Function<void(Gfx::Font const&)> callback)
  135. {
  136. Vector<RefPtr<Gfx::Font>> fonts;
  137. fonts.ensure_capacity(m_private->full_name_to_font_map.size());
  138. for (auto& it : m_private->full_name_to_font_map)
  139. fonts.append(it.value);
  140. quick_sort(fonts, [](auto& a, auto& b) { return a->qualified_name() < b->qualified_name(); });
  141. for (auto& font : fonts)
  142. callback(*font);
  143. }
  144. void FontDatabase::for_each_fixed_width_font(Function<void(Gfx::Font const&)> callback)
  145. {
  146. Vector<RefPtr<Gfx::Font>> fonts;
  147. fonts.ensure_capacity(m_private->full_name_to_font_map.size());
  148. for (auto& it : m_private->full_name_to_font_map) {
  149. if (it.value->is_fixed_width())
  150. fonts.append(it.value);
  151. }
  152. quick_sort(fonts, [](auto& a, auto& b) { return a->qualified_name() < b->qualified_name(); });
  153. for (auto& font : fonts)
  154. callback(*font);
  155. }
  156. RefPtr<Gfx::Font> FontDatabase::get_by_name(StringView name)
  157. {
  158. auto it = m_private->full_name_to_font_map.find(name);
  159. if (it == m_private->full_name_to_font_map.end()) {
  160. auto parts = name.split_view(" "sv);
  161. if (parts.size() >= 4) {
  162. auto slope = parts.take_last().to_number<int>().value_or(0);
  163. auto weight = parts.take_last().to_number<int>().value_or(0);
  164. auto size = parts.take_last().to_number<int>().value_or(0);
  165. auto family = MUST(String::join(' ', parts));
  166. return get(family, size, weight, Gfx::FontWidth::Normal, slope);
  167. }
  168. dbgln("Font lookup failed: '{}'", name);
  169. return nullptr;
  170. }
  171. return it->value;
  172. }
  173. RefPtr<Gfx::Font> FontDatabase::get(FlyString const& family, float point_size, unsigned weight, unsigned width, unsigned slope, Font::AllowInexactSizeMatch allow_inexact_size_match)
  174. {
  175. auto it = m_private->typefaces.find(family);
  176. if (it == m_private->typefaces.end())
  177. return nullptr;
  178. for (auto const& typeface : it->value) {
  179. if (typeface->weight() == weight && typeface->width() == width && typeface->slope() == slope)
  180. return typeface->get_font(point_size, allow_inexact_size_match);
  181. }
  182. return nullptr;
  183. }
  184. RefPtr<Gfx::Font> FontDatabase::get(FlyString const& family, FlyString const& variant, float point_size, Font::AllowInexactSizeMatch allow_inexact_size_match)
  185. {
  186. auto it = m_private->typefaces.find(family);
  187. if (it == m_private->typefaces.end())
  188. return nullptr;
  189. for (auto const& typeface : it->value) {
  190. if (typeface->variant() == variant)
  191. return typeface->get_font(point_size, allow_inexact_size_match);
  192. }
  193. return nullptr;
  194. }
  195. RefPtr<Typeface> FontDatabase::get_or_create_typeface(FlyString const& family, FlyString const& variant)
  196. {
  197. auto it = m_private->typefaces.find(family);
  198. if (it != m_private->typefaces.end()) {
  199. for (auto const& typeface : it->value) {
  200. if (typeface->variant() == variant)
  201. return typeface;
  202. }
  203. }
  204. auto typeface = adopt_ref(*new Typeface(family, variant));
  205. m_private->typefaces.ensure(family).append(typeface);
  206. return typeface;
  207. }
  208. void FontDatabase::for_each_typeface(Function<void(Typeface const&)> callback)
  209. {
  210. for (auto const& it : m_private->typefaces) {
  211. for (auto const& jt : it.value) {
  212. callback(*jt);
  213. }
  214. }
  215. }
  216. void FontDatabase::for_each_typeface_with_family_name(FlyString const& family_name, Function<void(Typeface const&)> callback)
  217. {
  218. auto it = m_private->typefaces.find(family_name);
  219. if (it == m_private->typefaces.end())
  220. return;
  221. for (auto const& typeface : it->value)
  222. callback(*typeface);
  223. }
  224. }