From 9947d7c97637c0f108dfbd7cf441a7398d985d4d Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 18 Sep 2024 13:55:26 +0200 Subject: [PATCH 1/2] Use the largest ascent as the ascent for a font --- crates/epaint/src/text/font.rs | 12 ++++++++++++ crates/epaint/src/text/text_layout.rs | 8 ++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/crates/epaint/src/text/font.rs b/crates/epaint/src/text/font.rs index 8fc6e9efc6d..3b601847b3a 100644 --- a/crates/epaint/src/text/font.rs +++ b/crates/epaint/src/text/font.rs @@ -468,6 +468,18 @@ impl Font { (Some(font_impl), glyph_info) } + pub(crate) fn ascent(&self) -> f32 { + if self.fonts.is_empty() { + self.row_height + } else { + let mut max_ascent = 0.0; + for font in &self.fonts { + max_ascent = f32::max(max_ascent, font.ascent()); + } + max_ascent + } + } + fn glyph_info_no_cache_or_fallback(&mut self, c: char) -> Option<(FontIndex, GlyphInfo)> { for (font_index, font_impl) in self.fonts.iter().enumerate() { if let Some(glyph_info) = font_impl.glyph_info(c) { diff --git a/crates/epaint/src/text/text_layout.rs b/crates/epaint/src/text/text_layout.rs index 96dd067582d..41b4630c72a 100644 --- a/crates/epaint/src/text/text_layout.rs +++ b/crates/epaint/src/text/text_layout.rs @@ -172,7 +172,7 @@ fn layout_section( chr, pos: pos2(paragraph.cursor_x, f32::NAN), size: vec2(glyph_info.advance_width, line_height), - ascent: font_impl.map_or(0.0, |font| font.ascent()), // Failure to find the font here would be weird + ascent: font.ascent(), uv_rect: glyph_info.uv_rect, section_index, }); @@ -392,7 +392,7 @@ fn replace_last_glyph_with_overflow_character( chr: overflow_character, pos: pos2(x, f32::NAN), size: vec2(replacement_glyph_info.advance_width, line_height), - ascent: font_impl.map_or(0.0, |font| font.ascent()), // Failure to find the font here would be weird + ascent: font.ascent(), uv_rect: replacement_glyph_info.uv_rect, section_index, }); @@ -404,13 +404,13 @@ fn replace_last_glyph_with_overflow_character( let x = 0.0; // TODO(emilk): heed paragraph leading_space 😬 - let (font_impl, replacement_glyph_info) = font.font_impl_and_glyph_info(overflow_character); + let (_, replacement_glyph_info) = font.font_impl_and_glyph_info(overflow_character); row.glyphs.push(Glyph { chr: overflow_character, pos: pos2(x, f32::NAN), size: vec2(replacement_glyph_info.advance_width, line_height), - ascent: font_impl.map_or(0.0, |font| font.ascent()), // Failure to find the font here would be weird + ascent: font.ascent(), uv_rect: replacement_glyph_info.uv_rect, section_index, }); From 5edaf400ad0da90fcff5158a84de4eb55913e491 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 18 Sep 2024 13:55:52 +0200 Subject: [PATCH 2/2] Change default `FontTweak` --- crates/epaint/src/text/fonts.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/crates/epaint/src/text/fonts.rs b/crates/epaint/src/text/fonts.rs index dcfbbd7fe1d..42d74cdd2d2 100644 --- a/crates/epaint/src/text/fonts.rs +++ b/crates/epaint/src/text/fonts.rs @@ -189,7 +189,7 @@ impl Default for FontTweak { scale: 1.0, y_offset_factor: 0.0, y_offset: 0.0, - baseline_offset_factor: -0.0333, // makes the default fonts look more centered in buttons and such + baseline_offset_factor: 0.0, } } } @@ -289,11 +289,8 @@ impl Default for FontDefinitions { font_data.insert( "emoji-icon-font".to_owned(), FontData::from_static(EMOJI_ICON).tweak(FontTweak { - scale: 0.88, // make it smaller - - // probably not correct, but this does make texts look better (#2724 for details) - y_offset_factor: 0.11, // move glyphs down to better align with common fonts - baseline_offset_factor: -0.11, // ...now the entire row is a bit down so shift it back + scale: 0.88, // Make it smaller + y_offset_factor: 0.04, // Move down slightly ..Default::default() }), );