Skip to content

Commit

Permalink
Use CSS compatible line-height computation.
Browse files Browse the repository at this point in the history
Let line-height be line_height * font_size with no regard for font metrics.
Font metrics are still used to determine the baseline with the line box.
  • Loading branch information
nicoburns committed Jul 20, 2024
1 parent 1a303a8 commit faa7ddf
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion parley/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ fn build_into_layout<B: Brush>(
brush: s.brush.clone(),
underline: conv_deco(&s.underline, &s.brush),
strikethrough: conv_deco(&s.strikethrough, &s.brush),
line_height: s.line_height,
line_height: s.line_height * s.font_size,
}
}));

Expand Down
10 changes: 6 additions & 4 deletions parley/src/layout/line/greedy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ impl<'a, B: Brush> BreakLines<'a, B> {
// Default vertical alignment is to align the bottom of boxes with the text baseline.
// This is equivalent to the entire height of the box being "ascent"
line.metrics.ascent = line.metrics.ascent.max(item.height);
line.metrics.line_height = line.metrics.line_height.max(item.height);

// Mark us as having seen non-whitespace content on this line
have_metrics = true;
Expand Down Expand Up @@ -504,12 +505,13 @@ impl<'a, B: Brush> BreakLines<'a, B> {
// Compute the run's vertical metrics
let run = &self.layout.runs[line_item.index];
let line_height = line_item.compute_line_height(self.layout);
line.metrics.line_height = line.metrics.line_height.max(line_height);
line.metrics.ascent =
line.metrics.ascent.max(run.metrics.ascent * line_height);
line.metrics.ascent.max(run.metrics.ascent);
line.metrics.descent =
line.metrics.descent.max(run.metrics.descent * line_height);
line.metrics.descent.max(run.metrics.descent);
line.metrics.leading =
line.metrics.leading.max(run.metrics.leading * line_height);
line.metrics.leading.max(run.metrics.leading);

// Mark us as having seen non-whitespace content on this line
have_metrics = true;
Expand Down Expand Up @@ -559,7 +561,7 @@ impl<'a, B: Brush> BreakLines<'a, B> {
// Round block/vertical axis metrics
line.metrics.ascent = line.metrics.ascent.round();
line.metrics.descent = line.metrics.descent.round();
line.metrics.leading = (line.metrics.leading * 0.5).round() * 2.;
line.metrics.leading = line.metrics.line_height - (line.metrics.ascent + line.metrics.descent);

// Compute
let above = (line.metrics.ascent + line.metrics.leading * 0.5).round();
Expand Down
6 changes: 4 additions & 2 deletions parley/src/layout/line/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ pub struct LineMetrics {
pub descent: f32,
/// Typographic leading.
pub leading: f32,
/// CSS definition of line height (px value or multiplicative value * font size)
pub line_height: f32,
/// Offset to the baseline.
pub baseline: f32,
/// Offset for alignment.
Expand All @@ -101,9 +103,9 @@ pub struct LineMetrics {
}

impl LineMetrics {
/// Returns the size of the line (ascent + descent + leading).
/// Returns the size of the line
pub fn size(&self) -> f32 {
self.ascent + self.descent + self.leading
self.line_height
}
}

Expand Down
2 changes: 1 addition & 1 deletion parley/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ pub struct Style<B: Brush> {
pub underline: Option<Decoration<B>>,
/// Strikethrough decoration.
pub strikethrough: Option<Decoration<B>>,
/// Multiplicative line height factor.
/// Baked line height (multiplicative line height multiplied by font size)
pub(crate) line_height: f32,
}

Expand Down

0 comments on commit faa7ddf

Please sign in to comment.