blob: 766afe5f96da16565052449581f9b1e86604d43a [file] [edit]
<!DOCTYPE html>
<meta charset="utf-8">
<title>SVGTextContentElement.getRotationOfChar</title>
<link rel="help" href="https://svgwg.org/svg2-draft/text.html#__svg__SVGTextContentElement__getRotationOfChar">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<svg id="container" width="800" height="300"></svg>
<script>
function createSVGElement(localPart, attributes, textOrElement) {
const element = document.createElementNS('http://www.w3.org/2000/svg', localPart);
for (let name in attributes) {
element.setAttribute(name, attributes[name]);
}
if (typeof(textOrElement) == 'string')
element.textContent = textOrElement;
else if (textOrElement instanceof Element)
element.appendChild(textOrElement);
else if (textOrElement !== undefined)
throw new TypeError('The third argument should be a string or an Element');
return element;
}
function normalize(rotation) {
return rotation - 360 * Math.floor(rotation / 360);
}
function normalizedRotation(element, index) {
return normalize(element.getRotationOfChar(index));
}
const container = document.querySelector('#container');
['horizontal-tb', 'vertical-rl'].forEach(mode => {
const isHorizontal = mode == 'horizontal-tb';
container.style.writingMode = mode;
test(() => {
container.style.textOrientation = 'mixed';
const element = container.appendChild(createSVGElement('text', {}, 'M月'));
assert_equals(element.getRotationOfChar(0), isHorizontal ? 0 : 90, 'M');
assert_equals(element.getRotationOfChar(1), 0, '月');
}, `'text-orientation: mixed' - ${mode}`);
test(() => {
container.style.textOrientation = 'upright';
const element = container.appendChild(createSVGElement('text', {}, 'T火'));
assert_equals(element.getRotationOfChar(0), 0, 'T');
assert_equals(element.getRotationOfChar(1), 0, '火');
}, `'text-orientation: upright' - ${mode}`);
test(() => {
container.style.textOrientation = 'sideways';
const element = container.appendChild(createSVGElement('text', {}, 'W水'));
assert_equals(element.getRotationOfChar(0), isHorizontal ? 0 : 90, 'W');
assert_equals(element.getRotationOfChar(1), isHorizontal ? 0 : 90, '水');
}, `'text-orientation: sideways' - ${mode}`);
test(() => {
container.style.textOrientation = 'mixed';
const element = container.appendChild(
createSVGElement('text', {rotate: '0 180.5 360 365 -10'}, 't木cde'));
const shift = isHorizontal ? 0 : 90;
assert_equals(normalizedRotation(element, 0), 0 + shift, 't');
assert_equals(normalizedRotation(element, 1), 180.5, '木');
assert_equals(normalizedRotation(element, 2), 0 + shift, 'c');
assert_equals(normalizedRotation(element, 3), 5 + shift, 'd');
assert_equals(normalizedRotation(element, 4), normalize(350 + shift), 'e');
}, `Rotation attribute - ${mode}`);
test(() => {
container.style.textOrientation = 'mixed';
container.appendChild(createSVGElement('path', {id: 'path1', d: 'M 40 40 L 200 200'}));
const element = container.appendChild(
createSVGElement('text', {rotate: '0 0 25'},
createSVGElement('textPath', {href: '#path1'}, 'F金r')));
assert_equals(normalizedRotation(element, 0), 45, 'F');
assert_equals(normalizedRotation(element, 1), isHorizontal ? 45 : 315, '金');
assert_equals(normalizedRotation(element, 2), 45 + 25, 'r');
}, `<textPath> - ${mode}`);
});
</script>