1 // Test that assures that SVGTextContentElement API takes lengthAdjust & co into account.
2 description("Complete test of the SVGTextContentElement API");
5 var svgNS = "http://www.w3.org/2000/svg";
7 var svgRoot = document.createElementNS(svgNS, "svg");
8 document.documentElement.appendChild(svgRoot);
10 var svgText = document.createElementNS(svgNS, "text");
11 svgText.style.fontFamily = "Arial";
12 svgText.style.fontSize = "20px";
13 svgText.setAttribute("x", "10");
14 svgText.setAttribute("y", "20");
15 svgText.setAttribute("lengthAdjust", "spacingAndGlyphs");
16 svgText.setAttribute("textLength", "200");
17 svgText.appendChild(document.createTextNode("Test"));
18 svgRoot.appendChild(svgText);
20 // Build scale matrix, we need to handle the spacingAndGlyphs transformation on our own.
21 var startPosition = svgText.getStartPositionOfChar(0);
22 var scale = svgText.textLength.baseVal.value / svgText.getComputedTextLength();
23 var scaleMatrix = svgRoot.createSVGMatrix()
24 .translate(startPosition.x, startPosition.y)
25 .scaleNonUniform(scale, 1)
26 .translate(-startPosition.x, -startPosition.y);
27 var inverseScaleMatrix = scaleMatrix.inverse();
29 function transformPoint(point, matrix) {
30 return point.matrixTransform(matrix);
33 function transformRect(rect, matrix) {
34 var topLeft = svgRoot.createSVGPoint();
37 topLeft = transformPoint(topLeft, matrix);
39 var bottomRight = svgRoot.createSVGPoint();
40 bottomRight.x = rect.x + rect.width;
41 bottomRight.y = rect.y + rect.height;
42 bottomRight = transformPoint(bottomRight, matrix);
44 var newRect = svgRoot.createSVGRect();
45 newRect.x = topLeft.x;
46 newRect.y = topLeft.y;
47 newRect.width = bottomRight.x - topLeft.x;
48 newRect.height = bottomRight.y - topLeft.y;
53 function numberToString(number) {
54 return number.toFixed(1);
57 function lengthToString(number) {
58 return numberToString(number * scale);
61 function pointToString(point) {
62 point = point.matrixTransform(scaleMatrix);
63 return "(" + numberToString(point.x) + "," + numberToString(point.y) + ")";
66 function rectToString(rect) {
67 rect = transformRect(rect, scaleMatrix);
68 return "(" + numberToString(rect.x) + "," + numberToString(rect.y) + ")-(" + numberToString(rect.width) + "x" + numberToString(rect.height) + ")";
71 debug("Test SVGTextContentElement SVG DOM properties");
72 shouldBeEqualToString("svgText.textLength.baseVal.value.toFixed(1)", "200.0");
73 shouldBe("svgText.lengthAdjust.baseVal", "SVGTextContentElement.LENGTHADJUST_SPACINGANDGLYPHS");
76 debug("Test getNumberOfChars() API");
77 shouldBe("svgText.getNumberOfChars()", "4");
80 debug("Test getComputedTextLength() API");
81 shouldBeEqualToString("lengthToString(svgText.getComputedTextLength())", "200.0");
84 debug("Test getSubStringLength() API");
85 shouldBeEqualToString("lengthToString(svgText.getSubStringLength(0, 1))", "61.5");
86 shouldBeEqualToString("lengthToString(svgText.getSubStringLength(0, 2))", "117.9");
87 shouldBeEqualToString("lengthToString(svgText.getSubStringLength(0, 3))", "169.2");;
88 shouldBeEqualToString("lengthToString(svgText.getSubStringLength(0, 4))", "200.0");
89 shouldBeEqualToString("lengthToString(svgText.getSubStringLength(1, 1))", "56.4");
90 shouldBeEqualToString("lengthToString(svgText.getSubStringLength(1, 2))", "107.7");
91 shouldBeEqualToString("lengthToString(svgText.getSubStringLength(1, 3))", "138.5");
92 shouldBeEqualToString("lengthToString(svgText.getSubStringLength(2, 1))", "51.3");
93 shouldBeEqualToString("lengthToString(svgText.getSubStringLength(2, 2))", "82.1");
94 shouldBeEqualToString("lengthToString(svgText.getSubStringLength(3, 1))", "30.8");
97 debug("Test getStartPositionOfChar() API");
98 shouldBeEqualToString("pointToString(svgText.getStartPositionOfChar(0))", "(10.0,20.0)");
99 shouldBeEqualToString("pointToString(svgText.getStartPositionOfChar(1))", "(71.5,20.0)");
100 shouldBeEqualToString("pointToString(svgText.getStartPositionOfChar(2))", "(127.9,20.0)");
101 shouldBeEqualToString("pointToString(svgText.getStartPositionOfChar(3))", "(179.2,20.0)");
104 debug("Test getEndPositionOfChar() API");
105 shouldBeEqualToString("pointToString(svgText.getEndPositionOfChar(0))", "(71.5,20.0)");
106 shouldBeEqualToString("pointToString(svgText.getEndPositionOfChar(1))", "(127.9,20.0)");
107 shouldBeEqualToString("pointToString(svgText.getEndPositionOfChar(2))", "(179.2,20.0)");
108 shouldBeEqualToString("pointToString(svgText.getEndPositionOfChar(3))", "(210.0,20.0)");
111 debug("Test getExtentOfChar() API");
112 shouldBeEqualToString("rectToString(svgText.getExtentOfChar(0))", "(10.0,1.9)-(61.5x22.3)");
113 shouldBeEqualToString("rectToString(svgText.getExtentOfChar(1))", "(71.5,1.9)-(56.4x22.3)");
114 shouldBeEqualToString("rectToString(svgText.getExtentOfChar(2))", "(127.9,1.9)-(51.3x22.3)");
115 shouldBeEqualToString("rectToString(svgText.getExtentOfChar(3))", "(179.2,1.9)-(30.8x22.3)");
118 debug("Test getRotationOfChar() API");
119 shouldBeEqualToString("svgText.getRotationOfChar(0).toFixed(1)", "0.0");
120 shouldBeEqualToString("svgText.getRotationOfChar(1).toFixed(1)", "0.0");
121 shouldBeEqualToString("svgText.getRotationOfChar(2).toFixed(1)", "0.0");
122 shouldBeEqualToString("svgText.getRotationOfChar(3).toFixed(1)", "0.0");
124 var point = svgRoot.createSVGPoint();
128 debug("Test getCharNumAtPosition() API");
131 point = point.matrixTransform(inverseScaleMatrix);
132 debug("> Testing point=" + pointToString(point));
133 shouldBe("svgText.getCharNumAtPosition(point)", "-1");
136 point = point.matrixTransform(inverseScaleMatrix);
137 debug("> Testing point=" + pointToString(point));
138 shouldBe("svgText.getCharNumAtPosition(point)", "-1");
141 point = point.matrixTransform(inverseScaleMatrix);
142 debug("> Testing point=" + pointToString(point));
143 shouldBe("svgText.getCharNumAtPosition(point)", "0");
146 point = point.matrixTransform(inverseScaleMatrix);
147 debug("> Testing point=" + pointToString(point));
148 shouldBe("svgText.getCharNumAtPosition(point)", "0");
151 point = point.matrixTransform(inverseScaleMatrix);
152 debug("> Testing point=" + pointToString(point));
153 shouldBe("svgText.getCharNumAtPosition(point)", "1");
156 point = point.matrixTransform(inverseScaleMatrix);
157 debug("> Testing point=" + pointToString(point));
158 shouldBe("svgText.getCharNumAtPosition(point)", "1");
161 point = point.matrixTransform(inverseScaleMatrix);
162 debug("> Testing point=" + pointToString(point));
163 shouldBe("svgText.getCharNumAtPosition(point)", "2");
166 point = point.matrixTransform(inverseScaleMatrix);
167 debug("> Testing point=" + pointToString(point));
168 shouldBe("svgText.getCharNumAtPosition(point)", "2");
171 point = point.matrixTransform(inverseScaleMatrix);
172 debug("> Testing point=" + pointToString(point));
173 shouldBe("svgText.getCharNumAtPosition(point)", "3");
176 point = point.matrixTransform(inverseScaleMatrix);
177 debug("> Testing point=" + pointToString(point));
178 shouldBe("svgText.getCharNumAtPosition(point)", "3");
181 point = point.matrixTransform(inverseScaleMatrix);
182 debug("> Testing point=" + pointToString(point));
183 shouldBe("svgText.getCharNumAtPosition(point)", "-1");
186 point = point.matrixTransform(inverseScaleMatrix);
187 debug("> Testing point=" + pointToString(point));
188 shouldBe("svgText.getCharNumAtPosition(point)", "-1");
191 document.documentElement.removeChild(svgRoot);
192 var successfullyParsed = true;