#include "../include/resource_loader.h" #include "../include/alloc.h" #include #include #include /* 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; }