~aleteoryx/muditaos

ref: 7eb1278f0b19713264cf4401475d215ad11a220f muditaos/module-gui/gui/core/Font.cpp -rw-r--r-- 2.6 KiB
7eb1278f — Maciej Gibowicz [BH-1476] Add greetings in all languages 1 year, 4 months 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "Font.hpp"
#include "FontManager.hpp" // for FontManager
#include "RawFont.hpp"
#include <log/log.hpp>
#include <algorithm>
#include <sstream>

namespace gui
{

    auto toWeight(const std::string &val) -> Font::Weight;

    Font::Font(std::string name, unsigned int size, Weight weight)
    {
        setFont(name, size, weight);
    }

    Font::Font(unsigned int size, Weight weight)
        : Font(FontManager::getInstance().getDefaultFontFamilyName(), size, weight)
    {}

    Font::Font(RawFont *rawfont)
    {
        if (rawfont == nullptr) {
            font = FontManager::getInstance().getFont(""); // get default
        }

        auto name  = rawfont->getName();
        auto pos   = name.npos;
        auto parse = [&]() {
            pos      = name.rfind('_');
            auto val = name.substr(pos + 1, name.length());
            name.erase(pos);
            return val;
        };

        unsigned int size = std::stoi(parse());
        Weight weight     = toWeight(parse());

        setFont(name, size, weight);
    }
    void Font::setFont(std::string new_name, unsigned int new_size, Weight new_weight)
    {
        bool update = false;
        auto set    = [&](auto &val, auto &new_val) {
            if (val != new_val) {
                val    = new_val;
                update = true;
            }
        };
        set(name, new_name);
        set(size, new_size);
        set(weight, new_weight);
        if (update) {
            std::string raw_font_name = new_name + "_" + c_str(new_weight) + "_" + std::to_string(new_size);
            font                      = FontManager::getInstance().getFontByName(raw_font_name);
        }
    }

    void Font::setFont(unsigned int size, Weight weight)
    {
        setFont((FontManager::getInstance().getDefaultFontFamilyName()), size, weight);
    }

    void Font::setSize(unsigned int new_size)
    {
        setFont(name, new_size, weight);
    }

    void Font::setWeight(Weight new_weight)
    {
        setFont(name, size, new_weight);
    }

    auto Font::raw() -> RawFont *
    {
        return font;
    }

    auto toWeight(const std::string &val) -> Font::Weight
    {
        if (val == c_str(Font::Weight::Regular)) {
            return Font::Weight::Regular;
        }
        else if (val == c_str(Font::Weight::Bold)) {
            return Font::Weight::Bold;
        }
        else if (val == c_str(Font::Weight::Light)) {
            return Font::Weight::Light;
        }
        return Font::Weight::Regular;
    }
}; // namespace gui