2d.text.measure.selection-rects-baselines.tentative.worker.js (2552B)
1 // DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. 2 // OffscreenCanvas test in a worker:2d.text.measure.selection-rects-baselines.tentative 3 // Description:Check that TextMetrics::getSelectionRects() works correctly with textBaseline. 4 // Note: 5 6 importScripts("/resources/testharness.js"); 7 importScripts("/html/canvas/resources/canvas-tests.js"); 8 9 var t = async_test("Check that TextMetrics::getSelectionRects() works correctly with textBaseline."); 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(100, 50); 17 var ctx = canvas.getContext('2d'); 18 19 ctx.font = '50px sans-serif'; 20 21 const kBaselines = [ 22 "top", 23 "hanging", 24 "middle", 25 "alphabetic", 26 "ideographic", 27 "bottom", 28 ]; 29 30 const kTexts = [ 31 'UNAVAILABLE', 32 '🏁🎶🏁', 33 ')(あ)(', 34 '-abcd_', 35 'איפה הספרייה?', 36 'bidiמתמטיקה' 37 ] 38 39 for (const text of kTexts) { 40 for (const baseline of kBaselines) { 41 const tm = ctx.measureText(text); 42 // First character. 43 for (const r of tm.getSelectionRects(0, 1)) { 44 assert_approx_equals(r.top, -tm.fontBoundingBoxAscent, 1.0); 45 assert_approx_equals(r.bottom, tm.fontBoundingBoxDescent, 1.0); 46 } 47 // Last character. 48 for (const r of tm.getSelectionRects(text.length - 1, text.length)) { 49 assert_approx_equals(r.top, -tm.fontBoundingBoxAscent, 1.0); 50 assert_approx_equals(r.bottom, tm.fontBoundingBoxDescent, 1.0); 51 } 52 // Whole string. 53 for (const r of tm.getSelectionRects(0, text.length)) { 54 assert_approx_equals(r.top, -tm.fontBoundingBoxAscent, 1.0); 55 assert_approx_equals(r.bottom, tm.fontBoundingBoxDescent, 1.0); 56 } 57 // Intermediate string. 58 for (const r of tm.getSelectionRects(1, text.length - 1)) { 59 assert_approx_equals(r.top, -tm.fontBoundingBoxAscent, 1.0); 60 assert_approx_equals(r.bottom, tm.fontBoundingBoxDescent, 1.0); 61 } 62 // Invalid start > end string. Creates 0 width rectangle. 63 for (const r of tm.getSelectionRects(3, 2)) { 64 assert_approx_equals(r.top, -tm.fontBoundingBoxAscent, 1.0); 65 assert_approx_equals(r.bottom, tm.fontBoundingBoxDescent, 1.0); 66 } 67 for (const r of tm.getSelectionRects(1, 0)) { 68 assert_approx_equals(r.top, -tm.fontBoundingBoxAscent, 1.0); 69 assert_approx_equals(r.bottom, tm.fontBoundingBoxDescent, 1.0); 70 } 71 } 72 } 73 t.done(); 74 }); 75 done();