Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / svg / text / text-rect-precision.html
blobf05043d0d2082748b24a5799c688aab6eed4b2d8
1 <html xmlns="http://www.w3.org/1999/xhtml">
2 <head>
3 <script src="../../resources/js-test.js"></script>
4 </head>
5 <body>
6 <svg style="position: absolute; top: 10px; left: 10px; width: 500px; height: 300px;">
7 <g>
8 <text id="test" x="0" y="50" font-size="25" fill="#000" text-rendering="geometricPrecision">Sphinx of black quartz, judge my vow.</text>
9 </g>
10 <g>
11 <text id="measure" x="0" y="150" fill="#000" text-rendering="geometricPrecision" xml:space="preserve">&nbsp;</text>
12 </g>
13 </svg>
14 <script>
16 var hasSubpixelPrecision = false;
18 function subpixelTolerance(testElement)
20 // Due to fixed-point rounding, each single-character measurement may differ by up to
21 // one LayoutUnit (i.e., 0.16 pixel) from the same character's measurement in the full
22 // string.
23 var str = testElement.firstChild.nodeValue;
24 return str.length * 0.16;
27 function measureText(testElement)
29 var measureElement = document.getElementById('measure');
30 measureElement.setAttribute('font-size', testElement.getAttribute('font-size'));
31 var str = testElement.firstChild.nodeValue;
33 var characterWidths = {};
34 var width = 0;
35 for (var i = 0; i < str.length; i++) {
36 var c = str[i];
37 var w = characterWidths[c];
38 if (!w) {
39 measureElement.firstChild.nodeValue = c;
40 w = measureElement.getBoundingClientRect().width;
41 characterWidths[c] = w;
42 hasSubpixelPrecision = hasSubpixelPrecision || w.toFixed(2) != Math.round(w);
44 width += w;
46 return width;
49 var el = document.getElementById('test');
50 var elementWidth = el.getBoundingClientRect().width;
51 var textWidth = measureText(el);
52 var tolerance = hasSubpixelPrecision ? subpixelTolerance(el) : 2; // enclosing may expand up to one pixel in each direction.
53 if (Math.abs(elementWidth - textWidth) <= tolerance)
54 testPassed('Width of text element is the sum of the width of all characters.');
55 else
56 testFailed('Width of text element is ' + elementWidth + ', expected ' + textWidth);
57 </script>
58 </body>
59 </html>