Truly independent web browser
LibWeb: Lay out mmultiscripts with prescripts and postscripts
Andreas Kling13bc0bcb0acd
diff --git a/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rsindex 1237232633e..cae722791e9 100644--- a/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rs@@ -866,6 +866,9 @@ impl<'pass> MathFormattingContext<'pass> {self.accents(root),),(MathLayoutKind::Mspace, _) => self.layout_space(&root_style),+ (MathLayoutKind::Mmultiscripts, _) if self.is_valid_multiscripts(&children) => {+ layout_multiscripts(&children, self.prescripts_separator_index(&children), &constants)+ }(MathLayoutKind::Mroot, 2) => {let radical = RadicalMetrics::new(&root_style, &constants);layout_root(&children[0], &children[1], &radical, &constants)@@ -934,6 +937,32 @@ impl<'pass> MathFormattingContext<'pass> {self.content = content;}+ fn prescripts_separator_index(&self, children: &[Child]) -> Option<usize> {+ children+ .iter()+ .position(|child| self.kind(child.node) == MathLayoutKind::Mprescripts)+ }++ // https://w3c.github.io/mathml-core/#prescripts-and-tensor-indices-mmultiscripts+ fn is_valid_multiscripts(&self, children: &[Child]) -> bool {+ // A valid <mmultiscripts> element contains the following in-flow children:+ // - A first in-flow child, called the mmultiscripts base, that is not an <mprescripts> element.+ // - Followed by an even number of in-flow children called mmultiscripts postscripts, none of them being a+ // <mprescripts> element.+ // - Optionally followed by an <mprescripts> element and an even number of in-flow children called+ // mmultiscripts prescripts, none of them being a <mprescripts> element.+ let separator_count = children+ .iter()+ .filter(|child| self.kind(child.node) == MathLayoutKind::Mprescripts)+ .count();+ match self.prescripts_separator_index(children) {+ Option::None => !children.is_empty() && children.len() % 2 == 1,+ Some(separator) => {+ separator_count == 1 && separator % 2 == 1 && (children.len() - separator - 1).is_multiple_of(2)+ }+ }+ }+fn layout_row_with_operator_spacing(&self,children: &[Child],@@ -1225,15 +1254,15 @@ fn layout_fraction_without_bar(}// https://w3c.github.io/mathml-core/#subscripts-and-superscripts-msub-msup-msubsup-fn layout_scripts(+/// The SubShift and SuperShift of a base with a subscript, a superscript or both.+// https://w3c.github.io/mathml-core/#base-with-subscript-and-superscript+fn script_shifts(base: &Child,subscript: Option<&Child>,superscript: Option<&Child>,constants: &MathConstants,-) -> MathContent {+) -> (Option<CssPixels>, Option<CssPixels>) {// FIXME: Use the italic correction of the base and the MATH table constants of the font.- // spaceAfterScript: MATH.MathConstants.spaceAfterScript or 1/24em if the constant is not available.- let space_after_script = constants.em.scaled(1.0 / 24.0);// subscriptTopMax: ⅘ × OS/2.sxHeight if the constant is not available.let subscript_top_max = constants.x_height.scaled(0.8);// superscriptBottomMin: ¼ × OS/2.sxHeight if the constant is not available.@@ -1286,6 +1315,18 @@ fn layout_scripts(}}}+ (sub_shift, super_shift)+}++fn layout_scripts(+ base: &Child,+ subscript: Option<&Child>,+ superscript: Option<&Child>,+ constants: &MathConstants,+) -> MathContent {+ // spaceAfterScript: MATH.MathConstants.spaceAfterScript or 1/24em if the constant is not available.+ let space_after_script = constants.em.scaled(1.0 / 24.0);+ let (sub_shift, super_shift) = script_shifts(base, subscript, superscript, constants);let mut ascent = base.ascent;let mut descent = base.descent;@@ -1317,6 +1358,98 @@ fn layout_scripts(}}+// https://w3c.github.io/mathml-core/#base-with-prescripts-and-postscripts+fn layout_multiscripts(children: &[Child], separator: Option<usize>, constants: &MathConstants) -> MathContent {+ // FIXME: Use the italic correction of the postscripts' base.+ let space_after_script = constants.em.scaled(1.0 / 24.0);+ let base = &children[0];+ let postscripts = &children[1..separator.unwrap_or(children.len())];+ let prescripts = separator.map_or(&children[0..0], |separator| &children[separator + 1..]);++ // SubShift (respectively SuperShift) is calculated by taking the maximum of all subshifts (respectively+ // supershifts) of each subscript/superscript pair as described in <msubsup>.+ let mut sub_shift = CssPixels::default();+ let mut super_shift = CssPixels::default();+ for pair in postscripts.chunks(2).chain(prescripts.chunks(2)) {+ let (pair_sub_shift, pair_super_shift) = script_shifts(base, Some(&pair[0]), Some(&pair[1]), constants);+ sub_shift = sub_shift.max(pair_sub_shift.unwrap_or_default());+ super_shift = super_shift.max(pair_super_shift.unwrap_or_default());+ }++ // The line-ascent (respectively line-descent) of the math content is calculated by taking the maximum of all the+ // line-ascent (respectively line-descent) of each subscript/superscript pair as described in <msubsup> but using+ // the SubShift and SuperShift values calculated above.+ let mut ascent = base.ascent;+ let mut descent = base.descent;+ for pair in postscripts.chunks(2).chain(prescripts.chunks(2)) {+ ascent = ascent.max(pair[0].ascent - sub_shift).max(pair[1].ascent + super_shift);+ descent = descent+ .max(pair[0].descent + sub_shift)+ .max(pair[1].descent - super_shift);+ }++ // Finally, the placement of the in-flow children is performed using the following algorithm:+ let mut positions = vec![ChildPosition::default(); children.len()];+ // Set inline-offset to 0.+ let mut inline_offset = CssPixels::default();+ // For each subscript/superscript pair of mmultiscripts prescripts:+ if let Some(separator) = separator {+ for (pair_index, pair) in prescripts.chunks(2).enumerate() {+ // Increment inline-offset by SpaceAfterScript.+ inline_offset += space_after_script;+ // Set pair-inline-size to the maximum of the inline size of the subscript's margin box and the inline size+ // of the superscript's margin box.+ let pair_inline_size = pair[0].inline_size.max(pair[1].inline_size);+ // Place the subscript (respectively superscript) at inline-start position inline-offset ++ // pair-inline-size − the inline size of the subscript's (respectively superscript's) margin box, so its+ // alphabetic baseline is shifted away from the alphabetic baseline by SubShift (respectively SuperShift)+ // towards the line-under (respectively line-over).+ let index = separator + 1 + pair_index * 2;+ positions[index] = ChildPosition {+ line_left_offset: inline_offset + pair_inline_size - pair[0].inline_size,+ baseline_shift: sub_shift,+ };+ positions[index + 1] = ChildPosition {+ line_left_offset: inline_offset + pair_inline_size - pair[1].inline_size,+ baseline_shift: -super_shift,+ };+ // Increment inline-offset by pair-inline-size.+ inline_offset += pair_inline_size;+ }+ // Place the mmultiscripts base and <mprescripts> boxes at inline offsets inline-offset and with their+ // alphabetic baselines aligned with the alphabetic baseline.+ positions[separator].line_left_offset = inline_offset;+ }+ positions[0].line_left_offset = inline_offset;+ // Increment inline-offset by the inline size of the mmultiscripts base's margin box.+ inline_offset += base.inline_size;+ // For each subscript/superscript pair of mmultiscripts postscripts:+ for (pair_index, pair) in postscripts.chunks(2).enumerate() {+ // Set pair-inline-size to the maximum of the inline size of the subscript's margin box and the inline size of+ // the superscript's margin box.+ let pair_inline_size = pair[0].inline_size.max(pair[1].inline_size);+ // Place the subscript at inline-start position inline-offset − LargeOpItalicCorrection, and the superscript+ // at inline-start position inline-offset + ItalicCorrection.+ let index = 1 + pair_index * 2;+ positions[index] = ChildPosition {+ line_left_offset: inline_offset,+ baseline_shift: sub_shift,+ };+ positions[index + 1] = ChildPosition {+ line_left_offset: inline_offset,+ baseline_shift: -super_shift,+ };+ // Increment inline-offset by pair-inline-size and SpaceAfterScript.+ inline_offset += pair_inline_size + space_after_script;+ }+ MathContent {+ inline_size: inline_offset,+ ascent,+ descent,+ positions,+ }+}+// https://w3c.github.io/mathml-core/#underscripts-and-overscripts-munder-mover-munderoverfn layout_underover(base: &Child,diff --git a/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/mrow/no-spacing.txt b/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/mrow/no-spacing.txtindex f4149b8bb10..cbab8e59f83 100644--- a/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/mrow/no-spacing.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/mrow/no-spacing.txt@@ -2,8 +2,8 @@ Harness status: OKFound 54 tests-43 Pass-11 Fail+46 Pass+8 FailPass Spacing inside an <mfrac> child of <math>.Pass Spacing around an <mfrac> child of an <math>.Pass Spacing inside an <mfrac> child of <mrow>.@@ -28,11 +28,11 @@ Pass Spacing inside an <msubsup> child of <mrow>.Pass Spacing around an <msubsup> child of an <mrow>.Pass Spacing inside an <msubsup> child of <mtd>.Fail Spacing around an <msubsup> child of an <mtd>.-Fail Spacing inside an <mmultiscripts> child of <math>.+Pass Spacing inside an <mmultiscripts> child of <math>.Pass Spacing around an <mmultiscripts> child of an <math>.-Fail Spacing inside an <mmultiscripts> child of <mrow>.+Pass Spacing inside an <mmultiscripts> child of <mrow>.Pass Spacing around an <mmultiscripts> child of an <mrow>.-Fail Spacing inside an <mmultiscripts> child of <mtd>.+Pass Spacing inside an <mmultiscripts> child of <mtd>.Fail Spacing around an <mmultiscripts> child of an <mtd>.Pass Spacing inside an <munder> child of <math>.Pass Spacing around an <munder> child of an <math>.diff --git a/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-2.txt b/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-2.txtindex 2778cd36d24..3583d5477d7 100644--- a/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-2.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-2.txt@@ -2,11 +2,10 @@ Harness status: OKFound 6 tests-3 Pass-3 Fail-Fail Respective horizontal positions+6 Pass+Pass Respective horizontal positionsPass Alignment of the base on the baseline-Fail Vertical position of scripts-Fail Width of scripted elements+Pass Vertical position of scripts+Pass Width of scripted elementsPass Height of scripted elementsPass Size of empty elements\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-3.txt b/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-3.txtindex 84cdec73db7..8e7f12fc6f0 100644--- a/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-3.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-3.txt@@ -2,10 +2,9 @@ Harness status: OKFound 5 tests-1 Pass-4 Fail+5 PassPass Alignment of the base on the baseline-Fail Dimensions of the scripted elements-Fail Vertical positions of scripts-Fail Horizontal alignment of scripts-Fail Horizontal positions of scripts\ No newline at end of file+Pass Dimensions of the scripted elements+Pass Vertical positions of scripts+Pass Horizontal alignment of scripts+Pass Horizontal positions of scripts\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-5.txt b/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-5.txtindex 3b113d92045..a71788dd530 100644--- a/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-5.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-5.txt@@ -2,8 +2,7 @@ Harness status: OKFound 3 tests-1 Pass-2 Fail+3 PassPass Alignment on the baseline with different and large script heights-Fail Tall subscripts/superscripts are not placed too high/low-Fail No collisions for tall subscripts and superscripts\ No newline at end of file+Pass Tall subscripts/superscripts are not placed too high/low+Pass No collisions for tall subscripts and superscripts\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-6-rtl.txt b/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-6-rtl.txtindex 54594308c2f..05e2203fc58 100644--- a/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-6-rtl.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-6-rtl.txt@@ -2,9 +2,9 @@ Harness status: OKFound 5 tests-5 Fail-Fail mprescripts RTL coordinates and sizes (rtl 2 postscripts)-Fail mprescripts RTL coordinates and sizes (rtl 2 prescripts)-Fail mprescripts RTL coordinates and sizes (rtl 2 prescripts, 4 postscripts)-Fail mprescripts RTL coordinates and sizes (rtl 4 prescripts, 2 postscripts)-Fail mprescripts RTL coordinates and sizes (rtl prescripts with padding/border/margin)\ No newline at end of file+5 Pass+Pass mprescripts RTL coordinates and sizes (rtl 2 postscripts)+Pass mprescripts RTL coordinates and sizes (rtl 2 prescripts)+Pass mprescripts RTL coordinates and sizes (rtl 2 prescripts, 4 postscripts)+Pass mprescripts RTL coordinates and sizes (rtl 4 prescripts, 2 postscripts)+Pass mprescripts RTL coordinates and sizes (rtl prescripts with padding/border/margin)\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-6.txt b/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-6.txtindex 455ebc173ef..508e157b96d 100644--- a/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-6.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-6.txt@@ -2,9 +2,9 @@ Harness status: OKFound 5 tests-5 Fail-Fail mprescripts coordinates and sizes (2 postscripts)-Fail mprescripts coordinates and sizes (2 prescripts)-Fail mprescripts coordinates and sizes (2 prescripts, 4 postscripts)-Fail mprescripts coordinates and sizes (4 prescripts, 2 postscripts)-Fail mprescripts coordinates and sizes (prescripts with padding/border/margin)\ No newline at end of file+5 Pass+Pass mprescripts coordinates and sizes (2 postscripts)+Pass mprescripts coordinates and sizes (2 prescripts)+Pass mprescripts coordinates and sizes (2 prescripts, 4 postscripts)+Pass mprescripts coordinates and sizes (4 prescripts, 2 postscripts)+Pass mprescripts coordinates and sizes (prescripts with padding/border/margin)\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-parameters-2.txt b/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-parameters-2.txtindex 3576c5b3351..1a7261a2b70 100644--- a/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-parameters-2.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/scripts/subsup-parameters-2.txt@@ -2,7 +2,8 @@ Harness status: OKFound 3 tests-3 Fail-Fail Null Italic Correction+1 Pass+2 Fail+Pass Null Italic CorrectionFail NonNull Italic Correction (MathGlyphVariantRecord)Fail NonNull Italic Correction (GlyphAssembly)\ No newline at end of file