@pixerate/editor 0.34.0 → 0.35.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/THIRD_PARTY_NOTICES.md +7 -0
- package/dist/canvas/index.cjs +178 -623
- package/dist/canvas/index.cjs.map +1 -1
- package/dist/canvas/index.d.cts +10 -1
- package/dist/canvas/index.d.ts +10 -1
- package/dist/canvas/index.js +35 -480
- package/dist/canvas/index.js.map +1 -1
- package/dist/{chunk-UG5DVXJK.cjs → chunk-BGNPT5PE.cjs} +446 -6
- package/dist/chunk-BGNPT5PE.cjs.map +1 -0
- package/dist/{chunk-RK6HUKDI.js → chunk-EGPIVUEF.js} +451 -11
- package/dist/chunk-EGPIVUEF.js.map +1 -0
- package/dist/vector/index.cjs +1801 -116
- package/dist/vector/index.cjs.map +1 -1
- package/dist/vector/index.d.cts +373 -7
- package/dist/vector/index.d.ts +373 -7
- package/dist/vector/index.js +1721 -36
- package/dist/vector/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-RK6HUKDI.js.map +0 -1
- package/dist/chunk-UG5DVXJK.cjs.map +0 -1
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["/home/runner/work/editor/editor/packages/core/dist/chunk-UG5DVXJK.cjs","../src/canvas/polygonBoolean.ts","../src/canvas/curveFit.ts","../src/canvas/gradient.ts","../src/canvas/shapeGeometry.ts","../src/canvas/pathops/bezier.ts","../src/canvas/pathops/boolean.ts"],"names":["dist","TWO_PI","directed","corners","out","dot"],"mappings":"AAAA;AC0EO,SAAS,cAAA,CAAe,IAAA,EAAgC;AAC9D,EAAA,IAAI,IAAA,EAAM,CAAA;AACV,EAAA,MAAM,EAAA,EAAI,IAAA,CAAK,MAAA;AACf,EAAA,IAAA,CAAA,IAAS,EAAA,EAAI,CAAA,EAAG,EAAA,EAAI,CAAA,EAAG,CAAA,EAAA,EAAK;AAC3B,IAAA,MAAM,CAAC,EAAA,EAAI,EAAE,EA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AA;AAC8B,IAAA;AAC9B,EAAA;AACF;AAGuD;AACnB,EAAA;AACX,EAAA;AACM,IAAA;AAC7B,EAAA;AAC6B,EAAA;AAC/B;ANs7DoC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","file":"/home/runner/work/editor/editor/packages/core/dist/chunk-UG5DVXJK.cjs","sourcesContent":[null,"/**\n * Polygon boolean operations (union, intersection, difference, xor and divide) on\n * multi-polygons with holes.\n *\n * Algorithm (overlay + winding classification):\n * 1. Every input edge is collected into one segment set. Vertices are merged through a\n * tolerance-aware point pool, so points closer than `epsilon` become one vertex.\n * 2. Segments are split at every crossing, T-junction and collinear overlap until no two\n * segments cross (a planar arrangement). Splitting is repeated because snapping a new\n * vertex can move a segment by up to `epsilon`.\n * 3. Coincident segments (shared edges) are merged, keeping for every operand the signed\n * number of its edges that run along the segment.\n * 4. For every segment the winding number of each operand on its right side is computed\n * with a ray cast from the segment midpoint (the segment itself excluded), and the left\n * side follows from the signed multiplicities. The fill rule turns windings into\n * inside / outside flags and the operation decides whether each side is in the result.\n * 5. Segments with the result on exactly one side are kept, oriented with the result on\n * their left, and traced into rings (taking the sharpest left turn at shared vertices,\n * so rings that touch at a vertex stay separate simple rings). Rings with a positive\n * signed area are outer rings; negative ones are holes, assigned to the smallest outer\n * ring that contains them.\n *\n * Handles shared edges, touching vertices, collinear overlaps, holes, nested and disjoint\n * inputs and self-intersecting input rings (resolved by the operand's fill rule).\n *\n * Limits: the overlay is O(n²) in the number of edges (pruned by a sweep on x), which is\n * comfortable for canvas shapes (thousands of edges) but not for GIS-scale data. Features\n * narrower than `epsilon` collapse. Coordinates must be finite.\n */\nimport type { Point } from './types.js';\n\n/** A closed ring of points. The closing point may be repeated or omitted. */\nexport type PolygonRing = Point[];\n/** A polygon: the outer ring followed by its holes. */\nexport type PolygonWithHoles = PolygonRing[];\n/** A set of polygons. */\nexport type MultiPolygon = PolygonWithHoles[];\n\n/** SVG fill rule used to decide which areas of a ring set are inside. */\nexport type PolygonFillRule = 'nonzero' | 'evenodd';\n\n/** Boolean operations. `xor` is Freeform's Exclude. */\nexport type PolygonBooleanOperation = 'union' | 'intersection' | 'difference' | 'xor';\n\n/**\n * A raw ring set with a fill rule (e.g. flattened SVG path data). Ring orientation is\n * significant for `nonzero`.\n */\nexport interface PolygonOperand {\n\trings: PolygonRing[];\n\tfillRule?: PolygonFillRule;\n}\n\nexport type PolygonInput = MultiPolygon | PolygonOperand;\n\nexport interface PolygonBooleanOptions {\n\t/**\n\t * Distance below which points are treated as equal. Defaults to `1e-9` times the\n\t * largest coordinate magnitude of the inputs (at least `1e-9`).\n\t */\n\tepsilon?: number;\n}\n\n/** One region of `dividePolygons`, with the indices of the operands that cover it. */\nexport interface PolygonPiece {\n\tpolygon: PolygonWithHoles;\n\toperands: number[];\n}\n\n// ---------------------------------------------------------------------------\n// Basic measures\n// ---------------------------------------------------------------------------\n\n/** Signed shoelace area of a ring (positive when counter-clockwise in a y-up frame). */\nexport function ringSignedArea(ring: readonly Point[]): number {\n\tlet sum = 0;\n\tconst n = ring.length;\n\tfor (let i = 0; i < n; i++) {\n\t\tconst [x1, y1] = ring[i];\n\t\tconst [x2, y2] = ring[(i + 1) % n];\n\t\tsum += x1 * y2 - x2 * y1;\n\t}\n\treturn sum / 2;\n}\n\n/** Area of a polygon (outer ring minus holes). */\nexport function polygonArea(polygon: PolygonWithHoles): number {\n\tif (polygon.length === 0) return 0;\n\tlet area = Math.abs(ringSignedArea(polygon[0]));\n\tfor (let i = 1; i < polygon.length; i++) area -= Math.abs(ringSignedArea(polygon[i]));\n\treturn area;\n}\n\n/** Total area of a multi-polygon (holes subtracted). */\nexport function multiPolygonArea(multi: MultiPolygon): number {\n\tlet total = 0;\n\tfor (const polygon of multi) total += polygonArea(polygon);\n\treturn total;\n}\n\n/** Axis-aligned bounds of a multi-polygon (or ring list), or `null` when empty. */\nexport function multiPolygonBounds(\n\tinput: MultiPolygon | PolygonRing[]\n): { minX: number; minY: number; maxX: number; maxY: number } | null {\n\tlet minX = Infinity;\n\tlet minY = Infinity;\n\tlet maxX = -Infinity;\n\tlet maxY = -Infinity;\n\tconst visit = (ring: PolygonRing) => {\n\t\tfor (const [x, y] of ring) {\n\t\t\tif (x < minX) minX = x;\n\t\t\tif (y < minY) minY = y;\n\t\t\tif (x > maxX) maxX = x;\n\t\t\tif (y > maxY) maxY = y;\n\t\t}\n\t};\n\tfor (const item of input as any[]) {\n\t\tif (item.length > 0 && Array.isArray(item[0]) && Array.isArray(item[0][0])) {\n\t\t\tfor (const ring of item as PolygonRing[]) visit(ring);\n\t\t} else {\n\t\t\tvisit(item as PolygonRing);\n\t\t}\n\t}\n\treturn minX === Infinity ? null : { minX, minY, maxX, maxY };\n}\n\n/**\n * Whether a point is inside a ring set under a fill rule. Points exactly on an edge may\n * report either side.\n */\nexport function pointInRings(\n\tpoint: Point,\n\trings: readonly (readonly Point[])[],\n\tfillRule: PolygonFillRule = 'nonzero'\n): boolean {\n\tconst [px, py] = point;\n\tlet winding = 0;\n\tfor (const ring of rings) {\n\t\tconst n = ring.length;\n\t\tfor (let i = 0; i < n; i++) {\n\t\t\tconst [ax, ay] = ring[i];\n\t\t\tconst [bx, by] = ring[(i + 1) % n];\n\t\t\tif (ay <= py && by > py) {\n\t\t\t\tif (ax + ((py - ay) / (by - ay)) * (bx - ax) > px) winding++;\n\t\t\t} else if (by <= py && ay > py) {\n\t\t\t\tif (ax + ((py - ay) / (by - ay)) * (bx - ax) > px) winding--;\n\t\t\t}\n\t\t}\n\t}\n\treturn fillRule === 'evenodd' ? (winding & 1) === 1 : winding !== 0;\n}\n\n/** Whether a point is inside a multi-polygon (holes excluded). */\nexport function pointInMultiPolygon(point: Point, multi: MultiPolygon): boolean {\n\treturn multi.some((polygon) => pointInRings(point, polygon, 'evenodd'));\n}\n\n// ---------------------------------------------------------------------------\n// Operand normalisation\n// ---------------------------------------------------------------------------\n\nfunction cleanRing(ring: readonly Point[]): Point[] {\n\tconst out: Point[] = [];\n\tfor (const p of ring) {\n\t\tif (!Number.isFinite(p[0]) || !Number.isFinite(p[1])) continue;\n\t\tconst last = out[out.length - 1];\n\t\tif (last && last[0] === p[0] && last[1] === p[1]) continue;\n\t\tout.push([p[0], p[1]]);\n\t}\n\twhile (out.length > 1) {\n\t\tconst first = out[0];\n\t\tconst last = out[out.length - 1];\n\t\tif (first[0] === last[0] && first[1] === last[1]) out.pop();\n\t\telse break;\n\t}\n\treturn out;\n}\n\nfunction isOperand(input: PolygonInput): input is PolygonOperand {\n\treturn !Array.isArray(input) && typeof input === 'object' && input !== null && Array.isArray(input.rings);\n}\n\ninterface NormalizedOperand {\n\trings: Point[][];\n\tfillRule: PolygonFillRule;\n}\n\n/**\n * Multi-polygons are re-oriented (outer rings positive, holes negative) and filled with\n * `nonzero`, so overlapping polygons in one operand behave as their union.\n */\nfunction normalizeInput(input: PolygonInput): NormalizedOperand {\n\tif (isOperand(input)) {\n\t\treturn {\n\t\t\trings: input.rings.map(cleanRing).filter((r) => r.length >= 3),\n\t\t\tfillRule: input.fillRule ?? 'nonzero'\n\t\t};\n\t}\n\tconst rings: Point[][] = [];\n\tfor (const polygon of input ?? []) {\n\t\tpolygon.forEach((raw, index) => {\n\t\t\tconst ring = cleanRing(raw);\n\t\t\tif (ring.length < 3) return;\n\t\t\tconst area = ringSignedArea(ring);\n\t\t\tif (area === 0) return;\n\t\t\tconst wantPositive = index === 0;\n\t\t\trings.push(area > 0 === wantPositive ? ring : ring.reverse());\n\t\t});\n\t}\n\treturn { rings, fillRule: 'nonzero' };\n}\n\n// ---------------------------------------------------------------------------\n// Overlay\n// ---------------------------------------------------------------------------\n\ninterface Vertex {\n\tid: number;\n\tx: number;\n\ty: number;\n}\n\n/** Merges points closer than `eps` (grid hash with neighbour lookup). */\nclass PointPool {\n\treadonly vertices: Vertex[] = [];\n\tprivate readonly cells = new Map<string, number[]>();\n\tconstructor(private readonly eps: number) {}\n\n\tget(x: number, y: number): Vertex {\n\t\tconst cx = Math.floor(x / this.eps);\n\t\tconst cy = Math.floor(y / this.eps);\n\t\tlet best: Vertex | null = null;\n\t\tlet bestDist = Infinity;\n\t\tfor (let dx = -1; dx <= 1; dx++) {\n\t\t\tfor (let dy = -1; dy <= 1; dy++) {\n\t\t\t\tconst ids = this.cells.get(`${cx + dx},${cy + dy}`);\n\t\t\t\tif (!ids) continue;\n\t\t\t\tfor (const id of ids) {\n\t\t\t\t\tconst v = this.vertices[id];\n\t\t\t\t\tconst d = Math.hypot(v.x - x, v.y - y);\n\t\t\t\t\tif (d <= this.eps && d < bestDist) {\n\t\t\t\t\t\tbest = v;\n\t\t\t\t\t\tbestDist = d;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (best) return best;\n\t\tconst vertex: Vertex = { id: this.vertices.length, x, y };\n\t\tthis.vertices.push(vertex);\n\t\tconst key = `${cx},${cy}`;\n\t\tconst bucket = this.cells.get(key);\n\t\tif (bucket) bucket.push(vertex.id);\n\t\telse this.cells.set(key, [vertex.id]);\n\t\treturn vertex;\n\t}\n}\n\ninterface RawSegment {\n\ta: Vertex;\n\tb: Vertex;\n\toperand: number;\n}\n\ninterface Splits {\n\tsegment: RawSegment;\n\tpoints: Map<number, { t: number; v: Vertex }>;\n}\n\n/** Projection parameter of `p` on `a→b` and its distance to the segment line. */\nfunction project(a: Vertex, b: Vertex, p: Vertex): { t: number; dist: number } {\n\tconst dx = b.x - a.x;\n\tconst dy = b.y - a.y;\n\tconst len2 = dx * dx + dy * dy;\n\tconst t = len2 === 0 ? 0 : ((p.x - a.x) * dx + (p.y - a.y) * dy) / len2;\n\tconst cx = a.x + t * dx;\n\tconst cy = a.y + t * dy;\n\treturn { t, dist: Math.hypot(p.x - cx, p.y - cy) };\n}\n\nfunction addSplit(splits: Splits, t: number, v: Vertex) {\n\tconst { a, b } = splits.segment;\n\tif (v.id === a.id || v.id === b.id) return;\n\tif (!splits.points.has(v.id)) splits.points.set(v.id, { t, v });\n}\n\n/** One pass: splits every segment at crossings / touches with other segments. */\nfunction splitPass(segments: RawSegment[], pool: PointPool, eps: number): { segments: RawSegment[]; changed: boolean } {\n\tconst order = segments\n\t\t.map((s, i) => ({\n\t\t\ti,\n\t\t\tminX: Math.min(s.a.x, s.b.x),\n\t\t\tmaxX: Math.max(s.a.x, s.b.x),\n\t\t\tminY: Math.min(s.a.y, s.b.y),\n\t\t\tmaxY: Math.max(s.a.y, s.b.y)\n\t\t}))\n\t\t.sort((p, q) => p.minX - q.minX);\n\tconst splits: Splits[] = segments.map((segment) => ({ segment, points: new Map() }));\n\n\tfor (let oi = 0; oi < order.length; oi++) {\n\t\tconst bi = order[oi];\n\t\tconst s1 = segments[bi.i];\n\t\tfor (let oj = oi + 1; oj < order.length; oj++) {\n\t\t\tconst bj = order[oj];\n\t\t\tif (bj.minX > bi.maxX + eps) break;\n\t\t\tif (bj.minY > bi.maxY + eps || bj.maxY < bi.minY - eps) continue;\n\t\t\tconst s2 = segments[bj.i];\n\t\t\tconst sharedA = s1.a.id === s2.a.id || s1.a.id === s2.b.id;\n\t\t\tconst sharedB = s1.b.id === s2.a.id || s1.b.id === s2.b.id;\n\t\t\tif (sharedA && sharedB) continue; // identical (or reversed) segment\n\n\t\t\tlet touched = false;\n\t\t\t// Endpoints of one segment lying on the other: T-junctions and collinear overlaps.\n\t\t\tfor (const [seg, other, split] of [\n\t\t\t\t[s1, s2, splits[bi.i]],\n\t\t\t\t[s2, s1, splits[bj.i]]\n\t\t\t] as const) {\n\t\t\t\tfor (const p of [other.a, other.b]) {\n\t\t\t\t\tif (p.id === seg.a.id || p.id === seg.b.id) continue;\n\t\t\t\t\tconst { t, dist } = project(seg.a, seg.b, p);\n\t\t\t\t\tconst len = Math.hypot(seg.b.x - seg.a.x, seg.b.y - seg.a.y);\n\t\t\t\t\tif (dist <= eps && t * len > eps && (1 - t) * len > eps) {\n\t\t\t\t\t\taddSplit(split, t, p);\n\t\t\t\t\t\ttouched = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (touched) continue;\n\n\t\t\t// Proper crossing.\n\t\t\tconst d1x = s1.b.x - s1.a.x;\n\t\t\tconst d1y = s1.b.y - s1.a.y;\n\t\t\tconst d2x = s2.b.x - s2.a.x;\n\t\t\tconst d2y = s2.b.y - s2.a.y;\n\t\t\tconst denom = d1x * d2y - d1y * d2x;\n\t\t\tconst len1 = Math.hypot(d1x, d1y);\n\t\t\tconst len2 = Math.hypot(d2x, d2y);\n\t\t\tif (Math.abs(denom) <= 1e-12 * len1 * len2) continue; // parallel\n\t\t\tconst ex = s2.a.x - s1.a.x;\n\t\t\tconst ey = s2.a.y - s1.a.y;\n\t\t\tconst t = (ex * d2y - ey * d2x) / denom;\n\t\t\tconst u = (ex * d1y - ey * d1x) / denom;\n\t\t\tconst tolT = eps / len1;\n\t\t\tconst tolU = eps / len2;\n\t\t\tif (t < -tolT || t > 1 + tolT || u < -tolU || u > 1 + tolU) continue;\n\t\t\tif (sharedA || sharedB) continue; // they meet at the shared endpoint only\n\t\t\tconst v = pool.get(s1.a.x + t * d1x, s1.a.y + t * d1y);\n\t\t\taddSplit(splits[bi.i], project(s1.a, s1.b, v).t, v);\n\t\t\taddSplit(splits[bj.i], project(s2.a, s2.b, v).t, v);\n\t\t}\n\t}\n\n\tlet changed = false;\n\tconst out: RawSegment[] = [];\n\tfor (const { segment, points } of splits) {\n\t\tif (points.size === 0) {\n\t\t\tout.push(segment);\n\t\t\tcontinue;\n\t\t}\n\t\tchanged = true;\n\t\tconst ordered = [...points.values()].sort((p, q) => p.t - q.t);\n\t\tlet prev = segment.a;\n\t\tfor (const { v } of ordered) {\n\t\t\tif (v.id !== prev.id) out.push({ a: prev, b: v, operand: segment.operand });\n\t\t\tprev = v;\n\t\t}\n\t\tif (prev.id !== segment.b.id) out.push({ a: prev, b: segment.b, operand: segment.operand });\n\t}\n\treturn { segments: out, changed };\n}\n\ninterface Edge {\n\ta: Vertex;\n\tb: Vertex;\n\t/** Signed multiplicity per operand (+1 per edge running a→b, −1 per edge running b→a). */\n\tmult: number[];\n}\n\ninterface Overlay {\n\tedges: Edge[];\n\t/** Winding number per operand on the right of each edge. */\n\tright: number[][];\n\t/** Winding number per operand on the left of each edge. */\n\tleft: number[][];\n\tfillRules: PolygonFillRule[];\n\teps: number;\n}\n\nfunction defaultEpsilon(operands: NormalizedOperand[]): number {\n\tlet max = 0;\n\tfor (const op of operands) {\n\t\tfor (const ring of op.rings) {\n\t\t\tfor (const [x, y] of ring) {\n\t\t\t\tconst m = Math.max(Math.abs(x), Math.abs(y));\n\t\t\t\tif (m > max) max = m;\n\t\t\t}\n\t\t}\n\t}\n\treturn Math.max(1e-9, max * 1e-9);\n}\n\nfunction buildOverlay(inputs: PolygonInput[], options: PolygonBooleanOptions): Overlay {\n\tconst operands = inputs.map(normalizeInput);\n\tconst eps = options.epsilon && options.epsilon > 0 ? options.epsilon : defaultEpsilon(operands);\n\tconst pool = new PointPool(eps);\n\tlet segments: RawSegment[] = [];\n\toperands.forEach((op, operand) => {\n\t\tfor (const ring of op.rings) {\n\t\t\tconst verts = ring.map(([x, y]) => pool.get(x, y));\n\t\t\tfor (let i = 0; i < verts.length; i++) {\n\t\t\t\tconst a = verts[i];\n\t\t\t\tconst b = verts[(i + 1) % verts.length];\n\t\t\t\tif (a.id !== b.id) segments.push({ a, b, operand });\n\t\t\t}\n\t\t}\n\t});\n\n\tfor (let pass = 0; pass < 8; pass++) {\n\t\tconst result = splitPass(segments, pool, eps);\n\t\tsegments = result.segments;\n\t\tif (!result.changed) break;\n\t}\n\n\t// Merge coincident segments.\n\tconst count = operands.length;\n\tconst byKey = new Map<string, Edge>();\n\tfor (const s of segments) {\n\t\tconst forward = s.a.id < s.b.id;\n\t\tconst a = forward ? s.a : s.b;\n\t\tconst b = forward ? s.b : s.a;\n\t\tconst key = `${a.id},${b.id}`;\n\t\tlet edge = byKey.get(key);\n\t\tif (!edge) {\n\t\t\tedge = { a, b, mult: new Array(count).fill(0) };\n\t\t\tbyKey.set(key, edge);\n\t\t}\n\t\tedge.mult[s.operand] += forward ? 1 : -1;\n\t}\n\tconst edges = [...byKey.values()].filter((e) => e.mult.some((m) => m !== 0));\n\n\tconst right: number[][] = [];\n\tconst left: number[][] = [];\n\tfor (let i = 0; i < edges.length; i++) {\n\t\tconst edge = edges[i];\n\t\t// Cast the ray across the edge rather than along it: rotating by −90° keeps windings\n\t\t// and turns a mostly horizontal edge into a mostly vertical one.\n\t\tconst horizontal = Math.abs(edge.b.y - edge.a.y) < Math.abs(edge.b.x - edge.a.x);\n\t\tconst tx = (v: Vertex) => (horizontal ? v.y : v.x);\n\t\tconst ty = (v: Vertex) => (horizontal ? -v.x : v.y);\n\t\tconst ax = tx(edge.a);\n\t\tconst ay = ty(edge.a);\n\t\tconst bx = tx(edge.b);\n\t\tconst by = ty(edge.b);\n\t\tconst mx = (ax + bx) / 2;\n\t\tconst my = (ay + by) / 2;\n\t\tconst winding = new Array(count).fill(0);\n\t\tfor (let j = 0; j < edges.length; j++) {\n\t\t\tif (j === i) continue;\n\t\t\tconst other = edges[j];\n\t\t\tconst oax = tx(other.a);\n\t\t\tconst oay = ty(other.a);\n\t\t\tconst obx = tx(other.b);\n\t\t\tconst oby = ty(other.b);\n\t\t\tlet dir = 0;\n\t\t\tif (oay <= my && oby > my) dir = 1;\n\t\t\telse if (oby <= my && oay > my) dir = -1;\n\t\t\telse continue;\n\t\t\tconst x = oax + ((my - oay) / (oby - oay)) * (obx - oax);\n\t\t\tif (x <= mx) continue;\n\t\t\tfor (let k = 0; k < count; k++) {\n\t\t\t\tif (other.mult[k] !== 0) winding[k] += dir * other.mult[k];\n\t\t\t}\n\t\t}\n\t\t// The ray starts just past the midpoint on the +x side, which is the right of the\n\t\t// edge when it points up (dy > 0) and its left otherwise.\n\t\tconst sideIsRight = by > ay;\n\t\tconst r = sideIsRight ? winding : winding.map((w, k) => w - edge.mult[k]);\n\t\tright.push(r);\n\t\tleft.push(r.map((w, k) => w + edge.mult[k]));\n\t}\n\treturn { edges, right, left, fillRules: operands.map((o) => o.fillRule), eps };\n}\n\nfunction insideFlags(windings: number[], fillRules: PolygonFillRule[]): boolean[] {\n\treturn windings.map((w, k) => (fillRules[k] === 'evenodd' ? (w & 1) === 1 : w !== 0));\n}\n\n// ---------------------------------------------------------------------------\n// Ring extraction\n// ---------------------------------------------------------------------------\n\ninterface DirectedEdge {\n\tfrom: Vertex;\n\tto: Vertex;\n\tangle: number;\n}\n\n/** Traces the kept directed edges (result on the left) into rings. */\nfunction traceRings(directed: DirectedEdge[]): Vertex[][] {\n\tconst outgoing = new Map<number, DirectedEdge[]>();\n\tfor (const e of directed) {\n\t\tconst list = outgoing.get(e.from.id);\n\t\tif (list) list.push(e);\n\t\telse outgoing.set(e.from.id, [e]);\n\t}\n\tconst TWO_PI = Math.PI * 2;\n\tconst next = (incoming: DirectedEdge): DirectedEdge | undefined => {\n\t\tconst candidates = outgoing.get(incoming.to.id);\n\t\tif (!candidates) return undefined;\n\t\tconst back = Math.atan2(incoming.from.y - incoming.to.y, incoming.from.x - incoming.to.x);\n\t\tlet best: DirectedEdge | undefined;\n\t\tlet bestTurn = Infinity;\n\t\tfor (const c of candidates) {\n\t\t\t// Clockwise sweep from the reversed incoming edge: the first hit is the sharpest\n\t\t\t// left turn, which keeps the result region on the left and splits rings at\n\t\t\t// vertices they merely touch.\n\t\t\tlet turn = back - c.angle;\n\t\t\tturn -= Math.floor(turn / TWO_PI) * TWO_PI;\n\t\t\tif (turn <= 1e-15) turn = TWO_PI;\n\t\t\tif (turn < bestTurn) {\n\t\t\t\tbestTurn = turn;\n\t\t\t\tbest = c;\n\t\t\t}\n\t\t}\n\t\treturn best;\n\t};\n\n\tconst used = new Set<DirectedEdge>();\n\tconst rings: Vertex[][] = [];\n\tfor (const start of directed) {\n\t\tif (used.has(start)) continue;\n\t\tconst ring: Vertex[] = [];\n\t\tlet edge: DirectedEdge | undefined = start;\n\t\tlet guard = directed.length + 1;\n\t\twhile (edge && !used.has(edge) && guard-- > 0) {\n\t\t\tused.add(edge);\n\t\t\tring.push(edge.from);\n\t\t\tedge = next(edge);\n\t\t}\n\t\tif (ring.length >= 3) rings.push(ring);\n\t}\n\treturn rings;\n}\n\n/** Removes vertices that lie on the straight line between their neighbours. */\nfunction removeCollinear(ring: Point[], eps: number): Point[] {\n\tlet points = ring;\n\tlet changed = true;\n\twhile (changed && points.length > 3) {\n\t\tchanged = false;\n\t\tconst out: Point[] = [];\n\t\tconst n = points.length;\n\t\tfor (let i = 0; i < n; i++) {\n\t\t\tconst prev = out.length > 0 ? out[out.length - 1] : points[(i - 1 + n) % n];\n\t\t\tconst cur = points[i];\n\t\t\tconst nxt = points[(i + 1) % n];\n\t\t\tconst dx = nxt[0] - prev[0];\n\t\t\tconst dy = nxt[1] - prev[1];\n\t\t\tconst len = Math.hypot(dx, dy);\n\t\t\tconst cross = (cur[0] - prev[0]) * dy - (cur[1] - prev[1]) * dx;\n\t\t\tconst dot = (cur[0] - prev[0]) * dx + (cur[1] - prev[1]) * dy;\n\t\t\tif (len > 0 && Math.abs(cross) / len <= eps && dot > 0 && dot < len * len) {\n\t\t\t\tchanged = true;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tout.push(cur);\n\t\t}\n\t\tif (out.length < 3) return out;\n\t\tpoints = out;\n\t}\n\treturn points;\n}\n\nfunction assemble(rings: Vertex[][], eps: number): MultiPolygon {\n\tconst outers: { ring: Point[]; area: number }[] = [];\n\tconst holes: { ring: Point[]; area: number }[] = [];\n\tconst minArea = eps * eps;\n\tfor (const vertices of rings) {\n\t\tconst ring = removeCollinear(\n\t\t\tvertices.map((v) => [v.x, v.y] as Point),\n\t\t\teps\n\t\t);\n\t\tif (ring.length < 3) continue;\n\t\tconst area = ringSignedArea(ring);\n\t\tif (Math.abs(area) <= minArea) continue;\n\t\t(area > 0 ? outers : holes).push({ ring, area: Math.abs(area) });\n\t}\n\touters.sort((a, b) => b.area - a.area);\n\tconst polygons: PolygonWithHoles[] = outers.map((o) => [o.ring]);\n\tfor (const hole of holes) {\n\t\t// Sample the midpoint of a hole edge: overlay edges are unique, so it is not on an\n\t\t// outer ring's boundary.\n\t\tconst [p, q] = [hole.ring[0], hole.ring[1]];\n\t\tconst sample: Point = [(p[0] + q[0]) / 2, (p[1] + q[1]) / 2];\n\t\tlet owner = -1;\n\t\tfor (let i = outers.length - 1; i >= 0; i--) {\n\t\t\t// Iterating smallest-first finds the tightest container.\n\t\t\tif (outers[i].area >= hole.area && pointInRings(sample, [outers[i].ring], 'evenodd')) {\n\t\t\t\towner = i;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (owner >= 0) polygons[owner].push(hole.ring);\n\t}\n\treturn polygons;\n}\n\nfunction extract(overlay: Overlay, isInside: (flags: boolean[]) => boolean): MultiPolygon {\n\tconst directed: DirectedEdge[] = [];\n\toverlay.edges.forEach((edge, i) => {\n\t\tconst l = isInside(insideFlags(overlay.left[i], overlay.fillRules));\n\t\tconst r = isInside(insideFlags(overlay.right[i], overlay.fillRules));\n\t\tif (l === r) return;\n\t\tconst [from, to] = l ? [edge.a, edge.b] : [edge.b, edge.a];\n\t\tdirected.push({ from, to, angle: Math.atan2(to.y - from.y, to.x - from.x) });\n\t});\n\treturn assemble(traceRings(directed), overlay.eps);\n}\n\nfunction predicate(operation: PolygonBooleanOperation): (flags: boolean[]) => boolean {\n\tswitch (operation) {\n\t\tcase 'union':\n\t\t\treturn (f) => f.some(Boolean);\n\t\tcase 'intersection':\n\t\t\treturn (f) => f.length > 0 && f.every(Boolean);\n\t\tcase 'difference':\n\t\t\treturn (f) => f[0] === true && !f.slice(1).some(Boolean);\n\t\tcase 'xor':\n\t\t\treturn (f) => f.filter(Boolean).length % 2 === 1;\n\t}\n}\n\n// ---------------------------------------------------------------------------\n// Public operations\n// ---------------------------------------------------------------------------\n\n/**\n * Applies a boolean operation to any number of operands.\n * - `union`: covered by any operand.\n * - `intersection`: covered by every operand.\n * - `difference`: the first operand minus all others.\n * - `xor`: covered by an odd number of operands (Exclude).\n *\n * Returns simple rings: outer rings have a positive signed area, holes a negative one.\n * Rings may touch at vertices but never cross.\n */\nexport function polygonBoolean(\n\toperation: PolygonBooleanOperation,\n\toperands: PolygonInput[],\n\toptions: PolygonBooleanOptions = {}\n): MultiPolygon {\n\tif (operands.length === 0) return [];\n\treturn extract(buildOverlay(operands, options), predicate(operation));\n}\n\nexport const unionPolygons = (a: PolygonInput, b: PolygonInput, options?: PolygonBooleanOptions) =>\n\tpolygonBoolean('union', [a, b], options);\nexport const intersectPolygons = (a: PolygonInput, b: PolygonInput, options?: PolygonBooleanOptions) =>\n\tpolygonBoolean('intersection', [a, b], options);\nexport const subtractPolygons = (a: PolygonInput, b: PolygonInput, options?: PolygonBooleanOptions) =>\n\tpolygonBoolean('difference', [a, b], options);\nexport const xorPolygons = (a: PolygonInput, b: PolygonInput, options?: PolygonBooleanOptions) =>\n\tpolygonBoolean('xor', [a, b], options);\n\n/**\n * Splits the overlay of the operands into its distinct regions (Freeform's Divide): every\n * connected area covered by the same set of operands becomes one piece. Pieces are ordered\n * by their operand set, then by size (largest first).\n */\nexport function dividePolygons(operands: PolygonInput[], options: PolygonBooleanOptions = {}): PolygonPiece[] {\n\tif (operands.length === 0) return [];\n\tconst overlay = buildOverlay(operands, options);\n\tconst signatures = new Map<string, number[]>();\n\tconst note = (windings: number[]) => {\n\t\tconst flags = insideFlags(windings, overlay.fillRules);\n\t\tif (!flags.some(Boolean)) return;\n\t\tconst key = flags.map((f) => (f ? '1' : '0')).join('');\n\t\tif (!signatures.has(key)) signatures.set(key, flags.flatMap((f, k) => (f ? [k] : [])));\n\t};\n\toverlay.edges.forEach((_, i) => {\n\t\tnote(overlay.left[i]);\n\t\tnote(overlay.right[i]);\n\t});\n\tconst pieces: PolygonPiece[] = [];\n\tconst keys = [...signatures.keys()].sort();\n\tfor (const key of keys) {\n\t\tconst polygons = extract(overlay, (flags) => flags.map((f) => (f ? '1' : '0')).join('') === key);\n\t\tpolygons.sort((a, b) => polygonArea(b) - polygonArea(a));\n\t\tfor (const polygon of polygons) pieces.push({ polygon, operands: signatures.get(key)! });\n\t}\n\treturn pieces;\n}\n","/**\n * Least-squares cubic Bézier fitting of point sequences (Schneider-style: fit one cubic with\n * fixed end tangents, reparameterise with Newton steps, split at the worst point), with\n * corner preservation by a turning-angle test.\n *\n * Portions ported from vectorcraft-main/crates/pathops/src/fit.rs and\n * vectorcraft-main/crates/pathops/src/edit.rs (MIT OR Apache-2.0, © ArtCraft Team and\n * contributors). See THIRD_PARTY_NOTICES.md.\n */\nimport type { Point } from './types.js';\n\n/** Same as `formatNumber` in shapeGeometry (kept local to avoid an import cycle). */\nfunction formatNumber(value: number, precision: number): string {\n\tconst factor = 10 ** precision;\n\tconst rounded = Math.round(value * factor) / factor;\n\treturn Object.is(rounded, -0) ? '0' : String(rounded);\n}\n\n/** One cubic Bézier segment. */\nexport interface CubicSegment {\n\tp0: Point;\n\tp1: Point;\n\tp2: Point;\n\tp3: Point;\n}\n\nexport interface FitCubicsOptions {\n\t/**\n\t * Vertices where the polyline turns by more than this many degrees stay sharp corners\n\t * (default 30). Use 180 to smooth through every vertex.\n\t */\n\tcornerAngleDeg?: number;\n\t/** Treat the points as a closed ring (the last point connects back to the first). */\n\tclosed?: boolean;\n}\n\n/** Maximum Newton reparameterisation rounds per fit. */\nconst MAX_REPARAM = 6;\n\ntype Vec = [number, number];\n\nconst sub = (a: Point, b: Point): Vec => [a[0] - b[0], a[1] - b[1]];\nconst add = (a: Point, v: Vec, s = 1): Vec => [a[0] + v[0] * s, a[1] + v[1] * s];\nconst dot = (a: Vec, b: Vec) => a[0] * b[0] + a[1] * b[1];\nconst dist = (a: Point, b: Point) => Math.hypot(a[0] - b[0], a[1] - b[1]);\n\nfunction unit(v: Vec): Vec | null {\n\tconst l = Math.hypot(v[0], v[1]);\n\treturn l > 1e-12 && Number.isFinite(l) ? [v[0] / l, v[1] / l] : null;\n}\n\nfunction evalCubic(c: CubicSegment, t: number): Vec {\n\tconst s = 1 - t;\n\tconst b0 = s * s * s;\n\tconst b1 = 3 * s * s * t;\n\tconst b2 = 3 * s * t * t;\n\tconst b3 = t * t * t;\n\treturn [\n\t\tb0 * c.p0[0] + b1 * c.p1[0] + b2 * c.p2[0] + b3 * c.p3[0],\n\t\tb0 * c.p0[1] + b1 * c.p1[1] + b2 * c.p2[1] + b3 * c.p3[1]\n\t];\n}\n\n/** Evaluates a cubic segment at `t` ∈ [0, 1]. */\nexport function cubicPointAt(c: CubicSegment, t: number): Point {\n\treturn evalCubic(c, t);\n}\n\nfunction dedup(points: readonly Point[]): Point[] {\n\tconst out: Point[] = [];\n\tfor (const p of points) {\n\t\tif (!Number.isFinite(p[0]) || !Number.isFinite(p[1])) continue;\n\t\tif (out.length === 0 || dist(out[out.length - 1], p) > 1e-12) out.push(p);\n\t}\n\treturn out;\n}\n\n/** Chord-length parameters (0 to 1), one per point. */\nfunction chordParams(pts: Point[]): number[] {\n\tconst u = [0];\n\tlet acc = 0;\n\tfor (let i = 1; i < pts.length; i++) {\n\t\tacc += dist(pts[i - 1], pts[i]);\n\t\tu.push(acc);\n\t}\n\treturn acc > 0 ? u.map((x) => x / acc) : u;\n}\n\n/** Solves the 2×2 normal equations for the handle lengths along the fixed tangents. */\nfunction generate(pts: Point[], u: number[], t0: Vec, t1: Vec): CubicSegment {\n\tconst p0 = pts[0];\n\tconst p3 = pts[pts.length - 1];\n\tconst dirIn: Vec = [-t1[0], -t1[1]];\n\tlet c00 = 0;\n\tlet c01 = 0;\n\tlet c11 = 0;\n\tlet x0 = 0;\n\tlet x1 = 0;\n\tfor (let i = 0; i < pts.length; i++) {\n\t\tconst t = u[i];\n\t\tconst s = 1 - t;\n\t\tconst b0 = s * s * s;\n\t\tconst b1 = 3 * s * s * t;\n\t\tconst b2 = 3 * s * t * t;\n\t\tconst b3 = t * t * t;\n\t\tconst a0: Vec = [t0[0] * b1, t0[1] * b1];\n\t\tconst a1: Vec = [dirIn[0] * b2, dirIn[1] * b2];\n\t\tc00 += dot(a0, a0);\n\t\tc01 += dot(a0, a1);\n\t\tc11 += dot(a1, a1);\n\t\tconst tmp: Vec = [\n\t\t\tpts[i][0] - (p0[0] * (b0 + b1) + p3[0] * (b2 + b3)),\n\t\t\tpts[i][1] - (p0[1] * (b0 + b1) + p3[1] * (b2 + b3))\n\t\t];\n\t\tx0 += dot(a0, tmp);\n\t\tx1 += dot(a1, tmp);\n\t}\n\tconst det = c00 * c11 - c01 * c01;\n\tconst seg = dist(p0, p3);\n\tconst eps = 1e-6 * seg;\n\tlet al = seg / 3;\n\tlet ar = seg / 3;\n\tif (Math.abs(det) > 1e-12) {\n\t\tal = (x0 * c11 - x1 * c01) / det;\n\t\tar = (c00 * x1 - c01 * x0) / det;\n\t}\n\tif (!Number.isFinite(al) || !Number.isFinite(ar) || al < eps || ar < eps) {\n\t\tal = seg / 3;\n\t\tar = seg / 3;\n\t}\n\t// Guard against wild handles on nearly degenerate data.\n\tconst cap = seg * 4 + 1e-9;\n\tal = Math.min(al, cap);\n\tar = Math.min(ar, cap);\n\treturn { p0, p1: add(p0, t0, al), p2: add(p3, dirIn, ar), p3 };\n}\n\n/** Largest distance between a point and the curve at its parameter, and that point's index. */\nfunction maxError(pts: Point[], c: CubicSegment, u: number[]): [number, number] {\n\tlet e = 0;\n\tlet idx = pts.length >> 1;\n\tfor (let i = 0; i < pts.length; i++) {\n\t\tconst d = dist(evalCubic(c, u[i]), pts[i]);\n\t\tif (d > e) {\n\t\t\te = d;\n\t\t\tidx = i;\n\t\t}\n\t}\n\treturn [e, idx];\n}\n\n/** One Newton–Raphson step towards the parameter of the point on `c` nearest `p`. */\nfunction newton(c: CubicSegment, p: Point, t: number): number {\n\tconst s = 1 - t;\n\tconst q = evalCubic(c, t);\n\tconst d1: Vec = [\n\t\t3 * (s * s * (c.p1[0] - c.p0[0]) + 2 * s * t * (c.p2[0] - c.p1[0]) + t * t * (c.p3[0] - c.p2[0])),\n\t\t3 * (s * s * (c.p1[1] - c.p0[1]) + 2 * s * t * (c.p2[1] - c.p1[1]) + t * t * (c.p3[1] - c.p2[1]))\n\t];\n\tconst d2: Vec = [\n\t\t6 * (s * (c.p2[0] - 2 * c.p1[0] + c.p0[0]) + t * (c.p3[0] - 2 * c.p2[0] + c.p1[0])),\n\t\t6 * (s * (c.p2[1] - 2 * c.p1[1] + c.p0[1]) + t * (c.p3[1] - 2 * c.p2[1] + c.p1[1]))\n\t];\n\tconst diff: Vec = [q[0] - p[0], q[1] - p[1]];\n\tconst num = dot(diff, d1);\n\tconst den = dot(d1, d1) + dot(diff, d2);\n\tif (Math.abs(den) < 1e-12) return t;\n\tconst nt = t - num / den;\n\treturn Number.isFinite(nt) ? Math.min(1, Math.max(0, nt)) : t;\n}\n\n/**\n * Best single cubic through `pts` with the given unit end tangents (`t1` points forward at\n * the end), starting from parameters `u`. Returns the curve, its max error and the index of\n * the worst point.\n */\nfunction fitSingleFrom(pts: Point[], u: number[], t0: Vec, t1: Vec): [CubicSegment, number, number] {\n\tlet best = generate(pts, u, t0, t1);\n\tlet [bestErr, bestSplit] = maxError(pts, best, u);\n\tconst params = u.slice();\n\tfor (let round = 0; round < MAX_REPARAM; round++) {\n\t\tfor (let i = 0; i < params.length; i++) params[i] = newton(best, pts[i], params[i]);\n\t\tconst c = generate(pts, params, t0, t1);\n\t\tconst [e, s] = maxError(pts, c, params);\n\t\tif (e < bestErr) {\n\t\t\tbest = c;\n\t\t\tbestErr = e;\n\t\t\tbestSplit = s;\n\t\t} else break;\n\t}\n\treturn [best, bestErr, bestSplit];\n}\n\nfunction fitRec(pts: Point[], t0: Vec, t1: Vec, tol: number, out: CubicSegment[], depth: number) {\n\tconst n = pts.length;\n\tif (n === 2) {\n\t\tconst d = dist(pts[0], pts[1]) / 3;\n\t\tout.push({ p0: pts[0], p1: add(pts[0], t0, d), p2: add(pts[1], t1, -d), p3: pts[1] });\n\t\treturn;\n\t}\n\tconst [c, err, splitAt] = fitSingleFrom(pts, chordParams(pts), t0, t1);\n\tif (err <= tol || depth > 40) {\n\t\tout.push(c);\n\t\treturn;\n\t}\n\t// Split at the worst point with a centre tangent estimated from its neighbours.\n\tconst split = Math.min(n - 2, Math.max(1, splitAt));\n\tconst tc = unit(sub(pts[split + 1], pts[split - 1])) ?? t0;\n\tfitRec(pts.slice(0, split + 1), t0, tc, tol, out, depth + 1);\n\tfitRec(pts.slice(split), tc, t1, tol, out, depth + 1);\n}\n\n/**\n * Unit tangent at `a` estimated from the parabola through `a`, `b`, `c` (chord-length\n * parameters): second-order accurate, so polylines sampled from curves get their true end\n * direction rather than the first chord's.\n */\nfunction tangentFrom(a: Point, b: Point, c: Point | undefined): Vec | null {\n\tconst chord = unit(sub(b, a));\n\tif (!c) return chord;\n\tconst d1 = dist(a, b);\n\tconst d2 = dist(b, c);\n\tif (!(d1 > 0) || !(d2 > 0)) return chord;\n\tconst ka = -(2 * d1 + d2) / (d1 * (d1 + d2));\n\tconst kb = (d1 + d2) / (d1 * d2);\n\tconst kc = -d1 / (d2 * (d1 + d2));\n\tconst est = unit([ka * a[0] + kb * b[0] + kc * c[0], ka * a[1] + kb * b[1] + kc * c[1]]);\n\t// Fall back to the chord when the estimate swings too far (noisy or sharp data).\n\treturn est && chord && dot(est, chord) > Math.cos(Math.PI / 4) ? est : chord;\n}\n\n/**\n * Fits a G1-continuous chain of cubics through `points` (an open run) with the unit\n * tangents `t0` (leaving the first point) and `t1` (arriving at the last point, forward);\n * both are estimated from the end points when omitted. Every point lies within `tolerance`\n * of the curve at its fitted parameter.\n */\nexport function fitCubicRun(points: readonly Point[], tolerance: number, t0?: Point, t1?: Point): CubicSegment[] {\n\tconst pts = dedup(points);\n\tconst out: CubicSegment[] = [];\n\tif (pts.length < 2) return out;\n\tconst n = pts.length;\n\tconst start = (t0 && unit([t0[0], t0[1]])) ?? tangentFrom(pts[0], pts[1], pts[2]) ?? [1, 0];\n\tconst back = tangentFrom(pts[n - 1], pts[n - 2], pts[n - 3]);\n\tconst end = (t1 && unit([t1[0], t1[1]])) ?? (back ? ([-back[0], -back[1]] as Vec) : [1, 0]);\n\tfitRec(pts, start, end, Math.max(tolerance, 1e-9), out, 0);\n\treturn out;\n}\n\n/** Turning angle in degrees between the directions `a`→`b` and `b`→`c`. */\nfunction turnDeg(a: Point, b: Point, c: Point): number {\n\tconst u = unit(sub(b, a));\n\tconst v = unit(sub(c, b));\n\tif (!u || !v) return 0;\n\treturn (Math.acos(Math.min(1, Math.max(-1, dot(u, v)))) * 180) / Math.PI;\n}\n\n/**\n * Corner vertices of a polyline. The turn at each vertex is measured to the nearest\n * neighbours at least `span` away on each side (so sample noise smaller than the fitting\n * tolerance does not read as corners), and only the sharpest vertex of a neighbourhood is\n * kept. End points of open polylines are not included.\n */\nfunction findCorners(pts: Point[], closed: boolean, cornerAngle: number, span: number): number[] {\n\tconst n = pts.length;\n\tconst at = (i: number) => pts[closed ? ((i % n) + n) % n : i];\n\tconst reach = (i: number, step: 1 | -1): number | null => {\n\t\tfor (let k = 1; k < (closed ? n : Infinity); k++) {\n\t\t\tconst j = i + step * k;\n\t\t\tif (!closed && (j < 0 || j >= n)) return k > 1 ? j - step : null;\n\t\t\tif (dist(at(j), pts[i]) >= span) return j;\n\t\t}\n\t\treturn null;\n\t};\n\tconst turns = new Float64Array(n);\n\tconst windows: [number, number][] = [];\n\tfor (let i = 0; i < n; i++) {\n\t\twindows.push([i, i]);\n\t\tif (!closed && (i === 0 || i === n - 1)) continue;\n\t\tconst prev = reach(i, -1);\n\t\tconst next = reach(i, 1);\n\t\tif (prev === null || next === null) continue;\n\t\twindows[i] = [prev, next];\n\t\tturns[i] = turnDeg(at(prev), pts[i], at(next));\n\t}\n\tconst corners: number[] = [];\n\tfor (let i = 0; i < n; i++) {\n\t\tif (!(turns[i] > cornerAngle)) continue;\n\t\tconst [lo, hi] = windows[i];\n\t\tlet sharpest = true;\n\t\tfor (let j = lo; j <= hi && sharpest; j++) {\n\t\t\tconst k = closed ? ((j % n) + n) % n : j;\n\t\t\tif (k === i || dist(pts[k], pts[i]) >= span) continue;\n\t\t\tif (turns[k] > turns[i] || (turns[k] === turns[i] && k < i)) sharpest = false;\n\t\t}\n\t\tif (sharpest) corners.push(i);\n\t}\n\treturn corners;\n}\n\n/**\n * Points to fit for a run of polyline vertices: the vertices, plus extra samples along edges\n * much longer than the run's typical edge so straight stretches stay straight.\n */\nfunction densify(run: Point[]): Point[] {\n\tif (run.length < 3) return run;\n\tconst lengths: number[] = [];\n\tfor (let i = 1; i < run.length; i++) lengths.push(dist(run[i - 1], run[i]));\n\tconst sorted = [...lengths].sort((a, b) => a - b);\n\tconst median = sorted[sorted.length >> 1];\n\tif (!(median > 0)) return run;\n\tconst out: Point[] = [run[0]];\n\tfor (let i = 1; i < run.length; i++) {\n\t\tconst len = lengths[i - 1];\n\t\tif (len > 2 * median) {\n\t\t\tconst steps = Math.min(64, Math.ceil(len / median));\n\t\t\tconst a = run[i - 1];\n\t\t\tconst b = run[i];\n\t\t\tfor (let k = 1; k < steps; k++) {\n\t\t\t\tconst t = k / steps;\n\t\t\t\tout.push([a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t]);\n\t\t\t}\n\t\t}\n\t\tout.push(run[i]);\n\t}\n\treturn out;\n}\n\n/**\n * Fits cubic Bézier segments to a polyline (`closed` for a ring), keeping vertices that turn\n * by more than `cornerAngleDeg` (default 30°) as sharp corners. Each stretch between corners\n * is fitted Schneider-style so that every input point is within `tolerance` of the result.\n *\n * Returns the segments in order (consecutive segments share end points; a closed result ends\n * where it starts). Fewer than two distinct points give an empty array.\n */\nexport function fitCubics(points: readonly Point[], tolerance: number, options: FitCubicsOptions = {}): CubicSegment[] {\n\tlet pts = dedup(points);\n\tconst closed = options.closed === true;\n\tif (closed && pts.length > 1 && dist(pts[0], pts[pts.length - 1]) <= 1e-12) pts = pts.slice(0, -1);\n\tif (pts.length < 2) return [];\n\tconst tol = Math.max(tolerance, 1e-6);\n\tconst cornerAngle = options.cornerAngleDeg ?? 30;\n\tconst n = pts.length;\n\tconst span = 4 * tol;\n\n\tif (!closed || n < 3) {\n\t\tconst corners = [0, ...(n > 2 ? findCorners(pts, false, cornerAngle, span) : []), n - 1];\n\t\tconst out: CubicSegment[] = [];\n\t\tfor (let k = 1; k < corners.length; k++) {\n\t\t\tconst run = pts.slice(corners[k - 1], corners[k] + 1);\n\t\t\tout.push(...fitCubicRun(densify(run), tol));\n\t\t}\n\t\tif (closed && n === 2) out.push(...fitCubicRun([pts[1], pts[0]], tol));\n\t\treturn out;\n\t}\n\n\tconst corners = findCorners(pts, true, cornerAngle, span);\n\tif (corners.length === 0) {\n\t\t// Smooth ring: start anywhere, with the same tangent leaving and arriving.\n\t\tconst ring = [...pts, pts[0]];\n\t\tconst t = unit(sub(pts[1], pts[n - 1])) ?? [1, 0];\n\t\t// (A central difference: already second-order accurate at the seam.)\n\t\treturn fitCubicRun(densify(ring), tol, t, t);\n\t}\n\tconst out: CubicSegment[] = [];\n\tfor (let k = 0; k < corners.length; k++) {\n\t\tconst from = corners[k];\n\t\tconst to = corners[(k + 1) % corners.length];\n\t\tconst run: Point[] = [];\n\t\tfor (let i = from; ; i = (i + 1) % n) {\n\t\t\trun.push(pts[i]);\n\t\t\tif (run.length > 1 && i === to) break;\n\t\t}\n\t\tout.push(...fitCubicRun(densify(run), tol));\n\t}\n\treturn out;\n}\n\n/** Whether a cubic is (within `tol`) the straight line from `p0` to `p3`. */\nexport function isStraightCubic(c: CubicSegment, tol: number): boolean {\n\tconst chord = sub(c.p3, c.p0);\n\tconst len = Math.hypot(chord[0], chord[1]);\n\tif (len < 1e-12) return dist(c.p1, c.p0) <= tol && dist(c.p2, c.p0) <= tol;\n\tconst dir: Vec = [chord[0] / len, chord[1] / len];\n\treturn [c.p1, c.p2].every((h) => {\n\t\tconst v = sub(h, c.p0);\n\t\tconst along = dot(v, dir);\n\t\tconst across = Math.abs(v[0] * dir[1] - v[1] * dir[0]);\n\t\treturn across <= tol && along >= -tol && along <= len + tol;\n\t});\n}\n\nexport interface FitPathDataOptions extends FitCubicsOptions {\n\t/** Fitting tolerance in path units (default 0.5). */\n\ttolerance?: number;\n\t/** Decimal places (default 3). */\n\tprecision?: number;\n\t/** Added to every point before formatting. */\n\toffset?: { x: number; y: number };\n\t/**\n\t * Keep the original polyline when fitting would need more segments than it has edges\n\t * (default `true`), so fitting never makes a path heavier.\n\t */\n\tnoWorse?: boolean;\n}\n\n/** SVG `d` for fitted segments (`L` for straight ones, `C` otherwise). */\nexport function cubicsToPathData(\n\tsegments: readonly CubicSegment[],\n\toptions: { precision?: number; offset?: { x: number; y: number }; closed?: boolean; straightTolerance?: number } = {}\n): string {\n\tif (segments.length === 0) return '';\n\tconst f = (n: number) => formatNumber(n, options.precision ?? 3);\n\tconst dx = options.offset?.x ?? 0;\n\tconst dy = options.offset?.y ?? 0;\n\tconst pt = (p: Point) => `${f(p[0] + dx)} ${f(p[1] + dy)}`;\n\tconst straight = options.straightTolerance ?? 1e-3;\n\tlet d = `M${pt(segments[0].p0)}`;\n\tsegments.forEach((c, i) => {\n\t\tconst last = options.closed && i === segments.length - 1;\n\t\tif (isStraightCubic(c, straight)) {\n\t\t\tif (!last) d += `L${pt(c.p3)}`;\n\t\t} else d += `C${pt(c.p1)} ${pt(c.p2)} ${pt(c.p3)}`;\n\t});\n\treturn options.closed ? `${d}Z` : d;\n}\n\nfunction polylineToPathData(points: readonly Point[], closed: boolean, options: FitPathDataOptions): string {\n\tif (points.length < 2) return '';\n\tconst f = (n: number) => formatNumber(n, options.precision ?? 3);\n\tconst dx = options.offset?.x ?? 0;\n\tconst dy = options.offset?.y ?? 0;\n\tlet d = `M${f(points[0][0] + dx)} ${f(points[0][1] + dy)}`;\n\tfor (let i = 1; i < points.length; i++) d += `L${f(points[i][0] + dx)} ${f(points[i][1] + dy)}`;\n\treturn closed ? `${d}Z` : d;\n}\n\n/**\n * Fits smooth curves to a polygon ring (closed by default) and returns SVG path data with\n * `C` curves for smooth stretches, `L` lines for straight ones and sharp corners kept.\n * Use it for boolean results of curved shapes or to smooth freehand strokes\n * (`{ closed: false }`, see `fitFreehandPath`).\n */\nexport function fitPathData(ring: readonly Point[], options: FitPathDataOptions = {}): string {\n\tconst closed = options.closed ?? true;\n\tconst tolerance = options.tolerance ?? 0.5;\n\tconst pts = dedup(ring);\n\tconst segments = fitCubics(pts, tolerance, { ...options, closed });\n\tif (segments.length === 0) return '';\n\tconst edges = closed ? pts.length : pts.length - 1;\n\tif (options.noWorse !== false) {\n\t\tconst straight = segments.filter((c) => isStraightCubic(c, tolerance * 0.02)).length;\n\t\t// Fitting only pays off when it removes vertices.\n\t\tif (segments.length >= edges && straight === segments.length) return polylineToPathData(pts, closed, options);\n\t\tif (segments.length > edges) return polylineToPathData(pts, closed, options);\n\t}\n\treturn cubicsToPathData(segments, {\n\t\tprecision: options.precision,\n\t\toffset: options.offset,\n\t\tclosed,\n\t\tstraightTolerance: tolerance * 0.02\n\t});\n}\n\n/**\n * Smooths a freehand stroke (pointer samples) into an open SVG path of cubic curves.\n * `tolerance` is the largest distance (in the points' units) a sample may be from the\n * curve (default 1); sharp turns above `cornerAngleDeg` (default 30) stay corners.\n */\nexport function fitFreehandPath(\n\tpoints: readonly Point[],\n\toptions: Omit<FitPathDataOptions, 'closed'> = {}\n): string {\n\treturn fitPathData(points, { tolerance: 1, ...options, closed: false });\n}\n","/**\n * Canvas gradient models, parsing, presets, and SVG defs generation.\n * Supports linear and radial gradients for shape fills, strokes, and freehand drawing.\n */\n\nexport interface GradientStop {\n\toffset: number; // 0 to 1\n\tcolor: string;\n\topacity?: number;\n}\n\nexport interface CanvasLinearGradient {\n\ttype: 'linear';\n\tangle?: number; // degrees clockwise, default 90 (top-to-bottom) or custom\n\tstops: GradientStop[];\n}\n\nexport interface CanvasRadialGradient {\n\ttype: 'radial';\n\tcx?: number; // 0 to 1 (default 0.5)\n\tcy?: number; // 0 to 1 (default 0.5)\n\tr?: number; // 0 to 1 (default 0.5)\n\tstops: GradientStop[];\n}\n\nexport type CanvasGradientDef = CanvasLinearGradient | CanvasRadialGradient;\n\nexport interface SvgGradientStop {\n\toffset: string;\n\tstopColor: string;\n\tstopOpacity?: number;\n}\n\nexport interface SvgGradientDef {\n\tid: string;\n\ttype: 'linear' | 'radial';\n\tattrs: Record<string, string | number>;\n\tstops: SvgGradientStop[];\n}\n\nexport const CANVAS_GRADIENT_PRESETS: Record<string, CanvasLinearGradient> = {\n\tsunset: {\n\t\ttype: 'linear',\n\t\tangle: 90,\n\t\tstops: [\n\t\t\t{ offset: 0, color: '#f97316' },\n\t\t\t{ offset: 1, color: '#ec4899' }\n\t\t]\n\t},\n\tocean: {\n\t\ttype: 'linear',\n\t\tangle: 90,\n\t\tstops: [\n\t\t\t{ offset: 0, color: '#06b6d4' },\n\t\t\t{ offset: 1, color: '#3b82f6' }\n\t\t]\n\t},\n\taurora: {\n\t\ttype: 'linear',\n\t\tangle: 90,\n\t\tstops: [\n\t\t\t{ offset: 0, color: '#10b981' },\n\t\t\t{ offset: 1, color: '#6366f1' }\n\t\t]\n\t},\n\tneon: {\n\t\ttype: 'linear',\n\t\tangle: 90,\n\t\tstops: [\n\t\t\t{ offset: 0, color: '#a855f7' },\n\t\t\t{ offset: 1, color: '#ec4899' }\n\t\t]\n\t},\n\tamber: {\n\t\ttype: 'linear',\n\t\tangle: 90,\n\t\tstops: [\n\t\t\t{ offset: 0, color: '#f59e0b' },\n\t\t\t{ offset: 1, color: '#ef4444' }\n\t\t]\n\t},\n\tcosmic: {\n\t\ttype: 'linear',\n\t\tangle: 90,\n\t\tstops: [\n\t\t\t{ offset: 0, color: '#3b82f6' },\n\t\t\t{ offset: 0.5, color: '#8b5cf6' },\n\t\t\t{ offset: 1, color: '#ec4899' }\n\t\t]\n\t}\n};\n\n/** Whether an arbitrary value matches a CanvasGradientDef object. */\nexport function isCanvasGradient(value: unknown): value is CanvasGradientDef {\n\tif (!value || typeof value !== 'object') return false;\n\tconst val = value as Partial<CanvasGradientDef>;\n\tif (val.type !== 'linear' && val.type !== 'radial') return false;\n\tif (!Array.isArray(val.stops)) return false;\n\treturn true;\n}\n\n/**\n * Splits CSS function arguments respecting nested parentheses.\n * e.g. \"rgba(255, 0, 0, 0.5) 0%, #00f 100%\" -> [\"rgba(255, 0, 0, 0.5) 0%\", \"#00f 100%\"]\n */\nfunction splitCssArgs(str: string): string[] {\n\tconst parts: string[] = [];\n\tlet depth = 0;\n\tlet current = '';\n\tfor (let i = 0; i < str.length; i++) {\n\t\tconst char = str[i];\n\t\tif (char === '(') depth++;\n\t\telse if (char === ')') depth = Math.max(0, depth - 1);\n\t\tif (char === ',' && depth === 0) {\n\t\t\tif (current.trim()) parts.push(current.trim());\n\t\t\tcurrent = '';\n\t\t} else {\n\t\t\tcurrent += char;\n\t\t}\n\t}\n\tif (current.trim()) parts.push(current.trim());\n\treturn parts;\n}\n\n/** Parses CSS angle strings like \"90deg\", \"0.25turn\", \"1.57rad\" or named directions like \"to bottom\" */\nfunction parseCssAngle(val: string): number {\n\tconst trimmed = val.trim().toLowerCase();\n\tif (trimmed.endsWith('deg')) {\n\t\treturn parseFloat(trimmed);\n\t}\n\tif (trimmed.endsWith('turn')) {\n\t\treturn parseFloat(trimmed) * 360;\n\t}\n\tif (trimmed.endsWith('rad')) {\n\t\treturn (parseFloat(trimmed) * 180) / Math.PI;\n\t}\n\tif (trimmed.startsWith('to ')) {\n\t\tconst dir = trimmed.slice(3).trim();\n\t\tif (dir === 'bottom') return 180;\n\t\tif (dir === 'top') return 0;\n\t\tif (dir === 'right') return 90;\n\t\tif (dir === 'left') return 270;\n\t\tif (dir === 'bottom right' || dir === 'right bottom') return 135;\n\t\tif (dir === 'bottom left' || dir === 'left bottom') return 225;\n\t\tif (dir === 'top right' || dir === 'right top') return 45;\n\t\tif (dir === 'top left' || dir === 'left top') return 315;\n\t}\n\tconst n = parseFloat(trimmed);\n\treturn Number.isFinite(n) ? n : 180;\n}\n\n/**\n * Parses CSS linear-gradient or radial-gradient strings into CanvasGradientDef.\n * Supports forms like:\n * - linear-gradient(90deg, #ff0000 0%, #0000ff 100%)\n * - linear-gradient(to right, red, blue)\n * - radial-gradient(circle, red 0%, blue 100%)\n */\nexport function parseCssGradient(css: string): CanvasGradientDef | null {\n\tconst trimmed = css.trim();\n\tconst linearMatch = trimmed.match(/^linear-gradient\\s*\\((.*)\\)$/is);\n\tif (linearMatch) {\n\t\tconst rawArgs = splitCssArgs(linearMatch[1]);\n\t\tif (rawArgs.length === 0) return null;\n\t\tlet angle = 180; // default in CSS linear-gradient without direction is to bottom (180deg)\n\t\tlet stopStartIndex = 0;\n\n\t\tconst first = rawArgs[0].trim().toLowerCase();\n\t\tif (first.endsWith('deg') || first.endsWith('turn') || first.endsWith('rad') || first.startsWith('to ')) {\n\t\t\tangle = parseCssAngle(first);\n\t\t\tstopStartIndex = 1;\n\t\t}\n\n\t\tconst stopStrings = rawArgs.slice(stopStartIndex);\n\t\tif (stopStrings.length === 0) return null;\n\t\tconst stops = parseStopStrings(stopStrings);\n\t\tif (stops.length === 0) return null;\n\t\treturn {\n\t\t\ttype: 'linear',\n\t\t\tangle,\n\t\t\tstops\n\t\t};\n\t}\n\n\tconst radialMatch = trimmed.match(/^radial-gradient\\s*\\((.*)\\)$/is);\n\tif (radialMatch) {\n\t\tconst rawArgs = splitCssArgs(radialMatch[1]);\n\t\tif (rawArgs.length === 0) return null;\n\t\tlet stopStartIndex = 0;\n\t\tconst first = rawArgs[0].trim().toLowerCase();\n\t\tif (first.startsWith('circle') || first.startsWith('ellipse') || first.includes('at ')) {\n\t\t\tstopStartIndex = 1;\n\t\t}\n\t\tconst stopStrings = rawArgs.slice(stopStartIndex);\n\t\tif (stopStrings.length === 0) return null;\n\t\tconst stops = parseStopStrings(stopStrings);\n\t\tif (stops.length === 0) return null;\n\t\treturn {\n\t\t\ttype: 'radial',\n\t\t\tcx: 0.5,\n\t\t\tcy: 0.5,\n\t\t\tr: 0.5,\n\t\t\tstops\n\t\t};\n\t}\n\n\treturn null;\n}\n\nfunction parseStopStrings(stopStrings: string[]): GradientStop[] {\n\tconst count = stopStrings.length;\n\treturn stopStrings.map((str, idx) => {\n\t\tconst parts = str.trim().split(/\\s+(?=[0-9%])/);\n\t\tconst color = parts[0];\n\t\tlet offset = count === 1 ? 0 : idx / (count - 1);\n\t\tif (parts.length > 1) {\n\t\t\tconst posStr = parts[1].trim();\n\t\t\tif (posStr.endsWith('%')) {\n\t\t\t\toffset = parseFloat(posStr) / 100;\n\t\t\t} else {\n\t\t\t\tconst val = parseFloat(posStr);\n\t\t\t\tif (Number.isFinite(val)) offset = val;\n\t\t\t}\n\t\t}\n\t\treturn {\n\t\t\toffset: Math.min(1, Math.max(0, offset)),\n\t\t\tcolor\n\t\t};\n\t});\n}\n\n/**\n * Resolves a paint value (which could be a CSS color, preset name, CSS gradient string, or CanvasGradientDef)\n * into either a CanvasGradientDef (if gradient) or null (if solid color or invalid).\n */\nexport function resolveCanvasGradient(value: unknown): CanvasGradientDef | null {\n\tif (!value) return null;\n\tif (isCanvasGradient(value)) return value;\n\tif (typeof value === 'string') {\n\t\tconst lower = value.trim().toLowerCase();\n\t\tif (CANVAS_GRADIENT_PRESETS[lower]) {\n\t\t\treturn CANVAS_GRADIENT_PRESETS[lower];\n\t\t}\n\t\tif (lower.startsWith('linear-gradient') || lower.startsWith('radial-gradient')) {\n\t\t\treturn parseCssGradient(value);\n\t\t}\n\t}\n\treturn null;\n}\n\n/**\n * Converts degrees clockwise (CSS angle: 0 = top, 90 = right, 180 = bottom, 270 = left)\n * to SVG linearGradient vector coordinates (x1, y1, x2, y2) normalized [0, 1].\n */\nexport function angleToSvgLinearCoords(angleDeg: number = 180): { x1: number; y1: number; x2: number; y2: number } {\n\t// Standard CSS angle: 0deg points UP (towards -Y in standard canvas coordinates)\n\t// Convert to standard mathematical angle in radians (0 at +X, counterclockwise)\n\t// CSS 0deg = Math 90deg (up)\n\t// CSS angle A -> Math theta = 90 - A\n\tconst rad = ((90 - angleDeg) * Math.PI) / 180;\n\t// Vector direction\n\tconst dx = Math.cos(rad);\n\tconst dy = -Math.sin(rad); // invert for SVG/screen y-down coords\n\n\t// Project from center (0.5, 0.5) to edges\n\tconst half = 0.5;\n\treturn {\n\t\tx1: Math.round((half - dx * half) * 1000) / 1000,\n\t\ty1: Math.round((half - dy * half) * 1000) / 1000,\n\t\tx2: Math.round((half + dx * half) * 1000) / 1000,\n\t\ty2: Math.round((half + dy * half) * 1000) / 1000\n\t};\n}\n\n/**\n * Converts a CanvasGradientDef to an SvgGradientDef descriptor ready for rendering `<linearGradient>` / `<radialGradient>`.\n */\nexport function gradientToSvgDef(gradient: CanvasGradientDef, id: string): SvgGradientDef {\n\tconst stops: SvgGradientStop[] = (gradient.stops ?? []).map((s) => ({\n\t\toffset: `${Math.round(s.offset * 1000) / 10}%`,\n\t\tstopColor: s.color,\n\t\tstopOpacity: s.opacity\n\t}));\n\n\tif (gradient.type === 'radial') {\n\t\treturn {\n\t\t\tid,\n\t\t\ttype: 'radial',\n\t\t\tattrs: {\n\t\t\t\tcx: gradient.cx !== undefined ? `${gradient.cx * 100}%` : '50%',\n\t\t\t\tcy: gradient.cy !== undefined ? `${gradient.cy * 100}%` : '50%',\n\t\t\t\tr: gradient.r !== undefined ? `${gradient.r * 100}%` : '50%'\n\t\t\t},\n\t\t\tstops\n\t\t};\n\t}\n\n\tconst coords = angleToSvgLinearCoords(gradient.angle ?? 180);\n\treturn {\n\t\tid,\n\t\ttype: 'linear',\n\t\tattrs: {\n\t\t\tx1: `${coords.x1 * 100}%`,\n\t\t\ty1: `${coords.y1 * 100}%`,\n\t\t\tx2: `${coords.x2 * 100}%`,\n\t\t\ty2: `${coords.y2 * 100}%`\n\t\t},\n\t\tstops\n\t};\n}\n","/**\n * Shape geometry: shape descriptions (rect, rounded rect, ellipse, regular polygon, star and\n * SVG path data) to polygons, SVG path parsing / flattening, and polygons back to path data.\n */\nimport type { Point } from './types.js';\nimport type { MultiPolygon, PolygonFillRule, PolygonRing } from './polygonBoolean.js';\nimport { fitPathData } from './curveFit.js';\n\n// ---------------------------------------------------------------------------\n// Shape descriptions\n// ---------------------------------------------------------------------------\n\nimport type { CanvasGradientDef, SvgGradientDef } from './gradient.js';\nimport { gradientToSvgDef, resolveCanvasGradient } from './gradient.js';\n\n/** Paint shared by every shape kind. Colours are any CSS colour, preset name, or gradient definition. */\nexport interface ShapePaint {\n\tfill?: string | CanvasGradientDef;\n\tstroke?: string | CanvasGradientDef;\n\tstrokeWidth?: number;\n\topacity?: number;\n\tstrokeLinecap?: 'round' | 'butt' | 'square';\n\tstrokeLinejoin?: 'round' | 'bevel' | 'miter';\n\tfillGradient?: CanvasGradientDef;\n\tstrokeGradient?: CanvasGradientDef;\n}\n\ninterface ShapeBase extends ShapePaint {\n\t/** Clockwise rotation in degrees (on a y-down canvas) about the centre of the node box. */\n\trotation?: number;\n}\n\n/** Rectangle filling the node box, optionally with rounded corners. */\nexport interface RectShape extends ShapeBase {\n\tkind: 'rect';\n\t/** One radius, or `[topLeft, topRight, bottomRight, bottomLeft]`. Clamped to fit. */\n\tcornerRadius?: number | readonly [number, number, number, number];\n}\n\n/** Ellipse (circle when the node is square) inscribed in the node box. */\nexport interface EllipseShape extends ShapeBase {\n\tkind: 'ellipse';\n}\n\n/** Regular polygon, stretched so its bounding box fills the node box. */\nexport interface RegularPolygonShape extends ShapeBase {\n\tkind: 'polygon';\n\t/** Number of sides (at least 3, default 6). The first vertex points up. */\n\tsides?: number;\n}\n\n/** Star, stretched so its bounding box fills the node box. */\nexport interface StarShape extends ShapeBase {\n\tkind: 'star';\n\t/** Number of points (at least 3, default 5). The first point points up. */\n\tpoints?: number;\n\t/** Inner radius as a fraction of the outer radius (default 0.5). */\n\tinnerRadius?: number;\n}\n\n/** Arbitrary SVG path data. */\nexport interface PathShape extends ShapeBase {\n\tkind: 'path';\n\td: string;\n\t/**\n\t * `[minX, minY, width, height]` of the path's coordinate space, scaled to the node box.\n\t * Without it the path coordinates are node-local pixels.\n\t */\n\tviewBox?: readonly [number, number, number, number];\n\t/** Fill rule (default `nonzero`, as in SVG). */\n\tfillRule?: PolygonFillRule;\n}\n\n/**\n * Shape description stored on a node as `data.shape`. Geometry is relative to the node\n * box (`0,0` to `width,height`), so resizing the node resizes the shape.\n */\nexport type ShapeDescription = RectShape | EllipseShape | RegularPolygonShape | StarShape | PathShape;\n\nexport type ShapeKind = ShapeDescription['kind'];\n\nexport const SHAPE_KINDS: readonly ShapeKind[] = ['rect', 'ellipse', 'polygon', 'star', 'path'];\n\n/** Whether a value is a supported shape description. */\nexport function isShapeDescription(value: unknown): value is ShapeDescription {\n\tif (!value || typeof value !== 'object') return false;\n\tconst kind = (value as { kind?: unknown }).kind;\n\tif (!SHAPE_KINDS.includes(kind as ShapeKind)) return false;\n\treturn kind !== 'path' || typeof (value as PathShape).d === 'string';\n}\n\nexport interface ShapeSize {\n\twidth: number;\n\theight: number;\n}\n\nexport interface FlattenOptions {\n\t/** Maximum distance between a curve and its polyline, in output units (default 0.25). */\n\ttolerance?: number;\n\t/** Upper bound on the segments one curve is split into (default 512). */\n\tmaxSegments?: number;\n}\n\n// ---------------------------------------------------------------------------\n// SVG path parsing\n// ---------------------------------------------------------------------------\n\n/** Normalised absolute path command produced by `parseSvgPath`. */\nexport type PathCommand =\n\t| { type: 'M'; x: number; y: number }\n\t| { type: 'L'; x: number; y: number }\n\t| { type: 'C'; x1: number; y1: number; x2: number; y2: number; x: number; y: number }\n\t| { type: 'Q'; x1: number; y1: number; x: number; y: number }\n\t| {\n\t\t\ttype: 'A';\n\t\t\trx: number;\n\t\t\try: number;\n\t\t\trotation: number;\n\t\t\tlargeArc: boolean;\n\t\t\tsweep: boolean;\n\t\t\tx: number;\n\t\t\ty: number;\n\t }\n\t| { type: 'Z' };\n\nconst PARAM_COUNTS: Record<string, number> = { m: 2, l: 2, h: 1, v: 1, c: 6, s: 4, q: 4, t: 2, a: 7, z: 0 };\n\n/** Character scanner for path data (handles `1.5.5`, `1e-3`, `-1-2` and compact arc flags). */\nclass PathScanner {\n\tprivate pos = 0;\n\tconstructor(private readonly text: string) {}\n\n\tprivate skip() {\n\t\twhile (this.pos < this.text.length && /[\\s,]/.test(this.text[this.pos])) this.pos++;\n\t}\n\n\t/** The next command letter, or `null` when a number (or the end) follows. */\n\tcommand(): string | null {\n\t\tthis.skip();\n\t\tconst ch = this.text[this.pos];\n\t\tif (ch && /[MmLlHhVvCcSsQqTtAaZz]/.test(ch)) {\n\t\t\tthis.pos++;\n\t\t\treturn ch;\n\t\t}\n\t\treturn null;\n\t}\n\n\tdone(): boolean {\n\t\tthis.skip();\n\t\treturn this.pos >= this.text.length;\n\t}\n\n\tnumber(): number | null {\n\t\tthis.skip();\n\t\tconst match = /^[+-]?(?:\\d+\\.?\\d*|\\.\\d+)(?:[eE][+-]?\\d+)?/.exec(this.text.slice(this.pos, this.pos + 64));\n\t\tif (!match) return null;\n\t\tthis.pos += match[0].length;\n\t\tconst value = Number(match[0]);\n\t\treturn Number.isFinite(value) ? value : null;\n\t}\n\n\tflag(): number | null {\n\t\tthis.skip();\n\t\tconst ch = this.text[this.pos];\n\t\tif (ch === '0' || ch === '1') {\n\t\t\tthis.pos++;\n\t\t\treturn ch === '1' ? 1 : 0;\n\t\t}\n\t\treturn null;\n\t}\n}\n\n/**\n * Parses SVG path data into absolute `M`, `L`, `C`, `Q`, `A` and `Z` commands. Relative\n * commands, `H` / `V`, smooth curves (`S` / `T`) and implicit repeated commands are\n * normalised. Parsing stops at the first malformed command (as browsers do).\n */\nexport function parseSvgPath(d: string): PathCommand[] {\n\tconst scanner = new PathScanner(d ?? '');\n\tconst commands: PathCommand[] = [];\n\tlet cx = 0;\n\tlet cy = 0;\n\tlet startX = 0;\n\tlet startY = 0;\n\tlet lastControl: { x: number; y: number; type: 'C' | 'Q' } | null = null;\n\tlet command = '';\n\n\twhile (!scanner.done()) {\n\t\tconst letter = scanner.command();\n\t\tif (letter) {\n\t\t\tcommand = letter;\n\t\t\tif (command === 'z' || command === 'Z') {\n\t\t\t\tcommands.push({ type: 'Z' });\n\t\t\t\tcx = startX;\n\t\t\t\tcy = startY;\n\t\t\t\tlastControl = null;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t} else if (!command || command === 'z' || command === 'Z') {\n\t\t\tbreak;\n\t\t}\n\t\tconst lower = command.toLowerCase();\n\t\tconst count = PARAM_COUNTS[lower];\n\t\tconst args: number[] = [];\n\t\tfor (let k = 0; k < count; k++) {\n\t\t\tconst value = lower === 'a' && (k === 3 || k === 4) ? scanner.flag() : scanner.number();\n\t\t\tif (value === null) return commands;\n\t\t\targs.push(value);\n\t\t}\n\t\tconst rel = command !== command.toUpperCase();\n\t\tconst ox = rel ? cx : 0;\n\t\tconst oy = rel ? cy : 0;\n\t\tswitch (lower) {\n\t\t\tcase 'm': {\n\t\t\t\tcx = args[0] + ox;\n\t\t\t\tcy = args[1] + oy;\n\t\t\t\tstartX = cx;\n\t\t\t\tstartY = cy;\n\t\t\t\tcommands.push({ type: 'M', x: cx, y: cy });\n\t\t\t\tlastControl = null;\n\t\t\t\t// Extra coordinate pairs after a moveto are implicit linetos.\n\t\t\t\tcommand = rel ? 'l' : 'L';\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase 'l':\n\t\t\t\tcx = args[0] + ox;\n\t\t\t\tcy = args[1] + oy;\n\t\t\t\tcommands.push({ type: 'L', x: cx, y: cy });\n\t\t\t\tlastControl = null;\n\t\t\t\tbreak;\n\t\t\tcase 'h':\n\t\t\t\tcx = args[0] + (rel ? cx : 0);\n\t\t\t\tcommands.push({ type: 'L', x: cx, y: cy });\n\t\t\t\tlastControl = null;\n\t\t\t\tbreak;\n\t\t\tcase 'v':\n\t\t\t\tcy = args[0] + (rel ? cy : 0);\n\t\t\t\tcommands.push({ type: 'L', x: cx, y: cy });\n\t\t\t\tlastControl = null;\n\t\t\t\tbreak;\n\t\t\tcase 'c': {\n\t\t\t\tconst c: PathCommand = {\n\t\t\t\t\ttype: 'C',\n\t\t\t\t\tx1: args[0] + ox,\n\t\t\t\t\ty1: args[1] + oy,\n\t\t\t\t\tx2: args[2] + ox,\n\t\t\t\t\ty2: args[3] + oy,\n\t\t\t\t\tx: args[4] + ox,\n\t\t\t\t\ty: args[5] + oy\n\t\t\t\t};\n\t\t\t\tcommands.push(c);\n\t\t\t\tlastControl = { x: c.x2, y: c.y2, type: 'C' };\n\t\t\t\tcx = c.x;\n\t\t\t\tcy = c.y;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase 's': {\n\t\t\t\tconst x1: number = lastControl?.type === 'C' ? 2 * cx - lastControl.x : cx;\n\t\t\t\tconst y1: number = lastControl?.type === 'C' ? 2 * cy - lastControl.y : cy;\n\t\t\t\tconst c: PathCommand = {\n\t\t\t\t\ttype: 'C',\n\t\t\t\t\tx1,\n\t\t\t\t\ty1,\n\t\t\t\t\tx2: args[0] + ox,\n\t\t\t\t\ty2: args[1] + oy,\n\t\t\t\t\tx: args[2] + ox,\n\t\t\t\t\ty: args[3] + oy\n\t\t\t\t};\n\t\t\t\tcommands.push(c);\n\t\t\t\tlastControl = { x: c.x2, y: c.y2, type: 'C' };\n\t\t\t\tcx = c.x;\n\t\t\t\tcy = c.y;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase 'q': {\n\t\t\t\tconst c: PathCommand = { type: 'Q', x1: args[0] + ox, y1: args[1] + oy, x: args[2] + ox, y: args[3] + oy };\n\t\t\t\tcommands.push(c);\n\t\t\t\tlastControl = { x: c.x1, y: c.y1, type: 'Q' };\n\t\t\t\tcx = c.x;\n\t\t\t\tcy = c.y;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase 't': {\n\t\t\t\tconst x1: number = lastControl?.type === 'Q' ? 2 * cx - lastControl.x : cx;\n\t\t\t\tconst y1: number = lastControl?.type === 'Q' ? 2 * cy - lastControl.y : cy;\n\t\t\t\tconst c: PathCommand = { type: 'Q', x1, y1, x: args[0] + ox, y: args[1] + oy };\n\t\t\t\tcommands.push(c);\n\t\t\t\tlastControl = { x: x1, y: y1, type: 'Q' };\n\t\t\t\tcx = c.x;\n\t\t\t\tcy = c.y;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase 'a': {\n\t\t\t\tconst c: PathCommand = {\n\t\t\t\t\ttype: 'A',\n\t\t\t\t\trx: Math.abs(args[0]),\n\t\t\t\t\try: Math.abs(args[1]),\n\t\t\t\t\trotation: args[2],\n\t\t\t\t\tlargeArc: args[3] !== 0,\n\t\t\t\t\tsweep: args[4] !== 0,\n\t\t\t\t\tx: args[5] + ox,\n\t\t\t\t\ty: args[6] + oy\n\t\t\t\t};\n\t\t\t\tcommands.push(c);\n\t\t\t\tlastControl = null;\n\t\t\t\tcx = c.x;\n\t\t\t\tcy = c.y;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\treturn commands;\n}\n\n// ---------------------------------------------------------------------------\n// Curve flattening\n// ---------------------------------------------------------------------------\n\nfunction segmentCount(curvature: number, tolerance: number, max: number): number {\n\tif (!(curvature > 0)) return 1;\n\treturn Math.max(1, Math.min(max, Math.ceil(Math.sqrt(curvature / tolerance))));\n}\n\nfunction flattenCubic(\n\tout: Point[],\n\tp0: Point,\n\tx1: number,\n\ty1: number,\n\tx2: number,\n\ty2: number,\n\tx: number,\n\ty: number,\n\ttolerance: number,\n\tmax: number\n) {\n\t// Uniform subdivision error <= max|B''| / (8 n²) with |B''| <= 6 * max second difference.\n\tconst dd = Math.max(\n\t\tMath.hypot(p0[0] - 2 * x1 + x2, p0[1] - 2 * y1 + y2),\n\t\tMath.hypot(x1 - 2 * x2 + x, y1 - 2 * y2 + y)\n\t);\n\tconst n = segmentCount((6 * dd) / 8, tolerance, max);\n\tfor (let k = 1; k <= n; k++) {\n\t\tconst t = k / n;\n\t\tconst mt = 1 - t;\n\t\tconst a = mt * mt * mt;\n\t\tconst b = 3 * mt * mt * t;\n\t\tconst c = 3 * mt * t * t;\n\t\tconst e = t * t * t;\n\t\tout.push([a * p0[0] + b * x1 + c * x2 + e * x, a * p0[1] + b * y1 + c * y2 + e * y]);\n\t}\n}\n\nfunction flattenQuadratic(\n\tout: Point[],\n\tp0: Point,\n\tx1: number,\n\ty1: number,\n\tx: number,\n\ty: number,\n\ttolerance: number,\n\tmax: number\n) {\n\tconst dd = Math.hypot(p0[0] - 2 * x1 + x, p0[1] - 2 * y1 + y);\n\tconst n = segmentCount((2 * dd) / 8, tolerance, max);\n\tfor (let k = 1; k <= n; k++) {\n\t\tconst t = k / n;\n\t\tconst mt = 1 - t;\n\t\tout.push([mt * mt * p0[0] + 2 * mt * t * x1 + t * t * x, mt * mt * p0[1] + 2 * mt * t * y1 + t * t * y]);\n\t}\n}\n\nfunction arcSteps(radius: number, sweep: number, tolerance: number, max: number): number {\n\tif (radius <= tolerance) return Math.max(1, Math.ceil(Math.abs(sweep) / (Math.PI / 2)));\n\tconst step = 2 * Math.acos(1 - tolerance / radius);\n\treturn Math.max(1, Math.min(max, Math.ceil(Math.abs(sweep) / step)));\n}\n\n/** Flattens an SVG elliptical arc (endpoint parameterisation, SVG 1.1 F.6.5). */\nfunction flattenArc(\n\tout: Point[],\n\tp0: Point,\n\tcmd: Extract<PathCommand, { type: 'A' }>,\n\ttolerance: number,\n\tmax: number\n) {\n\tconst [x0, y0] = p0;\n\tlet { rx, ry } = cmd;\n\tconst { x, y } = cmd;\n\tif (x0 === x && y0 === y) return;\n\tif (rx === 0 || ry === 0) {\n\t\tout.push([x, y]);\n\t\treturn;\n\t}\n\tconst phi = (cmd.rotation * Math.PI) / 180;\n\tconst cos = Math.cos(phi);\n\tconst sin = Math.sin(phi);\n\tconst dx = (x0 - x) / 2;\n\tconst dy = (y0 - y) / 2;\n\tconst x1p = cos * dx + sin * dy;\n\tconst y1p = -sin * dx + cos * dy;\n\tconst lambda = (x1p * x1p) / (rx * rx) + (y1p * y1p) / (ry * ry);\n\tif (lambda > 1) {\n\t\tconst s = Math.sqrt(lambda);\n\t\trx *= s;\n\t\try *= s;\n\t}\n\tconst num = rx * rx * ry * ry - rx * rx * y1p * y1p - ry * ry * x1p * x1p;\n\tconst den = rx * rx * y1p * y1p + ry * ry * x1p * x1p;\n\tlet coef = den === 0 ? 0 : Math.sqrt(Math.max(0, num / den));\n\tif (cmd.largeArc === cmd.sweep) coef = -coef;\n\tconst cxp = (coef * rx * y1p) / ry;\n\tconst cyp = (-coef * ry * x1p) / rx;\n\tconst cx = cos * cxp - sin * cyp + (x0 + x) / 2;\n\tconst cy = sin * cxp + cos * cyp + (y0 + y) / 2;\n\tconst angle = (ux: number, uy: number, vx: number, vy: number) => {\n\t\tconst a = Math.atan2(ux * vy - uy * vx, ux * vx + uy * vy);\n\t\treturn a;\n\t};\n\tconst theta1 = angle(1, 0, (x1p - cxp) / rx, (y1p - cyp) / ry);\n\tlet delta = angle((x1p - cxp) / rx, (y1p - cyp) / ry, (-x1p - cxp) / rx, (-y1p - cyp) / ry);\n\tif (!cmd.sweep && delta > 0) delta -= 2 * Math.PI;\n\telse if (cmd.sweep && delta < 0) delta += 2 * Math.PI;\n\tconst n = arcSteps(Math.max(rx, ry), delta, tolerance, max);\n\tfor (let k = 1; k < n; k++) {\n\t\tconst t = theta1 + (delta * k) / n;\n\t\tconst ex = rx * Math.cos(t);\n\t\tconst ey = ry * Math.sin(t);\n\t\tout.push([cos * ex - sin * ey + cx, sin * ex + cos * ey + cy]);\n\t}\n\tout.push([x, y]);\n}\n\n/**\n * Flattens SVG path data into closed rings (one per subpath; open subpaths are closed the\n * way SVG fills them). Curves are approximated within `tolerance`.\n */\nexport function flattenSvgPath(d: string | PathCommand[], options: FlattenOptions = {}): PolygonRing[] {\n\tconst tolerance = options.tolerance && options.tolerance > 0 ? options.tolerance : 0.25;\n\tconst max = options.maxSegments ?? 512;\n\tconst commands = typeof d === 'string' ? parseSvgPath(d) : d;\n\tconst rings: PolygonRing[] = [];\n\tlet current: Point[] = [];\n\tlet start: Point = [0, 0];\n\tconst flush = () => {\n\t\tif (current.length >= 3) rings.push(current);\n\t\tcurrent = [];\n\t};\n\tconst last = (): Point => current[current.length - 1] ?? start;\n\tfor (const cmd of commands) {\n\t\tswitch (cmd.type) {\n\t\t\tcase 'M':\n\t\t\t\tflush();\n\t\t\t\tstart = [cmd.x, cmd.y];\n\t\t\t\tcurrent = [start];\n\t\t\t\tbreak;\n\t\t\tcase 'L':\n\t\t\t\tif (current.length === 0) current = [start];\n\t\t\t\tcurrent.push([cmd.x, cmd.y]);\n\t\t\t\tbreak;\n\t\t\tcase 'C':\n\t\t\t\tif (current.length === 0) current = [start];\n\t\t\t\tflattenCubic(current, last(), cmd.x1, cmd.y1, cmd.x2, cmd.y2, cmd.x, cmd.y, tolerance, max);\n\t\t\t\tbreak;\n\t\t\tcase 'Q':\n\t\t\t\tif (current.length === 0) current = [start];\n\t\t\t\tflattenQuadratic(current, last(), cmd.x1, cmd.y1, cmd.x, cmd.y, tolerance, max);\n\t\t\t\tbreak;\n\t\t\tcase 'A':\n\t\t\t\tif (current.length === 0) current = [start];\n\t\t\t\tflattenArc(current, last(), cmd, tolerance, max);\n\t\t\t\tbreak;\n\t\t\tcase 'Z':\n\t\t\t\tflush();\n\t\t\t\t// A command after Z without a moveto starts at the subpath start.\n\t\t\t\tbreak;\n\t\t}\n\t}\n\tflush();\n\treturn rings.map(dedupeRing).filter((r) => r.length >= 3);\n}\n\nfunction dedupeRing(ring: Point[]): Point[] {\n\tconst out: Point[] = [];\n\tfor (const p of ring) {\n\t\tconst prev = out[out.length - 1];\n\t\tif (!prev || prev[0] !== p[0] || prev[1] !== p[1]) out.push(p);\n\t}\n\twhile (out.length > 1 && out[0][0] === out[out.length - 1][0] && out[0][1] === out[out.length - 1][1]) out.pop();\n\treturn out;\n}\n\n// ---------------------------------------------------------------------------\n// Shapes to polygons\n// ---------------------------------------------------------------------------\n\nfunction cornerRadii(shape: RectShape, width: number, height: number): [number, number, number, number] {\n\tconst raw = shape.cornerRadius ?? 0;\n\tlet radii = (typeof raw === 'number' ? [raw, raw, raw, raw] : [...raw]).map((r) =>\n\t\tNumber.isFinite(r) && r > 0 ? r : 0\n\t) as [number, number, number, number];\n\t// Scale all radii down together when adjacent corners would overlap (CSS rule).\n\tconst factor = Math.min(\n\t\t1,\n\t\twidth / Math.max(radii[0] + radii[1], 1e-12),\n\t\theight / Math.max(radii[1] + radii[2], 1e-12),\n\t\twidth / Math.max(radii[2] + radii[3], 1e-12),\n\t\theight / Math.max(radii[3] + radii[0], 1e-12)\n\t);\n\tif (factor < 1) radii = radii.map((r) => r * factor) as [number, number, number, number];\n\treturn radii;\n}\n\n/** Unit vertices of a regular polygon / star, stretched to fill the box. */\nfunction polygonVertices(count: number, inner: number | null, width: number, height: number): Point[] {\n\tconst raw: Point[] = [];\n\tconst total = inner === null ? count : count * 2;\n\tfor (let k = 0; k < total; k++) {\n\t\tconst angle = -Math.PI / 2 + (k * 2 * Math.PI) / total;\n\t\tconst r = inner !== null && k % 2 === 1 ? inner : 1;\n\t\traw.push([r * Math.cos(angle), r * Math.sin(angle)]);\n\t}\n\tlet minX = Infinity;\n\tlet minY = Infinity;\n\tlet maxX = -Infinity;\n\tlet maxY = -Infinity;\n\tfor (const [x, y] of raw) {\n\t\tminX = Math.min(minX, x);\n\t\tminY = Math.min(minY, y);\n\t\tmaxX = Math.max(maxX, x);\n\t\tmaxY = Math.max(maxY, y);\n\t}\n\tconst sx = width / (maxX - minX || 1);\n\tconst sy = height / (maxY - minY || 1);\n\treturn raw.map(([x, y]) => [(x - minX) * sx, (y - minY) * sy]);\n}\n\nfunction rotationTransform(shape: ShapeDescription, size: ShapeSize): ((p: Point) => Point) | null {\n\tconst deg = shape.rotation ?? 0;\n\tif (!deg || !Number.isFinite(deg)) return null;\n\tconst rad = (deg * Math.PI) / 180;\n\tconst cos = Math.cos(rad);\n\tconst sin = Math.sin(rad);\n\tconst cx = size.width / 2;\n\tconst cy = size.height / 2;\n\treturn ([x, y]) => [cx + (x - cx) * cos - (y - cy) * sin, cy + (x - cx) * sin + (y - cy) * cos];\n}\n\n/**\n * SVG path data for a shape in node-local pixels (exact curves: arcs for rounded corners\n * and ellipses), plus the fill rule and a `transform` for rotation / path view boxes.\n * Use it to render a shape; use `shapeToPolygon` for geometry.\n */\nexport function getShapeSvg(\n\tshape: ShapeDescription,\n\tsize: ShapeSize\n): { d: string; transform?: string; fillRule: PolygonFillRule } {\n\tconst { width: w, height: h } = size;\n\tconst f = (n: number) => formatNumber(n, 3);\n\tconst transforms: string[] = [];\n\tif (shape.rotation) transforms.push(`rotate(${f(shape.rotation)} ${f(w / 2)} ${f(h / 2)})`);\n\tlet d = '';\n\tlet fillRule: PolygonFillRule = 'nonzero';\n\tswitch (shape.kind) {\n\t\tcase 'rect': {\n\t\t\tconst [tl, tr, br, bl] = cornerRadii(shape, w, h);\n\t\t\tif (tl + tr + br + bl === 0) {\n\t\t\t\td = `M0 0H${f(w)}V${f(h)}H0Z`;\n\t\t\t} else {\n\t\t\t\tconst arc = (r: number, x: number, y: number) => (r > 0 ? `A${f(r)} ${f(r)} 0 0 1 ${f(x)} ${f(y)}` : '');\n\t\t\t\td =\n\t\t\t\t\t`M${f(tl)} 0H${f(w - tr)}${arc(tr, w, tr)}V${f(h - br)}${arc(br, w - br, h)}` +\n\t\t\t\t\t`H${f(bl)}${arc(bl, 0, h - bl)}V${f(tl)}${arc(tl, tl, 0)}Z`;\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tcase 'ellipse': {\n\t\t\tconst rx = w / 2;\n\t\t\tconst ry = h / 2;\n\t\t\td = `M0 ${f(ry)}A${f(rx)} ${f(ry)} 0 1 1 ${f(w)} ${f(ry)}A${f(rx)} ${f(ry)} 0 1 1 0 ${f(ry)}Z`;\n\t\t\tbreak;\n\t\t}\n\t\tcase 'polygon':\n\t\tcase 'star': {\n\t\t\tconst points =\n\t\t\t\tshape.kind === 'polygon'\n\t\t\t\t\t? polygonVertices(Math.max(3, Math.floor(shape.sides ?? 6)), null, w, h)\n\t\t\t\t\t: polygonVertices(Math.max(3, Math.floor(shape.points ?? 5)), clampRatio(shape.innerRadius), w, h);\n\t\t\td = ringsToPath([points], f);\n\t\t\tbreak;\n\t\t}\n\t\tcase 'path': {\n\t\t\td = shape.d;\n\t\t\tfillRule = shape.fillRule ?? 'nonzero';\n\t\t\tconst vb = validViewBox(shape.viewBox);\n\t\t\tif (vb) {\n\t\t\t\ttransforms.push(\n\t\t\t\t\t`scale(${f(w / vb[2])} ${f(h / vb[3])})`,\n\t\t\t\t\t`translate(${f(-vb[0])} ${f(-vb[1])})`\n\t\t\t\t);\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn transforms.length > 0 ? { d, transform: transforms.join(' '), fillRule } : { d, fillRule };\n}\n\n/** Everything a renderer needs to draw a shape as one SVG `<path>`. */\nexport interface ShapeRenderData {\n\t/** SVG `viewBox` (`0 0 width height`): node-local pixels. */\n\tviewBox: string;\n\twidth: number;\n\theight: number;\n\td: string;\n\ttransform?: string;\n\tfillRule: PolygonFillRule;\n\tfill: string;\n\tstroke: string;\n\tstrokeWidth: number;\n\topacity?: number;\n\tstrokeLinecap?: 'round' | 'butt' | 'square';\n\tstrokeLinejoin?: 'round' | 'bevel' | 'miter';\n\t/** Gradient def for the fill, when fill uses a gradient. */\n\tfillGradient?: SvgGradientDef;\n\t/** Gradient def for the stroke, when stroke uses a gradient. */\n\tstrokeGradient?: SvgGradientDef;\n}\n\nexport interface GetShapeRenderDataOptions {\n\t/** Unique ID suffix or base ID used when generating SVG gradient IDs (default: auto random). */\n\tid?: string;\n}\n\n/**\n * Resolves the SVG drawing of a shape for a node size. Without a size, a path's view box\n * size (or 100x100) is used. Paint defaults: `fill: currentColor`, `stroke: none`,\n * `strokeWidth: 1`. Supports solid colors, CSS gradients, presets, and CanvasGradientDef.\n */\nexport function getShapeRenderData(\n\tshape: ShapeDescription,\n\tsize: Partial<ShapeSize> = {},\n\toptions: GetShapeRenderDataOptions = {}\n): ShapeRenderData {\n\tconst vb = shape.kind === 'path' ? validViewBox(shape.viewBox) : null;\n\tconst width = size.width && size.width > 0 ? size.width : vb ? vb[2] : 100;\n\tconst height = size.height && size.height > 0 ? size.height : vb ? vb[3] : 100;\n\tconst svg = getShapeSvg(shape, { width, height });\n\n\tconst baseId = options.id || Math.random().toString(36).slice(2, 9);\n\n\t// Resolve fill (gradient or solid)\n\tlet fillStr = 'currentColor';\n\tlet fillGradientDef: SvgGradientDef | undefined;\n\tconst rawFill = shape.fillGradient ?? shape.fill;\n\tconst resolvedFillGradient = resolveCanvasGradient(rawFill);\n\tif (resolvedFillGradient) {\n\t\tconst gradId = `shape-fill-${baseId}`;\n\t\tfillGradientDef = gradientToSvgDef(resolvedFillGradient, gradId);\n\t\tfillStr = `url(#${gradId})`;\n\t} else if (typeof shape.fill === 'string') {\n\t\tfillStr = shape.fill;\n\t} else if (shape.fill === undefined) {\n\t\tfillStr = 'currentColor';\n\t}\n\n\t// Resolve stroke (gradient or solid)\n\tlet strokeStr = 'none';\n\tlet strokeGradientDef: SvgGradientDef | undefined;\n\tconst rawStroke = shape.strokeGradient ?? shape.stroke;\n\tconst resolvedStrokeGradient = resolveCanvasGradient(rawStroke);\n\tif (resolvedStrokeGradient) {\n\t\tconst gradId = `shape-stroke-${baseId}`;\n\t\tstrokeGradientDef = gradientToSvgDef(resolvedStrokeGradient, gradId);\n\t\tstrokeStr = `url(#${gradId})`;\n\t} else if (typeof shape.stroke === 'string') {\n\t\tstrokeStr = shape.stroke;\n\t} else if (shape.stroke === undefined) {\n\t\tstrokeStr = 'none';\n\t}\n\n\tconst data: ShapeRenderData = {\n\t\tviewBox: `0 0 ${formatNumber(width, 3)} ${formatNumber(height, 3)}`,\n\t\twidth,\n\t\theight,\n\t\td: svg.d,\n\t\tfillRule: svg.fillRule,\n\t\tfill: fillStr,\n\t\tstroke: strokeStr,\n\t\tstrokeWidth: shape.strokeWidth ?? 1\n\t};\n\tif (svg.transform) data.transform = svg.transform;\n\tif (shape.opacity !== undefined) data.opacity = shape.opacity;\n\tif (shape.strokeLinecap) data.strokeLinecap = shape.strokeLinecap;\n\tif (shape.strokeLinejoin) data.strokeLinejoin = shape.strokeLinejoin;\n\tif (fillGradientDef) data.fillGradient = fillGradientDef;\n\tif (strokeGradientDef) data.strokeGradient = strokeGradientDef;\n\treturn data;\n}\n\nfunction clampRatio(value: number | undefined): number {\n\tif (value === undefined || !Number.isFinite(value)) return 0.5;\n\treturn Math.min(1, Math.max(0.01, value));\n}\n\nfunction validViewBox(vb: PathShape['viewBox']): readonly [number, number, number, number] | null {\n\tif (!vb || vb.length !== 4 || !vb.every(Number.isFinite) || vb[2] <= 0 || vb[3] <= 0) return null;\n\treturn vb;\n}\n\n/**\n * Converts a shape to rings in node-local pixels (rotation applied), flattening curves at\n * `tolerance`. Returns the rings with the fill rule they must be read with.\n */\nexport function shapeToPolygon(\n\tshape: ShapeDescription,\n\tsize: ShapeSize,\n\toptions: FlattenOptions = {}\n): { rings: PolygonRing[]; fillRule: PolygonFillRule } {\n\tconst { width: w, height: h } = size;\n\tif (!(w > 0) || !(h > 0)) return { rings: [], fillRule: 'nonzero' };\n\tlet rings: PolygonRing[];\n\tlet fillRule: PolygonFillRule = 'nonzero';\n\tif (shape.kind === 'path') {\n\t\tfillRule = shape.fillRule ?? 'nonzero';\n\t\tconst vb = validViewBox(shape.viewBox);\n\t\tif (vb) {\n\t\t\tconst sx = w / vb[2];\n\t\t\tconst sy = h / vb[3];\n\t\t\t// Flatten in path units with the tolerance scaled to stay within `tolerance` px.\n\t\t\tconst tolerance = (options.tolerance ?? 0.25) / Math.max(sx, sy);\n\t\t\trings = flattenSvgPath(shape.d, { ...options, tolerance }).map((ring) =>\n\t\t\t\tring.map(([x, y]) => [(x - vb[0]) * sx, (y - vb[1]) * sy] as Point)\n\t\t\t);\n\t\t} else {\n\t\t\trings = flattenSvgPath(shape.d, options);\n\t\t}\n\t} else {\n\t\tconst { d } = getShapeSvg({ ...shape, rotation: 0 }, size);\n\t\trings = flattenSvgPath(d, options);\n\t}\n\tconst rotate = rotationTransform(shape, size);\n\tif (rotate) rings = rings.map((ring) => ring.map(rotate));\n\treturn { rings, fillRule };\n}\n\n// ---------------------------------------------------------------------------\n// Polygons to path data\n// ---------------------------------------------------------------------------\n\n/** Formats a number with at most `precision` decimals, without trailing zeros. */\nexport function formatNumber(value: number, precision = 3): string {\n\tconst factor = 10 ** precision;\n\tconst rounded = Math.round(value * factor) / factor;\n\treturn Object.is(rounded, -0) ? '0' : String(rounded);\n}\n\nfunction ringsToPath(rings: readonly (readonly Point[])[], f: (n: number) => string): string {\n\tlet d = '';\n\tfor (const ring of rings) {\n\t\tif (ring.length < 2) continue;\n\t\td += `M${f(ring[0][0])} ${f(ring[0][1])}`;\n\t\tfor (let i = 1; i < ring.length; i++) d += `L${f(ring[i][0])} ${f(ring[i][1])}`;\n\t\td += 'Z';\n\t}\n\treturn d;\n}\n\n/** Perpendicular distance from `p` to the segment `a`–`b`. */\nfunction segmentDistance(p: Point, a: Point, b: Point): number {\n\tconst dx = b[0] - a[0];\n\tconst dy = b[1] - a[1];\n\tconst len2 = dx * dx + dy * dy;\n\tif (len2 === 0) return Math.hypot(p[0] - a[0], p[1] - a[1]);\n\tconst t = Math.max(0, Math.min(1, ((p[0] - a[0]) * dx + (p[1] - a[1]) * dy) / len2));\n\treturn Math.hypot(p[0] - (a[0] + t * dx), p[1] - (a[1] + t * dy));\n}\n\nfunction rdp(points: Point[], tolerance: number): Point[] {\n\tif (points.length <= 2) return points;\n\tconst keep = new Uint8Array(points.length);\n\tkeep[0] = 1;\n\tkeep[points.length - 1] = 1;\n\tconst stack: [number, number][] = [[0, points.length - 1]];\n\twhile (stack.length) {\n\t\tconst [s, e] = stack.pop()!;\n\t\tlet maxDist = -1;\n\t\tlet index = -1;\n\t\tfor (let i = s + 1; i < e; i++) {\n\t\t\tconst dist = segmentDistance(points[i], points[s], points[e]);\n\t\t\tif (dist > maxDist) {\n\t\t\t\tmaxDist = dist;\n\t\t\t\tindex = i;\n\t\t\t}\n\t\t}\n\t\tif (index !== -1 && maxDist > tolerance) {\n\t\t\tkeep[index] = 1;\n\t\t\tstack.push([s, index], [index, e]);\n\t\t}\n\t}\n\treturn points.filter((_, i) => keep[i]);\n}\n\n/**\n * Simplifies a closed ring with Ramer–Douglas–Peucker at `tolerance`. Returns an empty\n * ring when fewer than three points survive.\n */\nexport function simplifyRing(ring: readonly Point[], tolerance: number): Point[] {\n\tif (ring.length <= 3 || !(tolerance > 0)) return [...ring];\n\t// Anchor on the point farthest from the first one so the closing seam is not lost.\n\tlet far = 0;\n\tlet farDist = -1;\n\tfor (let i = 1; i < ring.length; i++) {\n\t\tconst dist = Math.hypot(ring[i][0] - ring[0][0], ring[i][1] - ring[0][1]);\n\t\tif (dist > farDist) {\n\t\t\tfarDist = dist;\n\t\t\tfar = i;\n\t\t}\n\t}\n\tconst first = rdp(ring.slice(0, far + 1) as Point[], tolerance);\n\tconst second = rdp([...ring.slice(far), ring[0]] as Point[], tolerance);\n\tconst out = [...first.slice(0, -1), ...second.slice(0, -1)];\n\treturn out.length >= 3 ? out : [];\n}\n\nexport interface PolygonToPathOptions {\n\t/** Decimal places in the output (default 3). */\n\tprecision?: number;\n\t/** Ramer–Douglas–Peucker tolerance; `0` (default) keeps every vertex. */\n\tsimplify?: number;\n\t/** Added to every point before formatting (e.g. to make coordinates node-local). */\n\toffset?: { x: number; y: number };\n\t/**\n\t * `'lines'` (default) writes every ring as `M`/`L`/`Z`. `'fit'` fits cubic Béziers to each\n\t * ring (`fitPathData`), so flattened curves come back as smooth `C` curves while sharp\n\t * corners (turning more than `cornerAngleDeg`) and straight edges stay as they are.\n\t */\n\tcurves?: 'lines' | 'fit';\n\t/** Curve-fitting tolerance in path units for `curves: 'fit'` (default 0.25). */\n\tcurveTolerance?: number;\n\t/** Corner threshold in degrees for `curves: 'fit'` (default 30). */\n\tcornerAngleDeg?: number;\n}\n\n/**\n * Converts a multi-polygon (or a list of rings) to SVG path data using `M`/`L`/`Z`. Holes\n * keep their opposite orientation, so the path fills correctly with either fill rule.\n */\nexport function polygonToSvgPath(input: MultiPolygon | PolygonRing[], options: PolygonToPathOptions = {}): string {\n\tconst precision = options.precision ?? 3;\n\tconst f = (n: number) => formatNumber(n, precision);\n\tconst dx = options.offset?.x ?? 0;\n\tconst dy = options.offset?.y ?? 0;\n\tconst rings: Point[][] = [];\n\tconst fit = options.curves === 'fit';\n\tlet fitted = '';\n\tfor (const item of input as any[]) {\n\t\tconst list: Point[][] =\n\t\t\titem.length > 0 && Array.isArray(item[0]) && Array.isArray(item[0][0]) ? item : [item];\n\t\tfor (const ring of list) {\n\t\t\tlet r = ring;\n\t\t\tif (options.simplify && options.simplify > 0) r = simplifyRing(r, options.simplify);\n\t\t\tif (r.length < 3) continue;\n\t\t\tif (fit) {\n\t\t\t\tfitted += fitPathData(r, {\n\t\t\t\t\ttolerance: options.curveTolerance ?? 0.25,\n\t\t\t\t\tcornerAngleDeg: options.cornerAngleDeg,\n\t\t\t\t\tprecision,\n\t\t\t\t\toffset: { x: dx, y: dy }\n\t\t\t\t});\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\trings.push(dx || dy ? r.map(([x, y]) => [x + dx, y + dy] as Point) : r);\n\t\t}\n\t}\n\treturn fit ? fitted : ringsToPath(rings, f);\n}\n","/**\n * Bézier path model shared by the curve-preserving path operations: absolute `M` / `L` /\n * `Q` / `C` / `Z` commands, conversion from SVG path data (arcs become cubics), segment\n * math (evaluation, splitting, extrema, nearest point), exact areas, winding numbers and\n * arc-length utilities.\n */\nimport type { Point } from '../types.js';\nimport { formatNumber, parseSvgPath, type PathCommand } from '../shapeGeometry.js';\nimport type { PolygonFillRule } from '../polygonBoolean.js';\n\n// ---------------------------------------------------------------------------\n// Public path model\n// ---------------------------------------------------------------------------\n\n/** One absolute path command. Every subpath starts with `M`; `Z` closes it. */\nexport type BezierCommand =\n\t| { type: 'M'; x: number; y: number }\n\t| { type: 'L'; x: number; y: number }\n\t| { type: 'Q'; x1: number; y1: number; x: number; y: number }\n\t| { type: 'C'; x1: number; y1: number; x2: number; y2: number; x: number; y: number }\n\t| { type: 'Z' };\n\n/** A path as a list of absolute move / line / quadratic / cubic / close commands. */\nexport type BezierPath = BezierCommand[];\n\n/** Anything the path operations accept as a path: SVG path data, parsed commands or a `BezierPath`. */\nexport type BezierPathInput = string | readonly BezierCommand[] | readonly PathCommand[];\n\n/**\n * One segment as its control points: 2 points for a line, 3 for a quadratic and 4 for a\n * cubic Bézier.\n */\nexport type BezierSegment = readonly Point[];\n\n/** A subpath as a chain of segments (each starts where the previous one ends). */\nexport interface BezierContour {\n\tsegments: BezierSegment[];\n\tclosed: boolean;\n}\n\n// ---------------------------------------------------------------------------\n// Vector helpers\n// ---------------------------------------------------------------------------\n\nexport const sub = (a: Point, b: Point): Point => [a[0] - b[0], a[1] - b[1]];\nexport const add = (a: Point, b: Point): Point => [a[0] + b[0], a[1] + b[1]];\nexport const scale = (a: Point, s: number): Point => [a[0] * s, a[1] * s];\nexport const dot = (a: Point, b: Point) => a[0] * b[0] + a[1] * b[1];\nexport const cross = (a: Point, b: Point) => a[0] * b[1] - a[1] * b[0];\nexport const dist = (a: Point, b: Point) => Math.hypot(a[0] - b[0], a[1] - b[1]);\nexport const lerp = (a: Point, b: Point, t: number): Point => [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t];\nexport const samePoint = (a: Point, b: Point) => a[0] === b[0] && a[1] === b[1];\n\nexport function unit(v: Point): Point | null {\n\tconst l = Math.hypot(v[0], v[1]);\n\treturn l > 0 && Number.isFinite(l) ? [v[0] / l, v[1] / l] : null;\n}\n\n// ---------------------------------------------------------------------------\n// Segment math\n// ---------------------------------------------------------------------------\n\n/** Point at parameter `t` (de Casteljau, works for any degree). */\nexport function segmentPoint(seg: BezierSegment, t: number): Point {\n\tconst n = seg.length;\n\tif (n === 2) return lerp(seg[0], seg[1], t);\n\tconst mt = 1 - t;\n\tif (n === 3) {\n\t\tconst a = mt * mt;\n\t\tconst b = 2 * mt * t;\n\t\tconst c = t * t;\n\t\treturn [a * seg[0][0] + b * seg[1][0] + c * seg[2][0], a * seg[0][1] + b * seg[1][1] + c * seg[2][1]];\n\t}\n\tconst a = mt * mt * mt;\n\tconst b = 3 * mt * mt * t;\n\tconst c = 3 * mt * t * t;\n\tconst d = t * t * t;\n\treturn [\n\t\ta * seg[0][0] + b * seg[1][0] + c * seg[2][0] + d * seg[3][0],\n\t\ta * seg[0][1] + b * seg[1][1] + c * seg[2][1] + d * seg[3][1]\n\t];\n}\n\n/** First derivative at `t`. */\nexport function segmentDerivative(seg: BezierSegment, t: number): Point {\n\tconst n = seg.length;\n\tif (n === 2) return sub(seg[1], seg[0]);\n\tif (n === 3) {\n\t\tconst a = sub(seg[1], seg[0]);\n\t\tconst b = sub(seg[2], seg[1]);\n\t\treturn [2 * ((1 - t) * a[0] + t * b[0]), 2 * ((1 - t) * a[1] + t * b[1])];\n\t}\n\tconst a = sub(seg[1], seg[0]);\n\tconst b = sub(seg[2], seg[1]);\n\tconst c = sub(seg[3], seg[2]);\n\tconst mt = 1 - t;\n\treturn [\n\t\t3 * (mt * mt * a[0] + 2 * mt * t * b[0] + t * t * c[0]),\n\t\t3 * (mt * mt * a[1] + 2 * mt * t * b[1] + t * t * c[1])\n\t];\n}\n\n/** Second derivative at `t`. */\nexport function segmentSecondDerivative(seg: BezierSegment, t: number): Point {\n\tconst n = seg.length;\n\tif (n === 2) return [0, 0];\n\tif (n === 3) return [2 * (seg[2][0] - 2 * seg[1][0] + seg[0][0]), 2 * (seg[2][1] - 2 * seg[1][1] + seg[0][1])];\n\tconst a: Point = [seg[2][0] - 2 * seg[1][0] + seg[0][0], seg[2][1] - 2 * seg[1][1] + seg[0][1]];\n\tconst b: Point = [seg[3][0] - 2 * seg[2][0] + seg[1][0], seg[3][1] - 2 * seg[2][1] + seg[1][1]];\n\treturn [6 * ((1 - t) * a[0] + t * b[0]), 6 * ((1 - t) * a[1] + t * b[1])];\n}\n\n/**\n * Unit tangent at `t`. Where the derivative vanishes (a handle on its anchor) the direction\n * a hair further along is used, and the chord as the last resort.\n */\nexport function segmentTangent(seg: BezierSegment, t: number): Point {\n\tconst d = unit(segmentDerivative(seg, t));\n\tif (d) return d;\n\tconst h = t < 0.5 ? Math.min(1, t + 1e-6) : Math.max(0, t - 1e-6);\n\tconst e = unit(segmentDerivative(seg, h));\n\tif (e) return e;\n\treturn unit(sub(seg[seg.length - 1], seg[0])) ?? [1, 0];\n}\n\n/** Signed curvature at `t` (positive when turning counter-clockwise in a y-up frame). */\nexport function segmentCurvature(seg: BezierSegment, t: number): number {\n\tif (seg.length === 2) return 0;\n\tlet d = segmentDerivative(seg, t);\n\tlet tt = t;\n\tif (Math.hypot(d[0], d[1]) < 1e-12) {\n\t\ttt = t < 0.5 ? Math.min(1, t + 1e-6) : Math.max(0, t - 1e-6);\n\t\td = segmentDerivative(seg, tt);\n\t}\n\tconst dd = segmentSecondDerivative(seg, tt);\n\tconst len = Math.hypot(d[0], d[1]);\n\tif (!(len > 0)) return 0;\n\treturn cross(d, dd) / (len * len * len);\n}\n\n/** Splits a segment at `t` (de Casteljau). */\nexport function splitSegment(seg: BezierSegment, t: number): [Point[], Point[]] {\n\tconst n = seg.length;\n\tif (n === 2) {\n\t\tconst m = lerp(seg[0], seg[1], t);\n\t\treturn [\n\t\t\t[seg[0], m],\n\t\t\t[m, seg[1]]\n\t\t];\n\t}\n\tif (n === 3) {\n\t\tconst a = lerp(seg[0], seg[1], t);\n\t\tconst b = lerp(seg[1], seg[2], t);\n\t\tconst m = lerp(a, b, t);\n\t\treturn [\n\t\t\t[seg[0], a, m],\n\t\t\t[m, b, seg[2]]\n\t\t];\n\t}\n\tconst a = lerp(seg[0], seg[1], t);\n\tconst b = lerp(seg[1], seg[2], t);\n\tconst c = lerp(seg[2], seg[3], t);\n\tconst ab = lerp(a, b, t);\n\tconst bc = lerp(b, c, t);\n\tconst m = lerp(ab, bc, t);\n\treturn [\n\t\t[seg[0], a, ab, m],\n\t\t[m, bc, c, seg[3]]\n\t];\n}\n\n/** The part of a segment between parameters `t0` and `t1` (reversed when `t1 < t0`). */\nexport function subSegment(seg: BezierSegment, t0: number, t1: number): Point[] {\n\tif (t1 < t0) return subSegment(seg, t1, t0).reverse();\n\tif (t0 <= 0 && t1 >= 1) return [...seg];\n\tlet s: Point[] = [...seg];\n\tif (t1 < 1) s = splitSegment(s, t1)[0];\n\tif (t0 > 0) s = splitSegment(s, t1 > 0 ? t0 / t1 : 0)[1];\n\treturn s;\n}\n\n/** Whether every control point lies within `tol` of the chord (a line in disguise). */\nexport function isStraightSegment(seg: BezierSegment, tol: number): boolean {\n\tif (seg.length === 2) return true;\n\tconst a = seg[0];\n\tconst b = seg[seg.length - 1];\n\tconst chord = sub(b, a);\n\tconst len = Math.hypot(chord[0], chord[1]);\n\tfor (let i = 1; i < seg.length - 1; i++) {\n\t\tconst v = sub(seg[i], a);\n\t\tif (len === 0) {\n\t\t\tif (Math.hypot(v[0], v[1]) > tol) return false;\n\t\t\tcontinue;\n\t\t}\n\t\tif (Math.abs(cross(chord, v)) / len > tol) return false;\n\t\tconst along = dot(chord, v) / len;\n\t\tif (along < -tol || along > len + tol) return false;\n\t}\n\treturn true;\n}\n\n/** Real roots in the open interval (0, 1) of `a t² + b t + c`. */\nfunction quadraticRoots(a: number, b: number, c: number, out: number[]) {\n\tconst push = (t: number) => {\n\t\tif (t > 1e-12 && t < 1 - 1e-12) out.push(t);\n\t};\n\tconst scaleAbs = Math.max(Math.abs(a), Math.abs(b), Math.abs(c));\n\tif (scaleAbs === 0) return;\n\tif (Math.abs(a) <= 1e-12 * scaleAbs) {\n\t\tif (b !== 0) push(-c / b);\n\t\treturn;\n\t}\n\tconst disc = b * b - 4 * a * c;\n\tif (disc < 0) return;\n\tconst sq = Math.sqrt(disc);\n\tconst q = -0.5 * (b + (b >= 0 ? sq : -sq));\n\tpush(q / a);\n\tif (q !== 0) push(c / q);\n}\n\n/** Parameters in (0, 1) where the segment's `axis` coordinate has a local extremum. */\nexport function segmentExtrema(seg: BezierSegment, axis: 0 | 1): number[] {\n\tconst out: number[] = [];\n\tif (seg.length === 3) {\n\t\tconst d = seg[0][axis] - 2 * seg[1][axis] + seg[2][axis];\n\t\tif (d !== 0) {\n\t\t\tconst t = (seg[0][axis] - seg[1][axis]) / d;\n\t\t\tif (t > 1e-12 && t < 1 - 1e-12) out.push(t);\n\t\t}\n\t} else if (seg.length === 4) {\n\t\tconst p0 = seg[0][axis];\n\t\tconst p1 = seg[1][axis];\n\t\tconst p2 = seg[2][axis];\n\t\tconst p3 = seg[3][axis];\n\t\t// B'(t)/3 = a t² + b t + c\n\t\tconst a = -p0 + 3 * p1 - 3 * p2 + p3;\n\t\tconst b = 2 * (p0 - 2 * p1 + p2);\n\t\tconst c = p1 - p0;\n\t\tquadraticRoots(a, b, c, out);\n\t}\n\treturn out.sort((x, y) => x - y);\n}\n\n/** Exact axis-aligned bounds of a segment. */\nexport function segmentBounds(seg: BezierSegment): { minX: number; minY: number; maxX: number; maxY: number } {\n\tconst a = seg[0];\n\tconst b = seg[seg.length - 1];\n\tlet minX = Math.min(a[0], b[0]);\n\tlet maxX = Math.max(a[0], b[0]);\n\tlet minY = Math.min(a[1], b[1]);\n\tlet maxY = Math.max(a[1], b[1]);\n\tfor (const axis of [0, 1] as const) {\n\t\tfor (const t of segmentExtrema(seg, axis)) {\n\t\t\tconst p = segmentPoint(seg, t);\n\t\t\tif (axis === 0) {\n\t\t\t\tminX = Math.min(minX, p[0]);\n\t\t\t\tmaxX = Math.max(maxX, p[0]);\n\t\t\t} else {\n\t\t\t\tminY = Math.min(minY, p[1]);\n\t\t\t\tmaxY = Math.max(maxY, p[1]);\n\t\t\t}\n\t\t}\n\t}\n\treturn { minX, minY, maxX, maxY };\n}\n\n/** Parameters splitting the segment into pieces monotone in x and y. */\nexport function monotoneParams(seg: BezierSegment): number[] {\n\tif (seg.length === 2) return [];\n\tconst ts = [...segmentExtrema(seg, 0), ...segmentExtrema(seg, 1)].sort((a, b) => a - b);\n\tconst out: number[] = [];\n\tfor (const t of ts) if (out.length === 0 || t - out[out.length - 1] > 1e-9) out.push(t);\n\treturn out;\n}\n\n/**\n * Signed area swept by the segment relative to the origin (½ ∫ x dy − y dx). Exact:\n * the integrand is a polynomial of degree at most 5, which 3-point Gauss–Legendre\n * integrates exactly.\n */\nexport function segmentAreaTerm(seg: BezierSegment): number {\n\tif (seg.length === 2) return (seg[0][0] * seg[1][1] - seg[1][0] * seg[0][1]) / 2;\n\tconst nodes = [-Math.sqrt(3 / 5), 0, Math.sqrt(3 / 5)];\n\tconst weights = [5 / 9, 8 / 9, 5 / 9];\n\tlet sum = 0;\n\tfor (let i = 0; i < 3; i++) {\n\t\tconst t = (nodes[i] + 1) / 2;\n\t\tconst p = segmentPoint(seg, t);\n\t\tconst d = segmentDerivative(seg, t);\n\t\tsum += weights[i] * (p[0] * d[1] - p[1] * d[0]);\n\t}\n\t// ½ for the area, ½ for mapping [-1, 1] onto [0, 1].\n\treturn sum / 4;\n}\n\n// Gauss–Legendre nodes / weights (order 8) on [-1, 1].\nconst GL_X = [\n\t-0.9602898564975363, -0.7966664774136267, -0.525532409916329, -0.1834346424956498, 0.1834346424956498,\n\t0.525532409916329, 0.7966664774136267, 0.9602898564975363\n];\nconst GL_W = [\n\t0.1012285362903763, 0.2223810344533745, 0.3137066458778873, 0.362683783378362, 0.362683783378362,\n\t0.3137066458778873, 0.2223810344533745, 0.1012285362903763\n];\n\nfunction glLength(seg: BezierSegment, t0: number, t1: number): number {\n\tconst half = (t1 - t0) / 2;\n\tconst mid = (t0 + t1) / 2;\n\tlet sum = 0;\n\tfor (let i = 0; i < GL_X.length; i++) {\n\t\tconst d = segmentDerivative(seg, mid + half * GL_X[i]);\n\t\tsum += GL_W[i] * Math.hypot(d[0], d[1]);\n\t}\n\treturn sum * half;\n}\n\nfunction adaptiveLength(seg: BezierSegment, t0: number, t1: number, whole: number, tol: number, depth: number): number {\n\tconst m = (t0 + t1) / 2;\n\tconst left = glLength(seg, t0, m);\n\tconst right = glLength(seg, m, t1);\n\tif (depth >= 18 || Math.abs(left + right - whole) <= tol) return left + right;\n\treturn adaptiveLength(seg, t0, m, left, tol / 2, depth + 1) + adaptiveLength(seg, m, t1, right, tol / 2, depth + 1);\n}\n\n/** Arc length of the segment between `t0` and `t1` (default the whole segment). */\nexport function segmentLength(seg: BezierSegment, t0 = 0, t1 = 1, tolerance = 1e-9): number {\n\tif (t1 < t0) return segmentLength(seg, t1, t0, tolerance);\n\tif (seg.length === 2) return dist(seg[0], seg[1]) * (t1 - t0);\n\tconst whole = glLength(seg, t0, t1);\n\treturn adaptiveLength(seg, t0, t1, whole, Math.max(tolerance, whole * 1e-12), 0);\n}\n\n/** Parameter at arc length `length` from the segment start (clamped to [0, 1]). */\nexport function segmentParamAtLength(seg: BezierSegment, length: number, total = segmentLength(seg)): number {\n\tif (!(total > 0) || length <= 0) return 0;\n\tif (length >= total) return 1;\n\tif (seg.length === 2) return length / total;\n\tlet lo = 0;\n\tlet hi = 1;\n\tlet t = length / total;\n\tfor (let i = 0; i < 40; i++) {\n\t\tconst err = segmentLength(seg, 0, t) - length;\n\t\tif (Math.abs(err) <= 1e-10 * Math.max(1, total)) break;\n\t\tif (err > 0) hi = t;\n\t\telse lo = t;\n\t\tconst d = segmentDerivative(seg, t);\n\t\tconst speed = Math.hypot(d[0], d[1]);\n\t\tlet next = speed > 0 ? t - err / speed : (lo + hi) / 2;\n\t\tif (!(next > lo && next < hi)) next = (lo + hi) / 2;\n\t\tt = next;\n\t}\n\treturn t;\n}\n\n/**\n * Nearest point of the segment to `p`: the parameter and the distance. Coarse sampling\n * followed by Newton refinement around the best samples.\n */\nexport function segmentNearest(seg: BezierSegment, p: Point): { t: number; distance: number } {\n\tif (seg.length === 2) {\n\t\tconst d = sub(seg[1], seg[0]);\n\t\tconst len2 = dot(d, d);\n\t\tconst t = len2 === 0 ? 0 : Math.max(0, Math.min(1, dot(sub(p, seg[0]), d) / len2));\n\t\treturn { t, distance: dist(p, segmentPoint(seg, t)) };\n\t}\n\tconst samples = 16;\n\tlet best = { t: 0, distance: dist(p, seg[0]) };\n\tconst candidates: number[] = [];\n\tlet prev = Infinity;\n\tlet prevT = 0;\n\tlet descending = false;\n\tfor (let i = 0; i <= samples; i++) {\n\t\tconst t = i / samples;\n\t\tconst d = dist(p, segmentPoint(seg, t));\n\t\tif (d < best.distance) best = { t, distance: d };\n\t\tif (i > 0 && d > prev && descending) candidates.push(prevT);\n\t\tdescending = d < prev;\n\t\tprev = d;\n\t\tprevT = t;\n\t}\n\tcandidates.push(best.t, 1);\n\tfor (const start of candidates) {\n\t\tlet t = start;\n\t\tfor (let k = 0; k < 12; k++) {\n\t\t\tconst q = segmentPoint(seg, t);\n\t\t\tconst d1 = segmentDerivative(seg, t);\n\t\t\tconst d2 = segmentSecondDerivative(seg, t);\n\t\t\tconst diff = sub(q, p);\n\t\t\tconst f = dot(diff, d1);\n\t\t\tconst fp = dot(d1, d1) + dot(diff, d2);\n\t\t\tif (!(Math.abs(fp) > 1e-300)) break;\n\t\t\tconst next = Math.max(0, Math.min(1, t - f / fp));\n\t\t\tif (Math.abs(next - t) < 1e-14) {\n\t\t\t\tt = next;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tt = next;\n\t\t}\n\t\tconst d = dist(p, segmentPoint(seg, t));\n\t\tif (d < best.distance) best = { t, distance: d };\n\t}\n\treturn best;\n}\n\n// ---------------------------------------------------------------------------\n// Arcs\n// ---------------------------------------------------------------------------\n\n/** Converts an SVG elliptical arc (SVG 1.1 F.6.5) to cubic segments, at most 90° each. */\nexport function arcToCubics(\n\tfrom: Point,\n\tcmd: { rx: number; ry: number; rotation: number; largeArc: boolean; sweep: boolean; x: number; y: number }\n): Point[][] {\n\tconst [x0, y0] = from;\n\tconst { x, y } = cmd;\n\tif (x0 === x && y0 === y) return [];\n\tlet rx = Math.abs(cmd.rx);\n\tlet ry = Math.abs(cmd.ry);\n\tif (rx === 0 || ry === 0) return [[from, [x, y]]];\n\tconst phi = (cmd.rotation * Math.PI) / 180;\n\tconst cos = Math.cos(phi);\n\tconst sin = Math.sin(phi);\n\tconst dx = (x0 - x) / 2;\n\tconst dy = (y0 - y) / 2;\n\tconst x1p = cos * dx + sin * dy;\n\tconst y1p = -sin * dx + cos * dy;\n\tconst lambda = (x1p * x1p) / (rx * rx) + (y1p * y1p) / (ry * ry);\n\tif (lambda > 1) {\n\t\tconst s = Math.sqrt(lambda);\n\t\trx *= s;\n\t\try *= s;\n\t}\n\tconst num = rx * rx * ry * ry - rx * rx * y1p * y1p - ry * ry * x1p * x1p;\n\tconst den = rx * rx * y1p * y1p + ry * ry * x1p * x1p;\n\tlet coef = den === 0 ? 0 : Math.sqrt(Math.max(0, num / den));\n\tif (cmd.largeArc === cmd.sweep) coef = -coef;\n\tconst cxp = (coef * rx * y1p) / ry;\n\tconst cyp = (-coef * ry * x1p) / rx;\n\tconst cx = cos * cxp - sin * cyp + (x0 + x) / 2;\n\tconst cy = sin * cxp + cos * cyp + (y0 + y) / 2;\n\tconst angle = (ux: number, uy: number, vx: number, vy: number) => Math.atan2(ux * vy - uy * vx, ux * vx + uy * vy);\n\tconst theta1 = angle(1, 0, (x1p - cxp) / rx, (y1p - cyp) / ry);\n\tlet delta = angle((x1p - cxp) / rx, (y1p - cyp) / ry, (-x1p - cxp) / rx, (-y1p - cyp) / ry);\n\tif (!cmd.sweep && delta > 0) delta -= 2 * Math.PI;\n\telse if (cmd.sweep && delta < 0) delta += 2 * Math.PI;\n\tconst n = Math.max(1, Math.ceil(Math.abs(delta) / (Math.PI / 2) - 1e-9));\n\tconst step = delta / n;\n\tconst k = (4 / 3) * Math.tan(step / 4);\n\tconst map = (ux: number, uy: number): Point => {\n\t\tconst ex = rx * ux;\n\t\tconst ey = ry * uy;\n\t\treturn [cos * ex - sin * ey + cx, sin * ex + cos * ey + cy];\n\t};\n\tconst out: Point[][] = [];\n\tlet prev: Point = from;\n\tfor (let i = 0; i < n; i++) {\n\t\tconst a0 = theta1 + step * i;\n\t\tconst a1 = a0 + step;\n\t\tconst c0 = Math.cos(a0);\n\t\tconst s0 = Math.sin(a0);\n\t\tconst c1 = Math.cos(a1);\n\t\tconst s1 = Math.sin(a1);\n\t\tconst end: Point = i === n - 1 ? [x, y] : map(c1, s1);\n\t\tout.push([prev, map(c0 - k * s0, s0 + k * c0), map(c1 + k * s1, s1 - k * c1), end]);\n\t\tprev = end;\n\t}\n\treturn out;\n}\n\n// ---------------------------------------------------------------------------\n// Conversions\n// ---------------------------------------------------------------------------\n\nfunction isBezierCommandList(input: BezierPathInput): input is readonly BezierCommand[] {\n\treturn Array.isArray(input) && !input.some((c: any) => c && c.type === 'A');\n}\n\n/**\n * Converts SVG path data or parsed commands to a `BezierPath` (absolute `M` / `L` / `Q` /\n * `C` / `Z`); elliptical arcs become cubic Béziers (at most 90° each).\n */\nexport function toBezierPath(input: BezierPathInput): BezierPath {\n\tconst commands: readonly (PathCommand | BezierCommand)[] =\n\t\ttypeof input === 'string' ? parseSvgPath(input) : isBezierCommandList(input) ? input : (input as readonly PathCommand[]);\n\tconst out: BezierPath = [];\n\tlet current: Point = [0, 0];\n\tlet start: Point = [0, 0];\n\tlet open = false;\n\tconst ensureMove = () => {\n\t\tif (!open) {\n\t\t\tout.push({ type: 'M', x: start[0], y: start[1] });\n\t\t\tcurrent = start;\n\t\t\topen = true;\n\t\t}\n\t};\n\tfor (const cmd of commands) {\n\t\tswitch (cmd.type) {\n\t\t\tcase 'M':\n\t\t\t\tstart = [cmd.x, cmd.y];\n\t\t\t\tcurrent = start;\n\t\t\t\tout.push({ type: 'M', x: cmd.x, y: cmd.y });\n\t\t\t\topen = true;\n\t\t\t\tbreak;\n\t\t\tcase 'L':\n\t\t\t\tensureMove();\n\t\t\t\tout.push({ type: 'L', x: cmd.x, y: cmd.y });\n\t\t\t\tcurrent = [cmd.x, cmd.y];\n\t\t\t\tbreak;\n\t\t\tcase 'Q':\n\t\t\t\tensureMove();\n\t\t\t\tout.push({ type: 'Q', x1: cmd.x1, y1: cmd.y1, x: cmd.x, y: cmd.y });\n\t\t\t\tcurrent = [cmd.x, cmd.y];\n\t\t\t\tbreak;\n\t\t\tcase 'C':\n\t\t\t\tensureMove();\n\t\t\t\tout.push({ type: 'C', x1: cmd.x1, y1: cmd.y1, x2: cmd.x2, y2: cmd.y2, x: cmd.x, y: cmd.y });\n\t\t\t\tcurrent = [cmd.x, cmd.y];\n\t\t\t\tbreak;\n\t\t\tcase 'A': {\n\t\t\t\tensureMove();\n\t\t\t\tfor (const c of arcToCubics(current, cmd)) {\n\t\t\t\t\tif (c.length === 2) out.push({ type: 'L', x: c[1][0], y: c[1][1] });\n\t\t\t\t\telse out.push({ type: 'C', x1: c[1][0], y1: c[1][1], x2: c[2][0], y2: c[2][1], x: c[3][0], y: c[3][1] });\n\t\t\t\t}\n\t\t\t\tcurrent = [cmd.x, cmd.y];\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase 'Z':\n\t\t\t\tif (open) out.push({ type: 'Z' });\n\t\t\t\topen = false;\n\t\t\t\tcurrent = start;\n\t\t\t\tbreak;\n\t\t}\n\t}\n\treturn out;\n}\n\n/** Splits a path into contours of segments. Zero-length segments are dropped. */\nexport function pathToContours(input: BezierPathInput): BezierContour[] {\n\tconst path = toBezierPath(input);\n\tconst contours: BezierContour[] = [];\n\tlet current: BezierContour | null = null;\n\tlet pen: Point = [0, 0];\n\tlet start: Point = [0, 0];\n\tconst push = (seg: Point[]) => {\n\t\tif (!current) {\n\t\t\tcurrent = { segments: [], closed: false };\n\t\t\tcontours.push(current);\n\t\t}\n\t\tif (seg.every((p) => samePoint(p, seg[0]))) return;\n\t\tcurrent.segments.push(seg);\n\t};\n\tfor (const cmd of path) {\n\t\tswitch (cmd.type) {\n\t\t\tcase 'M':\n\t\t\t\tstart = [cmd.x, cmd.y];\n\t\t\t\tpen = start;\n\t\t\t\tcurrent = { segments: [], closed: false };\n\t\t\t\tcontours.push(current);\n\t\t\t\tbreak;\n\t\t\tcase 'L':\n\t\t\t\tpush([pen, [cmd.x, cmd.y]]);\n\t\t\t\tpen = [cmd.x, cmd.y];\n\t\t\t\tbreak;\n\t\t\tcase 'Q':\n\t\t\t\tpush([pen, [cmd.x1, cmd.y1], [cmd.x, cmd.y]]);\n\t\t\t\tpen = [cmd.x, cmd.y];\n\t\t\t\tbreak;\n\t\t\tcase 'C':\n\t\t\t\tpush([pen, [cmd.x1, cmd.y1], [cmd.x2, cmd.y2], [cmd.x, cmd.y]]);\n\t\t\t\tpen = [cmd.x, cmd.y];\n\t\t\t\tbreak;\n\t\t\tcase 'Z':\n\t\t\t\tif (current) (current as BezierContour).closed = true;\n\t\t\t\tcurrent = null;\n\t\t\t\tpen = start;\n\t\t\t\tbreak;\n\t\t}\n\t}\n\treturn contours;\n}\n\n/** Builds a path from contours (a closed contour's closing line is left to `Z`). */\nexport function contoursToPath(contours: readonly BezierContour[]): BezierPath {\n\tconst out: BezierPath = [];\n\tfor (const contour of contours) {\n\t\tconst segs = contour.segments;\n\t\tif (segs.length === 0) continue;\n\t\tout.push({ type: 'M', x: segs[0][0][0], y: segs[0][0][1] });\n\t\tlet count = segs.length;\n\t\tif (contour.closed && count > 1) {\n\t\t\tconst last = segs[count - 1];\n\t\t\tif (last.length === 2 && samePoint(last[1], segs[0][0])) count--;\n\t\t}\n\t\tfor (let i = 0; i < count; i++) {\n\t\t\tconst s = segs[i];\n\t\t\tconst end = s[s.length - 1];\n\t\t\tif (s.length === 2) out.push({ type: 'L', x: end[0], y: end[1] });\n\t\t\telse if (s.length === 3) out.push({ type: 'Q', x1: s[1][0], y1: s[1][1], x: end[0], y: end[1] });\n\t\t\telse out.push({ type: 'C', x1: s[1][0], y1: s[1][1], x2: s[2][0], y2: s[2][1], x: end[0], y: end[1] });\n\t\t}\n\t\tif (contour.closed) out.push({ type: 'Z' });\n\t}\n\treturn out;\n}\n\nexport interface BezierPathToSvgOptions {\n\t/** Decimal places (default 3). */\n\tprecision?: number;\n\t/** Added to every point before formatting. */\n\toffset?: { x: number; y: number };\n}\n\n/** Writes a path as compact SVG path data. */\nexport function bezierPathToSvg(input: BezierPathInput, options: BezierPathToSvgOptions = {}): string {\n\tconst path = toBezierPath(input);\n\tconst precision = options.precision ?? 3;\n\tconst dx = options.offset?.x ?? 0;\n\tconst dy = options.offset?.y ?? 0;\n\tconst fx = (n: number) => formatNumber(n + dx, precision);\n\tconst fy = (n: number) => formatNumber(n + dy, precision);\n\tlet d = '';\n\tfor (const c of path) {\n\t\tswitch (c.type) {\n\t\t\tcase 'M':\n\t\t\t\td += `M${fx(c.x)} ${fy(c.y)}`;\n\t\t\t\tbreak;\n\t\t\tcase 'L':\n\t\t\t\td += `L${fx(c.x)} ${fy(c.y)}`;\n\t\t\t\tbreak;\n\t\t\tcase 'Q':\n\t\t\t\td += `Q${fx(c.x1)} ${fy(c.y1)} ${fx(c.x)} ${fy(c.y)}`;\n\t\t\t\tbreak;\n\t\t\tcase 'C':\n\t\t\t\td += `C${fx(c.x1)} ${fy(c.y1)} ${fx(c.x2)} ${fy(c.y2)} ${fx(c.x)} ${fy(c.y)}`;\n\t\t\t\tbreak;\n\t\t\tcase 'Z':\n\t\t\t\td += 'Z';\n\t\t\t\tbreak;\n\t\t}\n\t}\n\treturn d;\n}\n\n/** 2D affine matrix `[a, b, c, d, e, f]` mapping `(x, y)` to `(a x + c y + e, b x + d y + f)`. */\nexport type AffineMatrix = readonly [number, number, number, number, number, number];\n\n/** Applies an affine transform to every point (exact for Bézier curves). */\nexport function transformBezierPath(input: BezierPathInput, m: AffineMatrix): BezierPath {\n\tconst [a, b, c, d, e, f] = m;\n\tconst X = (x: number, y: number) => a * x + c * y + e;\n\tconst Y = (x: number, y: number) => b * x + d * y + f;\n\treturn toBezierPath(input).map((cmd): BezierCommand => {\n\t\tswitch (cmd.type) {\n\t\t\tcase 'M':\n\t\t\tcase 'L':\n\t\t\t\treturn { type: cmd.type, x: X(cmd.x, cmd.y), y: Y(cmd.x, cmd.y) };\n\t\t\tcase 'Q':\n\t\t\t\treturn { type: 'Q', x1: X(cmd.x1, cmd.y1), y1: Y(cmd.x1, cmd.y1), x: X(cmd.x, cmd.y), y: Y(cmd.x, cmd.y) };\n\t\t\tcase 'C':\n\t\t\t\treturn {\n\t\t\t\t\ttype: 'C',\n\t\t\t\t\tx1: X(cmd.x1, cmd.y1),\n\t\t\t\t\ty1: Y(cmd.x1, cmd.y1),\n\t\t\t\t\tx2: X(cmd.x2, cmd.y2),\n\t\t\t\t\ty2: Y(cmd.x2, cmd.y2),\n\t\t\t\t\tx: X(cmd.x, cmd.y),\n\t\t\t\t\ty: Y(cmd.x, cmd.y)\n\t\t\t\t};\n\t\t\tcase 'Z':\n\t\t\t\treturn cmd;\n\t\t}\n\t});\n}\n\n/** Number of drawing segments (`L` / `Q` / `C`, plus the closing line of `Z` when it has length). */\nexport function countPathSegments(input: BezierPathInput): { lines: number; quads: number; cubics: number } {\n\tconst out = { lines: 0, quads: 0, cubics: 0 };\n\tfor (const contour of pathToContours(input)) {\n\t\tconst segs = closeContour(contour);\n\t\tfor (const s of segs) {\n\t\t\tif (s.length === 2) out.lines++;\n\t\t\telse if (s.length === 3) out.quads++;\n\t\t\telse out.cubics++;\n\t\t}\n\t}\n\treturn out;\n}\n\n/** The contour's segments, with the closing line added when the contour is closed (or filled). */\nexport function closeContour(contour: BezierContour, force = false): BezierSegment[] {\n\tconst segs = contour.segments;\n\tif (segs.length === 0) return [];\n\tif (!contour.closed && !force) return segs;\n\tconst first = segs[0][0];\n\tconst lastSeg = segs[segs.length - 1];\n\tconst last = lastSeg[lastSeg.length - 1];\n\treturn samePoint(first, last) ? segs : [...segs, [last, first]];\n}\n\n// ---------------------------------------------------------------------------\n// Measures\n// ---------------------------------------------------------------------------\n\n/** Exact bounds of a path, or `null` when it has no points. */\nexport function bezierPathBounds(input: BezierPathInput): { minX: number; minY: number; maxX: number; maxY: number } | null {\n\tlet minX = Infinity;\n\tlet minY = Infinity;\n\tlet maxX = -Infinity;\n\tlet maxY = -Infinity;\n\tconst path = toBezierPath(input);\n\tlet pen: Point | null = null;\n\tfor (const contour of pathToContours(path)) {\n\t\tfor (const s of contour.segments) {\n\t\t\tconst b = segmentBounds(s);\n\t\t\tminX = Math.min(minX, b.minX);\n\t\t\tminY = Math.min(minY, b.minY);\n\t\t\tmaxX = Math.max(maxX, b.maxX);\n\t\t\tmaxY = Math.max(maxY, b.maxY);\n\t\t}\n\t}\n\tfor (const c of path) {\n\t\tif (c.type === 'M') pen = [c.x, c.y];\n\t\tif (pen) {\n\t\t\tminX = Math.min(minX, pen[0]);\n\t\t\tminY = Math.min(minY, pen[1]);\n\t\t\tmaxX = Math.max(maxX, pen[0]);\n\t\t\tmaxY = Math.max(maxY, pen[1]);\n\t\t}\n\t}\n\treturn minX === Infinity ? null : { minX, minY, maxX, maxY };\n}\n\n/**\n * Signed area of the path as drawn: every subpath closed, contributions summed (positive\n * for counter-clockwise subpaths in a y-up frame). Exact for curves. Overlapping subpaths\n * count with their multiplicity; use `bezierPathArea` for the filled area.\n */\nexport function bezierPathSignedArea(input: BezierPathInput): number {\n\tlet sum = 0;\n\tfor (const contour of pathToContours(input)) sum += contourSignedArea(closeContour(contour, true));\n\treturn sum;\n}\n\n/** Signed area of a closed chain of segments. */\nexport function contourSignedArea(segs: readonly BezierSegment[]): number {\n\tlet sum = 0;\n\tfor (const s of segs) sum += segmentAreaTerm(s);\n\treturn sum;\n}\n\n/** Arc length of a path (closing lines of closed subpaths included). */\nexport function bezierPathLength(input: BezierPathInput): number {\n\tlet total = 0;\n\tfor (const contour of pathToContours(input)) for (const s of closeContour(contour)) total += segmentLength(s);\n\treturn total;\n}\n\n/** Point, unit tangent and angle (radians) at an arc length along the path. */\nexport interface PathPointAtLength {\n\tpoint: Point;\n\ttangent: Point;\n\tangle: number;\n\t/** Subpath index. */\n\tcontour: number;\n\t/** Segment index within the subpath (closing line last). */\n\tsegment: number;\n\t/** Parameter within the segment. */\n\tt: number;\n}\n\n/**\n * The point at arc length `length` from the start of the path (clamped to the path). Arc\n * lengths run through the subpaths in order, as `bezierPathLength` counts them.\n */\nexport function bezierPathPointAtLength(input: BezierPathInput, length: number): PathPointAtLength | null {\n\tconst contours = pathToContours(input);\n\tlet remaining = Math.max(0, length);\n\tlet last: PathPointAtLength | null = null;\n\tfor (let ci = 0; ci < contours.length; ci++) {\n\t\tconst segs = closeContour(contours[ci]);\n\t\tfor (let si = 0; si < segs.length; si++) {\n\t\t\tconst s = segs[si];\n\t\t\tconst len = segmentLength(s);\n\t\t\tif (remaining <= len) {\n\t\t\t\tconst t = segmentParamAtLength(s, remaining, len);\n\t\t\t\tconst tangent = segmentTangent(s, t);\n\t\t\t\treturn { point: segmentPoint(s, t), tangent, angle: Math.atan2(tangent[1], tangent[0]), contour: ci, segment: si, t };\n\t\t\t}\n\t\t\tremaining -= len;\n\t\t\tconst tangent = segmentTangent(s, 1);\n\t\t\tlast = { point: s[s.length - 1], tangent, angle: Math.atan2(tangent[1], tangent[0]), contour: ci, segment: si, t: 1 };\n\t\t}\n\t}\n\treturn last;\n}\n\n/**\n * Splits a segment chain by arc length: the part between `from` and `to` (measured from the\n * chain's start) as new segments.\n */\nexport function trimSegments(segs: readonly BezierSegment[], from: number, to: number, lengths?: number[]): Point[][] {\n\tconst lens = lengths ?? segs.map((s) => segmentLength(s));\n\tconst out: Point[][] = [];\n\tlet at = 0;\n\tfor (let i = 0; i < segs.length; i++) {\n\t\tconst len = lens[i];\n\t\tconst s0 = at;\n\t\tconst s1 = at + len;\n\t\tat = s1;\n\t\tif (s1 <= from || s0 >= to || len <= 0) continue;\n\t\tconst t0 = from > s0 ? segmentParamAtLength(segs[i], from - s0, len) : 0;\n\t\tconst t1 = to < s1 ? segmentParamAtLength(segs[i], to - s0, len) : 1;\n\t\tif (t1 > t0) out.push(subSegment(segs[i], t0, t1));\n\t}\n\treturn out;\n}\n\n// ---------------------------------------------------------------------------\n// Winding numbers\n// ---------------------------------------------------------------------------\n\n/** Parameter where a y-monotone segment reaches height `y` (it must span `y`). */\nexport function solveMonotone(seg: BezierSegment, axis: 0 | 1, value: number): number {\n\tconst a = seg[0][axis];\n\tconst b = seg[seg.length - 1][axis];\n\tif (seg.length === 2) return b === a ? 0 : (value - a) / (b - a);\n\tconst increasing = b > a;\n\tlet lo = 0;\n\tlet hi = 1;\n\tlet t = b === a ? 0.5 : Math.max(0, Math.min(1, (value - a) / (b - a)));\n\tfor (let i = 0; i < 60; i++) {\n\t\tconst v = segmentPoint(seg, t)[axis] - value;\n\t\tif (v === 0) return t;\n\t\tif (v > 0 === increasing) hi = t;\n\t\telse lo = t;\n\t\tconst d = segmentDerivative(seg, t)[axis];\n\t\tlet next = d !== 0 ? t - v / d : (lo + hi) / 2;\n\t\tif (!(next > lo && next < hi)) next = (lo + hi) / 2;\n\t\tif (Math.abs(next - t) < 1e-15) return next;\n\t\tt = next;\n\t}\n\treturn t;\n}\n\n/** Splits segments at their y extrema (pieces monotone in y). */\nfunction yMonotonePieces(segs: readonly BezierSegment[]): BezierSegment[] {\n\tconst out: BezierSegment[] = [];\n\tfor (const s of segs) {\n\t\tif (s.length === 2) {\n\t\t\tout.push(s);\n\t\t\tcontinue;\n\t\t}\n\t\tconst ts = segmentExtrema(s, 1);\n\t\tlet prev = 0;\n\t\tfor (const t of [...ts, 1]) {\n\t\t\tout.push(subSegment(s, prev, t));\n\t\t\tprev = t;\n\t\t}\n\t}\n\treturn out;\n}\n\n/**\n * Winding number of a set of closed segment chains around `point` (ray to +x, half-open\n * crossing rule). Points exactly on the outline may report either side.\n */\nexport function windingNumber(chains: readonly (readonly BezierSegment[])[], point: Point): number {\n\tconst [px, py] = point;\n\tlet winding = 0;\n\tfor (const chain of chains) {\n\t\tfor (const s of yMonotonePieces(chain)) {\n\t\t\tconst ay = s[0][1];\n\t\t\tconst by = s[s.length - 1][1];\n\t\t\tlet dir = 0;\n\t\t\tif (ay <= py && by > py) dir = 1;\n\t\t\telse if (by <= py && ay > py) dir = -1;\n\t\t\telse continue;\n\t\t\tlet minX = Infinity;\n\t\t\tlet maxX = -Infinity;\n\t\t\tfor (const p of s) {\n\t\t\t\tif (p[0] < minX) minX = p[0];\n\t\t\t\tif (p[0] > maxX) maxX = p[0];\n\t\t\t}\n\t\t\tif (minX > px) {\n\t\t\t\twinding += dir;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (maxX <= px) continue;\n\t\t\tconst t = solveMonotone(s, 1, py);\n\t\t\tif (segmentPoint(s, t)[0] > px) winding += dir;\n\t\t}\n\t}\n\treturn winding;\n}\n\n/** Whether `point` is inside the filled path (every subpath closed for filling). */\nexport function pointInBezierPath(input: BezierPathInput, point: Point, fillRule: PolygonFillRule = 'nonzero'): boolean {\n\tconst chains = pathToContours(input).map((c) => closeContour(c, true));\n\tconst w = windingNumber(chains, point);\n\treturn fillRule === 'evenodd' ? (w & 1) === 1 : w !== 0;\n}\n\n/** Distance from `point` to the path outline (closing lines of closed subpaths included). */\nexport function distanceToBezierPath(input: BezierPathInput | readonly BezierContour[], point: Point): number {\n\tconst contours =\n\t\tArray.isArray(input) && input.length > 0 && (input[0] as BezierContour).segments\n\t\t\t? (input as readonly BezierContour[])\n\t\t\t: pathToContours(input as BezierPathInput);\n\tlet best = Infinity;\n\tfor (const contour of contours) {\n\t\tfor (const s of closeContour(contour)) {\n\t\t\t// Skip segments whose control hull is farther than the best distance so far.\n\t\t\tlet minX = Infinity;\n\t\t\tlet minY = Infinity;\n\t\t\tlet maxX = -Infinity;\n\t\t\tlet maxY = -Infinity;\n\t\t\tfor (const p of s) {\n\t\t\t\tminX = Math.min(minX, p[0]);\n\t\t\t\tminY = Math.min(minY, p[1]);\n\t\t\t\tmaxX = Math.max(maxX, p[0]);\n\t\t\t\tmaxY = Math.max(maxY, p[1]);\n\t\t\t}\n\t\t\tconst dx = Math.max(minX - point[0], 0, point[0] - maxX);\n\t\t\tconst dy = Math.max(minY - point[1], 0, point[1] - maxY);\n\t\t\tif (Math.hypot(dx, dy) >= best) continue;\n\t\t\tbest = Math.min(best, segmentNearest(s, point).distance);\n\t\t}\n\t\tif (contour.segments.length === 0) continue;\n\t}\n\treturn best;\n}\n\n/** Flattens a closed chain into a polygon ring (for classification only). */\nexport function flattenChain(segs: readonly BezierSegment[], tolerance: number): Point[] {\n\tconst out: Point[] = [];\n\tfor (const s of segs) {\n\t\tif (out.length === 0) out.push(s[0]);\n\t\tif (s.length === 2) {\n\t\t\tout.push(s[1]);\n\t\t\tcontinue;\n\t\t}\n\t\tlet dd = 0;\n\t\tfor (let i = 0; i + 2 < s.length; i++) {\n\t\t\tdd = Math.max(dd, Math.hypot(s[i][0] - 2 * s[i + 1][0] + s[i + 2][0], s[i][1] - 2 * s[i + 1][1] + s[i + 2][1]));\n\t\t}\n\t\tconst deg = s.length - 1;\n\t\tconst n = Math.max(1, Math.min(256, Math.ceil(Math.sqrt((deg * (deg - 1) * dd) / (8 * tolerance)))));\n\t\tfor (let k = 1; k <= n; k++) out.push(segmentPoint(s, k / n));\n\t}\n\tif (out.length > 1 && samePoint(out[0], out[out.length - 1])) out.pop();\n\treturn out;\n}\n","/**\n * Curve-preserving boolean operations (union, intersection, difference, xor and divide) on\n * Bézier paths.\n *\n * Algorithm (planar arrangement of curve pieces + winding classification):\n * 1. Every segment of every operand (open subpaths closed for filling) is a *source*; it is\n * cut into pieces monotone in x and y. Each piece remembers its source and its parameter\n * range there, so results can be rebuilt from exact sub-curves of the inputs.\n * 2. Pieces are intersected pairwise (pruned by a sweep on x): lines analytically, curves by\n * recursive subdivision down to flat sub-curves, chord intersection and Newton polishing.\n * Endpoints lying on another piece (T-junctions, tangent touches), collinear / coincident\n * overlaps and clusters of hits along a tangency are resolved first. Points closer than\n * `epsilon` share one vertex. Splitting repeats until no piece crosses another.\n * 3. Coincident pieces (shared edges, identical curves) merge into one edge carrying every\n * operand's signed multiplicity.\n * 4. Each edge is classified by casting a horizontal or vertical ray from its midpoint (the\n * pieces are monotone, so a ray crosses each at most once); the operands' fill rules turn\n * the winding numbers on both sides into inside flags and the operation picks the edges\n * with the result on exactly one side.\n * 5. The kept edges are traced into rings, taking the sharpest left turn at each vertex\n * (ties between tangent curves broken by a short chord along each curve), and consecutive\n * pieces of one source are merged back into one exact sub-curve, so circles stay four\n * cubics and rectangles four lines.\n *\n * When the curve engine fails (non-converging intersections on near-coincident curves,\n * an inconsistent arrangement) the operation falls back to the polygon engine\n * (`polygonBoolean`) on flattened input and reports it (`engine: 'polygon'`).\n *\n * Portions ported from vectorcraft-main/crates/pathops/src/boolean.rs (MIT OR Apache-2.0,\n * © ArtCraft Team and contributors). See THIRD_PARTY_NOTICES.md.\n */\nimport type { Point } from '../types.js';\nimport {\n\tpolygonBoolean,\n\tdividePolygons,\n\ttype MultiPolygon,\n\ttype PolygonBooleanOperation,\n\ttype PolygonFillRule\n} from '../polygonBoolean.js';\nimport { polygonToSvgPath } from '../shapeGeometry.js';\nimport {\n\tcloseContour,\n\tcontourSignedArea,\n\tcontoursToPath,\n\tcross,\n\tdist,\n\tdot,\n\tflattenChain,\n\tisStraightSegment,\n\tmonotoneParams,\n\tpathToContours,\n\tsegmentDerivative,\n\tsegmentLength,\n\tsegmentNearest,\n\tsegmentPoint,\n\tsegmentTangent,\n\tsolveMonotone,\n\tsub,\n\tsubSegment,\n\ttoBezierPath,\n\twindingNumber,\n\ttype BezierContour,\n\ttype BezierPath,\n\ttype BezierPathInput,\n\ttype BezierSegment\n} from './bezier.js';\n\n// ---------------------------------------------------------------------------\n// Public types\n// ---------------------------------------------------------------------------\n\n/** Boolean operations (same names as `polygonBoolean`). `xor` is Exclude. */\nexport type PathBooleanOperation = PolygonBooleanOperation;\n\n/** A path with the fill rule it is read with (default `nonzero`). */\nexport interface PathOperand {\n\tpath: BezierPathInput;\n\tfillRule?: PolygonFillRule;\n}\n\nexport type PathOperandInput = BezierPathInput | PathOperand;\n\nexport interface PathOpsOptions {\n\t/**\n\t * Distance below which points are treated as equal. Defaults to `1e-9` times the\n\t * largest coordinate magnitude (at least `1e-9`).\n\t */\n\tepsilon?: number;\n\t/** Fall back to the polygon engine when the curve engine fails (default `true`). */\n\tfallback?: boolean;\n\t/** Flattening tolerance used by the polygon fallback (default 0.05). */\n\tfallbackTolerance?: number;\n\t/** Called with the reason whenever the polygon fallback is used. */\n\tonFallback?: (reason: string) => void;\n\t/**\n\t * Work budget of the curve–curve intersection search (flat sub-curve pairs per pair of\n\t * pieces, default 20,000). Exceeding it (nearly coincident curves) falls back.\n\t */\n\tsubdivisionBudget?: number;\n}\n\n/** Which engine produced a result, and why the curve engine was not used. */\nexport interface PathOpsReport {\n\tengine: 'curve' | 'polygon';\n\tfallbackReason?: string;\n\t/** Set when the input cannot be processed at all (non-finite coordinates); the result is empty. */\n\terror?: string;\n}\n\nexport interface PathBooleanResult extends PathOpsReport {\n\tpath: BezierPath;\n}\n\n/** One region of `dividePaths`, with the indices of the operands that cover it. */\nexport interface PathPiece {\n\tpath: BezierPath;\n\toperands: number[];\n}\n\nexport interface DividePathsResult extends PathOpsReport {\n\tpieces: PathPiece[];\n}\n\n/** Raised by the curve engine when it cannot produce a reliable arrangement. */\nexport class PathOpsError extends Error {\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t\tthis.name = 'PathOpsError';\n\t}\n}\n\n// ---------------------------------------------------------------------------\n// Operands\n// ---------------------------------------------------------------------------\n\ninterface NormalizedOperand {\n\tchains: BezierSegment[][];\n\tfillRule: PolygonFillRule;\n}\n\nfunction isOperandObject(input: PathOperandInput): input is PathOperand {\n\treturn !!input && typeof input === 'object' && !Array.isArray(input) && 'path' in input;\n}\n\nfunction normalizeOperand(input: PathOperandInput): NormalizedOperand {\n\tconst path = isOperandObject(input) ? input.path : input;\n\tconst fillRule = isOperandObject(input) ? input.fillRule ?? 'nonzero' : 'nonzero';\n\tconst chains: BezierSegment[][] = [];\n\tfor (const contour of pathToContours(path ?? '')) {\n\t\tconst chain = closeContour(contour, true);\n\t\tif (chain.length === 0) continue;\n\t\tfor (const seg of chain) {\n\t\t\tfor (const p of seg) {\n\t\t\t\tif (!Number.isFinite(p[0]) || !Number.isFinite(p[1])) throw new PathOpsError(NON_FINITE);\n\t\t\t}\n\t\t}\n\t\tchains.push(chain);\n\t}\n\treturn { chains, fillRule };\n}\n\nfunction scaleOf(operands: NormalizedOperand[]): number {\n\tlet max = 0;\n\tfor (const op of operands) {\n\t\tfor (const chain of op.chains) {\n\t\t\tfor (const seg of chain) {\n\t\t\t\tfor (const [x, y] of seg) max = Math.max(max, Math.abs(x), Math.abs(y));\n\t\t\t}\n\t\t}\n\t}\n\treturn Math.max(1, max);\n}\n\n// ---------------------------------------------------------------------------\n// Arrangement data\n// ---------------------------------------------------------------------------\n\ninterface Vertex {\n\tid: number;\n\tx: number;\n\ty: number;\n\tp: Point;\n}\n\n/** Merges points closer than `eps` (grid hash with neighbour lookup). */\nclass VertexPool {\n\treadonly vertices: Vertex[] = [];\n\tprivate readonly cells = new Map<string, number[]>();\n\tconstructor(private readonly eps: number) {}\n\n\tget(x: number, y: number): Vertex {\n\t\tconst cx = Math.floor(x / this.eps);\n\t\tconst cy = Math.floor(y / this.eps);\n\t\tlet best: Vertex | null = null;\n\t\tlet bestDist = Infinity;\n\t\tfor (let dx = -1; dx <= 1; dx++) {\n\t\t\tfor (let dy = -1; dy <= 1; dy++) {\n\t\t\t\tconst ids = this.cells.get(`${cx + dx},${cy + dy}`);\n\t\t\t\tif (!ids) continue;\n\t\t\t\tfor (const id of ids) {\n\t\t\t\t\tconst v = this.vertices[id];\n\t\t\t\t\tconst d = Math.hypot(v.x - x, v.y - y);\n\t\t\t\t\tif (d <= this.eps && d < bestDist) {\n\t\t\t\t\t\tbest = v;\n\t\t\t\t\t\tbestDist = d;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (best) return best;\n\t\tconst vertex: Vertex = { id: this.vertices.length, x, y, p: [x, y] };\n\t\tthis.vertices.push(vertex);\n\t\tconst key = `${cx},${cy}`;\n\t\tconst bucket = this.cells.get(key);\n\t\tif (bucket) bucket.push(vertex.id);\n\t\telse this.cells.set(key, [vertex.id]);\n\t\treturn vertex;\n\t}\n}\n\ninterface Source {\n\tseg: BezierSegment;\n\tline: boolean;\n}\n\ninterface Piece {\n\tpts: Point[];\n\ta: Vertex;\n\tb: Vertex;\n\toperand: number;\n\tsrc: number;\n\t/** Source parameters at `a` and `b` (`s0 < s1`). */\n\ts0: number;\n\ts1: number;\n\tdirty: boolean;\n\tminX: number;\n\tmaxX: number;\n\tminY: number;\n\tmaxY: number;\n}\n\nfunction hull(pts: readonly Point[]) {\n\tlet minX = Infinity;\n\tlet maxX = -Infinity;\n\tlet minY = Infinity;\n\tlet maxY = -Infinity;\n\tfor (const [x, y] of pts) {\n\t\tif (x < minX) minX = x;\n\t\tif (x > maxX) maxX = x;\n\t\tif (y < minY) minY = y;\n\t\tif (y > maxY) maxY = y;\n\t}\n\treturn { minX, maxX, minY, maxY };\n}\n\n/** Moves the end points of a segment onto two vertices, dragging the handles along. */\nfunction snapEnds(pts: readonly Point[], a: Point, b: Point): Point[] {\n\tconst n = pts.length;\n\tif (n === 2) return [a, b];\n\tconst d0 = sub(a, pts[0]);\n\tconst d1 = sub(b, pts[n - 1]);\n\tif (n === 3) {\n\t\treturn [a, [pts[1][0] + (d0[0] + d1[0]) / 2, pts[1][1] + (d0[1] + d1[1]) / 2], b];\n\t}\n\treturn [a, [pts[1][0] + d0[0], pts[1][1] + d0[1]], [pts[2][0] + d1[0], pts[2][1] + d1[1]], b];\n}\n\nfunction makePiece(\n\tpts: Point[],\n\ta: Vertex,\n\tb: Vertex,\n\toperand: number,\n\tsrc: number,\n\ts0: number,\n\ts1: number\n): Piece {\n\tconst snapped = snapEnds(pts, a.p, b.p);\n\treturn { pts: snapped, a, b, operand, src, s0, s1, dirty: true, ...hull(snapped) };\n}\n\n// ---------------------------------------------------------------------------\n// Intersections\n// ---------------------------------------------------------------------------\n\ninterface PairEvent {\n\ttp: number;\n\ttq: number;\n\tpoint: Point;\n\t/** Set for shared end points and end points lying on the other piece. */\n\tanchor?: boolean;\n\t/** Shared vertex: nothing to split. */\n\tshared?: boolean;\n}\n\ninterface IntersectContext {\n\teps: number;\n\tleaves: number;\n\tbudget: number;\n}\n\nconst NON_FINITE = 'non-finite coordinates';\n\nfunction flatness(pts: readonly Point[]): number {\n\tif (pts.length === 2) return 0;\n\tconst a = pts[0];\n\tconst b = pts[pts.length - 1];\n\tconst chord = sub(b, a);\n\tconst len = Math.hypot(chord[0], chord[1]);\n\tlet worst = 0;\n\tfor (let i = 1; i < pts.length - 1; i++) {\n\t\tconst v = sub(pts[i], a);\n\t\tconst d = len > 0 ? Math.abs(cross(chord, v)) / len : Math.hypot(v[0], v[1]);\n\t\tconst along = len > 0 ? dot(chord, v) / len : 0;\n\t\tconst over = Math.max(0, -along, along - len);\n\t\tworst = Math.max(worst, d, over);\n\t}\n\treturn worst;\n}\n\nfunction split2(pts: readonly Point[]): [Point[], Point[]] {\n\tconst n = pts.length;\n\tconst mid = (p: Point, q: Point): Point => [(p[0] + q[0]) / 2, (p[1] + q[1]) / 2];\n\tif (n === 2) {\n\t\tconst m = mid(pts[0], pts[1]);\n\t\treturn [\n\t\t\t[pts[0], m],\n\t\t\t[m, pts[1]]\n\t\t];\n\t}\n\tif (n === 3) {\n\t\tconst a = mid(pts[0], pts[1]);\n\t\tconst b = mid(pts[1], pts[2]);\n\t\tconst m = mid(a, b);\n\t\treturn [\n\t\t\t[pts[0], a, m],\n\t\t\t[m, b, pts[2]]\n\t\t];\n\t}\n\tconst a = mid(pts[0], pts[1]);\n\tconst b = mid(pts[1], pts[2]);\n\tconst c = mid(pts[2], pts[3]);\n\tconst ab = mid(a, b);\n\tconst bc = mid(b, c);\n\tconst m = mid(ab, bc);\n\treturn [\n\t\t[pts[0], a, ab, m],\n\t\t[m, bc, c, pts[3]]\n\t];\n}\n\n/** Closest points of two segments: parameters and distance. */\nfunction closestSegmentPoints(a0: Point, a1: Point, b0: Point, b1: Point): { s: number; u: number; d: number } {\n\tconst d1 = sub(a1, a0);\n\tconst d2 = sub(b1, b0);\n\tconst r = sub(a0, b0);\n\tconst a = dot(d1, d1);\n\tconst e = dot(d2, d2);\n\tconst f = dot(d2, r);\n\tlet s = 0;\n\tlet u = 0;\n\tif (a <= 1e-300 && e <= 1e-300) return { s: 0, u: 0, d: dist(a0, b0) };\n\tif (a <= 1e-300) {\n\t\tu = Math.max(0, Math.min(1, f / e));\n\t} else {\n\t\tconst c = dot(d1, r);\n\t\tif (e <= 1e-300) {\n\t\t\ts = Math.max(0, Math.min(1, -c / a));\n\t\t} else {\n\t\t\tconst b = dot(d1, d2);\n\t\t\tconst denom = a * e - b * b;\n\t\t\ts = denom > 0 ? Math.max(0, Math.min(1, (b * f - c * e) / denom)) : 0;\n\t\t\tu = (b * s + f) / e;\n\t\t\tif (u < 0) {\n\t\t\t\tu = 0;\n\t\t\t\ts = Math.max(0, Math.min(1, -c / a));\n\t\t\t} else if (u > 1) {\n\t\t\t\tu = 1;\n\t\t\t\ts = Math.max(0, Math.min(1, (b - c) / a));\n\t\t\t}\n\t\t}\n\t}\n\tconst pa: Point = [a0[0] + d1[0] * s, a0[1] + d1[1] * s];\n\tconst pb: Point = [b0[0] + d2[0] * u, b0[1] + d2[1] * u];\n\treturn { s, u, d: dist(pa, pb) };\n}\n\n/** Newton polishing of P(s) = Q(u). Returns the best parameters found. */\nfunction polish(P: readonly Point[], Q: readonly Point[], s: number, u: number): { s: number; u: number; d: number } {\n\tlet best = { s, u, d: dist(segmentPoint(P, s), segmentPoint(Q, u)) };\n\tfor (let i = 0; i < 10 && best.d > 0; i++) {\n\t\tconst f = sub(segmentPoint(P, s), segmentPoint(Q, u));\n\t\tconst dp = segmentDerivative(P, s);\n\t\tconst dq = segmentDerivative(Q, u);\n\t\t// Solve [dp, -dq] [ds, du]^T = -f\n\t\tconst det = cross(dp, [-dq[0], -dq[1]]);\n\t\tif (!(Math.abs(det) > 1e-300)) break;\n\t\tconst ds = cross([-f[0], -f[1]], [-dq[0], -dq[1]]) / det;\n\t\tconst du = cross(dp, [-f[0], -f[1]]) / det;\n\t\ts = Math.max(0, Math.min(1, s + ds));\n\t\tu = Math.max(0, Math.min(1, u + du));\n\t\tconst d = dist(segmentPoint(P, s), segmentPoint(Q, u));\n\t\tif (d < best.d) best = { s, u, d };\n\t\telse break;\n\t}\n\treturn best;\n}\n\nfunction intersectSubdivided(\n\tP: Point[],\n\tp0: number,\n\tp1: number,\n\tQ: Point[],\n\tq0: number,\n\tq1: number,\n\tfullP: readonly Point[],\n\tfullQ: readonly Point[],\n\tctx: IntersectContext,\n\tout: PairEvent[],\n\tdepth: number\n) {\n\tconst hp = hull(P);\n\tconst hq = hull(Q);\n\tconst e = ctx.eps;\n\tif (hp.minX > hq.maxX + e || hq.minX > hp.maxX + e || hp.minY > hq.maxY + e || hq.minY > hp.maxY + e) return;\n\tconst fp = flatness(P);\n\tconst fq = flatness(Q);\n\tconst flatTol = e * 0.5;\n\tif ((fp <= flatTol && fq <= flatTol) || depth >= 52) {\n\t\tif (++ctx.leaves > ctx.budget) throw new PathOpsError('curve intersection did not converge');\n\t\tconst a0 = P[0];\n\t\tconst a1 = P[P.length - 1];\n\t\tconst b0 = Q[0];\n\t\tconst b1 = Q[Q.length - 1];\n\t\tconst d1 = sub(a1, a0);\n\t\tconst d2 = sub(b1, b0);\n\t\tconst denom = cross(d1, d2);\n\t\tlet s = 0;\n\t\tlet u = 0;\n\t\tlet hit = false;\n\t\tconst l1 = Math.hypot(d1[0], d1[1]);\n\t\tconst l2 = Math.hypot(d2[0], d2[1]);\n\t\tif (Math.abs(denom) > 1e-12 * l1 * l2) {\n\t\t\tconst r = sub(b0, a0);\n\t\t\ts = cross(r, d2) / denom;\n\t\t\tu = cross(r, d1) / denom;\n\t\t\tconst ts = l1 > 0 ? e / l1 : 1;\n\t\t\tconst tu = l2 > 0 ? e / l2 : 1;\n\t\t\thit = s >= -ts && s <= 1 + ts && u >= -tu && u <= 1 + tu;\n\t\t}\n\t\tif (!hit) {\n\t\t\t// No crossing of the chords: a touch when they come within epsilon.\n\t\t\tconst c = closestSegmentPoints(a0, a1, b0, b1);\n\t\t\tif (c.d > e) return;\n\t\t\ts = c.s;\n\t\t\tu = c.u;\n\t\t}\n\t\ts = Math.max(0, Math.min(1, s));\n\t\tu = Math.max(0, Math.min(1, u));\n\t\tconst best = polish(fullP, fullQ, p0 + (p1 - p0) * s, q0 + (q1 - q0) * u);\n\t\tif (best.d > e * 2) return;\n\t\tconst a = segmentPoint(fullP, best.s);\n\t\tconst b = segmentPoint(fullQ, best.u);\n\t\tout.push({ tp: best.s, tq: best.u, point: [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2] });\n\t\treturn;\n\t}\n\tconst splitP = fp > flatTol;\n\tconst splitQ = fq > flatTol;\n\tconst pm = (p0 + p1) / 2;\n\tconst qm = (q0 + q1) / 2;\n\tif (splitP && splitQ) {\n\t\tconst [pa, pb] = split2(P);\n\t\tconst [qa, qb] = split2(Q);\n\t\tintersectSubdivided(pa, p0, pm, qa, q0, qm, fullP, fullQ, ctx, out, depth + 1);\n\t\tintersectSubdivided(pa, p0, pm, qb, qm, q1, fullP, fullQ, ctx, out, depth + 1);\n\t\tintersectSubdivided(pb, pm, p1, qa, q0, qm, fullP, fullQ, ctx, out, depth + 1);\n\t\tintersectSubdivided(pb, pm, p1, qb, qm, q1, fullP, fullQ, ctx, out, depth + 1);\n\t} else if (splitP) {\n\t\tconst [pa, pb] = split2(P);\n\t\tintersectSubdivided(pa, p0, pm, Q, q0, q1, fullP, fullQ, ctx, out, depth + 1);\n\t\tintersectSubdivided(pb, pm, p1, Q, q0, q1, fullP, fullQ, ctx, out, depth + 1);\n\t} else {\n\t\tconst [qa, qb] = split2(Q);\n\t\tintersectSubdivided(P, p0, p1, qa, q0, qm, fullP, fullQ, ctx, out, depth + 1);\n\t\tintersectSubdivided(P, p0, p1, qb, qm, q1, fullP, fullQ, ctx, out, depth + 1);\n\t}\n}\n\n/** A crossing or touch of two segments: the parameter on each and the point. */\nexport interface SegmentIntersection {\n\tta: number;\n\ttb: number;\n\tpoint: Point;\n}\n\n/**\n * Every point where two segments (lines, quadratics or cubics, as control points) cross or\n * touch, by the same subdivision search the boolean operations use: flat sub-curves, chord\n * intersection, Newton polishing. Hits closer than `epsilon` (default: 1e-9 of the segments'\n * size) are one. Coincident stretches report the hits the search finds along them.\n */\nexport function segmentIntersections(a: BezierSegment, b: BezierSegment, options: { epsilon?: number; budget?: number } = {}): SegmentIntersection[] {\n\tlet size = 1;\n\tfor (const [x, y] of [...a, ...b]) size = Math.max(size, Math.abs(x), Math.abs(y));\n\tconst eps = options.epsilon ?? size * 1e-9;\n\tconst found: PairEvent[] = [];\n\tconst ctx: IntersectContext = { eps, leaves: 0, budget: options.budget ?? 4096 };\n\ttry {\n\t\tintersectSubdivided(a.slice(), 0, 1, b.slice(), 0, 1, a, b, ctx, found, 0);\n\t} catch (error) {\n\t\tif (!(error instanceof PathOpsError)) throw error;\n\t}\n\tconst out: SegmentIntersection[] = [];\n\tfor (const ev of found.sort((x, y) => x.tp - y.tp)) {\n\t\tif (out.some((o) => dist(o.point, ev.point) <= eps * 4)) continue;\n\t\tout.push({ ta: ev.tp, tb: ev.tq, point: ev.point });\n\t}\n\treturn out;\n}\n\nfunction nearestOn(piece: Piece, p: Point, eps: number): { t: number; distance: number } | null {\n\tif (p[0] < piece.minX - eps || p[0] > piece.maxX + eps || p[1] < piece.minY - eps || p[1] > piece.maxY + eps) return null;\n\treturn segmentNearest(piece.pts, p);\n}\n\n/** Whether the two pieces stay within `tol` of each other between two events. */\nfunction staysClose(P: Piece, Q: Piece, e1: PairEvent, e2: PairEvent, tol: number): boolean {\n\tfor (const f of [0.25, 0.5, 0.75]) {\n\t\tconst tp = e1.tp + (e2.tp - e1.tp) * f;\n\t\tconst q = segmentNearest(Q.pts, segmentPoint(P.pts, tp));\n\t\tif (q.distance > tol) return false;\n\t}\n\treturn true;\n}\n\nfunction pairEvents(P: Piece, Q: Piece, ctx: IntersectContext): PairEvent[] {\n\tconst eps = ctx.eps;\n\tconst anchors: PairEvent[] = [];\n\t// Shared vertices.\n\tfor (const [va, tp] of [\n\t\t[P.a, 0],\n\t\t[P.b, 1]\n\t] as const) {\n\t\tif (va === Q.a) anchors.push({ tp, tq: 0, point: va.p, anchor: true, shared: true });\n\t\telse if (va === Q.b) anchors.push({ tp, tq: 1, point: va.p, anchor: true, shared: true });\n\t}\n\t// End points lying on the other piece (T-junctions, overlaps).\n\tfor (const [v, tq] of [\n\t\t[Q.a, 0],\n\t\t[Q.b, 1]\n\t] as const) {\n\t\tif (v === P.a || v === P.b) continue;\n\t\tconst near = nearestOn(P, v.p, eps);\n\t\tif (near && near.distance <= eps) anchors.push({ tp: near.t, tq, point: v.p, anchor: true });\n\t}\n\tfor (const [v, tp] of [\n\t\t[P.a, 0],\n\t\t[P.b, 1]\n\t] as const) {\n\t\tif (v === Q.a || v === Q.b) continue;\n\t\tconst near = nearestOn(Q, v.p, eps);\n\t\tif (near && near.distance <= eps) anchors.push({ tp, tq: near.t, point: v.p, anchor: true });\n\t}\n\n\t// Coincident overlap: two anchors with the pieces running together between them.\n\tif (anchors.length >= 2) {\n\t\tconst sorted = [...anchors].sort((x, y) => x.tp - y.tp);\n\t\tconst first = sorted[0];\n\t\tconst last = sorted[sorted.length - 1];\n\t\tif (dist(first.point, last.point) > eps && staysClose(P, Q, first, last, eps * 4)) {\n\t\t\treturn anchors.filter((a) => !a.shared);\n\t\t}\n\t}\n\n\tconst found: PairEvent[] = [];\n\tif (P.pts.length === 2 && Q.pts.length === 2) {\n\t\tconst a0 = P.pts[0];\n\t\tconst d1 = sub(P.pts[1], a0);\n\t\tconst b0 = Q.pts[0];\n\t\tconst d2 = sub(Q.pts[1], b0);\n\t\tconst denom = cross(d1, d2);\n\t\tconst l1 = Math.hypot(d1[0], d1[1]);\n\t\tconst l2 = Math.hypot(d2[0], d2[1]);\n\t\tif (Math.abs(denom) > 1e-12 * l1 * l2) {\n\t\t\tconst r = sub(b0, a0);\n\t\t\tconst s = cross(r, d2) / denom;\n\t\t\tconst u = cross(r, d1) / denom;\n\t\t\tconst ts = eps / l1;\n\t\t\tconst tu = eps / l2;\n\t\t\tif (s >= -ts && s <= 1 + ts && u >= -tu && u <= 1 + tu) {\n\t\t\t\tconst sc = Math.max(0, Math.min(1, s));\n\t\t\t\tconst uc = Math.max(0, Math.min(1, u));\n\t\t\t\tfound.push({ tp: sc, tq: uc, point: [a0[0] + d1[0] * sc, a0[1] + d1[1] * sc] });\n\t\t\t}\n\t\t}\n\t} else {\n\t\tctx.leaves = 0;\n\t\tintersectSubdivided(P.pts, 0, 1, Q.pts, 0, 1, P.pts, Q.pts, ctx, found, 0);\n\t}\n\tif (found.length === 0) return anchors.filter((a) => !a.shared);\n\n\t// Cluster hits along tangencies: consecutive events between which the pieces never\n\t// separate by more than a few epsilon are one contact.\n\tconst all = [...anchors, ...found].sort((x, y) => x.tp - y.tp || x.tq - y.tq);\n\tconst clusters: PairEvent[][] = [];\n\tfor (const ev of all) {\n\t\tconst cluster = clusters[clusters.length - 1];\n\t\tconst prev = cluster?.[cluster.length - 1];\n\t\tif (prev && (dist(prev.point, ev.point) <= eps * 2 || staysClose(P, Q, prev, ev, eps * 2))) cluster.push(ev);\n\t\telse clusters.push([ev]);\n\t}\n\tconst out: PairEvent[] = [];\n\tfor (const cluster of clusters) {\n\t\tconst shared = cluster.find((e) => e.shared);\n\t\tif (shared) continue;\n\t\tconst anchor = cluster.find((e) => e.anchor);\n\t\tout.push(anchor ?? cluster[Math.floor(cluster.length / 2)]);\n\t}\n\treturn out;\n}\n\ninterface SplitPoint {\n\tt: number;\n\tv: Vertex;\n}\n\nfunction addSplit(map: Map<Piece, SplitPoint[]>, piece: Piece, t: number, v: Vertex) {\n\tif (v === piece.a || v === piece.b) return;\n\tconst list = map.get(piece);\n\tif (list) list.push({ t, v });\n\telse map.set(piece, [{ t, v }]);\n}\n\n/** Resolves an event to a vertex, preferring the pieces' own end points. */\nfunction eventVertex(P: Piece, Q: Piece, ev: PairEvent, pool: VertexPool, eps: number): Vertex {\n\tfor (const v of [P.a, P.b, Q.a, Q.b]) if (dist(v.p, ev.point) <= eps) return v;\n\treturn pool.get(ev.point[0], ev.point[1]);\n}\n\nfunction splitPass(pieces: Piece[], pool: VertexPool, ctx: IntersectContext): { pieces: Piece[]; changed: boolean } {\n\tconst eps = ctx.eps;\n\tconst order = pieces.map((_, i) => i).sort((i, j) => pieces[i].minX - pieces[j].minX);\n\tconst splits = new Map<Piece, SplitPoint[]>();\n\tfor (let oi = 0; oi < order.length; oi++) {\n\t\tconst P = pieces[order[oi]];\n\t\tfor (let oj = oi + 1; oj < order.length; oj++) {\n\t\t\tconst Q = pieces[order[oj]];\n\t\t\tif (Q.minX > P.maxX + eps) break;\n\t\t\tif (!P.dirty && !Q.dirty) continue;\n\t\t\tif (Q.minY > P.maxY + eps || Q.maxY < P.minY - eps) continue;\n\t\t\tif (P.a === Q.a && P.b === Q.b && P.pts.length === 2 && Q.pts.length === 2) continue;\n\t\t\tif (P.a === Q.b && P.b === Q.a && P.pts.length === 2 && Q.pts.length === 2) continue;\n\t\t\tfor (const ev of pairEvents(P, Q, ctx)) {\n\t\t\t\tconst v = eventVertex(P, Q, ev, pool, eps);\n\t\t\t\taddSplit(splits, P, ev.tp, v);\n\t\t\t\taddSplit(splits, Q, ev.tq, v);\n\t\t\t}\n\t\t}\n\t}\n\tfor (const p of pieces) p.dirty = false;\n\tif (splits.size === 0) return { pieces, changed: false };\n\tconst out: Piece[] = [];\n\tfor (const piece of pieces) {\n\t\tconst list = splits.get(piece);\n\t\tif (!list) {\n\t\t\tout.push(piece);\n\t\t\tcontinue;\n\t\t}\n\t\tlist.sort((x, y) => x.t - y.t);\n\t\tlet prevT = 0;\n\t\tlet prevV = piece.a;\n\t\tconst range = piece.s1 - piece.s0;\n\t\tconst emit = (t: number, v: Vertex) => {\n\t\t\tif (v === prevV || t <= prevT) return;\n\t\t\tconst pts = subSegment(piece.pts, prevT, t);\n\t\t\tout.push(makePiece(pts, prevV, v, piece.operand, piece.src, piece.s0 + range * prevT, piece.s0 + range * t));\n\t\t\tprevT = t;\n\t\t\tprevV = v;\n\t\t};\n\t\tconst seen = new Set<Vertex>();\n\t\tfor (const { t, v } of list) {\n\t\t\tif (seen.has(v) || t <= 0 || t >= 1) continue;\n\t\t\tseen.add(v);\n\t\t\temit(t, v);\n\t\t}\n\t\tif (prevV !== piece.b) {\n\t\t\tconst pts = subSegment(piece.pts, prevT, 1);\n\t\t\tout.push(makePiece(pts, prevV, piece.b, piece.operand, piece.src, piece.s0 + range * prevT, piece.s1));\n\t\t}\n\t}\n\treturn { pieces: out, changed: true };\n}\n\n// ---------------------------------------------------------------------------\n// Edges and classification\n// ---------------------------------------------------------------------------\n\ninterface Edge {\n\ta: Vertex;\n\tb: Vertex;\n\t/** Control points from `a` to `b`. */\n\tpts: Point[];\n\tmult: number[];\n\tsrc: number;\n\t/** Source parameters at `a` and at `b`. */\n\tsa: number;\n\tsb: number;\n\tminX: number;\n\tmaxX: number;\n\tminY: number;\n\tmaxY: number;\n}\n\ninterface Arrangement {\n\tedges: Edge[];\n\tleft: number[][];\n\tright: number[][];\n\tfillRules: PolygonFillRule[];\n\tsources: Source[];\n\teps: number;\n\tscale: number;\n}\n\nfunction buildArrangement(inputs: PathOperandInput[], options: PathOpsOptions): Arrangement {\n\tconst operands = inputs.map(normalizeOperand);\n\tconst scale = scaleOf(operands);\n\tconst eps = options.epsilon && options.epsilon > 0 ? options.epsilon : 1e-9 * scale;\n\tconst pool = new VertexPool(eps);\n\tconst sources: Source[] = [];\n\tlet pieces: Piece[] = [];\n\toperands.forEach((op, operand) => {\n\t\tfor (const chain of op.chains) {\n\t\t\tfor (const raw of chain) {\n\t\t\t\tconst straight = raw.length > 2 && isStraightSegment(raw, eps * 0.01);\n\t\t\t\tconst seg: BezierSegment = straight ? [raw[0], raw[raw.length - 1]] : raw;\n\t\t\t\tconst src = sources.length;\n\t\t\t\tsources.push({ seg, line: seg.length === 2 });\n\t\t\t\tconst ts = [0, ...monotoneParams(seg), 1];\n\t\t\t\tfor (let i = 0; i + 1 < ts.length; i++) {\n\t\t\t\t\tconst pts = subSegment(seg, ts[i], ts[i + 1]);\n\t\t\t\t\tconst a = pool.get(pts[0][0], pts[0][1]);\n\t\t\t\t\tconst b = pool.get(pts[pts.length - 1][0], pts[pts.length - 1][1]);\n\t\t\t\t\tif (a === b) continue;\n\t\t\t\t\tpieces.push(makePiece(pts, a, b, operand, src, ts[i], ts[i + 1]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t});\n\n\tconst ctx: IntersectContext = { eps, leaves: 0, budget: options.subdivisionBudget ?? 20000 };\n\tlet settled = false;\n\tfor (let pass = 0; pass < 6; pass++) {\n\t\tconst result = splitPass(pieces, pool, ctx);\n\t\tpieces = result.pieces;\n\t\tif (!result.changed) {\n\t\t\tsettled = true;\n\t\t\tbreak;\n\t\t}\n\t}\n\tif (!settled) throw new PathOpsError('intersections did not settle');\n\n\t// Merge coincident pieces.\n\tconst count = operands.length;\n\tconst groups = new Map<string, Edge[]>();\n\tconst edges: Edge[] = [];\n\tfor (const piece of pieces) {\n\t\tconst forward = piece.a.id < piece.b.id;\n\t\tconst a = forward ? piece.a : piece.b;\n\t\tconst b = forward ? piece.b : piece.a;\n\t\tconst pts = forward ? piece.pts : [...piece.pts].reverse();\n\t\tconst key = `${a.id},${b.id}`;\n\t\tconst group = groups.get(key) ?? [];\n\t\tif (!groups.has(key)) groups.set(key, group);\n\t\tconst mid = segmentPoint(pts, 0.5);\n\t\tlet edge = group.find((e) => {\n\t\t\tif (e.pts.length === 2 && pts.length === 2) return true;\n\t\t\treturn (\n\t\t\t\tsegmentNearest(e.pts, mid).distance <= eps * 4 &&\n\t\t\t\tsegmentNearest(pts, segmentPoint(e.pts, 0.5)).distance <= eps * 4\n\t\t\t);\n\t\t});\n\t\tif (!edge) {\n\t\t\tedge = {\n\t\t\t\ta,\n\t\t\t\tb,\n\t\t\t\tpts,\n\t\t\t\tmult: new Array(count).fill(0),\n\t\t\t\tsrc: piece.src,\n\t\t\t\tsa: forward ? piece.s0 : piece.s1,\n\t\t\t\tsb: forward ? piece.s1 : piece.s0,\n\t\t\t\t...hull(pts)\n\t\t\t};\n\t\t\tgroup.push(edge);\n\t\t\tedges.push(edge);\n\t\t}\n\t\tedge.mult[piece.operand] += forward ? 1 : -1;\n\t}\n\tconst live = edges.filter((e) => e.mult.some((m) => m !== 0));\n\n\t// Winding numbers on both sides of every edge. Rays only meet edges whose y range (x\n\t// range for vertical rays) holds the ray, so edges are bucketed into bands on each axis.\n\tconst byY = new BandIndex(live, (e) => e.minY, (e) => e.maxY);\n\tconst byX = new BandIndex(live, (e) => e.minX, (e) => e.maxX);\n\tconst right: number[][] = [];\n\tconst left: number[][] = [];\n\tfor (let i = 0; i < live.length; i++) {\n\t\tconst edge = live[i];\n\t\tconst mid = segmentPoint(edge.pts, 0.5);\n\t\tconst tan = segmentTangent(edge.pts, 0.5);\n\t\tconst horizontal = Math.abs(tan[1]) < Math.abs(tan[0]);\n\t\tconst winding = new Array(count).fill(0);\n\t\tconst [mx, my] = mid;\n\t\tfor (const j of horizontal ? byX.at(mx) : byY.at(my)) {\n\t\t\tif (j === i) continue;\n\t\t\tconst o = live[j];\n\t\t\tlet dir = 0;\n\t\t\tif (!horizontal) {\n\t\t\t\t// Ray towards +x.\n\t\t\t\tif (o.maxX <= mx) continue;\n\t\t\t\tconst ay = o.a.y;\n\t\t\t\tconst by = o.b.y;\n\t\t\t\tif (ay <= my && by > my) dir = 1;\n\t\t\t\telse if (by <= my && ay > my) dir = -1;\n\t\t\t\telse continue;\n\t\t\t\tif (o.minX <= mx) {\n\t\t\t\t\tconst t = solveMonotone(o.pts, 1, my);\n\t\t\t\t\tif (!(segmentPoint(o.pts, t)[0] > mx)) continue;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// Ray towards +y, in the frame rotated by −90° ((x, y) → (y, −x)).\n\t\t\t\tif (o.maxY <= my) continue;\n\t\t\t\tconst ax = o.a.x;\n\t\t\t\tconst bx = o.b.x;\n\t\t\t\tif (bx < mx && mx <= ax) dir = 1;\n\t\t\t\telse if (ax < mx && mx <= bx) dir = -1;\n\t\t\t\telse continue;\n\t\t\t\tif (o.minY <= my) {\n\t\t\t\t\tconst t = solveMonotone(o.pts, 0, mx);\n\t\t\t\t\tif (!(segmentPoint(o.pts, t)[1] > my)) continue;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (let k = 0; k < count; k++) if (o.mult[k] !== 0) winding[k] += dir * o.mult[k];\n\t\t}\n\t\tconst sideIsRight = horizontal ? edge.b.x < edge.a.x : edge.b.y > edge.a.y;\n\t\tconst r = sideIsRight ? winding : winding.map((w, k) => w - edge.mult[k]);\n\t\tright.push(r);\n\t\tleft.push(r.map((w, k) => w + edge.mult[k]));\n\t}\n\treturn { edges: live, left, right, fillRules: operands.map((o) => o.fillRule), sources, eps, scale };\n}\n\n/** Edge indices bucketed into equal bands of one coordinate. */\nclass BandIndex {\n\tprivate readonly bands: number[][];\n\tprivate readonly min: number;\n\tprivate readonly size: number;\n\tconstructor(edges: Edge[], lo: (e: Edge) => number, hi: (e: Edge) => number) {\n\t\tlet min = Infinity;\n\t\tlet max = -Infinity;\n\t\tfor (const e of edges) {\n\t\t\tmin = Math.min(min, lo(e));\n\t\t\tmax = Math.max(max, hi(e));\n\t\t}\n\t\tconst count = Math.max(1, Math.min(4096, Math.ceil(Math.sqrt(edges.length) * 2)));\n\t\tthis.min = min;\n\t\tthis.size = max > min ? (max - min) / count : 1;\n\t\tthis.bands = Array.from({ length: count }, () => []);\n\t\tedges.forEach((e, i) => {\n\t\t\tconst from = this.index(lo(e));\n\t\t\tconst to = this.index(hi(e));\n\t\t\tfor (let b = from; b <= to; b++) this.bands[b].push(i);\n\t\t});\n\t}\n\tprivate index(v: number): number {\n\t\tconst k = Math.floor((v - this.min) / this.size);\n\t\treturn Math.max(0, Math.min(this.bands.length - 1, Number.isFinite(k) ? k : 0));\n\t}\n\tat(v: number): readonly number[] {\n\t\treturn this.bands[this.index(v)] ?? [];\n\t}\n}\n\nfunction insideFlags(windings: number[], fillRules: PolygonFillRule[]): boolean[] {\n\treturn windings.map((w, k) => (fillRules[k] === 'evenodd' ? (w & 1) === 1 : w !== 0));\n}\n\n// ---------------------------------------------------------------------------\n// Tracing and reconstruction\n// ---------------------------------------------------------------------------\n\ninterface DirectedEdge {\n\tfrom: Vertex;\n\tto: Vertex;\n\tpts: Point[];\n\tsrc: number;\n\tsFrom: number;\n\tsTo: number;\n\toutAngle: number;\n\tbackAngle: number;\n\tlength: number;\n}\n\nconst TWO_PI = Math.PI * 2;\nconst TIE = 1e-3;\n\nfunction directed(edge: Edge, forward: boolean): DirectedEdge {\n\tconst pts = forward ? edge.pts : [...edge.pts].reverse();\n\tconst out = segmentTangent(pts, 0);\n\tconst end = segmentTangent(pts, 1);\n\treturn {\n\t\tfrom: forward ? edge.a : edge.b,\n\t\tto: forward ? edge.b : edge.a,\n\t\tpts,\n\t\tsrc: edge.src,\n\t\tsFrom: forward ? edge.sa : edge.sb,\n\t\tsTo: forward ? edge.sb : edge.sa,\n\t\toutAngle: Math.atan2(out[1], out[0]),\n\t\tbackAngle: Math.atan2(-end[1], -end[0]),\n\t\tlength: dist(pts[0], pts[pts.length - 1]) + (pts.length > 2 ? flatness(pts) : 0)\n\t};\n}\n\n/** Angle of the chord from the start of `pts` to the point at fraction `t` (by parameter). */\nfunction chordAngle(pts: readonly Point[], t: number): number {\n\tconst p = segmentPoint(pts, t);\n\treturn Math.atan2(p[1] - pts[0][1], p[0] - pts[0][0]);\n}\n\nfunction mod2pi(a: number): number {\n\tconst r = a - Math.floor(a / TWO_PI) * TWO_PI;\n\treturn r >= TWO_PI ? 0 : r;\n}\n\n/** Traces the kept directed edges (result on the left) into rings. */\nfunction traceRings(edges: DirectedEdge[], scale: number): DirectedEdge[][] {\n\tconst outgoing = new Map<number, DirectedEdge[]>();\n\tfor (const e of edges) {\n\t\tconst list = outgoing.get(e.from.id);\n\t\tif (list) list.push(e);\n\t\telse outgoing.set(e.from.id, [e]);\n\t}\n\tconst next = (incoming: DirectedEdge): DirectedEdge | undefined => {\n\t\tconst candidates = outgoing.get(incoming.to.id)?.filter((c) => !used.has(c) || c === start);\n\t\tif (!candidates || candidates.length === 0) return undefined;\n\t\tif (candidates.length === 1) return candidates[0];\n\t\tconst turns = candidates.map((c) => mod2pi(incoming.backAngle - c.outAngle));\n\t\tconst tied = (x: number) => x < TIE || x > TWO_PI - TIE;\n\t\tlet ambiguous = turns.some(tied);\n\t\tif (!ambiguous) {\n\t\t\tconst sorted = [...turns].sort((a, b) => a - b);\n\t\t\tfor (let i = 1; i < sorted.length; i++) if (sorted[i] - sorted[i - 1] < TIE) ambiguous = true;\n\t\t}\n\t\tif (ambiguous) {\n\t\t\t// Tangent directions tie (tangent curves): compare chords a short way along each\n\t\t\t// curve instead, which separates curves by their curvature.\n\t\t\tconst back = [...incoming.pts].reverse();\n\t\t\tlet delta = Math.min(incoming.length, ...candidates.map((c) => c.length));\n\t\t\tdelta = Math.min(delta * 0.25, scale * 0.02);\n\t\t\tconst frac = (e: DirectedEdge) => Math.min(1, Math.max(1e-6, delta / Math.max(e.length, 1e-300)));\n\t\t\tconst backChord = chordAngle(back, frac(incoming));\n\t\t\tfor (let i = 0; i < candidates.length; i++) {\n\t\t\t\tturns[i] = mod2pi(backChord - chordAngle(candidates[i].pts, frac(candidates[i])));\n\t\t\t\tif (turns[i] < 1e-12) turns[i] = TWO_PI;\n\t\t\t}\n\t\t} else {\n\t\t\tfor (let i = 0; i < turns.length; i++) if (turns[i] < 1e-15) turns[i] = TWO_PI;\n\t\t}\n\t\tlet best = 0;\n\t\tfor (let i = 1; i < candidates.length; i++) if (turns[i] < turns[best]) best = i;\n\t\treturn candidates[best];\n\t};\n\n\tconst used = new Set<DirectedEdge>();\n\tconst rings: DirectedEdge[][] = [];\n\tlet start: DirectedEdge | null = null;\n\tfor (const first of edges) {\n\t\tif (used.has(first)) continue;\n\t\tstart = first;\n\t\tconst ring: DirectedEdge[] = [];\n\t\tlet edge: DirectedEdge | undefined = first;\n\t\tlet guard = edges.length + 1;\n\t\tlet closed = false;\n\t\twhile (edge && guard-- > 0) {\n\t\t\tused.add(edge);\n\t\t\tring.push(edge);\n\t\t\tedge = next(edge);\n\t\t\tif (edge === first) {\n\t\t\t\tclosed = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (!closed) throw new PathOpsError('result outline does not close');\n\t\trings.push(ring);\n\t}\n\treturn rings;\n}\n\nfunction mergeable(a: DirectedEdge, b: DirectedEdge, sources: Source[], eps: number): boolean {\n\tif (a.src === b.src && Math.abs(a.sTo - b.sFrom) < 1e-9 && a.sTo - a.sFrom > 0 === b.sTo - b.sFrom > 0) return true;\n\tif (a.pts.length === 2 && b.pts.length === 2) {\n\t\t// Collinear lines from different sources (e.g. shared rectangle sides).\n\t\tconst p = a.pts[0];\n\t\tconst q = b.pts[1];\n\t\tconst m = a.pts[1];\n\t\tconst chord = sub(q, p);\n\t\tconst len = Math.hypot(chord[0], chord[1]);\n\t\tif (len === 0) return false;\n\t\tconst off = Math.abs(cross(chord, sub(m, p))) / len;\n\t\tconst along = dot(chord, sub(m, p));\n\t\treturn off <= eps && along > 0 && along < len * len;\n\t}\n\tvoid sources;\n\treturn false;\n}\n\n/** Rebuilds a ring as segments: pieces of one source become one exact sub-curve. */\nfunction rebuildRing(ring: DirectedEdge[], sources: Source[], eps: number): BezierSegment[] {\n\tconst n = ring.length;\n\tlet startIndex = 0;\n\tfor (let k = 0; k < n; k++) {\n\t\tif (!mergeable(ring[(k + n - 1) % n], ring[k], sources, eps)) {\n\t\t\tstartIndex = k;\n\t\t\tbreak;\n\t\t}\n\t}\n\tconst ordered = [...ring.slice(startIndex), ...ring.slice(0, startIndex)];\n\tconst out: BezierSegment[] = [];\n\tlet i = 0;\n\twhile (i < ordered.length) {\n\t\tconst first = ordered[i];\n\t\tlet j = i + 1;\n\t\twhile (j < ordered.length && mergeable(ordered[j - 1], ordered[j], sources, eps)) j++;\n\t\tconst last = ordered[j - 1];\n\t\tif (j - i === 1) {\n\t\t\tout.push(first.pts);\n\t\t} else if (first.pts.length === 2 && ordered.slice(i, j).every((e) => e.pts.length === 2)) {\n\t\t\tout.push([first.from.p, last.to.p]);\n\t\t} else {\n\t\t\tconst source = sources[first.src];\n\t\t\tout.push(snapEnds(subSegment(source.seg, first.sFrom, last.sTo), first.from.p, last.to.p));\n\t\t}\n\t\ti = j;\n\t}\n\treturn out;\n}\n\ninterface Ring {\n\tsegs: BezierSegment[];\n\tarea: number;\n}\n\nfunction extractRings(arr: Arrangement, isInside: (flags: boolean[]) => boolean): Ring[] {\n\tconst kept: DirectedEdge[] = [];\n\tarr.edges.forEach((edge, i) => {\n\t\tconst l = isInside(insideFlags(arr.left[i], arr.fillRules));\n\t\tconst r = isInside(insideFlags(arr.right[i], arr.fillRules));\n\t\tif (l === r) return;\n\t\tkept.push(directed(edge, l));\n\t});\n\tconst rings: Ring[] = [];\n\tconst sliver = Math.max(arr.eps * 10, 1e-7 * arr.scale);\n\tfor (const ring of traceRings(kept, arr.scale)) {\n\t\tconst segs = rebuildRing(ring, arr.sources, arr.eps);\n\t\tconst area = contourSignedArea(segs);\n\t\tlet perimeter = 0;\n\t\tfor (const s of segs) perimeter += s.length === 2 ? dist(s[0], s[1]) : segmentLength(s, 0, 1, 1e-6);\n\t\tif (!(perimeter > 0) || (2 * Math.abs(area)) / perimeter < sliver) continue;\n\t\trings.push({ segs, area });\n\t}\n\treturn rings;\n}\n\n/** A point inside the region just left of a ring (for hole assignment). */\nfunction ringSample(ring: Ring): Point {\n\t// The midpoint of the longest segment: never a vertex shared with another ring.\n\tlet best = ring.segs[0];\n\tlet bestLen = -1;\n\tfor (const s of ring.segs) {\n\t\tconst l = dist(s[0], s[s.length - 1]);\n\t\tif (l > bestLen) {\n\t\t\tbest = s;\n\t\t\tbestLen = l;\n\t\t}\n\t}\n\treturn segmentPoint(best, 0.5);\n}\n\n/** Groups rings into outer rings with their holes (counter-clockwise outers in a y-up frame). */\nfunction groupRings(rings: Ring[]): Ring[][] {\n\tconst outers = rings.filter((r) => r.area > 0).sort((a, b) => b.area - a.area);\n\tconst holes = rings.filter((r) => r.area < 0);\n\tconst groups: Ring[][] = outers.map((o) => [o]);\n\tfor (const hole of holes) {\n\t\tconst sample = ringSample(hole);\n\t\tfor (let i = outers.length - 1; i >= 0; i--) {\n\t\t\tif (outers[i].area >= -hole.area && windingNumber([outers[i].segs], sample) !== 0) {\n\t\t\t\tgroups[i].push(hole);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\treturn groups;\n}\n\nfunction ringsToPath(rings: Ring[]): BezierPath {\n\treturn contoursToPath(rings.map((r) => ({ segments: r.segs, closed: true })));\n}\n\nfunction predicate(operation: PathBooleanOperation): (flags: boolean[]) => boolean {\n\tswitch (operation) {\n\t\tcase 'union':\n\t\t\treturn (f) => f.some(Boolean);\n\t\tcase 'intersection':\n\t\t\treturn (f) => f.length > 0 && f.every(Boolean);\n\t\tcase 'difference':\n\t\t\treturn (f) => f[0] === true && !f.slice(1).some(Boolean);\n\t\tcase 'xor':\n\t\t\treturn (f) => f.filter(Boolean).length % 2 === 1;\n\t}\n}\n\n// ---------------------------------------------------------------------------\n// Polygon fallback\n// ---------------------------------------------------------------------------\n\nfunction flattenOperand(input: PathOperandInput, tolerance: number) {\n\tconst path = isOperandObject(input) ? input.path : input;\n\tconst fillRule = isOperandObject(input) ? input.fillRule ?? 'nonzero' : 'nonzero';\n\tconst rings = pathToContours(path ?? '')\n\t\t.map((c) => flattenChain(closeContour(c, true), tolerance))\n\t\t.filter((r) => r.length >= 3 && r.every((p) => Number.isFinite(p[0]) && Number.isFinite(p[1])));\n\treturn { rings, fillRule };\n}\n\nfunction multiToPath(multi: MultiPolygon, tolerance: number): BezierPath {\n\tif (multi.length === 0) return [];\n\treturn toBezierPath(polygonToSvgPath(multi, { curves: 'fit', curveTolerance: tolerance, precision: 9 }));\n}\n\nfunction reasonOf(error: unknown): string {\n\treturn error instanceof Error ? error.message : String(error);\n}\n\n// ---------------------------------------------------------------------------\n// Public operations\n// ---------------------------------------------------------------------------\n\n/**\n * Applies a boolean operation to any number of path operands, keeping curves:\n * - `union`: covered by any operand.\n * - `intersection`: covered by every operand.\n * - `difference`: the first operand minus all others.\n * - `xor`: covered by an odd number of operands (Exclude).\n *\n * Open subpaths are closed for filling. Non-finite input gives an empty path (`error`); with\n * `fallback: false` a failure of the curve engine throws a `PathOpsError`. The result is a set of simple closed subpaths that\n * never cross (they may touch at points): outer subpaths run counter-clockwise in a y-up\n * frame (clockwise on screen), holes the other way, so it fills the same with `nonzero`\n * and `evenodd`. Segments of the inputs are kept as exact sub-curves.\n */\nexport function pathBoolean(\n\toperation: PathBooleanOperation,\n\toperands: PathOperandInput[],\n\toptions: PathOpsOptions = {}\n): PathBooleanResult {\n\tif (operands.length === 0) return { path: [], engine: 'curve' };\n\ttry {\n\t\tconst arr = buildArrangement(operands, options);\n\t\treturn { path: ringsToPath(extractRings(arr, predicate(operation))), engine: 'curve' };\n\t} catch (error) {\n\t\tconst reason = reasonOf(error);\n\t\tif (reason === NON_FINITE) return { path: [], engine: 'curve', error: reason };\n\t\tif (options.fallback === false) throw error;\n\t\toptions.onFallback?.(reason);\n\t\tconst tolerance = options.fallbackTolerance ?? 0.05;\n\t\tconst multi = polygonBoolean(\n\t\t\toperation,\n\t\t\toperands.map((o) => flattenOperand(o, tolerance)),\n\t\t\t{ epsilon: options.epsilon }\n\t\t);\n\t\treturn { path: multiToPath(multi, tolerance), engine: 'polygon', fallbackReason: reason };\n\t}\n}\n\nexport const unitePaths = (a: PathOperandInput, b: PathOperandInput, options?: PathOpsOptions) =>\n\tpathBoolean('union', [a, b], options).path;\nexport const intersectPaths = (a: PathOperandInput, b: PathOperandInput, options?: PathOpsOptions) =>\n\tpathBoolean('intersection', [a, b], options).path;\nexport const subtractPaths = (a: PathOperandInput, b: PathOperandInput, options?: PathOpsOptions) =>\n\tpathBoolean('difference', [a, b], options).path;\nexport const excludePaths = (a: PathOperandInput, b: PathOperandInput, options?: PathOpsOptions) =>\n\tpathBoolean('xor', [a, b], options).path;\n\n/**\n * Splits the arrangement of the operands into its regions (Divide): every connected area\n * covered by the same set of operands becomes one piece (an outer subpath with its holes).\n * Pieces are ordered by operand set, then by area (largest first).\n */\nexport function dividePaths(operands: PathOperandInput[], options: PathOpsOptions = {}): DividePathsResult {\n\tif (operands.length === 0) return { pieces: [], engine: 'curve' };\n\ttry {\n\t\tconst arr = buildArrangement(operands, options);\n\t\tconst signatures = new Map<string, number[]>();\n\t\tconst note = (windings: number[]) => {\n\t\t\tconst flags = insideFlags(windings, arr.fillRules);\n\t\t\tif (!flags.some(Boolean)) return;\n\t\t\tconst key = flags.map((f) => (f ? '1' : '0')).join('');\n\t\t\tif (!signatures.has(key)) signatures.set(key, flags.flatMap((f, k) => (f ? [k] : [])));\n\t\t};\n\t\tarr.edges.forEach((_, i) => {\n\t\t\tnote(arr.left[i]);\n\t\t\tnote(arr.right[i]);\n\t\t});\n\t\tconst pieces: PathPiece[] = [];\n\t\tfor (const key of [...signatures.keys()].sort()) {\n\t\t\tconst rings = extractRings(arr, (flags) => flags.map((f) => (f ? '1' : '0')).join('') === key);\n\t\t\tconst groups = groupRings(rings).sort((a, b) => b[0].area - a[0].area);\n\t\t\tfor (const group of groups) pieces.push({ path: ringsToPath(group), operands: signatures.get(key)! });\n\t\t}\n\t\treturn { pieces, engine: 'curve' };\n\t} catch (error) {\n\t\tconst reason = reasonOf(error);\n\t\tif (reason === NON_FINITE) return { pieces: [], engine: 'curve', error: reason };\n\t\tif (options.fallback === false) throw error;\n\t\toptions.onFallback?.(reason);\n\t\tconst tolerance = options.fallbackTolerance ?? 0.05;\n\t\tconst pieces = dividePolygons(\n\t\t\toperands.map((o) => flattenOperand(o, tolerance)),\n\t\t\t{ epsilon: options.epsilon }\n\t\t).map((p) => ({ path: multiToPath([p.polygon], tolerance), operands: p.operands }));\n\t\treturn { pieces, engine: 'polygon', fallbackReason: reason };\n\t}\n}\n\n/**\n * Resolves self-intersections and overlaps of one filled path into simple, consistently\n * oriented subpaths (outer subpaths and holes) under its fill rule.\n */\nexport function normalizeBezierPath(\n\tpath: BezierPathInput,\n\tfillRule: PolygonFillRule = 'nonzero',\n\toptions: PathOpsOptions = {}\n): BezierPath {\n\treturn pathBoolean('union', [{ path, fillRule }], options).path;\n}\n\n/** Filled area of a path under its fill rule (overlaps counted once; exact for curves). */\nexport function bezierPathArea(path: BezierPathInput, fillRule: PolygonFillRule = 'nonzero', options: PathOpsOptions = {}): number {\n\tconst normalized = normalizeBezierPath(path, fillRule, options);\n\tlet sum = 0;\n\tfor (const contour of pathToContours(normalized)) sum += contourSignedArea(closeContour(contour, true));\n\treturn Math.abs(sum);\n}\n\n/**\n * Normalised rings of a filled path with their nesting: used by offset / outline stroke to\n * drop spurious contours. `parent` is the index of the smallest enclosing ring.\n */\nexport interface NestedRing {\n\tsegments: BezierSegment[];\n\tarea: number;\n\tparent: number;\n}\n\n/** Internal: rings of a boolean result with their containment tree. */\nexport function pathBooleanRings(\n\toperation: PathBooleanOperation | ((flags: boolean[]) => boolean),\n\toperands: PathOperandInput[],\n\toptions: PathOpsOptions = {}\n): NestedRing[] {\n\tconst arr = buildArrangement(operands, options);\n\tconst rings = extractRings(arr, typeof operation === 'function' ? operation : predicate(operation));\n\tconst order = rings.map((_, i) => i).sort((a, b) => Math.abs(rings[a].area) - Math.abs(rings[b].area));\n\treturn rings.map((ring, i) => {\n\t\tconst sample = ringSample(ring);\n\t\tlet parent = -1;\n\t\tfor (const j of order) {\n\t\t\tif (j === i || Math.abs(rings[j].area) <= Math.abs(ring.area)) continue;\n\t\t\tif (windingNumber([rings[j].segs], sample) !== 0) {\n\t\t\t\tparent = j;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\treturn { segments: ring.segs, area: ring.area, parent };\n\t});\n}\n\n/** Contours of nested rings, keeping the ones `keep` accepts. */\nexport function nestedRingsToPath(rings: NestedRing[], keep: (index: number) => boolean = () => true): BezierPath {\n\tconst contours: BezierContour[] = [];\n\trings.forEach((r, i) => {\n\t\tif (keep(i)) contours.push({ segments: r.segments, closed: true });\n\t});\n\treturn contoursToPath(contours);\n}\n"]}
|