Flatpak: find the host's fonts for CJK text and the font list (#5124) (#5127)

* claim: #5124

* Flatpak: find the host's fonts for CJK text and the font list (#5124)

Inside the Flatpak sandbox the host's fonts are mounted at /run/host/fonts
and /run/host/user-fonts, which no font search looked at. Japanese and
Chinese text fell back to boxes, and the font list missed the host's
families.

- Search both folders for the CJK and Chinese fallbacks and for the
  installed-font list.
- The CJK fallback reads SOLIDCRAFT_FONT_DIRS (a path list, as the font
  list already does), with CRAFT_FONTS_DIR kept as a fallback.
This commit is contained in:
raykholo
2026-10-23 15:18:58 +00:00
committed by GitHub
parent 43fa25458d
commit e5dffc7052
3 changed files with 55 additions and 7 deletions
+14
View File
@@ -393,6 +393,11 @@ pub fn font_dirs() -> Vec<PathBuf> {
}
dirs.push(PathBuf::from("/usr/local/share/fonts"));
dirs.push(PathBuf::from("/usr/share/fonts"));
#[cfg(target_os = "linux")]
{
dirs.push(PathBuf::from("/run/host/fonts"));
dirs.push(PathBuf::from("/run/host/user-fonts"));
}
}
dirs
}
@@ -460,6 +465,15 @@ mod tests {
use super::*;
use ttf_parser::name_id;
#[cfg(target_os = "linux")]
#[test]
fn font_dirs_include_flatpak_host_fonts() {
let dirs = font_dirs();
for dir in ["/run/host/fonts", "/run/host/user-fonts"] {
assert!(dirs.contains(&PathBuf::from(dir)), "missing {dir}");
}
}
/// A face of family "Arial" whose full name is `full`.
fn face(full: &str, bold: bool, italic: bool) -> FaceNames {
FaceNames { index: 0, bold, italic, names: vec![(name_id::FAMILY, normalize("Arial")), (name_id::FULL_NAME, normalize(full))] }
+38 -5
View File
@@ -555,12 +555,22 @@ fn scan_dir_for_font_where(dir: &std::path::Path, depth_remaining: usize, matche
None
}
/// Find a CJK fallback font on the local system or in CRAFT_FONTS_DIR.
/// The font folders named by `SOLIDCRAFT_FONT_DIRS` (a path list), or else by the single folder
/// in `CRAFT_FONTS_DIR` (the craft-fonts pack).
#[cfg(not(target_arch = "wasm32"))]
fn font_override_dirs(dirs: Option<std::ffi::OsString>, pack: Option<std::ffi::OsString>) -> Vec<std::path::PathBuf> {
match (dirs, pack) {
(Some(dirs), _) => std::env::split_paths(&dirs).collect(),
(None, Some(pack)) => vec![std::path::PathBuf::from(pack)],
(None, None) => Vec::new(),
}
}
/// Find a CJK fallback font on the local system or in SOLIDCRAFT_FONT_DIRS / CRAFT_FONTS_DIR.
#[cfg(not(target_arch = "wasm32"))]
fn find_system_cjk_font() -> Option<Vec<u8>> {
// 1. Check optional craft-fonts pack directory if specified.
if let Ok(craft_dir) = std::env::var("CRAFT_FONTS_DIR") {
let dir = std::path::Path::new(&craft_dir);
// 1. Check the font folders the user named.
for dir in font_override_dirs(std::env::var_os("SOLIDCRAFT_FONT_DIRS"), std::env::var_os("CRAFT_FONTS_DIR")) {
let names = ["BIZUDPGothic-Regular.ttf", "BIZ-UDGothicR.ttc", "NotoSansCJK-Regular.ttc", "NotoSansJP-Regular.ttf"];
for name in names {
if let Some(bytes) = try_read_font(&dir.join(name)) {
@@ -615,7 +625,12 @@ fn find_system_cjk_font() -> Option<Vec<u8>> {
#[cfg(target_os = "linux")]
{
let search_dirs = [std::path::PathBuf::from("/usr/share/fonts"), std::path::PathBuf::from("/usr/local/share/fonts")];
let search_dirs = [
std::path::PathBuf::from("/usr/share/fonts"),
std::path::PathBuf::from("/usr/local/share/fonts"),
std::path::PathBuf::from("/run/host/fonts"),
std::path::PathBuf::from("/run/host/user-fonts"),
];
for dir in &search_dirs {
if let Some(bytes) = scan_dir_for_font(dir, 3, is_cjk_font_name) {
return Some(bytes);
@@ -809,6 +824,8 @@ fn chinese_font_dirs() -> Vec<std::path::PathBuf> {
{
dirs.push(std::path::PathBuf::from("/usr/share/fonts"));
dirs.push(std::path::PathBuf::from("/usr/local/share/fonts"));
dirs.push(std::path::PathBuf::from("/run/host/fonts"));
dirs.push(std::path::PathBuf::from("/run/host/user-fonts"));
if let Ok(home) = std::env::var("HOME") {
dirs.push(std::path::PathBuf::from(&home).join(".local/share/fonts"));
dirs.push(std::path::PathBuf::from(home).join(".fonts"));
@@ -884,6 +901,22 @@ pub(crate) fn test_context() -> egui::Context {
mod tests {
use super::*;
/// `SOLIDCRAFT_FONT_DIRS` wins and may list several folders; `CRAFT_FONTS_DIR` is the fallback.
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn cjk_font_folders_from_the_environment() -> Result<(), std::env::JoinPathsError> {
use std::path::PathBuf;
let list = std::env::join_paths(["/a", "/b"])?;
assert_eq!(font_override_dirs(Some(list), Some("/pack".into())), vec![PathBuf::from("/a"), PathBuf::from("/b")]);
assert_eq!(font_override_dirs(None, Some("/pack".into())), vec![PathBuf::from("/pack")]);
assert!(font_override_dirs(None, None).is_empty());
#[cfg(target_os = "linux")]
for dir in ["/run/host/fonts", "/run/host/user-fonts"] {
assert!(chinese_font_dirs().contains(&PathBuf::from(dir)), "missing {dir}");
}
Ok(())
}
/// The system faces are searched for on a thread; installing fonts before they are found does
/// not wait for the search and still gives the bundled faces.
#[cfg(not(target_arch = "wasm32"))]
+3 -2
View File
@@ -85,8 +85,9 @@ use a render scope that restores the previous locale when it exits, including ne
Standalone translated panels enter the same scope. Measure rendered text to size custom
widgets; UTF-8 byte lengths do not describe their displayed width.
The desktop host tries system CJK fonts across Windows, macOS and Linux, as well as an optional
`CRAFT_FONTS_DIR` font pack, and skips files that cannot be parsed. Chinese uses its own fallback
The desktop host tries system CJK fonts across Windows, macOS and Linux, the Flatpak host font
folders (`/run/host/fonts`, `/run/host/user-fonts`), the folders listed in `SOLIDCRAFT_FONT_DIRS`
and an optional `CRAFT_FONTS_DIR` font pack, and skips files that cannot be parsed. Chinese uses its own fallback
chain after the general CJK font: many Japanese CJK fonts (Hiragino Gothic, Meiryo, Yu Gothic)
match the CJK font search but lack simplified characters, so a second fallback found by the
Chinese font-name list (`PingFang`, `Microsoft YaHei`, `Noto Sans CJK SC`, …) covers them per