aboutsummaryrefslogtreecommitdiff
path: root/src/FontLoader.cpp
blob: ca333772b0a8c3ef7b9169ca78aef3ba7f13dd45 (plain)
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
#include "../include/FontLoader.hpp"
#include <SFML/Graphics/Font.hpp>
#include <array>
#include <assert.h>

static std::array<std::unique_ptr<sf::Font>, 3> font_cache;

namespace QuickMedia::FontLoader {
    sf::Font* get_font(FontType font_type) {
        sf::Font *font = font_cache[(size_t)font_type].get();
        if(!font) {
            auto new_font = std::make_unique<sf::Font>();
            std::vector<std::string> noto_directories;
            std::string font_file_name;
            switch(font_type) {
                case FontType::LATIN: {
                    noto_directories.push_back("/usr/share/fonts/noto");
                    noto_directories.push_back("/usr/share/fonts/truetype/noto");
                    font_file_name = "NotoSans-Regular.ttf";
                    break;
                }
                case FontType::LATIN_BOLD: {
                    noto_directories.push_back("/usr/share/fonts/noto");
                    noto_directories.push_back("/usr/share/fonts/truetype/noto");
                    font_file_name = "NotoSans-Bold.ttf";
                    break;
                }
                case FontType::CJK: {
                    noto_directories.push_back("/usr/share/fonts/noto-cjk");
                    noto_directories.push_back("/usr/share/fonts/truetype/noto-cjk");
                    font_file_name = "NotoSansCJK-Regular.ttc";
                    break;
                }
                case FontType::EMOJI: {
                    noto_directories.push_back("/usr/share/fonts/noto");
                    noto_directories.push_back("/usr/share/fonts/truetype/noto");
                    font_file_name = "NotoColorEmoji.ttf";
                    break;
                }
            }

            for(const std::string &noto_dir : noto_directories) {
                if(new_font->loadFromFile(noto_dir + "/" + font_file_name))
                    break;
            }

            font_cache[(size_t)font_type] = std::move(new_font);
            font = font_cache[(size_t)font_type].get();
        }
        return font;
    }
}