From 41eb234db4701ad4d4bd63019011e0b222d9ae15 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Thu, 19 Nov 2026 22:04:54 +0300 Subject: [PATCH] The nn::pl shared fonts reach the game decoded to plain sfnt after the BFTTF header, as the pl shared memory holds them, so the font loader no longer hands the encoded payload to Decode and Chinese text keeps its ellipsis on the line (#591) The firmware files keep the font XOR-encoded. The game's font loader reads the data at GetSharedFontAddress as a font only when it starts with an sfnt tag; otherwise it calls nn::fontll::ScalableFontEngineHelper::Decode, which returns NULL for the encoded payload, and the face is left without a name. font_data now decodes the payload in place after ov_bfttf_size accepts the file and checks for an sfnt tag. The services probe checks the unchanged header at Address - 8 and plain sfnt at Address. Same fix as MacNativeGames pull request #2145 (shared_font.c load). Co-Authored-By: Claude Opus 5.5 --- runtime/ov_probe.c | 11 +++++++---- runtime/ov_system.c | 16 +++++++++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/runtime/ov_probe.c b/runtime/ov_probe.c index b5315f08..709fc871 100644 --- a/runtime/ov_probe.c +++ b/runtime/ov_probe.c @@ -1002,11 +1002,14 @@ static void probe_fonts(void) { CHECK(got == sizeof head && size && size + 8 == (uint64_t)len, "shared font 0: size %llu, file %lld bytes", (unsigned long long)size, (long long)len); const uint8_t *p = (const uint8_t *)(uintptr_t)addr; - CHECK(addr && !memcmp(p - 8, head, sizeof head) && ov_bfttf_size(p - 8, size + 8) == size, - "shared font 0: the address is not 8 bytes into the BFTTF file"); + // The pl shared memory layout: the file's BFTTF header unchanged at Address - 8, the decoded sfnt font at Address. + CHECK(addr && !memcmp(p - 8, head, 8) && ov_bfttf_size(head, size + 8) == size, + "shared font 0: the address is not 8 bytes past the file's BFTTF header"); + CHECK(!memcmp(p, "\0\1\0\0", 4) || !memcmp(p, "OTTO", 4), + "shared font 0: the data at the address is not plain sfnt: %02x %02x %02x %02x", p[0], p[1], p[2], p[3]); CHECK(CALL("pl.GetSharedFontAddress", 0) == addr, "shared font 0: the address changed"); - printf("pl: shared font 0 (Standard) at %#llx, %llu bytes, BFTTF header checked (%s)\n", (unsigned long long)addr, - (unsigned long long)size, path); + printf("pl: shared font 0 (Standard) at %#llx, %llu bytes, BFTTF header and decoded sfnt checked (%s)\n", + (unsigned long long)addr, (unsigned long long)size, path); } // The host catalog (ui_text.c): every menu id has its own English, Russian, Brazilian Portuguese and Simplified diff --git a/runtime/ov_system.c b/runtime/ov_system.c index 5aeabed2..ae1f9b17 100644 --- a/runtime/ov_system.c +++ b/runtime/ov_system.c @@ -775,9 +775,14 @@ static void ov_user_difference(Cpu *c) { // ==== nn::pl shared fonts (shared_font.c). TotK asks for them only from its font loader, only with Simplified Chinese, // Korean or Traditional Chinese text, and only for type 0; tools/extract_fonts.py writes -// them from the user's firmware. Each type is read on first use into guest-visible memory as the BFTTF file the pl -// shared memory holds; Address is 8 bytes past the file start (after the BFTTF header) and Size the decoded font size, -// as in MNG. The load state is always Loaded: the game spins until it is, so missing files stop with a named fatal at +// them from the user's firmware. Each type is read on first use into guest-visible memory laid out as the pl shared +// memory holds it: the file's 8-byte BFTTF header as it is, then the font decoded to plain sfnt data. Address is 8 +// bytes past the file start (after the header) and Size the decoded font size, as in MNG. The firmware file keeps the +// font XOR-encoded, and the game cannot read it that way: its font loader takes the data at Address as a font when the +// first word is an sfnt tag and otherwise hands it to nn::fontll::ScalableFontEngineHelper::Decode, which knows only +// a BFTTF header at the start and returns NULL for an encoded payload. The face then gets no name, SetFont fails, and +// the text draws the previous face's glyphs with the failed face's metrics (the CJK ellipsis above the line; MNG +// shared_font.c load decodes the same way since its pull request #2145). The load state is always Loaded: the game spins until it is, so missing files stop with a named fatal at // Address/Size instead of a hang. TOTK_SHARED_FONTS= overrides /../../firmware/shared-fonts. #define SHARED_FONT_TYPES 6 #define BFTTF_MAGIC 0x7f9a0218u @@ -840,6 +845,11 @@ static const uint8_t *font_data(Cpu *c, const char *fn, int64_t t) { fclose(f); uint64_t size = got == (size_t)len ? ov_bfttf_size(p, (uint64_t)len) : 0; if (!size) rt_fatal("nn::pl: shared font %s is not a BFTTF font", path); + // Decode the font in place; the words are big-endian, so byte i of each word takes key byte i. + uint32_t key = be32(p) ^ BFTTF_MAGIC; + for (int64_t i = 8; i < len; i++) p[i] ^= (uint8_t)(key >> (24 - 8 * (i & 3))); + if (memcmp(p + 8, "\0\1\0\0", 4) && memcmp(p + 8, "OTTO", 4)) + rt_fatal("nn::pl: shared font %s is not an sfnt font after decoding", path); fonts.size[t] = size; __atomic_store_n(&fonts.file[t], p, __ATOMIC_RELEASE); rt_log("pl: shared font %lld (%s): %llu bytes from %s", (long long)t, font_type_name(t),