Replies: 7 comments 15 replies
-
Automatically loading the system font (at least as a falback for non latin characters) would be a very nice feature of eframe. Feel free to work on it! |
Beta Was this translation helpful? Give feedback.
-
Load system font let font = std::fs::read("c:/Windows/Fonts/msyh.ttc").unwrap();
fonts.font_data.insert(
"my_font".to_owned(),
egui::FontData::from_owned(font)
); |
Beta Was this translation helpful? Give feedback.
-
Others have gotten Chinese to work, e.g. with https://github.com/emilk/egui/files/7739412/sarasa-mono-sc-nerd-regular.zip Are you adding it like shown in https://github.com/emilk/egui/blob/master/examples/custom_font/src/main.rs / https://github.com/emilk/egui/blob/0.17.0/eframe/examples/custom_font.rs ? |
Beta Was this translation helpful? Give feedback.
-
After reading about the source of druid,I found that piet-direct2d maybe would load the fonts from system according to the input characters dynamically. |
Beta Was this translation helpful? Give feedback.
-
Expect this function. |
Beta Was this translation helpful? Give feedback.
-
An example using use std::fs::read;
use eframe::{
egui::{Context, FontData, FontDefinitions},
epaint::FontFamily,
};
use font_kit::{
family_name::FamilyName, handle::Handle, properties::Properties, source::SystemSource,
};
fn load_system_font(ctx: &Context) {
let mut fonts = FontDefinitions::default();
let handle = SystemSource::new()
.select_best_match(&[FamilyName::SansSerif], &Properties::new())
.unwrap();
let buf: Vec<u8> = match handle {
Handle::Memory { bytes, .. } => bytes.to_vec(),
Handle::Path { path, .. } => read(path).unwrap(),
};
const FONT_SYSTEM_SANS_SERIF: &'static str = "System Sans Serif";
fonts
.font_data
.insert(FONT_SYSTEM_SANS_SERIF.to_owned(), FontData::from_owned(buf));
if let Some(vec) = fonts.families.get_mut(&FontFamily::Proportional) {
vec.push(FONT_SYSTEM_SANS_SERIF.to_owned());
}
if let Some(vec) = fonts.families.get_mut(&FontFamily::Monospace) {
vec.push(FONT_SYSTEM_SANS_SERIF.to_owned());
}
ctx.set_fonts(fonts);
} |
Beta Was this translation helpful? Give feedback.
-
If it helps anyone, this is what I came up with in oculante based on previous replies in order to have fallback for different languages (embedding them all made the binary too large). It is not pretty but it seems to work on Linux/Windows/Mac. /// Attempt to load a system font by any of the given `family_names`, returning the first match.
fn load_font_family(family_names: &[&str]) -> Option<Vec<u8>> {
let system_source = SystemSource::new();
for &name in family_names {
match system_source
.select_best_match(&[FamilyName::Title(name.to_string())], &Properties::new())
{
Ok(h) => match &h {
Handle::Memory { bytes, .. } => {
debug!("Loaded {name} from memory.");
return Some(bytes.to_vec());
}
Handle::Path { path, .. } => {
info!("Loaded {name} from path: {:?}", path);
if let Ok(data) = read(path) {
return Some(data);
}
}
},
Err(e) => error!("Could not load {}: {:?}", name, e),
}
}
None
}
pub fn load_system_fonts(mut fonts: FontDefinitions) -> FontDefinitions {
let mut fontdb = HashMap::new();
fontdb.insert(
"simplified_chinese",
vec![
"Heiti SC",
"Songti SC",
"Noto Sans CJK SC", // Good coverage for Simplified Chinese
"Noto Sans SC",
"WenQuanYi Zen Hei", // INcludes both Simplified and Traditional Chinese.
"SimSun",
"Noto Sans SC",
"PingFang SC",
"Source Han Sans CN",
],
);
fontdb.insert("korean", vec!["Source Han Sans KR"]);
fontdb.insert(
"arabic_fonts",
vec![
"Noto Sans Arabic",
"Amiri",
"Lateef",
"Al Tarikh",
"Segoe UI",
],
);
// Add more stuff here for better language support
for (region, font_names) in fontdb {
if let Some(font_data) = load_font_family(&font_names) {
info!("Inserting font {region}");
fonts
.font_data
.insert(region.to_owned(), FontData::from_owned(font_data));
fonts
.families
.get_mut(&FontFamily::Proportional)
.unwrap()
.push(region.to_owned());
}
}
fonts
} |
Beta Was this translation helpful? Give feedback.
-
I don't know why we must specify a font file to load while develop in egui?It's very annoyed to specify a font while i am a non-native english speaker.While we developing a gui program with gtk or qt,we never need to think what font we should use.
Maybe we could automatically find a local font file by get the system default language and the system default font storage position in Windows,Linux and Mac.If we can't find it,then we could use default font embed in egui.
If my suggestion is valuable,please let me know.I'd glad to make a PR.
Beta Was this translation helpful? Give feedback.
All reactions