import { calculateSize } from './size.mjs'; function preserveAspectRatio(props) { let result = ""; switch (props.hAlign) { case "left": result += "xMin"; break; case "right": result += "xMax"; break; default: result += "xMid"; } switch (props.vAlign) { case "top": result += "YMin"; break; case "bottom": result += "YMax"; break; default: result += "YMid"; } result += props.slice ? " slice" : " meet"; return result; } function iconToSVG(icon, customisations) { const box = { left: icon.left, top: icon.top, width: icon.width, height: icon.height }; let body = icon.body; [icon, customisations].forEach((props) => { const transformations = []; const hFlip = props.hFlip; const vFlip = props.vFlip; let rotation = props.rotate; if (hFlip) { if (vFlip) { rotation += 2; } else { transformations.push("translate(" + (box.width + box.left).toString() + " " + (0 - box.top).toString() + ")"); transformations.push("scale(-1 1)"); box.top = box.left = 0; } } else if (vFlip) { transformations.push("translate(" + (0 - box.left).toString() + " " + (box.height + box.top).toString() + ")"); transformations.push("scale(1 -1)"); box.top = box.left = 0; } let tempValue; if (rotation < 0) { rotation -= Math.floor(rotation / 4) * 4; } rotation = rotation % 4; switch (rotation) { case 1: tempValue = box.height / 2 + box.top; transformations.unshift("rotate(90 " + tempValue.toString() + " " + tempValue.toString() + ")"); break; case 2: transformations.unshift("rotate(180 " + (box.width / 2 + box.left).toString() + " " + (box.height / 2 + box.top).toString() + ")"); break; case 3: tempValue = box.width / 2 + box.left; transformations.unshift("rotate(-90 " + tempValue.toString() + " " + tempValue.toString() + ")"); break; } if (rotation % 2 === 1) { if (box.left !== 0 || box.top !== 0) { tempValue = box.left; box.left = box.top; box.top = tempValue; } if (box.width !== box.height) { tempValue = box.width; box.width = box.height; box.height = tempValue; } } if (transformations.length) { body = '' + body + ""; } }); let width, height; if (customisations.width === null && customisations.height === null) { height = "1em"; width = calculateSize(height, box.width / box.height); } else if (customisations.width !== null && customisations.height !== null) { width = customisations.width; height = customisations.height; } else if (customisations.height !== null) { height = customisations.height; width = calculateSize(height, box.width / box.height); } else { width = customisations.width; height = calculateSize(width, box.height / box.width); } if (width === "auto") { width = box.width; } if (height === "auto") { height = box.height; } width = typeof width === "string" ? width : width.toString() + ""; height = typeof height === "string" ? height : height.toString() + ""; const result = { attributes: { width, height, preserveAspectRatio: preserveAspectRatio(customisations), viewBox: box.left.toString() + " " + box.top.toString() + " " + box.width.toString() + " " + box.height.toString() }, body }; if (customisations.inline) { result.inline = true; } return result; } export { iconToSVG };