Skip to content

Commit 0f2608c

Browse files
authored
Use CSS definition of line height (linebender#84)
Implements CSS style line height computation. This is now directly on top of main and linebender#76 builds on top of this.
1 parent 5ae3fb2 commit 0f2608c

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

parley/src/layout/line/greedy.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ impl<'a, B: Brush> BreakLines<'a, B> {
472472
// Default vertical alignment is to align the bottom of boxes with the text baseline.
473473
// This is equivalent to the entire height of the box being "ascent"
474474
line.metrics.ascent = line.metrics.ascent.max(item.height);
475+
line.metrics.line_height = line.metrics.line_height.max(item.height);
475476

476477
// Mark us as having seen non-whitespace content on this line
477478
have_metrics = true;
@@ -504,12 +505,10 @@ impl<'a, B: Brush> BreakLines<'a, B> {
504505
// Compute the run's vertical metrics
505506
let run = &self.layout.runs[line_item.index];
506507
let line_height = line_item.compute_line_height(self.layout);
507-
line.metrics.ascent =
508-
line.metrics.ascent.max(run.metrics.ascent * line_height);
509-
line.metrics.descent =
510-
line.metrics.descent.max(run.metrics.descent * line_height);
511-
line.metrics.leading =
512-
line.metrics.leading.max(run.metrics.leading * line_height);
508+
line.metrics.line_height = line.metrics.line_height.max(line_height);
509+
line.metrics.ascent = line.metrics.ascent.max(run.metrics.ascent);
510+
line.metrics.descent = line.metrics.descent.max(run.metrics.descent);
511+
line.metrics.leading = line.metrics.leading.max(run.metrics.leading);
513512

514513
// Mark us as having seen non-whitespace content on this line
515514
have_metrics = true;
@@ -559,7 +558,9 @@ impl<'a, B: Brush> BreakLines<'a, B> {
559558
// Round block/vertical axis metrics
560559
line.metrics.ascent = line.metrics.ascent.round();
561560
line.metrics.descent = line.metrics.descent.round();
562-
line.metrics.leading = (line.metrics.leading * 0.5).round() * 2.;
561+
line.metrics.line_height = line.metrics.line_height.round();
562+
line.metrics.leading =
563+
line.metrics.line_height - (line.metrics.ascent + line.metrics.descent);
563564

564565
// Compute
565566
let above = (line.metrics.ascent + line.metrics.leading * 0.5).round();

parley/src/layout/line/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ pub struct LineMetrics {
9090
pub descent: f32,
9191
/// Typographic leading.
9292
pub leading: f32,
93+
/// The absolute line height (in layout units).
94+
/// It matches the CSS definition of line height where it is derived as a multiple of the font size.
95+
pub line_height: f32,
9396
/// Offset to the baseline.
9497
pub baseline: f32,
9598
/// Offset for alignment.
@@ -101,9 +104,9 @@ pub struct LineMetrics {
101104
}
102105

103106
impl LineMetrics {
104-
/// Returns the size of the line (ascent + descent + leading).
107+
/// Returns the size of the line
105108
pub fn size(&self) -> f32 {
106-
self.ascent + self.descent + self.leading
109+
self.line_height
107110
}
108111
}
109112

parley/src/layout/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ pub struct Style<B: Brush> {
197197
pub underline: Option<Decoration<B>>,
198198
/// Strikethrough decoration.
199199
pub strikethrough: Option<Decoration<B>>,
200-
/// Multiplicative line height factor.
200+
/// Absolute line height in layout units (style line height * font size)
201201
pub(crate) line_height: f32,
202202
}
203203

parley/src/resolve/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ impl<B: Brush> ResolvedStyle<B> {
423423
brush: self.brush.clone(),
424424
underline: self.underline.as_layout_decoration(&self.brush),
425425
strikethrough: self.strikethrough.as_layout_decoration(&self.brush),
426-
line_height: self.line_height,
426+
line_height: self.line_height * self.font_size,
427427
}
428428
}
429429
}

0 commit comments

Comments
 (0)