1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-26 22:53:14 -04:00

Context::add_font (#5228)

make it easier to add fonts. 

For example if I want to add a custom FontFamily or if the user wants to
add a Chinese fallback
* [x] I have followed the instructions in the PR template

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
frederik-uni
2024-10-23 14:29:44 +02:00
committed by GitHub
parent d9a35a7289
commit 04ab5e7574
5 changed files with 138 additions and 5 deletions

View File

@@ -1,7 +1,10 @@
#![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;
use eframe::{
egui,
epaint::text::{FontInsert, InsertFontFamily},
};
fn main() -> eframe::Result {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
@@ -16,7 +19,28 @@ fn main() -> eframe::Result {
)
}
fn setup_custom_fonts(ctx: &egui::Context) {
// 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();
@@ -53,7 +77,8 @@ struct MyApp {
impl MyApp {
fn new(cc: &eframe::CreationContext<'_>) -> Self {
setup_custom_fonts(&cc.egui_ctx);
replace_fonts(&cc.egui_ctx);
add_font(&cc.egui_ctx);
Self {
text: "Edit this text field if you want".to_owned(),
}