UNPKG

@zsnout/ithkuil

Version:

A set of tools which can generate and parse romanized Ithkuil text and which can generate Ithkuil script from text and JSON data.

39 lines (38 loc) 1.33 kB
import { jsx as _jsx } from "@zsnout/ithkuil-jsx/jsx-runtime"; const firstMoveRegex = /^M\s+([-+e.\d]+)\s+([-+e.\d]+)/; function translatePath(path, tx, ty) { const d = path.getAttribute("d"); if (d && firstMoveRegex.test(d)) { path.setAttribute("d", d.replace(firstMoveRegex, (_, x, y) => `M ${+x + tx} ${+y + ty}`)); } return path; } function translateText(text, tx, ty) { const x = +(text.getAttribute("x") || 0); const y = +(text.getAttribute("y") || 0); text.setAttribute("x", "" + (x + tx)); text.setAttribute("y", "" + (y + ty)); return text; } export function Translate(props) { const x = props.x || 0; const y = props.y || 0; if (!props.children) { return; } if (props.children instanceof SVGPathElement) { return translatePath(props.children, x, y); } if (props.children instanceof SVGGElement) { const children = props.children.querySelectorAll("path"); children.forEach((child) => { translatePath(child, x, y); }); const texts = props.children.querySelectorAll("text"); texts.forEach((child) => { translateText(child, x, y); }); return props.children; } return _jsx("g", { transform: `translate(${x},${y})`, children: props.children }); }