From 7329bb7654919fddd9808035cfd66f13ba34af05 Mon Sep 17 00:00:00 2001 From: Alex Butler Date: Wed, 15 Dec 2021 16:25:08 +0000 Subject: [PATCH] Use line_gap as top padding Fixes #143 --- layout/CHANGELOG.md | 3 +++ layout/src/lines.rs | 10 +++++++--- layout/src/words.rs | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/layout/CHANGELOG.md b/layout/CHANGELOG.md index 91f3b16..aef2eec 100644 --- a/layout/CHANGELOG.md +++ b/layout/CHANGELOG.md @@ -1,3 +1,6 @@ +# Unreleased +* Use font line_gap as top-padding for layouts to address issues with glyphs taller than the font ascent. + # 0.2.3 * Default layouts: Keep word trailing space width if ending in a hard break or end of all glyphs _e.g. `"Foo \n"`_ _(This particularly changes the layout of right & centre aligned text ending in spaces)_. diff --git a/layout/src/lines.rs b/layout/src/lines.rs index 6de6725..18d2936 100644 --- a/layout/src/lines.rs +++ b/layout/src/lines.rs @@ -20,7 +20,7 @@ impl Line { /// Returns line glyphs positioned on the screen and aligned. pub fn aligned_on_screen( mut self, - screen_position: (f32, f32), + (screen_x, screen_y): (f32, f32), h_align: HorizontalAlign, v_align: VerticalAlign, ) -> Vec { @@ -28,9 +28,13 @@ impl Line { return Vec::new(); } + // use line_gap as top-padding for all lines + // fixes issues with glyphs taller than the ascent + let screen_y = screen_y + self.max_v_metrics.line_gap; + // implement v-aligns when they're are supported let screen_left = match h_align { - HorizontalAlign::Left => point(screen_position.0, screen_position.1), + HorizontalAlign::Left => point(screen_x, screen_y), // - Right alignment attained from left by shifting the line // leftwards by the rightmost x distance from render position // - Central alignment is attained from left by shifting the line @@ -40,7 +44,7 @@ impl Line { if h_align == HorizontalAlign::Center { shift_left /= 2.0; } - point(screen_position.0 - shift_left, screen_position.1) + point(screen_x - shift_left, screen_y) } }; diff --git a/layout/src/words.rs b/layout/src/words.rs index ba461e1..e30ce51 100644 --- a/layout/src/words.rs +++ b/layout/src/words.rs @@ -17,7 +17,7 @@ pub(crate) struct VMetrics { impl VMetrics { #[inline] pub fn height(&self) -> f32 { - self.ascent - self.descent + self.ascent - self.descent + self.line_gap } #[inline]