getrotationofchar.html (3987B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <title>SVGTextContentElement.getRotationOfChar</title> 4 <link rel="help" href="https://svgwg.org/svg2-draft/text.html#__svg__SVGTextContentElement__getRotationOfChar"> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <svg id="container" width="800" height="300"></svg> 8 <script> 9 function createSVGElement(localPart, attributes, textOrElement) { 10 const element = document.createElementNS('http://www.w3.org/2000/svg', localPart); 11 for (let name in attributes) { 12 element.setAttribute(name, attributes[name]); 13 } 14 if (typeof(textOrElement) == 'string') 15 element.textContent = textOrElement; 16 else if (textOrElement instanceof Element) 17 element.appendChild(textOrElement); 18 else if (textOrElement !== undefined) 19 throw new TypeError('The third argument should be a string or an Element'); 20 return element; 21 } 22 23 function normalize(rotation) { 24 return rotation - 360 * Math.floor(rotation / 360); 25 } 26 27 function normalizedRotation(element, index) { 28 return normalize(element.getRotationOfChar(index)); 29 } 30 31 const container = document.querySelector('#container'); 32 ['horizontal-tb', 'vertical-rl', 'vertical-lr', 'sideways-rl', 'sideways-lr'].forEach(mode => { 33 const isHorizontal = mode == 'horizontal-tb'; 34 const isVertical = mode == 'vertical-rl' || mode == 'vertical-lr'; 35 const sidewaysDelta = 90 * ((mode == 'sideways-rl') - (mode == 'sideways-lr')); 36 container.style.writingMode = mode; 37 38 test(() => { 39 container.style.textOrientation = 'mixed'; 40 const element = container.appendChild(createSVGElement('text', {}, ' M月X')); 41 assert_equals(element.getRotationOfChar(0), isVertical ? 90 : sidewaysDelta, 'M'); 42 assert_equals(element.getRotationOfChar(1), sidewaysDelta, '月'); 43 assert_equals(element.getRotationOfChar(2), isVertical ? 90 : sidewaysDelta, 'X'); 44 }, `'text-orientation: mixed' - ${mode}`); 45 46 test(() => { 47 container.style.textOrientation = 'upright'; 48 const element = container.appendChild(createSVGElement('text', {}, 'T火')); 49 assert_equals(element.getRotationOfChar(0), sidewaysDelta, 'T'); 50 assert_equals(element.getRotationOfChar(1), sidewaysDelta, '火'); 51 }, `'text-orientation: upright' - ${mode}`); 52 53 test(() => { 54 container.style.textOrientation = 'sideways'; 55 const element = container.appendChild(createSVGElement('text', {}, 'W水')); 56 assert_equals(element.getRotationOfChar(0), isVertical ? 90 : sidewaysDelta, 'W'); 57 assert_equals(element.getRotationOfChar(1), isVertical ? 90 : sidewaysDelta, '水'); 58 }, `'text-orientation: sideways' - ${mode}`); 59 60 test(() => { 61 container.style.textOrientation = 'mixed'; 62 const element = container.appendChild( 63 createSVGElement('text', {rotate: '0 180.5 360 365 -10'}, 't木cde')); 64 const shift = isHorizontal ? 0 : 90 * (mode == 'sideways-rl' || isVertical) + 270 * (mode == 'sideways-lr'); 65 assert_equals(normalizedRotation(element, 0), 0 + shift, 't'); 66 assert_equals(normalizedRotation(element, 1), 180.5 + sidewaysDelta, '木'); 67 assert_equals(normalizedRotation(element, 2), 0 + shift, 'c'); 68 assert_equals(normalizedRotation(element, 3), 5 + shift, 'd'); 69 assert_equals(normalizedRotation(element, 4), normalize(350 + shift), 'e'); 70 }, `Rotation attribute - ${mode}`); 71 72 test(() => { 73 container.style.textOrientation = 'mixed'; 74 container.appendChild(createSVGElement('path', {id: 'path1', d: 'M 40 40 L 200 200'})); 75 const element = container.appendChild( 76 createSVGElement('text', {rotate: '0 0 25'}, 77 createSVGElement('textPath', {href: '#path1'}, 'F金r'))); 78 const shift = (mode == 'sideways-lr') ? 180 : 0; 79 assert_equals(normalizedRotation(element, 0), 45 + shift, 'F'); 80 assert_equals(normalizedRotation(element, 1), isVertical ? 315 : 45 + shift, '金'); 81 assert_equals(normalizedRotation(element, 2), 45 + 25 + shift, 'r'); 82 }, `<textPath> - ${mode}`); 83 }); 84 </script>