aboutsummaryrefslogtreecommitdiff
path: root/src/resource_loader.c
blob: 2eb0c01d97baee7b64ccf9d996a2af6716f2ff7d (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
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
#include "../include/resource_loader.h"
#include "../include/alloc.h"
#include <mgl/system/fileutils.h>
#include <mgl/graphics/font.h>
#include <stdio.h>

/* Note: max font size: 100 */
static mgl_memory_mapped_file *font_file_cache[4];
static mgl_font *font_cache[4][100];

/* TODO: If font fails to load, then create a new font with only a squares that represents all glyphs */
/* TODO: Load default system font from fontconfig files */
mgl_font* mgui_get_font(mgui_font_type type, unsigned int character_size) {
    if(character_size > 0)
        --character_size;

    /* TODO: Make this work for character size >= 100 */
    if(character_size >= 100)
        character_size = 99;

    mgl_memory_mapped_file *font_file = font_file_cache[type];
    if(!font_file) {
        const char *font_paths[2];
        size_t num_font_paths = 0;
        const char *font_name = "";

        switch(type) {
            case MGUI_FONT_LATIN: {
                font_paths[0] = "/usr/share/fonts/noto/NotoSans-Regular.ttf";
                font_paths[1] = "/usr/share/fonts/truetype/noto/NotoSans-Regular.ttf";
                num_font_paths = 2;
                font_name = "NotoSans-Regular.ttf";
                break;
            }
            case MGUI_FONT_LATIN_BOLD: {
                font_paths[0] = "/usr/share/fonts/noto/NotoSans-Bold.ttf";
                font_paths[1] = "/usr/share/fonts/truetype/noto/NotoSans-Bold.ttf";
                num_font_paths = 2;
                font_name = "NotoSans-Bold.ttf";
                break;
            }
            case MGUI_FONT_CJK: {
                font_paths[0] = "/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc";
                font_paths[1] = "/usr/share/fonts/truetype/noto-cjk/NotoSansCJK-Regular.ttc";
                num_font_paths = 2;
                font_name = "NotoSansCJK-Regular.ttc";
                break;
            }
        }

        mgl_memory_mapped_file *new_font_file = mgui_alloc(sizeof(mgl_memory_mapped_file));

        mgl_memory_mapped_file_load_options load_options = {
            .readable = true,
            .writable = false
        };

        bool successfully_loaded_file = false;
        for(size_t i = 0; i < num_font_paths; ++i) {
            if(mgl_mapped_file_load(font_paths[i], new_font_file, &load_options) == 0) {
                successfully_loaded_file = true;
                break;
            }
        }

        if(!successfully_loaded_file)
            fprintf(stderr, "mgui warning: mgui_get_font failed to load font %s\n", font_name);

        font_file_cache[type] = new_font_file;
        font_file = new_font_file;
    }

    if(!font_file->data)
        return NULL;

    mgl_font *font = font_cache[type][character_size];
    if(!font) {
        mgl_font *new_font = mgui_alloc(sizeof(mgl_font));
        if(mgl_font_load_from_file(new_font, font_file, character_size) != 0)
            fprintf(stderr, "mgui warning: mgui_get_font failed to load font %d\n", (int)type);

        font_cache[type][character_size] = new_font;
        font = new_font;
    }
    return font;
}