FontDatabase.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 <stdio.h>
  34. #include <stdlib.h>
  35. namespace Gfx {
  36. static FontDatabase* s_the;
  37. FontDatabase& FontDatabase::the()
  38. {
  39. if (!s_the)
  40. s_the = new FontDatabase;
  41. return *s_the;
  42. }
  43. Font& FontDatabase::default_font()
  44. {
  45. static Font* font;
  46. if (!font) {
  47. font = FontDatabase::the().get_by_name("Katica 10 400");
  48. ASSERT(font);
  49. }
  50. return *font;
  51. }
  52. Font& FontDatabase::default_fixed_width_font()
  53. {
  54. static Font* font;
  55. if (!font) {
  56. font = FontDatabase::the().get_by_name("Csilla 10 400");
  57. ASSERT(font);
  58. }
  59. return *font;
  60. }
  61. Font& FontDatabase::default_bold_fixed_width_font()
  62. {
  63. static Font* font;
  64. if (!font) {
  65. font = FontDatabase::the().get_by_name("Csilla 10 700");
  66. ASSERT(font);
  67. }
  68. return *font;
  69. }
  70. Font& FontDatabase::default_bold_font()
  71. {
  72. static Font* font;
  73. if (!font) {
  74. font = FontDatabase::the().get_by_name("Katica 10 700");
  75. ASSERT(font);
  76. }
  77. return *font;
  78. }
  79. struct FontDatabase::Private {
  80. HashMap<String, RefPtr<Gfx::Font>> full_name_to_font_map;
  81. Vector<RefPtr<Typeface>> typefaces;
  82. };
  83. FontDatabase::FontDatabase()
  84. : m_private(make<Private>())
  85. {
  86. Core::DirIterator di("/res/fonts", Core::DirIterator::SkipDots);
  87. if (di.has_error()) {
  88. fprintf(stderr, "DirIterator: %s\n", di.error_string());
  89. exit(1);
  90. }
  91. while (di.has_next()) {
  92. String name = di.next_path();
  93. auto path = String::format("/res/fonts/%s", name.characters());
  94. if (name.ends_with(".font")) {
  95. if (auto font = Gfx::BitmapFont::load_from_file(path)) {
  96. m_private->full_name_to_font_map.set(font->qualified_name(), font);
  97. auto typeface = get_or_create_typeface(font->family(), font->variant());
  98. typeface->add_bitmap_font(font);
  99. }
  100. } else if (name.ends_with(".ttf")) {
  101. // FIXME: What about .otf and .woff
  102. if (auto font = TTF::Font::load_from_file(path)) {
  103. auto typeface = get_or_create_typeface(font->family(), font->variant());
  104. typeface->set_ttf_font(font);
  105. }
  106. }
  107. }
  108. }
  109. FontDatabase::~FontDatabase()
  110. {
  111. }
  112. void FontDatabase::for_each_font(Function<void(const Gfx::Font&)> callback)
  113. {
  114. Vector<RefPtr<Gfx::Font>> fonts;
  115. fonts.ensure_capacity(m_private->full_name_to_font_map.size());
  116. for (auto& it : m_private->full_name_to_font_map)
  117. fonts.append(it.value);
  118. quick_sort(fonts, [](auto& a, auto& b) { return a->qualified_name() < b->qualified_name(); });
  119. for (auto& font : fonts)
  120. callback(*font);
  121. }
  122. void FontDatabase::for_each_fixed_width_font(Function<void(const Gfx::Font&)> callback)
  123. {
  124. Vector<RefPtr<Gfx::Font>> fonts;
  125. fonts.ensure_capacity(m_private->full_name_to_font_map.size());
  126. for (auto& it : m_private->full_name_to_font_map) {
  127. if (it.value->is_fixed_width())
  128. fonts.append(it.value);
  129. }
  130. quick_sort(fonts, [](auto& a, auto& b) { return a->qualified_name() < b->qualified_name(); });
  131. for (auto& font : fonts)
  132. callback(*font);
  133. }
  134. RefPtr<Gfx::Font> FontDatabase::get_by_name(const StringView& name)
  135. {
  136. auto it = m_private->full_name_to_font_map.find(name);
  137. if (it == m_private->full_name_to_font_map.end()) {
  138. dbgln("Font lookup failed: '{}'", name);
  139. return nullptr;
  140. }
  141. return it->value;
  142. }
  143. RefPtr<Gfx::Font> FontDatabase::get(const String& family, unsigned size, unsigned weight)
  144. {
  145. for (auto typeface : m_private->typefaces) {
  146. if (typeface->family() == family && typeface->weight() == weight)
  147. return typeface->get_font(size);
  148. }
  149. return nullptr;
  150. }
  151. RefPtr<Gfx::Font> FontDatabase::get(const String& family, const String& variant, unsigned size)
  152. {
  153. for (auto typeface : m_private->typefaces) {
  154. if (typeface->family() == family && typeface->variant() == variant)
  155. return typeface->get_font(size);
  156. }
  157. return nullptr;
  158. }
  159. RefPtr<Typeface> FontDatabase::get_or_create_typeface(const String& family, const String& variant)
  160. {
  161. for (auto typeface : m_private->typefaces) {
  162. if (typeface->family() == family && typeface->variant() == variant)
  163. return typeface;
  164. }
  165. auto typeface = adopt(*new Typeface(family, variant));
  166. m_private->typefaces.append(typeface);
  167. return typeface;
  168. }
  169. void FontDatabase::for_each_typeface(Function<void(const Typeface&)> callback)
  170. {
  171. for (auto typeface : m_private->typefaces) {
  172. callback(*typeface);
  173. }
  174. }
  175. }