Pixel-Composer/scripts/font_loader/font_loader.gml

226 lines
5.4 KiB
Text
Raw Normal View History

globalvar FONT_DEF, FONT_ISLOADED, FONT_CACHE, FONT_CUST_CACHE, GLYPH_MAP;
2024-07-04 03:11:32 +02:00
globalvar f_h1, f_h2, f_h3, f_h5, f_p0, f_p0b, f_p1, f_p2, f_p3, f_p4;
globalvar f_code, f_sdf, f_sdf_medium;
2022-11-03 11:44:49 +01:00
2024-04-01 11:10:01 +02:00
global.LINE_HEIGHTS = {};
2024-02-12 10:25:23 +01:00
#region default
FONT_DEF = true;
FONT_CACHE = {};
FONT_CUST_CACHE = {};
FONT_ISLOADED = false;
GLYPH_MAP = {};
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;
2024-07-04 03:11:32 +02:00
f_p4 = _f_p4;
2024-02-12 10:25:23 +01:00
f_code = _f_code;
f_sdf = _f_sdf;
f_sdf_medium = _f_sdf_medium;
FONT_ISLOADED = false;
#endregion
2022-11-01 03:06:03 +01:00
function __font_add_height(font) {
2024-04-01 11:10:01 +02:00
INLINE
draw_set_font(font);
global.LINE_HEIGHTS[$ font] = string_height("l");
}
2024-04-01 11:10:01 +02:00
function __font_refresh() {
2024-04-01 11:10:01 +02:00
__font_add_height(f_h1);
__font_add_height(f_h2);
__font_add_height(f_h3);
__font_add_height(f_h5);
__font_add_height(f_p0);
__font_add_height(f_p0b);
__font_add_height(f_p1);
__font_add_height(f_p2);
__font_add_height(f_p3);
2024-07-04 03:11:32 +02:00
__font_add_height(f_p4);
2024-04-01 11:10:01 +02:00
__font_add_height(f_code);
__font_add_height(f_sdf);
__font_add_height(f_sdf_medium);
}
2024-04-01 11:10:01 +02:00
function _font_add(path, size, sdf = false, custom = false) {
var _cache = custom? FONT_CUST_CACHE : FONT_CACHE;
var font_cache_dir = DIRECTORY + "font_cache";
directory_verify(font_cache_dir);
var _key = $"{filename_name_only(path)}_{size}_{sdf}";
if(struct_has(_cache, _key) && font_exists(_cache[$ _key]))
return _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);
_cache[$ _key] = _f;
2023-11-24 10:41:53 +01:00
_font_extend_locale(_f, _f);
return _f;
}
function _font_extend_locale(baseFont, localFont, override = false) {
2023-11-24 10:41:53 +01:00
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;
}
}
2023-11-24 10:41:53 +01:00
function _font_path(rel) {
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;
}
2022-11-18 03:20:31 +01:00
function _font_load_from_struct(str, name, def, over = true) {
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);
2023-12-11 05:45:12 +01:00
var _font = _font_add(path, round(font.size * UI_SCALE), _sdf);
return _font;
}
2022-11-18 03:20:31 +01:00
function font_clear(font) { if(font_exists(font)) font_delete(font); }
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);
2024-07-04 03:11:32 +02:00
font_clear(f_p4);
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-11 09:59:58 +01:00
if(FONT_DEF || !file_exists_empty(path)) {
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;
2024-07-04 03:11:32 +02:00
f_p4 = _f_p4;
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;
2024-04-01 11:10:01 +02:00
__font_refresh();
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-12-11 09:59:58 +01: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-12-11 09:59:58 +01: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);
2024-07-04 03:11:32 +02:00
f_p4 = _font_load_from_struct(fontDef, "p4", _f_p4);
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;
2024-04-01 11:10:01 +02:00
__font_refresh();
}
2023-11-24 10:41:53 +01:00
#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