FontDatabase.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/QuickSort.h>
  27. #include <LibCore/DirIterator.h>
  28. #include <LibGfx/Font.h>
  29. #include <LibGUI/FontDatabase.h>
  30. #include <dirent.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. namespace GUI {
  34. static FontDatabase* s_the;
  35. FontDatabase& FontDatabase::the()
  36. {
  37. if (!s_the)
  38. s_the = new FontDatabase;
  39. return *s_the;
  40. }
  41. FontDatabase::FontDatabase()
  42. {
  43. Core::DirIterator di("/res/fonts", Core::DirIterator::SkipDots);
  44. if (di.has_error()) {
  45. fprintf(stderr, "DirIterator: %s\n", di.error_string());
  46. exit(1);
  47. }
  48. while (di.has_next()) {
  49. String name = di.next_path();
  50. if (!name.ends_with(".font"))
  51. continue;
  52. auto path = String::format("/res/fonts/%s", name.characters());
  53. if (auto font = Gfx::Font::load_from_file(path)) {
  54. Metadata metadata;
  55. metadata.path = path;
  56. metadata.glyph_height = font->glyph_height();
  57. metadata.is_fixed_width = font->is_fixed_width();
  58. m_name_to_metadata.set(font->name(), move(metadata));
  59. }
  60. }
  61. }
  62. FontDatabase::~FontDatabase()
  63. {
  64. }
  65. void FontDatabase::for_each_font(Function<void(const StringView&)> callback)
  66. {
  67. Vector<String> names;
  68. names.ensure_capacity(m_name_to_metadata.size());
  69. for (auto& it : m_name_to_metadata)
  70. names.append(it.key);
  71. quick_sort(names);
  72. for (auto& name : names)
  73. callback(name);
  74. }
  75. void FontDatabase::for_each_fixed_width_font(Function<void(const StringView&)> callback)
  76. {
  77. Vector<String> names;
  78. names.ensure_capacity(m_name_to_metadata.size());
  79. for (auto& it : m_name_to_metadata) {
  80. if (it.value.is_fixed_width)
  81. names.append(it.key);
  82. }
  83. quick_sort(names);
  84. for (auto& name : names)
  85. callback(name);
  86. }
  87. RefPtr<Gfx::Font> FontDatabase::get_by_name(const StringView& name)
  88. {
  89. auto it = m_name_to_metadata.find(name);
  90. if (it == m_name_to_metadata.end())
  91. return nullptr;
  92. return Gfx::Font::load_from_file((*it).value.path);
  93. }
  94. }