vizmatic 0.8.3 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/PROMPT.md +32 -0
- package/README.md +8 -3
- package/ROADMAP.md +4 -2
- package/SCIENCE.md +69 -3
- package/dist/chunk-6JI55YX4.js +604 -0
- package/dist/{chunk-LTX44BAE.js → chunk-BYKXNUX7.js} +25 -4
- package/dist/{chunk-4MXGLMBG.js → chunk-EPZLNABN.js} +107 -37
- package/dist/chunk-EWCO6ZT6.js +696 -0
- package/dist/{chunk-JZPMGVBP.js → chunk-G7HRSKHV.js} +1 -1
- package/dist/{chunk-RPAHMKAR.js → chunk-JTWC2OPN.js} +7 -5
- package/dist/chunk-LJZO36IR.js +124 -0
- package/dist/chunk-REL2BZ2B.js +88 -0
- package/dist/chunk-RLPEOWST.js +1 -0
- package/dist/chunk-VMHOWCDL.js +296 -0
- package/dist/chunk-YDD63HHD.js +1 -0
- package/dist/{chunk-I745F4QK.js → chunk-YNGDEARY.js} +1134 -643
- package/dist/cli.js +80 -543
- package/dist/gpu-selection-CDWCZH27.js +1 -0
- package/dist/gpu-selection-MX6NHMNA.js +635 -0
- package/dist/index.cjs +473 -155
- package/dist/index.d.cts +30 -239
- package/dist/index.d.ts +30 -239
- package/dist/index.js +410 -457
- package/dist/math-E7W5LFWO.js +1 -0
- package/dist/{math-LXB4FH4E.js → math-NEFQUPRV.js} +3 -1
- package/dist/scene-browser.js +17 -17
- package/dist/scene-cli.js +1 -1
- package/dist/scene-runtime.cjs +4 -2
- package/dist/scene-runtime.js +2 -2
- package/dist/stage-browser/browser.d.ts +1206 -854
- package/dist/stage-browser/gpu-renderer.js +138 -0
- package/dist/stage-browser/{math-4WFNHVV2.js → math-KYHUOK7A.js} +46 -10
- package/dist/stage-browser/stage.js +107 -19
- package/dist/stage-export.cjs +8221 -6784
- package/dist/stage-export.d.cts +13 -5
- package/dist/stage-export.d.ts +13 -5
- package/dist/stage-export.js +111 -601
- package/dist/stage.cjs +6243 -3474
- package/dist/stage.d.cts +1064 -765
- package/dist/stage.d.ts +1064 -765
- package/dist/stage.js +3571 -2405
- package/dist/{svg-import-B5BGbC1t.d.cts → svg-import-CSCFWYaB.d.cts} +21 -12
- package/dist/{svg-import-B5BGbC1t.d.ts → svg-import-CSCFWYaB.d.ts} +21 -12
- package/dist/teaching-math-DTKc0XsB.d.cts +243 -0
- package/dist/teaching-math-ydDBhJ2M.d.ts +243 -0
- package/dist/{video-CiN1MDCS.d.cts → theme-DhVdDQKO.d.cts} +4 -237
- package/dist/{video-CiN1MDCS.d.ts → theme-DhVdDQKO.d.ts} +4 -237
- package/dist/video-BYCeOz8F.d.ts +259 -0
- package/dist/video-CGLxJXqu.d.cts +259 -0
- package/examples/catalog-science-physics.tsx +6 -6
- package/examples/films/_boltzmann-entropy/model.ts +63 -0
- package/examples/films/_catalysis/model.ts +49 -0
- package/examples/films/_gravitational-waves/model.ts +32 -0
- package/examples/films/_heat-equation/model.ts +54 -0
- package/examples/films/_hydrogen-orbitals/model.ts +31 -0
- package/examples/films/_maxwell/model.ts +84 -0
- package/examples/films/_planetary-orbits/appearance.ts +71 -0
- package/examples/films/_planetary-orbits/model.ts +152 -0
- package/examples/films/_planetary-orbits/saturn.ts +142 -0
- package/examples/films/_quantum-tunneling/model.ts +58 -0
- package/examples/films/_reaction-diffusion/model.ts +61 -0
- package/examples/films/_stationary-action/model.ts +38 -0
- package/examples/films/attention.ts +309 -253
- package/examples/films/beam-search.ts +321 -49
- package/examples/films/boltzmann-entropy.ts +104 -0
- package/examples/films/cache-miss.ts +387 -394
- package/examples/films/catalog.json +238 -77
- package/examples/films/catalysis.ts +128 -0
- package/examples/films/central-limit.ts +27 -22
- package/examples/films/complex-maps.ts +70 -56
- package/examples/films/deck-example.ts +261 -200
- package/examples/films/euler-circle.ts +72 -28
- package/examples/films/flash-attention.ts +549 -0
- package/examples/films/forecast.ts +2 -2
- package/examples/films/fourier-epicycles.ts +21 -20
- package/examples/films/gradient-descent.ts +23 -49
- package/examples/films/gravitational-waves.ts +179 -0
- package/examples/films/heat-equation.ts +124 -0
- package/examples/films/hydrogen-orbitals.ts +135 -0
- package/examples/films/io-uring.ts +635 -0
- package/examples/films/linear-maps.ts +42 -31
- package/examples/films/lorenz.ts +42 -79
- package/examples/films/maxwell.ts +197 -0
- package/examples/films/pathlines.ts +85 -0
- package/examples/films/planetary-orbits.ts +146 -0
- package/examples/films/pythagoras.ts +18 -10
- package/examples/films/quantum-tunneling.ts +116 -0
- package/examples/films/rag-citations.ts +423 -0
- package/examples/films/reaction-diffusion.ts +70 -0
- package/examples/films/ring-allreduce.ts +531 -0
- package/examples/films/stationary-action.ts +170 -0
- package/examples/films/svd-3d.ts +605 -0
- package/examples/films/taylor-series.ts +9 -9
- package/examples/films/vizmatic-title.ts +1 -0
- package/examples/vizmatic-hero.tsx +102 -32
- package/package.json +3 -1
- package/dist/chunk-3F2VQ7NW.js +0 -1
- package/dist/chunk-GGFDBJ25.js +0 -88
- package/dist/math-QVCETQDQ.js +0 -1
|
@@ -0,0 +1,696 @@
|
|
|
1
|
+
import {
|
|
2
|
+
IDENTITY,
|
|
3
|
+
Node,
|
|
4
|
+
apply,
|
|
5
|
+
arcPath,
|
|
6
|
+
compose,
|
|
7
|
+
filmFont,
|
|
8
|
+
invert,
|
|
9
|
+
matScale,
|
|
10
|
+
mul,
|
|
11
|
+
parseColor,
|
|
12
|
+
parseSvgPath,
|
|
13
|
+
pathBounds,
|
|
14
|
+
pointAt,
|
|
15
|
+
polyPath,
|
|
16
|
+
radialGradient,
|
|
17
|
+
rectPath,
|
|
18
|
+
tangentAt,
|
|
19
|
+
text,
|
|
20
|
+
transformPath
|
|
21
|
+
} from "./chunk-YNGDEARY.js";
|
|
22
|
+
|
|
23
|
+
// src/stage/svg-import.ts
|
|
24
|
+
function safeCodePoint(code) {
|
|
25
|
+
if (!Number.isInteger(code) || code < 1 || code > 1114111 || code >= 55296 && code <= 57343) {
|
|
26
|
+
return "\uFFFD";
|
|
27
|
+
}
|
|
28
|
+
return String.fromCodePoint(code);
|
|
29
|
+
}
|
|
30
|
+
function decode(value) {
|
|
31
|
+
return value.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"').replace(/'/g, "'").replace(/&#(\d+);/g, (_, code) => safeCodePoint(Number(code))).replace(/&#x([\da-fA-F]+);/g, (_, code) => safeCodePoint(parseInt(code, 16))).replace(/&/g, "&");
|
|
32
|
+
}
|
|
33
|
+
var MAX_SVG_DEPTH = 64;
|
|
34
|
+
var MAX_SVG_NODES = 1e4;
|
|
35
|
+
function parseXml(source) {
|
|
36
|
+
const root = { name: "#root", attrs: {}, children: [], text: "" };
|
|
37
|
+
const stack = [root];
|
|
38
|
+
const token = /<!--[\s\S]*?-->|<!\[CDATA\[([\s\S]*?)\]\]>|<\?[\s\S]*?\?>|<!DOCTYPE[^>]*>|<\/([\w:.-]+)\s*>|<([\w:.-]+)((?:\s+[\w:.-]+\s*=\s*(?:"[^"]*"|'[^']*'))*)\s*(\/?)>|([^<]+)/g;
|
|
39
|
+
for (let match = token.exec(source); match; match = token.exec(source)) {
|
|
40
|
+
const top = stack[stack.length - 1];
|
|
41
|
+
const [, cdata, closing, opening, attributes, selfClosing, content] = match;
|
|
42
|
+
if (cdata !== void 0) {
|
|
43
|
+
top.text += cdata;
|
|
44
|
+
} else if (closing) {
|
|
45
|
+
if (stack.length > 1) stack.pop();
|
|
46
|
+
} else if (opening) {
|
|
47
|
+
const element = { name: opening.replace(/^.*:/, ""), attrs: {}, children: [], text: "" };
|
|
48
|
+
for (const attr of (attributes ?? "").matchAll(/([\w:.-]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g)) {
|
|
49
|
+
element.attrs[attr[1]] = decode(attr[2] ?? attr[3] ?? "");
|
|
50
|
+
}
|
|
51
|
+
top.children.push(element);
|
|
52
|
+
if (!selfClosing) stack.push(element);
|
|
53
|
+
} else if (content) {
|
|
54
|
+
top.text += decode(content);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return root;
|
|
58
|
+
}
|
|
59
|
+
function parseTransform(value) {
|
|
60
|
+
if (!value) return IDENTITY;
|
|
61
|
+
let m = IDENTITY;
|
|
62
|
+
for (const match of value.matchAll(/(matrix|translate|scale|rotate|skewX|skewY)\s*\(([^)]*)\)/g)) {
|
|
63
|
+
const a = match[2].trim().split(/[\s,]+/).filter(Boolean).map(Number);
|
|
64
|
+
const name = match[1];
|
|
65
|
+
if (name === "matrix" && a.length === 6) m = mul(m, a);
|
|
66
|
+
else if (name === "translate") m = mul(m, [1, 0, 0, 1, a[0] ?? 0, a[1] ?? 0]);
|
|
67
|
+
else if (name === "scale") m = mul(m, [a[0] ?? 1, 0, 0, a[1] ?? a[0] ?? 1, 0, 0]);
|
|
68
|
+
else if (name === "rotate") {
|
|
69
|
+
const r = (a[0] ?? 0) * Math.PI / 180;
|
|
70
|
+
const c = Math.cos(r), s = Math.sin(r);
|
|
71
|
+
const cx = a[1] ?? 0, cy = a[2] ?? 0;
|
|
72
|
+
m = mul(m, [1, 0, 0, 1, cx, cy]);
|
|
73
|
+
m = mul(m, [c, s, -s, c, 0, 0]);
|
|
74
|
+
m = mul(m, [1, 0, 0, 1, -cx, -cy]);
|
|
75
|
+
} else if (name === "skewX") m = mul(m, [1, 0, Math.tan((a[0] ?? 0) * Math.PI / 180), 1, 0, 0]);
|
|
76
|
+
else if (name === "skewY") m = mul(m, [1, Math.tan((a[0] ?? 0) * Math.PI / 180), 0, 1, 0, 0]);
|
|
77
|
+
}
|
|
78
|
+
return m;
|
|
79
|
+
}
|
|
80
|
+
var MONOSPACE = /monospace|mono\b|\bcode\b|courier|consolas|menlo|monaco|inconsolata/i;
|
|
81
|
+
var NOT_DRAWN = /* @__PURE__ */ new Set(["defs", "clipPath", "mask", "pattern", "filter", "symbol", "marker", "linearGradient", "radialGradient", "title", "desc", "metadata", "style", "script"]);
|
|
82
|
+
var SvgNode = class extends Node {
|
|
83
|
+
byId = /* @__PURE__ */ new Map();
|
|
84
|
+
pictureWidth = 0;
|
|
85
|
+
pictureHeight = 0;
|
|
86
|
+
/** Find an element by its SVG id. */
|
|
87
|
+
find(id) {
|
|
88
|
+
const found = this.byId.get(id);
|
|
89
|
+
if (!found) throw new Error(`The SVG has no element with id ${JSON.stringify(id)}.`);
|
|
90
|
+
return found;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
function parseCssRules(root) {
|
|
94
|
+
const rules = /* @__PURE__ */ new Map();
|
|
95
|
+
const visit = (element) => {
|
|
96
|
+
if (element.name === "style") {
|
|
97
|
+
for (const rule of element.text.matchAll(/([^{}]+)\{([^{}]*)\}/g)) {
|
|
98
|
+
const body = parseStyle(rule[2]);
|
|
99
|
+
for (const selector of rule[1].split(",")) {
|
|
100
|
+
const key = selector.trim();
|
|
101
|
+
rules.set(key, { ...rules.get(key), ...body });
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
element.children.forEach(visit);
|
|
106
|
+
};
|
|
107
|
+
visit(root);
|
|
108
|
+
return rules;
|
|
109
|
+
}
|
|
110
|
+
function parseStyle(value) {
|
|
111
|
+
const out = {};
|
|
112
|
+
if (!value) return out;
|
|
113
|
+
for (const part of value.split(";")) {
|
|
114
|
+
const index = part.indexOf(":");
|
|
115
|
+
if (index > 0) out[part.slice(0, index).trim()] = part.slice(index + 1).trim();
|
|
116
|
+
}
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
function safeColor(value) {
|
|
120
|
+
if (!value || value === "none" || value === "transparent") return null;
|
|
121
|
+
try {
|
|
122
|
+
return parseColor(value);
|
|
123
|
+
} catch {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function splitIntoGlyphs(path) {
|
|
128
|
+
const items = path.map((r) => {
|
|
129
|
+
const b = pathBounds([r]);
|
|
130
|
+
return { ring: r, b, area: b.width * b.height, owner: -1 };
|
|
131
|
+
});
|
|
132
|
+
const order = items.map((_, i) => i).sort((a, b) => items[b].area - items[a].area);
|
|
133
|
+
for (let k = 0; k < order.length; k++) {
|
|
134
|
+
const inner = items[order[k]];
|
|
135
|
+
for (let j = k - 1; j >= 0; j--) {
|
|
136
|
+
const outer = items[order[j]];
|
|
137
|
+
if (outer.owner !== -1) continue;
|
|
138
|
+
const inside = inner.b.x >= outer.b.x - 0.01 && inner.b.y >= outer.b.y - 0.01 && inner.b.x + inner.b.width <= outer.b.x + outer.b.width + 0.01 && inner.b.y + inner.b.height <= outer.b.y + outer.b.height + 0.01;
|
|
139
|
+
if (inside) {
|
|
140
|
+
inner.owner = order[j];
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const glyphs = [];
|
|
146
|
+
const ownerToGlyph = /* @__PURE__ */ new Map();
|
|
147
|
+
items.forEach((item, i) => {
|
|
148
|
+
if (item.owner !== -1) return;
|
|
149
|
+
const glyph = { x: item.b.x, rings: [item.ring] };
|
|
150
|
+
ownerToGlyph.set(i, glyph);
|
|
151
|
+
glyphs.push(glyph);
|
|
152
|
+
});
|
|
153
|
+
items.forEach((item) => {
|
|
154
|
+
if (item.owner !== -1) ownerToGlyph.get(item.owner)?.rings.push(item.ring);
|
|
155
|
+
});
|
|
156
|
+
return glyphs.sort((a, b) => a.x - b.x).map((glyph) => glyph.rings);
|
|
157
|
+
}
|
|
158
|
+
function decodeSvgDataUri(href) {
|
|
159
|
+
if (!/^data:image\/svg\+xml[,;]/i.test(href)) return null;
|
|
160
|
+
const comma = href.indexOf(",");
|
|
161
|
+
if (comma < 0) return null;
|
|
162
|
+
const header = href.slice(0, comma).toLowerCase();
|
|
163
|
+
const body = href.slice(comma + 1);
|
|
164
|
+
try {
|
|
165
|
+
if (header.includes(";base64")) {
|
|
166
|
+
const binary = atob(body.trim());
|
|
167
|
+
const bytes = new Uint8Array(binary.length);
|
|
168
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
169
|
+
return new TextDecoder().decode(bytes);
|
|
170
|
+
}
|
|
171
|
+
return decodeURIComponent(body);
|
|
172
|
+
} catch {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
function fromSvg(source, options = {}) {
|
|
177
|
+
return parseSvgInternal(source, options, { nodes: 0, embedDepth: 0 });
|
|
178
|
+
}
|
|
179
|
+
function parseSvgInternal(source, options, budget) {
|
|
180
|
+
const root = parseXml(source);
|
|
181
|
+
const svg = root.children.find((child) => child.name === "svg");
|
|
182
|
+
if (!svg) throw new Error("fromSvg needs markup with an <svg> element.");
|
|
183
|
+
const rules = parseCssRules(svg);
|
|
184
|
+
const defs = /* @__PURE__ */ new Map();
|
|
185
|
+
const index = (element) => {
|
|
186
|
+
if (element.attrs["id"]) defs.set(element.attrs["id"], element);
|
|
187
|
+
element.children.forEach(index);
|
|
188
|
+
};
|
|
189
|
+
index(svg);
|
|
190
|
+
const box = (svg.attrs["viewBox"] ?? "").trim().split(/[\s,]+/).map(Number);
|
|
191
|
+
const declaredW = parseFloat(svg.attrs["width"] ?? ""), declaredH = parseFloat(svg.attrs["height"] ?? "");
|
|
192
|
+
const viewW = box.length === 4 && box[2] ? box[2] : declaredW || 300;
|
|
193
|
+
const viewH = box.length === 4 && box[3] ? box[3] : declaredH || 150;
|
|
194
|
+
const viewX = box.length === 4 ? box[0] : 0, viewY = box.length === 4 ? box[1] : 0;
|
|
195
|
+
const naturalW = Number.isFinite(declaredW) && declaredW > 0 ? declaredW : viewW;
|
|
196
|
+
const naturalH = Number.isFinite(declaredH) && declaredH > 0 ? declaredH : viewH;
|
|
197
|
+
let scale = 1;
|
|
198
|
+
if (options.width !== void 0 || options.height !== void 0) {
|
|
199
|
+
const sx = options.width !== void 0 ? options.width / viewW : Infinity;
|
|
200
|
+
const sy = options.height !== void 0 ? options.height / viewH : Infinity;
|
|
201
|
+
scale = Math.min(sx, sy);
|
|
202
|
+
} else {
|
|
203
|
+
scale = Math.min(naturalW / viewW, naturalH / viewH);
|
|
204
|
+
}
|
|
205
|
+
const center = options.center ?? true;
|
|
206
|
+
const offsetX = center ? -(viewX + viewW / 2) * scale : -viewX * scale;
|
|
207
|
+
const offsetY = center ? -(viewY + viewH / 2) * scale : -viewY * scale;
|
|
208
|
+
const base = [scale, 0, 0, scale, offsetX, offsetY];
|
|
209
|
+
const out = new SvgNode();
|
|
210
|
+
out.pictureWidth = viewW * scale;
|
|
211
|
+
out.pictureHeight = viewH * scale;
|
|
212
|
+
const splitting = options.splitGlyphs ?? true;
|
|
213
|
+
const keepBackground = options.background ?? false;
|
|
214
|
+
let firstShape = true;
|
|
215
|
+
const gradientOf = (id, m, path, seen = /* @__PURE__ */ new Set()) => {
|
|
216
|
+
if (seen.has(id) || seen.size >= MAX_SVG_DEPTH) return null;
|
|
217
|
+
const element = defs.get(id);
|
|
218
|
+
if (!element) return null;
|
|
219
|
+
const nextSeen = new Set(seen);
|
|
220
|
+
nextSeen.add(id);
|
|
221
|
+
if (element.name === "pattern") {
|
|
222
|
+
const inner = element.children.find((child) => child.name === "linearGradient");
|
|
223
|
+
if (inner) return linear(inner, m, path, true, nextSeen);
|
|
224
|
+
const innerRadial = element.children.find((child) => child.name === "radialGradient");
|
|
225
|
+
if (innerRadial) return radial(innerRadial, m, path, true, nextSeen);
|
|
226
|
+
const fill = element.children.find((child) => child.attrs["fill"]?.startsWith("url(#"));
|
|
227
|
+
const nested = fill?.attrs["fill"]?.match(/url\(#([^)]+)\)/)?.[1];
|
|
228
|
+
return nested ? gradientOf(nested, m, path, nextSeen) : null;
|
|
229
|
+
}
|
|
230
|
+
if (element.name === "linearGradient") return linear(element, m, path, element.attrs["gradientUnits"] !== "userSpaceOnUse", nextSeen);
|
|
231
|
+
if (element.name === "radialGradient") return radial(element, m, path, element.attrs["gradientUnits"] !== "userSpaceOnUse", nextSeen);
|
|
232
|
+
return null;
|
|
233
|
+
};
|
|
234
|
+
const stopsOf = (element, seen = /* @__PURE__ */ new Set()) => {
|
|
235
|
+
let source2 = element;
|
|
236
|
+
const visited = new Set(seen);
|
|
237
|
+
for (let hop = 0; hop < MAX_SVG_DEPTH && !source2.children.some((child) => child.name === "stop"); hop++) {
|
|
238
|
+
const inherited = (source2.attrs["href"] ?? source2.attrs["xlink:href"] ?? "").replace(/^#/, "");
|
|
239
|
+
if (!inherited || visited.has(inherited)) break;
|
|
240
|
+
visited.add(inherited);
|
|
241
|
+
const next = defs.get(inherited);
|
|
242
|
+
if (!next) break;
|
|
243
|
+
source2 = next;
|
|
244
|
+
}
|
|
245
|
+
return source2.children.filter((child) => child.name === "stop").map((stop) => {
|
|
246
|
+
const style = parseStyle(stop.attrs["style"]);
|
|
247
|
+
const offsetRaw = stop.attrs["offset"] ?? "0";
|
|
248
|
+
const offset = offsetRaw.endsWith("%") ? parseFloat(offsetRaw) / 100 : parseFloat(offsetRaw);
|
|
249
|
+
const color = safeColor(style["stop-color"] ?? stop.attrs["stop-color"] ?? "#000000") ?? [0, 0, 0, 1];
|
|
250
|
+
const alpha = parseFloat(style["stop-opacity"] ?? stop.attrs["stop-opacity"] ?? "1");
|
|
251
|
+
return [offset, [color[0], color[1], color[2], color[3] * alpha]];
|
|
252
|
+
});
|
|
253
|
+
};
|
|
254
|
+
const linear = (element, m, path, objectBox, seen = /* @__PURE__ */ new Set()) => {
|
|
255
|
+
const stops = stopsOf(element, seen);
|
|
256
|
+
if (stops.length === 0) return null;
|
|
257
|
+
const coordinate = (value, fallback) => {
|
|
258
|
+
if (value === void 0) return fallback;
|
|
259
|
+
return value.endsWith("%") ? parseFloat(value) / 100 : parseFloat(value);
|
|
260
|
+
};
|
|
261
|
+
const x1 = coordinate(element.attrs["x1"], 0), y1 = coordinate(element.attrs["y1"], 0);
|
|
262
|
+
const x2 = coordinate(element.attrs["x2"], 1), y2 = coordinate(element.attrs["y2"], 0);
|
|
263
|
+
let from, to;
|
|
264
|
+
if (objectBox) {
|
|
265
|
+
const b = pathBounds(path) ?? { x: 0, y: 0, width: 1, height: 1 };
|
|
266
|
+
from = [b.x + x1 * b.width, b.y + y1 * b.height];
|
|
267
|
+
to = [b.x + x2 * b.width, b.y + y2 * b.height];
|
|
268
|
+
} else {
|
|
269
|
+
from = apply(m, x1, y1);
|
|
270
|
+
to = apply(m, x2, y2);
|
|
271
|
+
}
|
|
272
|
+
return { type: "linear", from, to, stops };
|
|
273
|
+
};
|
|
274
|
+
const radial = (element, m, path, objectBox, seen = /* @__PURE__ */ new Set()) => {
|
|
275
|
+
const stops = stopsOf(element, seen);
|
|
276
|
+
if (stops.length === 0) return null;
|
|
277
|
+
const coordinate = (value, fallback) => {
|
|
278
|
+
if (value === void 0) return fallback;
|
|
279
|
+
return value.endsWith("%") ? parseFloat(value) / 100 : parseFloat(value);
|
|
280
|
+
};
|
|
281
|
+
const cxRaw = coordinate(element.attrs["cx"], 0.5);
|
|
282
|
+
const cyRaw = coordinate(element.attrs["cy"], 0.5);
|
|
283
|
+
const rRaw = Math.max(0, coordinate(element.attrs["r"], 0.5));
|
|
284
|
+
let center2;
|
|
285
|
+
let radius;
|
|
286
|
+
if (objectBox) {
|
|
287
|
+
const b = pathBounds(path) ?? { x: 0, y: 0, width: 1, height: 1 };
|
|
288
|
+
center2 = [b.x + cxRaw * b.width, b.y + cyRaw * b.height];
|
|
289
|
+
radius = Math.max(0.5, rRaw * Math.max(b.width, b.height));
|
|
290
|
+
} else {
|
|
291
|
+
center2 = apply(m, cxRaw, cyRaw);
|
|
292
|
+
radius = Math.max(0.5, rRaw * matScale(m));
|
|
293
|
+
}
|
|
294
|
+
return radialGradient(center2, radius, stops);
|
|
295
|
+
};
|
|
296
|
+
const shapePath = (element) => {
|
|
297
|
+
const a = element.attrs;
|
|
298
|
+
const num = (key, fallback = 0) => {
|
|
299
|
+
const value = parseFloat(a[key] ?? "");
|
|
300
|
+
return Number.isFinite(value) ? value : fallback;
|
|
301
|
+
};
|
|
302
|
+
switch (element.name) {
|
|
303
|
+
case "path":
|
|
304
|
+
return a["d"] ? parseSvgPath(a["d"]) : null;
|
|
305
|
+
case "rect": {
|
|
306
|
+
const w = num("width"), h = num("height");
|
|
307
|
+
if (w <= 0 || h <= 0) return null;
|
|
308
|
+
return rectPath(num("x"), num("y"), w, h, num("rx", num("ry")));
|
|
309
|
+
}
|
|
310
|
+
case "circle":
|
|
311
|
+
return num("r") > 0 ? arcPath(num("cx"), num("cy"), num("r"), num("r"), 0, Math.PI * 2) : null;
|
|
312
|
+
case "ellipse":
|
|
313
|
+
return num("rx") > 0 && num("ry") > 0 ? arcPath(num("cx"), num("cy"), num("rx"), num("ry"), 0, Math.PI * 2) : null;
|
|
314
|
+
case "line":
|
|
315
|
+
return polyPath([[num("x1"), num("y1")], [num("x2"), num("y2")]]);
|
|
316
|
+
case "polyline":
|
|
317
|
+
case "polygon": {
|
|
318
|
+
const values = (a["points"] ?? "").trim().split(/[\s,]+/).filter(Boolean).map(Number);
|
|
319
|
+
const points = [];
|
|
320
|
+
for (let i = 0; i + 1 < values.length; i += 2) points.push([values[i], values[i + 1]]);
|
|
321
|
+
return points.length >= 2 ? polyPath(points, element.name === "polygon") : null;
|
|
322
|
+
}
|
|
323
|
+
default:
|
|
324
|
+
return null;
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
const styleOf = (element, inherited) => {
|
|
328
|
+
const a = element.attrs;
|
|
329
|
+
const cls = (a["class"] ?? "").split(/\s+/).filter(Boolean);
|
|
330
|
+
const merged = { ...rules.get(element.name) };
|
|
331
|
+
for (const name of cls) Object.assign(merged, rules.get(`.${name}`));
|
|
332
|
+
if (a["id"]) Object.assign(merged, rules.get(`#${a["id"]}`));
|
|
333
|
+
const pick = (key) => parseStyle(a["style"])[key] ?? a[key] ?? merged[key];
|
|
334
|
+
const style = { ...inherited };
|
|
335
|
+
const color = pick("color");
|
|
336
|
+
if (color) style.color = color;
|
|
337
|
+
const resolvePaintRef = (raw) => {
|
|
338
|
+
if (raw === "currentColor") return style.color;
|
|
339
|
+
if (raw === "context-stroke") return style.contextStroke ?? style.color;
|
|
340
|
+
if (raw === "context-fill") return style.contextFill ?? style.color;
|
|
341
|
+
return raw;
|
|
342
|
+
};
|
|
343
|
+
const fill = pick("fill");
|
|
344
|
+
if (fill) style.fill = resolvePaintRef(fill);
|
|
345
|
+
const stroke = pick("stroke");
|
|
346
|
+
if (stroke) style.stroke = resolvePaintRef(stroke);
|
|
347
|
+
const width = pick("stroke-width");
|
|
348
|
+
if (width !== void 0) style.strokeWidth = parseFloat(width) || 0;
|
|
349
|
+
const fo = pick("fill-opacity");
|
|
350
|
+
if (fo !== void 0) style.fillOpacity = parseFloat(fo);
|
|
351
|
+
const so = pick("stroke-opacity");
|
|
352
|
+
if (so !== void 0) style.strokeOpacity = parseFloat(so);
|
|
353
|
+
const cap = pick("stroke-linecap");
|
|
354
|
+
if (cap === "round" || cap === "butt" || cap === "square") style.cap = cap;
|
|
355
|
+
const join = pick("stroke-linejoin");
|
|
356
|
+
if (join === "round" || join === "miter" || join === "bevel") style.join = join;
|
|
357
|
+
const dash = pick("stroke-dasharray");
|
|
358
|
+
if (dash !== void 0) style.dash = dash === "none" ? null : dash.trim().split(/[\s,]+/).map(Number);
|
|
359
|
+
const dashOffset = pick("stroke-dashoffset");
|
|
360
|
+
if (dashOffset !== void 0) style.dashOffset = parseFloat(dashOffset) || 0;
|
|
361
|
+
const size = pick("font-size");
|
|
362
|
+
if (size !== void 0) style.fontSize = parseFloat(size) || style.fontSize;
|
|
363
|
+
const family = pick("font-family") ?? pick("font");
|
|
364
|
+
if (family) style.family = family;
|
|
365
|
+
const anchor = pick("text-anchor");
|
|
366
|
+
if (anchor) style.anchor = anchor;
|
|
367
|
+
return { style, opacity: parseFloat(pick("opacity") ?? "1") };
|
|
368
|
+
};
|
|
369
|
+
const leafFrom = (path, m, element, style) => {
|
|
370
|
+
const world = transformPath(path, m);
|
|
371
|
+
const scaleOf = matScale(m);
|
|
372
|
+
const url = (value) => value?.match(/^url\(#([^)]+)\)/)?.[1];
|
|
373
|
+
const fillId = url(style.fill), strokeId = url(style.stroke);
|
|
374
|
+
const fill = fillId ? gradientOf(fillId, m, world) : safeColor(style.fill);
|
|
375
|
+
const stroke = strokeId ? gradientOf(strokeId, m, world) : safeColor(style.stroke);
|
|
376
|
+
const visibleFill = fill && style.fillOpacity > 0 && !(Array.isArray(fill) && fill[3] === 0);
|
|
377
|
+
const visibleStroke = stroke && style.strokeOpacity > 0 && style.strokeWidth > 0 && !(Array.isArray(stroke) && stroke[3] === 0);
|
|
378
|
+
if (!visibleFill && !visibleStroke) return [];
|
|
379
|
+
const clippedStroke = element.attrs["clip-path"] ? 0.5 : 1;
|
|
380
|
+
const pieces = splitting && visibleFill && !visibleStroke && world.length > 1 ? splitIntoGlyphs(world) : [world];
|
|
381
|
+
return pieces.map((piece) => {
|
|
382
|
+
const b = pathBounds(piece) ?? { x: 0, y: 0, width: 0, height: 0 };
|
|
383
|
+
const cx = b.x + b.width / 2, cy = b.y + b.height / 2;
|
|
384
|
+
const leaf = new Node(transformPath(piece, [1, 0, 0, 1, -cx, -cy])).at(cx, cy);
|
|
385
|
+
if (visibleFill) {
|
|
386
|
+
leaf.base.fill = shiftPaint(fill, -cx, -cy);
|
|
387
|
+
leaf.base.fillOpacity = style.fillOpacity;
|
|
388
|
+
}
|
|
389
|
+
if (visibleStroke) {
|
|
390
|
+
leaf.base.stroke = shiftPaint(stroke, -cx, -cy);
|
|
391
|
+
leaf.base.strokeWidth = style.strokeWidth * scaleOf * clippedStroke;
|
|
392
|
+
leaf.base.strokeOpacity = style.strokeOpacity;
|
|
393
|
+
leaf.base.cap = style.cap;
|
|
394
|
+
leaf.base.join = style.join;
|
|
395
|
+
if (style.dash) leaf.base.dash = style.dash.map((value) => value * scaleOf);
|
|
396
|
+
leaf.base.dashOffset = style.dashOffset * scaleOf;
|
|
397
|
+
}
|
|
398
|
+
leaf.base.trimEach = true;
|
|
399
|
+
return leaf;
|
|
400
|
+
});
|
|
401
|
+
};
|
|
402
|
+
const initial = {
|
|
403
|
+
fill: "#000000",
|
|
404
|
+
stroke: null,
|
|
405
|
+
strokeWidth: 1,
|
|
406
|
+
fillOpacity: 1,
|
|
407
|
+
strokeOpacity: 1,
|
|
408
|
+
cap: "butt",
|
|
409
|
+
join: "miter",
|
|
410
|
+
dash: null,
|
|
411
|
+
dashOffset: 0,
|
|
412
|
+
color: "#000000",
|
|
413
|
+
fontSize: 16,
|
|
414
|
+
family: "",
|
|
415
|
+
anchor: "start"
|
|
416
|
+
};
|
|
417
|
+
const { style: rootStyle } = styleOf(svg, initial);
|
|
418
|
+
const markerUrlOf = (element, key, camel) => {
|
|
419
|
+
const inline = parseStyle(element.attrs["style"]);
|
|
420
|
+
const cls = (element.attrs["class"] ?? "").split(/\s+/).filter(Boolean);
|
|
421
|
+
const merged = { ...rules.get(element.name) };
|
|
422
|
+
for (const name of cls) Object.assign(merged, rules.get(`.${name}`));
|
|
423
|
+
if (element.attrs["id"]) Object.assign(merged, rules.get(`#${element.attrs["id"]}`));
|
|
424
|
+
const raw = inline[key] ?? inline[camel] ?? element.attrs[key] ?? element.attrs[camel] ?? merged[key];
|
|
425
|
+
return raw?.match(/url\(#([^)]+)\)/)?.[1] ?? null;
|
|
426
|
+
};
|
|
427
|
+
const emitMarker = (markerId, where, hostPath, m, hostStyle, depth, useStack) => {
|
|
428
|
+
if (useStack.has(`marker:${markerId}`)) return null;
|
|
429
|
+
const markerEl = defs.get(markerId);
|
|
430
|
+
if (!markerEl || markerEl.name !== "marker") return null;
|
|
431
|
+
const [px, py] = pointAt(hostPath, where === "start" ? 0 : 1);
|
|
432
|
+
const [tx, ty] = tangentAt(hostPath, where === "start" ? 1e-3 : 0.999);
|
|
433
|
+
const orient = (markerEl.attrs["orient"] ?? "0").trim();
|
|
434
|
+
let angle = 0;
|
|
435
|
+
if (orient === "auto") {
|
|
436
|
+
angle = Math.atan2(ty, tx);
|
|
437
|
+
} else if (orient === "auto-start-reverse") {
|
|
438
|
+
angle = where === "start" ? Math.atan2(-ty, -tx) : Math.atan2(ty, tx);
|
|
439
|
+
} else {
|
|
440
|
+
const deg = orient.endsWith("rad") ? parseFloat(orient) * 180 / Math.PI : parseFloat(orient);
|
|
441
|
+
angle = (Number.isFinite(deg) ? deg : 0) * Math.PI / 180;
|
|
442
|
+
}
|
|
443
|
+
const markerUnits = markerEl.attrs["markerUnits"] ?? "strokeWidth";
|
|
444
|
+
const unitScale = markerUnits === "userSpaceOnUse" ? 1 : hostStyle.strokeWidth > 0 ? hostStyle.strokeWidth : 1;
|
|
445
|
+
const markerW = (parseFloat(markerEl.attrs["markerWidth"] ?? "3") || 3) * unitScale;
|
|
446
|
+
const markerH = (parseFloat(markerEl.attrs["markerHeight"] ?? "3") || 3) * unitScale;
|
|
447
|
+
const vb = (markerEl.attrs["viewBox"] ?? "").trim().split(/[\s,]+/).filter(Boolean).map(Number);
|
|
448
|
+
const hasVb = vb.length === 4 && (vb[2] ?? 0) > 0 && (vb[3] ?? 0) > 0;
|
|
449
|
+
const scaleX = hasVb ? markerW / vb[2] : unitScale;
|
|
450
|
+
const scaleY = hasVb ? markerH / vb[3] : unitScale;
|
|
451
|
+
const refX = parseFloat(markerEl.attrs["refX"] ?? "0") || 0;
|
|
452
|
+
const refY = parseFloat(markerEl.attrs["refY"] ?? "0") || 0;
|
|
453
|
+
const fullWorld = mul(m, compose(px, py, angle, scaleX, scaleY, refX, refY));
|
|
454
|
+
const [worldPx, worldPy] = apply(m, px, py);
|
|
455
|
+
const dirX = m[0] * Math.cos(angle) + m[2] * Math.sin(angle);
|
|
456
|
+
const dirY = m[1] * Math.cos(angle) + m[3] * Math.sin(angle);
|
|
457
|
+
const worldAngle = Math.atan2(dirY, dirX);
|
|
458
|
+
const childMatrix = mul(compose(0, 0, -worldAngle, 1, 1, worldPx, worldPy), fullWorld);
|
|
459
|
+
const markerNode = new Node().at(worldPx, worldPy);
|
|
460
|
+
markerNode.base.rotation = worldAngle;
|
|
461
|
+
markerNode.data["marker"] = where;
|
|
462
|
+
const markerBaseStyle = {
|
|
463
|
+
...rootStyle,
|
|
464
|
+
contextFill: hostStyle.fill,
|
|
465
|
+
contextStroke: hostStyle.stroke
|
|
466
|
+
};
|
|
467
|
+
const { style: markerStyle, opacity: markerOpacity } = styleOf(markerEl, markerBaseStyle);
|
|
468
|
+
if (markerOpacity < 1) markerNode.base.opacity = markerOpacity;
|
|
469
|
+
const nextStack = new Set(useStack);
|
|
470
|
+
nextStack.add(`marker:${markerId}`);
|
|
471
|
+
for (const child of markerEl.children) {
|
|
472
|
+
walk(child, markerNode, markerStyle, childMatrix, depth + 1, nextStack);
|
|
473
|
+
}
|
|
474
|
+
return markerNode.children.length > 0 ? markerNode : null;
|
|
475
|
+
};
|
|
476
|
+
const walk = (element, parent, inherited, parentMatrix, depth = 0, useStack = /* @__PURE__ */ new Set()) => {
|
|
477
|
+
if (depth > MAX_SVG_DEPTH) throw new Error(`fromSvg exceeds the maximum nesting depth (${MAX_SVG_DEPTH}).`);
|
|
478
|
+
if (++budget.nodes > MAX_SVG_NODES) throw new Error(`fromSvg exceeds the ${MAX_SVG_NODES}-node budget.`);
|
|
479
|
+
if (NOT_DRAWN.has(element.name)) return;
|
|
480
|
+
if (element.attrs["filter"]) return;
|
|
481
|
+
const { style, opacity } = styleOf(element, inherited);
|
|
482
|
+
const m = mul(parentMatrix, parseTransform(element.attrs["transform"]));
|
|
483
|
+
const decodeSvgToken = (token) => token === "empty" ? "" : token.replace(/_u([0-9a-f]+)_/gi, (_, hex) => safeCodePoint(parseInt(hex, 16)));
|
|
484
|
+
const GRAPH_NODE_ID = /^vizmatic-graph-(?:index-\d+|id-.+?)-node-(.+)$/;
|
|
485
|
+
const GRAPH_ANY_ID = /^vizmatic-graph-(?:index-\d+|id-.+?)-(?:node|edge)-(.+)$/;
|
|
486
|
+
const registerAlias = (key, node) => {
|
|
487
|
+
if (!key) return;
|
|
488
|
+
if (!out.byId.has(key)) out.byId.set(key, node);
|
|
489
|
+
if (key.startsWith("custom-") && key.length > 7 && !out.byId.has(key.slice(7))) {
|
|
490
|
+
out.byId.set(key.slice(7), node);
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
const register = (node) => {
|
|
494
|
+
const explicitId = element.attrs["id"];
|
|
495
|
+
if (explicitId) {
|
|
496
|
+
out.byId.set(explicitId, node);
|
|
497
|
+
const graphMatch = GRAPH_ANY_ID.exec(explicitId);
|
|
498
|
+
if (graphMatch) registerAlias(decodeSvgToken(graphMatch[1]), node);
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
const dataKey = element.attrs["data-vizmatic-graph-node-id"] ?? element.attrs["data-vizmatic-graph-edge-id"] ?? element.attrs["data-node-id"] ?? element.attrs["data-id"];
|
|
502
|
+
if (dataKey) {
|
|
503
|
+
out.byId.set(dataKey, node);
|
|
504
|
+
registerAlias(dataKey, node);
|
|
505
|
+
try {
|
|
506
|
+
const decoded = decodeURIComponent(dataKey);
|
|
507
|
+
if (decoded !== dataKey) registerAlias(decoded, node);
|
|
508
|
+
} catch {
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
};
|
|
512
|
+
if (element.name === "g" || element.name === "svg" || element.name === "a") {
|
|
513
|
+
const group2 = new Node();
|
|
514
|
+
if (opacity < 1) group2.base.opacity = opacity;
|
|
515
|
+
const explicitId = element.attrs["id"] ?? "";
|
|
516
|
+
const nodeMatch = element.name === "g" ? GRAPH_NODE_ID.exec(explicitId) : null;
|
|
517
|
+
if (nodeMatch) {
|
|
518
|
+
const hitRect = element.children.find((c) => c.name === "rect") ?? element.children.find((c) => c.name === "a")?.children.find((c) => c.name === "rect");
|
|
519
|
+
if (hitRect) {
|
|
520
|
+
const rx = parseFloat(hitRect.attrs["x"] ?? "0") || 0;
|
|
521
|
+
const ry = parseFloat(hitRect.attrs["y"] ?? "0") || 0;
|
|
522
|
+
const rw = parseFloat(hitRect.attrs["width"] ?? "0") || 0;
|
|
523
|
+
const rh = parseFloat(hitRect.attrs["height"] ?? "0") || 0;
|
|
524
|
+
if (rw > 0 && rh > 0) {
|
|
525
|
+
const [p0x, p0y] = apply(m, rx, ry);
|
|
526
|
+
const [p1x, p1y] = apply(m, rx + rw, ry + rh);
|
|
527
|
+
const bx0 = Math.min(p0x, p1x), by0 = Math.min(p0y, p1y);
|
|
528
|
+
const bx1 = Math.max(p0x, p1x), by1 = Math.max(p0y, p1y);
|
|
529
|
+
const bw = bx1 - bx0, bh = by1 - by0;
|
|
530
|
+
const captured = out.children.filter((child) => {
|
|
531
|
+
if (child === parent || child.data["graphNode"] || child.data["svgEmbed"]) return false;
|
|
532
|
+
const b = child.bounds();
|
|
533
|
+
if (b.width <= 0 && b.height <= 0) return false;
|
|
534
|
+
if (b.width > bw + 6 || b.height > bh + 6) return false;
|
|
535
|
+
return b.x >= bx0 - 3 && b.y >= by0 - 3 && b.x + b.width <= bx1 + 3 && b.y + b.height <= by1 + 3;
|
|
536
|
+
});
|
|
537
|
+
if (captured.length > 0) {
|
|
538
|
+
const firstIdx = out.children.indexOf(captured[0]);
|
|
539
|
+
const capturedSet = new Set(captured);
|
|
540
|
+
out.children = out.children.filter((child) => !capturedSet.has(child));
|
|
541
|
+
out.children.splice(firstIdx, 0, group2);
|
|
542
|
+
group2.parent = out;
|
|
543
|
+
group2.data["graphNode"] = decodeSvgToken(nodeMatch[1]);
|
|
544
|
+
for (const item of captured) {
|
|
545
|
+
item.parent = group2;
|
|
546
|
+
group2.children.push(item);
|
|
547
|
+
}
|
|
548
|
+
register(group2);
|
|
549
|
+
for (const child of element.children) walk(child, group2, style, m, depth + 1, useStack);
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
register(group2);
|
|
556
|
+
parent.add(group2);
|
|
557
|
+
for (const child of element.children) walk(child, group2, style, m, depth + 1, useStack);
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
if (element.name === "use") {
|
|
561
|
+
const href = (element.attrs["href"] ?? element.attrs["xlink:href"] ?? "").replace(/^#/, "");
|
|
562
|
+
if (!href) return;
|
|
563
|
+
if (useStack.has(href)) throw new Error(`fromSvg encountered a circular <use> reference to #${href}.`);
|
|
564
|
+
const referenced = defs.get(href);
|
|
565
|
+
if (!referenced) return;
|
|
566
|
+
const nextUseStack = new Set(useStack);
|
|
567
|
+
nextUseStack.add(href);
|
|
568
|
+
const placed = mul(m, [1, 0, 0, 1, parseFloat(element.attrs["x"] ?? "0") || 0, parseFloat(element.attrs["y"] ?? "0") || 0]);
|
|
569
|
+
const group2 = new Node();
|
|
570
|
+
if (opacity < 1) group2.base.opacity = opacity;
|
|
571
|
+
parent.add(group2);
|
|
572
|
+
if (referenced.name === "symbol") for (const child of referenced.children) walk(child, group2, style, placed, depth + 1, nextUseStack);
|
|
573
|
+
else walk(referenced, group2, style, placed, depth + 1, nextUseStack);
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
if (element.name === "image") {
|
|
577
|
+
const href = element.attrs["href"] ?? element.attrs["xlink:href"];
|
|
578
|
+
if (!href) return;
|
|
579
|
+
const width = parseFloat(element.attrs["width"] ?? "0"), height = parseFloat(element.attrs["height"] ?? "0");
|
|
580
|
+
const ix = parseFloat(element.attrs["x"] ?? "0") || 0, iy = parseFloat(element.attrs["y"] ?? "0") || 0;
|
|
581
|
+
const inlineSvg = width > 0 && height > 0 && budget.embedDepth < 8 ? decodeSvgDataUri(href) : null;
|
|
582
|
+
if (inlineSvg) {
|
|
583
|
+
const subBudget = { nodes: budget.nodes, embedDepth: budget.embedDepth + 1 };
|
|
584
|
+
const inner = parseSvgInternal(inlineSvg, {
|
|
585
|
+
splitGlyphs: options.splitGlyphs,
|
|
586
|
+
background: true,
|
|
587
|
+
font: options.font
|
|
588
|
+
}, subBudget);
|
|
589
|
+
budget.nodes = subBudget.nodes;
|
|
590
|
+
let sx = inner.pictureWidth > 0 ? width / inner.pictureWidth : 1;
|
|
591
|
+
let sy = inner.pictureHeight > 0 ? height / inner.pictureHeight : 1;
|
|
592
|
+
const [alignment = "xMidYMid", fit = "meet"] = (element.attrs["preserveAspectRatio"] ?? "").trim().replace(/^defer\s+/, "").split(/\s+/).filter(Boolean);
|
|
593
|
+
let offsetX2 = width / 2, offsetY2 = height / 2;
|
|
594
|
+
if (alignment !== "none") {
|
|
595
|
+
sx = sy = fit === "slice" ? Math.max(sx, sy) : Math.min(sx, sy);
|
|
596
|
+
const align = /^x(Min|Mid|Max)Y(Min|Mid|Max)$/.exec(alignment);
|
|
597
|
+
const fraction = (value) => value === "Min" ? 0 : value === "Max" ? 1 : 0.5;
|
|
598
|
+
offsetX2 = inner.pictureWidth * sx / 2 + (width - inner.pictureWidth * sx) * fraction(align?.[1]);
|
|
599
|
+
offsetY2 = inner.pictureHeight * sy / 2 + (height - inner.pictureHeight * sy) * fraction(align?.[2]);
|
|
600
|
+
}
|
|
601
|
+
const embedMat = mul(m, [sx, 0, 0, sy, ix + offsetX2, iy + offsetY2]);
|
|
602
|
+
const holder = new Node().at(embedMat[4], embedMat[5]);
|
|
603
|
+
const rot = Math.atan2(embedMat[1], embedMat[0]);
|
|
604
|
+
holder.base.rotation = rot;
|
|
605
|
+
holder.base.sx = Math.hypot(embedMat[0], embedMat[1]);
|
|
606
|
+
holder.base.sy = Math.hypot(embedMat[2], embedMat[3]) * (embedMat[0] * embedMat[3] - embedMat[1] * embedMat[2] < 0 ? -1 : 1);
|
|
607
|
+
holder.clipPath = transformPath(rectPath(ix, iy, width, height), mul(invert(holder.localMatrix()), m));
|
|
608
|
+
holder.data["svgEmbed"] = true;
|
|
609
|
+
if (opacity < 1) holder.base.opacity = opacity;
|
|
610
|
+
for (const child of inner.children) {
|
|
611
|
+
child.parent = holder;
|
|
612
|
+
holder.children.push(child);
|
|
613
|
+
}
|
|
614
|
+
for (const [id, target] of inner.byId) {
|
|
615
|
+
if (!out.byId.has(id)) out.byId.set(id, target);
|
|
616
|
+
}
|
|
617
|
+
register(holder);
|
|
618
|
+
parent.add(holder);
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
const [x, y] = apply(m, ix, iy);
|
|
622
|
+
const unit = matScale(m);
|
|
623
|
+
const image = { src: href, width, height };
|
|
624
|
+
const node = new Node().at(x, y);
|
|
625
|
+
node.base.image = image;
|
|
626
|
+
node.base.sx = unit;
|
|
627
|
+
node.base.sy = unit;
|
|
628
|
+
if (opacity < 1) node.base.opacity = opacity;
|
|
629
|
+
register(node);
|
|
630
|
+
parent.add(node);
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
if (element.name === "text") {
|
|
634
|
+
const body = (element.text + element.children.map((child) => child.text).join("")).trim();
|
|
635
|
+
if (!body) return;
|
|
636
|
+
const color = safeColor(style.fill) ?? [0, 0, 0, 1];
|
|
637
|
+
const size = style.fontSize * matScale(m);
|
|
638
|
+
const named = options.font ?? filmFont();
|
|
639
|
+
const label = text(body, { size, color, font: named === null ? void 0 : MONOSPACE.test(style.family) ? "mono" : named });
|
|
640
|
+
const [x, y] = apply(m, parseFloat(element.attrs["x"] ?? "0") || 0, parseFloat(element.attrs["y"] ?? "0") || 0);
|
|
641
|
+
const half = label.get("px");
|
|
642
|
+
label.at(style.anchor === "middle" ? x : style.anchor === "end" ? x - half : x + half, y - size * 0.34);
|
|
643
|
+
if (opacity < 1) label.base.opacity = opacity;
|
|
644
|
+
register(label);
|
|
645
|
+
parent.add(label);
|
|
646
|
+
return;
|
|
647
|
+
}
|
|
648
|
+
const path = shapePath(element);
|
|
649
|
+
if (!path || path.length === 0) return;
|
|
650
|
+
const isBackground = firstShape && element.name === "rect" && !keepBackground && parseFloat(element.attrs["width"] ?? "0") >= viewW - 0.5 && parseFloat(element.attrs["height"] ?? "0") >= viewH - 0.5;
|
|
651
|
+
firstShape = false;
|
|
652
|
+
if (isBackground) return;
|
|
653
|
+
const leaves = leafFrom(path, m, element, style);
|
|
654
|
+
const markers = [];
|
|
655
|
+
if (element.name === "path" || element.name === "line" || element.name === "polyline") {
|
|
656
|
+
const startId = markerUrlOf(element, "marker-start", "markerStart");
|
|
657
|
+
if (startId) {
|
|
658
|
+
const startNode = emitMarker(startId, "start", path, m, style, depth, useStack);
|
|
659
|
+
if (startNode) markers.push(startNode);
|
|
660
|
+
}
|
|
661
|
+
const endId = markerUrlOf(element, "marker-end", "markerEnd");
|
|
662
|
+
if (endId) {
|
|
663
|
+
const endNode = emitMarker(endId, "end", path, m, style, depth, useStack);
|
|
664
|
+
if (endNode) markers.push(endNode);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
if (leaves.length === 0 && markers.length === 0) return;
|
|
668
|
+
if (leaves.length === 1 && markers.length === 0) {
|
|
669
|
+
if (opacity < 1) leaves[0].base.opacity = opacity;
|
|
670
|
+
register(leaves[0]);
|
|
671
|
+
parent.add(leaves[0]);
|
|
672
|
+
return;
|
|
673
|
+
}
|
|
674
|
+
const group = new Node();
|
|
675
|
+
if (opacity < 1) group.base.opacity = opacity;
|
|
676
|
+
group.add(...leaves, ...markers);
|
|
677
|
+
register(group);
|
|
678
|
+
parent.add(group);
|
|
679
|
+
};
|
|
680
|
+
for (const child of svg.children) walk(child, out, rootStyle, base);
|
|
681
|
+
return out;
|
|
682
|
+
}
|
|
683
|
+
function shiftPaint(paint, dx, dy) {
|
|
684
|
+
if (Array.isArray(paint)) return paint;
|
|
685
|
+
const gradient = paint;
|
|
686
|
+
if (gradient.type === "linear") {
|
|
687
|
+
return { ...gradient, from: [gradient.from[0] + dx, gradient.from[1] + dy], to: [gradient.to[0] + dx, gradient.to[1] + dy] };
|
|
688
|
+
}
|
|
689
|
+
if (gradient.type === "radial") return { ...gradient, center: [gradient.center[0] + dx, gradient.center[1] + dy] };
|
|
690
|
+
return gradient;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
export {
|
|
694
|
+
SvgNode,
|
|
695
|
+
fromSvg
|
|
696
|
+
};
|