1090168-3-ref.html (1570B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 </head> 6 <body> 7 <script type="text/javascript"> 8 var testFont = '40px sans-serif'; 9 10 function test(x, y, text, style, rotation, baseline) { 11 var canvas = document.createElement("canvas"); 12 canvas.width = 400; 13 canvas.height = 400; 14 canvas.style.cssText = 'position:absolute;' + style; 15 document.getElementsByTagName('body')[0].appendChild(canvas); 16 var ctx = canvas.getContext('2d'); 17 ctx.globalAlpha = 0.5; 18 ctx.beginPath(); 19 ctx.moveTo(x - 20, y); ctx.lineTo(x + 20, y); 20 ctx.moveTo(x, y - 20); ctx.lineTo(x, y + 20); 21 ctx.stroke(); 22 ctx.globalAlpha = 1.0; 23 ctx.font = testFont; 24 if (rotation != 0) { 25 ctx.translate(x,y); 26 ctx.rotate(rotation); 27 ctx.translate(-x,-y); 28 } 29 if (baseline != '') { 30 ctx.textBaseline = baseline; 31 } 32 ctx.fillText(text, x, y); 33 } 34 35 // Testcase: vertical text with various textBaselines 36 // test(100, 50, 'Top', 'writing-mode:vertical-lr', 0, 'top'); 37 // test(150, 50, 'Middle', 'writing-mode:vertical-lr', 0, 'middle'); 38 // test(200, 50, 'Alphabetic', 'writing-mode:vertical-lr', 0, 'alphabetic'); 39 // test(250, 50, 'Bottom', 'writing-mode:vertical-lr', 0, 'bottom'); 40 41 // Reference: horizontal text with 90° rotation and the same baselines 42 test(100, 50, 'Top', 'writing-mode:horizontal-tb', Math.PI/2, 'top'); 43 test(150, 50, 'Middle', 'writing-mode:horizontal-tb', Math.PI/2, 'middle'); 44 test(200, 50, 'Alphabetic', 'writing-mode:horizontal-tb', Math.PI/2, 'alphabetic'); 45 test(250, 50, 'Bottom', 'writing-mode:horizontal-tb', Math.PI/2, 'bottom'); 46 47 </script>