Serenity Operating System
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
27#include <AK/QuickSort.h>
28#include <LibCore/DirIterator.h>
29#include <LibGfx/Font.h>
30#include <LibGUI/FontDatabase.h>
31#include <dirent.h>
32#include <stdio.h>
33#include <stdlib.h>
34
35namespace GUI {
36
37static FontDatabase* s_the;
38
39FontDatabase& FontDatabase::the()
40{
41 if (!s_the)
42 s_the = new FontDatabase;
43 return *s_the;
44}
45
46FontDatabase::FontDatabase()
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
58 auto path = String::format("/res/fonts/%s", name.characters());
59 if (auto font = Gfx::Font::load_from_file(path)) {
60 Metadata metadata;
61 metadata.path = path;
62 metadata.glyph_height = font->glyph_height();
63 metadata.is_fixed_width = font->is_fixed_width();
64 m_name_to_metadata.set(font->name(), move(metadata));
65 }
66 }
67}
68
69FontDatabase::~FontDatabase()
70{
71}
72
73void FontDatabase::for_each_font(Function<void(const StringView&)> callback)
74{
75 Vector<String> names;
76 names.ensure_capacity(m_name_to_metadata.size());
77 for (auto& it : m_name_to_metadata)
78 names.append(it.key);
79 quick_sort(names);
80 for (auto& name : names)
81 callback(name);
82}
83
84void FontDatabase::for_each_fixed_width_font(Function<void(const StringView&)> callback)
85{
86 Vector<String> names;
87 names.ensure_capacity(m_name_to_metadata.size());
88 for (auto& it : m_name_to_metadata) {
89 if (it.value.is_fixed_width)
90 names.append(it.key);
91 }
92 quick_sort(names);
93 for (auto& name : names)
94 callback(name);
95}
96
97RefPtr<Gfx::Font> FontDatabase::get_by_name(const StringView& name)
98{
99 auto it = m_name_to_metadata.find(name);
100 if (it == m_name_to_metadata.end())
101 return nullptr;
102 return Gfx::Font::load_from_file((*it).value.path);
103}
104
105}