Pixel-Composer/scripts/font_loader/font_loader.gml

119 lines
2.8 KiB
Plaintext
Raw Normal View History

globalvar FONT_ISLOADED, FONT_CACHE;
2023-10-16 07:25:22 +02:00
globalvar f_h1, f_h2, 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_CACHE = {};
2023-01-17 08:11:55 +01:00
FONT_ISLOADED = false;
2022-11-01 03:06:03 +01:00
function _font_add(path, size) {
var font_cache_dir = DIRECTORY + "font_cache";
directory_verify(font_cache_dir);
var _key = $"{filename_name_only(path)}_{size}";
if(struct_has(FONT_CACHE, _key) && font_exists(FONT_CACHE[$ _key])) {
//print($"Add font {_key}: restore from cache");
return FONT_CACHE[$ _key];
}
var _t = current_time;
var _f = font_add(path, size, false, false, 0, 0);
//font_enable_sdf(_f, true);
//font_sdf_spread(_f, 8);
//font_enable_effects(_f, true, {
// outlineEnable: true,
// outlineDistance: 0.1,
// outlineColour: c_white
//});
FONT_CACHE[$ _key] = _f;
//print($"Add font {_key}: {current_time - _t} ms");
return _f;
}
2022-11-18 03:20:31 +01:00
function _font_path(rel) {
rel = string_replace_all(rel, "./", "");
var defPath = $"{DIRECTORY}themes/{PREF_MAP[? "theme"]}/fonts/{rel}";
2023-06-05 18:27:53 +02:00
if(LOCALE.fontDir == noone)
return defPath;
var overridePath = $"{LOCALE.fontDir}{rel}";
2023-06-05 18:27:53 +02:00
if(file_exists(overridePath))
return overridePath;
return defPath;
2022-11-18 03:20:31 +01:00
}
2023-10-16 07:25:22 +02:00
function _font_load_from_struct(str, name, def) {
if(!struct_has(str, name)) return def;
var font = str[$ name];
var path = _font_path(font.path);
2023-06-05 18:27:53 +02:00
2022-11-18 03:20:31 +01:00
if(!file_exists(path)) {
noti_status("Font resource " + string(path), " not found. Rollback to default font.");
return def;
}
2023-06-10 13:59:45 +02:00
font_add_enable_aa(THEME_VALUE.font_aa);
2023-10-16 07:25:22 +02:00
var _font = _font_add(path, font.size * UI_SCALE);
return _font;
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() {
2023-01-17 08:11:55 +01:00
if(FONT_ISLOADED) {
2023-10-16 07:25:22 +02:00
font_clear(f_h1);
font_clear(f_h2);
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");
2023-06-05 18:27:53 +02:00
2022-11-18 03:20:31 +01:00
if(!file_exists(path)) {
noti_status("Font not defined at " + path + ", rollback to default fonts.");
2023-03-07 14:29:47 +01:00
f_h1 = _f_h1;
2023-10-16 07:25:22 +02:00
f_h2 = _f_h2;
2022-11-18 03:20:31 +01:00
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;
2023-01-17 08:11:55 +01:00
FONT_ISLOADED = false;
2022-11-18 03:20:31 +01:00
return;
}
2023-02-28 09:43:01 +01:00
var s = file_text_read_all(path);
2023-03-05 07:16:44 +01:00
var fontDef = json_try_parse(s);
2022-11-18 03:20:31 +01:00
2023-10-16 07:25:22 +02:00
f_h1 = _font_load_from_struct(fontDef, "h1", _f_h1);
f_h2 = _font_load_from_struct(fontDef, "h2", _f_h2);
f_h3 = _font_load_from_struct(fontDef, "h3", _f_h3);
f_h5 = _font_load_from_struct(fontDef, "h5", _f_h5);
2022-11-18 03:20:31 +01:00
2023-10-16 07:25:22 +02:00
f_p0 = _font_load_from_struct(fontDef, "p0", _f_p0);
f_p0b = _font_load_from_struct(fontDef, "p0b", _f_p0b);
2022-11-18 03:20:31 +01:00
2023-10-16 07:25:22 +02:00
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-10-16 07:25:22 +02:00
f_code = _font_load_from_struct(fontDef, "code", _f_code);
2023-01-04 02:30:04 +01:00
2023-01-17 08:11:55 +01:00
FONT_ISLOADED = true;
2022-11-01 03:06:03 +01:00
}