2d.text.measure.index-from-offset-edge-cases.tentative.html (2403B)
1 <!DOCTYPE html> 2 <!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. --> 3 <meta charset="UTF-8"> 4 <title>OffscreenCanvas test: 2d.text.measure.index-from-offset-edge-cases.tentative</title> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script src="/html/canvas/resources/canvas-tests.js"></script> 8 9 <h1>2d.text.measure.index-from-offset-edge-cases.tentative</h1> 10 <p class="desc">Test the edge cases for getIndexFromOffset, where the point is at the edge of glyph and at the midpoint.</p> 11 12 13 <script> 14 promise_test(async t => { 15 16 var canvas = new OffscreenCanvas(100, 50); 17 var ctx = canvas.getContext('2d'); 18 19 var f = new FontFace("CanvasTest", "url('/fonts/CanvasTest.ttf')"); 20 f.load(); 21 document.fonts.add(f); 22 await document.fonts.ready; 23 ctx.font = '50px CanvasTest'; 24 ctx.direction = 'ltr'; 25 ctx.align = 'left' 26 ctx.baseline = 'alphabetic' 27 tm = ctx.measureText('A'); 28 const a_width = tm.width; 29 tm = ctx.measureText('B'); 30 const b_width = tm.width; 31 tm = ctx.measureText('C'); 32 const c_width = tm.width; 33 const epsilon = 1.0e-4; 34 35 tm = ctx.measureText('ABC'); 36 _assert(tm.getIndexFromOffset(0) == 0, "tm.getIndexFromOffset(0) == 0"); 37 _assert(tm.getIndexFromOffset(a_width / 2) == 0, "tm.getIndexFromOffset(a_width / 2) == 0"); 38 _assert(tm.getIndexFromOffset(a_width / 2 + 1) == 1, "tm.getIndexFromOffset(a_width / 2 + 1) == 1"); 39 _assert(tm.getIndexFromOffset(a_width) == 1, "tm.getIndexFromOffset(a_width) == 1"); 40 _assert(tm.getIndexFromOffset(a_width + b_width / 2) == 1, "tm.getIndexFromOffset(a_width + b_width / 2) == 1"); 41 _assert(tm.getIndexFromOffset(a_width + b_width / 2 + 1) == 2, "tm.getIndexFromOffset(a_width + b_width / 2 + 1) == 2"); 42 _assert(tm.getIndexFromOffset(a_width + b_width) == 2, "tm.getIndexFromOffset(a_width + b_width) == 2"); 43 _assert(tm.getIndexFromOffset(a_width + b_width + c_width / 2) == 2, "tm.getIndexFromOffset(a_width + b_width + c_width / 2) == 2"); 44 _assert(tm.getIndexFromOffset(a_width + b_width + c_width / 2 + 1) == 3, "tm.getIndexFromOffset(a_width + b_width + c_width / 2 + 1) == 3"); 45 _assert(tm.getIndexFromOffset(a_width + b_width + c_width) == 3, "tm.getIndexFromOffset(a_width + b_width + c_width) == 3"); 46 47 }, "Test the edge cases for getIndexFromOffset, where the point is at the edge of glyph and at the midpoint."); 48 </script>