@open-pencil/scene-graph 0.17.0-beta.4 → 0.17.0-beta.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/buffered-events.js +1 -1
- package/dist/buffered-events.js.map +1 -1
- package/dist/clipping.d.ts +7 -1
- package/dist/clipping.d.ts.map +1 -1
- package/dist/clipping.js +23 -19
- package/dist/clipping.js.map +1 -1
- package/dist/comments/schema.d.ts.map +1 -1
- package/dist/comments/schema.js +1 -0
- package/dist/comments/schema.js.map +1 -1
- package/dist/comments/threads.d.ts +2 -0
- package/dist/comments/threads.d.ts.map +1 -1
- package/dist/comments/threads.js +11 -5
- package/dist/comments/threads.js.map +1 -1
- package/dist/components/index.d.ts +2 -2
- package/dist/components/index.js +2 -2
- package/dist/components/properties.d.ts +1 -3
- package/dist/components/properties.d.ts.map +1 -1
- package/dist/components/properties.js +3 -10
- package/dist/components/properties.js.map +1 -1
- package/dist/constants.d.ts +12 -2
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +42 -1
- package/dist/constants.js.map +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +2 -0
- package/dist/copy.js.map +1 -1
- package/dist/corners.d.ts +22 -0
- package/dist/corners.d.ts.map +1 -0
- package/dist/corners.js +52 -0
- package/dist/corners.js.map +1 -0
- package/dist/css/grid.d.ts +4 -1
- package/dist/css/grid.d.ts.map +1 -1
- package/dist/css/grid.js +40 -7
- package/dist/css/grid.js.map +1 -1
- package/dist/css/index.d.ts +3 -3
- package/dist/css/index.js +3 -3
- package/dist/css/layout.d.ts +13 -1
- package/dist/css/layout.d.ts.map +1 -1
- package/dist/css/layout.js +26 -1
- package/dist/css/layout.js.map +1 -1
- package/dist/css/values.d.ts +23 -1
- package/dist/css/values.d.ts.map +1 -1
- package/dist/css/values.js +39 -1
- package/dist/css/values.js.map +1 -1
- package/dist/font-style.d.ts +2 -1
- package/dist/font-style.d.ts.map +1 -1
- package/dist/font-style.js +31 -6
- package/dist/font-style.js.map +1 -1
- package/dist/geometry.d.ts +68 -3
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +159 -25
- package/dist/geometry.js.map +1 -1
- package/dist/gradient.d.ts +9 -2
- package/dist/gradient.d.ts.map +1 -1
- package/dist/gradient.js +29 -1
- package/dist/gradient.js.map +1 -1
- package/dist/graph.d.ts +9 -2
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +46 -10
- package/dist/graph.js.map +1 -1
- package/dist/group-bounds.d.ts.map +1 -1
- package/dist/group-bounds.js +4 -3
- package/dist/group-bounds.js.map +1 -1
- package/dist/guides.d.ts +11 -2
- package/dist/guides.d.ts.map +1 -1
- package/dist/guides.js +30 -1
- package/dist/guides.js.map +1 -1
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +57 -5
- package/dist/hit-test.js.map +1 -1
- package/dist/icons/fit.d.ts +17 -0
- package/dist/icons/fit.d.ts.map +1 -0
- package/dist/icons/fit.js +21 -0
- package/dist/icons/fit.js.map +1 -0
- package/dist/icons/glyph.d.ts.map +1 -1
- package/dist/icons/glyph.js +5 -4
- package/dist/icons/glyph.js.map +1 -1
- package/dist/icons/index.d.ts +2 -1
- package/dist/icons/index.js +2 -1
- package/dist/ids.js +1 -1
- package/dist/ids.js.map +1 -1
- package/dist/images.d.ts +14 -4
- package/dist/images.d.ts.map +1 -1
- package/dist/images.js +45 -5
- package/dist/images.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/instances/assignments.js +39 -6
- package/dist/instances/assignments.js.map +1 -1
- package/dist/instances/index.d.ts +6 -1
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +58 -9
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/kept-overrides.js +2 -2
- package/dist/instances/kept-overrides.js.map +1 -1
- package/dist/instances/matching.js +68 -0
- package/dist/instances/matching.js.map +1 -0
- package/dist/instances/overrides.d.ts +4 -1
- package/dist/instances/overrides.d.ts.map +1 -1
- package/dist/instances/overrides.js +13 -1
- package/dist/instances/overrides.js.map +1 -1
- package/dist/instances/protection.js +67 -0
- package/dist/instances/protection.js.map +1 -0
- package/dist/instances/swap.js +7 -5
- package/dist/instances/swap.js.map +1 -1
- package/dist/instances/sync.js +34 -116
- package/dist/instances/sync.js.map +1 -1
- package/dist/layout/guides.d.ts +17 -1
- package/dist/layout/guides.d.ts.map +1 -1
- package/dist/layout/guides.js +31 -11
- package/dist/layout/guides.js.map +1 -1
- package/dist/layout/index.d.ts +5 -2
- package/dist/layout/index.js +5 -2
- package/dist/layout/limits.d.ts +40 -0
- package/dist/layout/limits.d.ts.map +1 -0
- package/dist/layout/limits.js +54 -0
- package/dist/layout/limits.js.map +1 -0
- package/dist/layout/sizing.d.ts.map +1 -1
- package/dist/layout/sizing.js +0 -1
- package/dist/layout/sizing.js.map +1 -1
- package/dist/layout/spacing.d.ts +10 -0
- package/dist/layout/spacing.d.ts.map +1 -0
- package/dist/layout/spacing.js +12 -0
- package/dist/layout/spacing.js.map +1 -0
- package/dist/layout/strokes.d.ts +10 -0
- package/dist/layout/strokes.d.ts.map +1 -0
- package/dist/layout/strokes.js +30 -0
- package/dist/layout/strokes.js.map +1 -0
- package/dist/masks.d.ts +28 -0
- package/dist/masks.d.ts.map +1 -0
- package/dist/masks.js +53 -0
- package/dist/masks.js.map +1 -0
- package/dist/matrix.d.ts +2 -1
- package/dist/matrix.d.ts.map +1 -1
- package/dist/matrix.js +14 -1
- package/dist/matrix.js.map +1 -1
- package/dist/node-defaults.d.ts +9 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +65 -38
- package/dist/node-defaults.js.map +1 -1
- package/dist/outline/flatten.d.ts.map +1 -1
- package/dist/outline/flatten.js +24 -17
- package/dist/outline/flatten.js.map +1 -1
- package/dist/outline/hit.d.ts +2 -8
- package/dist/outline/hit.d.ts.map +1 -1
- package/dist/outline/hit.js +16 -25
- package/dist/outline/hit.js.map +1 -1
- package/dist/outline/index.d.ts +3 -3
- package/dist/outline/index.js +3 -3
- package/dist/outline/shapes.d.ts +3 -1
- package/dist/outline/shapes.d.ts.map +1 -1
- package/dist/outline/shapes.js +18 -39
- package/dist/outline/shapes.js.map +1 -1
- package/dist/paints/index.d.ts +3 -1
- package/dist/paints/index.js +3 -1
- package/dist/paints/pattern.d.ts +13 -0
- package/dist/paints/pattern.d.ts.map +1 -0
- package/dist/paints/pattern.js +47 -0
- package/dist/paints/pattern.js.map +1 -0
- package/dist/paints/shows.d.ts +11 -0
- package/dist/paints/shows.d.ts.map +1 -0
- package/dist/paints/shows.js +16 -0
- package/dist/paints/shows.js.map +1 -0
- package/dist/parse-path.d.ts +4 -1
- package/dist/parse-path.d.ts.map +1 -1
- package/dist/parse-path.js +15 -1
- package/dist/parse-path.js.map +1 -1
- package/dist/paths/arc.d.ts +32 -0
- package/dist/paths/arc.d.ts.map +1 -0
- package/dist/paths/arc.js +49 -0
- package/dist/paths/arc.js.map +1 -0
- package/dist/paths/index.d.ts +5 -0
- package/dist/paths/index.js +5 -0
- package/dist/paths/network.d.ts +63 -0
- package/dist/paths/network.d.ts.map +1 -0
- package/dist/paths/network.js +315 -0
- package/dist/paths/network.js.map +1 -0
- package/dist/paths/sink.d.ts +20 -0
- package/dist/paths/sink.d.ts.map +1 -0
- package/dist/paths/sink.js +32 -0
- package/dist/paths/sink.js.map +1 -0
- package/dist/paths/smooth-corners.d.ts +25 -0
- package/dist/paths/smooth-corners.d.ts.map +1 -0
- package/dist/paths/smooth-corners.js +196 -0
- package/dist/paths/smooth-corners.js.map +1 -0
- package/dist/plugin-data/fields.d.ts +4 -0
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +2 -0
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/polygon.d.ts +1 -3
- package/dist/polygon.d.ts.map +1 -1
- package/dist/polygon.js +1 -16
- package/dist/polygon.js.map +1 -1
- package/dist/primitives/index.d.ts +1 -7
- package/dist/primitives/index.d.ts.map +1 -1
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +5 -5
- package/dist/resize.js.map +1 -1
- package/dist/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +25 -3
- package/dist/scaling/node.js.map +1 -1
- package/dist/shaders/model.d.ts.map +1 -1
- package/dist/shaders/model.js +1 -1
- package/dist/shaders/model.js.map +1 -1
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +2 -9
- package/dist/shared-styles.js.map +1 -1
- package/dist/strokes.d.ts +39 -0
- package/dist/strokes.d.ts.map +1 -0
- package/dist/strokes.js +104 -0
- package/dist/strokes.js.map +1 -0
- package/dist/text/auto-resize.d.ts +15 -0
- package/dist/text/auto-resize.d.ts.map +1 -0
- package/dist/text/auto-resize.js +23 -0
- package/dist/text/auto-resize.js.map +1 -0
- package/dist/text/index.d.ts +5 -3
- package/dist/text/index.js +5 -3
- package/dist/text/lengths.d.ts +6 -1
- package/dist/text/lengths.d.ts.map +1 -1
- package/dist/text/lengths.js +10 -1
- package/dist/text/lengths.js.map +1 -1
- package/dist/text/overflow.d.ts +14 -5
- package/dist/text/overflow.d.ts.map +1 -1
- package/dist/text/overflow.js +24 -6
- package/dist/text/overflow.js.map +1 -1
- package/dist/text/path.js +10 -0
- package/dist/text/path.js.map +1 -0
- package/dist/text/picture.js +5 -5
- package/dist/text/picture.js.map +1 -1
- package/dist/text/runs.d.ts +29 -0
- package/dist/text/runs.d.ts.map +1 -0
- package/dist/text/runs.js +72 -0
- package/dist/text/runs.js.map +1 -0
- package/dist/transfer/dependencies.d.ts.map +1 -1
- package/dist/transfer/dependencies.js +3 -2
- package/dist/transfer/dependencies.js.map +1 -1
- package/dist/transfer/nodes.js +2 -1
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/types.d.ts +56 -10
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/variables/index.d.ts +4 -3
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +4 -3
- package/dist/variables/index.js.map +1 -1
- package/dist/variables/lookup.d.ts +20 -0
- package/dist/variables/lookup.d.ts.map +1 -0
- package/dist/variables/lookup.js +34 -0
- package/dist/variables/lookup.js.map +1 -0
- package/dist/variables/paints.d.ts +35 -4
- package/dist/variables/paints.d.ts.map +1 -1
- package/dist/variables/paints.js +67 -16
- package/dist/variables/paints.js.map +1 -1
- package/dist/variables/resolution.d.ts +3 -1
- package/dist/variables/resolution.d.ts.map +1 -1
- package/dist/variables/resolution.js +8 -3
- package/dist/variables/resolution.js.map +1 -1
- package/dist/variables/subtree.js +1 -1
- package/dist/variables/subtree.js.map +1 -1
- package/dist/vector-network.d.ts.map +1 -1
- package/dist/vector-network.js +13 -79
- package/dist/vector-network.js.map +1 -1
- package/package.json +28 -7
- package/src/buffered-events.ts +2 -1
- package/src/clipping.ts +11 -8
- package/src/comments/schema.ts +1 -0
- package/src/comments/threads.ts +13 -5
- package/src/components/properties.ts +3 -14
- package/src/constants.ts +25 -1
- package/src/copy.ts +2 -0
- package/src/corners.ts +93 -0
- package/src/css/grid.ts +49 -7
- package/src/css/index.ts +13 -2
- package/src/css/layout.ts +36 -0
- package/src/css/values.ts +49 -0
- package/src/font-style.ts +38 -4
- package/src/geometry.ts +223 -28
- package/src/gradient.ts +23 -1
- package/src/graph.ts +35 -10
- package/src/group-bounds.ts +7 -3
- package/src/guides.ts +26 -1
- package/src/hit-test.ts +68 -10
- package/src/icons/fit.ts +24 -0
- package/src/icons/glyph.ts +6 -4
- package/src/icons/index.ts +1 -0
- package/src/ids.ts +1 -1
- package/src/images.ts +67 -7
- package/src/instances/assignments.ts +65 -7
- package/src/instances/index.ts +77 -13
- package/src/instances/kept-overrides.ts +2 -5
- package/src/instances/matching.ts +101 -0
- package/src/instances/overrides.ts +18 -0
- package/src/instances/protection.ts +103 -0
- package/src/instances/swap.ts +12 -9
- package/src/instances/sync.ts +52 -184
- package/src/layout/guides.ts +42 -9
- package/src/layout/index.ts +3 -0
- package/src/layout/limits.ts +64 -0
- package/src/layout/sizing.ts +0 -2
- package/src/layout/spacing.ts +11 -0
- package/src/layout/strokes.ts +23 -0
- package/src/masks.ts +51 -0
- package/src/matrix.ts +15 -2
- package/src/node-defaults.ts +71 -36
- package/src/outline/flatten.ts +31 -18
- package/src/outline/hit.ts +16 -33
- package/src/outline/index.ts +2 -2
- package/src/outline/shapes.ts +19 -36
- package/src/paints/index.ts +2 -0
- package/src/paints/pattern.ts +45 -0
- package/src/paints/shows.ts +13 -0
- package/src/parse-path.ts +14 -0
- package/src/paths/arc.ts +59 -0
- package/src/paths/index.ts +4 -0
- package/src/paths/network.ts +411 -0
- package/src/paths/sink.ts +50 -0
- package/src/paths/smooth-corners.ts +357 -0
- package/src/plugin-data/fields.ts +2 -0
- package/src/polygon.ts +0 -17
- package/src/primitives/index.ts +0 -5
- package/src/resize.ts +6 -7
- package/src/scaling/node.ts +29 -3
- package/src/shaders/model.ts +2 -1
- package/src/shared-styles.ts +3 -4
- package/src/strokes.ts +121 -0
- package/src/text/auto-resize.ts +25 -0
- package/src/text/index.ts +2 -0
- package/src/text/lengths.ts +11 -0
- package/src/text/overflow.ts +32 -6
- package/src/text/path.ts +13 -0
- package/src/text/picture.ts +5 -5
- package/src/text/runs.ts +80 -0
- package/src/transfer/dependencies.ts +4 -2
- package/src/transfer/nodes.ts +2 -1
- package/src/types.ts +62 -8
- package/src/variables/index.ts +1 -0
- package/src/variables/lookup.ts +31 -0
- package/src/variables/paints.ts +101 -20
- package/src/variables/resolution.ts +15 -2
- package/src/variables/subtree.ts +1 -1
- package/src/vector-network.ts +19 -142
- package/dist/random.d.ts +0 -7
- package/dist/random.d.ts.map +0 -1
- package/dist/random.js +0 -25
- package/dist/random.js.map +0 -1
- package/src/random.ts +0 -28
package/dist/parse-path.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse-path.js","names":[],"sources":["../src/parse-path.ts"],"sourcesContent":["import svgpath from 'svgpath'\n\nimport type { VectorNetwork, VectorVertex, VectorSegment, VectorRegion, WindingRule } from './types'\n\ninterface SubPath {\n startVertexIndex: number\n segmentIndices: number[]\n closed: boolean\n}\n\nexport type VectorPathWindingRule = WindingRule | 'NONE'\n\n/** How far apart an end and its subpath's start can be from rounding alone. */\nconst CLOSING_ROUNDING = 1e-6\nexport type SVGPathParseResult = { ok: true; network: VectorNetwork } | { ok: false; error: string }\n\ninterface ParseOptions {\n includeOpenRegions: boolean\n strictCommands: boolean\n}\n\n/**\n * Parse an SVG path `d` attribute into a VectorNetwork.\n *\n * Uses `svgpath` to normalize all commands to absolute M/L/C/Z\n * (arcs → cubics via `.unarc()`, smooth curves → explicit via `.unshort()`).\n *\n * Only closed subpaths form the fill region unless `includeOpenRegions` is set. SVG fills every\n * subpath as if it were closed, so set it for a filled path to keep holes that an open subpath and\n * a closed one cut together under the path's fill rule.\n */\nexport function parseSVGPath(\n d: string,\n windingRule: WindingRule = 'NONZERO',\n options: { includeOpenRegions?: boolean } = {}\n): VectorNetwork {\n const parsed = parsePath(d, windingRule, {\n includeOpenRegions: options.includeOpenRegions ?? false,\n strictCommands: false\n })\n return parsed.ok ? parsed.network : { vertices: [], segments: [], regions: [] }\n}\n\n/**\n * Parse a `d` with any command SVG has, its closed subpaths filled under `windingRule` and, with\n * `includeOpenRegions`, its open ones too, as SVG fills them. Unlike `parseSVGPath`, a path that\n * does not parse says why.\n */\nexport function parseSVGPathData(\n d: string,\n windingRule: WindingRule,\n options: { includeOpenRegions: boolean }\n): SVGPathParseResult {\n return parsePath(d, windingRule, { ...options, strictCommands: false })\n}\n\n/** Parse the strict absolute command subset accepted by Figma's VectorPath API. */\nexport function parsePluginVectorPath(\n d: string,\n windingRule: VectorPathWindingRule\n): SVGPathParseResult {\n return parsePath(d, windingRule, {\n includeOpenRegions: windingRule !== 'NONE',\n strictCommands: true\n })\n}\n\nfunction parsePath(\n d: string,\n windingRule: VectorPathWindingRule,\n options: ParseOptions\n): SVGPathParseResult {\n const vertices: VectorVertex[] = []\n const segments: VectorSegment[] = []\n const subPaths: SubPath[] = []\n\n let currentSubPath: SubPath | null = null\n let cx = 0\n let cy = 0\n\n const vertexMap = new Map<string, number>()\n /** How many segment ends sit on each vertex. */\n const uses: number[] = []\n\n function getOrCreateVertex(x: number, y: number): number {\n const key = `${x},${y}`\n const existing = vertexMap.get(key)\n if (existing !== undefined) return existing\n const idx = vertices.length\n vertices.push({ x, y })\n vertexMap.set(key, idx)\n return idx\n }\n\n function addSegment(\n x1: number,\n y1: number,\n x2: number,\n y2: number,\n tx1: number,\n ty1: number,\n tx2: number,\n ty2: number\n ): void {\n const startIdx = getOrCreateVertex(x1, y1)\n const endIdx = getOrCreateVertex(x2, y2)\n uses[startIdx] = (uses[startIdx] ?? 0) + 1\n uses[endIdx] = (uses[endIdx] ?? 0) + 1\n const segIdx = segments.length\n segments.push({\n start: startIdx,\n end: endIdx,\n tangentStart: { x: tx1 - x1, y: ty1 - y1 },\n tangentEnd: { x: tx2 - x2, y: ty2 - y2 }\n })\n if (currentSubPath) {\n currentSubPath.segmentIndices.push(segIdx)\n }\n }\n\n function addLine(x1: number, y1: number, x2: number, y2: number): void {\n addSegment(x1, y1, x2, y2, x1, y1, x2, y2)\n }\n\n function addCubic(\n x1: number,\n y1: number,\n cp1x: number,\n cp1y: number,\n cp2x: number,\n cp2y: number,\n x2: number,\n y2: number\n ): void {\n addSegment(x1, y1, x2, y2, cp1x, cp1y, cp2x, cp2y)\n }\n\n /**\n * A subpath that ends a hair off its start, as an arc turned into curves does, ends on the start\n * vertex, as Figma merges them; the point it ended on goes when no other segment uses it.\n */\n function closeOnStart(subPath: SubPath): void {\n const lastIndex = subPath.segmentIndices.at(-1)\n if (lastIndex === undefined) return\n const last = segments[lastIndex]\n if (last.end === subPath.startVertexIndex) return\n const stray = last.end\n last.end = subPath.startVertexIndex\n uses[subPath.startVertexIndex] += 1\n uses[stray] -= 1\n // Only the newest vertex can go without renumbering the others.\n if (uses[stray] > 0 || stray !== vertices.length - 1) return\n const point = vertices[stray]\n vertices.pop()\n uses.pop()\n vertexMap.delete(`${point.x},${point.y}`)\n }\n\n const parsed = svgpath(d)\n const parseError = 'err' in parsed && typeof parsed.err === 'string' ? parsed.err : null\n if (parseError) return { ok: false, error: parseError }\n if (options.strictCommands) {\n const unsupportedCommands = new Set<string>()\n parsed.iterate((segment) => {\n if (!['M', 'L', 'Q', 'C', 'Z'].includes(segment[0])) unsupportedCommands.add(segment[0])\n })\n const unsupportedCommand = unsupportedCommands.values().next().value\n if (unsupportedCommand !== undefined) {\n return { ok: false, error: `Unsupported path command ${unsupportedCommand}` }\n }\n }\n const normalized = parsed.abs().unshort().unarc()\n\n normalized.iterate((seg) => {\n const cmd = seg[0]\n\n if (cmd === 'M') {\n cx = seg[1]\n cy = seg[2]\n currentSubPath = {\n startVertexIndex: getOrCreateVertex(cx, cy),\n segmentIndices: [],\n closed: false\n }\n subPaths.push(currentSubPath)\n } else if (cmd === 'L') {\n addLine(cx, cy, seg[1], seg[2])\n cx = seg[1]\n cy = seg[2]\n } else if (cmd === 'H') {\n addLine(cx, cy, seg[1], cy)\n cx = seg[1]\n } else if (cmd === 'V') {\n addLine(cx, cy, cx, seg[1])\n cy = seg[1]\n } else if (cmd === 'C') {\n addCubic(cx, cy, seg[1], seg[2], seg[3], seg[4], seg[5], seg[6])\n cx = seg[5]\n cy = seg[6]\n } else if (cmd === 'Q') {\n const qx = seg[1]\n const qy = seg[2]\n const ex = seg[3]\n const ey = seg[4]\n const cp1x = cx + (2 / 3) * (qx - cx)\n const cp1y = cy + (2 / 3) * (qy - cy)\n const cp2x = ex + (2 / 3) * (qx - ex)\n const cp2y = ey + (2 / 3) * (qy - ey)\n addCubic(cx, cy, cp1x, cp1y, cp2x, cp2y, ex, ey)\n cx = ex\n cy = ey\n } else if (cmd === 'Z' || cmd === 'z') {\n if (currentSubPath) {\n const startVert = vertices[currentSubPath.startVertexIndex]\n // Only rounding, such as an arc turned into curves leaves, merges an end into the start;\n // a point the path drew, however close, keeps its own closing segment.\n const rounding =\n Math.abs(cx - startVert.x) <= CLOSING_ROUNDING &&\n Math.abs(cy - startVert.y) <= CLOSING_ROUNDING\n if (rounding) closeOnStart(currentSubPath)\n else addLine(cx, cy, startVert.x, startVert.y)\n currentSubPath.closed = true\n cx = startVert.x\n cy = startVert.y\n }\n }\n })\n\n const regions: VectorRegion[] = []\n const regionPaths = subPaths.filter(\n (subPath) =>\n windingRule !== 'NONE' &&\n subPath.segmentIndices.length > 0 &&\n (subPath.closed || options.includeOpenRegions)\n )\n if (regionPaths.length > 0 && windingRule !== 'NONE') {\n regions.push({\n windingRule,\n loops: regionPaths.map((subPath) => subPath.segmentIndices)\n })\n }\n\n return { ok: true, network: { vertices, segments, regions } }\n}\n"],"mappings":";;;AAaA,MAAM,mBAAmB;;;;;;;;;;;AAkBzB,SAAgB,aACd,GACA,cAA2B,WAC3B,UAA4C,CAAC,GAC9B;CACf,MAAM,SAAS,UAAU,GAAG,aAAa;EACvC,oBAAoB,QAAQ,sBAAsB;EAClD,gBAAgB;CAClB,CAAC;CACD,OAAO,OAAO,KAAK,OAAO,UAAU;EAAE,UAAU,CAAC;EAAG,UAAU,CAAC;EAAG,SAAS,CAAC;CAAE;AAChF;;;;;;AAOA,SAAgB,iBACd,GACA,aACA,SACoB;CACpB,OAAO,UAAU,GAAG,aAAa;EAAE,GAAG;EAAS,gBAAgB;CAAM,CAAC;AACxE;;AAGA,SAAgB,sBACd,GACA,aACoB;CACpB,OAAO,UAAU,GAAG,aAAa;EAC/B,oBAAoB,gBAAgB;EACpC,gBAAgB;CAClB,CAAC;AACH;AAEA,SAAS,UACP,GACA,aACA,SACoB;CACpB,MAAM,WAA2B,CAAC;CAClC,MAAM,WAA4B,CAAC;CACnC,MAAM,WAAsB,CAAC;CAE7B,IAAI,iBAAiC;CACrC,IAAI,KAAK;CACT,IAAI,KAAK;CAET,MAAM,4BAAY,IAAI,IAAoB;;CAE1C,MAAM,OAAiB,CAAC;CAExB,SAAS,k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.ts"],"sourcesContent":["import svgpath from 'svgpath'\n\nimport type { Rect } from './primitives'\nimport type { VectorNetwork, VectorVertex, VectorSegment, VectorRegion, WindingRule } from './types'\n\ninterface SubPath {\n startVertexIndex: number\n segmentIndices: number[]\n closed: boolean\n}\n\nexport type VectorPathWindingRule = WindingRule | 'NONE'\n\n/** How far apart an end and its subpath's start can be from rounding alone. */\nconst CLOSING_ROUNDING = 1e-6\nexport type SVGPathParseResult = { ok: true; network: VectorNetwork } | { ok: false; error: string }\n\ninterface ParseOptions {\n includeOpenRegions: boolean\n strictCommands: boolean\n}\n\n/**\n * Parse an SVG path `d` attribute into a VectorNetwork.\n *\n * Uses `svgpath` to normalize all commands to absolute M/L/C/Z\n * (arcs → cubics via `.unarc()`, smooth curves → explicit via `.unshort()`).\n *\n * Only closed subpaths form the fill region unless `includeOpenRegions` is set. SVG fills every\n * subpath as if it were closed, so set it for a filled path to keep holes that an open subpath and\n * a closed one cut together under the path's fill rule.\n */\nexport function parseSVGPath(\n d: string,\n windingRule: WindingRule = 'NONZERO',\n options: { includeOpenRegions?: boolean } = {}\n): VectorNetwork {\n const parsed = parsePath(d, windingRule, {\n includeOpenRegions: options.includeOpenRegions ?? false,\n strictCommands: false\n })\n return parsed.ok ? parsed.network : { vertices: [], segments: [], regions: [] }\n}\n\n/**\n * Parse a `d` with any command SVG has, its closed subpaths filled under `windingRule` and, with\n * `includeOpenRegions`, its open ones too, as SVG fills them. Unlike `parseSVGPath`, a path that\n * does not parse says why.\n */\nexport function parseSVGPathData(\n d: string,\n windingRule: WindingRule,\n options: { includeOpenRegions: boolean }\n): SVGPathParseResult {\n return parsePath(d, windingRule, { ...options, strictCommands: false })\n}\n\n/** Parse the strict absolute command subset accepted by Figma's VectorPath API. */\nexport function parsePluginVectorPath(\n d: string,\n windingRule: VectorPathWindingRule\n): SVGPathParseResult {\n return parsePath(d, windingRule, {\n includeOpenRegions: windingRule !== 'NONE',\n strictCommands: true\n })\n}\n\nfunction parsePath(\n d: string,\n windingRule: VectorPathWindingRule,\n options: ParseOptions\n): SVGPathParseResult {\n const vertices: VectorVertex[] = []\n const segments: VectorSegment[] = []\n const subPaths: SubPath[] = []\n\n let currentSubPath: SubPath | null = null\n let cx = 0\n let cy = 0\n\n const vertexMap = new Map<string, number>()\n /** How many segment ends sit on each vertex. */\n const uses: number[] = []\n\n function getOrCreateVertex(x: number, y: number): number {\n const key = `${x},${y}`\n const existing = vertexMap.get(key)\n if (existing !== undefined) return existing\n const idx = vertices.length\n vertices.push({ x, y })\n vertexMap.set(key, idx)\n return idx\n }\n\n function addSegment(\n x1: number,\n y1: number,\n x2: number,\n y2: number,\n tx1: number,\n ty1: number,\n tx2: number,\n ty2: number\n ): void {\n const startIdx = getOrCreateVertex(x1, y1)\n const endIdx = getOrCreateVertex(x2, y2)\n uses[startIdx] = (uses[startIdx] ?? 0) + 1\n uses[endIdx] = (uses[endIdx] ?? 0) + 1\n const segIdx = segments.length\n segments.push({\n start: startIdx,\n end: endIdx,\n tangentStart: { x: tx1 - x1, y: ty1 - y1 },\n tangentEnd: { x: tx2 - x2, y: ty2 - y2 }\n })\n if (currentSubPath) {\n currentSubPath.segmentIndices.push(segIdx)\n }\n }\n\n function addLine(x1: number, y1: number, x2: number, y2: number): void {\n addSegment(x1, y1, x2, y2, x1, y1, x2, y2)\n }\n\n function addCubic(\n x1: number,\n y1: number,\n cp1x: number,\n cp1y: number,\n cp2x: number,\n cp2y: number,\n x2: number,\n y2: number\n ): void {\n addSegment(x1, y1, x2, y2, cp1x, cp1y, cp2x, cp2y)\n }\n\n /**\n * A subpath that ends a hair off its start, as an arc turned into curves does, ends on the start\n * vertex, as Figma merges them; the point it ended on goes when no other segment uses it.\n */\n function closeOnStart(subPath: SubPath): void {\n const lastIndex = subPath.segmentIndices.at(-1)\n if (lastIndex === undefined) return\n const last = segments[lastIndex]\n if (last.end === subPath.startVertexIndex) return\n const stray = last.end\n last.end = subPath.startVertexIndex\n uses[subPath.startVertexIndex] += 1\n uses[stray] -= 1\n // Only the newest vertex can go without renumbering the others.\n if (uses[stray] > 0 || stray !== vertices.length - 1) return\n const point = vertices[stray]\n vertices.pop()\n uses.pop()\n vertexMap.delete(`${point.x},${point.y}`)\n }\n\n const parsed = svgpath(d)\n const parseError = 'err' in parsed && typeof parsed.err === 'string' ? parsed.err : null\n if (parseError) return { ok: false, error: parseError }\n if (options.strictCommands) {\n const unsupportedCommands = new Set<string>()\n parsed.iterate((segment) => {\n if (!['M', 'L', 'Q', 'C', 'Z'].includes(segment[0])) unsupportedCommands.add(segment[0])\n })\n const unsupportedCommand = unsupportedCommands.values().next().value\n if (unsupportedCommand !== undefined) {\n return { ok: false, error: `Unsupported path command ${unsupportedCommand}` }\n }\n }\n const normalized = parsed.abs().unshort().unarc()\n\n normalized.iterate((seg) => {\n const cmd = seg[0]\n\n if (cmd === 'M') {\n cx = seg[1]\n cy = seg[2]\n currentSubPath = {\n startVertexIndex: getOrCreateVertex(cx, cy),\n segmentIndices: [],\n closed: false\n }\n subPaths.push(currentSubPath)\n } else if (cmd === 'L') {\n addLine(cx, cy, seg[1], seg[2])\n cx = seg[1]\n cy = seg[2]\n } else if (cmd === 'H') {\n addLine(cx, cy, seg[1], cy)\n cx = seg[1]\n } else if (cmd === 'V') {\n addLine(cx, cy, cx, seg[1])\n cy = seg[1]\n } else if (cmd === 'C') {\n addCubic(cx, cy, seg[1], seg[2], seg[3], seg[4], seg[5], seg[6])\n cx = seg[5]\n cy = seg[6]\n } else if (cmd === 'Q') {\n const qx = seg[1]\n const qy = seg[2]\n const ex = seg[3]\n const ey = seg[4]\n const cp1x = cx + (2 / 3) * (qx - cx)\n const cp1y = cy + (2 / 3) * (qy - cy)\n const cp2x = ex + (2 / 3) * (qx - ex)\n const cp2y = ey + (2 / 3) * (qy - ey)\n addCubic(cx, cy, cp1x, cp1y, cp2x, cp2y, ex, ey)\n cx = ex\n cy = ey\n } else if (cmd === 'Z' || cmd === 'z') {\n if (currentSubPath) {\n const startVert = vertices[currentSubPath.startVertexIndex]\n // Only rounding, such as an arc turned into curves leaves, merges an end into the start;\n // a point the path drew, however close, keeps its own closing segment.\n const rounding =\n Math.abs(cx - startVert.x) <= CLOSING_ROUNDING &&\n Math.abs(cy - startVert.y) <= CLOSING_ROUNDING\n if (rounding) closeOnStart(currentSubPath)\n else addLine(cx, cy, startVert.x, startVert.y)\n currentSubPath.closed = true\n cx = startVert.x\n cy = startVert.y\n }\n }\n })\n\n const regions: VectorRegion[] = []\n const regionPaths = subPaths.filter(\n (subPath) =>\n windingRule !== 'NONE' &&\n subPath.segmentIndices.length > 0 &&\n (subPath.closed || options.includeOpenRegions)\n )\n if (regionPaths.length > 0 && windingRule !== 'NONE') {\n regions.push({\n windingRule,\n loops: regionPaths.map((subPath) => subPath.segmentIndices)\n })\n }\n\n return { ok: true, network: { vertices, segments, regions } }\n}\n\n/** A `viewBox` value as a rect, or null when it is malformed or does not establish a viewport. */\nexport function parseSVGViewBoxValue(value: string | null | undefined): Rect | null {\n if (!value) return null\n const values = value\n .trim()\n .split(/[\\s,]+/)\n .map(Number)\n if (values.length !== 4 || values.some((entry) => !Number.isFinite(entry))) return null\n const [x = 0, y = 0, width = 0, height = 0] = values\n if (width <= 0 || height <= 0) return null\n return { x, y, width, height }\n}\n"],"mappings":";;;AAcA,MAAM,mBAAmB;;;;;;;;;;;AAkBzB,SAAgB,aACd,GACA,cAA2B,WAC3B,UAA4C,CAAC,GAC9B;CACf,MAAM,SAAS,UAAU,GAAG,aAAa;EACvC,oBAAoB,QAAQ,sBAAsB;EAClD,gBAAgB;CAClB,CAAC;CACD,OAAO,OAAO,KAAK,OAAO,UAAU;EAAE,UAAU,CAAC;EAAG,UAAU,CAAC;EAAG,SAAS,CAAC;CAAE;AAChF;;;;;;AAOA,SAAgB,iBACd,GACA,aACA,SACoB;CACpB,OAAO,UAAU,GAAG,aAAa;EAAE,GAAG;EAAS,gBAAgB;CAAM,CAAC;AACxE;;AAGA,SAAgB,sBACd,GACA,aACoB;CACpB,OA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AAgB,UAChB,QAAQ,eAAe,SAAS,MAC/B,QAAQ,UAAU,QAAQ,mBAC/B;CACA,IAAI,YAAY,SAAS,KAAK,gBAAgB,QAC5C,QAAQ,KAAK;EACX;EACA,OAAO,YAAY,KAAK,YAAY,QAAQ,cAAc;CAC5D,CAAC;CAGH,OAAO;EAAE,IAAI;EAAM,SAAS;GAAE;GAAU;GAAU;EAAQ;CAAE;AAC9D;;AAGA,SAAgB,qBAAqB,OAA+C;CAClF,IAAI,CAAC,OAAO,OAAO;CACnB,MAAM,SAAS,MACZ,KAAK,CAAC,CACN,MAAM,QAAQ,CAAC,CACf,IAAI,MAAM;CACb,IAAI,OAAO,WAAW,KAAK,OAAO,MAAM,UAAU,CAAC,OAAO,SAAS,KAAK,CAAC,GAAG,OAAO;CACnF,MAAM,CAAC,IAAI,GAAG,IAAI,GAAG,QAAQ,GAAG,SAAS,KAAK;CAC9C,IAAI,SAAS,KAAK,UAAU,GAAG,OAAO;CACtC,OAAO;EAAE;EAAG;EAAG;EAAO;CAAO;AAC/B"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { SceneNode } from "../types.js";
|
|
2
|
+
//#region src/paths/arc.d.ts
|
|
3
|
+
/** A sweep around the ellipse's centre, from `start` by `sweep` radians, negative to turn back. */
|
|
4
|
+
interface ArcSweep {
|
|
5
|
+
type: 'arc';
|
|
6
|
+
rx: number;
|
|
7
|
+
ry: number;
|
|
8
|
+
start: number;
|
|
9
|
+
sweep: number;
|
|
10
|
+
}
|
|
11
|
+
/** A step to the ellipse's centre, as a pie slice's sides run. */
|
|
12
|
+
interface ArcCentre {
|
|
13
|
+
type: 'centre';
|
|
14
|
+
}
|
|
15
|
+
type ArcStep = ArcSweep | ArcCentre;
|
|
16
|
+
/**
|
|
17
|
+
* An ellipse or arc as the canvas, SVG export and hit testing draw it: contours of sweeps around
|
|
18
|
+
* its centre, each closed. A whole turn is one sweep, and with a hole a second sweep wound back,
|
|
19
|
+
* so a nonzero fill leaves the hole empty; a part ring is one contour whose ends run straight
|
|
20
|
+
* across the ring; a pie slice runs its sweep, then to the centre.
|
|
21
|
+
*/
|
|
22
|
+
interface ArcOutline {
|
|
23
|
+
cx: number;
|
|
24
|
+
cy: number;
|
|
25
|
+
contours: ArcStep[][];
|
|
26
|
+
}
|
|
27
|
+
declare function arcOutline(node: Pick<SceneNode, 'width' | 'height' | 'arcData'>): ArcOutline;
|
|
28
|
+
/** Whether a sweep goes all the way round, drawn as a whole ellipse rather than an arc. */
|
|
29
|
+
declare function sweepsWholeEllipse(step: ArcSweep): boolean;
|
|
30
|
+
//#endregion
|
|
31
|
+
export { ArcCentre, ArcOutline, ArcStep, ArcSweep, arcOutline, sweepsWholeEllipse };
|
|
32
|
+
//# sourceMappingURL=arc.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"arc.d.ts","names":[],"sources":["../../src/paths/arc.ts"],"mappings":";;;UAMiB;EACf;EACA;EACA;EACA;EACA;;;UAIe;EACf;;KAGU,UAAU,WAAW;;;;;;;UAQhB;EACf;EACA;EACA,UAAU;;iBAKI,WAAW,MAAM,KAAK,6CAA6C;;iBAqBnE,mBAAmB,MAAM"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { sweepsWholeTurn } from "../geometry.js";
|
|
2
|
+
//#region src/paths/arc.ts
|
|
3
|
+
const FULL_TURN = Math.PI * 2;
|
|
4
|
+
const WHOLE_ELLIPSE = {
|
|
5
|
+
startingAngle: 0,
|
|
6
|
+
endingAngle: FULL_TURN,
|
|
7
|
+
innerRadius: 0
|
|
8
|
+
};
|
|
9
|
+
function arcOutline(node) {
|
|
10
|
+
const { startingAngle: start, endingAngle: end, innerRadius } = node.arcData ?? WHOLE_ELLIPSE;
|
|
11
|
+
const rx = node.width / 2;
|
|
12
|
+
const ry = node.height / 2;
|
|
13
|
+
const whole = sweepsWholeTurn(start, end);
|
|
14
|
+
const sweep = whole ? FULL_TURN : end - start;
|
|
15
|
+
const outer = {
|
|
16
|
+
type: "arc",
|
|
17
|
+
rx,
|
|
18
|
+
ry,
|
|
19
|
+
start,
|
|
20
|
+
sweep
|
|
21
|
+
};
|
|
22
|
+
if (innerRadius > 0) {
|
|
23
|
+
const hole = {
|
|
24
|
+
type: "arc",
|
|
25
|
+
rx: rx * innerRadius,
|
|
26
|
+
ry: ry * innerRadius,
|
|
27
|
+
start: start + sweep,
|
|
28
|
+
sweep: -sweep
|
|
29
|
+
};
|
|
30
|
+
return {
|
|
31
|
+
cx: rx,
|
|
32
|
+
cy: ry,
|
|
33
|
+
contours: whole ? [[outer], [hole]] : [[outer, hole]]
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
cx: rx,
|
|
38
|
+
cy: ry,
|
|
39
|
+
contours: [whole ? [outer] : [outer, { type: "centre" }]]
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/** Whether a sweep goes all the way round, drawn as a whole ellipse rather than an arc. */
|
|
43
|
+
function sweepsWholeEllipse(step) {
|
|
44
|
+
return Math.abs(step.sweep) >= FULL_TURN;
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
export { arcOutline, sweepsWholeEllipse };
|
|
48
|
+
|
|
49
|
+
//# sourceMappingURL=arc.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"arc.js","names":[],"sources":["../../src/paths/arc.ts"],"sourcesContent":["import { sweepsWholeTurn } from '../geometry'\nimport type { SceneNode } from '../types'\n\nconst FULL_TURN = Math.PI * 2\n\n/** A sweep around the ellipse's centre, from `start` by `sweep` radians, negative to turn back. */\nexport interface ArcSweep {\n type: 'arc'\n rx: number\n ry: number\n start: number\n sweep: number\n}\n\n/** A step to the ellipse's centre, as a pie slice's sides run. */\nexport interface ArcCentre {\n type: 'centre'\n}\n\nexport type ArcStep = ArcSweep | ArcCentre\n\n/**\n * An ellipse or arc as the canvas, SVG export and hit testing draw it: contours of sweeps around\n * its centre, each closed. A whole turn is one sweep, and with a hole a second sweep wound back,\n * so a nonzero fill leaves the hole empty; a part ring is one contour whose ends run straight\n * across the ring; a pie slice runs its sweep, then to the centre.\n */\nexport interface ArcOutline {\n cx: number\n cy: number\n contours: ArcStep[][]\n}\n\nconst WHOLE_ELLIPSE = { startingAngle: 0, endingAngle: FULL_TURN, innerRadius: 0 }\n\nexport function arcOutline(node: Pick<SceneNode, 'width' | 'height' | 'arcData'>): ArcOutline {\n const { startingAngle: start, endingAngle: end, innerRadius } = node.arcData ?? WHOLE_ELLIPSE\n const rx = node.width / 2\n const ry = node.height / 2\n const whole = sweepsWholeTurn(start, end)\n const sweep = whole ? FULL_TURN : end - start\n const outer: ArcSweep = { type: 'arc', rx, ry, start, sweep }\n if (innerRadius > 0) {\n const hole: ArcSweep = {\n type: 'arc',\n rx: rx * innerRadius,\n ry: ry * innerRadius,\n start: start + sweep,\n sweep: -sweep\n }\n return { cx: rx, cy: ry, contours: whole ? [[outer], [hole]] : [[outer, hole]] }\n }\n return { cx: rx, cy: ry, contours: [whole ? [outer] : [outer, { type: 'centre' }]] }\n}\n\n/** Whether a sweep goes all the way round, drawn as a whole ellipse rather than an arc. */\nexport function sweepsWholeEllipse(step: ArcSweep): boolean {\n return Math.abs(step.sweep) >= FULL_TURN\n}\n"],"mappings":";;AAGA,MAAM,YAAY,KAAK,KAAK;AA8B5B,MAAM,gBAAgB;CAAE,eAAe;CAAG,aAAa;CAAW,aAAa;AAAE;AAEjF,SAAgB,WAAW,MAAmE;CAC5F,MAAM,EAAE,eAAe,OAAO,aAAa,KAAK,gBAAgB,KAAK,WAAW;CAChF,MAAM,KAAK,KAAK,QAAQ;CACxB,MAAM,KAAK,KAAK,SAAS;CACzB,MAAM,QAAQ,gBAAgB,OAAO,GAAG;CACxC,MAAM,QAAQ,QAAQ,YAAY,MAAM;CACxC,MAAM,QAAkB;EAAE,MAAM;EAAO;EAAI;EAAI;EAAO;CAAM;CAC5D,IAAI,cAAc,GAAG;EACnB,MAAM,OAAiB;GACrB,MAAM;GACN,IAAI,KAAK;GACT,IAAI,KAAK;GACT,OAAO,QAAQ;GACf,OAAO,CAAC;EACV;EACA,OAAO;GAAE,IAAI;GAAI,IAAI;GAAI,UAAU,QAAQ,CAAC,CAAC,KAAK,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,OAAO,IAAI,CAAC;EAAE;CACjF;CACA,OAAO;EAAE,IAAI;EAAI,IAAI;EAAI,UAAU,CAAC,QAAQ,CAAC,KAAK,IAAI,CAAC,OAAO,EAAE,MAAM,SAAS,CAAC,CAAC;CAAE;AACrF;;AAGA,SAAgB,mBAAmB,MAAyB;CAC1D,OAAO,KAAK,IAAI,KAAK,KAAK,KAAK;AACjC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ArcCentre, ArcOutline, ArcStep, ArcSweep, arcOutline, sweepsWholeEllipse } from "./arc.js";
|
|
2
|
+
import { PathSink, outlineSVGPath, replayOutline, roundCoordinate, svgPathSink } from "./sink.js";
|
|
3
|
+
import { Chain, RunContinuation, StrokeRun, addLoopToPath, addOpenSegmentsToPath, addRegionToPath, addSegmentDirected, addStrokeRunToPath, buildChains, joinedSegmentPairs, leavingDirection, segmentsByVertex, strokeRuns } from "./network.js";
|
|
4
|
+
import { SmoothCornerSink, SmoothRectNode, traceSmoothRect } from "./smooth-corners.js";
|
|
5
|
+
export { ArcCentre, ArcOutline, ArcStep, ArcSweep, Chain, PathSink, RunContinuation, SmoothCornerSink, SmoothRectNode, StrokeRun, addLoopToPath, addOpenSegmentsToPath, addRegionToPath, addSegmentDirected, addStrokeRunToPath, arcOutline, buildChains, joinedSegmentPairs, leavingDirection, outlineSVGPath, replayOutline, roundCoordinate, segmentsByVertex, strokeRuns, svgPathSink, sweepsWholeEllipse, traceSmoothRect };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { addLoopToPath, addOpenSegmentsToPath, addRegionToPath, addSegmentDirected, addStrokeRunToPath, buildChains, joinedSegmentPairs, leavingDirection, segmentsByVertex, strokeRuns } from "./network.js";
|
|
2
|
+
import { arcOutline, sweepsWholeEllipse } from "./arc.js";
|
|
3
|
+
import { outlineSVGPath, replayOutline, roundCoordinate, svgPathSink } from "./sink.js";
|
|
4
|
+
import { traceSmoothRect } from "./smooth-corners.js";
|
|
5
|
+
export { addLoopToPath, addOpenSegmentsToPath, addRegionToPath, addSegmentDirected, addStrokeRunToPath, arcOutline, buildChains, joinedSegmentPairs, leavingDirection, outlineSVGPath, replayOutline, roundCoordinate, segmentsByVertex, strokeRuns, svgPathSink, sweepsWholeEllipse, traceSmoothRect };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Vector } from "../primitives/index.js";
|
|
2
|
+
import { VectorNetwork, VectorRegion, VectorSegment, VectorVertex } from "../types.js";
|
|
3
|
+
import { PathSink } from "./sink.js";
|
|
4
|
+
//#region src/paths/network.d.ts
|
|
5
|
+
declare function addSegmentDirected(path: PathSink, seg: VectorSegment, vertices: VectorVertex[], forward: boolean): void;
|
|
6
|
+
/**
|
|
7
|
+
* Draws one region loop. It starts at the end of its first segment away from the second, as a
|
|
8
|
+
* loop's stored order runs; a segment that does not continue from the last starts a new subpath,
|
|
9
|
+
* and the loop closes only when it runs unbroken back to where it started.
|
|
10
|
+
*/
|
|
11
|
+
declare function addLoopToPath(path: PathSink, loop: number[], segments: VectorSegment[], vertices: VectorVertex[]): void;
|
|
12
|
+
/** A region's loops, one after another: the outline its fill rule fills. */
|
|
13
|
+
declare function addRegionToPath(path: PathSink, region: Pick<VectorRegion, 'loops'>, segments: VectorSegment[], vertices: VectorVertex[]): void;
|
|
14
|
+
declare function addOpenSegmentsToPath(path: PathSink, segments: VectorSegment[], vertices: VectorVertex[], cornerRadius?: number): void;
|
|
15
|
+
/**
|
|
16
|
+
* A run of connected segments in the order a walk took them, from the vertex it started at, or a
|
|
17
|
+
* lone segment's own start. The start is kept rather than worked out again from the first two
|
|
18
|
+
* segments, which two segments between the same vertices leave ambiguous.
|
|
19
|
+
*/
|
|
20
|
+
interface Chain {
|
|
21
|
+
start: number;
|
|
22
|
+
segments: number[];
|
|
23
|
+
}
|
|
24
|
+
declare function buildChains(segments: VectorSegment[]): Chain[];
|
|
25
|
+
/** A stretch of a network's segments stroked as one contour, so its corners join. */
|
|
26
|
+
interface StrokeRun {
|
|
27
|
+
/** The vertex the run starts at. */
|
|
28
|
+
start: number;
|
|
29
|
+
/** Segment indices in drawing order. */
|
|
30
|
+
segments: number[];
|
|
31
|
+
/** Whether the run comes back to its start, joining there instead of taking caps. */
|
|
32
|
+
closed: boolean;
|
|
33
|
+
}
|
|
34
|
+
type RunContinuation = (vertex: number, from: number) => number | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* The indices of the segments meeting at each vertex, in segment order; a segment from a vertex to
|
|
37
|
+
* itself is listed there once. `include` leaves out the segments it returns false for.
|
|
38
|
+
*/
|
|
39
|
+
declare function segmentsByVertex(segments: readonly VectorSegment[], include?: (index: number) => boolean): Map<number, number[]>;
|
|
40
|
+
/**
|
|
41
|
+
* The direction segment `index` leaves `vertex` in: along its handle there, or with that handle
|
|
42
|
+
* collapsed, toward the far end's control point, as a cubic leaves toward its next distinct
|
|
43
|
+
* control point; a straight segment leaves along its chord.
|
|
44
|
+
*/
|
|
45
|
+
declare function leavingDirection(network: Pick<VectorNetwork, 'vertices' | 'segments'>, index: number, vertex: number): Vector;
|
|
46
|
+
/**
|
|
47
|
+
* The pairs of segments a stroke may join at each vertex: the two at a vertex exactly two meet at,
|
|
48
|
+
* and with several regions, also each two region edges next to each other around a vertex,
|
|
49
|
+
* which region strokes join where the gap between them is unfilled, open segments aside.
|
|
50
|
+
*/
|
|
51
|
+
declare function joinedSegmentPairs(network: VectorNetwork): [vertex: number, a: number, b: number][];
|
|
52
|
+
/**
|
|
53
|
+
* Segments chained into runs as Figma strokes a network: by default a run passes through every
|
|
54
|
+
* vertex exactly two segments meet at, so its corners take the stroke's join and only its open
|
|
55
|
+
* ends take caps. Runs stop at open ends and at junctions where three or more segments meet, unless
|
|
56
|
+
* `continuation` names a segment to go on with there.
|
|
57
|
+
*/
|
|
58
|
+
declare function strokeRuns(segments: VectorSegment[], continuation?: RunContinuation): StrokeRun[];
|
|
59
|
+
/** Emits a stroke run as one contour, closed when the run comes back to its start. */
|
|
60
|
+
declare function addStrokeRunToPath(path: PathSink, run: StrokeRun, segments: VectorSegment[], vertices: VectorVertex[]): void;
|
|
61
|
+
//#endregion
|
|
62
|
+
export { Chain, RunContinuation, StrokeRun, addLoopToPath, addOpenSegmentsToPath, addRegionToPath, addSegmentDirected, addStrokeRunToPath, buildChains, joinedSegmentPairs, leavingDirection, segmentsByVertex, strokeRuns };
|
|
63
|
+
//# sourceMappingURL=network.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"network.d.ts","names":[],"sources":["../../src/paths/network.ts"],"mappings":";;;;iBAIgB,mBACd,MAAM,UACN,KAAK,eACL,UAAU,gBACV;;;;;;iBAsBc,cACd,MAAM,UACN,gBACA,UAAU,iBACV,UAAU;;iBA2BI,gBACd,MAAM,UACN,QAAQ,KAAK,wBACb,UAAU,iBACV,UAAU;iBA+GI,sBACd,MAAM,UACN,UAAU,iBACV,UAAU,gBACV;;;;;;UAuBe;EACf;EACA;;iBAGc,YAAY,UAAU,kBAAkB;;UAiDvC;;EAEf;;EAEA;;EAEA;;KAGU,mBAAmB,gBAAgB;;;;;iBAM/B,iBACd,mBAAmB,iBACnB,WAAW,4BACV;;;;;;iBAoBa,iBACd,SAAS,KAAK,yCACd,eACA,iBACC;;;;;;iBAkBa,mBACd,SAAS,iBACP,gBAAgB,WAAW;;;;;;;iBA+Bf,WAAW,UAAU,iBAAiB,eAAe,kBAAkB;;iBA6CvE,mBACd,MAAM,UACN,KAAK,WACL,UAAU,iBACV,UAAU"}
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
//#region src/paths/network.ts
|
|
2
|
+
function addSegmentDirected(path, seg, vertices, forward) {
|
|
3
|
+
const p0 = forward ? vertices[seg.start] : vertices[seg.end];
|
|
4
|
+
const p3 = forward ? vertices[seg.end] : vertices[seg.start];
|
|
5
|
+
const ts = seg.tangentStart;
|
|
6
|
+
const te = seg.tangentEnd;
|
|
7
|
+
if (ts.x === 0 && ts.y === 0 && te.x === 0 && te.y === 0) path.lineTo(p3.x, p3.y);
|
|
8
|
+
else if (forward) path.cubicTo(p0.x + ts.x, p0.y + ts.y, p3.x + te.x, p3.y + te.y, p3.x, p3.y);
|
|
9
|
+
else path.cubicTo(p0.x + te.x, p0.y + te.y, p3.x + ts.x, p3.y + ts.y, p3.x, p3.y);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Draws one region loop. It starts at the end of its first segment away from the second, as a
|
|
13
|
+
* loop's stored order runs; a segment that does not continue from the last starts a new subpath,
|
|
14
|
+
* and the loop closes only when it runs unbroken back to where it started.
|
|
15
|
+
*/
|
|
16
|
+
function addLoopToPath(path, loop, segments, vertices) {
|
|
17
|
+
if (loop.length === 0) return;
|
|
18
|
+
const first = segments[loop[0]];
|
|
19
|
+
const second = loop.length > 1 ? segments[loop[1]] : null;
|
|
20
|
+
const secondMeetsStart = second?.start === first.start || second?.end === first.start;
|
|
21
|
+
const secondMeetsEnd = second?.start === first.end || second?.end === first.end;
|
|
22
|
+
const start = !second || secondMeetsEnd || !secondMeetsStart ? first.start : first.end;
|
|
23
|
+
let current = start;
|
|
24
|
+
let broken = false;
|
|
25
|
+
path.moveTo(vertices[start].x, vertices[start].y);
|
|
26
|
+
for (const index of loop) {
|
|
27
|
+
const segment = segments[index];
|
|
28
|
+
const connected = segment.start === current || segment.end === current;
|
|
29
|
+
const forward = segment.start === current || !connected;
|
|
30
|
+
if (!connected) {
|
|
31
|
+
broken = true;
|
|
32
|
+
const from = vertices[segment.start];
|
|
33
|
+
path.moveTo(from.x, from.y);
|
|
34
|
+
}
|
|
35
|
+
addSegmentDirected(path, segment, vertices, forward);
|
|
36
|
+
current = forward ? segment.end : segment.start;
|
|
37
|
+
}
|
|
38
|
+
if (!broken && current === start) path.close();
|
|
39
|
+
}
|
|
40
|
+
/** A region's loops, one after another: the outline its fill rule fills. */
|
|
41
|
+
function addRegionToPath(path, region, segments, vertices) {
|
|
42
|
+
for (const loop of region.loops) addLoopToPath(path, loop, segments, vertices);
|
|
43
|
+
}
|
|
44
|
+
function isStraight(seg) {
|
|
45
|
+
const { tangentStart: ts, tangentEnd: te } = seg;
|
|
46
|
+
return ts.x === 0 && ts.y === 0 && te.x === 0 && te.y === 0;
|
|
47
|
+
}
|
|
48
|
+
/** Cubic handle length, as a fraction of the radius, of a circular arc turning by `angle`. */
|
|
49
|
+
function arcHandle(angle) {
|
|
50
|
+
return 4 / 3 * Math.tan(angle / 4);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The arc rounding `corner` between straight runs from `from` and to `after`, or null when the
|
|
54
|
+
* runs do not turn or have no length. A radius never takes more than half of either run.
|
|
55
|
+
*/
|
|
56
|
+
function roundedCorner(from, corner, after, radius) {
|
|
57
|
+
const inLength = Math.hypot(corner.x - from.x, corner.y - from.y);
|
|
58
|
+
const outLength = Math.hypot(after.x - corner.x, after.y - corner.y);
|
|
59
|
+
if (radius <= 0 || inLength === 0 || outLength === 0) return null;
|
|
60
|
+
const inDir = {
|
|
61
|
+
x: (corner.x - from.x) / inLength,
|
|
62
|
+
y: (corner.y - from.y) / inLength
|
|
63
|
+
};
|
|
64
|
+
const outDir = {
|
|
65
|
+
x: (after.x - corner.x) / outLength,
|
|
66
|
+
y: (after.y - corner.y) / outLength
|
|
67
|
+
};
|
|
68
|
+
const turn = Math.acos(Math.max(-1, Math.min(1, inDir.x * outDir.x + inDir.y * outDir.y)));
|
|
69
|
+
if (turn <= 1e-6) return null;
|
|
70
|
+
const reach = Math.min(radius * Math.tan(turn / 2), inLength / 2, outLength / 2);
|
|
71
|
+
const handle = arcHandle(turn) * (reach / Math.tan(turn / 2));
|
|
72
|
+
const start = {
|
|
73
|
+
x: corner.x - inDir.x * reach,
|
|
74
|
+
y: corner.y - inDir.y * reach
|
|
75
|
+
};
|
|
76
|
+
const end = {
|
|
77
|
+
x: corner.x + outDir.x * reach,
|
|
78
|
+
y: corner.y + outDir.y * reach
|
|
79
|
+
};
|
|
80
|
+
return {
|
|
81
|
+
start,
|
|
82
|
+
end,
|
|
83
|
+
handleIn: {
|
|
84
|
+
x: start.x + inDir.x * handle,
|
|
85
|
+
y: start.y + inDir.y * handle
|
|
86
|
+
},
|
|
87
|
+
handleOut: {
|
|
88
|
+
x: end.x - outDir.x * handle,
|
|
89
|
+
y: end.y - outDir.y * handle
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function drawCorner(path, corner) {
|
|
94
|
+
path.lineTo(corner.start.x, corner.start.y);
|
|
95
|
+
path.cubicTo(corner.handleIn.x, corner.handleIn.y, corner.handleOut.x, corner.handleOut.y, corner.end.x, corner.end.y);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Draws one chain, rounding each vertex where two straight segments meet by its corner radius, or
|
|
99
|
+
* `cornerRadius` when it has none, as Figma rounds a vector's corners. A chain that comes back to
|
|
100
|
+
* where it started is closed, its last corner rounded too.
|
|
101
|
+
*/
|
|
102
|
+
function addChainToPath(path, { start: first, segments: chain }, segments, vertices, cornerRadius) {
|
|
103
|
+
const order = [first];
|
|
104
|
+
for (const segIdx of chain) {
|
|
105
|
+
const seg = segments[segIdx];
|
|
106
|
+
order.push(seg.start === order[order.length - 1] ? seg.end : seg.start);
|
|
107
|
+
}
|
|
108
|
+
const closed = chain.length > 2 && order[order.length - 1] === first;
|
|
109
|
+
const cornerAt = (index) => {
|
|
110
|
+
const incoming = segments[chain[index - 1]];
|
|
111
|
+
const outgoing = segments[chain[index % chain.length]];
|
|
112
|
+
if (!isStraight(incoming) || !isStraight(outgoing)) return null;
|
|
113
|
+
const corner = vertices[order[index]];
|
|
114
|
+
return roundedCorner(vertices[order[index - 1]], corner, vertices[order[index % chain.length + 1]], corner.cornerRadius ?? cornerRadius);
|
|
115
|
+
};
|
|
116
|
+
const wrap = closed ? cornerAt(chain.length) : null;
|
|
117
|
+
const begin = wrap?.end ?? vertices[first];
|
|
118
|
+
path.moveTo(begin.x, begin.y);
|
|
119
|
+
for (let index = 0; index < chain.length; index++) {
|
|
120
|
+
const corner = index === chain.length - 1 ? wrap : cornerAt(index + 1);
|
|
121
|
+
if (corner) {
|
|
122
|
+
drawCorner(path, corner);
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
const seg = segments[chain[index]];
|
|
126
|
+
addSegmentDirected(path, seg, vertices, seg.start === order[index]);
|
|
127
|
+
}
|
|
128
|
+
if (closed) path.close();
|
|
129
|
+
}
|
|
130
|
+
function addOpenSegmentsToPath(path, segments, vertices, cornerRadius = 0) {
|
|
131
|
+
const visited = /* @__PURE__ */ new Set();
|
|
132
|
+
const chains = buildChains(segments);
|
|
133
|
+
for (const chain of chains) {
|
|
134
|
+
for (const segIdx of chain.segments) visited.add(segIdx);
|
|
135
|
+
addChainToPath(path, chain, segments, vertices, cornerRadius);
|
|
136
|
+
}
|
|
137
|
+
for (let i = 0; i < segments.length; i++) {
|
|
138
|
+
if (visited.has(i)) continue;
|
|
139
|
+
const seg = segments[i];
|
|
140
|
+
path.moveTo(vertices[seg.start].x, vertices[seg.start].y);
|
|
141
|
+
addSegmentDirected(path, seg, vertices, true);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function buildChains(segments) {
|
|
145
|
+
if (segments.length === 0) return [];
|
|
146
|
+
const adj = /* @__PURE__ */ new Map();
|
|
147
|
+
for (let i = 0; i < segments.length; i++) {
|
|
148
|
+
const s = segments[i];
|
|
149
|
+
const startList = adj.get(s.start) ?? [];
|
|
150
|
+
startList.push(i);
|
|
151
|
+
adj.set(s.start, startList);
|
|
152
|
+
const endList = adj.get(s.end) ?? [];
|
|
153
|
+
endList.push(i);
|
|
154
|
+
adj.set(s.end, endList);
|
|
155
|
+
}
|
|
156
|
+
const visited = /* @__PURE__ */ new Set();
|
|
157
|
+
const chains = [];
|
|
158
|
+
const startVertices = [...[...adj.entries()].filter(([, segs]) => segs.length === 1).map(([v]) => v), ...segments.map((segment) => segment.start)];
|
|
159
|
+
for (const startVertex of startVertices) {
|
|
160
|
+
let current = startVertex;
|
|
161
|
+
const chain = [];
|
|
162
|
+
for (;;) {
|
|
163
|
+
const segs = adj.get(current);
|
|
164
|
+
if (!segs) break;
|
|
165
|
+
const nextSeg = segs.find((s) => !visited.has(s));
|
|
166
|
+
if (nextSeg === void 0) break;
|
|
167
|
+
visited.add(nextSeg);
|
|
168
|
+
chain.push(nextSeg);
|
|
169
|
+
const seg = segments[nextSeg];
|
|
170
|
+
current = seg.start === current ? seg.end : seg.start;
|
|
171
|
+
}
|
|
172
|
+
const start = chain.length === 1 ? segments[chain[0]].start : startVertex;
|
|
173
|
+
if (chain.length > 0) chains.push({
|
|
174
|
+
start,
|
|
175
|
+
segments: chain
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
return chains;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* The indices of the segments meeting at each vertex, in segment order; a segment from a vertex to
|
|
182
|
+
* itself is listed there once. `include` leaves out the segments it returns false for.
|
|
183
|
+
*/
|
|
184
|
+
function segmentsByVertex(segments, include) {
|
|
185
|
+
const byVertex = /* @__PURE__ */ new Map();
|
|
186
|
+
const meet = (vertex, index) => {
|
|
187
|
+
const list = byVertex.get(vertex);
|
|
188
|
+
if (list) list.push(index);
|
|
189
|
+
else byVertex.set(vertex, [index]);
|
|
190
|
+
};
|
|
191
|
+
for (const [index, segment] of segments.entries()) {
|
|
192
|
+
if (include && !include(index)) continue;
|
|
193
|
+
meet(segment.start, index);
|
|
194
|
+
if (segment.end !== segment.start) meet(segment.end, index);
|
|
195
|
+
}
|
|
196
|
+
return byVertex;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* The direction segment `index` leaves `vertex` in: along its handle there, or with that handle
|
|
200
|
+
* collapsed, toward the far end's control point, as a cubic leaves toward its next distinct
|
|
201
|
+
* control point; a straight segment leaves along its chord.
|
|
202
|
+
*/
|
|
203
|
+
function leavingDirection(network, index, vertex) {
|
|
204
|
+
const segment = network.segments[index];
|
|
205
|
+
const atStart = segment.start === vertex;
|
|
206
|
+
const handle = atStart ? segment.tangentStart : segment.tangentEnd;
|
|
207
|
+
if (handle.x !== 0 || handle.y !== 0) return handle;
|
|
208
|
+
const from = network.vertices[vertex];
|
|
209
|
+
const to = network.vertices[atStart ? segment.end : segment.start];
|
|
210
|
+
const farHandle = atStart ? segment.tangentEnd : segment.tangentStart;
|
|
211
|
+
const control = {
|
|
212
|
+
x: to.x + farHandle.x - from.x,
|
|
213
|
+
y: to.y + farHandle.y - from.y
|
|
214
|
+
};
|
|
215
|
+
if (control.x !== 0 || control.y !== 0) return control;
|
|
216
|
+
return {
|
|
217
|
+
x: to.x - from.x,
|
|
218
|
+
y: to.y - from.y
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* The pairs of segments a stroke may join at each vertex: the two at a vertex exactly two meet at,
|
|
223
|
+
* and with several regions, also each two region edges next to each other around a vertex,
|
|
224
|
+
* which region strokes join where the gap between them is unfilled, open segments aside.
|
|
225
|
+
*/
|
|
226
|
+
function joinedSegmentPairs(network) {
|
|
227
|
+
const pairs = [];
|
|
228
|
+
for (const [vertex, indices] of segmentsByVertex(network.segments)) if (indices.length === 2) pairs.push([
|
|
229
|
+
vertex,
|
|
230
|
+
indices[0],
|
|
231
|
+
indices[1]
|
|
232
|
+
]);
|
|
233
|
+
if (network.regions.length < 2) return pairs;
|
|
234
|
+
const bounding = new Set(network.regions.flatMap((region) => region.loops.flat()));
|
|
235
|
+
const edges = segmentsByVertex(network.segments, (index) => bounding.has(index) && network.segments[index].start !== network.segments[index].end);
|
|
236
|
+
for (const [vertex, indices] of edges) {
|
|
237
|
+
if (indices.length < 2) continue;
|
|
238
|
+
const angle = (index) => {
|
|
239
|
+
const direction = leavingDirection(network, index, vertex);
|
|
240
|
+
return Math.atan2(direction.y, direction.x);
|
|
241
|
+
};
|
|
242
|
+
const around = indices.toSorted((a, b) => angle(a) - angle(b));
|
|
243
|
+
for (const [position, index] of around.entries()) pairs.push([
|
|
244
|
+
vertex,
|
|
245
|
+
index,
|
|
246
|
+
around[(position + 1) % around.length]
|
|
247
|
+
]);
|
|
248
|
+
}
|
|
249
|
+
return pairs;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Segments chained into runs as Figma strokes a network: by default a run passes through every
|
|
253
|
+
* vertex exactly two segments meet at, so its corners take the stroke's join and only its open
|
|
254
|
+
* ends take caps. Runs stop at open ends and at junctions where three or more segments meet, unless
|
|
255
|
+
* `continuation` names a segment to go on with there.
|
|
256
|
+
*/
|
|
257
|
+
function strokeRuns(segments, continuation) {
|
|
258
|
+
const incident = segmentsByVertex(segments);
|
|
259
|
+
const used = /* @__PURE__ */ new Set();
|
|
260
|
+
const across = (index, vertex) => segments[index].start === vertex ? segments[index].end : segments[index].start;
|
|
261
|
+
const throughPairs = (vertex, from) => {
|
|
262
|
+
const meeting = incident.get(vertex);
|
|
263
|
+
if (meeting?.length !== 2) return;
|
|
264
|
+
return meeting[0] === from ? meeting[1] : meeting[0];
|
|
265
|
+
};
|
|
266
|
+
const continueAt = continuation ?? throughPairs;
|
|
267
|
+
const extend = (vertex, from, add) => {
|
|
268
|
+
for (;;) {
|
|
269
|
+
const next = continueAt(vertex, from);
|
|
270
|
+
if (next === void 0 || used.has(next)) return vertex;
|
|
271
|
+
used.add(next);
|
|
272
|
+
add(next);
|
|
273
|
+
vertex = across(next, vertex);
|
|
274
|
+
from = next;
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
const runs = [];
|
|
278
|
+
for (const [first, segment] of segments.entries()) {
|
|
279
|
+
if (used.has(first)) continue;
|
|
280
|
+
used.add(first);
|
|
281
|
+
const run = [first];
|
|
282
|
+
const end = extend(segment.end, first, (index) => run.push(index));
|
|
283
|
+
if (end === segment.start) {
|
|
284
|
+
runs.push({
|
|
285
|
+
start: segment.start,
|
|
286
|
+
segments: run,
|
|
287
|
+
closed: true
|
|
288
|
+
});
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
const start = extend(segment.start, first, (index) => run.unshift(index));
|
|
292
|
+
runs.push({
|
|
293
|
+
start,
|
|
294
|
+
segments: run,
|
|
295
|
+
closed: start === end
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
return runs;
|
|
299
|
+
}
|
|
300
|
+
/** Emits a stroke run as one contour, closed when the run comes back to its start. */
|
|
301
|
+
function addStrokeRunToPath(path, run, segments, vertices) {
|
|
302
|
+
let current = run.start;
|
|
303
|
+
path.moveTo(vertices[current].x, vertices[current].y);
|
|
304
|
+
for (const index of run.segments) {
|
|
305
|
+
const segment = segments[index];
|
|
306
|
+
const forward = segment.start === current;
|
|
307
|
+
addSegmentDirected(path, segment, vertices, forward);
|
|
308
|
+
current = forward ? segment.end : segment.start;
|
|
309
|
+
}
|
|
310
|
+
if (run.closed) path.close();
|
|
311
|
+
}
|
|
312
|
+
//#endregion
|
|
313
|
+
export { addLoopToPath, addOpenSegmentsToPath, addRegionToPath, addSegmentDirected, addStrokeRunToPath, buildChains, joinedSegmentPairs, leavingDirection, segmentsByVertex, strokeRuns };
|
|
314
|
+
|
|
315
|
+
//# sourceMappingURL=network.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"network.js","names":[],"sources":["../../src/paths/network.ts"],"sourcesContent":["import type { Vector } from '../primitives'\nimport type { VectorNetwork, VectorRegion, VectorSegment, VectorVertex } from '../types'\nimport type { PathSink } from './sink'\n\nexport function addSegmentDirected(\n path: PathSink,\n seg: VectorSegment,\n vertices: VectorVertex[],\n forward: boolean\n): void {\n const p0 = forward ? vertices[seg.start] : vertices[seg.end]\n const p3 = forward ? vertices[seg.end] : vertices[seg.start]\n const ts = seg.tangentStart\n const te = seg.tangentEnd\n\n const isLine = ts.x === 0 && ts.y === 0 && te.x === 0 && te.y === 0\n if (isLine) {\n path.lineTo(p3.x, p3.y)\n } else if (forward) {\n path.cubicTo(p0.x + ts.x, p0.y + ts.y, p3.x + te.x, p3.y + te.y, p3.x, p3.y)\n } else {\n path.cubicTo(p0.x + te.x, p0.y + te.y, p3.x + ts.x, p3.y + ts.y, p3.x, p3.y)\n }\n}\n\n/**\n * Draws one region loop. It starts at the end of its first segment away from the second, as a\n * loop's stored order runs; a segment that does not continue from the last starts a new subpath,\n * and the loop closes only when it runs unbroken back to where it started.\n */\nexport function addLoopToPath(\n path: PathSink,\n loop: number[],\n segments: VectorSegment[],\n vertices: VectorVertex[]\n): void {\n if (loop.length === 0) return\n const first = segments[loop[0]]\n const second = loop.length > 1 ? segments[loop[1]] : null\n const secondMeetsStart = second?.start === first.start || second?.end === first.start\n const secondMeetsEnd = second?.start === first.end || second?.end === first.end\n const start = !second || secondMeetsEnd || !secondMeetsStart ? first.start : first.end\n let current = start\n let broken = false\n path.moveTo(vertices[start].x, vertices[start].y)\n for (const index of loop) {\n const segment = segments[index]\n const connected = segment.start === current || segment.end === current\n const forward = segment.start === current || !connected\n if (!connected) {\n broken = true\n const from = vertices[segment.start]\n path.moveTo(from.x, from.y)\n }\n addSegmentDirected(path, segment, vertices, forward)\n current = forward ? segment.end : segment.start\n }\n if (!broken && current === start) path.close()\n}\n\n/** A region's loops, one after another: the outline its fill rule fills. */\nexport function addRegionToPath(\n path: PathSink,\n region: Pick<VectorRegion, 'loops'>,\n segments: VectorSegment[],\n vertices: VectorVertex[]\n): void {\n for (const loop of region.loops) addLoopToPath(path, loop, segments, vertices)\n}\n\nfunction isStraight(seg: VectorSegment): boolean {\n const { tangentStart: ts, tangentEnd: te } = seg\n return ts.x === 0 && ts.y === 0 && te.x === 0 && te.y === 0\n}\n\n/** Cubic handle length, as a fraction of the radius, of a circular arc turning by `angle`. */\nfunction arcHandle(angle: number): number {\n return (4 / 3) * Math.tan(angle / 4)\n}\n\ninterface RoundedCorner {\n /** Where the arc leaves the incoming segment and joins the outgoing one. */\n start: Vector\n end: Vector\n handleIn: Vector\n handleOut: Vector\n}\n\n/**\n * The arc rounding `corner` between straight runs from `from` and to `after`, or null when the\n * runs do not turn or have no length. A radius never takes more than half of either run.\n */\nfunction roundedCorner(\n from: Vector,\n corner: Vector,\n after: Vector,\n radius: number\n): RoundedCorner | null {\n const inLength = Math.hypot(corner.x - from.x, corner.y - from.y)\n const outLength = Math.hypot(after.x - corner.x, after.y - corner.y)\n if (radius <= 0 || inLength === 0 || outLength === 0) return null\n const inDir = { x: (corner.x - from.x) / inLength, y: (corner.y - from.y) / inLength }\n const outDir = { x: (after.x - corner.x) / outLength, y: (after.y - corner.y) / outLength }\n const turn = Math.acos(Math.max(-1, Math.min(1, inDir.x * outDir.x + inDir.y * outDir.y)))\n if (turn <= 1e-6) return null\n const reach = Math.min(radius * Math.tan(turn / 2), inLength / 2, outLength / 2)\n const handle = arcHandle(turn) * (reach / Math.tan(turn / 2))\n const start = { x: corner.x - inDir.x * reach, y: corner.y - inDir.y * reach }\n const end = { x: corner.x + outDir.x * reach, y: corner.y + outDir.y * reach }\n return {\n start,\n end,\n handleIn: { x: start.x + inDir.x * handle, y: start.y + inDir.y * handle },\n handleOut: { x: end.x - outDir.x * handle, y: end.y - outDir.y * handle }\n }\n}\n\nfunction drawCorner(path: PathSink, corner: RoundedCorner) {\n path.lineTo(corner.start.x, corner.start.y)\n path.cubicTo(\n corner.handleIn.x,\n corner.handleIn.y,\n corner.handleOut.x,\n corner.handleOut.y,\n corner.end.x,\n corner.end.y\n )\n}\n\n/**\n * Draws one chain, rounding each vertex where two straight segments meet by its corner radius, or\n * `cornerRadius` when it has none, as Figma rounds a vector's corners. A chain that comes back to\n * where it started is closed, its last corner rounded too.\n */\nfunction addChainToPath(\n path: PathSink,\n { start: first, segments: chain }: Chain,\n segments: VectorSegment[],\n vertices: VectorVertex[],\n cornerRadius: number\n): void {\n // The chain's vertices in drawing order; a closed chain ends where it started.\n const order = [first]\n for (const segIdx of chain) {\n const seg = segments[segIdx]\n order.push(seg.start === order[order.length - 1] ? seg.end : seg.start)\n }\n const closed = chain.length > 2 && order[order.length - 1] === first\n const cornerAt = (index: number): RoundedCorner | null => {\n const incoming = segments[chain[index - 1]]\n const outgoing = segments[chain[index % chain.length]]\n if (!isStraight(incoming) || !isStraight(outgoing)) return null\n const corner = vertices[order[index]]\n return roundedCorner(\n vertices[order[index - 1]],\n corner,\n vertices[order[(index % chain.length) + 1]],\n corner.cornerRadius ?? cornerRadius\n )\n }\n const wrap = closed ? cornerAt(chain.length) : null\n const begin = wrap?.end ?? vertices[first]\n path.moveTo(begin.x, begin.y)\n for (let index = 0; index < chain.length; index++) {\n const last = index === chain.length - 1\n const corner = last ? wrap : cornerAt(index + 1)\n if (corner) {\n drawCorner(path, corner)\n continue\n }\n const seg = segments[chain[index]]\n addSegmentDirected(path, seg, vertices, seg.start === order[index])\n }\n if (closed) path.close()\n}\n\nexport function addOpenSegmentsToPath(\n path: PathSink,\n segments: VectorSegment[],\n vertices: VectorVertex[],\n cornerRadius = 0\n): void {\n const visited = new Set<number>()\n const chains = buildChains(segments)\n\n for (const chain of chains) {\n for (const segIdx of chain.segments) visited.add(segIdx)\n addChainToPath(path, chain, segments, vertices, cornerRadius)\n }\n\n for (let i = 0; i < segments.length; i++) {\n if (visited.has(i)) continue\n const seg = segments[i]\n path.moveTo(vertices[seg.start].x, vertices[seg.start].y)\n addSegmentDirected(path, seg, vertices, true)\n }\n}\n\n/**\n * A run of connected segments in the order a walk took them, from the vertex it started at, or a\n * lone segment's own start. The start is kept rather than worked out again from the first two\n * segments, which two segments between the same vertices leave ambiguous.\n */\nexport interface Chain {\n start: number\n segments: number[]\n}\n\nexport function buildChains(segments: VectorSegment[]): Chain[] {\n if (segments.length === 0) return []\n\n const adj = new Map<number, number[]>()\n for (let i = 0; i < segments.length; i++) {\n const s = segments[i]\n const startList = adj.get(s.start) ?? []\n startList.push(i)\n adj.set(s.start, startList)\n\n const endList = adj.get(s.end) ?? []\n endList.push(i)\n adj.set(s.end, endList)\n }\n\n const visited = new Set<number>()\n const chains: Chain[] = []\n // Open runs start at their ends; closed loops left over start wherever their first segment does,\n // so every loop is one contour, not one per segment.\n const degree1 = [...adj.entries()].filter(([, segs]) => segs.length === 1).map(([v]) => v)\n const startVertices = [...degree1, ...segments.map((segment) => segment.start)]\n\n for (const startVertex of startVertices) {\n let current = startVertex\n const chain: number[] = []\n\n for (;;) {\n const segs = adj.get(current)\n if (!segs) break\n\n const nextSeg = segs.find((s) => !visited.has(s))\n if (nextSeg === undefined) break\n\n visited.add(nextSeg)\n chain.push(nextSeg)\n\n const seg = segments[nextSeg]\n current = seg.start === current ? seg.end : seg.start\n }\n\n // A lone segment keeps its stored direction, as a dash pattern runs from its start.\n const start = chain.length === 1 ? segments[chain[0]].start : startVertex\n if (chain.length > 0) chains.push({ start, segments: chain })\n }\n\n return chains\n}\n\n/** A stretch of a network's segments stroked as one contour, so its corners join. */\nexport interface StrokeRun {\n /** The vertex the run starts at. */\n start: number\n /** Segment indices in drawing order. */\n segments: number[]\n /** Whether the run comes back to its start, joining there instead of taking caps. */\n closed: boolean\n}\n\nexport type RunContinuation = (vertex: number, from: number) => number | undefined\n\n/**\n * The indices of the segments meeting at each vertex, in segment order; a segment from a vertex to\n * itself is listed there once. `include` leaves out the segments it returns false for.\n */\nexport function segmentsByVertex(\n segments: readonly VectorSegment[],\n include?: (index: number) => boolean\n): Map<number, number[]> {\n const byVertex = new Map<number, number[]>()\n const meet = (vertex: number, index: number) => {\n const list = byVertex.get(vertex)\n if (list) list.push(index)\n else byVertex.set(vertex, [index])\n }\n for (const [index, segment] of segments.entries()) {\n if (include && !include(index)) continue\n meet(segment.start, index)\n if (segment.end !== segment.start) meet(segment.end, index)\n }\n return byVertex\n}\n\n/**\n * The direction segment `index` leaves `vertex` in: along its handle there, or with that handle\n * collapsed, toward the far end's control point, as a cubic leaves toward its next distinct\n * control point; a straight segment leaves along its chord.\n */\nexport function leavingDirection(\n network: Pick<VectorNetwork, 'vertices' | 'segments'>,\n index: number,\n vertex: number\n): Vector {\n const segment = network.segments[index]\n const atStart = segment.start === vertex\n const handle = atStart ? segment.tangentStart : segment.tangentEnd\n if (handle.x !== 0 || handle.y !== 0) return handle\n const from = network.vertices[vertex]\n const to = network.vertices[atStart ? segment.end : segment.start]\n const farHandle = atStart ? segment.tangentEnd : segment.tangentStart\n const control = { x: to.x + farHandle.x - from.x, y: to.y + farHandle.y - from.y }\n if (control.x !== 0 || control.y !== 0) return control\n return { x: to.x - from.x, y: to.y - from.y }\n}\n\n/**\n * The pairs of segments a stroke may join at each vertex: the two at a vertex exactly two meet at,\n * and with several regions, also each two region edges next to each other around a vertex,\n * which region strokes join where the gap between them is unfilled, open segments aside.\n */\nexport function joinedSegmentPairs(\n network: VectorNetwork\n): [vertex: number, a: number, b: number][] {\n const pairs: [number, number, number][] = []\n for (const [vertex, indices] of segmentsByVertex(network.segments)) {\n if (indices.length === 2) pairs.push([vertex, indices[0], indices[1]])\n }\n if (network.regions.length < 2) return pairs\n const bounding = new Set(network.regions.flatMap((region) => region.loops.flat()))\n const edges = segmentsByVertex(\n network.segments,\n (index) => bounding.has(index) && network.segments[index].start !== network.segments[index].end\n )\n for (const [vertex, indices] of edges) {\n if (indices.length < 2) continue\n const angle = (index: number) => {\n const direction = leavingDirection(network, index, vertex)\n return Math.atan2(direction.y, direction.x)\n }\n const around = indices.toSorted((a, b) => angle(a) - angle(b))\n for (const [position, index] of around.entries()) {\n pairs.push([vertex, index, around[(position + 1) % around.length]])\n }\n }\n return pairs\n}\n\n/**\n * Segments chained into runs as Figma strokes a network: by default a run passes through every\n * vertex exactly two segments meet at, so its corners take the stroke's join and only its open\n * ends take caps. Runs stop at open ends and at junctions where three or more segments meet, unless\n * `continuation` names a segment to go on with there.\n */\nexport function strokeRuns(segments: VectorSegment[], continuation?: RunContinuation): StrokeRun[] {\n const incident = segmentsByVertex(segments)\n const used = new Set<number>()\n const across = (index: number, vertex: number) =>\n segments[index].start === vertex ? segments[index].end : segments[index].start\n const throughPairs: RunContinuation = (vertex, from) => {\n const meeting = incident.get(vertex)\n if (meeting?.length !== 2) {\n return undefined\n }\n return meeting[0] === from ? meeting[1] : meeting[0]\n }\n const continueAt = continuation ?? throughPairs\n const extend = (vertex: number, from: number, add: (index: number) => void) => {\n for (;;) {\n const next = continueAt(vertex, from)\n if (next === undefined || used.has(next)) {\n return vertex\n }\n used.add(next)\n add(next)\n vertex = across(next, vertex)\n from = next\n }\n }\n\n const runs: StrokeRun[] = []\n for (const [first, segment] of segments.entries()) {\n if (used.has(first)) continue\n used.add(first)\n const run = [first]\n const end = extend(segment.end, first, (index) => run.push(index))\n if (end === segment.start) {\n runs.push({ start: segment.start, segments: run, closed: true })\n continue\n }\n const start = extend(segment.start, first, (index) => run.unshift(index))\n // A loop through a junction stops at it from both sides; it still closes there, whichever\n // of its segments is stored first.\n runs.push({ start, segments: run, closed: start === end })\n }\n return runs\n}\n\n/** Emits a stroke run as one contour, closed when the run comes back to its start. */\nexport function addStrokeRunToPath(\n path: PathSink,\n run: StrokeRun,\n segments: VectorSegment[],\n vertices: VectorVertex[]\n): void {\n let current = run.start\n path.moveTo(vertices[current].x, vertices[current].y)\n for (const index of run.segments) {\n const segment = segments[index]\n const forward = segment.start === current\n addSegmentDirected(path, segment, vertices, forward)\n current = forward ? segment.end : segment.start\n }\n if (run.closed) path.close()\n}\n"],"mappings":";AAIA,SAAgB,mBACd,MACA,KACA,UACA,SACM;CACN,MAAM,KAAK,UAAU,SAAS,IAAI,SAAS,SAAS,IAAI;CACxD,MAAM,KAAK,UAAU,SAAS,IAAI,OAAO,SAAS,IAAI;CACtD,MAAM,KAAK,IAAI;CACf,MAAM,KAAK,IAAI;CAGf,IADe,GAAG,MAAM,KAAK,GAAG,MAAM,KAAK,GAAG,MAAM,KAAK,GAAG,MAAM,GAEhE,KAAK,OAAO,GAAG,GA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F;EACA,MAAM,QAAQ,OAAO,QAAQ,OAAO,QAAQ,UAAU,IAAI,QAAQ,KAAK,CAAC;EAGxE,KAAK,KAAK;GAAE;GAAO,UAAU;GAAK,QAAQ,UAAU;EAAI,CAAC;CAC3D;CACA,OAAO;AACT;;AAGA,SAAgB,mBACd,MACA,KACA,UACA,UACM;CACN,IAAI,UAAU,IAAI;CAClB,KAAK,OAAO,SAAS,QAAQ,CAAC,GAAG,SAAS,QAAQ,CAAC,CAAC;CACpD,KAAK,MAAM,SAAS,IAAI,UAAU;EAChC,MAAM,UAAU,SAAS;EACzB,MAAM,UAAU,QAAQ,UAAU;EAClC,mBAAmB,MAAM,SAAS,UAAU,OAAO;EACnD,UAAU,UAAU,QAAQ,MAAM,QAAQ;CAC5C;CACA,IAAI,IAAI,QAAQ,KAAK,MAAM;AAC7B"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { PolygonOutlineCommand } from "../polygon.js";
|
|
2
|
+
//#region src/paths/sink.d.ts
|
|
3
|
+
/** Anything path commands can be written into: a CanvasKit path, SVG path data, a blob encoder. */
|
|
4
|
+
interface PathSink {
|
|
5
|
+
moveTo(x: number, y: number): unknown;
|
|
6
|
+
lineTo(x: number, y: number): unknown;
|
|
7
|
+
cubicTo(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number): unknown;
|
|
8
|
+
close(): unknown;
|
|
9
|
+
}
|
|
10
|
+
/** A coordinate rounded to two decimals, as exported path data writes it. */
|
|
11
|
+
declare function roundCoordinate(value: number): number;
|
|
12
|
+
/** Path commands written as SVG path data into `parts`, each coordinate written by `format`. */
|
|
13
|
+
declare function svgPathSink(parts: string[], format?: (value: number) => number): PathSink;
|
|
14
|
+
/** Outline commands written into `sink`. */
|
|
15
|
+
declare function replayOutline(commands: readonly PolygonOutlineCommand[], sink: PathSink): void;
|
|
16
|
+
/** Outline commands as SVG path data, each coordinate written by `format`. */
|
|
17
|
+
declare function outlineSVGPath(commands: readonly PolygonOutlineCommand[], format?: (value: number) => number): string;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { PathSink, outlineSVGPath, replayOutline, roundCoordinate, svgPathSink };
|
|
20
|
+
//# sourceMappingURL=sink.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sink.d.ts","names":[],"sources":["../../src/paths/sink.ts"],"mappings":";;;UAGiB;EACf,OAAO,WAAW;EAClB,OAAO,WAAW;EAClB,QAAQ,YAAY,YAAY,YAAY,YAAY,YAAY;EACpE;;;iBAIc,gBAAgB;;iBAKhB,YACd,iBACA,UAAS,2BACR;;iBAYa,cAAc,mBAAmB,yBAAyB,MAAM;;iBAWhE,eACd,mBAAmB,yBACnB,UAAS"}
|