FontDatabase.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <LibGUI/FontDatabase.h>
  30. #include <LibGfx/Font.h>
  31. #include <dirent.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. namespace GUI {
  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. struct FontDatabase::Private {
  43. HashMap<String, RefPtr<Gfx::Font>> full_name_to_font_map;
  44. };
  45. FontDatabase::FontDatabase()
  46. : m_private(make<Private>())
  47. {
  48. Core::DirIterator di("/res/fonts", Core::DirIterator::SkipDots);
  49. if (di.has_error()) {
  50. fprintf(stderr, "DirIterator: %s\n", di.error_string());
  51. exit(1);
  52. }
  53. while (di.has_next()) {
  54. String name = di.next_path();
  55. if (!name.ends_with(".font"))
  56. continue;
  57. auto path = String::format("/res/fonts/%s", name.characters());
  58. if (auto font = Gfx::Font::load_from_file(path)) {
  59. m_private->full_name_to_font_map.set(font->qualified_name(), font);
  60. }
  61. }
  62. }
  63. FontDatabase::~FontDatabase()
  64. {
  65. }
  66. void FontDatabase::for_each_font(Function<void(const Gfx::Font&)> callback)
  67. {
  68. Vector<RefPtr<Gfx::Font>> fonts;
  69. fonts.ensure_capacity(m_private->full_name_to_font_map.size());
  70. for (auto& it : m_private->full_name_to_font_map)
  71. fonts.append(it.value);
  72. quick_sort(fonts, [](auto& a, auto& b) { return a->qualified_name() < b->qualified_name(); });
  73. for (auto& font : fonts)
  74. callback(*font);
  75. }
  76. void FontDatabase::for_each_fixed_width_font(Function<void(const Gfx::Font&)> callback)
  77. {
  78. Vector<RefPtr<Gfx::Font>> fonts;
  79. fonts.ensure_capacity(m_private->full_name_to_font_map.size());
  80. for (auto& it : m_private->full_name_to_font_map) {
  81. if (it.value->is_fixed_width())
  82. fonts.append(it.value);
  83. }
  84. quick_sort(fonts, [](auto& a, auto& b) { return a->qualified_name() < b->qualified_name(); });
  85. for (auto& font : fonts)
  86. callback(*font);
  87. }
  88. RefPtr<Gfx::Font> FontDatabase::get_by_name(const StringView& name)
  89. {
  90. auto it = m_private->full_name_to_font_map.find(name);
  91. if (it == m_private->full_name_to_font_map.end()) {
  92. dbgln("Font lookup failed: '{}'", name);
  93. return nullptr;
  94. }
  95. dbgln("Font lookup succeeded: '{}'", name);
  96. return it->value;
  97. }
  98. }