Skip to content

eframe configure CJK font to render CJK characters

Published: at 04:14 AM

Code Snippet

When using eframe to build GUI applications in Rust, you might encounter issues with rendering CJK (Chinese, Japanese, Korean) characters if the default font does not support them. To resolve this, you can add a CJK font to your application.

fn add_font(ctx: &egui::Context) {
    ctx.add_font(FontInsert::new(
        "source han serif",
        egui::FontData::from_static(include_bytes!("../assets/fonts/SourceHanSerifCN-VF.ttf")),
        vec![InsertFontFamily {
            family: egui::FontFamily::Proportional,
            priority: egui::epaint::text::FontPriority::Lowest,
        }],
    ));
}

Set to Lowest priority to ensure that the CJK font is used only when necessary, allowing the default font to handle other characters like ASCII.