Skip to content

Commit

Permalink
Remove pixels_per_point from FontDefinitions
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Dec 27, 2020
1 parent 847f67c commit 69ffa9b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 🐛

Expand Down
14 changes: 10 additions & 4 deletions egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion egui/src/introspection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
66 changes: 30 additions & 36 deletions egui/src/paint/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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<String, FontData> = BTreeMap::new();

Expand Down Expand Up @@ -175,17 +165,19 @@ 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,
}
}
}

/// 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<TextStyle, Font>,
atlas: Arc<Mutex<TextureAtlas>>,
Expand All @@ -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);
Expand All @@ -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)
});
Expand All @@ -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<Texture> {
let atlas = self.atlas.lock();
let mut buffered_texture = self.buffered_texture.lock();
Expand Down Expand Up @@ -290,7 +280,11 @@ struct FontImplCache {
}

impl FontImplCache {
pub fn new(atlas: Arc<Mutex<TextureAtlas>>, definitions: &super::FontDefinitions) -> Self {
pub fn new(
atlas: Arc<Mutex<TextureAtlas>>,
pixels_per_point: f32,
definitions: &super::FontDefinitions,
) -> Self {
let rusttype_fonts = definitions
.font_data
.iter()
Expand All @@ -304,7 +298,7 @@ impl FontImplCache {

Self {
atlas,
pixels_per_point: definitions.pixels_per_point,
pixels_per_point,
rusttype_fonts,
cache: Default::default(),
}
Expand Down
4 changes: 1 addition & 3 deletions egui/src/paint/galley.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions egui/src/paint/texture_atlas.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO: `TextureData` or similar?
/// An 8-bit texture containing font data.
#[derive(Clone, Default)]
pub struct Texture {
Expand Down

0 comments on commit 69ffa9b

Please sign in to comment.