Truly independent web browser
LibWeb: Size MathML text tokens to their glyphs
sideshowbarker048402cd8521
diff --git a/Libraries/LibWeb/Rust/src/gfx_test_stubs.rs b/Libraries/LibWeb/Rust/src/gfx_test_stubs.rsindex 60c03b73d95..67e3aee57ff 100644--- a/Libraries/LibWeb/Rust/src/gfx_test_stubs.rs+++ b/Libraries/LibWeb/Rust/src/gfx_test_stubs.rs@@ -138,6 +138,17 @@ extern "C" fn ladybird_gfx_glyph_run_bounding_box(unreachable!("no unit test shapes text");}+#[unsafe(no_mangle)]+extern "C" fn ladybird_gfx_glyph_run_ink_bounding_box(+ _font: *const c_void,+ _glyphs: *const ShapedGlyph,+ _glyph_count: usize,+ _scale: f32,+ _out_rect: *mut f32,+) {+ unreachable!("no unit test shapes text");+}+#[unsafe(no_mangle)]#[allow(clippy::too_many_arguments)]extern "C" fn ladybird_gfx_glyph_run_glyph_intercepts(diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rsindex 7faaf1ce82b..c1eeb60fdfd 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs@@ -3676,7 +3676,22 @@ impl<'context> InlineFormattingContext<'context> {}None => inline_level_iterator::InlineLevelIterator::new(self),};+ let uses_token_ink_metrics = self.facts(self.containing_block).display().is_math_inside()+ && matches!(+ self.pass.arena().math_layout_kind(self.containing_block),+ math_formatting_context::MathLayoutKind::Token+ | math_formatting_context::MathLayoutKind::Mtext+ | math_formatting_context::MathLayoutKind::Mo+ )+ && self+ .pass+ .children(self.containing_block)+ .all(|child| self.facts(child).is_text_node())+ && iterator.contains_only_unbreakable_text(self);self.generate_line_boxes(iterator);+ if uses_token_ink_metrics && let [line] = self.line_data.get_mut().line_boxes.as_mut_slice() {+ math_formatting_context::size_token_line_to_ink(line);+ }if self.run.fragments.is_some() {self.fold_inline_ancestor_relative_insets_into_line_data();self.register_inline_anchor_candidates();diff --git a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rsindex aeebb5eb619..1a8958339e9 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs@@ -2648,6 +2648,14 @@ impl InlineLevelIterator {self.items.as_slice()}+ pub(crate) fn contains_only_unbreakable_text(+ &self,+ context: &inline_formatting_context::InlineFormattingContext<'_>,+ ) -> bool {+ self.items().iter().all(|item| item.type_ == ItemType::Text)+ && sequence_length(context, &self.prepared, self.items(), false, false).0 == self.items().len()+ }+pub(crate) fn skip_items(&mut self, count: usize) {self.items.skip(count);}diff --git a/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rsindex 6a0868ae051..c2e9c089f40 100644--- a/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rs@@ -93,6 +93,31 @@ impl MathLayoutKind {}}+// https://w3c.github.io/mathml-core/#layout-of-mtext+pub(super) fn size_token_line_to_ink(line: &mut line_box::LineBoxData) {+ let mut ink = libgfx_rust::FloatRect::default();+ for fragment in &line.fragments {+ if let Some(run) = &fragment.glyphs {+ let mut bounds = libgfx_rust::FloatRect::from_array(libgfx_rust::text_layout::glyph_run_ink_bounding_box(+ &run.font,+ &run.glyphs,+ 1.0,+ ));+ bounds.y += (fragment.block_offset + fragment.baseline - line.block_start - line.baseline).to_float();+ ink = ink.united(bounds);+ }+ }+ let ascent = CssPixels::nearest_value_for_f32(-ink.y);+ let shift = ascent - line.baseline;+ for fragment in &mut line.fragments {+ fragment.block_offset += shift;+ }+ line.baseline = ascent;+ line.block_length = ascent + CssPixels::nearest_value_for_f32(ink.bottom());+ line.block_end = line.block_start + line.block_length;+ line.next_line_block_offset = line.block_end;+}+/// The unit of a MathML attribute value that is a `<length-percentage>`. Mirrors the C++ `MathLengthUnit`, where 0/// stands for an attribute that is absent or invalid.pub(crate) mod math_length_unit {diff --git a/Tests/LibWeb/Layout/expected/mathml-out-of-flow-children-are-not-wrapped.txt b/Tests/LibWeb/Layout/expected/mathml-out-of-flow-children-are-not-wrapped.txtindex 4a82a265bb4..4d61864079d 100644--- a/Tests/LibWeb/Layout/expected/mathml-out-of-flow-children-are-not-wrapped.txt+++ b/Tests/LibWeb/Layout/expected/mathml-out-of-flow-children-are-not-wrapped.txt@@ -1,31 +1,31 @@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 36 0+0+0] [BFC] children: not-inline- BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 20 0+0+8] children: inline- frag 0 from BlockContainer start: 0, length: 0, rect: [8,8 33.171875x20] baseline: 16- BlockContainer <math> at [8,8] [0+0+0 33.171875 0+0+0] [0+0+0 20 0+0+0] [MathFC] children: not-inline- BlockContainer <mn> at [8,8] positioned [0+0+0 12 0+0+0] [0+0+0 20 0+0+0] [BFC] children: inline- frag 0 from TextNode start: 0, length: 1, rect: [8,8 12x20] baseline: 16+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 32 0+0+0] [BFC] children: not-inline+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 16 0+0+8] children: inline+ frag 0 from BlockContainer start: 0, length: 0, rect: [8,8.84375 33.171875x12.15625] baseline: 12.15625+ BlockContainer <math> at [8,8.84375] [0+0+0 33.171875 0+0+0] [0+0+0 12.15625 0+0+0] [MathFC] children: not-inline+ BlockContainer <mn> at [8,8.84375] positioned [0+0+0 12 0+0+0] [0+0+0 12.03125 0+0+0] [BFC] children: inline+ frag 0 from TextNode start: 0, length: 1, rect: [8,4.875 12x20] baseline: 16"0"TextNode <#text> (not painted)- BlockContainer <mn> at [8,8] positioned [0+0+0 7.9375 0+0+0] [0+0+0 20 0+0+0] [BFC] children: inline- frag 0 from TextNode start: 0, length: 1, rect: [8,8 7.9375x20] baseline: 16+ BlockContainer <mn> at [8,8.84375] positioned [0+0+0 7.9375 0+0+0] [0+0+0 11.71875 0+0+0] [BFC] children: inline+ frag 0 from TextNode start: 0, length: 1, rect: [8,4.5625 7.9375x20] baseline: 16"1"TextNode <#text> (not painted)- BlockContainer <mn> at [8,8] [0+0+0 11.015625 0+0+0] [0+0+0 20 0+0+0] [BFC] children: inline- frag 0 from TextNode start: 0, length: 1, rect: [8,8 11.015625x20] baseline: 16+ BlockContainer <mn> at [8,8.84375] [0+0+0 11.015625 0+0+0] [0+0+0 12.15625 0+0+0] [BFC] children: inline+ frag 0 from TextNode start: 0, length: 1, rect: [8,5 11.015625x20] baseline: 16"2"TextNode <#text> (not painted)- BlockContainer <mrow> at [23.453125,8] [0+0+0 13.28125 0+0+0] [0+0+0 20 0+0+0] [MathFC] children: not-inline- BlockContainer <mn> at [23.453125,8] positioned [0+0+0 11.375 0+0+0] [0+0+0 20 0+0+0] [BFC] children: inline- frag 0 from TextNode start: 0, length: 1, rect: [23.453125,8 11.375x20] baseline: 16+ BlockContainer <mrow> at [23.453125,8.921875] [0+0+0 13.28125 0+0+0] [0+0+0 12.078125 0+0+0] [MathFC] children: not-inline+ BlockContainer <mn> at [23.453125,8.921875] positioned [0+0+0 11.375 0+0+0] [0+0+0 12.21875 0+0+0] [BFC] children: inline+ frag 0 from TextNode start: 0, length: 1, rect: [23.453125,5.375 11.375x20] baseline: 16"3"TextNode <#text> (not painted)- BlockContainer <mo> at [23.453125,8] [0+0+0 13.28125 0+0+0] [0+0+0 20 0+0+0] [BFC] children: inline- frag 0 from TextNode start: 0, length: 1, rect: [23.453125,8 13.28125x20] baseline: 16+ BlockContainer <mo> at [23.453125,8.921875] [0+0+0 13.28125 0+0+0] [0+0+0 10.21875 0+0+0] [BFC] children: inline+ frag 0 from TextNode start: 0, length: 1, rect: [23.453125,5 13.28125x20] baseline: 16"+"TextNode <#text> (not painted)TextNode <#text> (not painted)SC for Viewport<#document> [0,0 800x600] (z-index: auto)- SC for BlockContainer<HTML> [0,0 800x36] (z-index: auto)- SC for BlockContainer<mn> [8,8 7.9375x20] (z-index: auto)+ SC for BlockContainer<HTML> [0,0 800x32] (z-index: auto)+ SC for BlockContainer<mn> [8,8.84375 7.9375x11.71875] (z-index: auto)diff --git a/Tests/LibWeb/Layout/expected/tree-builder/math-runs-split-by-oof.txt b/Tests/LibWeb/Layout/expected/tree-builder/math-runs-split-by-oof.txtindex 83a0d7f4308..01b6641de47 100644--- a/Tests/LibWeb/Layout/expected/tree-builder/math-runs-split-by-oof.txt+++ b/Tests/LibWeb/Layout/expected/tree-builder/math-runs-split-by-oof.txt@@ -8,19 +8,19 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] childrenfrag 0 from TextNode start: 0, length: 1, rect: [8,8 9.34375x16] baseline: 13"a"TextNode <#text> (not painted)- BlockContainer <mi> at [8,8] positioned [0+0+0 5.84375 0+0+0] [0+0+0 16 0+0+0] [BFC] children: inline- frag 0 from TextNode start: 0, length: 2, rect: [8,8 5.84375x16] baseline: 13+ BlockContainer <mi> at [8,8] positioned [0+0+0 5.84375 0+0+0] [0+0+0 10.65625 0+0+0] [BFC] children: inline+ frag 0 from TextNode start: 0, length: 2, rect: [8,5.65625 5.84375x16] baseline: 13"π₯"TextNode <#text> (not painted)- BlockContainer <mi> at [17.34375,8] [0+0+0 5.84375 0+0+0] [0+0+0 16 0+0+0] [BFC] children: inline+ BlockContainer <mi> at [17.34375,10.34375] [0+0+0 5.84375 0+0+0] [0+0+0 10.65625 0+0+0] [BFC] children: inlinefrag 0 from TextNode start: 0, length: 2, rect: [17.34375,8 5.84375x16] baseline: 13"π¦"TextNode <#text> (not painted)- BlockContainer <mi> at [23.1875,8] floating [0+0+0 5.84375 0+0+0] [0+0+0 16 0+0+0] [BFC] children: inline+ BlockContainer <mi> at [23.1875,10.34375] floating [0+0+0 5.84375 0+0+0] [0+0+0 10.65625 0+0+0] [BFC] children: inlinefrag 0 from TextNode start: 0, length: 2, rect: [23.1875,8 5.84375x16] baseline: 13"π§"TextNode <#text> (not painted)- BlockContainer <mn> at [29.03125,8] [0+0+0 6.34375 0+0+0] [0+0+0 16 0+0+0] [BFC] children: inline+ BlockContainer <mn> at [29.03125,11.625] [0+0+0 6.34375 0+0+0] [0+0+0 9.375 0+0+0] [BFC] children: inlinefrag 0 from TextNode start: 0, length: 1, rect: [29.03125,8 6.34375x16] baseline: 13"1"TextNode <#text> (not painted)diff --git a/Tests/LibWeb/Layout/expected/tree-builder/mathml-mspace-has-no-children.txt b/Tests/LibWeb/Layout/expected/tree-builder/mathml-mspace-has-no-children.txtindex 6879d2cc0ea..cc4dc14829d 100644--- a/Tests/LibWeb/Layout/expected/tree-builder/mathml-mspace-has-no-children.txt+++ b/Tests/LibWeb/Layout/expected/tree-builder/mathml-mspace-has-no-children.txt@@ -1,10 +1,10 @@Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inlineBlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 32 0+0+0] [BFC] children: not-inlineBlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 16 0+0+8] children: inline- frag 0 from BlockContainer start: 0, length: 0, rect: [8,8 15.84375x16] baseline: 13- BlockContainer <math> at [8,8] [0+0+0 15.84375 0+0+0] [0+0+0 16 0+0+0] [MathFC] children: not-inline+ frag 0 from BlockContainer start: 0, length: 0, rect: [8,10.34375 15.84375x10.65625] baseline: 10.65625+ BlockContainer <math> at [8,10.34375] [0+0+0 15.84375 0+0+0] [0+0+0 10.65625 0+0+0] [MathFC] children: not-inlineBlockContainer <mspace> at [8,11] [0+0+0 10 0+0+0] [0+0+0 10 0+0+0] [MathFC] children: not-inline- BlockContainer <mi> at [18,8] [0+0+0 5.84375 0+0+0] [0+0+0 16 0+0+0] [BFC] children: inline+ BlockContainer <mi> at [18,10.34375] [0+0+0 5.84375 0+0+0] [0+0+0 10.65625 0+0+0] [BFC] children: inlinefrag 0 from TextNode start: 0, length: 2, rect: [18,8 5.84375x16] baseline: 13"π¦"TextNode <#text> (not painted)diff --git a/Tests/LibWeb/Text/expected/MathML/token-ink.txt b/Tests/LibWeb/Text/expected/MathML/token-ink.txtnew file mode 100644index 00000000000..9dea5a19fa6--- /dev/null+++ b/Tests/LibWeb/Text/expected/MathML/token-ink.txt@@ -0,0 +1,7 @@+mi p: ascent 0, descent 8+mn x: ascent 32, descent 8+space-only : ascent 0, descent 0+mo p: ascent 0, descent 8+mtext xp: ascent 32, descent 8+breakable x p: ascent 52, descent 28+nested x: ascent 52, descent 28diff --git a/Tests/LibWeb/Text/input/MathML/token-ink.html b/Tests/LibWeb/Text/input/MathML/token-ink.htmlnew file mode 100644index 00000000000..3f4b007465c--- /dev/null+++ b/Tests/LibWeb/Text/input/MathML/token-ink.html@@ -0,0 +1,28 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ @font-face { font-family: Ahem; src: url("../wpt-import/fonts/Ahem.ttf"); }+ math { font: 40px/2 Ahem; }+ mi { text-transform: none; }+</style>+<math>+ <mspace id="baseline" width="1px" height="0px"></mspace>+ <mi>p</mi>+ <mn>x</mn>+ <mtext id="space-only" style="white-space:pre"> </mtext>+ <mo stretchy="false">p</mo>+ <mtext>xp</mtext>+ <mtext id="breakable">x p</mtext>+ <mtext id="nested"><span>x</span></mtext>+</math>+<script>+ asyncTest(async done => {+ await document.fonts.load("40px Ahem");+ const baseline = document.getElementById("baseline").getBoundingClientRect().top;+ for (const token of document.querySelectorAll("mi, mn, mo, mtext")) {+ const rect = token.getBoundingClientRect();+ println(`${token.id || token.localName} ${token.textContent}: ascent ${baseline - rect.top}, descent ${rect.bottom - baseline}`);+ }+ done();+ });+</script>