2d.text.measure.fillTextCluster-range.tentative.worker.js (2282B)
1 // DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. 2 // OffscreenCanvas test in a worker:2d.text.measure.fillTextCluster-range.tentative 3 // Description:Test that getTextClusters() and fillTextCluster() correctly render different ranges of the input text. 4 // Note: 5 6 importScripts("/resources/testharness.js"); 7 importScripts("/html/canvas/resources/canvas-tests.js"); 8 9 promise_test(async t => { 10 var canvas = new OffscreenCanvas(400, 300); 11 var ctx = canvas.getContext('2d'); 12 13 // Renders all the clusters in the list from position (x, y). 14 function renderClusters(clusters, x, y) { 15 for (const cluster of clusters) { 16 ctx.fillTextCluster(cluster, x, y); 17 } 18 } 19 20 var f = new FontFace("CanvasTest", "url('/fonts/CanvasTest.ttf')"); 21 f.load(); 22 self.fonts.add(f); 23 await self.fonts.ready; 24 25 ctx.font = '50px CanvasTest'; 26 ctx.textAlign = 'left'; 27 ctx.textBaseline = 'top'; 28 const text = 'EEEEE'; 29 let tm = ctx.measureText(text); 30 31 // Background color. 32 ctx.fillStyle = '#f00'; 33 ctx.fillRect(0, 0, canvas.width, canvas.height); 34 35 ctx.fillStyle = '#0f0'; 36 37 // Without the first character. 38 renderClusters(tm.getTextClusters(1, 5), 0, 0); 39 _assertPixelApprox(canvas, 5,5, 255,0,0,255, 2); 40 _assertPixelApprox(canvas, 55,5, 0,255,0,255, 2); 41 _assertPixelApprox(canvas, 105,5, 0,255,0,255, 2); 42 _assertPixelApprox(canvas, 155,5, 0,255,0,255, 2); 43 _assertPixelApprox(canvas, 205,5, 0,255,0,255, 2); 44 // Without the last character. 45 renderClusters(tm.getTextClusters(0, 4), 0, 100); 46 _assertPixelApprox(canvas, 5,105, 0,255,0,255, 2); 47 _assertPixelApprox(canvas, 55,105, 0,255,0,255, 2); 48 _assertPixelApprox(canvas, 105,105, 0,255,0,255, 2); 49 _assertPixelApprox(canvas, 155,105, 0,255,0,255, 2); 50 _assertPixelApprox(canvas, 205,105, 255,0,0,255, 2); 51 // Only the middle character. 52 renderClusters(tm.getTextClusters(2, 3), 0, 150); 53 _assertPixelApprox(canvas, 5,155, 255,0,0,255, 2); 54 _assertPixelApprox(canvas, 55,155, 255,0,0,255, 2); 55 _assertPixelApprox(canvas, 105,155, 0,255,0,255, 2); 56 _assertPixelApprox(canvas, 155,155, 255,0,0,255, 2); 57 _assertPixelApprox(canvas, 205,155, 255,0,0,255, 2); 58 }, "Test that getTextClusters() and fillTextCluster() correctly render different ranges of the input text."); 59 done();