FontDatabase.cpp 5.7 KB

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