Ask fontconfig for the menu's CJK font instead of hard-coding five paths

plat_font_path stats five absolute paths for NotoSansCJK-Regular.ttc and takes the first that exists. That covers
SteamOS and the usual distributions, but misses any system where the font lives elsewhere, and fails outright in a
sandbox: under Flatpak /usr is the runtime and ships no CJK font, the app may only write /app, and the host's fonts
appear at /run/host/fonts. All five paths miss, fonts_up merges 0 fonts, and every Chinese character in the settings
menu renders as '?' while English stays correct.

Asking fontconfig fixes all of these at once. It is the same database Tk/Xft and every other Linux toolkit reads; it
knows /app/share/fonts, the XDG data dirs, /run/host/fonts and the per-user directories, so installing the font where
the system looks is enough and no packaging can leave the menu stranded.

libfontconfig is dlopen'd, not linked: a host without it (headless, a bare container) still builds and runs and takes
the fallback, so this adds no build dependency, and the runtime already links -ldl for the NVIDIA libraries. The
result is checked against the character before it is used, because fontconfig returns the closest pattern even when
nothing covers a character -- without that a Latin-only system hands back DejaVu Sans and the menu believes it merged
glyphs when it did not. The pattern names zh-cn and zh-tw rather than zh-hans and zh-hant, which is how Noto CJK and
fontconfig tag the faces.

menu_font_fontconfig defaults to 1 and 0 goes back to the five fixed paths, for a player whose fontconfig resolves to
a font the menu's parser cannot read. TOTK_MENU_FONT names a file directly and wins over both. A system with the font
where the old paths expect it sees no change, and Windows, whose font names are fixed, is untouched.

Verified against the real fontconfig with the host having no CJK font, with fontconfig pointed at a directory holding
only NotoSansCJK-Regular.ttc (the Flatpak case, now resolved), with TOTK_MENU_FONT set and pointing at a missing file,
with menu_font_fontconfig=0, and with libfontconfig made unloadable (falls back quietly, no crash).
This commit is contained in:
jcleng committed 2026-11-11 13:15:39 +00:00
1 parent d8a1d14104
commit 00a0ffc197
4 files changed
+147 -5

No files matched your search

