From 76a686dfb6e7144a4c3eb1cbf9fb9c0f04569b1c Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 21 Sep 2022 18:37:41 +0200 Subject: [PATCH] Fix text sizes being too small Closes https://github.com/emilk/egui/issues/2068 Before this PR, the default font, Ubuntu-Light, was ~11% smaller than it should have been, and the default monospace font, Hack, was ~14% smaller. This means that setting the font size `12` in egui would yield smaller text than using that font size in any other app. Ooops! The change is that this PR now takes into account the ttf properties `units_per_em` and `height_unscaled`. If your egui application has specified you own font sizes or text styles you will see the text in your application grow larger, unless you go in and compensate by dividing all font sizes by ~1.21 for Ubuntu-Light/Proportional and ~1.16 for Hack/Monospace, and with something else if you are using a custom font! This effects any use of `FontId`, `RichText::size`, etc. This PR changes the default `Style::text_styles` to compensate, so the default egui style should look the same before and after this PR. --- crates/egui/src/introspection.rs | 2 +- crates/egui/src/style.rs | 29 ++++------ crates/egui_demo_lib/src/demo/font_book.rs | 2 +- crates/epaint/src/text/fonts.rs | 65 ++++++++++++++++------ 4 files changed, 59 insertions(+), 39 deletions(-) diff --git a/crates/egui/src/introspection.rs b/crates/egui/src/introspection.rs index c0003695db0..442fdc9fb47 100644 --- a/crates/egui/src/introspection.rs +++ b/crates/egui/src/introspection.rs @@ -14,7 +14,7 @@ pub fn font_family_ui(ui: &mut Ui, font_family: &mut FontFamily) { pub fn font_id_ui(ui: &mut Ui, font_id: &mut FontId) { let families = ui.fonts().families(); ui.horizontal(|ui| { - ui.add(Slider::new(&mut font_id.size, 4.0..=40.0).max_decimals(0)); + ui.add(Slider::new(&mut font_id.size, 4.0..=40.0).max_decimals(1)); for alternative in families { let text = alternative.to_string(); ui.radio_value(&mut font_id.family, alternative, text); diff --git a/crates/egui/src/style.rs b/crates/egui/src/style.rs index 6fb5727eb46..b83c0acbdab 100644 --- a/crates/egui/src/style.rs +++ b/crates/egui/src/style.rs @@ -602,25 +602,16 @@ pub struct DebugOptions { /// The default text styles of the default egui theme. pub fn default_text_styles() -> BTreeMap { - let mut text_styles = BTreeMap::new(); - text_styles.insert( - TextStyle::Small, - FontId::new(10.0, FontFamily::Proportional), - ); - text_styles.insert(TextStyle::Body, FontId::new(14.0, FontFamily::Proportional)); - text_styles.insert( - TextStyle::Button, - FontId::new(14.0, FontFamily::Proportional), - ); - text_styles.insert( - TextStyle::Heading, - FontId::new(20.0, FontFamily::Proportional), - ); - text_styles.insert( - TextStyle::Monospace, - FontId::new(14.0, FontFamily::Monospace), - ); - text_styles + use FontFamily::{Monospace, Proportional}; + + [ + (TextStyle::Small, FontId::new(9.0, Proportional)), + (TextStyle::Body, FontId::new(12.5, Proportional)), + (TextStyle::Button, FontId::new(12.5, Proportional)), + (TextStyle::Heading, FontId::new(18.0, Proportional)), + (TextStyle::Monospace, FontId::new(12.0, Monospace)), + ] + .into() } impl Default for Style { diff --git a/crates/egui_demo_lib/src/demo/font_book.rs b/crates/egui_demo_lib/src/demo/font_book.rs index 8d23b29460c..324a85a750b 100644 --- a/crates/egui_demo_lib/src/demo/font_book.rs +++ b/crates/egui_demo_lib/src/demo/font_book.rs @@ -10,7 +10,7 @@ impl Default for FontBook { fn default() -> Self { Self { filter: Default::default(), - font_id: egui::FontId::proportional(20.0), + font_id: egui::FontId::proportional(18.0), named_chars: Default::default(), } } diff --git a/crates/epaint/src/text/fonts.rs b/crates/epaint/src/text/fonts.rs index a95df05bdcb..c96b29e0080 100644 --- a/crates/epaint/src/text/fonts.rs +++ b/crates/epaint/src/text/fonts.rs @@ -1,5 +1,4 @@ -use std::collections::BTreeMap; -use std::sync::Arc; +use std::{collections::BTreeMap, sync::Arc}; use crate::{ mutex::{Mutex, MutexGuard}, @@ -271,7 +270,13 @@ impl Default for FontDefinitions { // Some good looking emojis. Use as first priority: font_data.insert( "NotoEmoji-Regular".to_owned(), - FontData::from_static(include_bytes!("../../fonts/NotoEmoji-Regular.ttf")), + FontData::from_static(include_bytes!("../../fonts/NotoEmoji-Regular.ttf")).tweak( + FontTweak { + scale: 0.8, // make it smaller + y_offset_factor: -0.2, // move it up + y_offset: 0.0, + }, + ), ); // Bigger emojis, and more. : @@ -279,7 +284,7 @@ impl Default for FontDefinitions { "emoji-icon-font".to_owned(), FontData::from_static(include_bytes!("../../fonts/emoji-icon-font.ttf")).tweak( FontTweak { - scale: 0.8, // make it smaller + scale: 0.9, // make it smaller y_offset_factor: 0.07, // move it down slightly y_offset: 0.0, }, @@ -526,6 +531,21 @@ impl FontsAndCache { // ---------------------------------------------------------------------------- +#[derive(Clone, Copy, Debug, PartialEq)] +struct HashableF32(f32); + +#[allow(clippy::derive_hash_xor_eq)] +impl std::hash::Hash for HashableF32 { + #[inline(always)] + fn hash(&self, state: &mut H) { + crate::f32_hash(state, self.0); + } +} + +impl Eq for HashableF32 {} + +// ---------------------------------------------------------------------------- + /// The collection of fonts used by `epaint`. /// /// Required in order to paint text. @@ -535,7 +555,7 @@ pub struct FontsImpl { definitions: FontDefinitions, atlas: Arc>, font_impl_cache: FontImplCache, - sized_family: ahash::HashMap<(u32, FontFamily), Font>, + sized_family: ahash::HashMap<(HashableF32, FontFamily), Font>, } impl FontsImpl { @@ -584,10 +604,9 @@ impl FontsImpl { /// Get the right font implementation from size and [`FontFamily`]. pub fn font(&mut self, font_id: &FontId) -> &mut Font { let FontId { size, family } = font_id; - let scale_in_pixels = self.font_impl_cache.scale_as_pixels(*size); self.sized_family - .entry((scale_in_pixels, family.clone())) + .entry((HashableF32(*size), family.clone())) .or_insert_with(|| { let fonts = &self.definitions.families.get(family); let fonts = fonts.unwrap_or_else(|| { @@ -596,7 +615,7 @@ impl FontsImpl { let fonts: Vec> = fonts .iter() - .map(|font_name| self.font_impl_cache.font_impl(scale_in_pixels, font_name)) + .map(|font_name| self.font_impl_cache.font_impl(*size, font_name)) .collect(); Font::new(fonts) @@ -699,23 +718,33 @@ impl FontImplCache { } } - #[inline] - pub fn scale_as_pixels(&self, scale_in_points: f32) -> u32 { - let scale_in_pixels = self.pixels_per_point * scale_in_points; - - // Round to an even number of physical pixels to get even kerning. - // See https://github.com/emilk/egui/issues/382 - scale_in_pixels.round() as u32 - } + pub fn font_impl(&mut self, scale_in_points: f32, font_name: &str) -> Arc { + use ab_glyph::Font as _; - pub fn font_impl(&mut self, scale_in_pixels: u32, font_name: &str) -> Arc { let (tweak, ab_glyph_font) = self .ab_glyph_fonts .get(font_name) .unwrap_or_else(|| panic!("No font data found for {:?}", font_name)) .clone(); - let scale_in_pixels = (scale_in_pixels as f32 * tweak.scale).round() as u32; + let scale_in_pixels = self.pixels_per_point * scale_in_points; + + // Scale the font properly (see https://github.com/emilk/egui/issues/2068). + let units_per_em = ab_glyph_font.units_per_em().unwrap_or_else(|| { + panic!( + "The font unit size of {:?} exceeds the expected range (16..=16384)", + font_name + ) + }); + let font_scaling = ab_glyph_font.height_unscaled() / units_per_em; + let scale_in_pixels = scale_in_pixels * font_scaling; + + // Tweak the scale as the user desired: + let scale_in_pixels = scale_in_pixels * tweak.scale; + + // Round to an even number of physical pixels to get even kerning. + // See https://github.com/emilk/egui/issues/382 + let scale_in_pixels = scale_in_pixels.round() as u32; let y_offset_points = { let scale_in_points = scale_in_pixels as f32 / self.pixels_per_point;