Featured image of post RustのGUIライブラリ「egui」で日本語フォントを読み込み表示する方法

RustのGUIライブラリ「egui」で日本語フォントを読み込み表示する方法

Rust向けの軽量GUIライブラリ「egui」で日本語を正しく表示するための実装方法を解説します。Windowsのメイリオ(Meiryo)フォントを読み込み、アプリケーションに適用する具体的なコードサンプルを紹介します。

eguiのサンプルを取得

下記のコマンドで、eguiのサンプルを実行できます。

1
2
3
git clone https://github.com/emilk/eframe_template/ egui_test
cd egui_test
cargo run

日本語対応フォントを読み込む

日本語を表示するには日本語の対応フォントを読み込む必要があります。

src/app.rsの中でpub fn newメソッドの中に下記の行を追加します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// 日本語対応フォントを読み込む
let mut fonts = egui::FontDefinitions::default();
fonts.font_data.insert(
    "Meiryo".to_owned(),
    egui::FontData::from_static(include_bytes!("C:/Windows/Fonts/Meiryo.ttc")),
);
fonts
    .families
    .entry(egui::FontFamily::Proportional)
    .or_default()
    .insert(0, "Meiryo".to_owned());
cc.egui_ctx.set_fonts(fonts);

これで日本語を表示できるようになります。

img.png

comments powered by Disqus