diff --git a/crates/ui-egui/src/system_fonts.rs b/crates/ui-egui/src/system_fonts.rs index 3a44e03c..3083fc5f 100644 --- a/crates/ui-egui/src/system_fonts.rs +++ b/crates/ui-egui/src/system_fonts.rs @@ -3,7 +3,8 @@ //! The embedded faces (Inter, egui's defaults, craft-fonts) come first in every family; this one //! only draws characters none of them has, such as an Arabic file name in a build without //! craft-fonts, or the Simplified Chinese interface in a build without a `Hans` face (#826). A -//! second installed face, [`devanagari_fallback`], draws Hindi when one is installed. It is +//! second and a third installed face, [`devanagari_fallback`] and [`telugu_fallback`], draw Hindi +//! and Telugu when one is installed. It is //! read at runtime and never embedded or shipped (AGENTS.md §1.4), and `PDFCRAFT_SYSTEM_FONTS=0` //! turns it off (published screenshots do). @@ -22,6 +23,8 @@ const MAX_FACES: u32 = 16; const PROBE: char = '\u{0627}'; /// Devanagari letter ka: the face must have it to cover Hindi. const DEVANAGARI_PROBE: char = '\u{0915}'; +/// Telugu letter ka: the face must have it to cover Telugu. +const TELUGU_PROBE: char = '\u{0C15}'; /// 欢 (U+6B22): Simplified only, absent from the Japanese craft-fonts faces. const HANS_PROBE: char = '\u{6B22}'; /// 說 (U+8AAA): Traditional only, absent from the Japanese craft-fonts faces. @@ -69,14 +72,21 @@ fn load(need: Need) -> Option> { /// or no candidate is installed; the interface then draws what the embedded faces can. pub fn devanagari_fallback() -> Option> { static CACHE: OnceLock>> = OnceLock::new(); - CACHE - .get_or_init(|| { - if std::env::var_os("PDFCRAFT_SYSTEM_FONTS").is_some_and(|v| v == "0") { - return None; - } - devanagari_candidates().iter().find_map(|path| read(path, DEVANAGARI_PROBE, MAX_BYTES)) - }) - .clone() + CACHE.get_or_init(|| script_fallback(&devanagari_candidates(), DEVANAGARI_PROBE)).clone() +} + +/// The installed fallback face for Telugu, read once. `None` when it is turned off or no +/// candidate is installed; the interface then draws what the embedded faces can. +pub fn telugu_fallback() -> Option> { + static CACHE: OnceLock>> = OnceLock::new(); + CACHE.get_or_init(|| script_fallback(&telugu_candidates(), TELUGU_PROBE)).clone() +} + +fn script_fallback(candidates: &[PathBuf], probe: char) -> Option> { + if std::env::var_os("PDFCRAFT_SYSTEM_FONTS").is_some_and(|v| v == "0") { + return None; + } + candidates.iter().find_map(|path| read(path, probe, MAX_BYTES)) } /// Well-known locations of Devanagari (Hindi) faces, best first. @@ -114,6 +124,37 @@ fn devanagari_candidates() -> Vec { } } +/// Well-known locations of Telugu faces, best first. +fn telugu_candidates() -> Vec { + if cfg!(windows) { + ["Nirmala.ttc", "Nirmala.ttf", "gautami.ttf"].iter().map(|f| windows_fonts().join(f)).collect() + } else if cfg!(target_os = "macos") { + [ + "/System/Library/Fonts/KohinoorTelugu.ttc", + "/System/Library/Fonts/Supplemental/Telugu Sangam MN.ttc", + "/System/Library/Fonts/Supplemental/Telugu MN.ttc", + "/Library/Fonts/Arial Unicode.ttf", + ] + .iter() + .map(PathBuf::from) + .collect() + } else { + unix_fonts(&[ + "google-noto/NotoSansTeluguUI-Regular.ttf", + "google-noto/NotoSansTelugu-Regular.ttf", + "google-noto-vf/NotoSansTelugu[wght].ttf", + "google-noto-vf/NotoSansTelugu-UI[wght].ttf", + "truetype/noto/NotoSansTelugu-Regular.ttf", + "truetype/noto/NotoSansTeluguUI-Regular.ttf", + "noto/NotoSansTelugu-Regular.ttf", + "noto/NotoSansTeluguUI-Regular.ttf", + "lohit-telugu-fonts/Lohit-Telugu.ttf", + "truetype/lohit-telugu/Lohit-Telugu.ttf", + "truetype/fonts-telu-extra/Pothana2000.ttf", + ]) + } +} + /// Well-known locations of faces with broad script coverage, best first. fn candidates(need: Need) -> Vec { match need { @@ -244,6 +285,24 @@ mod tests { } } + #[test] + fn a_script_fallback_is_the_first_candidate_with_the_probe() { + let inter = PathBuf::from(concat!(env!("CARGO_MANIFEST_DIR"), "/../../assets/fonts/Inter-Regular.ttf")); + let candidates = [PathBuf::from("definitely/not/here.ttf"), inter]; + assert!(script_fallback(&candidates, 'A').is_some()); + assert!(script_fallback(&candidates, TELUGU_PROBE).is_none()); + assert!(script_fallback(&candidates, DEVANAGARI_PROBE).is_none()); + } + + #[test] + fn script_candidates_are_absolute_font_files() { + for list in [devanagari_candidates(), telugu_candidates()] { + assert!(!list.is_empty()); + assert!(list.iter().all(|p| p.is_absolute() || cfg!(windows)), "{list:?}"); + assert!(list.iter().all(|p| p.extension().is_some_and(|e| e == "ttf" || e == "ttc")), "{list:?}"); + } + } + #[test] fn han_candidates_skip_adobe_designs() { for need in [Need::Hans, Need::Hant] { diff --git a/crates/ui-egui/src/theme.rs b/crates/ui-egui/src/theme.rs index 0d93fece..9f2bb900 100644 --- a/crates/ui-egui/src/theme.rs +++ b/crates/ui-egui/src/theme.rs @@ -218,6 +218,8 @@ pub fn install_fonts_for(ctx: &egui::Context, cjk: CjkPreference) { pub const SYSTEM_FALLBACK: &str = "system-fallback"; /// The name of the installed Devanagari (Hindi) face [`installed_font_definitions`] may add. pub const SYSTEM_DEVANAGARI_FALLBACK: &str = "system-devanagari-fallback"; +/// The name of the installed Telugu face [`installed_font_definitions`] may add. +pub const SYSTEM_TELUGU_FALLBACK: &str = "system-telugu-fallback"; /// What [`install_fonts_for`] installs: [`font_definitions_for`], then, on desktop, one face /// already installed on this machine. It only draws characters no embedded face has. @@ -230,8 +232,8 @@ pub const SYSTEM_DEVANAGARI_FALLBACK: &str = "system-devanagari-fallback"; /// - Otherwise it is the last fallback of every family (an Arabic file name in a build without /// craft-fonts). /// -/// An installed Devanagari face, when there is one, ends every family just before that one, so -/// Hindi text draws in a build without a Devanagari face. +/// An installed Devanagari face and an installed Telugu face, when there are, end every family +/// just before that one, so Hindi and Telugu text draw in a build without such faces (#1531). /// /// `PDFCRAFT_SYSTEM_FONTS=0` leaves them out. pub fn installed_font_definitions(cjk: CjkPreference) -> FontDefinitions { @@ -241,10 +243,14 @@ pub fn installed_font_definitions(cjk: CjkPreference) -> FontDefinitions { { use crate::system_fonts::{Need, fallback}; // Before the general installed face, which stays the last fallback outside Chinese. - if let Some(data) = crate::system_fonts::devanagari_fallback() { - fonts.font_data.insert(SYSTEM_DEVANAGARI_FALLBACK.to_owned(), data); + for (name, data) in [ + (SYSTEM_DEVANAGARI_FALLBACK, crate::system_fonts::devanagari_fallback()), + (SYSTEM_TELUGU_FALLBACK, crate::system_fonts::telugu_fallback()), + ] { + let Some(data) = data else { continue }; + fonts.font_data.insert(name.to_owned(), data); for stack in fonts.families.values_mut() { - stack.push(SYSTEM_DEVANAGARI_FALLBACK.to_owned()); + stack.push(name.to_owned()); } } let need = match cjk { diff --git a/crates/ui-egui/tests/fonts.rs b/crates/ui-egui/tests/fonts.rs index 96b38b07..63a31d4c 100644 --- a/crates/ui-egui/tests/fonts.rs +++ b/crates/ui-egui/tests/fonts.rs @@ -170,6 +170,29 @@ fn system_fallback_covers_devanagari() { assert!(layout_widths(&mut fonts, DEVANAGARI).iter().all(|w| w.is_finite() && *w > 0.0)); } +/// On a machine with an installed Telugu font, the installed definitions include it and Telugu +/// text has glyphs, so the language's own name in Preferences is not boxes (#1531). macOS always +/// ships Kohinoor Telugu, so there the test never skips. +#[test] +fn system_fallback_covers_telugu() { + assert!(!theme::font_definitions().font_data.contains_key(theme::SYSTEM_TELUGU_FALLBACK)); + let defs = theme::installed_font_definitions(theme::CjkPreference::Default); + let off = std::env::var_os("PDFCRAFT_SYSTEM_FONTS").is_some_and(|v| v == "0"); + if !defs.font_data.contains_key(theme::SYSTEM_TELUGU_FALLBACK) && (off || !cfg!(target_os = "macos")) { + eprintln!("skipping system_fallback_covers_telugu: no installed Telugu fallback font (or PDFCRAFT_SYSTEM_FONTS=0)"); + assert!(defs.families.values().all(|stack| !stack.iter().any(|n| n == theme::SYSTEM_TELUGU_FALLBACK))); + return; + } + for (family, stack) in &defs.families { + assert_eq!(stack.iter().filter(|n| *n == theme::SYSTEM_TELUGU_FALLBACK).count(), 1, "{family:?}: {stack:?}"); + } + let mut fonts = Fonts::new(TextOptions::default(), defs); + for id in families() { + assert!(fonts.has_glyphs(&id, TELUGU), "{id:?} lacks {TELUGU}"); + } + assert!(layout_widths(&mut fonts, TELUGU).iter().all(|w| w.is_finite() && *w > 0.0)); +} + /// The Han characters of a catalog's translations. fn catalog_han(catalog: &str) -> String { let mut han: Vec = catalog