Skip to content

Truly independent web browser

LibGfx+LibWeb: Include the font's line gap in normal line height

Andreas Kling992934526285
diff --git a/Base/res/fonts/SerenitySans-Regular.ttf b/Base/res/fonts/SerenitySans-Regular.ttf
index e49c45ec2a7..ca4a8383404 100644
Binary files a/Base/res/fonts/SerenitySans-Regular.ttf and b/Base/res/fonts/SerenitySans-Regular.ttf differ
diff --git a/Libraries/LibGfx/Font/FaceDescription.cpp b/Libraries/LibGfx/Font/FaceDescription.cpp
index 6f029605d80..c83f0bfaf3a 100644
--- a/Libraries/LibGfx/Font/FaceDescription.cpp
+++ b/Libraries/LibGfx/Font/FaceDescription.cpp
@@ -98,7 +98,8 @@ static FaceStyle read_declared_style(hb_face_t* face)
return style;
}
-// The ascender, the descender and the x-height at the default coordinates. A font adds the deltas of its variations.
+// The ascender, the descender, the line gap and the x-height at the default coordinates. A font adds the deltas of its
+// variations.
static FaceVerticalMetrics read_vertical_metrics(hb_face_t* face)
{
// A new font has the scale of the face, so positions are in font units.
@@ -111,6 +112,8 @@ static FaceVerticalMetrics read_vertical_metrics(hb_face_t* face)
metrics.ascender = position;
hb_ot_metrics_get_position_with_fallback(font, HB_OT_METRICS_TAG_HORIZONTAL_DESCENDER, &position);
metrics.descender = position;
+ if (hb_ot_metrics_get_position(font, HB_OT_METRICS_TAG_HORIZONTAL_LINE_GAP, &position))
+ metrics.line_gap = position;
// https://learn.microsoft.com/en-us/typography/opentype/spec/os2
// NB: Some fonts leave the ascender and descender of their hhea table at 0, and give them only in their OS/2 table.
@@ -120,9 +123,11 @@ static FaceVerticalMetrics read_vertical_metrics(hb_face_t* face)
if (os2.contains(68, 4) && (os2.i16_at(68) != 0 || os2.i16_at(70) != 0)) {
metrics.ascender = os2.i16_at(68);
metrics.descender = os2.i16_at(70);
+ metrics.line_gap = os2.contains(72, 2) ? os2.i16_at(72) : 0;
} else if (os2.contains(74, 4)) {
metrics.ascender = os2.u16_at(74);
metrics.descender = -static_cast<i32>(os2.u16_at(76));
+ metrics.line_gap = 0;
}
}
if (hb_ot_metrics_get_position(font, HB_OT_METRICS_TAG_X_HEIGHT, &position))
diff --git a/Libraries/LibGfx/Font/FaceDescription.h b/Libraries/LibGfx/Font/FaceDescription.h
index b492901bd14..42426429c71 100644
--- a/Libraries/LibGfx/Font/FaceDescription.h
+++ b/Libraries/LibGfx/Font/FaceDescription.h
@@ -28,6 +28,7 @@ struct FaceStyle {
struct FaceVerticalMetrics {
i32 ascender { 0 };
i32 descender { 0 };
+ i32 line_gap { 0 };
// The sxHeight of the OS/2 table, or 0 if the table does not give it.
i32 x_height { 0 };
};
diff --git a/Libraries/LibGfx/Font/Font.cpp b/Libraries/LibGfx/Font/Font.cpp
index 439b7f62f43..cebe575c843 100644
--- a/Libraries/LibGfx/Font/Font.cpp
+++ b/Libraries/LibGfx/Font/Font.cpp
@@ -70,6 +70,7 @@ FontPixelMetrics Font::compute_pixel_metrics() const
FontPixelMetrics metrics;
metrics.ascent = font_units_to_pixels(vertical_metrics.ascender + hb_ot_metrics_get_variation(m_harfbuzz_font, HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER));
metrics.descent = -font_units_to_pixels(vertical_metrics.descender + hb_ot_metrics_get_variation(m_harfbuzz_font, HB_OT_METRICS_TAG_HORIZONTAL_DESCENDER));
+ metrics.line_gap = max(0.0f, font_units_to_pixels(vertical_metrics.line_gap + hb_ot_metrics_get_variation(m_harfbuzz_font, HB_OT_METRICS_TAG_HORIZONTAL_LINE_GAP)));
// https://drafts.csswg.org/css-values-4/#ex
// In the cases where it is impossible or impractical to determine the x-height, a value of 0.5em must be assumed.
@@ -276,6 +277,7 @@ extern "C" void ladybird_gfx_font_snapshot(void const* font, Gfx::FFI::FfiFontSn
.id = typed_font.id(),
.ascent = metrics.ascent,
.descent = metrics.descent,
+ .line_gap = metrics.line_gap,
.x_height = metrics.x_height,
.zero_advance = metrics.advance_of_ascii_zero,
.pixel_size = typed_font.pixel_size(),
diff --git a/Libraries/LibGfx/Font/Font.h b/Libraries/LibGfx/Font/Font.h
index 3c301b2eb74..4a50bb89d8c 100644
--- a/Libraries/LibGfx/Font/Font.h
+++ b/Libraries/LibGfx/Font/Font.h
@@ -35,6 +35,9 @@ struct FontPixelMetrics {
// Number of pixels the font descends below the baseline.
float descent { 0 };
+
+ // Number of pixels the font recommends as extra spacing between lines.
+ float line_gap { 0 };
};
// https://learn.microsoft.com/en-us/typography/opentype/spec/os2#uswidthclass
diff --git a/Libraries/LibGfx/Rust/src/font.rs b/Libraries/LibGfx/Rust/src/font.rs
index 1604c458605..f1eba12709c 100644
--- a/Libraries/LibGfx/Rust/src/font.rs
+++ b/Libraries/LibGfx/Rust/src/font.rs
@@ -45,6 +45,7 @@ pub struct FfiFontSnapshot {
pub id: u64,
pub ascent: f32,
pub descent: f32,
+ pub line_gap: f32,
pub x_height: f32,
pub zero_advance: f32,
pub pixel_size: f32,
@@ -58,6 +59,7 @@ pub struct FontId(pub u64);
pub struct FontFacts {
pub ascent: f32,
pub descent: f32,
+ pub line_gap: f32,
pub x_height: f32,
pub zero_advance: f32,
pub pixel_size: f32,
@@ -140,6 +142,7 @@ impl FontHandle {
facts: FontFacts {
ascent: snapshot.ascent,
descent: snapshot.descent,
+ line_gap: snapshot.line_gap,
x_height: snapshot.x_height,
zero_advance: snapshot.zero_advance,
pixel_size: snapshot.pixel_size,
diff --git a/Libraries/LibWeb/CSS/ComputedStyleWorkingSet.cpp b/Libraries/LibWeb/CSS/ComputedStyleWorkingSet.cpp
index c763f2d5cd3..5d0081736bf 100644
--- a/Libraries/LibWeb/CSS/ComputedStyleWorkingSet.cpp
+++ b/Libraries/LibWeb/CSS/ComputedStyleWorkingSet.cpp
@@ -334,7 +334,7 @@ PreferredColorScheme ComputedStyleWorkingSet::color_scheme(PreferredColorScheme
CSSPixels normal_line_height(Gfx::FontPixelMetrics const& font_metrics)
{
- return CSSPixels { round_to<i32>(font_metrics.ascent) + round_to<i32>(font_metrics.descent) };
+ return CSSPixels { round_to<i32>(font_metrics.ascent) + round_to<i32>(font_metrics.descent) + round_to<i32>(font_metrics.line_gap) };
}
CSSPixels ComputedStyleWorkingSet::line_height(FontComputer const& font_computer) const
diff --git a/Libraries/LibWeb/CSS/FontResolution.cpp b/Libraries/LibWeb/CSS/FontResolution.cpp
index 99ca6253f3a..5c3cb9e4b7f 100644
--- a/Libraries/LibWeb/CSS/FontResolution.cpp
+++ b/Libraries/LibWeb/CSS/FontResolution.cpp
@@ -797,6 +797,7 @@ static StyleEngineFFI::FfiResolvedFont resolve_font_for_style_engine(void const*
.font_cascade_list = reinterpret_cast<StyleEngineFFI::FfiHostHandle>(font_list.ptr()),
.ascent = metrics.ascent,
.descent = metrics.descent,
+ .line_gap = metrics.line_gap,
.x_height = metrics.x_height,
.zero_advance = metrics.advance_of_ascii_zero,
.font_size_adjust_from_font = font_size_adjust_from_font,
diff --git a/Libraries/LibWeb/Rust/src/css/computed_value_types.rs b/Libraries/LibWeb/Rust/src/css/computed_value_types.rs
index b0cf2871309..6a076bc02a2 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_value_types.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_value_types.rs
@@ -740,7 +740,9 @@ impl FontValues {
if is_normal && let Some(font) = self.first_available_font() {
let facts = font.facts();
return crate::css::css_pixels::CssPixels::from_integer(
- i64::from(facts.ascent.round() as i32) + i64::from(facts.descent.round() as i32),
+ i64::from(facts.ascent.round_ties_even() as i32)
+ + i64::from(facts.descent.round_ties_even() as i32)
+ + i64::from(facts.line_gap.round_ties_even() as i32),
);
}
self.resolved_line_height_used
diff --git a/Libraries/LibWeb/Rust/src/css/style/bridge.rs b/Libraries/LibWeb/Rust/src/css/style/bridge.rs
index 50606d3d119..c181815c0e8 100644
--- a/Libraries/LibWeb/Rust/src/css/style/bridge.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/bridge.rs
@@ -445,6 +445,7 @@ pub struct FfiResolvedFont {
pub font_cascade_list: FfiHostHandle,
pub ascent: f32,
pub descent: f32,
+ pub line_gap: f32,
pub x_height: f32,
pub zero_advance: f32,
/// For a request naming `from-font`, the number it computes to: the request's metric of the
diff --git a/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs b/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs
index ed944806ab2..ce6cdabe0db 100644
--- a/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs
@@ -1445,7 +1445,11 @@ const _: () = {
/// The line height `normal` uses for a resolved font.
pub(super) fn normal_line_height(resolved: &bridge::FfiResolvedFont) -> f64 {
- f64::from(resolved.ascent.round() as i32 + resolved.descent.round() as i32)
+ f64::from(
+ resolved.ascent.round_ties_even() as i32
+ + resolved.descent.round_ties_even() as i32
+ + resolved.line_gap.round_ties_even() as i32,
+ )
}
/// The used line height, as the C++ working set reads it from the computed value in `values`
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/test_support.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/test_support.rs
index 8790294572c..659ad602dcd 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/test_support.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/test_support.rs
@@ -32,6 +32,7 @@ unsafe extern "C" fn resolve_font(
font_cascade_list: FfiHostHandle::from_pointer(std::ptr::dangling()),
ascent: 12.0,
descent: 4.0,
+ line_gap: 0.0,
x_height: 8.0,
zero_advance: 8.0,
font_size_adjust_from_font: 0.0,
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index d43c381c3eb..33097cb173e 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -3699,7 +3699,7 @@ impl<'pass> BlockFormattingContext<'pass> {
let marker_facts = self.facts(marker);
if !marker_facts.list_marker_is_inside() {
let marker_style = self.style(marker);
- let estimated_block_size = line_builder::normal_line_height(marker_style);
+ let estimated_block_size = line_builder::content_area_block_size(marker_style);
let marker_block_offset =
Self::marker_centered_block_offset(marker_style.line_height(), estimated_block_size);
let list_item_block_size = self.metrics_of(sized_child).content_block_size();
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
index 72358ea59e0..bf9ab4f28c7 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
@@ -324,7 +324,7 @@ fn apply_block_ellipsis(
ellipsis_inline_offset,
CssPixels::default(),
ellipsis_inline_size,
- line_builder::normal_line_height(style),
+ line_builder::content_area_block_size(style),
CssPixels::default(),
line.direction,
line.writing_mode,
@@ -766,7 +766,7 @@ pub(crate) fn compute(
(block_start, line.max_direct_fragment_block_length)
} else {
let style = context.style(node);
- let height = line_builder::normal_line_height(style);
+ let height = line_builder::content_area_block_size(style);
let baseline = line_builder::LineBuilder::baseline_for_style(style, height);
(inline_box_baseline - baseline, height)
};
@@ -878,7 +878,7 @@ pub(crate) fn compute(
placeholder_rect.y = block_offset;
placeholder_rect.height = CssPixels::default();
} else {
- placeholder_rect.height = line_builder::normal_line_height(style);
+ placeholder_rect.height = line_builder::content_area_block_size(style);
placeholder_rect.y = line.block_start + line.baseline
- line_builder::LineBuilder::baseline_for_style(style, placeholder_rect.height);
}
diff --git a/Libraries/LibWeb/Rust/src/layout/line_builder.rs b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
index 224bf995f49..880af04c78c 100644
--- a/Libraries/LibWeb/Rust/src/layout/line_builder.rs
+++ b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
@@ -1551,7 +1551,7 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
|| self.facts(snapshot.layout_node).is_text_node()
|| self.facts(snapshot.layout_node).is_fragmented_inline();
if is_text_node {
- let font_box_size = normal_line_height(style);
+ let font_box_size = content_area_block_size(style);
let font_baseline = Self::baseline_for_style(style, font_box_size);
let fragment_mut = &mut self.line_mut(line_index).fragments[fragment_index];
fragment_mut.record.block_offset += fragment_mut.baseline - font_baseline;
@@ -1779,7 +1779,8 @@ impl TextLayoutBounds {
// When its computed line-height is normal, the layout bounds of an inline box encloses all its glyphs, going
// from the highest A to the deepest D. (Note that glyphs in a single box can come from different fonts and
// thus might not all have the same A and D.)
- // NB: The bounds above already enclose the glyphs of the first available font.
+ // NB: The bounds above already enclose the glyphs of the first available font. Each other font brings in the
+ // leading of its own normal line height, which includes its line gap.
if let Some(font) = font
&& style.line_height_is_normal()
&& style
@@ -1787,8 +1788,13 @@ impl TextLayoutBounds {
.first_available_font()
.is_none_or(|first_available| font.as_raw() != first_available.as_raw())
{
- bounds.ascent = bounds.ascent.max(font_metric_in_whole_pixels(font.facts().ascent));
- bounds.descent = bounds.descent.max(font_metric_in_whole_pixels(font.facts().descent));
+ let facts = font.facts();
+ let normal_line_height = font_metric_in_whole_pixels(facts.ascent)
+ + font_metric_in_whole_pixels(facts.descent)
+ + font_metric_in_whole_pixels(facts.line_gap);
+ let font_bounds = Self::with_leading(facts.ascent, facts.descent, normal_line_height);
+ bounds.ascent = bounds.ascent.max(font_bounds.ascent);
+ bounds.descent = bounds.descent.max(font_bounds.descent);
}
bounds
}
@@ -1802,6 +1808,8 @@ pub(crate) fn font_metric_in_whole_pixels(metric: f32) -> CssPixels {
CssPixels::from_integer(metric.round_ties_even() as i64)
}
-pub(crate) fn normal_line_height(style: StyleValues) -> CssPixels {
+/// The block size of the content area of an inline box: its font's ascent plus descent. Unlike a normal line-height,
+/// it leaves out the font's line gap.
+pub(crate) fn content_area_block_size(style: StyleValues) -> CssPixels {
font_metric_in_whole_pixels(style.font_ascent()) + font_metric_in_whole_pixels(style.font_descent())
}
diff --git a/Tests/LibWeb/Layout/expected/first-available-font-can-lack-space-glyph.txt b/Tests/LibWeb/Layout/expected/first-available-font-can-lack-space-glyph.txt
index 05344fc5e70..938ccab989a 100644
--- a/Tests/LibWeb/Layout/expected/first-available-font-can-lack-space-glyph.txt
+++ b/Tests/LibWeb/Layout/expected/first-available-font-can-lack-space-glyph.txt
@@ -1,18 +1,18 @@
Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline
- BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 74 0+0+0] [BFC] children: not-inline
- BlockContainer <body> at [0,0] [0+0+0 800 0+0+0] [0+0+0 74 0+0+0] children: not-inline
- BlockContainer <div#test> at [0,0] [0+0+0 800 0+0+0] [0+0+0 20 0+0+0] children: inline
- frag 0 from TextNode start: 0, length: 1, rect: [0,0 16x20] baseline: 16
+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 76 0+0+0] [BFC] children: not-inline
+ BlockContainer <body> at [0,0] [0+0+0 800 0+0+0] [0+0+0 76 0+0+0] children: not-inline
+ BlockContainer <div#test> at [0,0] [0+0+0 800 0+0+0] [0+0+0 22 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [0,1 16x20] baseline: 16
"A"
TextNode <#text> (not painted)
- BlockContainer <div#fallback> at [0,20] [0+0+0 800 0+0+0] [0+0+0 27 0+0+0] children: inline
- frag 0 from TextNode start: 0, length: 1, rect: [0,20 17.828125x27] baseline: 21
+ BlockContainer <div#fallback> at [0,22] [0+0+0 800 0+0+0] [0+0+0 27 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [0,22 17.828125x27] baseline: 21
"A"
TextNode <#text> (not painted)
- BlockContainer <div#subset> at [0,47] [0+0+0 800 0+0+0] [0+0+0 27 0+0+0] children: inline
- frag 0 from TextNode start: 0, length: 1, rect: [0,47 16x27] baseline: 21
+ BlockContainer <div#subset> at [0,49] [0+0+0 800 0+0+0] [0+0+0 27 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [0,49 16x27] baseline: 21
"A"
TextNode <#text> (not painted)
SC for Viewport<#document> [0,0 800x600] (z-index: auto)
- SC for BlockContainer<HTML> [0,0 800x74] (z-index: auto)
+ SC for BlockContainer<HTML> [0,0 800x76] (z-index: auto)
diff --git a/Tests/LibWeb/Ref/expected/selection-highlight-rtl-partial-ref.html b/Tests/LibWeb/Ref/expected/selection-highlight-rtl-partial-ref.html
index 1b5afba7a06..e5a4fce6137 100644
--- a/Tests/LibWeb/Ref/expected/selection-highlight-rtl-partial-ref.html
+++ b/Tests/LibWeb/Ref/expected/selection-highlight-rtl-partial-ref.html
@@ -3,7 +3,7 @@
@font-face { font-family: TestHebrew; src: url(wpt-import/fonts/mplus-1p-regular.woff); }
body { margin: 0; font: 40px TestHebrew; }
p { margin: 10px; }
-#highlight { position: absolute; left: 724.13px; top: 10px; width: 43.31px; height: 56px; background-color: red; z-index: -1; }
+#highlight { position: absolute; left: 724.13px; top: 12px; width: 43.31px; height: 56px; background-color: red; z-index: -1; }
</style>
<div id="highlight"></div>
<p dir="rtl"><span>אבגדה</span></p>
diff --git a/Tests/LibWeb/Text/expected/css/line-height-normal-includes-line-gap.txt b/Tests/LibWeb/Text/expected/css/line-height-normal-includes-line-gap.txt
index c1db1fc988e..110b0ec5cff 100644
--- a/Tests/LibWeb/Text/expected/css/line-height-normal-includes-line-gap.txt
+++ b/Tests/LibWeb/Text/expected/css/line-height-normal-includes-line-gap.txt
@@ -1,3 +1,3 @@
-line box height: 100
+line box height: 109
inline box height: 100
-line box height at 768px: 768
+line box height at 768px: 838
diff --git a/Tests/LibWeb/Text/expected/css/style-memory/bytes-per-element-by-owner.txt b/Tests/LibWeb/Text/expected/css/style-memory/bytes-per-element-by-owner.txt
index 79fefd221af..1e523d1729d 100644
--- a/Tests/LibWeb/Text/expected/css/style-memory/bytes-per-element-by-owner.txt
+++ b/Tests/LibWeb/Text/expected/css/style-memory/bytes-per-element-by-owner.txt
@@ -105,7 +105,7 @@ uniqueFont: 500 elements, 500 styled
otherMaps: within 0
styleValues: within 28
animationEffects: within 0
- fontResolutions: within 244
+ fontResolutions: within 250
cssAnimations: within 0
hostMemo: within 0
cppInstalledStyles: within 8
diff --git a/Tests/LibWeb/Text/input/css/style-memory/bytes-per-element-by-owner.html b/Tests/LibWeb/Text/input/css/style-memory/bytes-per-element-by-owner.html
index 716c82a1c62..99650f5d4fc 100644
--- a/Tests/LibWeb/Text/input/css/style-memory/bytes-per-element-by-owner.html
+++ b/Tests/LibWeb/Text/input/css/style-memory/bytes-per-element-by-owner.html
@@ -123,7 +123,7 @@
styleValues: 28,
// NB: A resolution per distinct font request, which another engine keys a font fallback list by a ~168 B
// font description for: ours holds the request once, in a list that doubles its capacity.
- fontResolutions: 244,
+ fontResolutions: 250,
cppInstalledStyles: 8,
};