Pixel-Composer/scripts/font_loader/font_loader.gml

70 lines
1.7 KiB
Plaintext
Raw Normal View History

2023-01-04 02:30:04 +01:00
globalvar FONT_LOADED, f_h3, f_h5, f_p0, f_p0b, f_p1, f_p2, f_p3, f_code;
2022-11-03 11:44:49 +01:00
FONT_LOADED = false;
2022-11-01 03:06:03 +01:00
2022-11-18 03:20:31 +01:00
function _font_path(rel) {
2022-11-22 14:25:39 +01:00
return DIRECTORY + "themes/" + PREF_MAP[? "theme"] + "/fonts/" + string_replace_all(rel, "./", "");
2022-11-18 03:20:31 +01:00
}
function _font_load_from_struct(str, def) {
var path = _font_path(str.path);
if(!file_exists(path)) {
noti_status("Font resource " + string(path), " not found. Rollback to default font.");
return def;
}
2022-11-22 14:25:39 +01:00
return font_add(path, str.size * UI_SCALE, false, false, str.range[0], str.range[1]);
2022-11-18 03:20:31 +01:00
}
function font_clear(font) { if(font_exists(font)) font_delete(font); }
2022-11-01 03:06:03 +01:00
function loadFonts() {
2022-11-03 11:44:49 +01:00
if(FONT_LOADED) {
2022-11-18 03:20:31 +01:00
font_clear(f_h3);
font_clear(f_h5);
2022-11-03 11:44:49 +01:00
2022-11-18 03:20:31 +01:00
font_clear(f_p0);
font_clear(f_p0b);
2022-11-03 11:44:49 +01:00
2022-11-18 03:20:31 +01:00
font_clear(f_p1);
font_clear(f_p2);
font_clear(f_p3);
2023-01-04 02:30:04 +01:00
font_clear(f_code);
2022-11-03 11:44:49 +01:00
}
2022-11-18 03:20:31 +01:00
var path = _font_path("./fonts.json");
if(!file_exists(path)) {
noti_status("Font not defined at " + path + ", rollback to default fonts.");
f_h3 = _f_h3;
f_h5 = _f_h5;
f_p0 = _f_p0;
f_p0b = _f_p0b;
f_p1 = _f_p1;
f_p2 = _f_p2;
f_p3 = _f_p3;
2023-01-04 02:30:04 +01:00
f_code = _f_code;
2022-11-18 03:20:31 +01:00
FONT_LOADED = false;
return;
}
var f = file_text_open_read(path);
var s = file_text_read_all(f);
file_text_close(f);
var fontDef = json_parse(s);
f_h3 = _font_load_from_struct(fontDef.h3, _f_h3);
f_h5 = _font_load_from_struct(fontDef.h5, _f_h5);
f_p0 = _font_load_from_struct(fontDef.p0, _f_p0);
f_p0b = _font_load_from_struct(fontDef.p0b, _f_p0b);
f_p1 = _font_load_from_struct(fontDef.p1, _f_p1);
f_p2 = _font_load_from_struct(fontDef.p2, _f_p2);
f_p3 = _font_load_from_struct(fontDef.p3, _f_p3);
2022-11-03 11:44:49 +01:00
2023-01-04 02:30:04 +01:00
f_code = _font_load_from_struct(fontDef.code, _f_code);
2022-11-03 11:44:49 +01:00
FONT_LOADED = true;
2022-11-01 03:06:03 +01:00
}