@doki-land/davinci 0.0.0-stage → 0.0.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/dist/scene.js ADDED
@@ -0,0 +1,697 @@
1
+ // ../davinci-scene/dist/version.js
2
+ var SCENE_VERSION = "/v0";
3
+ function assertSceneVersion(version, stage) {
4
+ if (version !== SCENE_VERSION) {
5
+ throw new Error(`davinci.scene.unsupported-version: ${stage} wants ${SCENE_VERSION}, got ${JSON.stringify(version)}`);
6
+ }
7
+ }
8
+
9
+ // ../davinci-types/dist/geometry.js
10
+ var IDENTITY_AFFINE = {
11
+ a: 1,
12
+ b: 0,
13
+ c: 0,
14
+ d: 1,
15
+ tx: 0,
16
+ ty: 0
17
+ };
18
+ function multiplyTransform(a, b) {
19
+ return {
20
+ a: a.a * b.a + a.c * b.b,
21
+ b: a.b * b.a + a.d * b.b,
22
+ c: a.a * b.c + a.c * b.d,
23
+ d: a.b * b.c + a.d * b.d,
24
+ tx: a.a * b.tx + a.c * b.ty + a.tx,
25
+ ty: a.b * b.tx + a.d * b.ty + a.ty
26
+ };
27
+ }
28
+ function applyTransform(p, t) {
29
+ return {
30
+ x: t.a * p.x + t.c * p.y + t.tx,
31
+ y: t.b * p.x + t.d * p.y + t.ty
32
+ };
33
+ }
34
+
35
+ // ../davinci-types/dist/color.js
36
+ function toHex(c) {
37
+ const r = Math.round(c.r * 255).toString(16).padStart(2, "0");
38
+ const g = Math.round(c.g * 255).toString(16).padStart(2, "0");
39
+ const b = Math.round(c.b * 255).toString(16).padStart(2, "0");
40
+ if (c.a >= 1)
41
+ return `#${r}${g}${b}`;
42
+ const a = Math.round(c.a * 255).toString(16).padStart(2, "0");
43
+ return `#${r}${g}${b}${a}`;
44
+ }
45
+
46
+ // ../davinci-types/dist/diagnostics.js
47
+ function diag(severity, code, message, source) {
48
+ return { severity, code, message, source };
49
+ }
50
+
51
+ // ../davinci-scene/dist/validate.js
52
+ function validateScene(scene) {
53
+ const items = [];
54
+ if (scene.version !== SCENE_VERSION) {
55
+ items.push(diag("error", "davinci.scene.unsupported-version", `scene version wants ${SCENE_VERSION}, got ${JSON.stringify(scene.version)}`));
56
+ }
57
+ const idCounts = /* @__PURE__ */ new Map();
58
+ const queue = [scene.root];
59
+ while (queue.length > 0) {
60
+ const node = queue.shift();
61
+ idCounts.set(node.id, (idCounts.get(node.id) ?? 0) + 1);
62
+ checkGeometry(node, items);
63
+ if (node.clip && !(isFiniteRect(node.clip) && node.clip.w > 0 && node.clip.h > 0)) {
64
+ items.push(diag("error", "davinci.scene.invalid-clip", "clip rect must be finite and positive", node.id));
65
+ }
66
+ if (node.kind === "group")
67
+ queue.push(...node.children);
68
+ }
69
+ for (const [id, count] of idCounts) {
70
+ if (count > 1) {
71
+ items.push(diag("error", "davinci.scene.duplicate-id", `id "${id}" appears ${count} times in one scene`, id));
72
+ }
73
+ }
74
+ return items;
75
+ }
76
+ function isFiniteRect(r) {
77
+ return Number.isFinite(r.x) && Number.isFinite(r.y) && Number.isFinite(r.w) && Number.isFinite(r.h);
78
+ }
79
+ function checkGeometry(node, items) {
80
+ const bad = (what) => diag("error", "davinci.scene.invalid-geometry", `${what} must be finite`, node.id);
81
+ switch (node.kind) {
82
+ case "rect":
83
+ case "image":
84
+ if (!isFiniteRect(node))
85
+ items.push(bad(`${node.kind} rect`));
86
+ break;
87
+ case "ellipse":
88
+ if (![node.cx, node.cy, node.rx, node.ry].every(Number.isFinite))
89
+ items.push(bad("ellipse"));
90
+ break;
91
+ case "text":
92
+ if (node.anchor && !(Number.isFinite(node.anchor.x) && Number.isFinite(node.anchor.y))) {
93
+ items.push(bad("text anchor"));
94
+ }
95
+ break;
96
+ case "path":
97
+ for (const cmd of node.path.commands) {
98
+ if (!pointsOfCommand(cmd).every((p) => Number.isFinite(p.x) && Number.isFinite(p.y))) {
99
+ items.push(bad(`path ${cmd.kind}`));
100
+ break;
101
+ }
102
+ }
103
+ break;
104
+ case "marker":
105
+ if (!node.positions.every((p) => Number.isFinite(p.x) && Number.isFinite(p.y))) {
106
+ items.push(bad("marker positions"));
107
+ }
108
+ break;
109
+ case "group":
110
+ break;
111
+ }
112
+ }
113
+ function pointsOfCommand(cmd) {
114
+ switch (cmd.kind) {
115
+ case "move":
116
+ case "line":
117
+ case "arc":
118
+ return [cmd.to];
119
+ case "quadratic":
120
+ return [cmd.cp, cmd.to];
121
+ case "cubic":
122
+ return [cmd.cp1, cmd.cp2, cmd.to];
123
+ case "close":
124
+ return [];
125
+ }
126
+ }
127
+
128
+ // ../davinci-scene/dist/resolve.js
129
+ var DEFAULT_STROKE = { color: { r: 0, g: 0, b: 0, a: 1 }, width: 1 };
130
+ var DEFAULT_FILL = { color: { r: 0, g: 0, b: 0, a: 0 } };
131
+ var BLACK = { r: 0, g: 0, b: 0, a: 1 };
132
+ function resolveStroke(s) {
133
+ if (!s)
134
+ return void 0;
135
+ return {
136
+ color: s.color ?? DEFAULT_STROKE.color,
137
+ width: s.width ?? DEFAULT_STROKE.width,
138
+ dash: s.dash
139
+ };
140
+ }
141
+ function resolveFill(f) {
142
+ if (!f)
143
+ return void 0;
144
+ return { color: f.color ?? DEFAULT_FILL.color };
145
+ }
146
+ var TEXT_ADVANCE_RATIO = 0.6;
147
+ var WIDE_ADVANCE_RATIO = 1;
148
+ var TEXT_LINE_RATIO = 1.2;
149
+ var DEFAULT_TEXT_SIZE = 12;
150
+ var ELLIPSIS = "\u2026";
151
+ function estimateTextSize(text, sizePx) {
152
+ const size = Number.isFinite(sizePx) && sizePx > 0 ? sizePx : DEFAULT_TEXT_SIZE;
153
+ const lines = text.split("\n");
154
+ let widest = 0;
155
+ for (const line of lines)
156
+ widest = Math.max(widest, measureWidth(line, size));
157
+ return { width: widest, height: lines.length * size * TEXT_LINE_RATIO };
158
+ }
159
+ function truncateToWidth(text, maxWidth, sizePx) {
160
+ const size = Number.isFinite(sizePx) && sizePx > 0 ? sizePx : DEFAULT_TEXT_SIZE;
161
+ const limit = maxWidth > 0 ? maxWidth : 0;
162
+ if (measureWidth(text, size) <= limit)
163
+ return text;
164
+ const ellipsisWidth = measureWidth(ELLIPSIS, size);
165
+ if (ellipsisWidth > limit)
166
+ return "";
167
+ const chars = Array.from(text);
168
+ let used = ellipsisWidth;
169
+ let keep = 0;
170
+ for (const ch of chars) {
171
+ const advance = charAdvance(ch, size);
172
+ if (used + advance > limit)
173
+ break;
174
+ used += advance;
175
+ keep += 1;
176
+ }
177
+ return chars.slice(0, keep).join("") + ELLIPSIS;
178
+ }
179
+ function measureWidth(text, size) {
180
+ let width = 0;
181
+ for (const ch of Array.from(text))
182
+ width += charAdvance(ch, size);
183
+ return width;
184
+ }
185
+ function charAdvance(ch, size) {
186
+ return isWideCodepoint(ch.codePointAt(0) ?? 0) ? size * WIDE_ADVANCE_RATIO : size * TEXT_ADVANCE_RATIO;
187
+ }
188
+ function isWideCodepoint(cp) {
189
+ return cp >= 4352 && cp <= 4447 || // Hangul Jamo
190
+ cp >= 11904 && cp <= 12350 || // radicals, Kangxi, CJK symbols and punctuation
191
+ cp >= 12353 && cp <= 13311 || // kana, Hangul compatibility, enclosed CJK
192
+ cp >= 13312 && cp <= 19903 || // CJK Extension A
193
+ cp >= 19968 && cp <= 40959 || // CJK Unified Ideographs
194
+ cp >= 40960 && cp <= 42191 || // Yi
195
+ cp >= 44032 && cp <= 55203 || // Hangul syllables
196
+ cp >= 63744 && cp <= 64255 || // CJK Compatibility Ideographs
197
+ cp >= 65072 && cp <= 65103 || // CJK Form Variants
198
+ cp >= 65280 && cp <= 65376 || // Fullwidth Forms
199
+ cp >= 65504 && cp <= 65510;
200
+ }
201
+ function fontCssOf(font) {
202
+ const size = Number.isFinite(font.sizePx) && font.sizePx > 0 ? font.sizePx : DEFAULT_TEXT_SIZE;
203
+ const style = font.italic ? "italic " : "";
204
+ const weight = font.weight ?? 400;
205
+ return `${style}${weight} ${size}px ${fontFamilyCss(font.family)}`;
206
+ }
207
+ function fontFamilyCss(family) {
208
+ const entries = (family || "sans-serif").split(",").map((entry) => entry.trim()).filter((entry) => entry.length > 0);
209
+ if (entries.length === 0)
210
+ return "sans-serif";
211
+ return entries.map((entry) => {
212
+ const unquoted = entry.replace(/^['"]|['"]$/g, "");
213
+ return /\s/.test(unquoted) ? `"${unquoted}"` : unquoted;
214
+ }).join(", ");
215
+ }
216
+ function resolve(scene, opts = {}) {
217
+ assertSceneVersion(scene.version, "resolve");
218
+ const keepIds = opts.preserveNodeIds === true;
219
+ const root = resolveNode(scene.root, IDENTITY_AFFINE, void 0, keepIds);
220
+ const width = opts.width ?? root.bounds.w;
221
+ const height = opts.height ?? root.bounds.h;
222
+ return { version: SCENE_VERSION, root, width, height };
223
+ }
224
+ function resolveNode(node, parentTransform, parentClip, keepIds) {
225
+ const local = node.transform ?? IDENTITY_AFFINE;
226
+ const worldTransform = multiplyTransform(parentTransform, local);
227
+ const worldClip = intersectClip(parentClip, node.clip);
228
+ const base = keepIds ? { worldTransform, worldClip, id: node.id } : { worldTransform, worldClip };
229
+ switch (node.kind) {
230
+ case "group": {
231
+ const children = node.children.map((c) => resolveNode(c, worldTransform, worldClip, keepIds));
232
+ const bounds = unionAll(children.map((c) => c.bounds));
233
+ return { kind: "group", children, bounds, ...base };
234
+ }
235
+ case "path": {
236
+ const bounds = transformRect(boundsOfPath(node.path.commands), worldTransform);
237
+ return { kind: "path", path: node.path, stroke: resolveStroke(node.stroke), fill: resolveFill(node.fill), bounds, ...base };
238
+ }
239
+ case "rect": {
240
+ const localRect = { x: node.x, y: node.y, w: node.w, h: node.h };
241
+ const bounds = transformRect(localRect, worldTransform);
242
+ return { kind: "rect", x: node.x, y: node.y, w: node.w, h: node.h, rx: node.rx ?? 0, ry: node.ry ?? 0, stroke: resolveStroke(node.stroke), fill: resolveFill(node.fill), bounds, ...base };
243
+ }
244
+ case "ellipse": {
245
+ const local2 = { x: node.cx - node.rx, y: node.cy - node.ry, w: node.rx * 2, h: node.ry * 2 };
246
+ const bounds = transformRect(local2, worldTransform);
247
+ return { kind: "ellipse", cx: node.cx, cy: node.cy, rx: node.rx, ry: node.ry, stroke: resolveStroke(node.stroke), fill: resolveFill(node.fill), bounds, ...base };
248
+ }
249
+ case "image": {
250
+ const localRect = { x: node.x, y: node.y, w: node.w, h: node.h };
251
+ const bounds = transformRect(localRect, worldTransform);
252
+ return { kind: "image", resourceRef: node.resourceRef, x: node.x, y: node.y, w: node.w, h: node.h, bounds, ...base };
253
+ }
254
+ case "text": {
255
+ const anchor = node.anchor ?? { x: 0, y: 0 };
256
+ const size = estimateTextSize(node.text, node.font.sizePx);
257
+ const localRect = {
258
+ x: anchor.x - size.width / 2,
259
+ y: anchor.y - size.height / 2,
260
+ w: size.width,
261
+ h: size.height
262
+ };
263
+ const bounds = transformRect(localRect, worldTransform);
264
+ return {
265
+ kind: "text",
266
+ text: node.text,
267
+ fontCss: fontCssOf(node.font),
268
+ anchor,
269
+ color: node.color ?? BLACK,
270
+ bounds,
271
+ ...base
272
+ };
273
+ }
274
+ case "marker": {
275
+ const children = node.positions.map((p) => ({
276
+ kind: "group",
277
+ children: [],
278
+ worldTransform: multiplyTransform(worldTransform, { ...IDENTITY_AFFINE, tx: p.x, ty: p.y }),
279
+ worldClip,
280
+ bounds: { x: p.x, y: p.y, w: 0, h: 0 }
281
+ }));
282
+ const bounds = unionAll(children.map((c) => c.bounds));
283
+ return { kind: "group", children, bounds, ...base };
284
+ }
285
+ }
286
+ }
287
+ function intersectClip(a, b) {
288
+ if (!a)
289
+ return b;
290
+ if (!b)
291
+ return a;
292
+ const x = Math.max(a.x, b.x);
293
+ const y = Math.max(a.y, b.y);
294
+ const w = Math.min(a.x + a.w, b.x + b.w) - x;
295
+ const h = Math.min(a.y + a.h, b.y + b.h) - y;
296
+ if (w <= 0 || h <= 0)
297
+ return void 0;
298
+ return { x, y, w, h };
299
+ }
300
+ function unionRect(a, b) {
301
+ const x = Math.min(a.x, b.x);
302
+ const y = Math.min(a.y, b.y);
303
+ return {
304
+ x,
305
+ y,
306
+ w: Math.max(a.x + a.w, b.x + b.w) - x,
307
+ h: Math.max(a.y + a.h, b.y + b.h) - y
308
+ };
309
+ }
310
+ function unionAll(rects) {
311
+ if (rects.length === 0)
312
+ return { x: 0, y: 0, w: 0, h: 0 };
313
+ return rects.slice(1).reduce((acc, b) => unionRect(acc, b), rects[0]);
314
+ }
315
+ function transformRect(r, t) {
316
+ const tl = applyTransform({ x: r.x, y: r.y }, t);
317
+ const tr = applyTransform({ x: r.x + r.w, y: r.y }, t);
318
+ const bl = applyTransform({ x: r.x, y: r.y + r.h }, t);
319
+ const br = applyTransform({ x: r.x + r.w, y: r.y + r.h }, t);
320
+ const xs = [tl.x, tr.x, bl.x, br.x];
321
+ const ys = [tl.y, tr.y, bl.y, br.y];
322
+ const minX = Math.min(...xs);
323
+ const maxX = Math.max(...xs);
324
+ const minY = Math.min(...ys);
325
+ const maxY = Math.max(...ys);
326
+ return { x: minX, y: minY, w: maxX - minX, h: maxY - minY };
327
+ }
328
+ function boundsOfPath(commands) {
329
+ let minX = Number.POSITIVE_INFINITY;
330
+ let minY = Number.POSITIVE_INFINITY;
331
+ let maxX = Number.NEGATIVE_INFINITY;
332
+ let maxY = Number.NEGATIVE_INFINITY;
333
+ const add = (p) => {
334
+ if (p.x < minX)
335
+ minX = p.x;
336
+ if (p.x > maxX)
337
+ maxX = p.x;
338
+ if (p.y < minY)
339
+ minY = p.y;
340
+ if (p.y > maxY)
341
+ maxY = p.y;
342
+ };
343
+ for (const cmd of commands) {
344
+ switch (cmd.kind) {
345
+ case "move":
346
+ case "line":
347
+ add(cmd.to);
348
+ break;
349
+ case "cubic":
350
+ add(cmd.cp1);
351
+ add(cmd.cp2);
352
+ add(cmd.to);
353
+ break;
354
+ case "quadratic":
355
+ add(cmd.cp);
356
+ add(cmd.to);
357
+ break;
358
+ case "arc":
359
+ add(cmd.to);
360
+ break;
361
+ case "close":
362
+ break;
363
+ }
364
+ }
365
+ if (!Number.isFinite(minX))
366
+ return { x: 0, y: 0, w: 0, h: 0 };
367
+ return { x: minX, y: minY, w: maxX - minX, h: maxY - minY };
368
+ }
369
+
370
+ // ../davinci-scene/dist/svg.js
371
+ var SAFE_ID_PREFIX = /^[A-Za-z_][A-Za-z0-9_.-]*$/;
372
+ function resolveResourceIdPrefix(family, identity, explicit) {
373
+ if (typeof explicit === "string" && explicit !== "")
374
+ return explicit;
375
+ return `${family}-${fnv1aHex(JSON.stringify(identity) ?? "")}-`;
376
+ }
377
+ function fnv1aHex(text) {
378
+ let hash = 2166136261;
379
+ for (let i = 0; i < text.length; i++) {
380
+ hash ^= text.charCodeAt(i);
381
+ hash = Math.imul(hash, 16777619);
382
+ }
383
+ return (hash >>> 0).toString(16);
384
+ }
385
+ function renderSvg(scene, options = {}) {
386
+ assertSceneVersion(scene.version, "renderSvg");
387
+ const idPrefix = options.resourceIdPrefix ?? "";
388
+ if (idPrefix !== "" && !SAFE_ID_PREFIX.test(idPrefix)) {
389
+ throw new Error(`davinci.scene.invalid-resource-prefix: ${JSON.stringify(idPrefix)} is not a safe id token`);
390
+ }
391
+ const ctx = { clips: [], clipSeq: 0, idPrefix };
392
+ const body = [];
393
+ walk(scene.root, ctx, body);
394
+ const width = dimension(scene.width);
395
+ const height = dimension(scene.height);
396
+ const lines = [
397
+ `<svg xmlns="http://www.w3.org/2000/svg" width="${num(width)}" height="${num(height)}" viewBox="0 0 ${num(width)} ${num(height)}" preserveAspectRatio="xMidYMid meet">`
398
+ ];
399
+ if (scene.background) {
400
+ lines.push(` <rect x="0" y="0" width="${num(width)}" height="${num(height)}"${fillAttrs(scene.background)}/>`);
401
+ }
402
+ if (ctx.clips.length > 0) {
403
+ lines.push(" <defs>");
404
+ for (const clip of ctx.clips) {
405
+ lines.push(` <clipPath id="${clip.id}"><rect x="${num(clip.rect.x)}" y="${num(clip.rect.y)}" width="${num(clip.rect.w)}" height="${num(clip.rect.h)}"/></clipPath>`);
406
+ }
407
+ lines.push(" </defs>");
408
+ }
409
+ lines.push(...body);
410
+ lines.push("</svg>");
411
+ return lines.join("\n");
412
+ }
413
+ function walk(group, ctx, out) {
414
+ for (const child of group.children) {
415
+ if (child.kind === "group") {
416
+ walk(child, ctx, out);
417
+ continue;
418
+ }
419
+ out.push(...leafLines(child, ctx));
420
+ }
421
+ }
422
+ function leafLines(node, ctx) {
423
+ const transform = transformAttr(node.worldTransform);
424
+ const clipId = node.worldClip ? clipFor(node.worldClip, ctx) : null;
425
+ const element = elementMarkup(node, transform);
426
+ if (!clipId)
427
+ return [` ${element}`];
428
+ return [` <g clip-path="url(#${clipId})">`, ` ${element}`, " </g>"];
429
+ }
430
+ function elementMarkup(node, transform) {
431
+ const transformAttr2 = transform ? ` transform="${transform}"` : "";
432
+ const idAttr = node.id ? ` data-dvz-id="${esc(node.id)}"` : "";
433
+ switch (node.kind) {
434
+ case "path": {
435
+ const fill = node.fill ? fillAttrs(node.fill.color) : ' fill="none"';
436
+ return `<path${idAttr} d="${pathData(node.path)}"${fill}${strokeAttrs(node.stroke)}${transformAttr2}/>`;
437
+ }
438
+ case "rect": {
439
+ const radii = node.rx > 0 || node.ry > 0 ? ` rx="${num(node.rx)}" ry="${num(node.ry)}"` : "";
440
+ return `<rect${idAttr} x="${num(node.x)}" y="${num(node.y)}" width="${num(node.w)}" height="${num(node.h)}"${radii}${paintAttrs(node.fill, node.stroke)}${transformAttr2}/>`;
441
+ }
442
+ case "ellipse": {
443
+ return `<ellipse${idAttr} cx="${num(node.cx)}" cy="${num(node.cy)}" rx="${num(node.rx)}" ry="${num(node.ry)}"${paintAttrs(node.fill, node.stroke)}${transformAttr2}/>`;
444
+ }
445
+ case "image": {
446
+ return `<image${idAttr} x="${num(node.x)}" y="${num(node.y)}" width="${num(node.w)}" height="${num(node.h)}" href="${esc(node.resourceRef)}" preserveAspectRatio="xMidYMid meet"${transformAttr2}/>`;
447
+ }
448
+ case "text": {
449
+ const font = node.fontCss ? ` style="font:${esc(node.fontCss)}"` : "";
450
+ return `<text${idAttr} x="${num(node.anchor.x)}" y="${num(node.anchor.y)}" text-anchor="middle" dominant-baseline="middle"${fillAttrs(node.color)}${font}${transformAttr2}>${esc(node.text)}</text>`;
451
+ }
452
+ }
453
+ }
454
+ function paintAttrs(fill, stroke) {
455
+ const fillPart = fill ? fillAttrs(fill.color) : ' fill="none"';
456
+ return `${fillPart}${strokeAttrs(stroke)}`;
457
+ }
458
+ function fillAttrs(color) {
459
+ const opacity = color.a < 1 ? ` fill-opacity="${num(color.a)}"` : "";
460
+ return ` fill="${toHex({ ...color, a: 1 })}"${opacity}`;
461
+ }
462
+ function strokeAttrs(stroke) {
463
+ if (!stroke)
464
+ return "";
465
+ const opacity = stroke.color.a < 1 ? ` stroke-opacity="${num(stroke.color.a)}"` : "";
466
+ const dash = stroke.dash && stroke.dash.length > 0 ? ` stroke-dasharray="${stroke.dash.map((d) => num(d)).join(" ")}"` : "";
467
+ return ` stroke="${toHex({ ...stroke.color, a: 1 })}" stroke-width="${num(stroke.width)}"${opacity}${dash}`;
468
+ }
469
+ function pathData(path) {
470
+ return path.commands.map(commandText).join(" ");
471
+ }
472
+ function commandText(command) {
473
+ switch (command.kind) {
474
+ case "move":
475
+ return `M ${num(command.to.x)} ${num(command.to.y)}`;
476
+ case "line":
477
+ return `L ${num(command.to.x)} ${num(command.to.y)}`;
478
+ case "cubic":
479
+ return `C ${num(command.cp1.x)} ${num(command.cp1.y)} ${num(command.cp2.x)} ${num(command.cp2.y)} ${num(command.to.x)} ${num(command.to.y)}`;
480
+ case "quadratic":
481
+ return `Q ${num(command.cp.x)} ${num(command.cp.y)} ${num(command.to.x)} ${num(command.to.y)}`;
482
+ case "arc":
483
+ return `A ${num(command.rx)} ${num(command.ry)} ${num(command.xRot)} ${command.largeArc ? 1 : 0} ${command.sweep ? 1 : 0} ${num(command.to.x)} ${num(command.to.y)}`;
484
+ case "close":
485
+ return "Z";
486
+ }
487
+ }
488
+ function transformAttr(transform) {
489
+ const identity = transform.a === 1 && transform.b === 0 && transform.c === 0 && transform.d === 1 && transform.tx === 0 && transform.ty === 0;
490
+ if (identity)
491
+ return "";
492
+ return `matrix(${num(transform.a)},${num(transform.b)},${num(transform.c)},${num(transform.d)},${num(transform.tx)},${num(transform.ty)})`;
493
+ }
494
+ function clipFor(rect, ctx) {
495
+ const id = `${ctx.idPrefix}clip-${ctx.clipSeq++}`;
496
+ ctx.clips.push({ id, rect });
497
+ return id;
498
+ }
499
+ function num(value) {
500
+ if (!Number.isFinite(value))
501
+ return "0";
502
+ const rounded = Math.round(value * 1e3) / 1e3;
503
+ return Object.is(rounded, -0) ? "0" : String(rounded);
504
+ }
505
+ function dimension(value) {
506
+ return Number.isFinite(value) && value > 0 ? value : 0;
507
+ }
508
+ function esc(text) {
509
+ return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
510
+ }
511
+
512
+ // ../davinci-scene/dist/canvas.js
513
+ function renderCanvas(scene, ctx) {
514
+ assertSceneVersion(scene.version, "renderCanvas");
515
+ if (scene.background) {
516
+ ctx.save();
517
+ ctx.fillStyle = css(scene.background);
518
+ ctx.fillRect(0, 0, scene.width, scene.height);
519
+ ctx.restore();
520
+ }
521
+ walkGroup(scene.root, ctx);
522
+ }
523
+ function walkGroup(group, ctx) {
524
+ for (const child of group.children) {
525
+ if (child.kind === "group") {
526
+ walkGroup(child, ctx);
527
+ continue;
528
+ }
529
+ drawLeaf(child, ctx);
530
+ }
531
+ }
532
+ function drawLeaf(node, ctx) {
533
+ ctx.save();
534
+ if (node.worldClip) {
535
+ ctx.beginPath();
536
+ ctx.rect(node.worldClip.x, node.worldClip.y, node.worldClip.w, node.worldClip.h);
537
+ ctx.clip();
538
+ }
539
+ applyTransform2(node.worldTransform, ctx);
540
+ switch (node.kind) {
541
+ case "rect": {
542
+ ctx.beginPath();
543
+ if (node.rx > 0 || node.ry > 0) {
544
+ roundedRect(ctx, node.x, node.y, node.w, node.h, Math.max(node.rx, node.ry));
545
+ } else {
546
+ ctx.rect(node.x, node.y, node.w, node.h);
547
+ }
548
+ paint(ctx, node.fill?.color, node.stroke?.color, node.stroke?.width, node.stroke?.dash);
549
+ break;
550
+ }
551
+ case "ellipse": {
552
+ ctx.beginPath();
553
+ ctx.ellipse(node.cx, node.cy, node.rx, node.ry, 0, 0, Math.PI * 2);
554
+ paint(ctx, node.fill?.color, node.stroke?.color, node.stroke?.width, node.stroke?.dash);
555
+ break;
556
+ }
557
+ case "path": {
558
+ ctx.beginPath();
559
+ tracePath(node.path, ctx);
560
+ paint(ctx, node.fill?.color, node.stroke?.color, node.stroke?.width, node.stroke?.dash);
561
+ break;
562
+ }
563
+ case "text": {
564
+ ctx.font = node.fontCss;
565
+ ctx.textAlign = "center";
566
+ ctx.textBaseline = "middle";
567
+ ctx.fillStyle = css(node.color);
568
+ ctx.fillText(node.text, node.anchor.x, node.anchor.y);
569
+ break;
570
+ }
571
+ case "image":
572
+ break;
573
+ }
574
+ ctx.restore();
575
+ }
576
+ function paint(ctx, fill, stroke, strokeWidth, dash) {
577
+ if (fill) {
578
+ ctx.fillStyle = css(fill);
579
+ ctx.fill();
580
+ }
581
+ if (stroke && strokeWidth && strokeWidth > 0) {
582
+ ctx.strokeStyle = css(stroke);
583
+ ctx.lineWidth = strokeWidth;
584
+ ctx.setLineDash(dash ? [...dash] : []);
585
+ ctx.stroke();
586
+ ctx.setLineDash([]);
587
+ }
588
+ }
589
+ function applyTransform2(t, ctx) {
590
+ const identity = t.a === 1 && t.b === 0 && t.c === 0 && t.d === 1 && t.tx === 0 && t.ty === 0;
591
+ if (!identity)
592
+ ctx.transform(t.a, t.b, t.c, t.d, t.tx, t.ty);
593
+ }
594
+ function tracePath(path, ctx) {
595
+ let cx = 0;
596
+ let cy = 0;
597
+ for (const cmd of path.commands) {
598
+ switch (cmd.kind) {
599
+ case "move":
600
+ ctx.moveTo(cmd.to.x, cmd.to.y);
601
+ cx = cmd.to.x;
602
+ cy = cmd.to.y;
603
+ break;
604
+ case "line":
605
+ ctx.lineTo(cmd.to.x, cmd.to.y);
606
+ cx = cmd.to.x;
607
+ cy = cmd.to.y;
608
+ break;
609
+ case "quadratic":
610
+ ctx.quadraticCurveTo(cmd.cp.x, cmd.cp.y, cmd.to.x, cmd.to.y);
611
+ cx = cmd.to.x;
612
+ cy = cmd.to.y;
613
+ break;
614
+ case "cubic":
615
+ ctx.bezierCurveTo(cmd.cp1.x, cmd.cp1.y, cmd.cp2.x, cmd.cp2.y, cmd.to.x, cmd.to.y);
616
+ cx = cmd.to.x;
617
+ cy = cmd.to.y;
618
+ break;
619
+ case "arc":
620
+ traceArc(ctx, cx, cy, cmd);
621
+ cx = cmd.to.x;
622
+ cy = cmd.to.y;
623
+ break;
624
+ case "close":
625
+ ctx.closePath();
626
+ break;
627
+ }
628
+ }
629
+ }
630
+ function traceArc(ctx, x0, y0, cmd) {
631
+ const phi = cmd.xRot * Math.PI / 180;
632
+ const cosPhi = Math.cos(phi);
633
+ const sinPhi = Math.sin(phi);
634
+ const x1 = cmd.to.x;
635
+ const y1 = cmd.to.y;
636
+ const dx = (x0 - x1) / 2;
637
+ const dy = (y0 - y1) / 2;
638
+ const x1p = cosPhi * dx + sinPhi * dy;
639
+ const y1p = -sinPhi * dx + cosPhi * dy;
640
+ let rx = Math.abs(cmd.rx);
641
+ let ry = Math.abs(cmd.ry);
642
+ const lambda = x1p * x1p / (rx * rx) + y1p * y1p / (ry * ry);
643
+ if (lambda > 1) {
644
+ const s = Math.sqrt(lambda);
645
+ rx *= s;
646
+ ry *= s;
647
+ }
648
+ const rx2 = rx * rx;
649
+ const ry2 = ry * ry;
650
+ const denom = rx2 * y1p * y1p + ry2 * x1p * x1p;
651
+ const factor = (cmd.largeArc !== cmd.sweep ? 1 : -1) * Math.sqrt(Math.max(0, (rx2 * ry2 - denom) / Math.max(denom, 1e-12)));
652
+ const cxp = factor * rx * y1p / ry;
653
+ const cyp = -factor * ry * x1p / rx;
654
+ const cx = cosPhi * cxp - sinPhi * cyp + (x0 + x1) / 2;
655
+ const cy = sinPhi * cxp + cosPhi * cyp + (y0 + y1) / 2;
656
+ const angleBetween = (ux, uy, vx, vy) => {
657
+ const dot = ux * vx + uy * vy;
658
+ const len = Math.sqrt((ux * ux + uy * uy) * (vx * vx + vy * vy));
659
+ let a = Math.acos(Math.max(-1, Math.min(1, dot / Math.max(len, 1e-12))));
660
+ if (ux * vy - uy * vx < 0)
661
+ a = -a;
662
+ return a;
663
+ };
664
+ const start = angleBetween(1, 0, (x1p - cxp) / rx, (y1p - cyp) / ry);
665
+ const end = angleBetween((x1p - cxp) / rx, (y1p - cyp) / ry, (-x1p - cxp) / rx, (-y1p - cyp) / ry);
666
+ ctx.ellipse(cx, cy, rx, ry, phi, start, start + end, !cmd.sweep);
667
+ }
668
+ function roundedRect(ctx, x, y, w, h, r) {
669
+ const rr = Math.min(r, w / 2, h / 2);
670
+ ctx.moveTo(x + rr, y);
671
+ ctx.lineTo(x + w - rr, y);
672
+ ctx.quadraticCurveTo(x + w, y, x + w, y + rr);
673
+ ctx.lineTo(x + w, y + h - rr);
674
+ ctx.quadraticCurveTo(x + w, y + h, x + w - rr, y + h);
675
+ ctx.lineTo(x + rr, y + h);
676
+ ctx.quadraticCurveTo(x, y + h, x, y + h - rr);
677
+ ctx.lineTo(x, y + rr);
678
+ ctx.quadraticCurveTo(x, y, x + rr, y);
679
+ ctx.closePath();
680
+ }
681
+ function css(color) {
682
+ const hex = toHex({ ...color, a: 1 });
683
+ if (color.a >= 1)
684
+ return hex;
685
+ return `rgba(${Math.round(color.r)}, ${Math.round(color.g)}, ${Math.round(color.b)}, ${color.a})`;
686
+ }
687
+ export {
688
+ SCENE_VERSION,
689
+ assertSceneVersion,
690
+ estimateTextSize,
691
+ renderCanvas,
692
+ renderSvg,
693
+ resolve,
694
+ resolveResourceIdPrefix,
695
+ truncateToWidth,
696
+ validateScene
697
+ };