2d.text.drawing.style.wordSpacing.measure.html (2223B)
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.wordSpacing.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.wordSpacing.measure</h1> 10 <p class="desc">Testing word spacing with different length units</p> 11 12 13 <script> 14 var t = async_test("Testing word 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, again').width; 27 28 function test_word_spacing(value, difference_spacing, epsilon) { 29 ctx.wordSpacing = value; 30 _assertSame(ctx.letterSpacing, '0px', "ctx.letterSpacing", "'0px'"); 31 _assertSame(ctx.wordSpacing, value, "ctx.wordSpacing", "value"); 32 width_with_word_spacing = ctx.measureText('Hello World, again').width; 33 assert_approx_equals(width_with_word_spacing, width_normal + difference_spacing, epsilon, "word spacing doesn't work."); 34 } 35 36 // The first value is the word Spacing to be set, the second value the 37 // change in length of string 'Hello World', note that there are 2 words 38 // in 'Hello World, again', so the length difference is always wordSpacing * 2. 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', 6, 0.1], 42 ['5px', 10, 0.1], 43 ['-2px', -4, 0.1], 44 ['1em', 20, 0.1], 45 ['-0.5em', -10, 0.1], 46 ['1in', 192, 0.1], 47 ['-0.1cm', -7.57, 0.2], 48 ['-0.6mm', -4.54, 0.2]] 49 50 for (const test_case of test_cases) { 51 test_word_spacing(test_case[0], test_case[1], test_case[2]); 52 } 53 t.done(); 54 55 }); 56 </script>