diff --git a/CHANGELOG.md b/CHANGELOG.md index 707a2585..d81d746d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Changed 🔧 * Renamed `FontFamily::VariableWidth` to `FontFamily::Proportional`. +* Remove `pixels_per_point` from `FontDefinitions`. ### Fixed 🐛 diff --git a/egui/src/context.rs b/egui/src/context.rs index 87d8e44f..29f65aad 100644 --- a/egui/src/context.rs +++ b/egui/src/context.rs @@ -543,14 +543,20 @@ impl Context { self.input = std::mem::take(&mut self.input).begin_frame(new_raw_input); self.frame_state.lock().begin_frame(&self.input); - let mut font_definitions = self.options.lock().font_definitions.clone(); - font_definitions.pixels_per_point = self.input.pixels_per_point(); + let font_definitions = self.options.lock().font_definitions.clone(); + let pixels_per_point = self.input.pixels_per_point(); let same_as_current = match &self.fonts { None => false, - Some(fonts) => *fonts.definitions() == font_definitions, + Some(fonts) => { + *fonts.definitions() == font_definitions + && (fonts.pixels_per_point() - pixels_per_point).abs() < 1e-3 + } }; if !same_as_current { - self.fonts = Some(Arc::new(Fonts::from_definitions(font_definitions))); + self.fonts = Some(Arc::new(Fonts::from_definitions( + pixels_per_point, + font_definitions, + ))); } // Ensure we register the background area so panels and background ui can catch clicks: diff --git a/egui/src/introspection.rs b/egui/src/introspection.rs index d35ecf0c..c5b96be7 100644 --- a/egui/src/introspection.rs +++ b/egui/src/introspection.rs @@ -62,7 +62,7 @@ impl paint::FontDefinitions { ); } if ui.button("Reset fonts").clicked { - *self = paint::FontDefinitions::default_with_pixels_per_point(self.pixels_per_point); + *self = Default::default(); } } } diff --git a/egui/src/paint/fonts.rs b/egui/src/paint/fonts.rs index dc2a77ea..5da5ba96 100644 --- a/egui/src/paint/fonts.rs +++ b/egui/src/paint/fonts.rs @@ -81,9 +81,6 @@ fn rusttype_font_from_font_data(name: &str, data: &FontData) -> rusttype::Font<' /// ``` #[derive(Clone, Debug, PartialEq)] pub struct FontDefinitions { - /// The dpi scale factor. Needed to get pixel perfect fonts. - pub pixels_per_point: f32, // TODO: remove from here - /// List of font names and their definitions. /// The definition must be the contents of either a `.ttf` or `.otf` font file. /// @@ -105,13 +102,6 @@ pub struct FontDefinitions { impl Default for FontDefinitions { fn default() -> Self { - Self::default_with_pixels_per_point(f32::NAN) // must be set later - } -} - -impl FontDefinitions { - /// Default values for the fonts - pub fn default_with_pixels_per_point(pixels_per_point: f32) -> Self { #[allow(unused)] let mut font_data: BTreeMap = BTreeMap::new(); @@ -175,7 +165,6 @@ impl FontDefinitions { family_and_size.insert(TextStyle::Monospace, (FontFamily::Monospace, 13.0)); // 13 for `ProggyClean` Self { - pixels_per_point, font_data, fonts_for_family, family_and_size, @@ -183,9 +172,12 @@ impl FontDefinitions { } } -/// Note: the `default()` fonts are invalid (missing `pixels_per_point`). +/// The collection of fonts used by Egui. +/// +/// Note: `Fonts::default()` is invalid (missing `pixels_per_point`). #[derive(Default)] pub struct Fonts { + pixels_per_point: f32, definitions: FontDefinitions, fonts: BTreeMap, atlas: Arc>, @@ -195,22 +187,7 @@ pub struct Fonts { } impl Fonts { - pub fn from_definitions(definitions: FontDefinitions) -> Fonts { - let mut fonts = Self::default(); - fonts.set_definitions(definitions); - fonts - } - - pub fn definitions(&self) -> &FontDefinitions { - &self.definitions - } - - pub fn set_definitions(&mut self, definitions: FontDefinitions) { - if self.definitions == definitions { - return; - } - self.definitions = definitions; - + pub fn from_definitions(pixels_per_point: f32, definitions: FontDefinitions) -> Self { // We want an atlas big enough to be able to include all the Emojis in the `TextStyle::Heading`, // so we can show the Emoji picker demo window. let mut atlas = TextureAtlas::new(2048, 64); @@ -224,14 +201,13 @@ impl Fonts { let atlas = Arc::new(Mutex::new(atlas)); - let mut font_impl_cache = FontImplCache::new(atlas.clone(), &self.definitions); + let mut font_impl_cache = FontImplCache::new(atlas.clone(), pixels_per_point, &definitions); - self.fonts = self - .definitions + let fonts = definitions .family_and_size .iter() .map(|(&text_style, &(family, scale_in_points))| { - let fonts = &self.definitions.fonts_for_family.get(&family); + let fonts = &definitions.fonts_for_family.get(&family); let fonts = fonts.unwrap_or_else(|| { panic!("FontFamily::{:?} is not bound to any fonts", family) }); @@ -254,10 +230,24 @@ impl Fonts { texture.version = hasher.finish(); } - self.buffered_texture = Default::default(); //atlas.lock().texture().clone(); - self.atlas = atlas; + Self { + pixels_per_point, + definitions, + fonts, + atlas, + buffered_texture: Default::default(), //atlas.lock().texture().clone(); + } } + pub fn pixels_per_point(&self) -> f32 { + self.pixels_per_point + } + + pub fn definitions(&self) -> &FontDefinitions { + &self.definitions + } + + /// Call each frame to get the latest available font texture data. pub fn texture(&self) -> Arc { let atlas = self.atlas.lock(); let mut buffered_texture = self.buffered_texture.lock(); @@ -290,7 +280,11 @@ struct FontImplCache { } impl FontImplCache { - pub fn new(atlas: Arc>, definitions: &super::FontDefinitions) -> Self { + pub fn new( + atlas: Arc>, + pixels_per_point: f32, + definitions: &super::FontDefinitions, + ) -> Self { let rusttype_fonts = definitions .font_data .iter() @@ -304,7 +298,7 @@ impl FontImplCache { Self { atlas, - pixels_per_point: definitions.pixels_per_point, + pixels_per_point, rusttype_fonts, cache: Default::default(), } diff --git a/egui/src/paint/galley.rs b/egui/src/paint/galley.rs index f66afa70..8dbdbec9 100644 --- a/egui/src/paint/galley.rs +++ b/egui/src/paint/galley.rs @@ -657,9 +657,7 @@ fn test_text_layout() { use crate::paint::*; let pixels_per_point = 1.0; - let fonts = Fonts::from_definitions(FontDefinitions::default_with_pixels_per_point( - pixels_per_point, - )); + let fonts = Fonts::from_definitions(pixels_per_point, FontDefinitions::default()); let font = &fonts[TextStyle::Monospace]; let galley = font.layout_multiline("".to_owned(), 1024.0); diff --git a/egui/src/paint/texture_atlas.rs b/egui/src/paint/texture_atlas.rs index cabd86f4..2b06db6f 100644 --- a/egui/src/paint/texture_atlas.rs +++ b/egui/src/paint/texture_atlas.rs @@ -1,3 +1,4 @@ +// TODO: `TextureData` or similar? /// An 8-bit texture containing font data. #[derive(Clone, Default)] pub struct Texture {