Skip to content

Commit

Permalink
Use line_gap as top padding
Browse files Browse the repository at this point in the history
Fixes #143
  • Loading branch information
alexheretic committed Dec 15, 2021
1 parent d24cd4d commit f864b87
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
3 changes: 3 additions & 0 deletions layout/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)_.

Expand Down
3 changes: 2 additions & 1 deletion layout/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,11 +723,12 @@ mod layout_test {
],
);

let sfont = A_FONT.as_scaled(40.0);
for g in glyphs {
println!("{:?}", (g.glyph.scale, g.glyph.position));
// all glyphs should have the same ascent drawing position
let y_pos = g.glyph.position.y;
assert_relative_eq!(y_pos, A_FONT.as_scaled(40.0).ascent());
assert_relative_eq!(y_pos, sfont.ascent() + sfont.line_gap());
}
}

Expand Down
10 changes: 7 additions & 3 deletions layout/src/lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,21 @@ 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<SectionGlyph> {
if self.glyphs.is_empty() {
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
Expand All @@ -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)
}
};

Expand Down
2 changes: 1 addition & 1 deletion layout/src/words.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit f864b87

Please sign in to comment.