Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CSS definition of line height #84

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 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,10 @@ 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.ascent =
line.metrics.ascent.max(run.metrics.ascent * line_height);
line.metrics.descent =
line.metrics.descent.max(run.metrics.descent * line_height);
line.metrics.leading =
line.metrics.leading.max(run.metrics.leading * line_height);
line.metrics.line_height = line.metrics.line_height.max(line_height);
line.metrics.ascent = line.metrics.ascent.max(run.metrics.ascent);
line.metrics.descent = line.metrics.descent.max(run.metrics.descent);
line.metrics.leading = 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 +558,9 @@ 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.line_height = line.metrics.line_height.round();
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
7 changes: 5 additions & 2 deletions parley/src/layout/line/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ pub struct LineMetrics {
pub descent: f32,
/// Typographic leading.
pub leading: f32,
/// The absolute line height (in layout units).
/// It matches the CSS definition of line height where it is derived as a multiple of the font size.
pub line_height: f32,
/// Offset to the baseline.
pub baseline: f32,
/// Offset for alignment.
Expand All @@ -101,9 +104,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.
/// Absolute line height in layout units (style line height * font size)
pub(crate) line_height: f32,
}

Expand Down
2 changes: 1 addition & 1 deletion parley/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ impl<B: Brush> ResolvedStyle<B> {
brush: self.brush.clone(),
underline: self.underline.as_layout_decoration(&self.brush),
strikethrough: self.strikethrough.as_layout_decoration(&self.brush),
line_height: self.line_height,
line_height: self.line_height * self.font_size,
}
}
}
Expand Down