~aleteoryx/muditaos

muditaos/module-gui/gui/core/FontManager.cpp -rw-r--r-- 7.3 KiB
a405cad6Aleteoryx trim readme 6 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include "FontManager.hpp"
#include "Common.hpp"
#include "FontInfo.hpp"
#include "RawFont.hpp"
#include <log/log.hpp>
#include <json11.hpp>
#include <fstream>
#include <cstring>

namespace
{
    auto getFileSize(const std::string &path) -> std::uintmax_t
    {
        try {
            return std::filesystem::file_size(path);
        }
        catch (const std::filesystem::filesystem_error &e) {
            return 0;
        }
    }
} // namespace

namespace gui
{
    FontManager::~FontManager()
    {
        clear();
    }

    auto FontManager::init(const std::filesystem::path &baseDirectory) -> bool
    {
        loadFonts(baseDirectory);

        const auto fallbackFont = find(fallbackFontName);
        if (fallbackFont == nullptr) {
            return false;
        }

        for (auto &font : fonts) {
            font->setFallbackFont(fallbackFont);
        }

        initialized = true;
        return initialized;
    }

    auto FontManager::clear() -> void
    {
        for (auto font : fonts) {
            LOG_INFO("Deleting font '%s'", font->getName().c_str());
            delete font;
        }
        fonts.clear();
    }

    auto FontManager::getInstance() -> FontManager &
    {
        static FontManager instance;
        if (!instance.initialized) {
            LOG_WARN("Using uninitialized font manager will result in no fonts loaded");
        }
        return instance;
    }

    auto FontManager::getFont() const -> RawFont *
    {
        return getFont(defaultFontName);
    }

    auto FontManager::getFont(std::uint32_t num) const -> RawFont *
    {
        if (fonts.empty()) {
            return nullptr;
        }
        if (num > fonts.size()) {
            return fonts[0];
        }
        return fonts[num];
    }

    auto FontManager::getFont(const std::string &fontType) const -> RawFont *
    {
        const auto fontPath = fontMap.find(fontType);
        if (fontPath != fontMap.end()) {
            auto rawFont = find(fontPath->second);
            if (rawFont != nullptr) {
                return rawFont;
            }
        }
        if (!fonts.empty()) {
#if DEBUG_MISSING_ASSETS == 1
            LOG_ERROR("=> font not found: %s, using default", fontType.c_str());
#endif
            return fonts[0];
        }
        return nullptr;
    }

    auto FontManager::getFontByName(std::string_view name) const -> RawFont *
    {
        const auto font = find(name);

        // Default return first font
        if ((font == nullptr) && !fonts.empty()) {
#if DEBUG_MISSING_ASSETS == 1
            LOG_ERROR("=> font not found: %s, using default", std::string(name).c_str());
#endif
            return fonts[0];
        }
        return font;
    }

    auto FontManager::getFontName(const std::string &fontType) const -> std::string
    {
        return getFont(fontType)->getName();
    }

    auto FontManager::getDefaultFontFamilyName() const -> std::string
    {
        return defaultFontFamilyName;
    }

    auto FontManager::loadFont(const std::string &fontType, const std::filesystem::path &path) -> RawFont *
    {
        const auto fileSize = getFileSize(path);
        if (fileSize == 0) {
            return nullptr;
        }

        auto fontData = std::make_unique<std::uint8_t[]>(fileSize);

        std::ifstream input(path, std::ios::in | std::ifstream::binary);
        if (!input.is_open()) {
            return nullptr;
        }
        if (!input.read(reinterpret_cast<char *>(fontData.get()), static_cast<std::streamsize>(fileSize))) {
            return nullptr;
        }
        const auto bytesRead = static_cast<std::uintmax_t>(input.gcount());
        if (bytesRead != fileSize) {
            LOG_FATAL("Failed to read from file, expected %" PRIuMAX "B, got %" PRIuMAX "B", fileSize, bytesRead);
            return nullptr;
        }

        // Allocate memory for new font
        auto rawFont = new (std::nothrow) RawFont();
        if (rawFont == nullptr) {
            return nullptr;
        }

        if (rawFont->load(fontData.get()) != gui::Status::GUI_SUCCESS) {
            delete rawFont;
            return nullptr;
        }

        // Set id and push it to vector
        rawFont->id = fonts.size();
        fonts.push_back(rawFont);
        fontMap.emplace(fontType, rawFont->getName());
        return rawFont;
    }

    auto FontManager::getFontsList() -> std::map<std::string, std::string>
    {
        const auto fileSize = getFileSize(fontMapFile);
        if (fileSize == 0) {
            LOG_FATAL("File size is zero!");
            return {};
        }

        auto fontmapString = std::make_unique<char[]>(fileSize + 1);
        std::memset(fontmapString.get(), 0, fileSize + 1);

        std::ifstream input(fontMapFile, std::ios::in);
        if (!input.is_open()) {
            LOG_FATAL("Failed to open file '%s'", fontMapFile.c_str());
            return {};
        }
        if (!input.read(fontmapString.get(), static_cast<std::streamsize>(fileSize))) {
            return {};
        }
        const auto bytesRead = static_cast<std::uintmax_t>(input.gcount());
        if (bytesRead != fileSize) {
            LOG_FATAL("Failed to read from file '%s', expected %" PRIuMAX "B, got %" PRIuMAX "B",
                      fontMapFile.c_str(),
                      fileSize,
                      bytesRead);
            return {};
        }

        json11::Json fontmapJson;
        std::string err;
        fontmapJson = json11::Json::parse(fontmapString.get(), err);
        if (!err.empty()) {
            LOG_FATAL("Failed to parse font map string!");
            return {};
        }

        auto fontmapObjects   = fontmapJson.object_items();
        const auto &infoJson  = fontmapObjects["info"];
        fallbackFontName      = infoJson["fallback_font"].string_value();
        defaultFontFamilyName = infoJson["default_font_family"].string_value();
        defaultFontName       = infoJson["default_font"].string_value();

        const auto &styleJson = fontmapObjects["style"];
        std::map<std::string, std::string> fontFiles;

        for (const auto &entry : styleJson.object_items()) {
            const auto &fontName = entry.second.string_value();
            if (!std::filesystem::is_regular_file(fontFolder / fontName)) {
                LOG_WARN("Could not find font '%s'", fontName.c_str());
            }
            else {
                LOG_INFO("Add font to list: '%s' - '%s'", entry.first.c_str(), fontName.c_str());
                fontFiles.emplace(entry.first, fontFolder / fontName);
            }
        }
        LOG_INFO("Total number of fonts: %zu", fontFiles.size());
        return fontFiles;
    }

    auto FontManager::loadFonts(const std::filesystem::path &baseDirectory) -> void
    {
        fontFolder  = baseDirectory / "fonts";
        fontMapFile = fontFolder / "fontmap.json";

        const auto &fontFiles = getFontsList();
        for (const auto &font : fontFiles) {
            loadFont(font.first, font.second);
        }
    }

    auto FontManager::find(std::string_view name) const -> RawFont *
    {
        for (const auto &font : fonts) {
            if (name.compare(font->info.face) == 0) {
                return font;
            }
        }
        return nullptr;
    }
} // namespace gui