Skip to content

Truly independent web browser

LibWeb: Count vertical-align keywords in a line box’s baseline

sideshowbarker1532359d76f4
diff --git a/Libraries/LibWeb/Rust/src/layout/line_builder.rs b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
index 215803aaad4..bab1de801de 100644
--- a/Libraries/LibWeb/Rust/src/layout/line_builder.rs
+++ b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
@@ -33,8 +33,6 @@ struct FragmentAlignmentSnapshot {
style_source: Node,
layout_node: Node,
baseline: CssPixels,
- block_size: CssPixels,
- border_box_block_start: CssPixels,
is_atomic_inline: bool,
inline_offset: CssPixels,
block_offset: CssPixels,
@@ -1080,6 +1078,76 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
TextLayoutBounds::new(style, fragment.glyphs.as_ref().map(|glyphs| &glyphs.font))
}
+ /// How far the box that `metrics` describe reaches above the baseline of the line box, where `style` aligns it
+ /// within the box of `parent_style`.
+ fn extent_above_line_box_baseline(
+ &self,
+ style: StyleValues,
+ parent_style: StyleValues,
+ metrics: VerticalAlignMetrics,
+ ) -> CssPixels {
+ let block_start = self.block_offset_for_alignment(style, parent_style, metrics, CssPixels::default())
+ - metrics.effective_box_block_start_offset;
+ self.current_block_offset - block_start
+ }
+
+ /// How far its own vertical-align shifts the baseline of `box_`, an inline box, down from where 'baseline' puts it.
+ fn vertical_shift_of_inline_box(&self, box_: Node) -> CssPixels {
+ let style = self.style(box_);
+ if style.vertical_align_is_keyword() && style.vertical_align_keyword() == vertical_align::BASELINE {
+ return CssPixels::default();
+ }
+ let metrics = self.inline_box_alignment_metrics(box_);
+ let parent_style = self.parent_style(box_);
+ let baseline_style = style.with_vertical_align_keyword(vertical_align::BASELINE);
+ self.block_offset_for_alignment(style, parent_style, metrics, CssPixels::default())
+ - self.block_offset_for_alignment(baseline_style, parent_style, metrics, CssPixels::default())
+ }
+
+ /// The inline boxes of this formatting context around `style_source`, innermost first, up to the first one aligned
+ /// to the line box, each with the shift of its baseline by its own vertical-align.
+ fn vertical_shifts_of_enclosing_inline_boxes(&self, style_source: Node) -> Vec<(Node, CssPixels)> {
+ let containing_block = self.context().containing_block;
+ let mut shifts = Vec::new();
+ if style_source == containing_block {
+ return shifts;
+ }
+ let mut ancestor = self.context().parent_node(style_source);
+ while !ancestor.is_invalid() && ancestor != containing_block && self.facts(ancestor).is_fragmented_inline() {
+ if line_relative_alignment(self.style(ancestor)).is_some() {
+ break;
+ }
+ shifts.push((ancestor, self.vertical_shift_of_inline_box(ancestor)));
+ ancestor = self.context().parent_node(ancestor);
+ }
+ shifts
+ }
+
+ fn fragment_alignment_metrics(
+ &self,
+ line_index: usize,
+ fragment_index: usize,
+ style: StyleValues,
+ ) -> VerticalAlignMetrics {
+ let line = self.line(line_index);
+ let fragment = &line.fragments[fragment_index];
+ let mut metrics = VerticalAlignMetrics {
+ baseline: fragment.baseline,
+ block_size: fragment.physical_vertical_extent(),
+ effective_box_block_start_offset: fragment.border_box_block_start,
+ // Intentional old quirk: start is reused as end.
+ effective_box_block_end_offset: fragment.border_box_block_start,
+ line_height: style.line_height(),
+ };
+ if fragment.is_atomic_inline {
+ let atomic_inline = fragment.box_cells();
+ metrics.effective_box_block_start_offset = atomic_inline.margin_top + atomic_inline.border_box_top(false);
+ metrics.effective_box_block_end_offset =
+ atomic_inline.margin_bottom + atomic_inline.border_box_bottom(false);
+ }
+ metrics
+ }
+
fn inline_box_alignment_metrics(&self, node: Node) -> VerticalAlignMetrics {
let style = self.style(node);
let sides = self.context().sides_of_entered_inline_box(node);
@@ -1133,26 +1201,30 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
continue;
}
let alignment_style = self.alignment_style(style_source, style);
- // NB: The box reaches as far above the baseline of the line box as it is shifted up from it (see
- // block_offset_for_alignment).
let shifted_by_keyword = alignment_style.vertical_align_is_keyword()
&& matches!(
alignment_style.vertical_align_keyword(),
vertical_align::SUPER | vertical_align::SUB
);
- let adjusted_baseline = if !alignment_style.vertical_align_is_keyword() {
- fragment_baseline + alignment_style.vertical_align_value().to_px(style.line_height())
- } else if alignment_style.vertical_align_keyword() == vertical_align::SUPER {
- fragment_baseline + self.containing_style().font_size() / 3
- } else if alignment_style.vertical_align_keyword() == vertical_align::SUB {
- fragment_baseline - self.containing_style().font_size() / 5
- } else {
- fragment_baseline
- };
+ // https://drafts.csswg.org/css2/#line-height
+ // The line box height is the distance between the uppermost box top and the lowermost box bottom.
+ // NB: So, the baseline of the line box is as far below its top as the box that reaches highest above it
+ // once aligned, which is how far its vertical-align and those of the inline boxes around it shift it.
+ let enclosing_shifts = self.vertical_shifts_of_enclosing_inline_boxes(style_source);
+ let enclosing_shift = enclosing_shifts
+ .iter()
+ .fold(CssPixels::default(), |total, (_, shift)| total + *shift);
+ let metrics = self.fragment_alignment_metrics(line_index, fragment_index, style);
+ let adjusted_baseline =
+ self.extent_above_line_box_baseline(alignment_style, self.parent_style(style_source), metrics)
+ - enclosing_shift;
// NB: A vertical-align length or percentage, like 'sub' and 'super', shifts the box relative to the
// baseline, which leaves the strut on the baseline, whichever box ends up setting the baseline of the
- // line box.
- if !alignment_style.vertical_align_is_keyword() || shifted_by_keyword {
+ // line box. So does such a shift of an inline box around it.
+ if !alignment_style.vertical_align_is_keyword()
+ || shifted_by_keyword
+ || enclosing_shift != CssPixels::default()
+ {
should_align_strut_to_line_box_baseline = true;
}
if adjusted_baseline > line_box_baseline {
@@ -1180,28 +1252,17 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
}
// https://drafts.csswg.org/css2/#line-height
// The inline-level boxes are aligned vertically according to their 'vertical-align' property.
- // NB: An inline box around the fragment that sits on the baseline holds its own layout bounds above it, which
- // a box whose content is only other inline boxes has no text fragment of its own to bring in.
- let containing_block = self.context().containing_block;
- let mut ancestor = if style_source == containing_block {
- NodeSlotId::INVALID
- } else {
- self.context().parent_node(style_source)
- };
- while !ancestor.is_invalid() && ancestor != containing_block && self.facts(ancestor).is_fragmented_inline()
- {
- let ancestor_style = self.style(ancestor);
- if !ancestor_style.vertical_align_is_keyword()
- || ancestor_style.vertical_align_keyword() != vertical_align::BASELINE
- {
- break;
- }
- let ascent = TextLayoutBounds::new(ancestor_style, None).ascent;
+ // NB: An inline box around the fragment holds its own layout bounds around its baseline, wherever its
+ // vertical-align and those of the boxes around it put that, which a box whose content is only other
+ // inline boxes has no text fragment of its own to bring in.
+ let mut ancestor_shift = enclosing_shift;
+ for (ancestor, shift) in enclosing_shifts {
+ let ascent = TextLayoutBounds::new(self.style(ancestor), None).ascent - ancestor_shift;
if ascent > line_box_baseline {
should_align_strut_to_line_box_baseline = true;
line_box_baseline = ascent;
}
- ancestor = self.context().parent_node(ancestor);
+ ancestor_shift -= shift;
}
}
if let Some(marker_line) = self.outside_marker_line(line_index)
@@ -1667,29 +1728,13 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
style_source: fragment.style_source,
layout_node: fragment.layout_node,
baseline: fragment.baseline,
- block_size: fragment.physical_vertical_extent(),
- border_box_block_start: fragment.border_box_block_start,
is_atomic_inline: fragment.is_atomic_inline,
inline_offset: fragment.inline_offset,
block_offset: fragment.block_offset,
}
};
let style = self.style(snapshot.style_source);
- let mut metrics = VerticalAlignMetrics {
- baseline: snapshot.baseline,
- block_size: snapshot.block_size,
- effective_box_block_start_offset: snapshot.border_box_block_start,
- // Intentional old quirk: start is reused as end.
- effective_box_block_end_offset: snapshot.border_box_block_start,
- line_height: style.line_height(),
- };
- if snapshot.is_atomic_inline {
- let atomic_inline = self.line(line_index).fragments[fragment_index].box_cells();
- metrics.effective_box_block_start_offset =
- atomic_inline.margin_top + atomic_inline.border_box_top(false);
- metrics.effective_box_block_end_offset =
- atomic_inline.margin_bottom + atomic_inline.border_box_bottom(false);
- }
+ let metrics = self.fragment_alignment_metrics(line_index, fragment_index, style);
let containing_block = self.context().containing_block;
let fragment_inline_offset = if self.context().atomic_items_are_right_to_left {
self.line(line_index).inline_length
diff --git a/Tests/LibWeb/Layout/expected/vertical-align-line-relative-subtrees.txt b/Tests/LibWeb/Layout/expected/vertical-align-line-relative-subtrees.txt
index 8663032e8b3..c63e418ac7e 100644
--- a/Tests/LibWeb/Layout/expected/vertical-align-line-relative-subtrees.txt
+++ b/Tests/LibWeb/Layout/expected/vertical-align-line-relative-subtrees.txt
@@ -28,13 +28,13 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
frag 1 from BlockContainer start: 0, length: 0, rect: [20,108 20x60] baseline: 60
TextNode <#text> (not painted)
BlockContainer <span.tall> at [20,108] inline-block [0+0+0 20 0+0+0] [0+0+0 60 0+0+0] [BFC] children: not-inline
- BlockContainer <div.vertical> at [0,172] [0+0+0 22 0+0+0] [0+0+0 200 0+0+0] [BFC] children: inline
- InlineNode <span.middle> at [2,172] [0+0+0 20 0+0+0] [0+0+0 60 0+0+0]
- frag 0 from TextNode start: 0, length: 1, rect: [2,172 20x20] baseline: 16
+ BlockContainer <div.vertical> at [0,172] [0+0+0 20 0+0+0] [0+0+0 200 0+0+0] [BFC] children: inline
+ InlineNode <span.middle> at [0,172] [0+0+0 20 0+0+0] [0+0+0 60 0+0+0]
+ frag 0 from TextNode start: 0, length: 1, rect: [0,172 20x20] baseline: 16
"x"
- frag 1 from BlockContainer start: 0, length: 0, rect: [12,192 10x40] baseline: 0
+ frag 1 from BlockContainer start: 0, length: 0, rect: [10,192 10x40] baseline: 0
TextNode <#text> (not painted)
- BlockContainer <span.vertical-top> at [12,192] inline-block [0+0+0 10 0+0+0] [0+0+0 40 0+0+0] [BFC] children: not-inline
+ BlockContainer <span.vertical-top> at [10,192] inline-block [0+0+0 10 0+0+0] [0+0+0 40 0+0+0] [BFC] children: not-inline
SC for Viewport<#document> [0,0 800x600] (z-index: auto)
SC for BlockContainer<HTML> [0,0 800x372] (z-index: auto)
diff --git a/Tests/LibWeb/Layout/expected/vertical-align-text-top-bottom.txt b/Tests/LibWeb/Layout/expected/vertical-align-text-top-bottom.txt
index e940b961a39..1273061bc6a 100644
--- a/Tests/LibWeb/Layout/expected/vertical-align-text-top-bottom.txt
+++ b/Tests/LibWeb/Layout/expected/vertical-align-text-top-bottom.txt
@@ -1,6 +1,6 @@
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 94 0+0+0] [BFC] children: not-inline
- BlockContainer <body> at [0,0] [0+0+0 800 0+0+0] [0+0+0 94 0+0+0] children: not-inline
+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 86 0+0+0] [BFC] children: not-inline
+ BlockContainer <body> at [0,0] [0+0+0 800 0+0+0] [0+0+0 86 0+0+0] children: not-inline
BlockContainer <div.line> at [0,0] [0+0+0 800 0+0+0] [0+0+0 40 0+0+0] children: inline
frag 0 from TextNode start: 0, length: 1, rect: [0,10 20x20] baseline: 16
"A"
@@ -9,18 +9,18 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
TextNode <#text> (not painted)
BlockContainer <span.box.text-top> at [20,10] inline-block [0+0+0 20 0+0+0] [0+0+0 10 0+0+0] [BFC] children: not-inline
BlockContainer <span.box.text-bottom> at [40,20] inline-block [0+0+0 20 0+0+0] [0+0+0 10 0+0+0] [BFC] children: not-inline
- BlockContainer <div.line> at [0,40] [0+0+0 800 0+0+0] [0+0+0 54 0+0+0] children: inline
- frag 0 from TextNode start: 0, length: 1, rect: [0,64 20x20] baseline: 16
+ BlockContainer <div.line> at [0,40] [0+0+0 800 0+0+0] [0+0+0 46 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [0,56 20x20] baseline: 16
"A"
TextNode <#text> (not painted)
- InlineNode <span.parent> at [20,48] [0+0+0 80 0+0+0] [0+0+0 40 0+0+0]
- frag 0 from TextNode start: 0, length: 1, rect: [20,48 40x40] baseline: 32
+ InlineNode <span.parent> at [20,40] [0+0+0 80 0+0+0] [0+0+0 40 0+0+0]
+ frag 0 from TextNode start: 0, length: 1, rect: [20,40 40x40] baseline: 32
"B"
- frag 1 from BlockContainer start: 0, length: 0, rect: [60,48 20x10] baseline: 40
- frag 2 from BlockContainer start: 0, length: 0, rect: [80,78 20x10] baseline: 6
+ frag 1 from BlockContainer start: 0, length: 0, rect: [60,40 20x10] baseline: 40
+ frag 2 from BlockContainer start: 0, length: 0, rect: [80,70 20x10] baseline: 6
TextNode <#text> (not painted)
- BlockContainer <span.box.text-top> at [60,48] inline-block [0+0+0 20 0+0+0] [0+0+0 10 0+0+0] [BFC] children: not-inline
- BlockContainer <span.box.text-bottom> at [80,78] inline-block [0+0+0 20 0+0+0] [0+0+0 10 0+0+0] [BFC] children: not-inline
+ BlockContainer <span.box.text-top> at [60,40] inline-block [0+0+0 20 0+0+0] [0+0+0 10 0+0+0] [BFC] children: not-inline
+ BlockContainer <span.box.text-bottom> at [80,70] inline-block [0+0+0 20 0+0+0] [0+0+0 10 0+0+0] [BFC] children: not-inline
SC for Viewport<#document> [0,0 800x600] (z-index: auto)
- SC for BlockContainer<HTML> [0,0 800x94] (z-index: auto)
+ SC for BlockContainer<HTML> [0,0 800x86] (z-index: auto)
diff --git a/Tests/LibWeb/Text/expected/css/vertical-align-keywords-in-line-box-height.txt b/Tests/LibWeb/Text/expected/css/vertical-align-keywords-in-line-box-height.txt
new file mode 100644
index 00000000000..5c7534488df
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/vertical-align-keywords-in-line-box-height.txt
@@ -0,0 +1,20 @@
+middle-text: 60 (5)
+text-top-text: 60 (5)
+text-bottom-text: 60 (5)
+middle-empty: 60 (5)
+text-top-empty: 60 (5)
+text-bottom-empty: 60 (5)
+middle-inline-block: 60 (0)
+text-top-inline-block: 60 (0)
+text-bottom-inline-block: 60 (0)
+middle-alone: 60 (5)
+text-top-alone: 60 (5)
+middle-and-text-top: 82 (5, 27)
+middle-beside-taller-baseline: 67 (5, 12)
+text-top-beside-taller-baseline: 89 (5, 34)
+middle-in-top: 60 (22, 5)
+text-top-in-parent: 60 (0, 5)
+text-bottom-in-parent: 60 (30, 5)
+shifted-box-around-inline-block: 50 (15, 21)
+middle-box-around-inline-block: 50 (15, 21)
+sub-box-around-inline-block: 50 (15, 21)
diff --git a/Tests/LibWeb/Text/input/css/vertical-align-keywords-in-line-box-height.html b/Tests/LibWeb/Text/input/css/vertical-align-keywords-in-line-box-height.html
new file mode 100644
index 00000000000..7b81a14d901
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/vertical-align-keywords-in-line-box-height.html
@@ -0,0 +1,41 @@
+<!DOCTYPE html>
+<style>
+ @font-face { font-family: Ahem; src: url("../wpt-import/fonts/Ahem.ttf"); }
+ body { margin: 0; font: 20px/20px Ahem; }
+ .box { font-size: 50px; line-height: 60px; }
+ .ib { display: inline-block; width: 10px; height: 60px; }
+ .ibs { display: inline-block; width: 10px; height: 10px; }
+ .lh50 { line-height: 50px; }
+ .case { position: relative; }
+</style>
+<div class="case" id="middle-text"><span class="box" style="vertical-align: middle">y</span>x</div>
+<div class="case" id="text-top-text"><span class="box" style="vertical-align: text-top">y</span>x</div>
+<div class="case" id="text-bottom-text"><span class="box" style="vertical-align: text-bottom">y</span>x</div>
+<div class="case" id="middle-empty"><span class="box" style="vertical-align: middle"></span>x</div>
+<div class="case" id="text-top-empty"><span class="box" style="vertical-align: text-top"></span>x</div>
+<div class="case" id="text-bottom-empty"><span class="box" style="vertical-align: text-bottom"></span>x</div>
+<div class="case" id="middle-inline-block"><span class="ib" style="vertical-align: middle"></span>x</div>
+<div class="case" id="text-top-inline-block"><span class="ib" style="vertical-align: text-top"></span>x</div>
+<div class="case" id="text-bottom-inline-block"><span class="ib" style="vertical-align: text-bottom"></span>x</div>
+<div class="case" id="middle-alone"><span class="box" style="vertical-align: middle">y</span></div>
+<div class="case" id="text-top-alone"><span class="box" style="vertical-align: text-top">y</span></div>
+<div class="case" id="middle-and-text-top"><span class="box" style="vertical-align: middle">y</span><span class="box" style="vertical-align: text-top">y</span>x</div>
+<div class="case" id="middle-beside-taller-baseline"><span class="box">z</span><span class="box" style="vertical-align: middle">y</span>x</div>
+<div class="case" id="text-top-beside-taller-baseline"><span class="box">z</span><span class="box" style="vertical-align: text-top">y</span>x</div>
+<div class="case" id="middle-in-top"><span style="vertical-align: top"><span class="box" style="vertical-align: middle">y</span></span>x</div>
+<div class="case" id="text-top-in-parent"><span style="font-size: 30px; line-height: 30px">a<span class="box" style="vertical-align: text-top">y</span></span>x</div>
+<div class="case" id="text-bottom-in-parent"><span style="font-size: 30px; line-height: 30px">a<span class="box" style="vertical-align: text-bottom">y</span></span>x</div>
+<div class="case" id="shifted-box-around-inline-block"><span class="lh50" style="vertical-align: 10px"><span class="ibs"></span></span></div>
+<div class="case" id="middle-box-around-inline-block"><span class="lh50" style="vertical-align: middle"><span class="ibs"></span></span></div>
+<div class="case" id="sub-box-around-inline-block"><span class="lh50" style="vertical-align: sub"><span class="ibs"></span></span></div>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ await document.fonts.ready;
+ for (const element of document.querySelectorAll(".case")) {
+ const rect = element.getBoundingClientRect();
+ const offsets = [...element.querySelectorAll("span")].map(span => span.getBoundingClientRect().top - rect.top);
+ println(`${element.id}: ${rect.height} (${offsets.join(", ")})`);
+ }
+ });
+</script>