2d.text.measure.strokeTextCluster-range.tentative.worker.js (2314B)
1 // DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. 2 // OffscreenCanvas test in a worker:2d.text.measure.strokeTextCluster-range.tentative 3 // Description:Test that getTextClusters() and strokeTextCluster() 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.strokeTextCluster(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.strokeStyle = '#0f0'; 36 ctx.lineWidth = 12; 37 38 // Without the first character. 39 renderClusters(tm.getTextClusters(1, 5), 0, 0); 40 _assertPixelApprox(canvas, 5,5, 255,0,0,255, 2); 41 _assertPixelApprox(canvas, 55,5, 0,255,0,255, 2); 42 _assertPixelApprox(canvas, 105,5, 0,255,0,255, 2); 43 _assertPixelApprox(canvas, 155,5, 0,255,0,255, 2); 44 _assertPixelApprox(canvas, 205,5, 0,255,0,255, 2); 45 // Without the last character. 46 renderClusters(tm.getTextClusters(0, 4), 0, 100); 47 _assertPixelApprox(canvas, 5,105, 0,255,0,255, 2); 48 _assertPixelApprox(canvas, 55,105, 0,255,0,255, 2); 49 _assertPixelApprox(canvas, 105,105, 0,255,0,255, 2); 50 _assertPixelApprox(canvas, 155,105, 0,255,0,255, 2); 51 _assertPixelApprox(canvas, 245,105, 255,0,0,255, 2); 52 // Only the middle character. 53 renderClusters(tm.getTextClusters(2, 3), 0, 200); 54 _assertPixelApprox(canvas, 5,205, 255,0,0,255, 2); 55 _assertPixelApprox(canvas, 55,205, 255,0,0,255, 2); 56 _assertPixelApprox(canvas, 105,205, 0,255,0,255, 2); 57 _assertPixelApprox(canvas, 195,205, 255,0,0,255, 2); 58 _assertPixelApprox(canvas, 245,205, 255,0,0,255, 2); 59 }, "Test that getTextClusters() and strokeTextCluster() correctly render different ranges of the input text."); 60 done();