2d.text.drawing.style.letterSpacing.change.font.html (2041B)
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.change.font</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.change.font</h1> 10 <p class="desc">Set letter spacing and word spacing to font dependent value and verify it works after font change.</p> 11 12 13 <script> 14 var t = async_test("Set letter spacing and word spacing to font dependent value and verify it works after font change."); 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 // Get the width for 'Hello World' at default size, 10px. 27 var width_normal = ctx.measureText('Hello World').width; 28 29 ctx.letterSpacing = '1em'; 30 _assertSame(ctx.letterSpacing, '1em', "ctx.letterSpacing", "'1em'"); 31 // 1em = 10px. Add 10px after each letter in "Hello World", 32 // makes it 110px longer. 33 var width_with_spacing = ctx.measureText('Hello World').width; 34 assert_approx_equals(width_with_spacing, width_normal + 110, 0.1, "letter-spacing error"); 35 36 // Changing font to 20px. Without resetting the spacing, 1em letterSpacing 37 // is now 20px, so it's suppose to be 220px longer without any letterSpacing set. 38 ctx.font = '20px serif'; 39 width_with_spacing = ctx.measureText('Hello World').width; 40 // Now calculate the reference spacing for "Hello World" with no spacing. 41 ctx.letterSpacing = '0em'; 42 width_normal = ctx.measureText('Hello World').width; 43 assert_approx_equals(width_with_spacing, width_normal + 220, 0.1, "letter-spacing error after font change"); 44 t.done(); 45 46 }); 47 </script>