text-orientation.js (7904B)
1 (function () { 2 var log = function () {}; 3 4 function OrientationTester(container, orientation) { 5 this.container = container; 6 this.setOrientation(orientation); 7 } 8 extend(OrientationTester.prototype, { 9 setOrientation: function (orientation) { 10 this.orientation = orientation; 11 }, 12 measure: function (results) { 13 this.results = results; 14 this._measureNode(this.container); 15 }, 16 _measureNode: function (node, block) { 17 switch (node.nodeType) { 18 case Node.ELEMENT_NODE: 19 var blockOverride = node.dataset.block; 20 if (blockOverride) 21 block = blockOverride; 22 var nodes = node.childNodes; 23 for (var i = 0; i < nodes.length; i++) 24 this._measureNode(nodes[i], block); 25 return; 26 case Node.TEXT_NODE: 27 break; 28 default: 29 return; 30 } 31 32 if (this.orientation == "R") { 33 var advanceExpected = 8; 34 var advanceFailed = 4; 35 } else { 36 advanceExpected = 4; 37 advanceFailed = 8; 38 } 39 40 var range = document.createRange(); 41 var text = node.textContent; 42 for (var ich = 0; ich < text.length; ich++) { 43 var code = text.charCodeAt(ich); 44 if (code == 10 || code == 13) 45 continue; 46 range.setStart(node, ich); 47 if (code >= 0xD800 && code <= 0xDBFF) { 48 var next = text.charCodeAt(ich+1); 49 if (next >= 0xDC00 && next <= 0xDFFF) { 50 ich++; 51 code = ((code & 0x3FF) << 10) + (next & 0x3FF) + 0x10000; 52 } 53 } 54 range.setEnd(node, ich + 1); 55 rect = range.getBoundingClientRect(); 56 if (rect.width == 16) { 57 if (rect.height == advanceExpected) { 58 this.results.passCount++; 59 continue; 60 } 61 //log("U+" + stringFromUnicode(code) + " " + rect.width + "x" + rect.height); 62 if (rect.height == advanceFailed) { 63 this.results.failed(this, code, block); 64 continue; 65 } 66 } 67 this.results.inconclusive(this, code, block, rect); 68 } 69 }}); 70 71 function Results(name) { 72 this.details = document.createElement("details"); 73 this.summary = appendChildElement(this.details, "summary"); 74 this.summary.textContent = name; 75 var typeList = appendChildElement(this.details, "ul"); 76 this.failList = appendChildElement(appendChildElement(typeList, "li", "Failures"), "ol"); 77 this.inconclusiveList = appendChildElement(appendChildElement(typeList, "li", "Inconclusives"), "ol"); 78 this.passCount = 0; 79 this.failCount = 0; 80 this.inconclusiveCount = 0; 81 } 82 extend(Results.prototype, { 83 failed: function (test, code, block) { 84 this.failCount++; 85 this.append(this.failList, test, code, block); 86 }, 87 inconclusive: function (test, code, block, rect) { 88 this.inconclusiveCount++; 89 this.append(this.inconclusiveList, test, code, block, " but inconclusive (rendered as " + rect.width + "x" + rect.height + ")"); 90 }, 91 append: function (list, test, code, block, message) { 92 var text = stringFromUnicode(code) + " should be " + test.orientation; 93 if (block) 94 text = block + ": " + text; 95 if (message) 96 text += message; 97 appendChildElement(list, "li", text); 98 }, 99 done: function (test) { 100 this.summary.textContent += " (" + this.passCount + " passes, " + 101 this.failCount + " fails, " + 102 this.inconclusiveCount + " inconclusives)"; 103 details.appendChild(this.details); 104 assert_equals(this.failCount, 0, "Fail count"); 105 assert_greater_than(this.passCount, 0, "Pass count"); 106 test.done(); 107 }}); 108 109 function Runner() { 110 var nodes = document.querySelectorAll("div[data-vo]"); 111 this.testers = []; 112 for (var i = 0; i < nodes.length; i++) { 113 var node = nodes[i]; 114 var vo = node.dataset.vo; 115 var tester = new OrientationTester(node, vo); 116 tester.test = async_test("Default orientation for vo=" + vo); 117 this.testers.push(tester); 118 } 119 this.testU = async_test("Orientation=Upright"); 120 this.testR = async_test("Orientation=Rotated"); 121 } 122 extend(Runner.prototype, { 123 run: function () { 124 log("Started"); 125 var start = new Date; 126 127 for (var i = 0; i < this.testers.length; i++) { 128 var tester = this.testers[i]; 129 var test = tester.test; 130 test.step(function () { 131 var results = new Results(test.name); 132 tester.measure(results); 133 results.done(test); 134 }); 135 } 136 this.runOrientation(this.testU, "U"); 137 this.runOrientation(this.testR, "R"); 138 139 log("Elapsed " + (new Date() - start)); 140 done(); 141 }, 142 runOrientation: function (test, orientation) { 143 container.classList.add(orientation); 144 var results = new Results(test.name); 145 var me = this; 146 test.step(function () { 147 for (var i = 0; i < me.testers.length; i++) { 148 var tester = me.testers[i]; 149 tester.setOrientation(orientation); 150 tester.measure(results); 151 } 152 results.done(test); 153 }) 154 container.classList.remove(orientation); 155 }}); 156 157 setup({explicit_done:true, explicit_timeout:true}); 158 var runner = new Runner(); 159 window.onload = function () { 160 if (window.location.search == "?wait") { 161 log("Sleeping 5 secs for debug purpose"); 162 return setTimeout(run, 5000); 163 } 164 run(); 165 } 166 167 function run() { 168 onFontReady("16px orientation", function () { runner.run(); }); 169 } 170 171 function onFontReady(font, func) { 172 log("Waiting test fonts to load"); 173 if (document.fonts) { 174 if ('load' in document.fonts) 175 return document.fonts.load(font).then(func); 176 if ('ready' in document.fonts) 177 return document.fonts.ready.then(func); 178 } 179 document.offsetTop; // last resort to load @font-face 180 func(); 181 } 182 183 function arrayFromRangesByValue(dict) { 184 var array = []; 185 for (var value in dict) { 186 var ranges = dict[value]; 187 for (var i = 0; i < ranges.length; i += 2) { 188 var to = ranges[i+1]; 189 for (var code = ranges[i]; code <= to; code++) 190 array[code] = value; 191 } 192 } 193 return array; 194 }; 195 196 function stringFromUnicode(code) { 197 var hex = code.toString(16).toUpperCase(); 198 if (hex.length < 4) { 199 hex = "0000" + hex; 200 hex = hex.substr(hex.length - 4); 201 } 202 return hex + ' "' + String.fromCharCode(code) + '"'; 203 } 204 205 function appendChildElement(parent, tag, text) { 206 var node = document.createElement(tag); 207 if (text) 208 node.textContent = text; 209 parent.appendChild(node); 210 return node; 211 } 212 213 function extend(target, dict) { 214 for (var key in dict) 215 target[key] = dict[key]; 216 } 217 })();