Pixel-Composer/scripts/font_loader/font_loader.gml

169 lines
4.5 KiB
Text
Raw Normal View History

2023-11-24 10:41:53 +01:00
globalvar FONT_ISLOADED, FONT_CACHE, GLYPH_MAP;
2023-11-25 08:54:35 +01:00
globalvar f_h1, f_h2, f_h3, f_h5, f_p0, f_p0b, f_p1, f_p2, f_p3, f_code, f_sdf, f_sdf_medium;
2022-11-03 11:44:49 +01:00
FONT_CACHE = {};
2023-01-17 08:11:55 +01:00
FONT_ISLOADED = false;
2023-11-24 10:41:53 +01:00
GLYPH_MAP = {};
2022-11-01 03:06:03 +01:00
2023-11-24 10:41:53 +01:00
function _font_add(path, size, sdf = false) { #region
var font_cache_dir = DIRECTORY + "font_cache";
directory_verify(font_cache_dir);
var _key = $"{filename_name_only(path)}_{size}_{sdf}";
2023-11-24 10:41:53 +01:00
if(struct_has(FONT_CACHE, _key) && font_exists(FONT_CACHE[$ _key]))
return FONT_CACHE[$ _key];
var _t = current_time;
var _f = font_add(path, size, false, false, 0, 0);
2023-11-24 10:41:53 +01:00
if(sdf) font_enable_sdf(_f, true);
FONT_CACHE[$ _key] = _f;
2023-11-24 10:41:53 +01:00
_font_extend_locale(_f, _f);
return _f;
2023-11-24 10:41:53 +01:00
} #endregion
2023-11-24 10:41:53 +01:00
function _font_extend_locale(baseFont, localFont, override = false) { #region
if(!struct_exists(GLYPH_MAP, baseFont))
GLYPH_MAP[$ baseFont] = {};
var Gmap = GLYPH_MAP[$ baseFont];
var _fInfo = font_get_info(localFont);
var _gMap = _fInfo.glyphs;
var _glyphs = variable_struct_get_names(_gMap);
for( var i = 0, n = array_length(_glyphs); i < n; i++ ) {
var _g = _glyphs[i];
if(_gMap[$ _g] == undefined) continue;
if(override || !struct_has(Gmap, _g))
Gmap[$ _g] = localFont;
}
} #endregion
function _font_path(rel) { #region
rel = string_replace_all(rel, "./", "");
2023-12-08 03:50:09 +01:00
var defPath = $"{DIRECTORY}Themes/{PREFERENCES.theme}/fonts/{rel}";
2023-06-05 18:27:53 +02:00
if(LOCALE.fontDir == noone)
return defPath;
var overridePath = $"{LOCALE.fontDir}{rel}";
2023-12-08 03:50:09 +01:00
if(file_exists_empty(overridePath))
2023-06-05 18:27:53 +02:00
return overridePath;
2023-11-24 10:41:53 +01:00
2023-06-05 18:27:53 +02:00
return defPath;
2023-11-24 10:41:53 +01:00
} #endregion
2022-11-18 03:20:31 +01:00
2023-11-24 10:41:53 +01:00
function _font_load_from_struct(str, name, def, over = true) { #region
2023-10-16 07:25:22 +02:00
if(!struct_has(str, name)) return def;
2023-06-05 18:27:53 +02:00
2023-11-24 10:41:53 +01:00
var font = str[$ name];
var path = "";
2023-12-08 03:50:09 +01:00
if(over && file_exists_empty(PREFERENCES.font_overwrite))
path = PREFERENCES.font_overwrite;
else
2023-11-24 10:41:53 +01:00
path = _font_path(font.path);
2023-12-08 03:50:09 +01:00
if(!file_exists_empty(path)) {
noti_status($"Font resource {path} not found. Rollback to default font.");
return def;
2022-11-18 03:20:31 +01:00
}
2023-06-10 13:59:45 +02:00
font_add_enable_aa(THEME_VALUE.font_aa);
2023-11-24 10:41:53 +01:00
var _sdf = struct_try_get(font, "sdf", false);
var _font = _font_add(path, font.size * UI_SCALE, _sdf);
return _font;
2023-11-24 10:41:53 +01:00
} #endregion
2022-11-18 03:20:31 +01:00
function font_clear(font) { if(font_exists(font)) font_delete(font); }
2023-11-24 10:41:53 +01:00
function loadFonts() { #region
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);
font_clear(f_sdf);
2023-11-25 08:54:35 +01:00
font_clear(f_sdf_medium);
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
2023-12-08 03:50:09 +01:00
if(!file_exists_empty(path)) {
2022-11-18 03:20:31 +01:00
noti_status("Font not defined at " + path + ", rollback to default fonts.");
2023-11-24 10:41:53 +01:00
f_h1 = _f_h1;
f_h2 = _f_h2;
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;
f_sdf = _f_sdf;
2023-11-25 08:54:35 +01:00
f_sdf_medium = _f_sdf_medium;
2023-01-17 08:11:55 +01:00
FONT_ISLOADED = false;
2022-11-18 03:20:31 +01:00
return;
}
2023-11-13 07:11:52 +01:00
var s = file_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-11-24 10:41:53 +01:00
f_code = _font_load_from_struct(fontDef, "code", _f_code, false);
f_sdf = _font_load_from_struct(fontDef, "sdf", _f_sdf);
2023-11-25 08:54:35 +01:00
f_sdf_medium = _font_load_from_struct(fontDef, "sdf_medium", _f_sdf_medium);
2023-01-04 02:30:04 +01:00
2023-01-17 08:11:55 +01:00
FONT_ISLOADED = true;
2023-11-24 10:41:53 +01:00
} #endregion
#region unused font cache
//function __fontCache() {
// var _f = font_add("LXGWWenKaiMonoLite-Bold.ttf", 16, false, false, 0, 0);
// var _fInfo = font_get_info(_f);
// var _gMap = _fInfo.glyphs;
// var _glyphs = variable_struct_get_names(_gMap);
// draw_set_text(_f, fa_left, fa_top, c_white);
// for( var i = 0, n = array_length(_glyphs); i < n; i++ ) {
// var _g = _glyphs[i];
// var _glyph = _gMap[$ _g];
// if(_glyph.w == 0 || _glyph.h == 0) continue;
// var _s = surface_create(_glyph.w, _glyph.h);
// surface_set_target(_s); DRAW_CLEAR
// draw_text(0, 0, chr(_glyph.char));
// surface_reset_target();
// surface_save(_s, $"{DIRECTORY}Locale/extend/cache/{_glyph.char}.png");
// surface_clear(_s);
// }
//} run_in(1, __fontCache);
#endregion