修复中文显示问题 - 添加系统字体加载支持
This commit is contained in:
@@ -40,7 +40,10 @@ enum StatusMessage {
|
||||
}
|
||||
|
||||
impl PushScreenApp {
|
||||
fn new(_cc: &eframe::CreationContext<'_>) -> Self {
|
||||
fn new(cc: &eframe::CreationContext<'_>) -> Self {
|
||||
// 配置中文字体
|
||||
configure_fonts(&cc.egui_ctx);
|
||||
|
||||
let (tx, rx) = channel();
|
||||
|
||||
// 启动状态监控线程
|
||||
@@ -140,6 +143,53 @@ impl PushScreenApp {
|
||||
}
|
||||
}
|
||||
|
||||
/// 配置中文字体支持
|
||||
fn configure_fonts(ctx: &egui::Context) {
|
||||
let mut fonts = egui::FontDefinitions::default();
|
||||
|
||||
// 尝试从系统加载中文字体
|
||||
let font_paths = [
|
||||
r"C:\Windows\Fonts\msyh.ttc", // 微软雅黑
|
||||
r"C:\Windows\Fonts\msyhbd.ttc", // 微软雅黑粗体
|
||||
r"C:\Windows\Fonts\simsun.ttc", // 宋体
|
||||
r"C:\Windows\Fonts\simhei.ttf", // 黑体
|
||||
r"C:\Windows\Fonts\arial.ttf", // Arial (备用)
|
||||
];
|
||||
|
||||
let mut font_loaded = false;
|
||||
|
||||
for font_path in &font_paths {
|
||||
if let Ok(font_data) = std::fs::read(font_path) {
|
||||
let font_name = std::path::Path::new(font_path)
|
||||
.file_stem()
|
||||
.and_then(|s| s.to_str())
|
||||
.unwrap_or("custom_font");
|
||||
|
||||
fonts.font_data.insert(
|
||||
font_name.to_owned(),
|
||||
egui::FontData::from_owned(font_data),
|
||||
);
|
||||
|
||||
// 将字体添加到所有字体族
|
||||
fonts.families.get_mut(&egui::FontFamily::Proportional).unwrap()
|
||||
.insert(0, font_name.to_owned());
|
||||
fonts.families.get_mut(&egui::FontFamily::Monospace).unwrap()
|
||||
.push(font_name.to_owned());
|
||||
|
||||
font_loaded = true;
|
||||
info!("成功加载字体: {}", font_path);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if !font_loaded {
|
||||
// 如果系统字体加载失败,使用 egui 默认字体并尝试嵌入基本中文字体数据
|
||||
error!("无法加载系统中文字体,使用默认字体");
|
||||
}
|
||||
|
||||
ctx.set_fonts(fonts);
|
||||
}
|
||||
|
||||
impl eframe::App for PushScreenApp {
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
// 接收状态更新
|
||||
|
||||
Reference in New Issue
Block a user