FontDatabase.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/NonnullRefPtrVector.h>
  27. #include <AK/QuickSort.h>
  28. #include <LibCore/DirIterator.h>
  29. #include <LibGfx/Font.h>
  30. #include <LibGfx/FontDatabase.h>
  31. #include <LibGfx/Typeface.h>
  32. #include <LibTTF/Font.h>
  33. #include <stdlib.h>
  34. namespace Gfx {
  35. static FontDatabase* s_the;
  36. FontDatabase& FontDatabase::the()
  37. {
  38. if (!s_the)
  39. s_the = new FontDatabase;
  40. return *s_the;
  41. }
  42. Font& FontDatabase::default_font()
  43. {
  44. static Font* font;
  45. if (!font) {
  46. font = FontDatabase::the().get_by_name("Katica 10 400");
  47. VERIFY(font);
  48. }
  49. return *font;
  50. }
  51. Font& FontDatabase::default_fixed_width_font()
  52. {
  53. static Font* font;
  54. if (!font) {
  55. font = FontDatabase::the().get_by_name("Csilla 10 400");
  56. VERIFY(font);
  57. }
  58. return *font;
  59. }
  60. Font& FontDatabase::default_bold_fixed_width_font()
  61. {
  62. static Font* font;
  63. if (!font) {
  64. font = FontDatabase::the().get_by_name("Csilla 10 700");
  65. VERIFY(font);
  66. }
  67. return *font;
  68. }
  69. Font& FontDatabase::default_bold_font()
  70. {
  71. static Font* font;
  72. if (!font) {
  73. font = FontDatabase::the().get_by_name("Katica 10 700");
  74. VERIFY(font);
  75. }
  76. return *font;
  77. }
  78. struct FontDatabase::Private {
  79. HashMap<String, RefPtr<Gfx::Font>> full_name_to_font_map;
  80. Vector<RefPtr<Typeface>> typefaces;
  81. };
  82. FontDatabase::FontDatabase()
  83. : m_private(make<Private>())
  84. {
  85. Core::DirIterator dir_iterator("/res/fonts", Core::DirIterator::SkipDots);
  86. if (dir_iterator.has_error()) {
  87. warnln("DirIterator: {}", dir_iterator.error_string());
  88. exit(1);
  89. }
  90. while (dir_iterator.has_next()) {
  91. auto path = dir_iterator.next_full_path();
  92. if (path.ends_with(".font"sv)) {
  93. if (auto font = Gfx::BitmapFont::load_from_file(path)) {
  94. m_private->full_name_to_font_map.set(font->qualified_name(), font);
  95. auto typeface = get_or_create_typeface(font->family(), font->variant());
  96. typeface->add_bitmap_font(font);
  97. }
  98. } else if (path.ends_with(".ttf"sv)) {
  99. // FIXME: What about .otf and .woff
  100. if (auto font = TTF::Font::load_from_file(path)) {
  101. auto typeface = get_or_create_typeface(font->family(), font->variant());
  102. typeface->set_ttf_font(font);
  103. }
  104. }
  105. }
  106. }
  107. FontDatabase::~FontDatabase()
  108. {
  109. }
  110. void FontDatabase::for_each_font(Function<void(const Gfx::Font&)> callback)
  111. {
  112. Vector<RefPtr<Gfx::Font>> fonts;
  113. fonts.ensure_capacity(m_private->full_name_to_font_map.size());
  114. for (auto& it : m_private->full_name_to_font_map)
  115. fonts.append(it.value);
  116. quick_sort(fonts, [](auto& a, auto& b) { return a->qualified_name() < b->qualified_name(); });
  117. for (auto& font : fonts)
  118. callback(*font);
  119. }
  120. void FontDatabase::for_each_fixed_width_font(Function<void(const Gfx::Font&)> callback)
  121. {
  122. Vector<RefPtr<Gfx::Font>> fonts;
  123. fonts.ensure_capacity(m_private->full_name_to_font_map.size());
  124. for (auto& it : m_private->full_name_to_font_map) {
  125. if (it.value->is_fixed_width())
  126. fonts.append(it.value);
  127. }
  128. quick_sort(fonts, [](auto& a, auto& b) { return a->qualified_name() < b->qualified_name(); });
  129. for (auto& font : fonts)
  130. callback(*font);
  131. }
  132. RefPtr<Gfx::Font> FontDatabase::get_by_name(const StringView& name)
  133. {
  134. auto it = m_private->full_name_to_font_map.find(name);
  135. if (it == m_private->full_name_to_font_map.end()) {
  136. dbgln("Font lookup failed: '{}'", name);
  137. return nullptr;
  138. }
  139. return it->value;
  140. }
  141. RefPtr<Gfx::Font> FontDatabase::get(const String& family, unsigned size, unsigned weight)
  142. {
  143. for (auto typeface : m_private->typefaces) {
  144. if (typeface->family() == family && typeface->weight() == weight)
  145. return typeface->get_font(size);
  146. }
  147. return nullptr;
  148. }
  149. RefPtr<Gfx::Font> FontDatabase::get(const String& family, const String& variant, unsigned size)
  150. {
  151. for (auto typeface : m_private->typefaces) {
  152. if (typeface->family() == family && typeface->variant() == variant)
  153. return typeface->get_font(size);
  154. }
  155. return nullptr;
  156. }
  157. RefPtr<Typeface> FontDatabase::get_or_create_typeface(const String& family, const String& variant)
  158. {
  159. for (auto typeface : m_private->typefaces) {
  160. if (typeface->family() == family && typeface->variant() == variant)
  161. return typeface;
  162. }
  163. auto typeface = adopt(*new Typeface(family, variant));
  164. m_private->typefaces.append(typeface);
  165. return typeface;
  166. }
  167. void FontDatabase::for_each_typeface(Function<void(const Typeface&)> callback)
  168. {
  169. for (auto typeface : m_private->typefaces) {
  170. callback(*typeface);
  171. }
  172. }
  173. }