Spaces:
Sleeping
Sleeping
File size: 4,312 Bytes
d605f27 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
const preservedAttributes = {
...[
"viewBox", "transform", "x", "y", "width", "height", "ry", "d", "x1", "y1", "x2", "y2", "color",
"stroke-width", "points", "stroke-dasharray", "font-size", "font-weight", "font-style", "text-anchor",
].reduce((dict, key) => ({...dict, [key]: key}), {}),
"xlink:href": "href",
};
const domNodeToElement = node => {
const elem : any = {
type: node.tagName,
};
for (const key in preservedAttributes) {
const value = node.getAttribute(key);
if (value)
elem[preservedAttributes[key]] = value;
}
switch (node.tagName) {
case "tspan":
elem.text = node.textContent;
break;
case "a":
if (!/:(\d+:\d+:\d+)$/.test(elem.href)) {
//if (!/lilypond/.test(elem.href))
// console.warn("unexpected a.href:", elem.href);
delete elem.href;
}
else
elem.href = elem.href.match(/:(\d+:\d+:\d+)$/)[1];
break;
case "path":
//console.log("path:", node.getAttribute("d"), elem);
//if (elem.d)
// elem.d = sha1(elem.d);
break;
case "polygon":
//if (elem.points)
// elem.points = sha1(elem.points);
break;
}
[
"x", "y", "width", "height", "ry", "x1", "y1", "x2", "y2", "stroke-width", "font-size", "font-weight", "font-style", "text-anchor",
].forEach(att => {
if (elem[att]) {
const n = Number(elem[att]);
if (Number.isFinite(n))
elem[att] = n;
}
});
if (elem.transform) {
let [_, tx, ty, sx, sy] = [null, 0, 0, 1, 1];
if (/translate\([\d.\-,\s]+\)\s*scale\([\d.\-,\s]+\)/.test(elem.transform))
[_, tx, ty, sx, sy] = elem.transform.match(/translate\(([\d.-]+),\s*([\d.-]+)\)\s*scale\(([\d.-]+),\s*([\d.-]+)\)/);
else if (/translate\([\d.\-,\s]+\)/.test(elem.transform))
[_, tx, ty] = elem.transform.match(/translate\(([\d.-]+),\s*([\d.-]+)\)/);
else if (/scale\([\d.\-,\s]+\)/.test(elem.transform))
[_, sx, sy] = elem.transform.match(/scale\(([\d.-]+),\s*([\d.-]+)\)/);
else if (/^rotate(.*)$/.test(elem.transform)) {}
else
console.warn("unexpected transform:", elem.transform);
elem.transform = {
translate: {
x: Number(tx),
y: Number(ty),
},
scale: {
x: Number(sx),
y: Number(sy),
},
};
}
for (let i = 0; i < node.childNodes.length; ++i) {
const child = node.childNodes[i];
if (child.nodeType === 1) {
//console.log("child:", child);
elem.children = elem.children || [];
elem.children.push(domNodeToElement(child));
}
}
switch (elem.type) {
case "text":
if (elem.children)
elem.text = elem.children.filter(e => e.type === "tspan" && e.text).map(e => e.text).join("");
break;
}
return elem;
};
const svgToElements = (svgText, {logger = null, DOMParser = null} = {}) => {
const dom = new DOMParser().parseFromString(svgText, "text/xml");
//console.log("dom:", dom);
if (logger)
logger.append("svgToElements");
const svg : any = dom.childNodes[0];
console.assert(svg && svg.tagName === "svg");
const root = domNodeToElement(svg);
//if (logger)
// logger.append("svgToElements.root", JSON.parse(JSON.stringify(root)));
if (!root.children) {
console.log("invalid svg:", root, svgText);
return null;
}
// remove proxy tags of a & g
while (true) {
const index = root.children.findIndex(c => c.type === "a" && c.children);
if (index >= 0) {
const a = root.children[index];
a.children.forEach(p => {
p.href = a.href;
p.color = a.color;
});
root.children.splice(index, 1, ...a.children);
}
else {
const gi = root.children.findIndex(c => c.type === "g" && c.children);
if (gi >= 0) {
const g = root.children[gi];
for (const child of g.children) {
if (g.transform) {
child.transform = child.transform || {
translate: {x: 0, y: 0},
scale: {x: 1, y: 1},
};
child.transform.translate.x = g.transform.translate.x + child.transform.translate.x * g.transform.scale.x;
child.transform.translate.y = g.transform.translate.y + child.transform.translate.y * g.transform.scale.y;
child.transform.scale.x *= g.transform.scale.x;
child.transform.scale.y *= g.transform.scale.y;
}
if (g.color)
child.color = g.color;
if (g.href)
child.href = g.href;
}
root.children.splice(gi, 1, ...g.children);
}
else
break;
}
};
return root;
};
export {
svgToElements,
};
|