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