2d.text.measure.index-from-offset-edge-cases.tentative.worker.js (2207B)
1 // DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. 2 // OffscreenCanvas test in a worker:2d.text.measure.index-from-offset-edge-cases.tentative 3 // Description:Test the edge cases for getIndexFromOffset, where the point is at the edge of glyph and at the midpoint. 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(100, 50); 11 var ctx = canvas.getContext('2d'); 12 13 var f = new FontFace("CanvasTest", "url('/fonts/CanvasTest.ttf')"); 14 f.load(); 15 self.fonts.add(f); 16 await self.fonts.ready; 17 ctx.font = '50px CanvasTest'; 18 ctx.direction = 'ltr'; 19 ctx.align = 'left' 20 ctx.baseline = 'alphabetic' 21 tm = ctx.measureText('A'); 22 const a_width = tm.width; 23 tm = ctx.measureText('B'); 24 const b_width = tm.width; 25 tm = ctx.measureText('C'); 26 const c_width = tm.width; 27 const epsilon = 1.0e-4; 28 29 tm = ctx.measureText('ABC'); 30 _assert(tm.getIndexFromOffset(0) == 0, "tm.getIndexFromOffset(0) == 0"); 31 _assert(tm.getIndexFromOffset(a_width / 2) == 0, "tm.getIndexFromOffset(a_width / 2) == 0"); 32 _assert(tm.getIndexFromOffset(a_width / 2 + 1) == 1, "tm.getIndexFromOffset(a_width / 2 + 1) == 1"); 33 _assert(tm.getIndexFromOffset(a_width) == 1, "tm.getIndexFromOffset(a_width) == 1"); 34 _assert(tm.getIndexFromOffset(a_width + b_width / 2) == 1, "tm.getIndexFromOffset(a_width + b_width / 2) == 1"); 35 _assert(tm.getIndexFromOffset(a_width + b_width / 2 + 1) == 2, "tm.getIndexFromOffset(a_width + b_width / 2 + 1) == 2"); 36 _assert(tm.getIndexFromOffset(a_width + b_width) == 2, "tm.getIndexFromOffset(a_width + b_width) == 2"); 37 _assert(tm.getIndexFromOffset(a_width + b_width + c_width / 2) == 2, "tm.getIndexFromOffset(a_width + b_width + c_width / 2) == 2"); 38 _assert(tm.getIndexFromOffset(a_width + b_width + c_width / 2 + 1) == 3, "tm.getIndexFromOffset(a_width + b_width + c_width / 2 + 1) == 3"); 39 _assert(tm.getIndexFromOffset(a_width + b_width + c_width) == 3, "tm.getIndexFromOffset(a_width + b_width + c_width) == 3"); 40 }, "Test the edge cases for getIndexFromOffset, where the point is at the edge of glyph and at the midpoint."); 41 done();