2d.text.measure.text-clusters-split.tentative.worker.js (1751B)
1 // DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. 2 // OffscreenCanvas test in a worker:2d.text.measure.text-clusters-split.tentative 3 // Description:Test that getTextClusters() splits the input correctly into the minimal clusters, keeping emojis together. 4 // Note: 5 6 importScripts("/resources/testharness.js"); 7 importScripts("/html/canvas/resources/canvas-tests.js"); 8 9 var t = async_test("Test that getTextClusters() splits the input correctly into the minimal clusters, keeping emojis together."); 10 var t_pass = t.done.bind(t); 11 var t_fail = t.step_func(function(reason) { 12 throw reason; 13 }); 14 t.step(function() { 15 16 var canvas = new OffscreenCanvas(400, 200); 17 var ctx = canvas.getContext('2d'); 18 19 function getClusterIndexes(text) { 20 const clusters = ctx.measureText(text).getTextClusters(); 21 const result = []; 22 for (let i = 0; i < clusters.length; i++) { 23 const end = clusters[i].end; 24 if (end === (i + 1 < clusters.length ? clusters[i + 1].start : text.length)) { 25 result.push(clusters[i].start); 26 } else { 27 result.push([clusters[i].start, clusters[i].end]); 28 } 29 } 30 return result; 31 } 32 33 function assertClusterIndexes(text, expected) { 34 const actual = getClusterIndexes(text); 35 assert_array_equals(actual, expected); 36 } 37 38 ctx.font = '50px serif'; 39 const text = 'ABC ☺️❤️'; 40 ctx.fillText(text, 20, 100); 41 assertClusterIndexes(text, [0, 1, 2, 3, 4, 6]); 42 43 // [UAX#29]: https://unicode.org/reports/tr29/ 44 // [UAX#29] GB9: × (Extend | ZWJ) 45 assertClusterIndexes('X\u200DY', [0, 2]); 46 // [UAX#29] GB11: \p{Extended_Pictographic} Extend* ZWJ × \p{Extended_Pictographic} 47 assertClusterIndexes('\u{1FFFD}\u200D\u{1FFFD}', [0]); 48 t.done(); 49 }); 50 done();