diff --git a/crates/ui-egui/src/tests.rs b/crates/ui-egui/src/tests.rs index d70823fb64..80d3c76920 100644 --- a/crates/ui-egui/src/tests.rs +++ b/crates/ui-egui/src/tests.rs @@ -357,6 +357,40 @@ fn non_latin_file_name_is_drawn_in_the_caption() { assert!(cjk.canvas.ime_faces.is_empty(), "CJK collections are not copied into the fallback list"); } +/// The CJK UI font a Japanese file name asks for is loaded and draws the name, also in a build +/// without craft-i18n's CJK catalogs and craft-fonts' CJK faces. There it was never loaded, and +/// the caption, like an input method's composition, showed boxes. +#[test] +fn a_japanese_file_name_is_drawn_in_the_caption() { + // A font to draw it with: an installed CJK font in a build without embedded CJK faces, or + // embedded faces that have the name's characters (embedded faces without them load no + // installed font for it). + let embedded = wordcraft_fonts::ui_cjk_fonts_for(false, false); + let drawable = if embedded.is_empty() { + wordcraft_fonts::system_cjk_ui_font_for(false, false, &[]).is_some() + } else { + wordcraft_fonts::ui_uncovered_cjk(&["日本語"], &embedded).is_empty() + }; + // macOS and Windows always have one (Hiragino, Yu Gothic), and CI runs these tests on both (the + // Print job), so there a missing font fails instead of hiding a regression. + assert!(drawable || cfg!(not(any(target_os = "macos", windows))), "macOS and Windows have a CJK font, but none draws 日本語"); + if !drawable { + eprintln!("skipped: no CJK font draws 日本語"); + return; + } + let ctx = egui::Context::default(); + let frame = |app: &mut WordApp| { + ctx.run_ui(egui::RawInput::default(), |ui| app.logic(ui.ctx())).drop_without_applying_deltas(); + }; + let mut a = app(); + a.session.path = Some("日本語.docx".into()); + // The fonts the name asks for load from the next frame. + frame(&mut a); + frame(&mut a); + assert!(a.want_system_cjk); + assert!(ctx.fonts_mut(|f| f.has_glyphs(&crate::theme::semibold(13.0), "日本語")), "the caption draws the name"); +} + /// File › Recover draws the recovered documents' names with the interface faces: a Chinese /// name there was a row of boxes because nothing asked for the CJK UI font. (The Recent list /// has its own, `ensure_recent_fonts`.) diff --git a/crates/ui-egui/src/theme.rs b/crates/ui-egui/src/theme.rs index a67814261f..03a040c658 100644 --- a/crates/ui-egui/src/theme.rs +++ b/crates/ui-egui/src/theme.rs @@ -695,7 +695,8 @@ fn system_arabic_font() -> Option<&'static wordcraft_fonts::SystemUiFont> { /// The installed CJK interface font for a Chinese (`hans`) or other interface, chosen once: /// `None` when the embedded faces already cover the interface text /// ([`cjk_font_text`](crate::i18n::Lang::cjk_font_text)). The font is picked for the catalogs' -/// missing characters, or for the language names when only those are missing. +/// missing characters, or for the language names when only those are missing; see +/// [`system_cjk_needed`]. #[cfg(not(target_arch = "wasm32"))] fn system_cjk_font(hans: bool, korean: bool) -> Option<&'static wordcraft_fonts::SystemUiFont> { static ZH: std::sync::OnceLock> = std::sync::OnceLock::new(); @@ -711,19 +712,41 @@ fn system_cjk_font(hans: bool, korean: bool) -> Option<&'static wordcraft_fonts: .get_or_init(|| { let embedded = wordcraft_fonts::ui_cjk_fonts_for(hans, korean); let (catalogs, names) = crate::i18n::Lang::cjk_font_text_for(hans, korean); - let mut needed = wordcraft_fonts::ui_uncovered_cjk(&catalogs, &embedded); - if needed.is_empty() { - needed = wordcraft_fonts::ui_uncovered_cjk(&[&names], &embedded); - if needed.is_empty() { - return None; - } - } + let needed = system_cjk_needed(&catalogs, &names, &embedded)?; log::debug!("interface: {} CJK characters lack an embedded glyph", needed.len()); - wordcraft_fonts::system_cjk_ui_font_for(hans, korean, &needed) + let found = wordcraft_fonts::system_cjk_ui_font_for(hans, korean, &needed); + if found.is_none() && needed.is_empty() && !hans && !korean { + // No Japanese or Chinese font installed: a Korean one (NanumGothic alone on Linux) + // still draws Korean document text. + return wordcraft_fonts::system_cjk_ui_font_for(false, true, &needed); + } + found }) .as_ref() } +/// The CJK characters an installed interface font is picked for: those of the catalogs, or of +/// the language names, that the embedded faces lack. `None` when the embedded faces draw them all. +/// +/// The interface asks for this font only when CJK is on screen: the interface language, or CJK +/// document text drawn as interface text (an input method's composition, a file name in the +/// caption). A build with neither craft-i18n's CJK catalogs nor craft-fonts' CJK faces has no CJK +/// text of its own to measure, and that text drew as boxes. It gets the font picked for the +/// language's own characters (an empty list: [`wordcraft_fonts::system_cjk_ui_font_for`] probes +/// one, `の` for a Japanese or other interface), or failing that a Korean font. +#[cfg(not(target_arch = "wasm32"))] +pub(crate) fn system_cjk_needed(catalogs: &[&str], names: &str, embedded: &[&wordcraft_fonts::CraftFont]) -> Option> { + let needed = wordcraft_fonts::ui_uncovered_cjk(catalogs, embedded); + if !needed.is_empty() { + return Some(needed); + } + let needed = wordcraft_fonts::ui_uncovered_cjk(&[names], embedded); + if !needed.is_empty() { + return Some(needed); + } + embedded.is_empty().then(Vec::new) +} + #[cfg(not(target_arch = "wasm32"))] fn system_hant_font() -> Option<&'static wordcraft_fonts::SystemUiFont> { static HANT: std::sync::OnceLock> = std::sync::OnceLock::new(); @@ -1160,6 +1183,29 @@ mod tests { assert!(semi.iter().any(|n| n.starts_with("fallback ")), "the caption is painted with semibold"); } + /// A build without craft-i18n's CJK catalogs and craft-fonts' CJK faces has no CJK interface + /// text of its own, so the CJK UI font that CJK document text asks for (a composition, a file + /// name, a comment) was never loaded, and that text drew as boxes. It is now picked for the + /// language's own characters. + #[test] + fn cjk_text_gets_an_installed_cjk_font_in_a_build_without_one() { + assert_eq!(super::system_cjk_needed(&[], "English", &[]), Some(Vec::new()), "picked for the language's own characters"); + assert_eq!(super::system_cjk_needed(&["ファイル"], "English", &[]), Some(vec!['ァ', 'イ', 'フ', 'ル'])); + assert_eq!(super::system_cjk_needed(&[], "English 日本語", &[]), Some(vec!['日', '本', '語'])); + let embedded = wordcraft_fonts::ui_cjk_fonts_for(false, false); + if !embedded.is_empty() { + assert_eq!(super::system_cjk_needed(&[], "English", &embedded), None, "the embedded faces draw CJK"); + } else { + // macOS and Windows always have one, and CI runs these tests on both (the Print job). + let installed = wordcraft_fonts::system_cjk_ui_font_for(false, false, &[]).is_some(); + assert!(installed || cfg!(not(any(target_os = "macos", windows))), "macOS and Windows have a CJK font"); + if installed { + let defs = super::font_definitions_language(false, false, false, true); + assert!(defs.font_data.keys().any(|n| n.starts_with("system ")), "the installed CJK font follows the interface fonts"); + } + } + } + /// #656: dialog primary buttons, the Discord pill, the table eraser and the Navigation search /// highlight take their colours from the theme, not fixed light-theme values. #[test]