@invana/canvas-core 0.0.12

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,893 @@
1
+ // src/specs/geometry.ts
2
+ function connectorGeometryKey(spec) {
3
+ const { stroke: _stroke, ...geometry } = spec;
4
+ return JSON.stringify(geometry);
5
+ }
6
+
7
+ // src/specs/style.ts
8
+ function hasSilhouetteFill(fill) {
9
+ if (fill === void 0) return false;
10
+ if (typeof fill === "number") return true;
11
+ const layers = Array.isArray(fill) ? fill : [fill];
12
+ return layers.some((l) => l.kind === "solid" || l.kind === "image");
13
+ }
14
+
15
+ // src/specs/shapeGeometry/polygonMath.ts
16
+ function polygonBounds(vertices) {
17
+ if (vertices.length === 0) return { x: 0, y: 0, width: 0, height: 0 };
18
+ let minX = vertices[0].x;
19
+ let maxX = minX;
20
+ let minY = vertices[0].y;
21
+ let maxY = minY;
22
+ for (let i = 1; i < vertices.length; i++) {
23
+ const v = vertices[i];
24
+ if (v.x < minX) minX = v.x;
25
+ else if (v.x > maxX) maxX = v.x;
26
+ if (v.y < minY) minY = v.y;
27
+ else if (v.y > maxY) maxY = v.y;
28
+ }
29
+ return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
30
+ }
31
+ function pointInPolygon(localX, localY, vertices) {
32
+ const n = vertices.length;
33
+ if (n < 3) return false;
34
+ let inside = false;
35
+ for (let i = 0, j = n - 1; i < n; j = i++) {
36
+ const vi = vertices[i];
37
+ const vj = vertices[j];
38
+ const intersects = vi.y > localY !== vj.y > localY && localX < (vj.x - vi.x) * (localY - vi.y) / (vj.y - vi.y) + vi.x;
39
+ if (intersects) inside = !inside;
40
+ }
41
+ return inside;
42
+ }
43
+ function distanceToSegmentSq(px, py, x1, y1, x2, y2) {
44
+ const dx = x2 - x1;
45
+ const dy = y2 - y1;
46
+ const lenSq = dx * dx + dy * dy;
47
+ let t = lenSq === 0 ? 0 : ((px - x1) * dx + (py - y1) * dy) / lenSq;
48
+ t = t < 0 ? 0 : t > 1 ? 1 : t;
49
+ const ex = x1 + t * dx - px;
50
+ const ey = y1 + t * dy - py;
51
+ return ex * ex + ey * ey;
52
+ }
53
+ function distanceToOutlineSq(x, y, vertices, closed = true) {
54
+ const n = vertices.length;
55
+ if (n === 0) return Infinity;
56
+ if (n === 1) {
57
+ const v = vertices[0];
58
+ return (x - v.x) * (x - v.x) + (y - v.y) * (y - v.y);
59
+ }
60
+ const edges = closed ? n : n - 1;
61
+ let best = Infinity;
62
+ for (let i = 0; i < edges; i++) {
63
+ const a = vertices[i];
64
+ const b = vertices[(i + 1) % n];
65
+ const d = distanceToSegmentSq(x, y, a.x, a.y, b.x, b.y);
66
+ if (d < best) best = d;
67
+ }
68
+ return best;
69
+ }
70
+ function polygonContainsInflated(x, y, vertices, pad = 0, closed = true) {
71
+ const inside = closed && pointInPolygon(x, y, vertices);
72
+ if (pad === 0) return inside;
73
+ const nearOutline = distanceToOutlineSq(x, y, vertices, closed) <= pad * pad;
74
+ return pad > 0 ? inside || nearOutline : inside && !nearOutline;
75
+ }
76
+ function offsetPolygon(vertices, distance) {
77
+ const n = vertices.length;
78
+ if (n < 3 || distance === 0) return vertices.map((v) => ({ x: v.x, y: v.y }));
79
+ const ccw = signedArea(vertices) > 0;
80
+ const out = new Array(n);
81
+ for (let i = 0; i < n; i++) {
82
+ const prev = vertices[(i + n - 1) % n];
83
+ const curr = vertices[i];
84
+ const next = vertices[(i + 1) % n];
85
+ const e1 = unitNormal(prev, curr, ccw);
86
+ const e2 = unitNormal(curr, next, ccw);
87
+ const bx = e1.x + e2.x;
88
+ const by = e1.y + e2.y;
89
+ const dot = e1.x * e2.x + e1.y * e2.y;
90
+ const denom = 1 + dot;
91
+ if (Math.abs(denom) < 1e-6) {
92
+ out[i] = { x: curr.x + e1.x * distance, y: curr.y + e1.y * distance };
93
+ } else {
94
+ const k = distance / denom;
95
+ out[i] = { x: curr.x + bx * k, y: curr.y + by * k };
96
+ }
97
+ }
98
+ return out;
99
+ }
100
+ function regularPolygonVertices(sides, radius, rotationRad) {
101
+ const n = Math.max(3, Math.floor(sides));
102
+ const out = new Array(n);
103
+ const base = -Math.PI / 2 + rotationRad;
104
+ const step = Math.PI * 2 / n;
105
+ for (let i = 0; i < n; i++) {
106
+ const a = base + i * step;
107
+ out[i] = { x: Math.cos(a) * radius, y: Math.sin(a) * radius };
108
+ }
109
+ return out;
110
+ }
111
+ function starVertices(points, innerRadius, outerRadius, rotationRad) {
112
+ const p = Math.max(3, Math.floor(points));
113
+ const total = p * 2;
114
+ const out = new Array(total);
115
+ const base = -Math.PI / 2 + rotationRad;
116
+ const step = Math.PI / p;
117
+ for (let i = 0; i < total; i++) {
118
+ const r = i % 2 === 0 ? outerRadius : innerRadius;
119
+ const a = base + i * step;
120
+ out[i] = { x: Math.cos(a) * r, y: Math.sin(a) * r };
121
+ }
122
+ return out;
123
+ }
124
+ function rayPolygonIntersection(localFromCenter, vertices) {
125
+ const n = vertices.length;
126
+ if (n < 2) return null;
127
+ const dx = localFromCenter.x;
128
+ const dy = localFromCenter.y;
129
+ if (dx === 0 && dy === 0) return null;
130
+ let bestT = -Infinity;
131
+ let hit = null;
132
+ for (let i = 0, j = n - 1; i < n; j = i++) {
133
+ const a = vertices[j];
134
+ const b = vertices[i];
135
+ const ex = b.x - a.x;
136
+ const ey = b.y - a.y;
137
+ const denom = dx * -ey - dy * -ex;
138
+ if (denom === 0) continue;
139
+ const t = (a.x * -ey - a.y * -ex) / denom;
140
+ const u = (dx * a.y - dy * a.x) / denom;
141
+ if (t >= 0 && u >= 0 && u <= 1 && t > bestT) {
142
+ bestT = t;
143
+ hit = { x: dx * t, y: dy * t };
144
+ }
145
+ }
146
+ return hit;
147
+ }
148
+ function roundedPolygonOutline(corners) {
149
+ const n = corners.length;
150
+ if (n < 3) return corners.map((c) => ({ x: c.x, y: c.y }));
151
+ const out = [];
152
+ for (let i = 0; i < n; i++) {
153
+ const p = corners[i];
154
+ const prev = corners[(i + n - 1) % n];
155
+ const next = corners[(i + 1) % n];
156
+ const v1x = prev.x - p.x;
157
+ const v1y = prev.y - p.y;
158
+ const v2x = next.x - p.x;
159
+ const v2y = next.y - p.y;
160
+ const l1 = Math.hypot(v1x, v1y);
161
+ const l2 = Math.hypot(v2x, v2y);
162
+ if (p.r <= 0 || l1 < 1e-9 || l2 < 1e-9) {
163
+ out.push({ x: p.x, y: p.y });
164
+ continue;
165
+ }
166
+ const u1x = v1x / l1;
167
+ const u1y = v1y / l1;
168
+ const u2x = v2x / l2;
169
+ const u2y = v2y / l2;
170
+ const cosT = Math.max(-1, Math.min(1, u1x * u2x + u1y * u2y));
171
+ const theta = Math.acos(cosT);
172
+ if (theta < 1e-6 || Math.PI - theta < 1e-6) {
173
+ out.push({ x: p.x, y: p.y });
174
+ continue;
175
+ }
176
+ const half = theta / 2;
177
+ const tangent = Math.min(p.r / Math.tan(half), l1 / 2, l2 / 2);
178
+ const radius = tangent * Math.tan(half);
179
+ if (radius < 1e-6) {
180
+ out.push({ x: p.x, y: p.y });
181
+ continue;
182
+ }
183
+ let bx = u1x + u2x;
184
+ let by = u1y + u2y;
185
+ const bl = Math.hypot(bx, by);
186
+ if (bl < 1e-9) {
187
+ out.push({ x: p.x, y: p.y });
188
+ continue;
189
+ }
190
+ bx /= bl;
191
+ by /= bl;
192
+ const cx = p.x + bx * (radius / Math.sin(half));
193
+ const cy = p.y + by * (radius / Math.sin(half));
194
+ const a1 = Math.atan2(p.y + u1y * tangent - cy, p.x + u1x * tangent - cx);
195
+ const a2 = Math.atan2(p.y + u2y * tangent - cy, p.x + u2x * tangent - cx);
196
+ let delta = a2 - a1;
197
+ while (delta > Math.PI) delta -= 2 * Math.PI;
198
+ while (delta < -Math.PI) delta += 2 * Math.PI;
199
+ const steps = Math.max(2, Math.ceil(Math.abs(delta) * radius / 2));
200
+ for (let s = 0; s <= steps; s++) {
201
+ const a = a1 + delta * (s / steps);
202
+ out.push({ x: cx + Math.cos(a) * radius, y: cy + Math.sin(a) * radius });
203
+ }
204
+ }
205
+ return out;
206
+ }
207
+ function signedArea(vertices) {
208
+ let sum = 0;
209
+ const n = vertices.length;
210
+ for (let i = 0, j = n - 1; i < n; j = i++) {
211
+ const a = vertices[j];
212
+ const b = vertices[i];
213
+ sum += (b.x - a.x) * (b.y + a.y);
214
+ }
215
+ return -sum * 0.5;
216
+ }
217
+ function unitNormal(a, b, ccw) {
218
+ const dx = b.x - a.x;
219
+ const dy = b.y - a.y;
220
+ const len = Math.hypot(dx, dy) || 1;
221
+ return ccw ? { x: -dy / len, y: dx / len } : { x: dy / len, y: -dx / len };
222
+ }
223
+
224
+ // src/specs/shapeGeometry/tabbedRect.ts
225
+ function tabbedRectTabWidth(spec) {
226
+ const declared = spec.tabWidth ?? spec.width;
227
+ return spec.height <= 0 ? declared : Math.min(declared, spec.width);
228
+ }
229
+ function tabbedRectBounds(spec) {
230
+ if (spec.height <= 0) {
231
+ return { x: 0, y: 0, width: tabbedRectTabWidth(spec), height: spec.tabHeight };
232
+ }
233
+ return { x: 0, y: 0, width: spec.width, height: spec.tabHeight + spec.height };
234
+ }
235
+ function collapsedTabbedRect(_spec) {
236
+ return { height: 0 };
237
+ }
238
+ function fitTabbedRectToContent(spec, content) {
239
+ if (spec.tabWidth !== void 0) return {};
240
+ const pad = spec.tabPadding ?? 10;
241
+ const slantSides = (spec.tabAlign ?? "left") === "center" ? 2 : 1;
242
+ const fitted = content.width + 2 * pad + (spec.tabSkew ?? 0) * slantSides;
243
+ return { tabWidth: spec.height <= 0 ? fitted : Math.min(spec.width, fitted) };
244
+ }
245
+ function scaleTabbedRect(spec, factor) {
246
+ return {
247
+ width: spec.width * factor,
248
+ height: spec.height * factor,
249
+ tabHeight: spec.tabHeight * factor,
250
+ ...spec.tabWidth !== void 0 ? { tabWidth: spec.tabWidth * factor } : {},
251
+ ...spec.tabPadding !== void 0 ? { tabPadding: spec.tabPadding * factor } : {},
252
+ ...spec.cornerRadius !== void 0 ? { cornerRadius: spec.cornerRadius * factor } : {},
253
+ ...spec.tabCornerRadius !== void 0 ? { tabCornerRadius: spec.tabCornerRadius * factor } : {},
254
+ ...spec.tabOffset !== void 0 ? { tabOffset: spec.tabOffset * factor } : {},
255
+ ...spec.tabSkew !== void 0 ? { tabSkew: spec.tabSkew * factor } : {}
256
+ };
257
+ }
258
+ function tabbedRectOutline(spec, inset = 0) {
259
+ const geo = tabbedRectGeometry(spec, inset);
260
+ if (!geo) return [];
261
+ const { left, right, bottom, tabTop, shoulder, tabLeft, tabRight } = geo;
262
+ const r = Math.max(0, (spec.cornerRadius ?? 0) - inset);
263
+ const tr = Math.max(0, (spec.tabCornerRadius ?? spec.cornerRadius ?? 0) - inset);
264
+ const corners = [];
265
+ corners.push({ x: geo.tabTopLeft, y: tabTop, r: tr });
266
+ corners.push({ x: geo.tabTopRight, y: tabTop, r: tr });
267
+ if (geo.bodyless) {
268
+ corners.push({ x: tabRight, y: bottom, r });
269
+ corners.push({ x: tabLeft, y: bottom, r });
270
+ return roundedPolygonOutline(corners);
271
+ }
272
+ if (geo.flushRight) {
273
+ corners.push({ x: right, y: shoulder, r });
274
+ } else {
275
+ corners.push({ x: tabRight, y: shoulder, r: 0 });
276
+ corners.push({ x: right, y: shoulder, r });
277
+ }
278
+ corners.push({ x: right, y: bottom, r });
279
+ corners.push({ x: left, y: bottom, r });
280
+ if (!geo.flushLeft) {
281
+ corners.push({ x: left, y: shoulder, r });
282
+ corners.push({ x: tabLeft, y: shoulder, r: 0 });
283
+ }
284
+ return roundedPolygonOutline(corners);
285
+ }
286
+ function tabbedRectFoldLine(spec, inset = 0) {
287
+ if (spec.tabDivider === false) return void 0;
288
+ const geo = tabbedRectGeometry(spec, inset);
289
+ if (!geo || geo.bodyless) return void 0;
290
+ return [
291
+ { x: geo.tabLeft, y: geo.shoulder },
292
+ { x: geo.tabRight, y: geo.shoulder }
293
+ ];
294
+ }
295
+ function tabbedRectTabBox(spec, inset = 0) {
296
+ const geo = tabbedRectGeometry(spec, inset);
297
+ if (!geo) return void 0;
298
+ return {
299
+ x: geo.tabTopLeft,
300
+ y: geo.tabTop,
301
+ width: Math.max(0, geo.tabTopRight - geo.tabTopLeft),
302
+ height: geo.shoulder - geo.tabTop
303
+ };
304
+ }
305
+ function tabbedRectGeometry(spec, inset) {
306
+ if (spec.height <= 0) return bodylessGeometryOf(spec, inset);
307
+ const totalH = spec.tabHeight + spec.height;
308
+ const left = inset;
309
+ const right = spec.width - inset;
310
+ const bottom = totalH - inset;
311
+ const tabTop = inset;
312
+ const shoulder = spec.tabHeight + inset;
313
+ if (right <= left || bottom <= shoulder) return null;
314
+ const x0 = tabXOf(spec);
315
+ const tabLeft = Math.max(left, x0 + inset);
316
+ const tabRight = Math.min(right, x0 + tabbedRectTabWidth(spec) - inset);
317
+ if (tabRight <= tabLeft) return null;
318
+ const align = spec.tabAlign ?? "left";
319
+ const flushLeft = tabLeft - left < 1e-6;
320
+ const flushRight = right - tabRight < 1e-6;
321
+ const slantLeft = (align === "right" || align === "center") && !flushLeft;
322
+ const slantRight = (align === "left" || align === "center") && !flushRight;
323
+ const sides = (slantLeft ? 1 : 0) + (slantRight ? 1 : 0);
324
+ const skew = sides === 0 ? 0 : Math.max(0, Math.min(spec.tabSkew ?? 0, (tabRight - tabLeft) / (2 * sides)));
325
+ return {
326
+ left,
327
+ right,
328
+ bottom,
329
+ tabTop,
330
+ shoulder,
331
+ tabLeft,
332
+ tabRight,
333
+ tabTopLeft: tabLeft + (slantLeft ? skew : 0),
334
+ tabTopRight: tabRight - (slantRight ? skew : 0),
335
+ flushLeft,
336
+ flushRight,
337
+ bodyless: false
338
+ };
339
+ }
340
+ function bodylessGeometryOf(spec, inset) {
341
+ const tabTop = inset;
342
+ const base = spec.tabHeight - inset;
343
+ const tabLeft = inset;
344
+ const tabRight = tabbedRectTabWidth(spec) - inset;
345
+ if (base <= tabTop || tabRight <= tabLeft) return null;
346
+ const align = spec.tabAlign ?? "left";
347
+ const slantLeft = align === "right" || align === "center";
348
+ const slantRight = align === "left" || align === "center";
349
+ const sides = (slantLeft ? 1 : 0) + (slantRight ? 1 : 0);
350
+ const skew = sides === 0 ? 0 : Math.max(0, Math.min(spec.tabSkew ?? 0, (tabRight - tabLeft) / (2 * sides)));
351
+ return {
352
+ left: tabLeft,
353
+ right: tabRight,
354
+ bottom: base,
355
+ tabTop,
356
+ // No fold to sit on: the shoulder *is* the outline's bottom edge, which
357
+ // keeps the label box (tabTop → shoulder) spanning the whole tab.
358
+ shoulder: base,
359
+ tabLeft,
360
+ tabRight,
361
+ tabTopLeft: tabLeft + (slantLeft ? skew : 0),
362
+ tabTopRight: tabRight - (slantRight ? skew : 0),
363
+ flushLeft: true,
364
+ flushRight: true,
365
+ bodyless: true
366
+ };
367
+ }
368
+ function tabXOf(spec) {
369
+ if (spec.height <= 0) return 0;
370
+ const tabW = tabbedRectTabWidth(spec);
371
+ const offset = spec.tabOffset ?? 0;
372
+ switch (spec.tabAlign ?? "left") {
373
+ case "center":
374
+ return (spec.width - tabW) / 2;
375
+ case "right":
376
+ return Math.max(0, spec.width - tabW - offset);
377
+ default:
378
+ return Math.min(offset, Math.max(0, spec.width - tabW));
379
+ }
380
+ }
381
+
382
+ // src/specs/shapeGeometry/bounds.ts
383
+ var TAU = Math.PI * 2;
384
+ function boundsOfCircle(spec) {
385
+ const r = spec.radius;
386
+ return { x: -r, y: -r, width: r * 2, height: r * 2 };
387
+ }
388
+ function scaleCircle(spec, factor) {
389
+ return { radius: spec.radius * factor };
390
+ }
391
+ function boundsOfEllipse(spec) {
392
+ return {
393
+ x: -spec.radiusX,
394
+ y: -spec.radiusY,
395
+ width: spec.radiusX * 2,
396
+ height: spec.radiusY * 2
397
+ };
398
+ }
399
+ function scaleEllipse(spec, factor) {
400
+ return { radiusX: spec.radiusX * factor, radiusY: spec.radiusY * factor };
401
+ }
402
+ function boundsOfRect(spec) {
403
+ return { x: 0, y: 0, width: spec.width, height: spec.height };
404
+ }
405
+ function scaleRect(spec, factor) {
406
+ return {
407
+ width: spec.width * factor,
408
+ height: spec.height * factor,
409
+ ...spec.cornerRadius !== void 0 ? { cornerRadius: spec.cornerRadius * factor } : {}
410
+ };
411
+ }
412
+ function boundsOfPolygon(spec) {
413
+ return polygonBounds(spec.vertices);
414
+ }
415
+ function scalePolygon(spec, factor) {
416
+ return { vertices: spec.vertices.map((v) => ({ x: v.x * factor, y: v.y * factor })) };
417
+ }
418
+ function verticesOfRegularPolygon(spec) {
419
+ return regularPolygonVertices(spec.sides, spec.radius, spec.rotation ?? 0);
420
+ }
421
+ function boundsOfRegularPolygon(spec) {
422
+ return polygonBounds(verticesOfRegularPolygon(spec));
423
+ }
424
+ function scaleRegularPolygon(spec, factor) {
425
+ return { radius: spec.radius * factor };
426
+ }
427
+ function verticesOfStar(spec) {
428
+ return starVertices(spec.points, spec.innerRadius, spec.outerRadius, spec.rotation ?? 0);
429
+ }
430
+ function boundsOfStar(spec) {
431
+ return polygonBounds(verticesOfStar(spec));
432
+ }
433
+ function scaleStar(spec, factor) {
434
+ return {
435
+ innerRadius: spec.innerRadius * factor,
436
+ outerRadius: spec.outerRadius * factor
437
+ };
438
+ }
439
+ function boundsOfArc(spec) {
440
+ const { innerR, outerR, startAngle: a0, endAngle: a1 } = spec;
441
+ if (a1 <= a0 || outerR <= 0) return { x: 0, y: 0, width: 0, height: 0 };
442
+ const corners = [
443
+ { x: Math.cos(a0) * innerR, y: Math.sin(a0) * innerR },
444
+ { x: Math.cos(a0) * outerR, y: Math.sin(a0) * outerR },
445
+ { x: Math.cos(a1) * innerR, y: Math.sin(a1) * innerR },
446
+ { x: Math.cos(a1) * outerR, y: Math.sin(a1) * outerR }
447
+ ];
448
+ const sweep = a1 - a0;
449
+ for (const k of [0, 1, 2, 3]) {
450
+ const cardinal = k * Math.PI / 2;
451
+ let n = Math.ceil((a0 - cardinal) / TAU);
452
+ if (cardinal + n * TAU < a0) n++;
453
+ const angle = cardinal + n * TAU;
454
+ if (angle <= a1 || sweep >= TAU) {
455
+ corners.push({ x: Math.cos(angle) * outerR, y: Math.sin(angle) * outerR });
456
+ }
457
+ }
458
+ return polygonBounds(corners);
459
+ }
460
+ function scaleArc(spec, factor) {
461
+ return { innerR: spec.innerR * factor, outerR: spec.outerR * factor };
462
+ }
463
+ function boundsOfPath(spec) {
464
+ return polygonBounds(spec.points);
465
+ }
466
+ function scalePath(spec, factor) {
467
+ return { points: spec.points.map((p) => ({ x: p.x * factor, y: p.y * factor })) };
468
+ }
469
+ function boundsOfComposite(spec) {
470
+ return { x: 0, y: 0, width: spec.width, height: spec.height };
471
+ }
472
+ function resolveCompositeRoot(spec) {
473
+ if (spec.root) return spec.root;
474
+ return {
475
+ kind: "rect",
476
+ x: 0,
477
+ y: 0,
478
+ width: spec.width,
479
+ height: spec.height,
480
+ ...spec.cornerRadius !== void 0 ? { cornerRadius: spec.cornerRadius } : {},
481
+ ...spec.fill !== void 0 ? { fill: spec.fill } : {},
482
+ ...spec.stroke !== void 0 ? { stroke: spec.stroke } : {}
483
+ };
484
+ }
485
+ function boundsOfCompositeRoot(root) {
486
+ switch (root.kind) {
487
+ case "circle":
488
+ return boundsOfCircle(root);
489
+ case "ellipse":
490
+ return boundsOfEllipse(root);
491
+ case "polygon":
492
+ return boundsOfPolygon(root);
493
+ case "regular-polygon":
494
+ return boundsOfRegularPolygon(root);
495
+ case "star":
496
+ return boundsOfStar(root);
497
+ case "arc":
498
+ return boundsOfArc(root);
499
+ default:
500
+ return boundsOfRect(root);
501
+ }
502
+ }
503
+ function compositeRootOffset(spec) {
504
+ const b = boundsOfCompositeRoot(resolveCompositeRoot(spec));
505
+ return {
506
+ x: spec.width / 2 - (b.x + b.width / 2),
507
+ y: spec.height / 2 - (b.y + b.height / 2)
508
+ };
509
+ }
510
+
511
+ // src/specs/shapeGeometry/contains.ts
512
+ var TAU2 = Math.PI * 2;
513
+ function strokeBandOf(stroke) {
514
+ const width = stroke?.width ?? (stroke ? 1 : 0);
515
+ if (!stroke || width <= 0) return { outer: 0, inner: 0 };
516
+ const alignment = stroke.alignment === "inside" ? 1 : stroke.alignment === "outside" ? 0 : 0.5;
517
+ const outer = (1 - alignment) * width;
518
+ return { outer, inner: width - outer };
519
+ }
520
+ function containsCircle(spec, localX, localY, pad = 0) {
521
+ const r = spec.radius + pad;
522
+ if (r <= 0) return false;
523
+ return localX * localX + localY * localY <= r * r;
524
+ }
525
+ function containsEllipse(spec, localX, localY, pad = 0) {
526
+ const rx = spec.radiusX + pad;
527
+ const ry = spec.radiusY + pad;
528
+ if (rx <= 0 || ry <= 0) return false;
529
+ const nx = localX / rx;
530
+ const ny = localY / ry;
531
+ return nx * nx + ny * ny <= 1;
532
+ }
533
+ function containsRect(spec, localX, localY, pad = 0) {
534
+ const w = spec.width + pad * 2;
535
+ const h = spec.height + pad * 2;
536
+ if (w <= 0 || h <= 0) return false;
537
+ const left = -pad;
538
+ const top = -pad;
539
+ if (localX < left || localX > left + w || localY < top || localY > top + h) return false;
540
+ const r = Math.min(Math.max(0, (spec.cornerRadius ?? 0) + pad), w / 2, h / 2);
541
+ if (r <= 0) return true;
542
+ const cx = clamp(localX, left + r, left + w - r);
543
+ const cy = clamp(localY, top + r, top + h - r);
544
+ const dx = localX - cx;
545
+ const dy = localY - cy;
546
+ return dx * dx + dy * dy <= r * r;
547
+ }
548
+ function containsPolygon(spec, localX, localY, pad = 0) {
549
+ return polygonContainsInflated(localX, localY, spec.vertices, pad);
550
+ }
551
+ function containsRegularPolygon(spec, localX, localY, pad = 0) {
552
+ return polygonContainsInflated(localX, localY, verticesOfRegularPolygon(spec), pad);
553
+ }
554
+ function containsStar(spec, localX, localY, pad = 0) {
555
+ return polygonContainsInflated(localX, localY, verticesOfStar(spec), pad);
556
+ }
557
+ function containsTabbedRect(spec, localX, localY, pad = 0) {
558
+ return polygonContainsInflated(localX, localY, tabbedRectOutline(spec, 0), pad);
559
+ }
560
+ function containsPath(spec, localX, localY, pad = 0) {
561
+ const closed = spec.closed === true || spec.smooth === true;
562
+ return polygonContainsInflated(localX, localY, spec.points, pad, closed);
563
+ }
564
+ function containsArc(spec, localX, localY, pad = 0) {
565
+ const { startAngle: a0, endAngle: a1 } = spec;
566
+ const innerR = Math.max(0, spec.innerR - pad);
567
+ const outerR = spec.outerR + pad;
568
+ if (a1 <= a0 || outerR <= 0) return false;
569
+ const rSq = localX * localX + localY * localY;
570
+ const inRadial = rSq >= innerR * innerR && rSq <= outerR * outerR;
571
+ const inSweep = a1 - a0 >= TAU2 || angleWithin(Math.atan2(localY, localX), a0, a1);
572
+ if (inSweep && inRadial) {
573
+ if (pad >= 0) return true;
574
+ return distanceToRadialEdgesSq(spec, localX, localY) > pad * pad;
575
+ }
576
+ if (pad <= 0 || a1 - a0 >= TAU2) return false;
577
+ return distanceToRadialEdgesSq(spec, localX, localY) <= pad * pad;
578
+ }
579
+ function containsComposite(spec, localX, localY, pad = 0) {
580
+ const off = compositeRootOffset(spec);
581
+ const root = resolveCompositeRoot(spec);
582
+ if (containsRootSpec(root, localX - off.x, localY - off.y, pad)) return true;
583
+ for (const p of spec.parts) {
584
+ if (p.part === "rect") {
585
+ if (p.fill === void 0 && !p.stroke) continue;
586
+ const box = {
587
+ width: p.width,
588
+ height: p.height,
589
+ ...p.cornerRadius !== void 0 ? { cornerRadius: p.cornerRadius } : {}
590
+ };
591
+ const half = partHalfStroke(p.stroke);
592
+ if (withHollowRule(
593
+ (q) => containsRect(box, localX - p.x, localY - p.y, q),
594
+ p.fill !== void 0,
595
+ pad + half,
596
+ half
597
+ )) {
598
+ return true;
599
+ }
600
+ } else if (p.part === "circle") {
601
+ if (p.fill === void 0 && !p.stroke) continue;
602
+ const disc = { radius: p.radius };
603
+ const half = partHalfStroke(p.stroke);
604
+ if (withHollowRule(
605
+ (q) => containsCircle(disc, localX - p.x, localY - p.y, q),
606
+ p.fill !== void 0,
607
+ pad + half,
608
+ half
609
+ )) {
610
+ return true;
611
+ }
612
+ } else if (p.part === "line") {
613
+ const tol = pad + partHalfStroke(p.stroke);
614
+ if (tol > 0 && distanceToSegmentSq(localX, localY, p.x, p.y, p.x2, p.y2) <= tol * tol) {
615
+ return true;
616
+ }
617
+ } else if (p.part === "icon" && p.background) {
618
+ const chip = {
619
+ width: p.size,
620
+ height: p.size,
621
+ ...p.background.cornerRadius !== void 0 ? { cornerRadius: p.background.cornerRadius } : {}
622
+ };
623
+ if (containsRect(chip, localX - p.x, localY - p.y, pad)) return true;
624
+ }
625
+ }
626
+ return false;
627
+ }
628
+ function containsSpec(spec, localX, localY, strokeTolerance) {
629
+ const fn = CONTAINS[spec.kind];
630
+ if (!fn) return void 0;
631
+ if (spec.kind === "composite") return fn(spec, localX, localY, strokeTolerance ?? 0);
632
+ const band = strokeTolerance !== void 0 ? { outer: strokeTolerance, inner: strokeTolerance } : strokeBandOf(spec.stroke);
633
+ return withHollowRule(
634
+ (pad) => fn(spec, localX, localY, pad),
635
+ hasSilhouetteFill(spec.fill),
636
+ band.outer,
637
+ band.inner
638
+ );
639
+ }
640
+ var CONTAINS = {
641
+ circle: containsCircle,
642
+ ellipse: containsEllipse,
643
+ rect: containsRect,
644
+ "tabbed-rect": containsTabbedRect,
645
+ polygon: containsPolygon,
646
+ "regular-polygon": containsRegularPolygon,
647
+ star: containsStar,
648
+ arc: containsArc,
649
+ path: containsPath,
650
+ composite: containsComposite
651
+ };
652
+ function containsRootSpec(root, x, y, pad) {
653
+ const band = strokeBandOf(root.stroke);
654
+ const test = (p) => {
655
+ switch (root.kind) {
656
+ case "circle":
657
+ return containsCircle(root, x, y, p);
658
+ case "ellipse":
659
+ return containsEllipse(root, x, y, p);
660
+ case "polygon":
661
+ return containsPolygon(root, x, y, p);
662
+ case "regular-polygon":
663
+ return containsRegularPolygon(root, x, y, p);
664
+ case "star":
665
+ return containsStar(root, x, y, p);
666
+ case "arc":
667
+ return containsArc(root, x, y, p);
668
+ default:
669
+ return containsRect(root, x, y, p);
670
+ }
671
+ };
672
+ return withHollowRule(test, hasSilhouetteFill(root.fill), pad + band.outer, band.inner);
673
+ }
674
+ function withHollowRule(test, filled, outer, inner) {
675
+ if (filled) return test(outer);
676
+ if (outer <= 0 && inner <= 0) return false;
677
+ return test(outer) && !test(-inner);
678
+ }
679
+ function partHalfStroke(stroke) {
680
+ if (!stroke) return 0;
681
+ return Math.max(0, stroke.width ?? 1) / 2;
682
+ }
683
+ function clamp(v, lo, hi) {
684
+ return v < lo ? lo : v > hi ? hi : v;
685
+ }
686
+ function angleWithin(theta, a0, a1) {
687
+ let n = Math.ceil((a0 - theta) / TAU2);
688
+ if (theta + n * TAU2 < a0) n++;
689
+ const t = theta + n * TAU2;
690
+ return t >= a0 && t <= a1;
691
+ }
692
+ function distanceToRadialEdgesSq(spec, x, y) {
693
+ const edge = (a) => {
694
+ const c = Math.cos(a);
695
+ const s = Math.sin(a);
696
+ return distanceToSegmentSq(
697
+ x,
698
+ y,
699
+ c * spec.innerR,
700
+ s * spec.innerR,
701
+ c * spec.outerR,
702
+ s * spec.outerR
703
+ );
704
+ };
705
+ return Math.min(edge(spec.startAngle), edge(spec.endAngle));
706
+ }
707
+
708
+ // src/specs/shapeGeometry/index.ts
709
+ function boundsOfSpec(spec) {
710
+ switch (spec.kind) {
711
+ case "circle":
712
+ return boundsOfCircle(spec);
713
+ case "ellipse":
714
+ return boundsOfEllipse(spec);
715
+ case "rect":
716
+ return boundsOfRect(spec);
717
+ case "tabbed-rect":
718
+ return tabbedRectBounds(spec);
719
+ case "polygon":
720
+ return boundsOfPolygon(spec);
721
+ case "regular-polygon":
722
+ return boundsOfRegularPolygon(spec);
723
+ case "star":
724
+ return boundsOfStar(spec);
725
+ case "arc":
726
+ return boundsOfArc(spec);
727
+ case "path":
728
+ return boundsOfPath(spec);
729
+ case "composite":
730
+ return boundsOfComposite(spec);
731
+ default:
732
+ return void 0;
733
+ }
734
+ }
735
+ function scaleSpec(spec, factor) {
736
+ switch (spec.kind) {
737
+ case "circle":
738
+ return scaleCircle(spec, factor);
739
+ case "ellipse":
740
+ return scaleEllipse(spec, factor);
741
+ case "rect":
742
+ return scaleRect(spec, factor);
743
+ case "tabbed-rect":
744
+ return scaleTabbedRect(spec, factor);
745
+ case "polygon":
746
+ return scalePolygon(spec, factor);
747
+ case "regular-polygon":
748
+ return scaleRegularPolygon(spec, factor);
749
+ case "star":
750
+ return scaleStar(spec, factor);
751
+ case "arc":
752
+ return scaleArc(spec, factor);
753
+ case "path":
754
+ return scalePath(spec, factor);
755
+ default:
756
+ return void 0;
757
+ }
758
+ }
759
+ function collapsedSpec(spec) {
760
+ return spec.kind === "tabbed-rect" ? collapsedTabbedRect() : void 0;
761
+ }
762
+ function fitSpecToContent(spec, content) {
763
+ return spec.kind === "tabbed-rect" ? fitTabbedRectToContent(spec, content) : void 0;
764
+ }
765
+
766
+ // src/state/data/flush.ts
767
+ var raf = typeof requestAnimationFrame === "function" ? requestAnimationFrame : void 0;
768
+ var caf = typeof cancelAnimationFrame === "function" ? cancelAnimationFrame : void 0;
769
+ function scheduleFlush(mode, cb) {
770
+ if (mode === "manual") return () => {
771
+ };
772
+ if (mode === "frame" && raf && caf) {
773
+ const handle = raf(() => cb());
774
+ return () => caf(handle);
775
+ }
776
+ let cancelled = false;
777
+ queueMicrotask(() => {
778
+ if (!cancelled) cb();
779
+ });
780
+ return () => {
781
+ cancelled = true;
782
+ };
783
+ }
784
+
785
+ // src/specs/SpecStore.ts
786
+ var SpecStore = class {
787
+ specs = /* @__PURE__ */ new Map();
788
+ added = /* @__PURE__ */ new Set();
789
+ changed = /* @__PURE__ */ new Set();
790
+ removed = /* @__PURE__ */ new Set();
791
+ listeners = /* @__PURE__ */ new Set();
792
+ version = 0;
793
+ scheduled = false;
794
+ flushMode = "microtask";
795
+ cancel;
796
+ // ── Reads ──────────────────────────────────────────────────────────────────
797
+ get(id) {
798
+ return this.specs.get(id);
799
+ }
800
+ has(id) {
801
+ return this.specs.has(id);
802
+ }
803
+ get size() {
804
+ return this.specs.size;
805
+ }
806
+ ids() {
807
+ return this.specs.keys();
808
+ }
809
+ entries() {
810
+ return this.specs.entries();
811
+ }
812
+ // ── Writes ─────────────────────────────────────────────────────────────────
813
+ /** Publish (or replace) the spec for `id`. */
814
+ set(id, spec) {
815
+ const existed = this.specs.has(id);
816
+ this.specs.set(id, spec);
817
+ if (existed) {
818
+ this.removed.delete(id);
819
+ if (!this.added.has(id)) this.changed.add(id);
820
+ } else {
821
+ this.added.add(id);
822
+ this.removed.delete(id);
823
+ }
824
+ this.schedule();
825
+ }
826
+ /**
827
+ * Shallow-merge `partial` over the stored spec. Returns `false` when `id` is
828
+ * unknown, so a caller can fall back to {@link set} with a full spec.
829
+ */
830
+ patch(id, partial) {
831
+ const current = this.specs.get(id);
832
+ if (!current) return false;
833
+ this.specs.set(id, { ...current, ...partial });
834
+ if (!this.added.has(id)) this.changed.add(id);
835
+ this.schedule();
836
+ return true;
837
+ }
838
+ delete(id) {
839
+ if (!this.specs.delete(id)) return;
840
+ this.changed.delete(id);
841
+ if (!this.added.delete(id)) this.removed.add(id);
842
+ this.schedule();
843
+ }
844
+ /** Drop every spec. Emits one flush listing all ids as removed. */
845
+ clear() {
846
+ for (const id of this.specs.keys()) {
847
+ if (!this.added.delete(id)) this.removed.add(id);
848
+ }
849
+ this.specs.clear();
850
+ this.changed.clear();
851
+ this.schedule();
852
+ }
853
+ // ── Flush ──────────────────────────────────────────────────────────────────
854
+ onFlush(listener) {
855
+ this.listeners.add(listener);
856
+ return () => this.listeners.delete(listener);
857
+ }
858
+ /**
859
+ * Choose **when** a coalesced flush fires. `'manual'` disarms auto-flush so the
860
+ * engine's single rAF drives it — which is how the renderer stays on one clock.
861
+ */
862
+ setFlushMode(mode) {
863
+ this.flushMode = mode;
864
+ if (mode === "manual" && this.scheduled) {
865
+ this.cancel?.();
866
+ this.scheduled = false;
867
+ }
868
+ }
869
+ /** Emit the pending delta, if any. Safe to call when nothing is dirty. */
870
+ flush() {
871
+ this.scheduled = false;
872
+ if (this.added.size === 0 && this.changed.size === 0 && this.removed.size === 0) return;
873
+ const event = {
874
+ added: [...this.added],
875
+ changed: [...this.changed],
876
+ removed: [...this.removed],
877
+ version: ++this.version
878
+ };
879
+ this.added.clear();
880
+ this.changed.clear();
881
+ this.removed.clear();
882
+ for (const listener of this.listeners) listener(event);
883
+ }
884
+ schedule() {
885
+ if (this.scheduled || this.flushMode === "manual") return;
886
+ this.scheduled = true;
887
+ this.cancel = scheduleFlush(this.flushMode, () => this.flush());
888
+ }
889
+ };
890
+
891
+ export { SpecStore, boundsOfArc, boundsOfCircle, boundsOfComposite, boundsOfCompositeRoot, boundsOfEllipse, boundsOfPath, boundsOfPolygon, boundsOfRect, boundsOfRegularPolygon, boundsOfSpec, boundsOfStar, collapsedSpec, collapsedTabbedRect, compositeRootOffset, connectorGeometryKey, containsArc, containsCircle, containsComposite, containsEllipse, containsPath, containsPolygon, containsRect, containsRegularPolygon, containsSpec, containsStar, containsTabbedRect, distanceToOutlineSq, distanceToSegmentSq, fitSpecToContent, fitTabbedRectToContent, hasSilhouetteFill, offsetPolygon, pointInPolygon, polygonBounds, polygonContainsInflated, rayPolygonIntersection, regularPolygonVertices, resolveCompositeRoot, roundedPolygonOutline, scaleArc, scaleCircle, scaleEllipse, scalePath, scalePolygon, scaleRect, scaleRegularPolygon, scaleSpec, scaleStar, scaleTabbedRect, starVertices, strokeBandOf, tabbedRectBounds, tabbedRectFoldLine, tabbedRectGeometry, tabbedRectOutline, tabbedRectTabBox, tabbedRectTabWidth, verticesOfRegularPolygon, verticesOfStar };
892
+ //# sourceMappingURL=index.js.map
893
+ //# sourceMappingURL=index.js.map