mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 21:12:57 -05:00
Use the interface language for UI font fallback
This commit is contained in:
1 parent
582fa21727
commit
d40ddddc7c
5 files changed
+89
-8
No files matched your search
@@ -143,7 +143,8 @@ async fn run(host: Host, canvas: web_sys::HtmlCanvasElement, moving: Option<Movi
|
||||
let pen = host.pointers.borrow().pen();
|
||||
let spec = InstallSpec::new(ShellInfo::current(), Arc::new(SystemWindows)).pen(pen);
|
||||
// The interface faces the page serves are fetched by the stack as text needs them.
|
||||
let fonts = vectorcraft_ui_egui::ui_fonts().fetcher(Arc::new(crate::fonts::BrowserFetcher));
|
||||
let language = taken.borrow().as_ref().map_or_else(i18n::system_lang, VectorcraftApp::ui_language);
|
||||
let fonts = vectorcraft_ui_egui::configure_ui_fonts(ctx, language, Some(Arc::new(crate::fonts::BrowserFetcher)));
|
||||
embed::install(ctx, spec, &fonts).map_err(|e| e.to_string())?;
|
||||
egui_extras::install_image_loaders(ctx);
|
||||
let files = {
|
||||
|
||||
@@ -830,8 +830,6 @@ fn main() -> 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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<Arc<dyn FontFetcher>>,
|
||||
}
|
||||
|
||||
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<Arc<dyn FontFetcher>>) -> 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::<Option<FontSelection>>(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();
|
||||
|
||||
Reference in new issue
Block a user