| Crates.io | egui-chinese-font |
| lib.rs | egui-chinese-font |
| version | 0.1.0 |
| created_at | 2025-06-25 04:01:21.113122+00 |
| updated_at | 2025-06-25 04:01:21.113122+00 |
| description | Cross-platform Chinese font loading for egui applications |
| homepage | https://github.com/username/egui-chinese-font |
| repository | https://github.com/username/egui-chinese-font |
| max_upload_size | |
| id | 1725291 |
| size | 154,171 |
A cross-platform Rust crate for automatically loading Chinese fonts in egui applications.
| Platform | Fonts Detected |
|---|---|
| Windows | Microsoft YaHei, SimSun, SimHei, KaiTi, FangSong, Microsoft JhengHei |
| macOS | PingFang SC, STHeiti, Hiragino Sans GB, Arial Unicode MS |
| Linux | Noto Sans CJK, WQY MicroHei, Droid Sans Fallback, AR PL UMing |
Add this to your Cargo.toml:
[dependencies]
egui-chinese-font = "0.1"
egui = "0.27"
use egui_chinese_font::setup_chinese_fonts;
fn main() -> Result<(), eframe::Error> {
let options = eframe::NativeOptions::default();
eframe::run_native(
"Chinese Font Example",
options,
Box::new(|cc| {
// Setup Chinese fonts - this is all you need!
setup_chinese_fonts(&cc.egui_ctx).expect("Failed to load Chinese fonts");
Box::new(MyApp::default())
}),
)
}
#[derive(Default)]
struct MyApp;
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("你好世界!"); // This will now display correctly
ui.label("这是中文测试文本。");
ui.label("Traditional Chinese: 繁體中文測試");
});
}
}
If you want to use your own Chinese font file:
use egui_chinese_font::setup_custom_chinese_font;
// Load your custom font
let font_data = std::fs::read("path/to/your/chinese_font.ttf").unwrap();
setup_custom_chinese_font(&ctx, font_data, Some("my_chinese_font"));
use egui_chinese_font::{setup_chinese_fonts, FontError};
match setup_chinese_fonts(&ctx) {
Ok(()) => println!("Chinese fonts loaded successfully"),
Err(FontError::NotFound(msg)) => eprintln!("No Chinese fonts found: {}", msg),
Err(FontError::ReadError(err)) => eprintln!("Failed to read font file: {}", err),
Err(FontError::UnsupportedPlatform) => eprintln!("Platform not supported"),
}
setup_chinese_fonts(ctx: &egui::Context) -> Result<(), FontError> - Automatically detect and load system Chinese fontssetup_custom_chinese_font(ctx: &egui::Context, font_data: Vec<u8>, font_name: Option<&str>) - Load custom Chinese font dataget_chinese_font_paths() -> Vec<String> - Get list of potential Chinese font paths for debuggingFontError::NotFound(String) - No suitable Chinese fonts found on the systemFontError::ReadError(std::io::Error) - Error reading font fileFontError::UnsupportedPlatform - Current platform is not supportedSee the examples/ directory for complete working examples:
basic.rs - Simple Chinese text displaycargo run --example basicC:\Windows\Fonts\sudo apt install fonts-noto-cjk (Ubuntu/Debian)Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under either of
at your option.
egui-chinese-font 是一个专为 egui GUI 框架设计的中文字体加载库。它能够自动检测并加载系统中的中文字体,让你的 Rust GUI 应用程序完美显示中文文本。
| 操作系统 | 支持的字体 | 备注 |
|---|---|---|
| Windows | 微软雅黑、宋体、黑体、楷体、仿宋、微软正黑体 | 推荐使用微软雅黑 |
| macOS | 苹方简体、华文黑体、冬青黑体简体中文、Arial Unicode MS | 系统内置字体 |
| Linux | Noto Sans CJK、文泉驿微米黑、文泉驿正黑、文鼎PL明体 | 需要安装中文字体包 |
在你的 Cargo.toml 文件中添加:
[dependencies]
egui-chinese-font = "0.1"
egui = "0.27"
eframe = "0.27" # 如果你使用 eframe
use egui_chinese_font::setup_chinese_fonts;
fn main() -> Result<(), eframe::Error> {
let options = eframe::NativeOptions::default();
eframe::run_native(
"我的中文应用",
options,
Box::new(|cc| {
// 设置中文字体 - 这是关键的一行!
setup_chinese_fonts(&cc.egui_ctx)
.expect("无法加载中文字体");
Box::new(MyApp::default())
}),
)
}
#[derive(Default)]
struct MyApp;
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("你好,世界!");
ui.label("这是中文文本显示测试");
ui.label("支持简体中文:北京欢迎您");
ui.label("支持繁體中文:台北歡迎您");
ui.label("支持混合文本:Hello 世界 2025");
});
}
}
use egui_chinese_font::setup_custom_chinese_font;
// 从文件加载自定义字体
let font_data = std::fs::read("assets/my_chinese_font.ttf")?;
setup_custom_chinese_font(&ctx, font_data, Some("我的字体"));
// 或者从内嵌资源加载
let font_data = include_bytes!("../assets/chinese_font.ttf").to_vec();
setup_custom_chinese_font(&ctx, font_data, Some("内嵌字体"));
use egui_chinese_font::{setup_chinese_fonts, FontError};
match setup_chinese_fonts(&ctx) {
Ok(()) => {
println!("✅ 中文字体加载成功!");
},
Err(FontError::NotFound(msg)) => {
eprintln!("❌ 未找到中文字体: {}", msg);
// 可以尝试加载备用字体或显示警告
},
Err(FontError::ReadError(e)) => {
eprintln!("❌ 读取字体文件失败: {}", e);
},
Err(FontError::UnsupportedPlatform) => {
eprintln!("❌ 当前平台不支持自动字体检测");
}
}
use egui_chinese_font::get_chinese_font_paths;
// 检查系统中可用的中文字体路径
let font_paths = get_chinese_font_paths();
println!("可用的中文字体路径:");
for (i, path) in font_paths.iter().enumerate() {
println!(" {}. {}", i + 1, path);
// 检查文件是否存在
if std::path::Path::new(path).exists() {
println!(" ✅ 存在");
} else {
println!(" ❌ 不存在");
}
}
setup_chinese_fonts() 是否成功调用get_chinese_font_paths() 检查可用字体# Ubuntu/Debian
sudo apt install fonts-noto-cjk fonts-wqy-microhei
# CentOS/RHEL
sudo yum install google-noto-cjk-fonts wqy-microhei-fonts
# Arch Linux
sudo pacman -S noto-fonts-cjk wqy-microhei
setup_custom_chinese_font() 加载高质量字体文件本项目采用双重许可证:
你可以根据项目需要选择其中一种许可证。
欢迎为这个项目做出贡献!你可以:
Add this to your Cargo.toml:
[dependencies]
egui-chinese-font = "0.1.0"
egui = "0.27.0"
use egui_chinese_font::setup_chinese_fonts;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let options = eframe::NativeOptions::default();
eframe::run_native(
"我的中文应用",
options,
Box::new(|cc| {
// 设置中文字体
if let Err(e) = setup_chinese_fonts(&cc.egui_ctx) {
eprintln!("加载中文字体失败: {}", e);
}
Box::new(MyApp::default())
}),
)?;
Ok(())
}
struct MyApp;
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("你好,世界!");
ui.label("这是中文文本显示测试");
});
}
}
use egui_chinese_font::setup_custom_chinese_font;
// 加载自定义字体文件
let font_data = std::fs::read("path/to/your/chinese_font.ttf")?;
setup_custom_chinese_font(&ctx, font_data, Some("my_chinese_font"));
use egui_chinese_font::get_chinese_font_paths;
let font_paths = get_chinese_font_paths();
for path in font_paths {
println!("可能的字体路径: {}", path);
}
这个库为 egui Rust GUI 框架提供了完整的中文支持,让开发者能够轻松创建支持中文显示的现代化应用程序。
This library provides complete Chinese language support for the egui Rust GUI framework, enabling developers to easily create modern applications with Chinese text display.