FontDatabase.cpp 5.6 KB

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