From d40ddddc7c6f4b200fba2e42f03f035f1c3a23a7 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Mon, 30 Nov 2026 07:23:27 -0800 Subject: [PATCH] Use the interface language for UI font fallback --- apps/vectorcraft-web/src/web.rs | 3 +- apps/vectorcraft/src/main.rs | 9 ++-- crates/ui-egui/src/app.rs | 3 ++ crates/ui-egui/src/lib.rs | 2 +- crates/ui-egui/src/ui_fonts.rs | 80 ++++++++++++++++++++++++++++++++- 5 files changed, 89 insertions(+), 8 deletions(-) diff --git a/apps/vectorcraft-web/src/web.rs b/apps/vectorcraft-web/src/web.rs index 64a596e5ba..519056ba0c 100644 --- a/apps/vectorcraft-web/src/web.rs +++ b/apps/vectorcraft-web/src/web.rs @@ -143,7 +143,8 @@ async fn run(host: Host, canvas: web_sys::HtmlCanvasElement, moving: Option std::process::ExitCode { Some(pen) => spec.pen(pen), None => spec, }; - embed::install(ctx, spec, &vectorcraft_ui_egui::ui_fonts()).map_err(|e| e.to_string())?; - egui_extras::install_image_loaders(ctx); let folders = settings.lock().get().ui.last_folders.clone(); let wake = { let ctx = ctx.clone(); @@ -848,7 +846,6 @@ fn main() -> std::process::ExitCode { .settings(Arc::new(settings.clone())) .files(files.clone()) .build(); - host.clone().install(ctx); let mut session = Session::new(); // The profiles loaded before come back before the preferences name any of them. prefs_dir::attach_profile_store(&mut session); @@ -873,12 +870,16 @@ fn main() -> std::process::ExitCode { prefs_dir::attach_proof_setups(&mut session); prefs_dir::attach_script_data(&mut session); let mut app = VectorcraftApp::new(session, services(winit_window.clone())); + load_prefs(&mut app, &settings); + let fonts = vectorcraft_ui_egui::configure_ui_fonts(ctx, app.ui_language(), None); + embed::install(ctx, spec, &fonts).map_err(|e| e.to_string())?; + egui_extras::install_image_loaders(ctx); + host.clone().install(ctx); app.host = Arc::new(host); app.adopt_context(&cc.egui_ctx); // A nightly is updated by the Launcher: it never checks for a stable release, which // would install this program's own updater over the nightly build. app.set_startup_update_check(!nightly); - load_prefs(&mut app, &settings); safe_mode::after_start(&mut app, stopped); // Loads started later (a preference change, the end of Safe Mode) are waited for, // after the launch's own. diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 228a01b2f6..2fe54745e7 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -410,6 +410,9 @@ impl VectorcraftApp { let lang = self.ui_language(); if self.host.lang() != lang { self.host = Arc::new(self.host.with_lang(lang)); + if let Some(ctx) = &self.ui_ctx { + crate::ui_fonts::follow_language(ctx, lang); + } } let native_ppp = self.ui_ctx.as_ref().map_or(1.0, |ctx| ctx.pixels_per_point() / ctx.zoom_factor()); // The canvas steps through the zoom stops of the current window's display. diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 3cd948fce0..8b24682035 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -129,7 +129,7 @@ pub use caches::{CacheKey, CanvasCache, LivePaintGapsCache, Perf, PlacedKey, Pri #[cfg(test)] pub(crate) use services::browsable_url; pub use services::{DownloadFn, ExistsFn, Inbox, ReadFn, SaveStreamFn, SaveWriteFn, ScreenDensity, ScreenPpi, Services, WriteFn}; -pub use ui_fonts::ui_fonts; +pub use ui_fonts::{configure_ui_fonts, ui_fonts}; pub struct VectorcraftApp { pub session: Session, diff --git a/crates/ui-egui/src/ui_fonts.rs b/crates/ui-egui/src/ui_fonts.rs index 0e00238ab4..f01e7dbcb8 100644 --- a/crates/ui-egui/src/ui_fonts.rs +++ b/crates/ui-egui/src/ui_fonts.rs @@ -1,10 +1,12 @@ //! The app's interface font stack: the craft faces it embeds (Japanese, Georgian, Devanagari, //! Gujarati), JetBrains Mono for monospace, and the framework's loader for every other script. -//! The stack is installed once by the shell (`embed::install`). +//! The shell installs the stack at startup and refreshes it when the interface language changes. use std::sync::Arc; -use craft::ui::fonts::{Family, I18nScripts, SEMIBOLD, UiFonts}; +use craft::ui::fonts::{Family, FontFetcher, I18nScripts, SEMIBOLD, UiFonts}; + +use crate::i18n::{Lang, LangExt}; /// The interface fonts configuration. Characters no face covers are looked up in the installed /// system fonts on native; the web build has the faces its server offers instead. @@ -22,6 +24,49 @@ pub fn ui_fonts() -> UiFonts<'static> { } } +#[derive(Clone)] +struct FontSelection { + language: Lang, + fetcher: Option>, +} + +impl FontSelection { + fn fonts(&self) -> UiFonts<'static> { + let fonts = ui_fonts().ui_language(self.language.code()); + match &self.fetcher { + Some(fetcher) => fonts.fetcher(Arc::clone(fetcher)), + None => fonts, + } + } +} + +fn font_selection_id() -> egui::Id { + egui::Id::new("vectorcraft-interface-font-selection") +} + +/// Builds the shell's initial font configuration and keeps its language and font fetcher with +/// this context so they can be reused when the interface language changes. +pub fn configure_ui_fonts(ctx: &egui::Context, language: Lang, fetcher: Option>) -> UiFonts<'static> { + let selection = FontSelection { language, fetcher }; + let fonts = selection.fonts(); + ctx.data_mut(|data| data.insert_temp(font_selection_id(), Some(selection))); + fonts +} + +pub(crate) fn follow_language(ctx: &egui::Context, language: Lang) { + let changed = ctx.data_mut(|data| { + let selection = data.get_temp_mut_or_default::>(font_selection_id()).as_mut()?; + if selection.language == language { + return None; + } + selection.language = language; + Some(selection.clone()) + }); + if let Some(selection) = changed { + selection.fonts().install(ctx); + } +} + #[cfg(test)] mod tests { use craft::ui::fonts::{FontRef, Sample, UiFonts, assert_covers, missing_glyphs}; @@ -53,6 +98,37 @@ mod tests { assert_covers(&ui_fonts().stack(), [Sample::new("ja", "japanese", JAPANESE)]); } + #[test] + fn han_fonts_follow_the_interface_language() { + if !embeds("Hans") || !embeds("Jpan") { + eprintln!("skipped: built without craft-fonts (set CRAFT_FONTS_DIR to a craft-fonts checkout to run it)"); + return; + } + let mut app = crate::VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + let ctx = crate::testing::ui_context(); + app.session.prefs.interface_language = "zh-hans".into(); + configure_ui_fonts(&ctx, app.ui_language(), None).install(&ctx); + + for (language, preferred, alternate) in [ + (None, "Noto Sans CJK SC", "BIZ UDPGothic"), + (Some("ja"), "BIZ UDPGothic", "Noto Sans CJK SC"), + (Some("zh-hans"), "Noto Sans CJK SC", "BIZ UDPGothic"), + ] { + if let Some(language) = language { + crate::menus::run_ui_command(&mut app, "app.language", &serde_json::json!({"lang": language})).unwrap().unwrap(); + } + crate::testing::part_frame(&mut app, &ctx, egui::RawInput::default(), |_, ui| { + ui.ctx().fonts(|fonts| { + let chain = &fonts.definitions().families[&egui::FontFamily::Proportional]; + let index = |family: &str| { + chain.iter().position(|face| face.starts_with(family)).unwrap_or_else(|| panic!("missing {family} in {chain:?}")) + }; + assert!(index(preferred) < index(alternate), "expected {preferred} before {alternate} in {chain:?}"); + }); + }); + } + } + #[test] fn the_ui_works_without_the_craft_fonts() { let stack = UiFonts::new(&I18nScripts).stack();