~aleteoryx/muditaos

ref: 14901580347673ce91e308950a2b73b70ba9dcf3 muditaos/module-gui/gui/core/FontManager.cpp -rw-r--r-- 4.6 KiB
14901580 — Alek Rudnik [EGD-7725] Tune up file system io access 4 years 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "FontManager.hpp"
#include "Common.hpp"   // for Status, Status::GUI_SUCCESS
#include "FontInfo.hpp" // for FontInfo
#include "RawFont.hpp"  // for RawFont
#include <log/log.hpp>  // for LOG_ERROR, LOG_INFO, LOG_WARN
#include <Utils.hpp>
#include <filesystem>
#include <cstdio>

namespace style::window::font
{
    inline constexpr auto default_fallback_font = "dejavu_sans_bold_27";
}

namespace gui
{

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

    void FontManager::clear()
    {
        for (RawFont *font : fonts) {
            delete font;
        }
        fonts.clear();
    }

    void FontManager::loadFonts(std::string baseDirectory)
    {
        fontFolder                         = baseDirectory + "/fonts";
        std::vector<std::string> fontFiles = getFontsList();

        for (std::string fontName : fontFiles) {
            loadFont(fontName);
        }
    }

    RawFont *FontManager::loadFont(std::string filename)
    {

        auto file = std::fopen(filename.c_str(), "rb");

        auto fileSize = std::filesystem::file_size(filename);
        std::rewind(file);

        char *fontData = new char[fileSize];
        if (fontData == nullptr) {
            std::fclose(file);
            LOG_ERROR(" Failed to allocate temporary font buffer");
            return nullptr;
        }

        // read data to buffer
        auto bytesRead = std::fread(fontData, 1, fileSize, file);

        // close file
        std::fclose(file);
        if (static_cast<uintmax_t>(bytesRead) != fileSize) {
            LOG_ERROR("Failed to read all file");
            delete[] fontData;
            return nullptr;
        }

        // allocate memory for new font
        RawFont *font = new RawFont();
        if (font->load(reinterpret_cast<uint8_t *>(fontData)) != gui::Status::GUI_SUCCESS) {
            delete font;
            delete[] fontData;
            return nullptr;
        }
        else {
            // set id and push it to vector
            font->id = fonts.size();
            fonts.push_back(font);
        }
        delete[] fontData;
        return font;
    }

    bool hasEnding(std::string const &fullString, std::string const &ending)
    {
        if (fullString.length() >= ending.length()) {
            return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending));
        }
        else {
            return false;
        }
    }

    std::vector<std::string> FontManager::getFontsList()
    {

        std::vector<std::string> fontFiles;

        LOG_INFO("Scanning fonts folder: %s", fontFolder.c_str());

        for (const auto &entry : std::filesystem::directory_iterator(fontFolder)) {
            if (!std::filesystem::is_directory(entry) && entry.path().extension() == ".mpf") {
                fontFiles.push_back(entry.path().string());
            }
        }

        LOG_INFO("Total number of fonts: %u", static_cast<unsigned int>(fontFiles.size()));
        return fontFiles;
    }

    bool FontManager::init(std::string baseDirectory)
    {
        loadFonts(baseDirectory);

        auto fallback_font = find(style::window::font::default_fallback_font);
        if (fallback_font != nullptr) {
            for (auto font : fonts) {
                font->setFallbackFont(fallback_font);
            }
        }
        initialized = true;
        return initialized;
    }

    FontManager &FontManager::getInstance()
    {

        static FontManager instance;
        if (!instance.initialized) {
            LOG_WARN("Using uninitialized font manager will result in no font loaded");
        }
        return instance;
    }

    [[nodiscard]] auto FontManager::getFont(std::string_view name) const -> RawFont *
    {
        auto font = find(name);
        // default return first font
        if (font == nullptr && fonts.size() > 0) {
            LOG_ERROR("=> font not found: %s using default", name.data());
            return fonts[0];
        }
        return font;
    }

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

    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