diff --git a/crates/ui-egui/build.rs b/crates/ui-egui/build.rs index 1026119aba..864aea32d6 100644 --- a/crates/ui-egui/build.rs +++ b/crates/ui-egui/build.rs @@ -37,19 +37,27 @@ const CRAFT_SCRIPTS: &[&str] = &["Deva", "Beng", "Guru", "Gujr", "Orya", "Taml", /// Writes `$OUT_DIR/craft_fonts.rs`: the optional craft-fonts build input (`CRAFT_FONTS_DIR`, see /// craft-fonts' docs/integration.md). Unset means an empty table and no change; set but unusable is -/// a warning, or an error with `CRAFT_FONTS_REQUIRED`. +/// a warning. `CRAFT_FONTS_REQUIRED` makes both an error, and so is a table missing a +/// [`CRAFT_SCRIPTS`] script. fn craft_fonts_main() { println!("cargo::rerun-if-env-changed=CRAFT_FONTS_DIR"); println!("cargo::rerun-if-env-changed=CRAFT_FONTS_REQUIRED"); + let required = std::env::var_os("CRAFT_FONTS_REQUIRED").is_some(); let mut src = String::from("pub static CRAFT_FONTS: &[CraftFont] = &[\n"); - if let Some(dir) = std::env::var_os("CRAFT_FONTS_DIR").map(std::path::PathBuf::from) { - match craft_fonts(&dir) { - Ok(entries) => src.push_str(&entries), - Err(e) if std::env::var_os("CRAFT_FONTS_REQUIRED").is_some() => { - println!("cargo::error=CRAFT_FONTS_DIR={}: {e}", dir.display()); + match std::env::var_os("CRAFT_FONTS_DIR").map(std::path::PathBuf::from) { + Some(dir) => match craft_fonts(&dir) { + Ok((entries, covered)) => { + let missing: Vec<&str> = CRAFT_SCRIPTS.iter().copied().filter(|s| !covered.contains(s)).collect(); + if required && !missing.is_empty() { + println!("cargo::error=CRAFT_FONTS_DIR={}: no face for {}", dir.display(), missing.join(", ")); + } + src.push_str(&entries); } + Err(e) if required => println!("cargo::error=CRAFT_FONTS_DIR={}: {e}", dir.display()), Err(e) => println!("cargo::warning=building without craft-fonts: CRAFT_FONTS_DIR={}: {e}", dir.display()), - } + }, + None if required => println!("cargo::error=CRAFT_FONTS_REQUIRED is set but CRAFT_FONTS_DIR is not"), + None => {} } src.push_str("];\n"); let out = std::path::PathBuf::from(std::env::var_os("OUT_DIR").unwrap_or_default()).join("craft_fonts.rs"); @@ -58,12 +66,14 @@ fn craft_fonts_main() { } } -/// One `CraftFont { .. }` initialiser per manifest line whose scripts include a [`CRAFT_SCRIPTS`] one. -fn craft_fonts(dir: &std::path::Path) -> Result { +/// One `CraftFont { .. }` initialiser per manifest line whose scripts include a [`CRAFT_SCRIPTS`] one, +/// and the [`CRAFT_SCRIPTS`] scripts those lines cover. +fn craft_fonts(dir: &std::path::Path) -> Result<(String, Vec<&'static str>), String> { let manifest = dir.join("fonts/manifest.txt"); println!("cargo::rerun-if-changed={}", manifest.display()); let text = std::fs::read_to_string(&manifest).map_err(|e| format!("{}: {e}", manifest.display()))?; let mut out = String::new(); + let mut covered = Vec::new(); for line in text.lines().map(str::trim).filter(|l| !l.is_empty() && !l.starts_with('#')) { let f: Vec<&str> = line.split(" | ").map(str::trim).collect(); let [family, style, file, scripts, ..] = f.as_slice() else { @@ -73,6 +83,7 @@ fn craft_fonts(dir: &std::path::Path) -> Result { if !scripts.iter().any(|s| CRAFT_SCRIPTS.contains(s)) { continue; } + covered.extend(CRAFT_SCRIPTS.iter().copied().filter(|s| scripts.contains(s))); let path = dir.join(file).canonicalize().map_err(|e| format!("{file}: {e}"))?; println!("cargo::rerun-if-changed={}", path.display()); let scripts: Vec = scripts.iter().map(|s| format!("{s:?}")).collect(); @@ -83,7 +94,7 @@ fn craft_fonts(dir: &std::path::Path) -> Result { path.display().to_string(), ); } - Ok(out) + Ok((out, covered)) } fn read(path: &std::path::Path, fallback: &str, parse: impl Fn(&str) -> Result) -> Option { diff --git a/crates/ui-egui/src/theme.rs b/crates/ui-egui/src/theme.rs index 3a5aa5a74b..7f81b4344a 100644 --- a/crates/ui-egui/src/theme.rs +++ b/crates/ui-egui/src/theme.rs @@ -832,19 +832,23 @@ fn indic(script: Indic) -> Vec<(String, u32)> { }; let mut v: Vec<(String, u32)> = vec![(format!("{win}\\Nirmala.ttc"), 0), (format!("{win}\\Nirmala.ttf"), 0), (format!("{win}\\{win_face}"), 0)]; v.extend(mac.iter().map(|(m, index)| (m.replace("{sys}", mac_sys).replace("{sup}", mac_sup), *index))); - for d in [ - "/usr/share/fonts/truetype/noto", - "/usr/share/fonts/opentype/noto", - "/usr/share/fonts/google-noto", - "/usr/share/fonts/noto", - "/usr/share/fonts/TTF", - "/usr/local/share/fonts/noto", - ] { - v.push((format!("{d}/NotoSans{noto}-Regular.ttf"), 0)); + let mut linux: Vec = Vec::new(); + for d in ["truetype/noto", "opentype/noto", "google-noto", "noto", "TTF"] { + linux.push(format!("{d}/NotoSans{noto}-Regular.ttf")); } + // Fedora ships the variable Noto Sans faces; their default instance is Regular. + linux.push(format!("google-noto-vf/NotoSans{noto}[wght].ttf")); let (dir, file) = lohit; - for d in ["/usr/share/fonts/truetype", "/usr/share/fonts", "/usr/local/share/fonts"] { - v.push((format!("{d}/{dir}/{file}.ttf"), 0)); + for d in ["truetype/", ""] { + linux.push(format!("{d}{dir}/{file}.ttf")); + } + // The system fonts, then the same layouts under a Flatpak sandbox's read-only host mounts. + for root in ["/usr/share/fonts", "/run/host/fonts"] { + v.extend(linux.iter().map(|f| (format!("{root}/{f}"), 0))); + } + for root in ["/usr/local/share/fonts", "/run/host/local-fonts"] { + v.push((format!("{root}/noto/NotoSans{noto}-Regular.ttf"), 0)); + v.push((format!("{root}/{dir}/{file}.ttf"), 0)); } v } @@ -1229,6 +1233,34 @@ mod tests { } } + #[test] + fn indic_faces_reach_fedora_and_flatpak_layouts() { + // Fedora installs only the variable Noto Sans faces; a Flatpak sees the host's fonts under + // /run/host. Missing either leaves the interface in tofu boxes there. + let cases = [ + (Indic::Devanagari, "Devanagari"), + (Indic::Bengali, "Bengali"), + (Indic::Gurmukhi, "Gurmukhi"), + (Indic::Gujarati, "Gujarati"), + (Indic::Odia, "Oriya"), + (Indic::Tamil, "Tamil"), + (Indic::Telugu, "Telugu"), + (Indic::Kannada, "Kannada"), + (Indic::Malayalam, "Malayalam"), + ]; + for (script, noto) in cases { + let paths: Vec = indic(script).into_iter().map(|(path, _)| path).collect(); + for expected in [ + format!("/usr/share/fonts/google-noto-vf/NotoSans{noto}[wght].ttf"), + format!("/run/host/fonts/google-noto-vf/NotoSans{noto}[wght].ttf"), + format!("/run/host/fonts/truetype/noto/NotoSans{noto}-Regular.ttf"), + format!("/run/host/local-fonts/noto/NotoSans{noto}-Regular.ttf"), + ] { + assert!(paths.contains(&expected), "{script:?} doesn't probe {expected}"); + } + } + } + #[test] fn fallback_covers_its_script() { // Real render check, per script: the proportional family (what cells and the UI use) must