Skip to content

Truly independent web browser

LibWeb: Implement SVGTextContentElement.getCharNumAtPosition()

Andreas Kling11f6b87c9af0
diff --git a/Libraries/LibWeb/SVG/SVGTextContentElement.cpp b/Libraries/LibWeb/SVG/SVGTextContentElement.cpp
index b06e4966498..9f9063ff5ad 100644
--- a/Libraries/LibWeb/SVG/SVGTextContentElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGTextContentElement.cpp
@@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <LibWeb/Bindings/DOMPointReadOnly.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/Geometry/DOMPoint.h>
#include <LibWeb/Geometry/DOMRect.h>
@@ -190,4 +191,38 @@ GC::Ref<SVGAnimatedEnumeration> SVGTextContentElement::length_adjust()
return svg_animated_enumeration_for_attribute(AttributeNames::lengthAdjust, keywords, LENGTHADJUST_SPACING);
}
+// https://svgwg.org/svg2-draft/text.html#__svg__SVGTextContentElement__getCharNumAtPosition
+WebIDL::Long SVGTextContentElement::get_char_num_at_position(Bindings::DOMPointInit const& point)
+{
+ // The getCharNumAtPosition method is used to find which character caused a text glyph to be rendered at a given
+ // position in the coordinate system. Because the relationship between characters and glyphs is not one-to-one, only
+ // the first character of the relevant typographic character is returned. When getCharNumAtPosition(point) is
+ // called, the following steps are run:
+ // 1. Assign an index to each character in the DOM within this element, where the first character has index 0.
+ // NB: Each addressable character has a cell, in order.
+ auto cells = character_cells();
+
+ // 2. Let last be the highest index assigned to a character.
+ // 3. Let charnum be 0.
+ // 4. Let result be -1.
+ WebIDL::Long result = -1;
+
+ // 5. While charnum < last:
+ // NB: The steps never advance charnum, so this visits every character, including the one at index last.
+ for (size_t charnum = 0; charnum < cells.size(); ++charnum) {
+ // 1. If the character at index charnum corresponds to a typographic character and it is the first character in
+ // document order to correspond to that typographic character, and point in this element's coordinate system
+ // is within the glyph cell for the typographic character, then set result to charnum.
+ auto const& cell = cells[charnum];
+ if (!cell.starts_typographic_character)
+ continue;
+ Gfx::FloatRect glyph_cell { cell.x, cell.y, cell.width, cell.height };
+ if (glyph_cell.contains(static_cast<float>(point.x), static_cast<float>(point.y)))
+ result = static_cast<WebIDL::Long>(charnum);
+ }
+
+ // 6. Return result.
+ return result;
+}
+
}
diff --git a/Libraries/LibWeb/SVG/SVGTextContentElement.h b/Libraries/LibWeb/SVG/SVGTextContentElement.h
index 19f0635c69d..592e22d7818 100644
--- a/Libraries/LibWeb/SVG/SVGTextContentElement.h
+++ b/Libraries/LibWeb/SVG/SVGTextContentElement.h
@@ -39,6 +39,7 @@ public:
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::DOMRect>> get_extent_of_char(WebIDL::UnsignedLong charnum);
+ WebIDL::Long get_char_num_at_position(Bindings::DOMPointInit const&);
protected:
SVGTextContentElement(DOM::Document&, DOM::QualifiedName);
diff --git a/Libraries/LibWeb/SVG/SVGTextContentElement.idl b/Libraries/LibWeb/SVG/SVGTextContentElement.idl
index c0e1c5996fd..61f77eaf854 100644
--- a/Libraries/LibWeb/SVG/SVGTextContentElement.idl
+++ b/Libraries/LibWeb/SVG/SVGTextContentElement.idl
@@ -17,7 +17,7 @@ interface SVGTextContentElement : SVGGraphicsElement {
[FIXME] DOMPoint getEndPositionOfChar(unsigned long charnum);
DOMRect getExtentOfChar(unsigned long charnum);
[FIXME] float getRotationOfChar(unsigned long charnum);
- [FIXME] long getCharNumAtPosition(optional DOMPointInit point = {});
+ long getCharNumAtPosition(optional DOMPointInit point = {});
[FIXME] undefined selectSubString(unsigned long charnum, unsigned long nchars);
};
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 3a1ba2b7e6f..084990c247d 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
-1586 Pass
-194 Fail
+1593 Pass
+187 Fail
Pass idl_test setup
Pass idl_test validation
Pass Partial interface Document: original interface defined
@@ -1113,7 +1113,7 @@ Pass SVGTextContentElement interface: operation getStartPositionOfChar(unsigned
Fail SVGTextContentElement interface: operation getEndPositionOfChar(unsigned long)
Pass SVGTextContentElement interface: operation getExtentOfChar(unsigned long)
Fail SVGTextContentElement interface: operation getRotationOfChar(unsigned long)
-Fail SVGTextContentElement interface: operation getCharNumAtPosition(optional DOMPointInit)
+Pass SVGTextContentElement interface: operation getCharNumAtPosition(optional DOMPointInit)
Fail SVGTextContentElement interface: operation selectSubString(unsigned long, unsigned long)
Pass SVGTextPositioningElement interface: existence and properties of interface object
Pass SVGTextPositioningElement interface object length
@@ -1156,8 +1156,8 @@ Pass SVGTextContentElement interface: objects.text must inherit property "getExt
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
Fail SVGTextContentElement interface: calling getRotationOfChar(unsigned long) on objects.text with too few arguments must throw TypeError
-Fail SVGTextContentElement interface: objects.text must inherit property "getCharNumAtPosition(optional DOMPointInit)" with the proper type
-Fail SVGTextContentElement interface: calling getCharNumAtPosition(optional DOMPointInit) on objects.text with too few arguments must throw TypeError
+Pass SVGTextContentElement interface: objects.text must inherit property "getCharNumAtPosition(optional DOMPointInit)" with the proper type
+Pass SVGTextContentElement interface: calling getCharNumAtPosition(optional DOMPointInit) on objects.text with too few arguments must throw TypeError
Fail SVGTextContentElement interface: objects.text must inherit property "selectSubString(unsigned long, unsigned long)" with the proper type
Fail SVGTextContentElement interface: calling selectSubString(unsigned long, unsigned long) on objects.text with too few arguments must throw TypeError
Pass SVGGraphicsElement interface: objects.text must inherit property "transform" with the proper type
@@ -1202,8 +1202,8 @@ Pass SVGTextContentElement interface: objects.tspan must inherit property "getEx
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
Fail SVGTextContentElement interface: calling getRotationOfChar(unsigned long) on objects.tspan with too few arguments must throw TypeError
-Fail SVGTextContentElement interface: objects.tspan must inherit property "getCharNumAtPosition(optional DOMPointInit)" with the proper type
-Fail SVGTextContentElement interface: calling getCharNumAtPosition(optional DOMPointInit) on objects.tspan with too few arguments must throw TypeError
+Pass SVGTextContentElement interface: objects.tspan must inherit property "getCharNumAtPosition(optional DOMPointInit)" with the proper type
+Pass SVGTextContentElement interface: calling getCharNumAtPosition(optional DOMPointInit) on objects.tspan with too few arguments must throw TypeError
Fail SVGTextContentElement interface: objects.tspan must inherit property "selectSubString(unsigned long, unsigned long)" with the proper type
Fail SVGTextContentElement interface: calling selectSubString(unsigned long, unsigned long) on objects.tspan with too few arguments must throw TypeError
Pass SVGGraphicsElement interface: objects.tspan must inherit property "transform" with the proper type
@@ -1269,8 +1269,8 @@ Pass SVGTextContentElement interface: objects.textPath must inherit property "ge
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
Fail SVGTextContentElement interface: calling getRotationOfChar(unsigned long) on objects.textPath with too few arguments must throw TypeError
-Fail SVGTextContentElement interface: objects.textPath must inherit property "getCharNumAtPosition(optional DOMPointInit)" with the proper type
-Fail SVGTextContentElement interface: calling getCharNumAtPosition(optional DOMPointInit) on objects.textPath with too few arguments must throw TypeError
+Pass SVGTextContentElement interface: objects.textPath must inherit property "getCharNumAtPosition(optional DOMPointInit)" with the proper type
+Pass SVGTextContentElement interface: calling getCharNumAtPosition(optional DOMPointInit) on objects.textPath with too few arguments must throw TypeError
Fail SVGTextContentElement interface: objects.textPath must inherit property "selectSubString(unsigned long, unsigned long)" with the proper type
Fail SVGTextContentElement interface: calling selectSubString(unsigned long, unsigned long) on objects.textPath with too few arguments must throw TypeError
Pass SVGGraphicsElement interface: objects.textPath must inherit property "transform" with the proper type