SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
fix(web): keep the CJK face a fallback for a non-CJK UI (#3535)
Igor Bronovskyib3811168fc82
diff --git a/apps/solidcraft-web/src/main.rs b/apps/solidcraft-web/src/main.rsindex b134903b75..4b4819f654 100644--- a/apps/solidcraft-web/src/main.rs+++ b/apps/solidcraft-web/src/main.rs@@ -101,6 +101,10 @@ mod web {/// On-demand CJK font: fetched only when a CJK language is selected.font: solidcraft_ui_egui::fonts::CjkFontLoader,font_result: FontLoadResult,+ /// The fetched CJK font's bytes: kept so a language change can re-order the fallbacks.+ cjk_bytes: Option<Vec<u8>>,+ /// The language the fonts were last set up for (a CJK UI leads with the CJK face).+ font_language: String,space_mouse: SpaceMouse,}@@ -375,7 +379,10 @@ mod web {let done = self.font_result.borrow_mut().take();if let Some(res) = done {let installed = match res {- Ok(bytes) => solidcraft_ui_egui::fonts::install_cjk_font(ctx, bytes),+ Ok(bytes) => {+ self.cjk_bytes = Some(bytes);+ true+ }Err(err) => {log::warn!("failed to fetch CJK font from {JAPANESE_FONT_PATH}: {err}");false@@ -387,11 +394,29 @@ mod web {log::warn!("CJK font unavailable; showing English for this session");}self.font.finished(installed, &mut self.app.preferences.language);+ self.apply_fonts(ctx);ctx.request_repaint();}if self.font.tick(&mut self.app.preferences.language) {self.fetch_font(ctx);}+ // The CJK face leads only a CJK UI: its Cyrillic glyphs are full-width, so switching+ // to Ukrainian (or English) must put the Latin face back in front.+ if self.app.preferences.language != self.font_language {+ self.apply_fonts(ctx);+ }+ }++ /// Set up the UI fonts for the live language, keeping the fetched CJK font as a fallback+ /// (it leads only for a CJK UI, see [`solidcraft_ui_egui::fonts::install_cjk_font_for`]).+ fn apply_fonts(&mut self, ctx: &egui::Context) {+ match self.cjk_bytes.clone() {+ Some(bytes) => {+ solidcraft_ui_egui::fonts::install_cjk_font_for(ctx, bytes, &self.app.preferences.language);+ }+ None => solidcraft_ui_egui::fonts::setup_fonts(ctx),+ }+ self.font_language.clone_from(&self.app.preferences.language);}/// CJK text on screen with no CJK font yet (an English UI showing a Japanese name):@@ -549,6 +574,7 @@ mod web {app.home.show(rev);}let api = Api::install(sender, cc.egui_ctx.clone()).map_err(|e| format!("SolidCraft browser API: {e:?}"))?;+ let font_language = app.preferences.language.clone();Ok(Box::new(Shell {app,api,@@ -560,6 +586,8 @@ mod web {recents_synced: false,font: Default::default(),font_result: Rc::new(std::cell::RefCell::new(None)),+ cjk_bytes: None,+ font_language,space_mouse: SpaceMouse::default(),}))}),diff --git a/crates/ui-egui/src/fonts.rs b/crates/ui-egui/src/fonts.rsindex da556f4004..bc54ccc14d 100644--- a/crates/ui-egui/src/fonts.rs+++ b/crates/ui-egui/src/fonts.rs@@ -137,14 +137,11 @@ fn promote_font_to_front(fonts: &mut egui::FontDefinitions, name: &str) {}}-/// Install font bytes as fallback for Proportional and Monospace font families.-/// For CJK languages, promotes the font to the front of Proportional so digits and labels share one baseline.+/// Install font bytes as a fallback for the Proportional and Monospace families for `language`.+/// For a CJK language the font is promoted to the front of Proportional so digits and labels share+/// one baseline with the CJK glyphs; for any other language it stays a fallback (its Cyrillic can+/// be full-width, so it must not draw the Latin or Cyrillic UI text)./// Returns true if the font was parsed and installed successfully.-pub fn install_cjk_font(ctx: &egui::Context, bytes: Vec<u8>) -> bool {- install_cjk_font_for(ctx, bytes, "ja")-}--/// Install font bytes for `language`.pub fn install_cjk_font_for(ctx: &egui::Context, bytes: Vec<u8>, language: &str) -> bool {match with_cjk_fallback(egui::FontDefinitions::default(), bytes) {Some(mut fonts) => {@@ -680,8 +677,8 @@ fn find_chinese_font_in(dirs: &[std::path::PathBuf]) -> Option<Vec<u8>> {#[cfg(target_arch = "wasm32")]pub fn setup_fonts_for(_ctx: &egui::Context, _language: &str, _ui_font: &str) {}-/// On web only the symbol fallback goes in at start (#2955); the Japanese font loads later-/// through [`install_cjk_font`], which keeps it.+/// On web only the symbol fallback goes in at start (#2955); the CJK font loads later through+/// [`install_cjk_font_for`], which keeps it as a fallback unless the UI is a CJK language.#[cfg(target_arch = "wasm32")]pub fn setup_fonts(ctx: &egui::Context) {ctx.set_fonts(with_symbol_fallback(egui::FontDefinitions::default()));@@ -819,6 +816,30 @@ mod tests {assert!(prop.contains(&defaults.families[&egui::FontFamily::Proportional][0]), "Latin font remains in the fallback chain");}+ /// A Ukrainian UI drew Cyrillic wide and loose: the bundled CJK face (BIZ UDPGothic) sets every+ /// Cyrillic glyph in a full em, so it may only draw a non-CJK UI's text when nothing else has+ /// the glyph. A CJK UI still leads with it, so its digits share the CJK baseline.+ #[test]+ fn a_non_cjk_ui_keeps_the_cjk_face_as_a_fallback() {+ let installed = |language: &str| {+ let ctx = egui::Context::default();+ assert!(install_cjk_font_for(&ctx, latin_font_bytes(), language), "{language}: the font installs");+ ctx.run_ui(egui::RawInput::default(), |_| {}).textures_delta.clear();+ ctx.fonts(|f| f.definitions().families[&egui::FontFamily::Proportional].clone())+ };+ let latin = egui::FontDefinitions::default().families[&egui::FontFamily::Proportional][0].clone();+ for language in ["uk", "en", "de"] {+ let list = installed(language);+ assert_eq!(list.first(), Some(&latin), "{language} keeps the Latin face first: {list:?}");+ assert_eq!(list.last().map(String::as_str), Some(SYMBOL_FALLBACK), "{language} ends with the symbol fallback: {list:?}");+ assert!(list.iter().position(|name| name == CJK_FALLBACK).is_some_and(|at| at > 0), "{language} keeps CJK behind Latin: {list:?}");+ }+ for language in ["ja", "zh", "ko"] {+ let list = installed(language);+ assert_eq!(list.first().map(String::as_str), Some(CJK_FALLBACK), "{language} leads with the CJK face: {list:?}");+ }+ }+#[test]fn hangul_font_falls_back_after_the_cjk_font() {let defaults = egui::FontDefinitions::default();