Skip to content

Truly independent web browser

LibWeb: Implement getStartPositionOfChar() and getEndPositionOfChar()

sideshowbarkerdec6194bee43
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
index 0d6b0ab60d7..e9207174f04 100644
--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
@@ -36,6 +36,9 @@ pub struct FfiSvgTextCharacterCell {
pub y: f32,
pub width: f32,
pub height: f32,
+ /// Where the alignment point of the typographic character is along the block axis: The current text position the
+ /// character was placed at, which the dominant baseline of its font is aligned to.
+ pub alignment_point_y: f32,
/// Whether this code unit is the first, in document order, of the typographic character whose
/// cell this is. A later code unit of the same character carries the same cell, unmarked.
pub starts_typographic_character: bool,
@@ -112,6 +115,7 @@ fn append_svg_text_character_cells(
y: top,
width: end_x - first.x - spacing_after_each_character,
height,
+ alignment_point_y: 0.0,
starts_typographic_character: true,
};
cells.push(cell);
@@ -133,6 +137,7 @@ fn append_svg_text_character_cells(
y: top,
width: 0.0,
height,
+ alignment_point_y: 0.0,
starts_typographic_character: false,
},
length_in_code_units - covered,
@@ -2168,6 +2173,9 @@ impl<'pass> SvgFormattingContext<'pass> {
text_offset,
self.text_length_adjustment.spacing(),
);
+ for cell in &mut shaped.character_cells {
+ cell.alignment_point_y = self.current_text_position.y;
+ }
if glyph_scale != 1.0 {
let glyph_cells = shaped.glyph_cells.to_float_rect();
shaped.glyph_cells = float_rect_to_css_pixels(FloatRect::new(
diff --git a/Libraries/LibWeb/SVG/SVGTextContentElement.cpp b/Libraries/LibWeb/SVG/SVGTextContentElement.cpp
index 0ca9f0cdc16..cd589b7a7b0 100644
--- a/Libraries/LibWeb/SVG/SVGTextContentElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGTextContentElement.cpp
@@ -132,10 +132,52 @@ WebIDL::ExceptionOr<float> SVGTextContentElement::get_sub_string_length(WebIDL::
return sub_string_length(cells, charnum, nchars);
}
-GC::Ref<Geometry::DOMPoint> SVGTextContentElement::get_start_position_of_char(WebIDL::UnsignedLong charnum)
+WebIDL::ExceptionOr<GC::Ref<Geometry::DOMPoint>> SVGTextContentElement::get_start_position_of_char(WebIDL::UnsignedLong charnum)
{
- dbgln("(STUBBED) SVGTextContentElement::get_start_position_of_char(charnum={}). Called on: {}", charnum, debug_description());
- return Geometry::DOMPoint::create();
+ // The getStartPositionOfChar method is used to get the position of a typographic character after text layout has
+ // been performed. When getStartPositionOfChar(charnum) is called, the following steps are run:
+ auto cells = character_cells();
+
+ // 1. Let cluster be the result of finding the typographic character for the character at index charnum within the
+ // current element.
+ // 2. If cluster is null, then throw an IndexSizeError.
+ // NB: Every addressable character here corresponds to the typographic character whose cell it carries, so only an
+ // index past the last character finds nothing.
+ if (charnum >= cells.size())
+ return WebIDL::IndexSizeError::create("Character index out of range"_utf16);
+ auto const& cluster = cells[charnum];
+
+ // 3. Let p be the alignment point of the typographic character that correspond to the character at index charnum,
+ // in the coordinate system of the current element.
+ // FIXME: The glyph cells of text on a path are measured before the glyphs are laid along the path, so the point
+ // isn't on the path.
+ // 4. Return a newly created, detached SVGPoint object representing the point p.
+ return Geometry::DOMPoint::create(cluster.x, cluster.alignment_point_y);
+}
+
+// https://svgwg.org/svg2-draft/text.html#__svg__SVGTextContentElement__getEndPositionOfChar
+WebIDL::ExceptionOr<GC::Ref<Geometry::DOMPoint>> SVGTextContentElement::get_end_position_of_char(WebIDL::UnsignedLong charnum)
+{
+ // The getEndPositionOfChar method is used to get the trailing position of a typographic character after text
+ // layout has been performed. When getEndPositionOfChar(charnum) is called, the following steps are run:
+ auto cells = character_cells();
+
+ // 1. Let cluster be the result of finding the typographic character for the character at index charnum within the
+ // current element.
+ // 2. If cluster is null, then throw an IndexSizeError.
+ if (charnum >= cells.size())
+ return WebIDL::IndexSizeError::create("Character index out of range"_utf16);
+ auto const& cluster = cells[charnum];
+
+ // 3. Let p be the alignment point of cluster that correspond to the character at index charnum, in the coordinate
+ // system of the current element.
+ // 4. Let direction be a unit vector in the direction of the cluster's advance. [...]
+ // FIXME: Every advance is taken to run along the x axis, which holds for the left-to-right horizontal text that
+ // layout shapes, off a path.
+ // 5. Let advance be cluster's advance.
+ // 6. Set p to p + advance · direction.
+ // 7. Return a newly created, detached SVGPoint object representing the point p.
+ return Geometry::DOMPoint::create(cluster.x + cluster.width, cluster.alignment_point_y);
}
// https://svgwg.org/svg2-draft/text.html#__svg__SVGTextContentElement__getExtentOfChar
diff --git a/Libraries/LibWeb/SVG/SVGTextContentElement.h b/Libraries/LibWeb/SVG/SVGTextContentElement.h
index 38cf8bc0346..70a7d332387 100644
--- a/Libraries/LibWeb/SVG/SVGTextContentElement.h
+++ b/Libraries/LibWeb/SVG/SVGTextContentElement.h
@@ -45,7 +45,8 @@ public:
WebIDL::ExceptionOr<WebIDL::Long> get_number_of_chars();
WebIDL::ExceptionOr<float> get_computed_text_length();
WebIDL::ExceptionOr<float> get_sub_string_length(WebIDL::UnsignedLong charnum, WebIDL::UnsignedLong nchars);
- GC::Ref<Geometry::DOMPoint> get_start_position_of_char(WebIDL::UnsignedLong charnum);
+ WebIDL::ExceptionOr<GC::Ref<Geometry::DOMPoint>> get_start_position_of_char(WebIDL::UnsignedLong charnum);
+ WebIDL::ExceptionOr<GC::Ref<Geometry::DOMPoint>> get_end_position_of_char(WebIDL::UnsignedLong charnum);
WebIDL::ExceptionOr<GC::Ref<Geometry::DOMRect>> get_extent_of_char(WebIDL::UnsignedLong charnum);
WebIDL::Long get_char_num_at_position(Bindings::DOMPointInit const&);
diff --git a/Libraries/LibWeb/SVG/SVGTextContentElement.idl b/Libraries/LibWeb/SVG/SVGTextContentElement.idl
index 61f77eaf854..a181b4babbd 100644
--- a/Libraries/LibWeb/SVG/SVGTextContentElement.idl
+++ b/Libraries/LibWeb/SVG/SVGTextContentElement.idl
@@ -14,7 +14,7 @@ interface SVGTextContentElement : SVGGraphicsElement {
float getComputedTextLength();
float getSubStringLength(unsigned long charnum, unsigned long nchars);
DOMPoint getStartPositionOfChar(unsigned long charnum);
- [FIXME] DOMPoint getEndPositionOfChar(unsigned long charnum);
+ DOMPoint getEndPositionOfChar(unsigned long charnum);
DOMRect getExtentOfChar(unsigned long charnum);
[FIXME] float getRotationOfChar(unsigned long charnum);
long getCharNumAtPosition(optional DOMPointInit point = {});
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/idlharness.window.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/idlharness.window.txt
index 6a20e69a028..471d2893fb1 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/svg/idlharness.window.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/idlharness.window.txt
@@ -2,8 +2,8 @@ Harness status: OK
Found 1780 tests
-1641 Pass
-139 Fail
+1648 Pass
+132 Fail
Pass idl_test setup
Pass idl_test validation
Pass Partial interface Document: original interface defined
@@ -1110,7 +1110,7 @@ Pass SVGTextContentElement interface: operation getNumberOfChars()
Pass SVGTextContentElement interface: operation getComputedTextLength()
Pass SVGTextContentElement interface: operation getSubStringLength(unsigned long, unsigned long)
Pass SVGTextContentElement interface: operation getStartPositionOfChar(unsigned long)
-Fail SVGTextContentElement interface: operation getEndPositionOfChar(unsigned long)
+Pass SVGTextContentElement interface: operation getEndPositionOfChar(unsigned long)
Pass SVGTextContentElement interface: operation getExtentOfChar(unsigned long)
Fail SVGTextContentElement interface: operation getRotationOfChar(unsigned long)
Pass SVGTextContentElement interface: operation getCharNumAtPosition(optional DOMPointInit)
@@ -1150,8 +1150,8 @@ Pass SVGTextContentElement interface: objects.text must inherit property "getSub
Pass SVGTextContentElement interface: calling getSubStringLength(unsigned long, unsigned long) on objects.text with too few arguments must throw TypeError
Pass SVGTextContentElement interface: objects.text must inherit property "getStartPositionOfChar(unsigned long)" with the proper type
Pass SVGTextContentElement interface: calling getStartPositionOfChar(unsigned long) on objects.text with too few arguments must throw TypeError
-Fail SVGTextContentElement interface: objects.text must inherit property "getEndPositionOfChar(unsigned long)" with the proper type
-Fail SVGTextContentElement interface: calling getEndPositionOfChar(unsigned long) on objects.text with too few arguments must throw TypeError
+Pass SVGTextContentElement interface: objects.text must inherit property "getEndPositionOfChar(unsigned long)" with the proper type
+Pass SVGTextContentElement interface: calling getEndPositionOfChar(unsigned long) on objects.text with too few arguments must throw TypeError
Pass SVGTextContentElement interface: objects.text must inherit property "getExtentOfChar(unsigned long)" with the proper type
Pass SVGTextContentElement interface: calling getExtentOfChar(unsigned long) on objects.text with too few arguments must throw TypeError
Fail SVGTextContentElement interface: objects.text must inherit property "getRotationOfChar(unsigned long)" with the proper type
@@ -1196,8 +1196,8 @@ Pass SVGTextContentElement interface: objects.tspan must inherit property "getSu
Pass SVGTextContentElement interface: calling getSubStringLength(unsigned long, unsigned long) on objects.tspan with too few arguments must throw TypeError
Pass SVGTextContentElement interface: objects.tspan must inherit property "getStartPositionOfChar(unsigned long)" with the proper type
Pass SVGTextContentElement interface: calling getStartPositionOfChar(unsigned long) on objects.tspan with too few arguments must throw TypeError
-Fail SVGTextContentElement interface: objects.tspan must inherit property "getEndPositionOfChar(unsigned long)" with the proper type
-Fail SVGTextContentElement interface: calling getEndPositionOfChar(unsigned long) on objects.tspan with too few arguments must throw TypeError
+Pass SVGTextContentElement interface: objects.tspan must inherit property "getEndPositionOfChar(unsigned long)" with the proper type
+Pass SVGTextContentElement interface: calling getEndPositionOfChar(unsigned long) on objects.tspan with too few arguments must throw TypeError
Pass SVGTextContentElement interface: objects.tspan must inherit property "getExtentOfChar(unsigned long)" with the proper type
Pass SVGTextContentElement interface: calling getExtentOfChar(unsigned long) on objects.tspan with too few arguments must throw TypeError
Fail SVGTextContentElement interface: objects.tspan must inherit property "getRotationOfChar(unsigned long)" with the proper type
@@ -1263,8 +1263,8 @@ Pass SVGTextContentElement interface: objects.textPath must inherit property "ge
Pass SVGTextContentElement interface: calling getSubStringLength(unsigned long, unsigned long) on objects.textPath with too few arguments must throw TypeError
Pass SVGTextContentElement interface: objects.textPath must inherit property "getStartPositionOfChar(unsigned long)" with the proper type
Pass SVGTextContentElement interface: calling getStartPositionOfChar(unsigned long) on objects.textPath with too few arguments must throw TypeError
-Fail SVGTextContentElement interface: objects.textPath must inherit property "getEndPositionOfChar(unsigned long)" with the proper type
-Fail SVGTextContentElement interface: calling getEndPositionOfChar(unsigned long) on objects.textPath with too few arguments must throw TypeError
+Pass SVGTextContentElement interface: objects.textPath must inherit property "getEndPositionOfChar(unsigned long)" with the proper type
+Pass SVGTextContentElement interface: calling getEndPositionOfChar(unsigned long) on objects.textPath with too few arguments must throw TypeError
Pass SVGTextContentElement interface: objects.textPath must inherit property "getExtentOfChar(unsigned long)" with the proper type
Pass SVGTextContentElement interface: calling getExtentOfChar(unsigned long) on objects.textPath with too few arguments must throw TypeError
Fail SVGTextContentElement interface: objects.textPath must inherit property "getRotationOfChar(unsigned long)" with the proper type
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/text/scripted/getstartpositionofchar-dominant-baseline-shift.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/text/scripted/getstartpositionofchar-dominant-baseline-shift.txt
new file mode 100644
index 00000000000..66f1a564add
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/text/scripted/getstartpositionofchar-dominant-baseline-shift.txt
@@ -0,0 +1,9 @@
+Harness status: OK
+
+Found 4 tests
+
+4 Pass
+Pass Horizontal: getStartPositionOfChar/getEndPositionOfChar not affected by inherited dominant-baseline
+Pass Horizontal: getStartPositionOfChar/getEndPositionOfChar not affected by explicit dominant-baseline on tspan
+Pass Vertical: getStartPositionOfChar/getEndPositionOfChar not affected by dominant-baseline
+Pass Horizontal: x position unaffected by dominant-baseline
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/text/scripted/getstartpositionofchar-dominant-baseline.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/text/scripted/getstartpositionofchar-dominant-baseline.txt
new file mode 100644
index 00000000000..3ab6a120c8e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/text/scripted/getstartpositionofchar-dominant-baseline.txt
@@ -0,0 +1,7 @@
+Harness status: OK
+
+Found 2 tests
+
+2 Pass
+Pass textPath: get*PositionOfChar().y should not depend on dominant-baseline
+Pass tspan: get*PositionOfChar().y should not depend on dominant-baseline
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/text/scripted/getstartpositionofchar.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/text/scripted/getstartpositionofchar.txt
new file mode 100644
index 00000000000..4747b39027f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/text/scripted/getstartpositionofchar.txt
@@ -0,0 +1,11 @@
+Harness status: OK
+
+Found 5 tests
+
+1 Pass
+4 Fail
+Fail vertical-rl
+Fail vertical-lr
+Fail sideways-rl
+Fail sideways-lr
+Pass tspan
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/text/scripted/lengthadjust-char-positions.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/text/scripted/lengthadjust-char-positions.txt
new file mode 100644
index 00000000000..87f1f261228
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/text/scripted/lengthadjust-char-positions.txt
@@ -0,0 +1,13 @@
+Harness status: OK
+
+Found 7 tests
+
+6 Pass
+1 Fail
+Pass control: without textLength the advances are one em each
+Pass lengthAdjust="spacing" moves the characters apart and leaves each advance alone
+Pass lengthAdjust="spacingAndGlyphs" scales the advances, the positions and the extents
+Fail lengthAdjust="spacingAndGlyphs" does not rotate or change the height of a character
+Pass getSubStringLength sums the scaled advances under lengthAdjust="spacingAndGlyphs"
+Pass getCharNumAtPosition finds the characters where lengthAdjust="spacing" put them
+Pass getCharNumAtPosition finds the stretched characters under lengthAdjust="spacingAndGlyphs"
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/text/scripted/textlength-inconsistent.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/text/scripted/textlength-inconsistent.txt
new file mode 100644
index 00000000000..b91a599bd3e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/text/scripted/textlength-inconsistent.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass The first characters should be on the left of the second character.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/text/scripted/tspan-xml-space-preserve-position.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/text/scripted/tspan-xml-space-preserve-position.txt
new file mode 100644
index 00000000000..d4a5b0cc189
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/text/scripted/tspan-xml-space-preserve-position.txt
@@ -0,0 +1,9 @@
+Harness status: OK
+
+Found 4 tests
+
+4 Pass
+Pass First tspan starts at its specified x/y
+Pass Second tspan starts at its specified x/y, not offset by inter-tspan whitespace
+Pass Second character of multi-char tspan follows naturally without position shift
+Pass Third tspan starts at its specified x/y
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/svg/text/scripted/getstartpositionofchar-dominant-baseline-shift.html b/Tests/LibWeb/Text/input/wpt-import/svg/text/scripted/getstartpositionofchar-dominant-baseline-shift.html
new file mode 100644
index 00000000000..aa75c067d4d
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/svg/text/scripted/getstartpositionofchar-dominant-baseline-shift.html
@@ -0,0 +1,72 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>SVG getStartPositionOfChar with dominant-baseline should return unshifted position</title>
+<link rel="help" href="https://w3c.github.io/svgwg/svg2-draft/text.html#__svg__SVGTextContentElement__getStartPositionOfChar">
+<link rel="help" href="https://drafts.csswg.org/css-inline-3/#propdef-dominant-baseline">
+<link rel="help" href="https://bugs.webkit.org/show_bug.cgi?id=311553">
+<meta name="assert" content="getStartPositionOfChar and getEndPositionOfChar should return positions based on the y attribute value, not shifted by dominant-baseline.">
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+
+<svg width="400" height="400" style="font-family: serif; font-size: 60px;">
+ <!-- Horizontal text: auto vs middle baseline -->
+ <text>
+ <tspan id="h-auto" x="20" y="80">T</tspan>
+ </text>
+ <text dominant-baseline="middle">
+ <tspan id="h-middle" x="20" y="80">T</tspan>
+ </text>
+
+ <!-- Horizontal text: explicit on tspan -->
+ <text>
+ <tspan id="h-explicit-auto" x="20" y="160">T</tspan>
+ </text>
+ <text>
+ <tspan id="h-explicit-middle" x="20" y="160" dominant-baseline="middle">T</tspan>
+ </text>
+
+ <!-- Vertical text: auto vs middle baseline -->
+ <text writing-mode="tb">
+ <tspan id="v-auto" x="300" y="20">T</tspan>
+ </text>
+ <text writing-mode="tb" dominant-baseline="middle">
+ <tspan id="v-middle" x="300" y="20">T</tspan>
+ </text>
+</svg>
+
+<script>
+test(() => {
+ const auto = document.getElementById('h-auto');
+ const middle = document.getElementById('h-middle');
+ assert_equals(auto.getStartPositionOfChar(0).y, middle.getStartPositionOfChar(0).y,
+ 'getStartPositionOfChar().y should not depend on inherited dominant-baseline');
+ assert_equals(auto.getEndPositionOfChar(0).y, middle.getEndPositionOfChar(0).y,
+ 'getEndPositionOfChar().y should not depend on inherited dominant-baseline');
+}, 'Horizontal: getStartPositionOfChar/getEndPositionOfChar not affected by inherited dominant-baseline');
+
+test(() => {
+ const auto = document.getElementById('h-explicit-auto');
+ const middle = document.getElementById('h-explicit-middle');
+ assert_equals(auto.getStartPositionOfChar(0).y, middle.getStartPositionOfChar(0).y,
+ 'getStartPositionOfChar().y should not depend on explicit dominant-baseline');
+ assert_equals(auto.getEndPositionOfChar(0).y, middle.getEndPositionOfChar(0).y,
+ 'getEndPositionOfChar().y should not depend on explicit dominant-baseline');
+}, 'Horizontal: getStartPositionOfChar/getEndPositionOfChar not affected by explicit dominant-baseline on tspan');
+
+test(() => {
+ const auto = document.getElementById('v-auto');
+ const middle = document.getElementById('v-middle');
+ assert_equals(auto.getStartPositionOfChar(0).x, middle.getStartPositionOfChar(0).x,
+ 'getStartPositionOfChar().x should not depend on dominant-baseline in vertical text');
+ assert_equals(auto.getEndPositionOfChar(0).x, middle.getEndPositionOfChar(0).x,
+ 'getEndPositionOfChar().x should not depend on dominant-baseline in vertical text');
+}, 'Vertical: getStartPositionOfChar/getEndPositionOfChar not affected by dominant-baseline');
+
+test(() => {
+ // Verify the x position is also unaffected in horizontal mode
+ const auto = document.getElementById('h-auto');
+ const middle = document.getElementById('h-middle');
+ assert_equals(auto.getStartPositionOfChar(0).x, middle.getStartPositionOfChar(0).x,
+ 'getStartPositionOfChar().x should be the same regardless of dominant-baseline');
+}, 'Horizontal: x position unaffected by dominant-baseline');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/svg/text/scripted/getstartpositionofchar-dominant-baseline.html b/Tests/LibWeb/Text/input/wpt-import/svg/text/scripted/getstartpositionofchar-dominant-baseline.html
new file mode 100644
index 00000000000..54c696eb334
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/svg/text/scripted/getstartpositionofchar-dominant-baseline.html
@@ -0,0 +1,49 @@
+<meta charset="utf-8">
+<title>SVGTextContentElement.getStartPositionOfChar and dominant-baseline</title>
+<link rel="help" href="https://w3c.github.io/svgwg/svg2-draft/text.html#__svg__SVGTextContentElement__getStartPositionOfChar">
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+
+<style>
+text {
+ font-family: Arial;
+ font-size: 60px;
+ fill: #ffff00;
+}
+</style>
+<svg>
+ <defs>
+ <path id="path" d="M20 80h120"></path>
+ </defs>
+ <g>
+ <text>
+ <textPath id="textPath1" href="#path">Test</textPath>
+ <tspan id="tspan1" x="0" y="100">Test</tspan>
+ </text>
+ </g>
+ <g transform="translate(140 0)">
+ <text dominant-baseline="middle">
+ <textPath id="textPath2" href="#path">Test</textPath>
+ <tspan id="tspan2" x="0" y="100">Test</tspan>
+ </text>
+ </g>
+</svg>
+
+<script>
+function $(sel) { return document.querySelector(sel); }
+
+test(() => {
+ assert_equals($('#textPath1').getStartPositionOfChar(0).y,
+ $('#textPath2').getStartPositionOfChar(0).y);
+ assert_equals($('#textPath1').getEndPositionOfChar(0).y,
+ $('#textPath2').getEndPositionOfChar(0).y);
+}, 'textPath: get*PositionOfChar().y should not depend on dominant-baseline');
+
+test(() => {
+ assert_equals($('#tspan1').getStartPositionOfChar(0).y,
+ $('#tspan2').getStartPositionOfChar(0).y);
+ assert_equals($('#tspan1').getEndPositionOfChar(0).y,
+ $('#tspan2').getEndPositionOfChar(0).y);
+}, 'tspan: get*PositionOfChar().y should not depend on dominant-baseline');
+</script>
+
diff --git a/Tests/LibWeb/Text/input/wpt-import/svg/text/scripted/getstartpositionofchar.html b/Tests/LibWeb/Text/input/wpt-import/svg/text/scripted/getstartpositionofchar.html
new file mode 100644
index 00000000000..b990acea0e4
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/svg/text/scripted/getstartpositionofchar.html
@@ -0,0 +1,92 @@
+<!DOCTYPE html>
+<title>SVGTextContentElement.getStartPositionOfChar and getEndPositionOfChar</title>
+<link rel="help" href="https://w3c.github.io/svgwg/svg2-draft/text.html#__svg__SVGTextContentElement__getStartPositionOfChar">
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+<link rel="stylesheet" href="../../../fonts/ahem.css">
+<style>
+text {
+ font: 10px/1 Ahem;
+}
+#vrl {
+ writing-mode: vertical-rl;
+}
+#vlr {
+ writing-mode: vertical-lr;
+}
+#srl {
+ writing-mode: sideways-rl;
+}
+#slr {
+ writing-mode: sideways-lr;
+}
+</style>
+
+<svg>
+ <text id="vrl" x="100" y="100">ABC</text>
+ <text id="vlr" x="100" y="100">ABC</text>
+ <text id="srl" x="100" y="100">ABC</text>
+ <text id="slr" x="100" y="100">ABC</text>
+ <text x="100" y="100">A<tspan id="tsp">B</tspan>C</text>
+</svg>
+
+<script>
+const FONT_SIZE = 10;
+function $(sel) { return document.querySelector(sel); }
+
+setup({ explicit_done: true });
+
+document.fonts.ready.then(() => {
+ test(() => {
+ const target = $('#vrl');
+ const base = target.getStartPositionOfChar(0).y;
+ assert_equals(target.getEndPositionOfChar(0).y, base + FONT_SIZE);
+ assert_equals(target.getStartPositionOfChar(1).y, base + FONT_SIZE);
+ assert_equals(target.getEndPositionOfChar(1).y, base + FONT_SIZE * 2);
+ assert_equals(target.getStartPositionOfChar(2).y, base + FONT_SIZE * 2);
+ assert_equals(target.getEndPositionOfChar(2).y, base + FONT_SIZE * 3);
+ assert_throws_dom("IndexSizeError", () => target.getStartPositionOfChar(3));
+ assert_throws_dom("IndexSizeError", () => target.getEndPositionOfChar(3));
+ }, 'vertical-rl');
+
+ test(() => {
+ const target = $('#vlr');
+ const base = target.getStartPositionOfChar(0).y;
+ assert_equals(target.getEndPositionOfChar(0).y, base + FONT_SIZE);
+ assert_equals(target.getStartPositionOfChar(1).y, base + FONT_SIZE);
+ assert_equals(target.getEndPositionOfChar(1).y, base + FONT_SIZE * 2);
+ assert_equals(target.getStartPositionOfChar(2).y, base + FONT_SIZE * 2);
+ assert_equals(target.getEndPositionOfChar(2).y, base + FONT_SIZE * 3);
+ }, 'vertical-lr');
+
+ test(() => {
+ const target = $('#srl');
+ const base = target.getStartPositionOfChar(0).y;
+ assert_equals(target.getEndPositionOfChar(0).y, base + FONT_SIZE);
+ assert_equals(target.getStartPositionOfChar(1).y, base + FONT_SIZE);
+ assert_equals(target.getEndPositionOfChar(1).y, base + FONT_SIZE * 2);
+ assert_equals(target.getStartPositionOfChar(2).y, base + FONT_SIZE * 2);
+ assert_equals(target.getEndPositionOfChar(2).y, base + FONT_SIZE * 3);
+ }, 'sideways-rl');
+
+ test(() => {
+ const target = $('#slr');
+ const base = target.getStartPositionOfChar(0).y;
+ assert_equals(target.getEndPositionOfChar(0).y, base - FONT_SIZE);
+ assert_equals(target.getStartPositionOfChar(1).y, base - FONT_SIZE);
+ assert_equals(target.getEndPositionOfChar(1).y, base - FONT_SIZE * 2);
+ assert_equals(target.getStartPositionOfChar(2).y, base - FONT_SIZE * 2);
+ assert_equals(target.getEndPositionOfChar(2).y, base - FONT_SIZE * 3);
+ }, 'sideways-lr');
+
+ test(() => {
+ const target = $('#tsp');
+ const base = target.parentElement.getStartPositionOfChar(0).y;
+ assert_equals(target.getStartPositionOfChar(0).x, base + FONT_SIZE);
+ assert_equals(target.getEndPositionOfChar(0).x, base + FONT_SIZE * 2);
+ assert_throws_dom("IndexSizeError", () => target.getStartPositionOfChar(1));
+ assert_throws_dom("IndexSizeError", () => target.getEndPositionOfChar(1));
+ }, 'tspan');
+ done();
+});
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/svg/text/scripted/lengthadjust-char-positions.html b/Tests/LibWeb/Text/input/wpt-import/svg/text/scripted/lengthadjust-char-positions.html
new file mode 100644
index 00000000000..0489b561224
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/svg/text/scripted/lengthadjust-char-positions.html
@@ -0,0 +1,123 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>Character positions and extents under lengthAdjust</title>
+<link rel="help" href="https://w3c.github.io/svgwg/svg2-draft/text.html#TextElementLengthAdjustAttribute">
+<link rel="help" href="https://w3c.github.io/svgwg/svg2-draft/text.html#__svg__SVGTextContentElement__getStartPositionOfChar">
+<link rel="help" href="https://w3c.github.io/svgwg/svg2-draft/text.html#__svg__SVGTextContentElement__getEndPositionOfChar">
+<link rel="help" href="https://w3c.github.io/svgwg/svg2-draft/text.html#__svg__SVGTextContentElement__getExtentOfChar">
+<link rel="help" href="https://w3c.github.io/svgwg/svg2-draft/text.html#__svg__SVGTextContentElement__getCharNumAtPosition">
+<link rel="stylesheet" href="../../../fonts/ahem.css">
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+
+<svg width="400" height="200">
+<style>
+text {
+ font: 20px/1 Ahem;
+}
+</style>
+<text id="natural" x="0" y="40">ABCD</text>
+<text id="spacing" x="0" y="80" textLength="200">ABCD</text>
+<text id="glyphs" x="0" y="120" textLength="200" lengthAdjust="spacingAndGlyphs">ABCD</text>
+</svg>
+
+<script>
+// Ahem advances one em per character, so "ABCD" at 20px is naturally 80 units wide and
+// every number below is exact. textLength="200" asks for a 2.5x stretch.
+const EPSILON = 1 / 64;
+
+promise_setup(() => document.fonts.ready);
+
+promise_test(async () => {
+ const text = document.getElementById('natural');
+ assert_equals(text.getNumberOfChars(), 4);
+ assert_approx_equals(text.getComputedTextLength(), 80, EPSILON);
+ for (let i = 0; i < 4; i++) {
+ assert_approx_equals(text.getStartPositionOfChar(i).x, i * 20, EPSILON, 'start of char ' + i);
+ assert_approx_equals(text.getExtentOfChar(i).width, 20, EPSILON, 'extent of char ' + i);
+ }
+ assert_approx_equals(text.getEndPositionOfChar(3).x, 80, EPSILON);
+}, 'control: without textLength the advances are one em each');
+
+promise_test(async () => {
+ const text = document.getElementById('spacing');
+ // 'spacing' distributes the extra 120 units into the 3 gaps, 40 each. The glyphs keep
+ // their own advance, so getComputedTextLength stays at the natural 80.
+ assert_approx_equals(text.getComputedTextLength(), 80, EPSILON);
+ for (let i = 0; i < 4; i++) {
+ assert_approx_equals(text.getStartPositionOfChar(i).x, i * 60, EPSILON, 'start of char ' + i);
+ assert_approx_equals(text.getEndPositionOfChar(i).x, i * 60 + 20, EPSILON, 'end of char ' + i);
+ assert_approx_equals(text.getExtentOfChar(i).x, i * 60, EPSILON, 'extent x of char ' + i);
+ assert_approx_equals(text.getExtentOfChar(i).width, 20, EPSILON, 'extent of char ' + i);
+ }
+ assert_approx_equals(text.getEndPositionOfChar(3).x, 200, EPSILON);
+}, 'lengthAdjust="spacing" moves the characters apart and leaves each advance alone');
+
+promise_test(async () => {
+ const text = document.getElementById('glyphs');
+ // 'spacingAndGlyphs' scales the glyphs themselves, so each advance really is 2.5x wider
+ // and the sum of the advances is the requested textLength.
+ assert_approx_equals(text.getComputedTextLength(), 200, EPSILON);
+ for (let i = 0; i < 4; i++) {
+ assert_approx_equals(text.getStartPositionOfChar(i).x, i * 50, EPSILON, 'start of char ' + i);
+ assert_approx_equals(text.getEndPositionOfChar(i).x, (i + 1) * 50, EPSILON, 'end of char ' + i);
+ assert_approx_equals(text.getExtentOfChar(i).x, i * 50, EPSILON, 'extent x of char ' + i);
+ assert_approx_equals(text.getExtentOfChar(i).width, 50, EPSILON, 'extent of char ' + i);
+ }
+}, 'lengthAdjust="spacingAndGlyphs" scales the advances, the positions and the extents');
+
+promise_test(async () => {
+ const text = document.getElementById('glyphs');
+ // The stretch is along the inline axis only, so nothing is rotated and the glyph boxes
+ // keep their height.
+ const natural = document.getElementById('natural');
+ for (let i = 0; i < 4; i++) {
+ assert_equals(text.getRotationOfChar(i), 0, 'rotation of char ' + i);
+ assert_approx_equals(text.getExtentOfChar(i).height, natural.getExtentOfChar(i).height, EPSILON, 'height of char ' + i);
+ }
+}, 'lengthAdjust="spacingAndGlyphs" does not rotate or change the height of a character');
+
+promise_test(async () => {
+ const text = document.getElementById('glyphs');
+ assert_approx_equals(text.getSubStringLength(0, 1), 50, EPSILON);
+ assert_approx_equals(text.getSubStringLength(1, 2), 100, EPSILON);
+ assert_approx_equals(text.getSubStringLength(0, 4), 200, EPSILON);
+}, 'getSubStringLength sums the scaled advances under lengthAdjust="spacingAndGlyphs"');
+
+// getCharNumAtPosition takes a point in the same space the methods above report, so the
+// stretch has to be applied to the glyph boxes it tests against too. Only points that land
+// squarely on a glyph are asked about: what happens in the gap that lengthAdjust="spacing"
+// opens up between two characters is not something the specification settles.
+function charAt(text, x) {
+ const point = document.querySelector('svg').createSVGPoint();
+ point.x = x;
+ point.y = text.getExtentOfChar(0).y + text.getExtentOfChar(0).height / 2;
+ return text.getCharNumAtPosition(point);
+}
+
+promise_test(async () => {
+ const text = document.getElementById('spacing');
+ // Characters keep their 20 unit advance and sit at 0, 60, 120 and 180.
+ assert_equals(charAt(text, 10), 0);
+ assert_equals(charAt(text, 70), 1);
+ assert_equals(charAt(text, 130), 2);
+ assert_equals(charAt(text, 190), 3);
+ assert_equals(charAt(text, -1), -1);
+ assert_equals(charAt(text, 201), -1);
+}, 'getCharNumAtPosition finds the characters where lengthAdjust="spacing" put them');
+
+promise_test(async () => {
+ const text = document.getElementById('glyphs');
+ // Characters are 50 wide and sit at 0, 50, 100 and 150.
+ assert_equals(charAt(text, 1), 0);
+ assert_equals(charAt(text, 49), 0);
+ assert_equals(charAt(text, 51), 1);
+ assert_equals(charAt(text, 99), 1);
+ assert_equals(charAt(text, 101), 2);
+ assert_equals(charAt(text, 149), 2);
+ assert_equals(charAt(text, 151), 3);
+ assert_equals(charAt(text, 199), 3);
+ assert_equals(charAt(text, -1), -1);
+ assert_equals(charAt(text, 201), -1);
+}, 'getCharNumAtPosition finds the stretched characters under lengthAdjust="spacingAndGlyphs"');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/svg/text/scripted/textlength-inconsistent.svg b/Tests/LibWeb/Text/input/wpt-import/svg/text/scripted/textlength-inconsistent.svg
new file mode 100644
index 00000000000..f9c7bfb89da
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/svg/text/scripted/textlength-inconsistent.svg
@@ -0,0 +1,19 @@
+<svg xmlns="http://www.w3.org/2000/svg"
+ xmlns:html="http://www.w3.org/1999/xhtml">
+ <title>Inconsistent two 'textLength' attributes</title>
+ <html:script src="../../../resources/testharness.js"/>
+ <html:script src="../../../resources/testharnessreport.js"/>
+ <html:link rel="help" href="https://crbug.com/1399351"/>
+ <g>
+ <text textLength="150" x="10" y="100">
+ <tspan textLength="300">Scalable Vector</tspan>
+ </text>
+ </g>
+ <html:script>
+test(() => {
+ const tspan = document.querySelector('tspan');
+ assert_less_than(tspan.getStartPositionOfChar(0).x,
+ tspan.getStartPositionOfChar(1).x);
+}, 'The first characters should be on the left of the second character.');
+ </html:script>
+</svg>
diff --git a/Tests/LibWeb/Text/input/wpt-import/svg/text/scripted/tspan-xml-space-preserve-position.html b/Tests/LibWeb/Text/input/wpt-import/svg/text/scripted/tspan-xml-space-preserve-position.html
new file mode 100644
index 00000000000..9cf340dd5c2
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/svg/text/scripted/tspan-xml-space-preserve-position.html
@@ -0,0 +1,52 @@
+<!DOCTYPE html>
+<title>tspan positioning with xml:space="preserve" and whitespace between tspans</title>
+<link rel="help" href="https://w3c.github.io/svgwg/svg2-draft/text.html#WhiteSpace">
+<link rel="help" href="https://bugs.webkit.org/show_bug.cgi?id=311185">
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+<link rel="stylesheet" href="../../../fonts/ahem.css">
+<style>
+text { font: 10px/1 Ahem; }
+</style>
+
+<svg width="200" height="200">
+ <text>
+ <tspan id="t1" x="10" y="20" xml:space="preserve">A</tspan>
+ <tspan id="t2" x="10" y="40" xml:space="preserve">BC</tspan>
+ <tspan id="t3" x="10" y="60" xml:space="preserve">D</tspan>
+ </text>
+</svg>
+
+<script>
+setup({ explicit_done: true });
+
+document.fonts.ready.then(() => {
+ test(() => {
+ const t1 = document.getElementById('t1');
+ assert_equals(t1.getStartPositionOfChar(0).x, 10, 'tspan 1 char 0 x');
+ assert_equals(t1.getStartPositionOfChar(0).y, 20, 'tspan 1 char 0 y');
+ }, 'First tspan starts at its specified x/y');
+
+ test(() => {
+ const t2 = document.getElementById('t2');
+ assert_equals(t2.getStartPositionOfChar(0).x, 10, 'tspan 2 char 0 x');
+ assert_equals(t2.getStartPositionOfChar(0).y, 40, 'tspan 2 char 0 y');
+ }, 'Second tspan starts at its specified x/y, not offset by inter-tspan whitespace');
+
+ test(() => {
+ const t2 = document.getElementById('t2');
+ const pos0 = t2.getStartPositionOfChar(0);
+ const pos1 = t2.getStartPositionOfChar(1);
+ assert_equals(pos1.x, pos0.x + 10, 'tspan 2 char 1 x');
+ assert_equals(pos1.y, 40, 'tspan 2 char 1 y');
+ }, 'Second character of multi-char tspan follows naturally without position shift');
+
+ test(() => {
+ const t3 = document.getElementById('t3');
+ assert_equals(t3.getStartPositionOfChar(0).x, 10, 'tspan 3 char 0 x');
+ assert_equals(t3.getStartPositionOfChar(0).y, 60, 'tspan 3 char 0 y');
+ }, 'Third tspan starts at its specified x/y');
+
+ done();
+});
+</script>