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