Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(text): discover CJK system fonts through Flatpak host mounts (#1204)

Nigel Ihza Firdausy1a9a39eadf86
diff --git a/crates/text/src/cjk.rs b/crates/text/src/cjk.rs
index 17bad6ffd..9b70f23f6 100644
--- a/crates/text/src/cjk.rs
+++ b/crates/text/src/cjk.rs
@@ -292,10 +292,30 @@ pub fn font_files(script: CjkScript) -> Vec<FontFile> {
f("/usr/share/fonts/un-core/UnDotum.ttf", ""),
],
});
- v
+ with_flatpak_host_fonts(v)
}
}
+/// Flatpak mounts the host system font directory outside the runtime's `/usr/share/fonts`.
+/// Preserve the collection face and font preference order when trying that mirror.
+#[cfg(not(target_arch = "wasm32"))]
+fn with_flatpak_host_fonts(files: Vec<FontFile>) -> Vec<FontFile> {
+ let mut candidates = Vec::new();
+ for file in files {
+ let host = if cfg!(target_os = "linux") {
+ file.path
+ .strip_prefix("/usr/share/fonts")
+ .ok()
+ .map(|relative| FontFile { path: std::path::Path::new("/run/host/fonts").join(relative), family: file.family })
+ } else {
+ None
+ };
+ candidates.push(file);
+ candidates.extend(host);
+ }
+ candidates
+}
+
/// Broad-coverage fonts tried after every script's own fonts (they cover Han, kana and Hangul
/// but look worse than the script fonts).
#[cfg(not(target_arch = "wasm32"))]
@@ -307,7 +327,10 @@ pub fn last_resort_files() -> Vec<FontFile> {
let dir = std::env::var("WINDIR").unwrap_or_else(|_| "C:\\Windows".into());
vec![FontFile { path: std::path::Path::new(&dir).join("Fonts").join("ARIALUNI.TTF"), family: "" }]
} else {
- vec![f("/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf"), f("/usr/share/fonts/google-droid-sans-fonts/DroidSansFallbackFull.ttf")]
+ with_flatpak_host_fonts(vec![
+ f("/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf"),
+ f("/usr/share/fonts/google-droid-sans-fonts/DroidSansFallbackFull.ttf"),
+ ])
}
}
@@ -478,3 +501,34 @@ mod tests {
assert_ne!(sc, tc);
}
}
+
+#[cfg(all(test, target_os = "linux"))]
+mod flatpak_tests {
+ use super::*;
+
+ #[test]
+ fn cjk_discovery_includes_host_fonts_with_the_matching_collection_face() {
+ for script in [CjkScript::Japanese, CjkScript::SimplifiedChinese, CjkScript::TraditionalChinese, CjkScript::Korean] {
+ let files = font_files(script);
+ let native = &files[0];
+ let host = &files[1];
+ assert_eq!(native.path, std::path::Path::new("/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc"));
+ assert_eq!(host.path, std::path::Path::new("/run/host/fonts/opentype/noto/NotoSansCJK-Regular.ttc"));
+ assert_eq!(host.family, families(script).iter().find(|family| family.starts_with("Noto Sans CJK")).copied().unwrap());
+ for pair in files.windows(2) {
+ if let Ok(relative) = pair[0].path.strip_prefix("/usr/share/fonts") {
+ assert_eq!(pair[1].path, std::path::Path::new("/run/host/fonts").join(relative));
+ assert_eq!(pair[1].family, pair[0].family);
+ }
+ }
+ }
+ }
+
+ #[test]
+ fn last_resort_discovery_includes_the_host_droid_font() {
+ let files = last_resort_files();
+ assert!(
+ files.iter().any(|file| file.path == std::path::Path::new("/run/host/fonts/truetype/droid/DroidSansFallbackFull.ttf") && file.family.is_empty())
+ );
+ }
+}