From f779e8a346742c5f6152dfda02a935a8f56b6e33 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 29 Dec 2021 21:43:37 +0100 Subject: [PATCH] Add an eframe example of how to install a custom font --- eframe/examples/custom_font.rs | 65 ++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 eframe/examples/custom_font.rs diff --git a/eframe/examples/custom_font.rs b/eframe/examples/custom_font.rs new file mode 100644 index 00000000..199521da --- /dev/null +++ b/eframe/examples/custom_font.rs @@ -0,0 +1,65 @@ +use eframe::{egui, epi}; + +struct MyApp { + text: String, +} + +impl Default for MyApp { + fn default() -> Self { + Self { + text: "Edit this text field if you want".to_owned(), + } + } +} + +impl epi::App for MyApp { + fn name(&self) -> &str { + "egui example: custom font" + } + + fn setup( + &mut self, + ctx: &egui::CtxRef, + _frame: &epi::Frame, + _storage: Option<&dyn epi::Storage>, + ) { + // 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(), + egui::FontData::from_static(include_bytes!("../../epaint/fonts/Hack-Regular.ttf")), + ); + + // Put my font first (highest priority) for proportional text: + fonts + .fonts_for_family + .entry(egui::FontFamily::Proportional) + .or_default() + .insert(0, "my_font".to_owned()); + + // Put my font as last fallback for monospace: + fonts + .fonts_for_family + .entry(egui::FontFamily::Monospace) + .or_default() + .push("my_font".to_owned()); + + // Tell egui to use these fonts: + ctx.set_fonts(fonts); + } + + fn update(&mut self, ctx: &egui::CtxRef, _frame: &epi::Frame) { + egui::CentralPanel::default().show(ctx, |ui| { + ui.heading("egui using custom fonts"); + ui.text_edit_multiline(&mut self.text); + }); + } +} + +fn main() { + let options = eframe::NativeOptions::default(); + eframe::run_native(Box::new(MyApp::default()), options); +}