From f051f96b73dad5e68500750d2e524acc5b476102 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 18 Sep 2024 15:59:57 +0200 Subject: [PATCH] Use `Font::ascent` for the logical rect, but `FontImpl::ascent` for centering --- crates/epaint/src/text/text_layout.rs | 16 ++++++++++------ crates/epaint/src/text/text_layout_types.rs | 9 ++++++--- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/crates/epaint/src/text/text_layout.rs b/crates/epaint/src/text/text_layout.rs index e8aea6108..0dbb40c34 100644 --- a/crates/epaint/src/text/text_layout.rs +++ b/crates/epaint/src/text/text_layout.rs @@ -172,7 +172,8 @@ fn layout_section( chr, pos: pos2(paragraph.cursor_x, f32::NAN), size: vec2(glyph_info.advance_width, line_height), - ascent: font.ascent(), + font_impl_ascent: font_impl.map_or(0.0, |f| f.ascent()), + font_ascent: font.ascent(), uv_rect: glyph_info.uv_rect, section_index, }); @@ -392,7 +393,8 @@ 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.ascent(), + font_impl_ascent: font_impl.map_or(0.0, |f| f.ascent()), + font_ascent: font.ascent(), uv_rect: replacement_glyph_info.uv_rect, section_index, }); @@ -404,13 +406,14 @@ fn replace_last_glyph_with_overflow_character( let x = 0.0; // TODO(emilk): heed paragraph leading_space 😬 - let (_, replacement_glyph_info) = font.font_impl_and_glyph_info(overflow_character); + let (font_impl, 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.ascent(), + font_impl_ascent: font_impl.map_or(0.0, |f| f.ascent()), + font_ascent: font.ascent(), uv_rect: replacement_glyph_info.uv_rect, section_index, }); @@ -596,8 +599,9 @@ fn galley_from_rows( for glyph in &mut row.glyphs { let format = &job.sections[glyph.section_index as usize].format; - glyph.pos.y = - cursor_y + glyph.ascent + format.valign.to_factor() * (row_height - glyph.size.y); + glyph.pos.y = cursor_y + + glyph.font_impl_ascent + + format.valign.to_factor() * (row_height - glyph.size.y); glyph.pos.y = point_scale.round_to_pixel(glyph.pos.y); } diff --git a/crates/epaint/src/text/text_layout_types.rs b/crates/epaint/src/text/text_layout_types.rs index 5872844f9..22361d10d 100644 --- a/crates/epaint/src/text/text_layout_types.rs +++ b/crates/epaint/src/text/text_layout_types.rs @@ -608,8 +608,11 @@ pub struct Glyph { /// Logical position: pos.y is the same for all chars of the same [`TextFormat`]. pub pos: Pos2, - /// `ascent` value from the font - pub ascent: f32, + /// `ascent` value from the `Font` + pub font_ascent: f32, + + /// `ascent` value from the `FontImpl` + pub font_impl_ascent: f32, /// Advance width and line height. /// @@ -631,7 +634,7 @@ impl Glyph { /// Same y range for all characters with the same [`TextFormat`]. #[inline] pub fn logical_rect(&self) -> Rect { - Rect::from_min_size(self.pos - vec2(0.0, self.ascent), self.size) + Rect::from_min_size(self.pos - vec2(0.0, self.font_ascent), self.size) } }