96 lines
2.8 KiB
Rust
96 lines
2.8 KiB
Rust
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
|
|
#![allow(rustdoc::missing_crate_level_docs)] // it's an example
|
|
|
|
use eframe::{
|
|
egui,
|
|
epaint::text::{FontInsert, InsertFontFamily},
|
|
};
|
|
|
|
fn main() -> eframe::Result {
|
|
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
|
|
let options = eframe::NativeOptions {
|
|
viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]),
|
|
..Default::default()
|
|
};
|
|
eframe::run_native(
|
|
"egui example: custom font",
|
|
options,
|
|
Box::new(|cc| Ok(Box::new(MyApp::new(cc)))),
|
|
)
|
|
}
|
|
|
|
// Demonstrates how to add a font to the existing ones
|
|
fn add_font(ctx: &egui::Context) {
|
|
ctx.add_font(FontInsert::new(
|
|
"my_font",
|
|
egui::FontData::from_static(include_bytes!(
|
|
"../../../crates/epaint_default_fonts/fonts/Hack-Regular.ttf"
|
|
)),
|
|
vec![
|
|
InsertFontFamily {
|
|
family: egui::FontFamily::Proportional,
|
|
priority: egui::epaint::text::FontPriority::Highest,
|
|
},
|
|
InsertFontFamily {
|
|
family: egui::FontFamily::Monospace,
|
|
priority: egui::epaint::text::FontPriority::Lowest,
|
|
},
|
|
],
|
|
));
|
|
}
|
|
|
|
// Demonstrates how to replace all fonts.
|
|
fn replace_fonts(ctx: &egui::Context) {
|
|
// Start with the default fonts (we will be adding to them rather than replacing them).
|
|
let mut fonts = egui::FontDefinitions::default();
|
|
|
|
// Install my own font (maybe supporting non-latin characters).
|
|
// .ttf and .otf files supported.
|
|
fonts.font_data.insert(
|
|
"my_font".to_owned(),
|
|
std::sync::Arc::new(egui::FontData::from_static(include_bytes!(
|
|
"../../../crates/epaint_default_fonts/fonts/Hack-Regular.ttf"
|
|
))),
|
|
);
|
|
|
|
// Put my font first (highest priority) for proportional text:
|
|
fonts
|
|
.families
|
|
.entry(egui::FontFamily::Proportional)
|
|
.or_default()
|
|
.insert(0, "my_font".to_owned());
|
|
|
|
// Put my font as last fallback for monospace:
|
|
fonts
|
|
.families
|
|
.entry(egui::FontFamily::Monospace)
|
|
.or_default()
|
|
.push("my_font".to_owned());
|
|
|
|
// Tell egui to use these fonts:
|
|
ctx.set_fonts(fonts);
|
|
}
|
|
|
|
struct MyApp {
|
|
text: String,
|
|
}
|
|
|
|
impl MyApp {
|
|
fn new(cc: &eframe::CreationContext<'_>) -> Self {
|
|
replace_fonts(&cc.egui_ctx);
|
|
add_font(&cc.egui_ctx);
|
|
Self {
|
|
text: "Edit this text field if you want".to_owned(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl eframe::App for MyApp {
|
|
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
|
egui::CentralPanel::default().show(ctx, |ui| {
|
|
ui.heading("egui using custom fonts");
|
|
ui.text_edit_multiline(&mut self.text);
|
|
});
|
|
}
|
|
}
|