/ eframe / examples / custom_font.rs
custom_font.rs
 1  #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
 2  
 3  use eframe::egui;
 4  
 5  fn main() {
 6      let options = eframe::NativeOptions::default();
 7      eframe::run_native(
 8          "egui example: custom font",
 9          options,
10          Box::new(|cc| Box::new(MyApp::new(cc))),
11      );
12  }
13  
14  fn setup_custom_fonts(ctx: &egui::Context) {
15      // Start with the default fonts (we will be adding to them rather than replacing them).
16      let mut fonts = egui::FontDefinitions::default();
17  
18      // Install my own font (maybe supporting non-latin characters).
19      // .ttf and .otf files supported.
20      fonts.font_data.insert(
21          "my_font".to_owned(),
22          egui::FontData::from_static(include_bytes!("../../epaint/fonts/Hack-Regular.ttf")),
23      );
24  
25      // Put my font first (highest priority) for proportional text:
26      fonts
27          .families
28          .entry(egui::FontFamily::Proportional)
29          .or_default()
30          .insert(0, "my_font".to_owned());
31  
32      // Put my font as last fallback for monospace:
33      fonts
34          .families
35          .entry(egui::FontFamily::Monospace)
36          .or_default()
37          .push("my_font".to_owned());
38  
39      // Tell egui to use these fonts:
40      ctx.set_fonts(fonts);
41  }
42  
43  struct MyApp {
44      text: String,
45  }
46  
47  impl MyApp {
48      fn new(cc: &eframe::CreationContext<'_>) -> Self {
49          setup_custom_fonts(&cc.egui_ctx);
50          Self {
51              text: "Edit this text field if you want".to_owned(),
52          }
53      }
54  }
55  
56  impl eframe::App for MyApp {
57      fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
58          egui::CentralPanel::default().show(ctx, |ui| {
59              ui.heading("egui using custom fonts");
60              ui.text_edit_multiline(&mut self.text);
61          });
62      }
63  }