@tioniq/sceniq 0.0.0-stage → 0.1.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.
@@ -0,0 +1,1122 @@
1
+ import {
2
+ Color,
3
+ DrawableContent
4
+ } from "../chunk-J4XB4OPH.js";
5
+ import {
6
+ Point,
7
+ RectBounds,
8
+ Size,
9
+ View
10
+ } from "../chunk-CVQBUUHT.js";
11
+
12
+ // src/svg/svg-draw-context.ts
13
+ function withCustomFill(dc, customFill) {
14
+ if (dc.customFill === customFill) {
15
+ return dc;
16
+ }
17
+ return { ...dc, customFill };
18
+ }
19
+ function withGlobalFill(dc, globalFill) {
20
+ if (globalFill === void 0 || dc.globalFill === globalFill) {
21
+ return dc;
22
+ }
23
+ return { ...dc, globalFill };
24
+ }
25
+ function collectDefs(items, dc) {
26
+ for (const item of items) {
27
+ if (item.type === "mask") {
28
+ dc.masks.set(item.id, item);
29
+ } else if (item.type === "clipPath") {
30
+ dc.clipPaths.set(item.id, item);
31
+ } else if (item.type === "defs") {
32
+ for (const child of item.children) {
33
+ const asAny = child;
34
+ if ("id" in child && asAny.id) {
35
+ dc.defs.set(asAny.id, child);
36
+ }
37
+ collectDefs([child], dc);
38
+ }
39
+ } else if (item.type === "g") {
40
+ collectDefs(item.children, dc);
41
+ }
42
+ }
43
+ }
44
+ function buildDrawContext(items, globalFill, defaultLineWidth, defaultLineCap, defaultLineJoin) {
45
+ const dc = {
46
+ masks: /* @__PURE__ */ new Map(),
47
+ clipPaths: /* @__PURE__ */ new Map(),
48
+ defs: /* @__PURE__ */ new Map(),
49
+ globalFill,
50
+ customFill: null,
51
+ defaultLineWidth,
52
+ defaultLineCap,
53
+ defaultLineJoin
54
+ };
55
+ collectDefs(items, dc);
56
+ return dc;
57
+ }
58
+
59
+ // src/svg/path-cache.ts
60
+ var cache = /* @__PURE__ */ new WeakMap();
61
+ function cachedPath(item, source, build) {
62
+ const entry = cache.get(item);
63
+ if (entry !== void 0 && entry.source === source) {
64
+ return entry.path;
65
+ }
66
+ const path = build(source);
67
+ cache.set(item, { source, path });
68
+ return path;
69
+ }
70
+
71
+ // src/svg/svg-render-helpers.ts
72
+ function resolveFill(itemFill, customFill, globalFill) {
73
+ if (customFill) {
74
+ return customFill instanceof Color ? customFill.toHexString() : customFill;
75
+ }
76
+ if (itemFill !== void 0) {
77
+ return itemFill;
78
+ }
79
+ return globalFill;
80
+ }
81
+ function applyTransform(ctx, transform) {
82
+ if (!transform) {
83
+ return;
84
+ }
85
+ for (const op of transform.ops) {
86
+ switch (op.kind) {
87
+ case "translate":
88
+ ctx.translate(op.tx, op.ty);
89
+ break;
90
+ case "scale":
91
+ ctx.scale(op.sx, op.sy);
92
+ break;
93
+ case "rotate": {
94
+ if (op.cx !== 0 || op.cy !== 0) {
95
+ ctx.translate(op.cx, op.cy);
96
+ ctx.rotate(op.angle * Math.PI / 180);
97
+ ctx.translate(-op.cx, -op.cy);
98
+ } else {
99
+ ctx.rotate(op.angle * Math.PI / 180);
100
+ }
101
+ break;
102
+ }
103
+ case "skewX":
104
+ ctx.transform(1, 0, Math.tan(op.angle * Math.PI / 180), 1, 0, 0);
105
+ break;
106
+ case "skewY":
107
+ ctx.transform(1, Math.tan(op.angle * Math.PI / 180), 0, 1, 0, 0);
108
+ break;
109
+ case "matrix":
110
+ ctx.transform(op.a, op.b, op.c, op.d, op.e, op.f);
111
+ break;
112
+ }
113
+ }
114
+ }
115
+ function setDash(ctx, style) {
116
+ if (style.strokeDasharray && style.strokeDasharray.length > 0) {
117
+ ctx.setLineDash(style.strokeDasharray);
118
+ ctx.lineDashOffset = style.strokeDashoffset ?? 0;
119
+ } else {
120
+ ctx.setLineDash([]);
121
+ ctx.lineDashOffset = 0;
122
+ }
123
+ }
124
+ function withOpacity(ctx, opacity, fn) {
125
+ if (opacity !== null && opacity !== void 0 && opacity !== 1) {
126
+ const prev = ctx.globalAlpha;
127
+ ctx.globalAlpha = prev * opacity;
128
+ fn();
129
+ ctx.globalAlpha = prev;
130
+ } else {
131
+ fn();
132
+ }
133
+ }
134
+ function applyClipItems(ctx, items) {
135
+ for (const child of items) {
136
+ if (child.type === "path") {
137
+ ctx.clip(cachedPath(child, child.d, (d) => new Path2D(d)), child.clipRule ?? void 0);
138
+ } else if (child.type === "rect") {
139
+ const p = new Path2D();
140
+ const rx = child.rx ?? child.ry ?? 0;
141
+ const ry = child.ry ?? child.rx ?? 0;
142
+ if (rx > 0 || ry > 0) {
143
+ p.roundRect(child.x, child.y, child.width, child.height, [rx, ry]);
144
+ } else {
145
+ p.rect(child.x, child.y, child.width, child.height);
146
+ }
147
+ ctx.clip(p);
148
+ }
149
+ }
150
+ }
151
+
152
+ // src/svg/svg-draw.ts
153
+ function drawItems(ctx, items, dc) {
154
+ for (const item of items) {
155
+ drawItem(ctx, item, dc);
156
+ }
157
+ }
158
+ function drawItem(ctx, item, dc) {
159
+ switch (item.type) {
160
+ case "path":
161
+ drawPath(ctx, item, dc);
162
+ break;
163
+ case "circle":
164
+ drawCircle(ctx, item, dc);
165
+ break;
166
+ case "ellipse":
167
+ drawEllipse(ctx, item, dc);
168
+ break;
169
+ case "rect":
170
+ drawRect(ctx, item, dc);
171
+ break;
172
+ case "line":
173
+ drawLine(ctx, item, dc);
174
+ break;
175
+ case "polyline":
176
+ drawPolyShape(ctx, item, dc, false);
177
+ break;
178
+ case "polygon":
179
+ drawPolyShape(ctx, item, dc, true);
180
+ break;
181
+ case "text":
182
+ drawText(ctx, item, dc);
183
+ break;
184
+ case "g":
185
+ drawGroup(ctx, item, dc);
186
+ break;
187
+ case "use":
188
+ drawUse(ctx, item, dc);
189
+ break;
190
+ case "defs":
191
+ case "mask":
192
+ case "clipPath":
193
+ break;
194
+ }
195
+ }
196
+ function pushClip(ctx, style, dc) {
197
+ const clipId = style.clipPath ? getRefId(style.clipPath) : null;
198
+ const maskId = style.mask ? getRefId(style.mask) : null;
199
+ if (!clipId && !maskId) {
200
+ return false;
201
+ }
202
+ ctx.save();
203
+ if (clipId) {
204
+ const clipDef = dc.clipPaths.get(clipId);
205
+ if (clipDef) {
206
+ applyClipItems(ctx, clipDef.children);
207
+ }
208
+ }
209
+ if (maskId) {
210
+ const maskDef = dc.masks.get(maskId);
211
+ if (maskDef) {
212
+ applyClipItems(ctx, maskDef.children);
213
+ }
214
+ }
215
+ return true;
216
+ }
217
+ function strokeAndFillShape(ctx, path2d, style, dc) {
218
+ const fillColor = resolveFill(style.fill, dc.customFill, dc.globalFill);
219
+ const stroke = style.stroke;
220
+ withOpacity(ctx, style.opacity, () => {
221
+ if (fillColor !== null && fillColor !== "none") {
222
+ const prevAlpha = ctx.globalAlpha;
223
+ if (style.fillOpacity !== null && style.fillOpacity !== void 0) {
224
+ ctx.globalAlpha = prevAlpha * style.fillOpacity;
225
+ }
226
+ ctx.fillStyle = fillColor;
227
+ ctx.fill(path2d, style.fillRule ?? void 0);
228
+ ctx.globalAlpha = prevAlpha;
229
+ }
230
+ if (stroke && stroke !== "none") {
231
+ const prevAlpha = ctx.globalAlpha;
232
+ if (style.strokeOpacity !== null && style.strokeOpacity !== void 0) {
233
+ ctx.globalAlpha = prevAlpha * style.strokeOpacity;
234
+ }
235
+ ctx.lineJoin = style.strokeLinejoin ?? dc.defaultLineJoin;
236
+ ctx.lineCap = style.strokeLinecap ?? dc.defaultLineCap;
237
+ ctx.lineWidth = style.strokeWidth ?? dc.defaultLineWidth;
238
+ ctx.strokeStyle = stroke;
239
+ setDash(ctx, style);
240
+ ctx.stroke(path2d);
241
+ ctx.globalAlpha = prevAlpha;
242
+ }
243
+ });
244
+ }
245
+ function drawPath(ctx, item, dc) {
246
+ const path2d = cachedPath(item, item.d, parsePath);
247
+ const clipped = pushClip(ctx, item, dc);
248
+ strokeAndFillShape(ctx, path2d, item, dc);
249
+ if (clipped) {
250
+ ctx.restore();
251
+ }
252
+ }
253
+ function drawCircle(ctx, item, dc) {
254
+ const p = new Path2D();
255
+ p.arc(item.cx, item.cy, item.r, 0, Math.PI * 2);
256
+ const clipped = pushClip(ctx, item, dc);
257
+ strokeAndFillShape(ctx, p, item, dc);
258
+ if (clipped) {
259
+ ctx.restore();
260
+ }
261
+ }
262
+ function drawEllipse(ctx, item, dc) {
263
+ const p = new Path2D();
264
+ p.ellipse(item.cx, item.cy, item.rx, item.ry, 0, 0, Math.PI * 2);
265
+ const clipped = pushClip(ctx, item, dc);
266
+ strokeAndFillShape(ctx, p, item, dc);
267
+ if (clipped) {
268
+ ctx.restore();
269
+ }
270
+ }
271
+ function drawRect(ctx, item, dc) {
272
+ const p = new Path2D();
273
+ const rx = item.rx ?? item.ry ?? 0;
274
+ const ry = item.ry ?? item.rx ?? 0;
275
+ if (rx > 0 || ry > 0) {
276
+ p.roundRect(item.x, item.y, item.width, item.height, [rx, ry]);
277
+ } else {
278
+ p.rect(item.x, item.y, item.width, item.height);
279
+ }
280
+ const clipped = pushClip(ctx, item, dc);
281
+ strokeAndFillShape(ctx, p, item, dc);
282
+ if (clipped) {
283
+ ctx.restore();
284
+ }
285
+ }
286
+ function drawLine(ctx, item, dc) {
287
+ const stroke = item.stroke && item.stroke !== "none" ? item.stroke : null;
288
+ const effectiveStroke = stroke ?? dc.globalFill;
289
+ if (!effectiveStroke || effectiveStroke === "none") {
290
+ return;
291
+ }
292
+ const p = new Path2D();
293
+ p.moveTo(item.x1, item.y1);
294
+ p.lineTo(item.x2, item.y2);
295
+ withOpacity(ctx, item.opacity, () => {
296
+ ctx.lineJoin = item.strokeLinejoin ?? dc.defaultLineJoin;
297
+ ctx.lineCap = item.strokeLinecap ?? dc.defaultLineCap;
298
+ ctx.lineWidth = item.strokeWidth ?? dc.defaultLineWidth;
299
+ ctx.strokeStyle = effectiveStroke;
300
+ setDash(ctx, item);
301
+ ctx.stroke(p);
302
+ });
303
+ }
304
+ function parsePath(d) {
305
+ return new Path2D(d);
306
+ }
307
+ function buildPolygonPath(points) {
308
+ return buildPolyPath(points, true);
309
+ }
310
+ function buildPolylinePath(points) {
311
+ return buildPolyPath(points, false);
312
+ }
313
+ function buildPolyPath(points, close) {
314
+ const p = new Path2D();
315
+ const coords = points.trim().split(/[\s,]+/).map(Number);
316
+ for (let i = 0; i + 1 < coords.length; i += 2) {
317
+ if (i === 0) {
318
+ p.moveTo(coords[0], coords[1]);
319
+ } else {
320
+ p.lineTo(coords[i], coords[i + 1]);
321
+ }
322
+ }
323
+ if (close) {
324
+ p.closePath();
325
+ }
326
+ return p;
327
+ }
328
+ function drawPolyShape(ctx, item, dc, close) {
329
+ const p = cachedPath(item, item.points, close ? buildPolygonPath : buildPolylinePath);
330
+ const clipped = pushClip(ctx, item, dc);
331
+ strokeAndFillShape(ctx, p, item, dc);
332
+ if (clipped) {
333
+ ctx.restore();
334
+ }
335
+ }
336
+ function drawText(ctx, item, dc) {
337
+ const size = item.fontSize ?? 16;
338
+ const family = item.fontFamily ?? "sans-serif";
339
+ const weight = item.fontWeight ?? "normal";
340
+ const style = item.fontStyle ?? "normal";
341
+ ctx.font = `${style} ${weight} ${size}px ${family}`;
342
+ ctx.textAlign = item.textAnchor ?? "start";
343
+ ctx.textBaseline = item.dominantBaseline ?? "alphabetic";
344
+ const fillColor = resolveFill(item.fill, dc.customFill, dc.globalFill);
345
+ withOpacity(ctx, item.opacity, () => {
346
+ if (fillColor && fillColor !== "none") {
347
+ const prevAlpha = ctx.globalAlpha;
348
+ if (item.fillOpacity !== null && item.fillOpacity !== void 0) {
349
+ ctx.globalAlpha = prevAlpha * item.fillOpacity;
350
+ }
351
+ ctx.fillStyle = fillColor;
352
+ ctx.fillText(item.content, item.x, item.y);
353
+ ctx.globalAlpha = prevAlpha;
354
+ }
355
+ if (item.stroke && item.stroke !== "none") {
356
+ ctx.strokeStyle = item.stroke;
357
+ ctx.lineWidth = item.strokeWidth ?? dc.defaultLineWidth;
358
+ ctx.strokeText(item.content, item.x, item.y);
359
+ }
360
+ });
361
+ }
362
+ function drawGroup(ctx, item, dc) {
363
+ ctx.save();
364
+ applyTransform(ctx, item.transform);
365
+ const childDc = withGlobalFill(dc, item.fill);
366
+ const clipId = item.clipPath ? getRefId(item.clipPath) : null;
367
+ if (clipId) {
368
+ const clipDef = dc.clipPaths.get(clipId);
369
+ if (clipDef) {
370
+ applyClipItems(ctx, clipDef.children);
371
+ }
372
+ }
373
+ const maskId = item.mask ? getRefId(item.mask) : null;
374
+ if (maskId) {
375
+ const maskDef = dc.masks.get(maskId);
376
+ if (maskDef) {
377
+ applyClipItems(ctx, maskDef.children);
378
+ }
379
+ }
380
+ withOpacity(ctx, item.opacity, () => drawItems(ctx, item.children, childDc));
381
+ ctx.restore();
382
+ }
383
+ function drawUse(ctx, item, dc) {
384
+ const refId = item.href.startsWith("#") ? item.href.slice(1) : item.href;
385
+ const ref = dc.defs.get(refId);
386
+ if (!ref) {
387
+ return;
388
+ }
389
+ ctx.save();
390
+ ctx.translate(item.x ?? 0, item.y ?? 0);
391
+ drawItem(ctx, ref, dc);
392
+ ctx.restore();
393
+ }
394
+ function getRefId(ref) {
395
+ if (ref.startsWith("url(#")) {
396
+ return ref.slice(5, -1);
397
+ }
398
+ if (ref.startsWith("#")) {
399
+ return ref.slice(1);
400
+ }
401
+ return ref;
402
+ }
403
+
404
+ // src/svg/svg-parser-helpers.ts
405
+ function parseChildren(element) {
406
+ const result = [];
407
+ for (let i = 0; i < element.children.length; i++) {
408
+ const child = element.children.item(i);
409
+ if (!child) {
410
+ continue;
411
+ }
412
+ const item = parseItem(child);
413
+ if (item) {
414
+ result.push(item);
415
+ }
416
+ }
417
+ return result;
418
+ }
419
+ function parseItem(element) {
420
+ const tag = element.tagName.toLowerCase();
421
+ switch (tag) {
422
+ case "path":
423
+ return parsePath2(element);
424
+ case "mask":
425
+ return parseMask(element);
426
+ case "clippath":
427
+ return parseClipPath(element);
428
+ case "circle":
429
+ return parseCircle(element);
430
+ case "ellipse":
431
+ return parseEllipse(element);
432
+ case "rect":
433
+ return parseRectEl(element);
434
+ case "line":
435
+ return parseLineEl(element);
436
+ case "polyline":
437
+ return parsePolyline(element);
438
+ case "polygon":
439
+ return parsePolygon(element);
440
+ case "text":
441
+ return parseTextEl(element);
442
+ case "g":
443
+ return parseGroup(element);
444
+ case "defs":
445
+ return parseDefs(element);
446
+ case "use":
447
+ return parseUse(element);
448
+ default:
449
+ return null;
450
+ }
451
+ }
452
+ function parseLength(s) {
453
+ if (!s) {
454
+ return null;
455
+ }
456
+ const n = parseFloat(s);
457
+ return Number.isNaN(n) ? null : n;
458
+ }
459
+ function getNum(el, attr, fallback = 0) {
460
+ const v = el.getAttribute(attr);
461
+ if (v === null) {
462
+ return fallback;
463
+ }
464
+ return parseLength(v) ?? fallback;
465
+ }
466
+ function getOptNum(el, attr) {
467
+ const v = el.getAttribute(attr);
468
+ if (v === null) {
469
+ return null;
470
+ }
471
+ return parseLength(v);
472
+ }
473
+ function readStyle(el) {
474
+ const attrs = {};
475
+ const fill = el.getAttribute("fill");
476
+ if (fill !== null) {
477
+ attrs.fill = fill;
478
+ }
479
+ const fillRule = el.getAttribute("fill-rule");
480
+ if (fillRule) {
481
+ attrs.fillRule = fillRule;
482
+ }
483
+ const fillOpacity = el.getAttribute("fill-opacity");
484
+ if (fillOpacity !== null) {
485
+ attrs.fillOpacity = parseFloat(fillOpacity);
486
+ }
487
+ const stroke = el.getAttribute("stroke");
488
+ if (stroke !== null) {
489
+ attrs.stroke = stroke;
490
+ }
491
+ const strokeWidth = el.getAttribute("stroke-width");
492
+ if (strokeWidth !== null) {
493
+ attrs.strokeWidth = parseFloat(strokeWidth);
494
+ }
495
+ const strokeLinecap = el.getAttribute("stroke-linecap");
496
+ if (strokeLinecap) {
497
+ attrs.strokeLinecap = strokeLinecap;
498
+ }
499
+ const strokeLinejoin = el.getAttribute("stroke-linejoin");
500
+ if (strokeLinejoin) {
501
+ attrs.strokeLinejoin = strokeLinejoin;
502
+ }
503
+ const strokeDasharray = el.getAttribute("stroke-dasharray");
504
+ if (strokeDasharray && strokeDasharray !== "none") {
505
+ attrs.strokeDasharray = strokeDasharray.trim().split(/[\s,]+/).map(Number).filter((n) => !Number.isNaN(n));
506
+ }
507
+ const strokeDashoffset = el.getAttribute("stroke-dashoffset");
508
+ if (strokeDashoffset !== null) {
509
+ attrs.strokeDashoffset = parseFloat(strokeDashoffset);
510
+ }
511
+ const strokeOpacity = el.getAttribute("stroke-opacity");
512
+ if (strokeOpacity !== null) {
513
+ attrs.strokeOpacity = parseFloat(strokeOpacity);
514
+ }
515
+ const clipRule = el.getAttribute("clip-rule");
516
+ if (clipRule) {
517
+ attrs.clipRule = clipRule;
518
+ }
519
+ const opacity = el.getAttribute("opacity");
520
+ if (opacity !== null) {
521
+ attrs.opacity = parseFloat(opacity);
522
+ }
523
+ const mask = el.getAttribute("mask");
524
+ if (mask) {
525
+ attrs.mask = mask;
526
+ }
527
+ const clipPath = el.getAttribute("clip-path");
528
+ if (clipPath) {
529
+ attrs.clipPath = clipPath;
530
+ }
531
+ const styleAttr = el.getAttribute("style");
532
+ if (styleAttr) {
533
+ Object.assign(attrs, parseInlineStyle(styleAttr));
534
+ }
535
+ return attrs;
536
+ }
537
+ function parseInlineStyle(style) {
538
+ const result = {};
539
+ for (const decl of style.split(";")) {
540
+ const colonIdx = decl.indexOf(":");
541
+ if (colonIdx < 0) {
542
+ continue;
543
+ }
544
+ const prop = decl.slice(0, colonIdx).trim();
545
+ const val = decl.slice(colonIdx + 1).trim();
546
+ switch (prop) {
547
+ case "fill":
548
+ result.fill = val;
549
+ break;
550
+ case "fill-rule":
551
+ result.fillRule = val;
552
+ break;
553
+ case "fill-opacity":
554
+ result.fillOpacity = parseFloat(val);
555
+ break;
556
+ case "stroke":
557
+ result.stroke = val;
558
+ break;
559
+ case "stroke-width":
560
+ result.strokeWidth = parseFloat(val);
561
+ break;
562
+ case "stroke-linecap":
563
+ result.strokeLinecap = val;
564
+ break;
565
+ case "stroke-linejoin":
566
+ result.strokeLinejoin = val;
567
+ break;
568
+ case "stroke-dasharray":
569
+ result.strokeDasharray = val === "none" ? [] : val.trim().split(/[\s,]+/).map(Number);
570
+ break;
571
+ case "stroke-dashoffset":
572
+ result.strokeDashoffset = parseFloat(val);
573
+ break;
574
+ case "stroke-opacity":
575
+ result.strokeOpacity = parseFloat(val);
576
+ break;
577
+ case "opacity":
578
+ result.opacity = parseFloat(val);
579
+ break;
580
+ case "clip-rule":
581
+ result.clipRule = val;
582
+ break;
583
+ }
584
+ }
585
+ return result;
586
+ }
587
+ function parseTransform(el) {
588
+ const attr = el.getAttribute("transform");
589
+ if (!attr) {
590
+ return null;
591
+ }
592
+ const ops = [];
593
+ const re = /(\w+)\(([^)]*)\)/g;
594
+ for (const match of attr.matchAll(re)) {
595
+ const kind = match[1];
596
+ const nums = match[2].trim().split(/[\s,]+/).map(Number);
597
+ switch (kind) {
598
+ case "translate":
599
+ ops.push({ kind: "translate", tx: nums[0] ?? 0, ty: nums[1] ?? 0 });
600
+ break;
601
+ case "scale":
602
+ ops.push({ kind: "scale", sx: nums[0] ?? 1, sy: nums[1] ?? nums[0] ?? 1 });
603
+ break;
604
+ case "rotate":
605
+ ops.push({ kind: "rotate", angle: nums[0] ?? 0, cx: nums[1] ?? 0, cy: nums[2] ?? 0 });
606
+ break;
607
+ case "skewX":
608
+ ops.push({ kind: "skewX", angle: nums[0] ?? 0 });
609
+ break;
610
+ case "skewY":
611
+ ops.push({ kind: "skewY", angle: nums[0] ?? 0 });
612
+ break;
613
+ case "matrix":
614
+ ops.push({ kind: "matrix", a: nums[0], b: nums[1], c: nums[2], d: nums[3], e: nums[4], f: nums[5] });
615
+ break;
616
+ }
617
+ }
618
+ return ops.length ? { ops } : null;
619
+ }
620
+ function parsePath2(el) {
621
+ const d = el.getAttribute("d");
622
+ if (!d) {
623
+ return null;
624
+ }
625
+ return { type: "path", d, ...readStyle(el) };
626
+ }
627
+ function parseMask(el) {
628
+ const id = el.getAttribute("id");
629
+ if (!id) {
630
+ return null;
631
+ }
632
+ return { type: "mask", id, children: parseChildren(el) };
633
+ }
634
+ function parseClipPath(el) {
635
+ const id = el.getAttribute("id");
636
+ if (!id) {
637
+ return null;
638
+ }
639
+ return { type: "clipPath", id, children: parseChildren(el) };
640
+ }
641
+ function parseCircle(el) {
642
+ return {
643
+ type: "circle",
644
+ cx: getNum(el, "cx"),
645
+ cy: getNum(el, "cy"),
646
+ r: getNum(el, "r"),
647
+ ...readStyle(el)
648
+ };
649
+ }
650
+ function parseEllipse(el) {
651
+ return {
652
+ type: "ellipse",
653
+ cx: getNum(el, "cx"),
654
+ cy: getNum(el, "cy"),
655
+ rx: getNum(el, "rx"),
656
+ ry: getNum(el, "ry"),
657
+ ...readStyle(el)
658
+ };
659
+ }
660
+ function parseRectEl(el) {
661
+ return {
662
+ type: "rect",
663
+ x: getNum(el, "x"),
664
+ y: getNum(el, "y"),
665
+ width: getNum(el, "width"),
666
+ height: getNum(el, "height"),
667
+ rx: getOptNum(el, "rx"),
668
+ ry: getOptNum(el, "ry"),
669
+ ...readStyle(el)
670
+ };
671
+ }
672
+ function parseLineEl(el) {
673
+ return {
674
+ type: "line",
675
+ x1: getNum(el, "x1"),
676
+ y1: getNum(el, "y1"),
677
+ x2: getNum(el, "x2"),
678
+ y2: getNum(el, "y2"),
679
+ ...readStyle(el)
680
+ };
681
+ }
682
+ function parsePolyline(el) {
683
+ const points = el.getAttribute("points");
684
+ if (!points) {
685
+ return null;
686
+ }
687
+ return { type: "polyline", points, ...readStyle(el) };
688
+ }
689
+ function parsePolygon(el) {
690
+ const points = el.getAttribute("points");
691
+ if (!points) {
692
+ return null;
693
+ }
694
+ return { type: "polygon", points, ...readStyle(el) };
695
+ }
696
+ function parseTextEl(el) {
697
+ const fontSizeRaw = el.getAttribute("font-size");
698
+ const fontFamily = el.getAttribute("font-family");
699
+ const fontWeight = el.getAttribute("font-weight");
700
+ const fontStyle = el.getAttribute("font-style");
701
+ const textAnchor = el.getAttribute("text-anchor");
702
+ const dominantBaseline = el.getAttribute("dominant-baseline");
703
+ return {
704
+ type: "text",
705
+ x: getNum(el, "x"),
706
+ y: getNum(el, "y"),
707
+ content: el.textContent ?? "",
708
+ fontSize: fontSizeRaw ? parseFloat(fontSizeRaw) : null,
709
+ fontFamily,
710
+ fontWeight,
711
+ fontStyle,
712
+ textAnchor,
713
+ dominantBaseline,
714
+ ...readStyle(el)
715
+ };
716
+ }
717
+ function parseGroup(el) {
718
+ const opacityStr = el.getAttribute("opacity");
719
+ const fill = el.getAttribute("fill");
720
+ const stroke = el.getAttribute("stroke");
721
+ const mask = el.getAttribute("mask");
722
+ const clipPath = el.getAttribute("clip-path");
723
+ return {
724
+ type: "g",
725
+ children: parseChildren(el),
726
+ transform: parseTransform(el),
727
+ opacity: opacityStr !== null ? parseFloat(opacityStr) : null,
728
+ fill: fill ?? void 0,
729
+ stroke: stroke ?? void 0,
730
+ mask: mask ?? void 0,
731
+ clipPath: clipPath ?? void 0
732
+ };
733
+ }
734
+ function parseDefs(el) {
735
+ return { type: "defs", children: parseChildren(el) };
736
+ }
737
+ function parseUse(el) {
738
+ const href = el.getAttribute("href") ?? el.getAttribute("xlink:href");
739
+ if (!href) {
740
+ return null;
741
+ }
742
+ return {
743
+ type: "use",
744
+ href,
745
+ x: getOptNum(el, "x"),
746
+ y: getOptNum(el, "y"),
747
+ width: getOptNum(el, "width"),
748
+ height: getOptNum(el, "height")
749
+ };
750
+ }
751
+ var xmlParser;
752
+ function parseXml(str) {
753
+ if (!xmlParser) {
754
+ xmlParser = new globalThis.DOMParser();
755
+ }
756
+ const doc = xmlParser.parseFromString(str, "image/svg+xml");
757
+ const rootElement = doc.documentElement;
758
+ if (rootElement.tagName !== "svg") {
759
+ throw new Error("Svg is not valid");
760
+ }
761
+ return rootElement;
762
+ }
763
+
764
+ // src/svg/svg-serializer.ts
765
+ function escapeXml(s) {
766
+ return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
767
+ }
768
+ function styleAttrsToSvg(s) {
769
+ const parts = [];
770
+ if (s.fill !== void 0 && s.fill !== null) {
771
+ parts.push(`fill="${escapeXml(s.fill)}"`);
772
+ }
773
+ if (s.fillRule) {
774
+ parts.push(`fill-rule="${s.fillRule}"`);
775
+ }
776
+ if (s.fillOpacity !== void 0 && s.fillOpacity !== null) {
777
+ parts.push(`fill-opacity="${s.fillOpacity}"`);
778
+ }
779
+ if (s.stroke) {
780
+ parts.push(`stroke="${escapeXml(s.stroke)}"`);
781
+ }
782
+ if (s.strokeWidth !== void 0 && s.strokeWidth !== null) {
783
+ parts.push(`stroke-width="${s.strokeWidth}"`);
784
+ }
785
+ if (s.strokeLinecap) {
786
+ parts.push(`stroke-linecap="${s.strokeLinecap}"`);
787
+ }
788
+ if (s.strokeLinejoin) {
789
+ parts.push(`stroke-linejoin="${s.strokeLinejoin}"`);
790
+ }
791
+ if (s.strokeDasharray && s.strokeDasharray.length > 0) {
792
+ parts.push(`stroke-dasharray="${s.strokeDasharray.join(" ")}"`);
793
+ }
794
+ if (s.strokeDashoffset !== void 0 && s.strokeDashoffset !== null) {
795
+ parts.push(`stroke-dashoffset="${s.strokeDashoffset}"`);
796
+ }
797
+ if (s.strokeOpacity !== void 0 && s.strokeOpacity !== null) {
798
+ parts.push(`stroke-opacity="${s.strokeOpacity}"`);
799
+ }
800
+ if (s.clipRule) {
801
+ parts.push(`clip-rule="${s.clipRule}"`);
802
+ }
803
+ if (s.opacity !== void 0 && s.opacity !== null) {
804
+ parts.push(`opacity="${s.opacity}"`);
805
+ }
806
+ if (s.mask) {
807
+ parts.push(`mask="${escapeXml(s.mask)}"`);
808
+ }
809
+ if (s.clipPath) {
810
+ parts.push(`clip-path="${escapeXml(s.clipPath)}"`);
811
+ }
812
+ return parts.length ? ` ${parts.join(" ")}` : "";
813
+ }
814
+ function transformToSvg(t) {
815
+ if (!t || t.ops.length === 0) {
816
+ return "";
817
+ }
818
+ const parts = t.ops.map((op) => {
819
+ switch (op.kind) {
820
+ case "translate":
821
+ return `translate(${op.tx},${op.ty})`;
822
+ case "scale":
823
+ return `scale(${op.sx},${op.sy})`;
824
+ case "rotate":
825
+ return op.cx || op.cy ? `rotate(${op.angle},${op.cx},${op.cy})` : `rotate(${op.angle})`;
826
+ case "skewX":
827
+ return `skewX(${op.angle})`;
828
+ case "skewY":
829
+ return `skewY(${op.angle})`;
830
+ case "matrix":
831
+ return `matrix(${op.a},${op.b},${op.c},${op.d},${op.e},${op.f})`;
832
+ default:
833
+ return "";
834
+ }
835
+ });
836
+ return ` transform="${parts.join(" ")}"`;
837
+ }
838
+ function itemToRawSvg(item) {
839
+ switch (item.type) {
840
+ case "path":
841
+ return `<path d="${escapeXml(item.d)}"${styleAttrsToSvg(item)}/>`;
842
+ case "circle":
843
+ return `<circle cx="${item.cx}" cy="${item.cy}" r="${item.r}"${styleAttrsToSvg(item)}/>`;
844
+ case "ellipse":
845
+ return `<ellipse cx="${item.cx}" cy="${item.cy}" rx="${item.rx}" ry="${item.ry}"${styleAttrsToSvg(item)}/>`;
846
+ case "rect": {
847
+ const rxStr = item.rx !== null && item.rx !== void 0 ? ` rx="${item.rx}"` : "";
848
+ const ryStr = item.ry !== null && item.ry !== void 0 ? ` ry="${item.ry}"` : "";
849
+ return `<rect x="${item.x}" y="${item.y}" width="${item.width}" height="${item.height}"${rxStr}${ryStr}${styleAttrsToSvg(item)}/>`;
850
+ }
851
+ case "line":
852
+ return `<line x1="${item.x1}" y1="${item.y1}" x2="${item.x2}" y2="${item.y2}"${styleAttrsToSvg(item)}/>`;
853
+ case "polyline":
854
+ return `<polyline points="${escapeXml(item.points)}"${styleAttrsToSvg(item)}/>`;
855
+ case "polygon":
856
+ return `<polygon points="${escapeXml(item.points)}"${styleAttrsToSvg(item)}/>`;
857
+ case "text": {
858
+ const fontParts = [];
859
+ if (item.fontSize !== null && item.fontSize !== void 0) {
860
+ fontParts.push(`font-size="${item.fontSize}"`);
861
+ }
862
+ if (item.fontFamily) {
863
+ fontParts.push(`font-family="${escapeXml(item.fontFamily)}"`);
864
+ }
865
+ if (item.fontWeight) {
866
+ fontParts.push(`font-weight="${item.fontWeight}"`);
867
+ }
868
+ if (item.fontStyle) {
869
+ fontParts.push(`font-style="${item.fontStyle}"`);
870
+ }
871
+ if (item.textAnchor) {
872
+ fontParts.push(`text-anchor="${item.textAnchor}"`);
873
+ }
874
+ if (item.dominantBaseline) {
875
+ fontParts.push(`dominant-baseline="${item.dominantBaseline}"`);
876
+ }
877
+ const fontStr = fontParts.length ? ` ${fontParts.join(" ")}` : "";
878
+ return `<text x="${item.x}" y="${item.y}"${fontStr}${styleAttrsToSvg(item)}>${escapeXml(item.content)}</text>`;
879
+ }
880
+ case "g": {
881
+ const opacityStr = item.opacity !== null && item.opacity !== void 0 ? ` opacity="${item.opacity}"` : "";
882
+ const fillStr = item.fill !== void 0 && item.fill !== null ? ` fill="${escapeXml(item.fill)}"` : "";
883
+ const strokeStr = item.stroke !== void 0 && item.stroke !== null ? ` stroke="${escapeXml(item.stroke)}"` : "";
884
+ const maskStr = item.mask ? ` mask="${escapeXml(item.mask)}"` : "";
885
+ const clipStr = item.clipPath ? ` clip-path="${escapeXml(item.clipPath)}"` : "";
886
+ const inner = item.children.map(itemToRawSvg).join("");
887
+ return `<g${transformToSvg(item.transform)}${opacityStr}${fillStr}${strokeStr}${maskStr}${clipStr}>${inner}</g>`;
888
+ }
889
+ case "defs":
890
+ return `<defs>${item.children.map(itemToRawSvg).join("")}</defs>`;
891
+ case "mask":
892
+ return `<mask id="${escapeXml(item.id)}">${item.children.map(itemToRawSvg).join("")}</mask>`;
893
+ case "clipPath":
894
+ return `<clipPath id="${escapeXml(item.id)}">${item.children.map(itemToRawSvg).join("")}</clipPath>`;
895
+ case "use": {
896
+ const x = item.x !== null && item.x !== void 0 ? ` x="${item.x}"` : "";
897
+ const y = item.y !== null && item.y !== void 0 ? ` y="${item.y}"` : "";
898
+ const w = item.width !== null && item.width !== void 0 ? ` width="${item.width}"` : "";
899
+ const h = item.height !== null && item.height !== void 0 ? ` height="${item.height}"` : "";
900
+ return `<use href="${escapeXml(item.href)}"${x}${y}${w}${h}/>`;
901
+ }
902
+ }
903
+ }
904
+
905
+ // src/svg/parser.ts
906
+ var SvgImpl = class _SvgImpl {
907
+ size;
908
+ items;
909
+ fill;
910
+ lineWidth;
911
+ lineCap;
912
+ lineJoin;
913
+ /**
914
+ * DrawContext is built once from item definitions (masks, clipPaths, defs).
915
+ * It is shared across SvgView and SvgDrawableContent – no per-frame allocation.
916
+ */
917
+ _baseDrawContext;
918
+ constructor(size, items, fill, lineWidth, lineCap, lineJoin) {
919
+ this.size = size;
920
+ this.items = items;
921
+ this.fill = fill;
922
+ this.lineWidth = lineWidth;
923
+ this.lineCap = lineCap;
924
+ this.lineJoin = lineJoin;
925
+ this._baseDrawContext = buildDrawContext(
926
+ items,
927
+ fill ?? null,
928
+ lineWidth ?? 1,
929
+ lineCap ?? "butt",
930
+ lineJoin ?? "miter"
931
+ );
932
+ }
933
+ createView(options) {
934
+ const size = options?.originalSize ?? this.size;
935
+ return new SvgView({
936
+ ...options,
937
+ originalSize: size,
938
+ items: this.items,
939
+ fill: options?.fill !== void 0 ? options.fill : this.fill ?? null,
940
+ baseDrawContext: this._baseDrawContext,
941
+ size: options?.size ?? size
942
+ });
943
+ }
944
+ createDrawableContent(location, scale) {
945
+ return new SvgDrawableContent(
946
+ location ?? Point.zero,
947
+ scale ?? Point.one,
948
+ this.size,
949
+ this.items,
950
+ this._baseDrawContext
951
+ );
952
+ }
953
+ with(overrides) {
954
+ if (!overrides) {
955
+ return new _SvgImpl(this.size, this.items, this.fill, this.lineWidth, this.lineCap, this.lineJoin);
956
+ }
957
+ return new _SvgImpl(
958
+ overrides.size ?? this.size,
959
+ overrides.paths ?? this.items,
960
+ overrides.fill !== void 0 ? overrides.fill : this.fill,
961
+ overrides.lineWidth !== void 0 ? overrides.lineWidth : this.lineWidth,
962
+ overrides.lineCap !== void 0 ? overrides.lineCap : this.lineCap,
963
+ overrides.lineJoin !== void 0 ? overrides.lineJoin : this.lineJoin
964
+ );
965
+ }
966
+ copy() {
967
+ return new _SvgImpl(this.size, this.items.map(deepCopyItem), this.fill, this.lineWidth, this.lineCap, this.lineJoin);
968
+ }
969
+ toRawSvg() {
970
+ const fillAttr = this.fill ? ` fill="${escapeXml(this.fill)}"` : "";
971
+ return `<svg xmlns="http://www.w3.org/2000/svg" width="${this.size.width}" height="${this.size.height}" viewBox="0 0 ${this.size.width} ${this.size.height}"${fillAttr}>` + this.items.map(itemToRawSvg).join("") + `</svg>`;
972
+ }
973
+ };
974
+ function deepCopyItem(item) {
975
+ if (item.type === "g" || item.type === "mask" || item.type === "clipPath" || item.type === "defs") {
976
+ return { ...item, children: item.children.map(deepCopyItem) };
977
+ }
978
+ return { ...item };
979
+ }
980
+ var SvgView = class extends View {
981
+ _items;
982
+ _fill;
983
+ _originalSize;
984
+ _baseDrawContext;
985
+ constructor(options) {
986
+ super(options);
987
+ this._originalSize = options.originalSize;
988
+ this._fill = this.createVary(options.fill ?? null);
989
+ this._items = options.items;
990
+ this._baseDrawContext = options.baseDrawContext;
991
+ }
992
+ doDraw(context, state) {
993
+ const { width, height } = state.localBounds.size;
994
+ const scaleX = width / this._originalSize.width;
995
+ const scaleY = height / this._originalSize.height;
996
+ if (scaleX === 0 || scaleY === 0) {
997
+ return;
998
+ }
999
+ const needsScale = scaleX !== 1 || scaleY !== 1;
1000
+ const saved = needsScale ? context.getTransform() : null;
1001
+ if (needsScale) {
1002
+ context.scale(scaleX, scaleY);
1003
+ }
1004
+ const dc = withCustomFill(this._baseDrawContext, this._fill.value);
1005
+ drawItems(context, this._items, dc);
1006
+ if (saved) {
1007
+ context.setTransform(saved);
1008
+ }
1009
+ }
1010
+ };
1011
+ var SvgDrawableContent = class extends DrawableContent {
1012
+ _location;
1013
+ _scale;
1014
+ _svgSize;
1015
+ _items;
1016
+ _baseDrawContext;
1017
+ constructor(location, scale, svgSize, items, baseDrawContext) {
1018
+ super();
1019
+ this._location = location;
1020
+ this._scale = scale;
1021
+ this._svgSize = svgSize;
1022
+ this._items = items;
1023
+ this._baseDrawContext = baseDrawContext;
1024
+ }
1025
+ get size() {
1026
+ return this._svgSize.scale(this._scale);
1027
+ }
1028
+ draw(context) {
1029
+ const { x, y } = this._location;
1030
+ const saved = context.getTransform();
1031
+ context.translate(x, y);
1032
+ const { x: sx, y: sy } = this._scale;
1033
+ const needsScale = sx !== 1 || sy !== 1;
1034
+ if (needsScale) {
1035
+ context.scale(sx, sy);
1036
+ }
1037
+ drawItems(context, this._items, this._baseDrawContext);
1038
+ context.setTransform(saved);
1039
+ }
1040
+ };
1041
+ function parseSvg(str) {
1042
+ if (typeof globalThis.DOMParser === "undefined") {
1043
+ throw new Error("sceniq needs DOMParser to parse SVG");
1044
+ }
1045
+ const rootElement = parseXml(str);
1046
+ let width = 0;
1047
+ let height = 0;
1048
+ const viewBoxAttr = rootElement.getAttribute("viewBox");
1049
+ if (viewBoxAttr) {
1050
+ const vb = viewBoxAttr.trim().split(/[\s,]+/).map(Number);
1051
+ if (vb.length === 4) {
1052
+ width = vb[2];
1053
+ height = vb[3];
1054
+ }
1055
+ }
1056
+ if (!width) {
1057
+ width = parseLength(rootElement.getAttribute("width") ?? "") ?? 0;
1058
+ }
1059
+ if (!height) {
1060
+ height = parseLength(rootElement.getAttribute("height") ?? "") ?? 0;
1061
+ }
1062
+ width = width || 24;
1063
+ height = height || 24;
1064
+ const fill = rootElement.getAttribute("fill");
1065
+ const items = parseChildren(rootElement);
1066
+ return new SvgImpl(new Size(width, height), items, fill);
1067
+ }
1068
+
1069
+ // src/svg/svg-container-view.ts
1070
+ var SvgContainerView = class extends View {
1071
+ _defaultData;
1072
+ _renderer;
1073
+ _svgScale;
1074
+ _color;
1075
+ constructor(defaultData, renderer, params) {
1076
+ super({
1077
+ location: new Point(params?.x ?? 0, params?.y ?? 0)
1078
+ });
1079
+ this._defaultData = defaultData;
1080
+ this._renderer = renderer;
1081
+ const size = params?.size;
1082
+ this._svgScale = this.createVar(size == void 0 ? 1 : size / defaultData.size);
1083
+ this._color = this.createVar(params?.color ?? null);
1084
+ }
1085
+ doGetDrawBounds(measuredBox) {
1086
+ const scale = this._svgScale.value;
1087
+ const size = this._defaultData.size * scale;
1088
+ const width = measuredBox.getRealWidth(size);
1089
+ const height = measuredBox.getRealHeight(size);
1090
+ return RectBounds.fromSize(new Size(width, height));
1091
+ }
1092
+ doDraw(context, _state) {
1093
+ const scale = this._svgScale.value;
1094
+ const color = this._color.value;
1095
+ if (this._defaultData.lineWidth) {
1096
+ context.lineWidth = this._defaultData.lineWidth;
1097
+ }
1098
+ if (this._defaultData.lineCap) {
1099
+ context.lineCap = this._defaultData.lineCap;
1100
+ }
1101
+ if (this._defaultData.lineJoin) {
1102
+ context.lineJoin = this._defaultData.lineJoin;
1103
+ }
1104
+ const saved = context.getTransform();
1105
+ context.scale(scale, scale);
1106
+ if (color) {
1107
+ context.fillStyle = context.strokeStyle = color;
1108
+ }
1109
+ this._renderer(context);
1110
+ context.setTransform(saved);
1111
+ }
1112
+ };
1113
+ export {
1114
+ SvgContainerView,
1115
+ buildDrawContext,
1116
+ drawItem,
1117
+ drawItems,
1118
+ escapeXml,
1119
+ itemToRawSvg,
1120
+ parseSvg,
1121
+ parseXml
1122
+ };