From 392e4cc0c9fe4b0e6153a57fc4c9d25ccc101e11 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Thu, 16 Jan 2025 16:39:14 -0800 Subject: [PATCH] feat(bar): add cjk font fallbacks This commit adds CJK font fallbacks to Microsoft YaHei and Malgun Gothic. This will be looked up at runtime on the user's system, and only loaded if the files exist in the default Windows font installation location. resolve #1139 --- komorebi-bar/src/bar.rs | 46 ++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/komorebi-bar/src/bar.rs b/komorebi-bar/src/bar.rs index 862b3895..08b9fc21 100644 --- a/komorebi-bar/src/bar.rs +++ b/komorebi-bar/src/bar.rs @@ -44,6 +44,7 @@ use komorebi_themes::Base16Value; use komorebi_themes::Catppuccin; use komorebi_themes::CatppuccinValue; use std::cell::RefCell; +use std::collections::HashMap; use std::path::PathBuf; use std::rc::Rc; use std::sync::atomic::Ordering; @@ -503,6 +504,27 @@ impl Komobar { let mut fonts = FontDefinitions::default(); egui_phosphor::add_to_fonts(&mut fonts, egui_phosphor::Variant::Regular); + let mut fallbacks = HashMap::new(); + + fallbacks.insert("Microsoft YaHei", "C:\\Windows\\Fonts\\msyh.ttc"); // chinese + fallbacks.insert("Malgun Gothic", "C:\\Windows\\Fonts\\malgun.ttf"); // korean + + for (name, path) in fallbacks { + if let Ok(bytes) = std::fs::read(path) { + fonts + .font_data + .insert(name.to_owned(), Arc::from(FontData::from_owned(bytes))); + + for family in [FontFamily::Proportional, FontFamily::Monospace] { + fonts + .families + .entry(family) + .or_default() + .insert(0, name.to_owned()); + } + } + } + let property = FontPropertyBuilder::new().family(name).build(); if let Some((font, _)) = system_fonts::get(&property) { @@ -510,21 +532,17 @@ impl Komobar { .font_data .insert(name.to_owned(), Arc::new(FontData::from_owned(font))); - fonts - .families - .entry(FontFamily::Proportional) - .or_default() - .insert(0, name.to_owned()); - - fonts - .families - .entry(FontFamily::Monospace) - .or_default() - .push(name.to_owned()); - - // Tell egui to use these fonts: - ctx.set_fonts(fonts); + for family in [FontFamily::Proportional, FontFamily::Monospace] { + fonts + .families + .entry(family) + .or_default() + .insert(0, name.to_owned()); + } } + + // Tell egui to use these fonts: + ctx.set_fonts(fonts); } } impl eframe::App for Komobar {