FontDatabase.cpp 3.4 KB

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