+1
View File
@@ -93,6 +93,7 @@ Many rows below are developer tools that only act together with another setting
| --- | --- | --- | --- |
| `amiibo_dir` | Folder the Amiibo tab starts in; the tab saves the folder you last browsed here. | Only changes which folder the tab lists first. | Safe |
| `exefs` | Folder of the game's ExeFS (with `main.npdm`); a relative path is relative to `totk.exe`'s folder. | Without it, an `exefs` folder next to `totk.exe` is used, else the path set at build time. The game code is compiled into `totk.exe`, and the loader checks the files here by SHA-256, so files from another build or game version stop the start with a message. | Caution |
| `menu_font_fontconfig` (default `1`) | On Linux, `1` asks fontconfig which font file draws each menu language's name, and `0` keeps the fixed distribution paths (`/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc` and four more). The menu merges that file into its font atlas. | `0` is for a system whose fontconfig resolves to a font the menu's parser cannot read; the log then says `menu: system font ... left out`. Has no effect on Windows, where the file names are fixed, and none on a language the ASCII font already draws. The atlas is rebuilt when the window size or the menu language changes. `TOTK_MENU_FONT` names a file directly and wins over both. | Safe |
| `mods_dir` (default `mods`) | Folder that holds one subfolder per player mod, beside `totk.exe`. | Mods found there replace game files; see `docs/mods.md`. | Caution |
| `mods_disabled` | Names of mods that are switched off, separated by `\|`. | Listed mods are not applied; the Mods tab writes this line. | Safe |
| `mods_order` | Mod names, top first, separated by `\|`; the top mod wins a file that two mods replace. | Changes which mod's file the game gets; unlisted mods go below the listed ones, by name. | Safe |
+5 -2
View File
@@ -46,7 +46,7 @@ static const char *const known[] = {
"FREEZE_IDLE", "FREE_CORES",
"FULLSCREEN", "FXAA", "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_NAME", "GRASS_RANGE", "HDR", "HDR_GAME_NITS", "HDR_HUE",
"HDR_PEAK_NITS", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "KSPIN_US", "LANG", "LIB_PREBUILD",
"LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MAP_CLICK", "MEMORY", "MENU_LANG", "MENU_TEST",
"LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MAP_CLICK", "MEMORY", "MENU_LANG", "MENU_TEST", "MENU_FONT_FONTCONFIG",
"MEM_RESERVE_MIB", "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MOTION", "MOUSE_AIM_SCALE", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_ASPECT", "MOVIE_HOST", "MOVIE_PACE",
"MVOBJ_ALL", "MVOBJ_BANKS", "MVOBJ_EQUAL", "MVOBJ_EXACT", "MVOBJ_EXEC", "MVOBJ_NOC4", "MVOBJ_ONLY", "MVOBJ_TIMES", "MVPREV_CHECK", "MVPREV_GROW", "MVPREV_SKIP", "MVPREV_TRUST", "MV_CHECK",
"MV_SURVEY",
@@ -84,7 +84,7 @@ static const struct { const char *key, *section; } sections[] = {
{"PCSS", "display"},
{"MODS_DIR", "mods"}, {"MODS_ORDER", "mods"}, {"MODS_DISABLED", "mods"}, {"MODS_REBUILD", "mods"},
{"TEX_HEAP_EXTRA_MB", "mods"},
{"MENU_LANG", "language"}, {"LANG", "language"}, {"OPERATION_MODE", "language"},
{"MENU_LANG", "language"}, {"MENU_FONT_FONTCONFIG", "language"}, {"LANG", "language"}, {"OPERATION_MODE", "language"},
{"VOLUME", "audio"}, {"AUDIO_CHANNELS", "audio"},
{"MOTION", "input"}, {"RUMBLE_STRENGTH", "input"}, {"RUMBLE_CURVE", "input"}, {"RUMBLE_TRIGGERS", "input"},
{"PAD_LAYOUT", "input"}, {"PROMPTS", "input"}, {"STICK_DEADZONE", "input"}, {"LSTICK_DEADZONE", "input"}, {"RSTICK_DEADZONE", "input"},
@@ -160,6 +160,9 @@ static const CfgMeta metas[] = {
{"DIALOGUE_CLICK", "0", "0|1", LIVE}, // menu_cursor.c: in a dialogue box left click is B, right click A
{"WALK_KEY", "none", NULL, LIVE}, {"WALK_SCALE", "0.51", "0.2..0.9", LIVE},
{"MENU_LANG", "", "en|ru|pt-BR|zh-Hans", LIVE},
// plat_font_path on Linux: 1 asks fontconfig which file draws the language, 0 keeps the fixed distribution
// paths. A rebuild of the atlas reads it, so a change shows on the next window size or language switch.
{"MENU_FONT_FONTCONFIG", "1", "0|1", LIVE},
{"AMIIBO_DIR", "", NULL, LIVE},
// Discord Rich Presence (discord_rpc.c): off by default, the Menu tab of the settings menu switches it.
{"DISCORD_RPC", "0", "0|1", LIVE},
+138 -2
View File
@@ -34,6 +34,7 @@
#include <SDL3/SDL_clipboard.h>
#include "cfg.h"
#include "platform.h"
// The write-watch ABI of Linux 6.7 (uapi linux/fs.h and linux/userfaultfd.h), for headers older than it: the release
@@ -1250,7 +1251,135 @@ const char *plat_user_locale(void) {
return tag;
}
// Noto CJK as the common distributions and SteamOS install it; one collection covers every CJK language.
// A CJK font for the menu: the file that draws this language on this system.
//
// The languages the catalog has (ja, ko, zh-Hans, zh-Hant) are not covered by the font built into the menu, so their
// glyphs are merged in from a system file. Distributions disagree on where Noto CJK lives, and a sandboxed build
// (Flatpak, Snap, an AppImage on a host whose fonts are mounted at /run/host/fonts) may have no Noto CJK where the
// fixed paths expect it while the host still has CJK fonts that would do. So ask fontconfig first: it is the database
// Tk/Xft and every other Linux toolkit reads, it knows /app/share/fonts, the XDG data dirs, /run/host/fonts and the
// per-user dirs, and adding a font there is enough -- the menu does not need to learn about it. The fixed
// distribution paths stay as the fallback for a system without fontconfig, and TOTK_MENU_FONT overrides both.
//
// fontconfig is loaded with dlopen rather than linked, so a host without libfontconfig (a bare container) still builds
// and runs and just takes the fallback. The runtime already links -ldl for the NVIDIA libraries.
static int cjk_lang(const char *lang) {
return !strcmp(lang, "ja") || !strcmp(lang, "ko") || !strcmp(lang, "zh-Hans") || !strcmp(lang, "zh-Hant");
}
// The fontconfig language tag of a catalog language. GL says zh-Hans/zh-Hant, but fontconfig and OpenType name the
// faces zh-cn/zh-tw (Noto Sans CJK SC is lang=zh-cn), and a pattern naming only zh-hans does not reach it.
static const char *cjk_lang_tag(const char *lang) {
if (!strcmp(lang, "ja")) return "ja";
if (!strcmp(lang, "ko")) return "ko";
if (!strcmp(lang, "zh-Hans")) return "zh-cn";
return "zh-tw";
}
// The handful of fontconfig calls used here (2.0 ABI, unchanged since). Loaded once, kept for the process: the menu
// rebuilds its atlas when the window height changes and must not dlopen on every rebuild.
typedef struct _FcPattern FcPattern;
typedef struct _FcCharSet FcCharSet;
typedef struct _FcConfig FcConfig;
typedef struct {
int state; // 0 untried, 1 usable, -1 no fontconfig on this system
FcPattern *(*NameParse)(const unsigned char *);
void (*PatternDestroy)(FcPattern *);
int (*PatternAddCharSet)(FcPattern *, const char *, const FcCharSet *);
int (*PatternGetString)(const FcPattern *, const char *, int, unsigned char **);
FcCharSet *(*CharSetCreate)(void);
int (*CharSetAddChar)(FcCharSet *, unsigned int);
void (*CharSetDestroy)(FcCharSet *);
FcPattern *(*FontMatch)(FcConfig *, FcPattern *, int *); // 3 args: (config, pattern, FcResult *)
int (*PatternGetCharSet)(const FcPattern *, const char *, int, FcCharSet **);
int (*CharSetHasChar)(const FcCharSet *, unsigned int);
FcConfig *(*ConfigGetCurrent)(void);
int (*Init)(void);
} Fc;
static Fc fc;
static int fc_load(void) {
if (fc.state) return fc.state > 0;
void *lib = plat_lib_open("libfontconfig.so.1");
if (!lib) lib = plat_lib_open("libfontconfig.so");
if (!lib) { fc.state = -1; return 0; }
fc.NameParse = (FcPattern *(*)(const unsigned char *))plat_lib_sym(lib, "FcNameParse");
fc.PatternDestroy = (void (*)(FcPattern *))plat_lib_sym(lib, "FcPatternDestroy");
fc.PatternAddCharSet = (int (*)(FcPattern *, const char *, const FcCharSet *))plat_lib_sym(lib, "FcPatternAddCharSet");
fc.PatternGetString = (int (*)(const FcPattern *, const char *, int, unsigned char **))plat_lib_sym(lib, "FcPatternGetString");
fc.CharSetCreate = (FcCharSet *(*)(void))plat_lib_sym(lib, "FcCharSetCreate");
fc.CharSetAddChar = (int (*)(FcCharSet *, unsigned int))plat_lib_sym(lib, "FcCharSetAddChar");
fc.CharSetDestroy = (void (*)(FcCharSet *))plat_lib_sym(lib, "FcCharSetDestroy");
fc.FontMatch = (FcPattern *(*)(FcConfig *, FcPattern *, int *))plat_lib_sym(lib, "FcFontMatch");
fc.PatternGetCharSet = (int (*)(const FcPattern *, const char *, int, FcCharSet **))plat_lib_sym(lib, "FcPatternGetCharSet");
fc.CharSetHasChar = (int (*)(const FcCharSet *, unsigned int))plat_lib_sym(lib, "FcCharSetHasChar");
fc.ConfigGetCurrent = (FcConfig *(*)(void))plat_lib_sym(lib, "FcConfigGetCurrent");
fc.Init = (int (*)(void))plat_lib_sym(lib, "FcInit");
if (!fc.NameParse || !fc.PatternDestroy || !fc.PatternAddCharSet || !fc.PatternGetString || !fc.CharSetCreate ||
!fc.CharSetAddChar || !fc.CharSetDestroy || !fc.FontMatch || !fc.ConfigGetCurrent ||
!fc.PatternGetCharSet || !fc.CharSetHasChar) {
fc.state = -1;
return 0;
}
// FcInit loads the configuration and the font set; FcConfigGetCurrent is NULL before it, and FcFontMatch asserts
// on a NULL config. Idempotent, so the second call of a rebuild costs a flag check.
if (fc.Init) fc.Init();
if (!fc.ConfigGetCurrent()) {
fc.state = -1;
return 0;
}
fc.state = 1;
return 1;
}
// The file fontconfig picks for this language, or 0. Asks for the generic CJK-capable family with the language set and
// one representative character of that language in the charset, so a Latin-only or Japanese face cannot win by default.
static int fc_font_for(const char *lang, char *out, size_t cap) {
static const struct { const char *lang; unsigned int cp; } probe[] = {
{"ja", 0x65E5}, // 日
{"ko", 0xD55C}, // 한
{"zh-Hans", 0x8BBE}, // 设
{"zh-Hant", 0x8A2D}, // 設
};
unsigned int cp = 0;
for (size_t i = 0; i < sizeof probe / sizeof *probe; i++)
if (!strcmp(probe[i].lang, lang)) cp = probe[i].cp;
if (!cp || !fc_load()) return 0;
char query[64];
snprintf(query, sizeof query, "sans-serif:lang=%s", cjk_lang_tag(lang));
FcPattern *pat = fc.NameParse((const unsigned char *)query);
if (!pat) return 0;
FcCharSet *cs = fc.CharSetCreate();
int ok = 0;
if (cs) {
fc.CharSetAddChar(cs, cp);
if (fc.PatternAddCharSet(pat, "charset", cs)) {
int result = 0; // FcResult; FcFontMatch asserts when this pointer is NULL
FcPattern *m = fc.FontMatch(fc.ConfigGetCurrent(), pat, &result);
unsigned char *file = NULL;
if (m) {
// fontconfig returns the closest pattern even when nothing covers the character, so check the charset
// before trusting it: without this a Latin-only system hands back DejaVu Sans and the menu merges no
// glyphs while believing it did.
FcCharSet *got = NULL;
if (result == 0 /* FcResultMatch */ &&
fc.PatternGetCharSet(m, "charset", 0, &got) == 0 && got && fc.CharSetHasChar(got, cp) &&
fc.PatternGetString(m, "file", 0, &file) == 0 && file)
ok = snprintf(out, cap, "%s", (const char *)file) < (int)cap;
fc.PatternDestroy(m);
}
}
fc.CharSetDestroy(cs);
}
fc.PatternDestroy(pat);
return ok;
}
// See platform.h. TOTK_MENU_FONT names a file to use for every CJK language; then fontconfig; then the fixed paths,
// which are the normal layout on SteamOS and the distributions that predate fontconfig's own lookup.
int plat_font_path(const char *lang, char *out, size_t cap) {
static const char *files[] = {
"/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc",
@@ -1259,7 +1388,14 @@ int plat_font_path(const char *lang, char *out, size_t cap) {
"/usr/share/fonts/noto/NotoSansCJK-Regular.ttc",
"/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc",
};
if (strcmp(lang, "ja") && strcmp(lang, "ko") && strcmp(lang, "zh-Hans") && strcmp(lang, "zh-Hant")) return 0;
if (!cjk_lang(lang)) return 0;
char override[PATH_MAX];
if (plat_getenv("TOTK_MENU_FONT", override, sizeof override) && plat_file_exists(override))
return snprintf(out, cap, "%s", override) < (int)cap;
// menu_font_fontconfig=0 skips the fontconfig lookup and keeps the five fixed paths, for a player whose fontconfig
// resolves to a face the font parser cannot read. Read through cfg_bool like every other switch, so totk.ini and
// the TOTK_ variable both work and the menu can grey it out.
if (cfg_bool("TOTK_MENU_FONT_FONTCONFIG") && fc_font_for(lang, out, cap) && plat_file_exists(out)) return 1;
for (size_t i = 0; i < sizeof files / sizeof *files; i++)
if (plat_file_exists(files[i])) return snprintf(out, cap, "%s", files[i]) < (int)cap;
return 0;
+3 -1
View File
@@ -387,7 +387,9 @@ const char *plat_ui_language(void);
const char *plat_user_locale(void);
uint32_t plat_thread_id(void); // id of the calling thread, for unique file names
// A system font file with the glyphs of a catalog language ("ja", "ko", "zh-Hans", "zh-Hant"): the menu shows each
// language's name in its own script. Copies the UTF-8 path into out; 0 when the system has none.
// language's name in its own script. Copies the UTF-8 path into out; 0 when the system has none. On Linux the file is
// the one fontconfig picks for that language (so a sandbox that installs a font where its fontconfig finds it needs no
// code change), with TOTK_MENU_FONT overriding it and the usual distribution paths as the fallback.
int plat_font_path(const char *lang, char *out, size_t cap);
// The operating system and its build ("Windows 10.0.28000") into out; 0 when unknown.
int plat_os_version(char *out, size_t cap);