Skip to content

Commit

Permalink
feat(bar): add cjk font fallbacks
Browse files Browse the repository at this point in the history
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
  • Loading branch information
LGUG2Z committed Jan 17, 2025
1 parent 129dc5d commit 392e4cc
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions komorebi-bar/src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -503,28 +504,45 @@ 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) {
fonts
.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 {
Expand Down

0 comments on commit 392e4cc

Please sign in to comment.