2d.text.drawing.style.letterSpacing.measure.html (2236B)
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.drawing.style.letterSpacing.measure</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.drawing.style.letterSpacing.measure</h1> 10 <p class="desc">Testing letter spacing with different length units</p> 11 12 13 <script> 14 var t = async_test("Testing letter spacing with different length units"); 15 var t_pass = t.done.bind(t); 16 var t_fail = t.step_func(function(reason) { 17 throw reason; 18 }); 19 t.step(function() { 20 21 var canvas = new OffscreenCanvas(100, 50); 22 var ctx = canvas.getContext('2d'); 23 24 _assertSame(ctx.letterSpacing, '0px', "ctx.letterSpacing", "'0px'"); 25 _assertSame(ctx.wordSpacing, '0px', "ctx.wordSpacing", "'0px'"); 26 var width_normal = ctx.measureText('Hello World').width; 27 28 function test_letter_spacing(value, difference_spacing, epsilon) { 29 ctx.letterSpacing = value; 30 _assertSame(ctx.letterSpacing, value, "ctx.letterSpacing", "value"); 31 _assertSame(ctx.wordSpacing, '0px', "ctx.wordSpacing", "'0px'"); 32 width_with_letter_spacing = ctx.measureText('Hello World').width; 33 assert_approx_equals(width_with_letter_spacing, width_normal + difference_spacing, epsilon, "letter spacing doesn't work."); 34 } 35 36 // The first value is the letter Spacing to be set, the second value the 37 // change in length of string 'Hello World', note that there are 11 letters 38 // in 'hello world', so the length difference is always letterSpacing * 11. 39 // and the third value is the acceptable differencee for the length change, 40 // note that unit such as 1cm/1mm doesn't map to an exact pixel value. 41 test_cases = [['3px', 33, 0.1], 42 ['5px', 55, 0.1], 43 ['-2px', -22, 0.1], 44 ['1em', 110, 0.1], 45 ['-0.1em', -11, 0.1], 46 ['1in', 1056, 0.1], 47 ['-0.1cm', -41.65, 0.2], 48 ['-0.6mm', -24.95, 0.2]] 49 50 for (const test_case of test_cases) { 51 test_letter_spacing(test_case[0], test_case[1], test_case[2]); 52 } 53 t.done(); 54 55 }); 56 </script>