You can override the default Egui fonts
Closes https://github.com/emilk/egui/issues/32
This commit is contained in:
parent
82d55cb67e
commit
ca96172552
7 changed files with 35 additions and 21 deletions
|
@ -8,6 +8,7 @@
|
||||||
* Demo App: Add slider to scale all of Egui
|
* Demo App: Add slider to scale all of Egui
|
||||||
* Windows are now constrained to the screen
|
* Windows are now constrained to the screen
|
||||||
* Panels: you can now create panels using `SidePanel` and `TopPanel`.
|
* Panels: you can now create panels using `SidePanel` and `TopPanel`.
|
||||||
|
* You can override the default Egui fonts
|
||||||
* Fix a bug where some regions would slowly grow for non-integral scales (`pixels_per_point`).
|
* Fix a bug where some regions would slowly grow for non-integral scales (`pixels_per_point`).
|
||||||
* You can no longer throw windows
|
* You can no longer throw windows
|
||||||
* `Context::begin_frame()` no longer returns anything.
|
* `Context::begin_frame()` no longer returns anything.
|
||||||
|
|
|
@ -12,6 +12,11 @@ use crate::Context;
|
||||||
/// Implement this trait to write apps that can be compiled both natively using the [`egui_glium`](https://crates.io/crates/egui_glium) crate,
|
/// Implement this trait to write apps that can be compiled both natively using the [`egui_glium`](https://crates.io/crates/egui_glium) crate,
|
||||||
/// and deployed as a web site using the [`egui_web`](https://crates.io/crates/egui_web) crate.
|
/// and deployed as a web site using the [`egui_web`](https://crates.io/crates/egui_web) crate.
|
||||||
pub trait App {
|
pub trait App {
|
||||||
|
/// Called once before the first frame.
|
||||||
|
/// Allows you to do setup code and to call `ctx.set_fonts()`.
|
||||||
|
/// Optional.
|
||||||
|
fn setup(&mut self, _ctx: &std::sync::Arc<Context>) {}
|
||||||
|
|
||||||
/// Called each time the UI needs repainting, which may be many times per second.
|
/// Called each time the UI needs repainting, which may be many times per second.
|
||||||
/// Put your widgets into a `SidePanel`, `TopPanel`, `CentralPanel`, `Window` or `Area`.
|
/// Put your widgets into a `SidePanel`, `TopPanel`, `CentralPanel`, `Window` or `Area`.
|
||||||
fn ui(
|
fn ui(
|
||||||
|
|
|
@ -92,7 +92,10 @@ pub use {
|
||||||
layout::*,
|
layout::*,
|
||||||
math::*,
|
math::*,
|
||||||
memory::Memory,
|
memory::Memory,
|
||||||
paint::{color, PaintCmd, PaintJobs, Rgba, Srgba, Stroke, TextStyle, Texture, TextureId},
|
paint::{
|
||||||
|
color, FontDefinitions, FontFamily, PaintCmd, PaintJobs, Rgba, Srgba, Stroke, TextStyle,
|
||||||
|
Texture, TextureId,
|
||||||
|
},
|
||||||
painter::Painter,
|
painter::Painter,
|
||||||
style::Style,
|
style::Style,
|
||||||
types::*,
|
types::*,
|
||||||
|
|
|
@ -36,6 +36,11 @@ pub struct FontDefinitions {
|
||||||
pub pixels_per_point: f32,
|
pub pixels_per_point: f32,
|
||||||
|
|
||||||
pub fonts: BTreeMap<TextStyle, (FontFamily, f32)>,
|
pub fonts: BTreeMap<TextStyle, (FontFamily, f32)>,
|
||||||
|
|
||||||
|
/// The TTF data for each font family.
|
||||||
|
/// Egui has built-in-default for these,
|
||||||
|
/// but you can override them if you like.
|
||||||
|
pub ttf_data: BTreeMap<FontFamily, &'static [u8]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for FontDefinitions {
|
impl Default for FontDefinitions {
|
||||||
|
@ -51,11 +56,20 @@ impl FontDefinitions {
|
||||||
fonts.insert(TextStyle::Body, (FontFamily::VariableWidth, 14.0));
|
fonts.insert(TextStyle::Body, (FontFamily::VariableWidth, 14.0));
|
||||||
fonts.insert(TextStyle::Button, (FontFamily::VariableWidth, 16.0));
|
fonts.insert(TextStyle::Button, (FontFamily::VariableWidth, 16.0));
|
||||||
fonts.insert(TextStyle::Heading, (FontFamily::VariableWidth, 24.0));
|
fonts.insert(TextStyle::Heading, (FontFamily::VariableWidth, 24.0));
|
||||||
fonts.insert(TextStyle::Monospace, (FontFamily::Monospace, 13.0));
|
fonts.insert(TextStyle::Monospace, (FontFamily::Monospace, 13.0)); // 13 for `ProggyClean`
|
||||||
|
|
||||||
|
// TODO: figure out a way to make the WASM smaller despite including a font. Zip it?
|
||||||
|
let monospace_typeface_data = include_bytes!("../../fonts/ProggyClean.ttf"); // Use 13 for this. NOTHING ELSE.
|
||||||
|
let variable_typeface_data = include_bytes!("../../fonts/Comfortaa-Regular.ttf"); // Funny, hard to read
|
||||||
|
|
||||||
|
let mut ttf_data: BTreeMap<FontFamily, &'static [u8]> = BTreeMap::new();
|
||||||
|
ttf_data.insert(FontFamily::Monospace, monospace_typeface_data);
|
||||||
|
ttf_data.insert(FontFamily::VariableWidth, variable_typeface_data);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
pixels_per_point,
|
pixels_per_point,
|
||||||
fonts,
|
fonts,
|
||||||
|
ttf_data,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -98,32 +112,21 @@ impl Fonts {
|
||||||
|
|
||||||
let atlas = Arc::new(Mutex::new(atlas));
|
let atlas = Arc::new(Mutex::new(atlas));
|
||||||
|
|
||||||
// TODO: figure out a way to make the WASM smaller despite including a font. Zip it?
|
|
||||||
let monospace_typeface_data = include_bytes!("../../fonts/ProggyClean.ttf"); // Use 13 for this. NOTHING ELSE.
|
|
||||||
|
|
||||||
// let monospace_typeface_data = include_bytes!("../../fonts/Roboto-Regular.ttf");
|
|
||||||
|
|
||||||
let variable_typeface_data = include_bytes!("../../fonts/Comfortaa-Regular.ttf"); // Funny, hard to read
|
|
||||||
|
|
||||||
// let variable_typeface_data = include_bytes!("../../fonts/DejaVuSans.ttf"); // Basic, boring, takes up more space
|
|
||||||
|
|
||||||
self.definitions = definitions.clone();
|
self.definitions = definitions.clone();
|
||||||
let FontDefinitions {
|
let FontDefinitions {
|
||||||
pixels_per_point,
|
pixels_per_point,
|
||||||
fonts,
|
fonts,
|
||||||
|
ttf_data,
|
||||||
} = definitions;
|
} = definitions;
|
||||||
self.fonts = fonts
|
self.fonts = fonts
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(text_style, (family, size))| {
|
.map(|(text_style, (family, size))| {
|
||||||
let typeface_data: &[u8] = match family {
|
let typeface_data = ttf_data
|
||||||
FontFamily::Monospace => monospace_typeface_data,
|
.get(&family)
|
||||||
FontFamily::VariableWidth => variable_typeface_data,
|
.unwrap_or_else(|| panic!("Missing TTF data for {:?}", family));
|
||||||
};
|
let font = Font::new(atlas.clone(), typeface_data, size, pixels_per_point);
|
||||||
|
|
||||||
(
|
(text_style, font)
|
||||||
text_style,
|
|
||||||
Font::new(atlas.clone(), typeface_data, size, pixels_per_point),
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ mod texture_atlas;
|
||||||
pub use {
|
pub use {
|
||||||
color::{Rgba, Srgba},
|
color::{Rgba, Srgba},
|
||||||
command::{PaintCmd, Stroke},
|
command::{PaintCmd, Stroke},
|
||||||
fonts::{FontDefinitions, Fonts, TextStyle},
|
fonts::{FontDefinitions, FontFamily, Fonts, TextStyle},
|
||||||
stats::PaintStats,
|
stats::PaintStats,
|
||||||
tessellator::{PaintJob, PaintJobs, PaintOptions, TextureId, Triangles, Vertex, WHITE_UV},
|
tessellator::{PaintJob, PaintJobs, PaintOptions, TextureId, Triangles, Vertex, WHITE_UV},
|
||||||
texture_atlas::Texture,
|
texture_atlas::Texture,
|
||||||
|
|
|
@ -57,6 +57,7 @@ pub fn run(
|
||||||
|
|
||||||
let mut ctx = egui::Context::new();
|
let mut ctx = egui::Context::new();
|
||||||
*ctx.memory() = egui::app::get_value(storage.as_ref(), EGUI_MEMORY_KEY).unwrap_or_default();
|
*ctx.memory() = egui::app::get_value(storage.as_ref(), EGUI_MEMORY_KEY).unwrap_or_default();
|
||||||
|
app.setup(&ctx);
|
||||||
|
|
||||||
let mut raw_input = egui::RawInput {
|
let mut raw_input = egui::RawInput {
|
||||||
pixels_per_point: Some(native_pixels_per_point(&display)),
|
pixels_per_point: Some(native_pixels_per_point(&display)),
|
||||||
|
|
|
@ -132,7 +132,8 @@ pub struct AppRunner {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppRunner {
|
impl AppRunner {
|
||||||
pub fn new(web_backend: WebBackend, app: Box<dyn App>) -> Result<Self, JsValue> {
|
pub fn new(web_backend: WebBackend, mut app: Box<dyn App>) -> Result<Self, JsValue> {
|
||||||
|
app.setup(&web_backend.ctx);
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
pixels_per_point: native_pixels_per_point(),
|
pixels_per_point: native_pixels_per_point(),
|
||||||
web_backend,
|
web_backend,
|
||||||
|
|
Loading…
Reference in a new issue