From fdd8e6983c0f1b613b5ce872b807eac91f787530 Mon Sep 17 00:00:00 2001 From: Google Gemini Date: Tue, 24 Nov 2026 18:34:43 +0800 Subject: [PATCH] tools, hle: Add dynamic CJK TrueType vector scaling for sharper text --- runtime/ui/i18n.mm | 13 ++++++++----- runtime/ui_layout.c | 9 +++++++++ runtime/ui_layout.h | 1 + tools/game_sites.py | 8 ++++++++ tools/patches.py | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 5 deletions(-) diff --git a/runtime/ui/i18n.mm b/runtime/ui/i18n.mm index b0e858a01..34b7213b5 100644 --- a/runtime/ui/i18n.mm +++ b/runtime/ui/i18n.mm @@ -544,12 +544,10 @@ static int ttc_face(const char *path, const char *lang) { static bool add_single_fallback(ImFontAtlas *atlas, float size, const ImFontConfig *base, const char *const *fonts, const char *lang, const ImVector &ranges, const char *shipped_path = nullptr) { - if (!fonts) return false; - for (int i = -1; i < 0 || fonts[i]; i++) { - const char *path = (i < 0 && shipped_path) ? shipped_path : (i >= 0 ? fonts[i] : nullptr); - if (!path || !*path) continue; + auto try_load = [&](const char *path) -> bool { + if (!path || !*path) return false; FILE *f = fopen(path, "rb"); - if (!f) continue; + if (!f) return false; fclose(f); ImFontConfig cfg = base ? *base : ImFontConfig(); cfg.MergeMode = true; @@ -557,6 +555,11 @@ static bool add_single_fallback(ImFontAtlas *atlas, float size, const ImFontConf cfg.FontDataOwnedByAtlas = true; atlas->AddFontFromFileTTF(path, size, &cfg, ranges.Data); return true; + }; + if (shipped_path && try_load(shipped_path)) return true; + if (fonts) { + for (int i = 0; fonts[i]; i++) + if (try_load(fonts[i])) return true; } return false; } diff --git a/runtime/ui_layout.c b/runtime/ui_layout.c index 40ef713dd..0f5f3ef6d 100644 --- a/runtime/ui_layout.c +++ b/runtime/ui_layout.c @@ -98,6 +98,15 @@ bool rt_ui_layout_pane_draw(Cpu *c, void (*native)(Cpu *)) { #endif } +uint32_t rt_cjk_font_scale(void) { + const char *env = getenv("SAVAGEWIND_CJK_SCALE"); + if (env && *env) { + int v = atoi(env); + if (v >= 1 && v <= 4) return (uint32_t)v; + } + return (g_settings.sharper_text >= 2) ? 2 : 1; +} + uint64_t ui_layout_begin(Cpu *c) { #ifdef SW_METAL return 0; diff --git a/runtime/ui_layout.h b/runtime/ui_layout.h index ed6d2c8c8..adb446ff1 100644 --- a/runtime/ui_layout.h +++ b/runtime/ui_layout.h @@ -10,3 +10,4 @@ uint64_t ui_layout_begin(Cpu *c); void ui_layout_end(Cpu *c, uint64_t commands); void rt_ui_layout_draw(Cpu *c, void (*native)(Cpu *)); bool rt_ui_layout_pane_draw(Cpu *c, void (*native)(Cpu *)); +uint32_t rt_cjk_font_scale(void); diff --git a/tools/game_sites.py b/tools/game_sites.py index 5ef248299..db9bfd88a 100644 --- a/tools/game_sites.py +++ b/tools/game_sites.py @@ -203,6 +203,10 @@ Site('quick_menu_capture_alpha', 'code', 'f9400268/ffc003ff bd405100 1e202008 54000000/ff00001f ' 'bd402100 1e2e1001 1e212000 54000001/ff00001f 90000001/9f00001f 91000021/ffc003ff ' 'aa1303e0 94000000/fc000000 52801fe8 39016408', 7), + # Scalable font engine (CJK/Chinese TrueType) resolution scaling + Site('cjk_font_size', 'code', '53103d01 2a0103e4 94000000/fc000000 f9400660 52800102 b9403381 94000000/fc000000 aa0003f4', 0), + Site('cjk_font_bin', 'code', '7d406f81 4b080128 f94c826a 79406b8b 9bba296a bd400940 7e21d821 1f012400', 0), + Site('cjk_font_quad', 'code', '2d42212b 79c03aa9 1e220120 1e200960 1e380009 79009389 79c00329 0b090289', 0), # the wait for the ground (tools/patches.py _tp_gate): is the map around Link not loaded yet? (the world pauses for it) Site('tp_gate', 'code', 'd10283ff fd002be8 f90037f9 a905fbfd a9075ff8 a90857f6 a9094ff4 910163fd ' '90000016/9f00001f', 0), @@ -480,6 +484,7 @@ ('surf_contact', 'surf_boost_remove', 'surf_jump_request'), ('stale_actor_helper', 'stale_actor_kind', 'stale_actor_target', 'stale_actor_flag', 'stale_actor_flag2'), ('quick_menu_capture_alpha',), + ('cjk_font_size', 'cjk_font_bin', 'cjk_font_quad'), ('kf_angle',), ('demo_move_arrive',), ('rail_point_radius',), @@ -516,6 +521,9 @@ KNOWN = { '1.9.0': { + 'cjk_font_size': 0x7000017d40, + 'cjk_font_bin': 0x7000017d78, + 'cjk_font_quad': 0x7000017f78, 'ui_layout_draw': 0x70013ee084, 'ui_layout_native': 0x7000f6f690, 'ui_layout_pane': 0x7000027d30, diff --git a/tools/patches.py b/tools/patches.py index 2f13427ae..97e7c806c 100644 --- a/tools/patches.py +++ b/tools/patches.py @@ -41,6 +41,42 @@ def _ui_layout_pane(gen, a): f'if (rt_ui_layout_pane_draw(c, {fn})) return; c->x[8] = LD8(c->x[0] + 0x58); }}') +def _cjk_font_size(gen, a): + assert _word(gen, a) == 0x53103d01, hex(_word(gen, a)) # lsl w1, w8, #0x10 + return ('{ extern uint32_t rt_cjk_font_scale(void); ' + 'uint32_t scale = rt_cjk_font_scale(); ' + 'uint32_t size = (uint32_t)c->x[8] * scale; ' + 'c->x[1] = size << 16; }') + + +def _cjk_font_bin(gen, a): + assert _word(gen, a) == 0x7d406f81, hex(_word(gen, a)) # ldr h1, [x28, #0x36] + return ('{ extern uint32_t rt_cjk_font_scale(void); ' + 'uint64_t ea = c->x[28] + 0x36ull; ' + 'uint32_t scale = rt_cjk_font_scale(); ' + 'c->v[1] = (u128)((uint32_t)LD16(ea) * scale); }') + + +def _cjk_font_quad(gen, a): + assert _word(gen, a) == 0x2d42212b, hex(_word(gen, a)) # ldp s11, s8, [x9, #0x10] + return ('{ extern uint32_t rt_cjk_font_scale(void); ' + 'uint64_t ea = c->x[9] + 16; ' + 'uint32_t u_s11 = LD32(ea); ' + 'uint32_t u_s8 = LD32(ea + 4); ' + 'uint32_t scale = rt_cjk_font_scale(); ' + 'if (scale > 1) { ' + 'float f_s11, f_s8; ' + 'memcpy(&f_s11, &u_s11, 4); ' + 'memcpy(&f_s8, &u_s8, 4); ' + 'f_s11 /= (float)scale; ' + 'f_s8 /= (float)scale; ' + 'memcpy(&u_s11, &f_s11, 4); ' + 'memcpy(&u_s8, &f_s8, 4); ' + '} ' + 'c->v[11] = (u128)u_s11; ' + 'c->v[8] = (u128)u_s8; }') + + def _quick_sort_enter(gen, a): # Menu::enter: sample held X before the selector initializes its list. assert _word(gen, a) == 0xfc1d0fe8, hex(_word(gen, a)) # str d8, [sp, #-0x30]! @@ -1312,6 +1348,9 @@ def _tp_gate(gen, a): 'horse_crash': _horse_crash, 'horse_look_ahead': _horse_look_ahead, 'item_tick': _item_tick, + 'cjk_font_size': _cjk_font_size, + 'cjk_font_bin': _cjk_font_bin, + 'cjk_font_quad': _cjk_font_quad, }