Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / svg / text / script-tests / lengthAdjust-text-metrics.js
blobfc100d30b88a00972d646b03e5aa306638e0202b
1 // Test that assures that SVGTextContentElement API takes lengthAdjust & co into account.
2 description("Complete test of the SVGTextContentElement API");
4 // Prepare testcase
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();
35     topLeft.x = rect.x;
36     topLeft.y = rect.y;
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;
49     return newRect;
52 // Helper functions
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");
75 debug("");
76 debug("Test getNumberOfChars() API");
77 shouldBe("svgText.getNumberOfChars()", "4");
79 debug("");
80 debug("Test getComputedTextLength() API");
81 shouldBeEqualToString("lengthToString(svgText.getComputedTextLength())", "200.0");
83 debug("");
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");
96 debug("");
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)");
103 debug("");
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)");
110 debug("");
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)");
117 debug("");
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();
125 point.y = 10.0;
127 debug("");
128 debug("Test getCharNumAtPosition() API");
130 point.x = 0.0;
131 point = point.matrixTransform(inverseScaleMatrix);
132 debug("> Testing point=" + pointToString(point));
133 shouldBe("svgText.getCharNumAtPosition(point)", "-1");
135 point.x = 9.9;
136 point = point.matrixTransform(inverseScaleMatrix);
137 debug("> Testing point=" + pointToString(point));
138 shouldBe("svgText.getCharNumAtPosition(point)", "-1");
140 point.x = 10.1;
141 point = point.matrixTransform(inverseScaleMatrix);
142 debug("> Testing point=" + pointToString(point));
143 shouldBe("svgText.getCharNumAtPosition(point)", "0");
145 point.x = 71.4;
146 point = point.matrixTransform(inverseScaleMatrix);
147 debug("> Testing point=" + pointToString(point));
148 shouldBe("svgText.getCharNumAtPosition(point)", "0");
150 point.x = 71.6;
151 point = point.matrixTransform(inverseScaleMatrix);
152 debug("> Testing point=" + pointToString(point));
153 shouldBe("svgText.getCharNumAtPosition(point)", "1");
155 point.x = 127.8;
156 point = point.matrixTransform(inverseScaleMatrix);
157 debug("> Testing point=" + pointToString(point));
158 shouldBe("svgText.getCharNumAtPosition(point)", "1");
160 point.x = 128.0;
161 point = point.matrixTransform(inverseScaleMatrix);
162 debug("> Testing point=" + pointToString(point));
163 shouldBe("svgText.getCharNumAtPosition(point)", "2");
165 point.x = 179.1;
166 point = point.matrixTransform(inverseScaleMatrix);
167 debug("> Testing point=" + pointToString(point));
168 shouldBe("svgText.getCharNumAtPosition(point)", "2");
170 point.x = 179.3;
171 point = point.matrixTransform(inverseScaleMatrix);
172 debug("> Testing point=" + pointToString(point));
173 shouldBe("svgText.getCharNumAtPosition(point)", "3");
175 point.x = 209.9;
176 point = point.matrixTransform(inverseScaleMatrix);
177 debug("> Testing point=" + pointToString(point));
178 shouldBe("svgText.getCharNumAtPosition(point)", "3");
180 point.x = 210.1;
181 point = point.matrixTransform(inverseScaleMatrix);
182 debug("> Testing point=" + pointToString(point));
183 shouldBe("svgText.getCharNumAtPosition(point)", "-1");
185 point.x = 250.0;
186 point = point.matrixTransform(inverseScaleMatrix);
187 debug("> Testing point=" + pointToString(point));
188 shouldBe("svgText.getCharNumAtPosition(point)", "-1");
190 // Cleanup
191 document.documentElement.removeChild(svgRoot);
192 var successfullyParsed = true;