Skip to content

Truly independent web browser

LibWeb: Size nested inline boxes using font metrics

Andreas Kling87021db2737f
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
index 8d704db375b..2471d172956 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
@@ -698,15 +698,28 @@ pub(crate) fn compute(
&& !context.inline_boxes_continued_in_next_fragment.borrow().contains(&node);
let has_low_edge = if reversed { last } else { first };
let has_high_edge = if reversed { first } else { last };
- let content_block_start = line
- .first_direct_fragment_block_start
- .or(line.fallback_block_start_from_contributions)
- .unwrap_or_default();
- let content_block_length = if line.first_direct_fragment_block_start.is_some() {
- line.max_direct_fragment_block_length
- } else {
- context.style(node).line_height()
- };
+ let (inline_box_baseline, inline_box_vertical_shift) = inline_box_anchor();
+ // https://www.w3.org/TR/CSS22/visudet.html#inline-non-replaced
+ // The height of the content area should be based on the font, but this specification does not specify how.
+ let (content_block_start, content_block_length) =
+ if let Some(block_start) = line.first_direct_fragment_block_start {
+ (block_start, line.max_direct_fragment_block_length)
+ } else {
+ let style = context.style(node);
+ let height = line_builder::normal_line_height(style);
+ let baseline = line_builder::LineBuilder::baseline_for_style(style, height);
+ // NB: Line-relative alignment and atomic-only descendants can leave this box without a
+ // recorded baseline. In that case, retain the position of its aligned contributions.
+ let block_start = if context.line_data().line_boxes[line.line_index]
+ .inline_box_baseline(node)
+ .is_some()
+ {
+ inline_box_baseline - baseline
+ } else {
+ line.fallback_block_start_from_contributions.unwrap_or_default()
+ };
+ (block_start, height)
+ };
let border_inline_start = contributions_inline_start
- if has_low_edge {
border_padding_low
@@ -721,7 +734,6 @@ pub(crate) fn compute(
};
let border_block_start = content_block_start - border_padding_block_low;
let border_block_length = content_block_length + border_padding_block_low + border_padding_block_high;
- let (inline_box_baseline, inline_box_vertical_shift) = inline_box_anchor();
let rect = FfiCssPixelRect {
x: border_inline_start,
y: border_block_start,
diff --git a/Libraries/LibWeb/Rust/src/layout/line_builder.rs b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
index b309865ff6a..59aad274b87 100644
--- a/Libraries/LibWeb/Rust/src/layout/line_builder.rs
+++ b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
@@ -622,7 +622,7 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
.pixels_greater_than(current_inline_size + next_item_inline_size)
}
- fn baseline_for_style(style: StyleValues, line_height: CssPixels) -> CssPixels {
+ pub(super) fn baseline_for_style(style: StyleValues, line_height: CssPixels) -> CssPixels {
let typographic = CssPixels::nearest_value_for_f32(style.font_ascent() + style.font_descent());
CssPixels::nearest_value_for_f32(style.font_ascent()) + (line_height - typographic) / 2
}
diff --git a/Tests/LibWeb/Layout/expected/block-and-inline/abspos-first-in-nested-inline-flow-ancestors.txt b/Tests/LibWeb/Layout/expected/block-and-inline/abspos-first-in-nested-inline-flow-ancestors.txt
index 7e2cc2f79a6..b9a2eed576a 100644
--- a/Tests/LibWeb/Layout/expected/block-and-inline/abspos-first-in-nested-inline-flow-ancestors.txt
+++ b/Tests/LibWeb/Layout/expected/block-and-inline/abspos-first-in-nested-inline-flow-ancestors.txt
@@ -2,7 +2,7 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 20 0+0+0] [BFC] children: not-inline
BlockContainer <body> at [0,0] [0+0+0 800 0+0+0] [0+0+0 20 0+0+0] children: not-inline
BlockContainer <div> at [0,0] positioned [0+0+0 800 0+0+0] [0+0+0 20 0+0+0] children: inline
- InlineNode <span> at [0,5] [0+0+0 40 0+0+0] [0+0+0 20 0+0+0]
+ InlineNode <span> at [0,5] [0+0+0 40 0+0+0] [0+0+0 10 0+0+0]
InlineNode <i> at [0,5] [0+0+0 40 0+0+0] [0+0+0 10 0+0+0]
frag 0 from TextNode start: 0, length: 4, rect: [0,5 40x10] baseline: 8
"text"
diff --git a/Tests/LibWeb/Layout/expected/block-and-inline/abspos-in-nested-inlines-on-later-line.txt b/Tests/LibWeb/Layout/expected/block-and-inline/abspos-in-nested-inlines-on-later-line.txt
index 9a0fc491c22..c52e0d131d4 100644
--- a/Tests/LibWeb/Layout/expected/block-and-inline/abspos-in-nested-inlines-on-later-line.txt
+++ b/Tests/LibWeb/Layout/expected/block-and-inline/abspos-in-nested-inlines-on-later-line.txt
@@ -6,7 +6,7 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
"line1"
TextNode <#text> (not painted)
BreakNode <br> (not painted)
- InlineNode <span> at [0,25] [0+0+0 40 0+0+0] [0+0+0 20 0+0+0]
+ InlineNode <span> at [0,25] [0+0+0 40 0+0+0] [0+0+0 10 0+0+0]
InlineNode <i> at [0,25] [0+0+0 40 0+0+0] [0+0+0 10 0+0+0]
frag 0 from TextNode start: 0, length: 2, rect: [0,25 20x10] baseline: 8
"ab"
diff --git a/Tests/LibWeb/Ref/expected/inline-border-with-aligned-nested-spans-ref.html b/Tests/LibWeb/Ref/expected/inline-border-with-aligned-nested-spans-ref.html
new file mode 100644
index 00000000000..62033165a01
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/inline-border-with-aligned-nested-spans-ref.html
@@ -0,0 +1,20 @@
+<!doctype html>
+<style>
+ @font-face {
+ font-family: Ahem;
+ src: url("../data/fonts/Ahem.ttf");
+ }
+ body { font: 20px/40px Ahem; }
+ .line { line-height: 80px; }
+ .large { font-size: 60px; }
+ .border { border: 2px solid blue; }
+ .top { vertical-align: top; }
+ .bottom { vertical-align: bottom; }
+ .atomic { display: inline-block; width: 40px; height: 20px; background: green; }
+</style>
+<div class="line"><span class="top"><span class="border">XX</span></span><span class="large">X</span></div>
+<div class="line"><span class="bottom"><span class="border">XX</span></span><span class="large">X</span></div>
+<div class="line"><span class=""><span class="border">XX</span></span><span class="large">X</span></div>
+<div class="line"><span class="top"><span class="border"><span class="atomic"></span></span></span><span class="large">X</span></div>
+<div class="line"><span class="bottom"><span class="border"><span class="atomic"></span></span></span><span class="large">X</span></div>
+<div class="line"><span class=""><span class="border"><span class="atomic"></span></span></span><span class="large">X</span></div>
diff --git a/Tests/LibWeb/Ref/expected/inline-border-with-nested-spans-ref.html b/Tests/LibWeb/Ref/expected/inline-border-with-nested-spans-ref.html
new file mode 100644
index 00000000000..4e93ad54ead
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/inline-border-with-nested-spans-ref.html
@@ -0,0 +1,16 @@
+<!doctype html>
+<meta charset="utf-8">
+<style>
+ body { font: 20px/3 sans-serif; }
+ .border { border: 1px dotted blue; padding: 2px 3px; }
+ .tight { line-height: 0.8; }
+ .shifted { vertical-align: 6px; }
+ .relative { position: relative; top: 4px; }
+ .wrap { width: 100px; }
+</style>
+<div><span class="border">ˌkɒksɪˈnɛlɪdiː</span></div>
+<div><span class="border tight">phonetic text</span></div>
+<div><span class="shifted"><span class="border">shifted text</span></span></div>
+<div><span class="border relative">relative text</span></div>
+<div class="wrap"><span class="border">nested text wrapping across lines</span></div>
+<div dir="rtl"><span class="border">nested text</span></div>
diff --git a/Tests/LibWeb/Ref/input/inline-border-with-aligned-nested-spans.html b/Tests/LibWeb/Ref/input/inline-border-with-aligned-nested-spans.html
new file mode 100644
index 00000000000..86ed33830be
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/inline-border-with-aligned-nested-spans.html
@@ -0,0 +1,21 @@
+<!doctype html>
+<link rel="match" href="../expected/inline-border-with-aligned-nested-spans-ref.html">
+<style>
+ @font-face {
+ font-family: Ahem;
+ src: url("../data/fonts/Ahem.ttf");
+ }
+ body { font: 20px/40px Ahem; }
+ .line { line-height: 80px; }
+ .large { font-size: 60px; }
+ .border { border: 2px solid blue; }
+ .top { vertical-align: top; }
+ .bottom { vertical-align: bottom; }
+ .atomic { display: inline-block; width: 40px; height: 20px; background: green; }
+</style>
+<div class="line"><span class="border top"><span>XX</span></span><span class="large">X</span></div>
+<div class="line"><span class="border bottom"><span>XX</span></span><span class="large">X</span></div>
+<div class="line"><span class="border "><span>XX</span></span><span class="large">X</span></div>
+<div class="line"><span class="border top"><span><span class="atomic"></span></span></span><span class="large">X</span></div>
+<div class="line"><span class="border bottom"><span><span class="atomic"></span></span></span><span class="large">X</span></div>
+<div class="line"><span class="border "><span><span class="atomic"></span></span></span><span class="large">X</span></div>
diff --git a/Tests/LibWeb/Ref/input/inline-border-with-nested-spans.html b/Tests/LibWeb/Ref/input/inline-border-with-nested-spans.html
new file mode 100644
index 00000000000..9bf2ed6044d
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/inline-border-with-nested-spans.html
@@ -0,0 +1,17 @@
+<!doctype html>
+<meta charset="utf-8">
+<link rel="match" href="../expected/inline-border-with-nested-spans-ref.html">
+<style>
+ body { font: 20px/3 sans-serif; }
+ .border { border: 1px dotted blue; padding: 2px 3px; }
+ .tight { line-height: 0.8; }
+ .shifted { vertical-align: 6px; }
+ .relative { position: relative; top: 4px; }
+ .wrap { width: 100px; }
+</style>
+<div><span class="border"><span><span>ˌkɒksɪˈnɛlɪdiː</span></span></span></div>
+<div><span class="border tight"><span><span>phonetic text</span></span></span></div>
+<div><span class="shifted"><span class="border"><span><span>shifted text</span></span></span></span></div>
+<div><span class="border relative"><span><span>relative text</span></span></span></div>
+<div class="wrap"><span class="border"><span>nested text wrapping across lines</span></span></div>
+<div dir="rtl"><span class="border"><span>nested text</span></span></div>