@open-pencil/scene-graph 0.14.0 → 0.15.1
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/arrow-caps.d.ts +44 -0
- package/dist/arrow-caps.d.ts.map +1 -0
- package/dist/arrow-caps.js +131 -0
- package/dist/arrow-caps.js.map +1 -0
- package/dist/chunks/checkpoint.js +78 -0
- package/dist/chunks/checkpoint.js.map +1 -0
- package/dist/chunks/components/properties.js +1571 -0
- package/dist/chunks/components/properties.js.map +1 -0
- package/dist/chunks/coordinate.js +87 -33
- package/dist/chunks/coordinate.js.map +1 -1
- package/dist/chunks/font-style.js +2 -1
- package/dist/chunks/font-style.js.map +1 -1
- package/dist/chunks/index.js +1283 -0
- package/dist/chunks/index.js.map +1 -0
- package/dist/constants.js +1 -18
- package/dist/constants2.d.ts +0 -1
- package/dist/constants2.d.ts.map +1 -1
- package/dist/coordinate.d.ts +107 -11
- package/dist/coordinate.d.ts.map +1 -1
- package/dist/coordinate.js +2 -2
- package/dist/coordinate2.d.ts +2 -2
- package/dist/copy.d.ts +2 -2
- package/dist/copy.js +2 -2
- package/dist/geometry.d.ts +2 -2
- package/dist/geometry.js +2 -431
- package/dist/guides.d.ts +2 -0
- package/dist/guides.js +0 -0
- package/dist/guides2.d.ts +11 -0
- package/dist/guides2.d.ts.map +1 -0
- package/dist/hit-test.d.ts +1 -2
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +26 -14
- package/dist/hit-test.js.map +1 -1
- package/dist/images.js +0 -1
- package/dist/images.js.map +1 -1
- package/dist/images2.d.ts.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.js +5 -6
- package/dist/instances.d.ts +2 -2
- package/dist/instances.js +2 -2
- package/dist/layout-guides.d.ts +2 -0
- package/dist/layout-guides.js +2 -0
- package/dist/layout-guides2.d.ts +16 -0
- package/dist/layout-guides2.d.ts.map +1 -0
- package/dist/matrix2.d.ts +0 -1
- package/dist/matrix2.d.ts.map +1 -1
- package/dist/node-defaults.d.ts +1 -2
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +1 -1
- package/dist/parse-path.d.ts +1 -2
- package/dist/parse-path.d.ts.map +1 -1
- package/dist/parse-path.js.map +1 -1
- package/dist/preview.d.ts +2 -12
- package/dist/preview.js +1 -1
- package/dist/preview2.d.ts +12 -0
- package/dist/preview2.d.ts.map +1 -0
- package/dist/primitives2.d.ts.map +1 -1
- package/dist/resize.d.ts +18 -4
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +70 -22
- package/dist/resize.js.map +1 -1
- package/dist/snap.js +1 -1
- package/dist/source-metadata.d.ts +1 -2
- package/dist/source-metadata.d.ts.map +1 -1
- package/dist/source-metadata.js +1 -1
- package/dist/text-picture.d.ts +13 -2
- package/dist/text-picture.d.ts.map +1 -1
- package/dist/text-picture.js +2 -2
- package/dist/types.d.ts +499 -2
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -1296
- package/dist/types2.d.ts +2 -430
- package/dist/undo.js +1 -1
- package/dist/undo2.d.ts +5 -0
- package/dist/undo2.d.ts.map +1 -1
- package/dist/variables.d.ts +1 -2
- package/dist/variables.d.ts.map +1 -1
- package/dist/variables.js +1 -1
- package/dist/variant-name.d.ts.map +1 -1
- package/dist/variant-properties.d.ts +10 -0
- package/dist/variant-properties.d.ts.map +1 -0
- package/dist/variant-properties.js +39 -0
- package/dist/variant-properties.js.map +1 -0
- package/dist/vector-network.js +1 -1
- package/dist/vector-network2.d.ts +1 -2
- package/dist/vector-network2.d.ts.map +1 -1
- package/package.json +40 -15
- package/src/arrow-caps.ts +146 -0
- package/src/bindings.ts +20 -0
- package/src/checkpoint.ts +123 -0
- package/src/components/properties.ts +238 -0
- package/src/constants.ts +6 -0
- package/src/coordinate.ts +226 -0
- package/src/copy.ts +294 -0
- package/src/events.ts +21 -0
- package/src/export-scale.ts +16 -0
- package/src/font-style.ts +77 -0
- package/src/geometry.ts +530 -0
- package/src/guides.ts +8 -0
- package/src/hit-test.ts +205 -0
- package/src/images.ts +34 -0
- package/src/index.ts +686 -0
- package/src/instance-overrides.test.ts +67 -0
- package/src/instance-overrides.ts +152 -0
- package/src/instances.ts +480 -0
- package/src/layout-guides.ts +103 -0
- package/src/matrix.ts +116 -0
- package/src/mutation-impact.ts +70 -0
- package/src/node-defaults.ts +186 -0
- package/src/parse-path.ts +191 -0
- package/src/preview.ts +80 -0
- package/src/primitives.ts +42 -0
- package/src/resize.ts +302 -0
- package/src/shared-styles.ts +73 -0
- package/src/snap.ts +129 -0
- package/src/source-metadata.ts +8 -0
- package/src/text-picture.ts +65 -0
- package/src/types.ts +635 -0
- package/src/undo.ts +171 -0
- package/src/variables.ts +399 -0
- package/src/variant-name.ts +15 -0
- package/src/variant-properties.ts +48 -0
- package/src/vector-network.ts +293 -0
- package/dist/chunks/copy.js +0 -576
- package/dist/chunks/copy.js.map +0 -1
- package/dist/constants.js.map +0 -1
- package/dist/geometry.js.map +0 -1
- package/dist/preview.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
- package/dist/types2.d.ts.map +0 -1
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { LayoutGrid, SceneNode } from './types'
|
|
2
|
+
|
|
3
|
+
export interface LayoutGuideLine {
|
|
4
|
+
axis: 'x' | 'y'
|
|
5
|
+
position: number
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const MAX_LAYOUT_GUIDE_LINES = 10_000
|
|
9
|
+
|
|
10
|
+
function normalizedCount(grid: LayoutGrid): number {
|
|
11
|
+
const value = grid.count ?? grid.numSections ?? 1
|
|
12
|
+
if (!Number.isFinite(value)) return 0
|
|
13
|
+
return Math.min(MAX_LAYOUT_GUIDE_LINES, Math.max(0, Math.floor(value)))
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function pattern(grid: LayoutGrid): 'COLUMNS' | 'ROWS' | 'GRID' {
|
|
17
|
+
if (grid.pattern === 'GRID' || grid.pattern === 'ROWS') return grid.pattern
|
|
18
|
+
if (grid.axis === 'Y') return 'ROWS'
|
|
19
|
+
return 'COLUMNS'
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function alignment(grid: LayoutGrid): 'MIN' | 'CENTER' | 'MAX' | 'STRETCH' {
|
|
23
|
+
const value = grid.alignment ?? grid.type
|
|
24
|
+
if (value === 'CENTER' || value === 'MAX' || value === 'STRETCH') return value
|
|
25
|
+
return 'MIN'
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function sectionSize(nodeSize: number, grid: LayoutGrid): number {
|
|
29
|
+
const fixed = grid.sectionSize ?? 0
|
|
30
|
+
if (alignment(grid) !== 'STRETCH') return fixed
|
|
31
|
+
const count = normalizedCount(grid)
|
|
32
|
+
const gutter = grid.gutterSize ?? 0
|
|
33
|
+
const offset = grid.offset ?? 0
|
|
34
|
+
return (nodeSize - offset * 2 - Math.max(0, count - 1) * gutter) / count
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function start(nodeSize: number, grid: LayoutGrid, size: number): number {
|
|
38
|
+
const count = normalizedCount(grid)
|
|
39
|
+
const gutter = grid.gutterSize ?? 0
|
|
40
|
+
const offset = grid.offset ?? 0
|
|
41
|
+
const span = count * size + Math.max(0, count - 1) * gutter
|
|
42
|
+
if (alignment(grid) === 'CENTER') return (nodeSize - span) / 2 + offset
|
|
43
|
+
if (alignment(grid) === 'MAX') return nodeSize - span - offset
|
|
44
|
+
return offset
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface LayoutGuideSection {
|
|
48
|
+
axis: 'x' | 'y'
|
|
49
|
+
start: number
|
|
50
|
+
end: number
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function layoutGuideSections(
|
|
54
|
+
node: Pick<SceneNode, 'width' | 'height'>,
|
|
55
|
+
grid: LayoutGrid
|
|
56
|
+
): LayoutGuideSection[] {
|
|
57
|
+
if (grid.visible === false || pattern(grid) === 'GRID') return []
|
|
58
|
+
const axis = pattern(grid) === 'ROWS' ? 'y' : 'x'
|
|
59
|
+
const nodeSize = axis === 'x' ? node.width : node.height
|
|
60
|
+
const size = sectionSize(nodeSize, grid)
|
|
61
|
+
const count = normalizedCount(grid)
|
|
62
|
+
if (count <= 0 || size <= 0) return []
|
|
63
|
+
const first = start(nodeSize, grid, size)
|
|
64
|
+
const step = size + (grid.gutterSize ?? 0)
|
|
65
|
+
return Array.from({ length: count }, (_, index) => {
|
|
66
|
+
const sectionStart = first + index * step
|
|
67
|
+
return { axis, start: sectionStart, end: sectionStart + size }
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function layoutGuideLines(node: Pick<SceneNode, 'width' | 'height'>, grid: LayoutGrid) {
|
|
72
|
+
if (grid.visible === false) return []
|
|
73
|
+
const gridPattern = pattern(grid)
|
|
74
|
+
const lines: LayoutGuideLine[] = []
|
|
75
|
+
if (gridPattern === 'GRID') {
|
|
76
|
+
const size = grid.sectionSize ?? 0
|
|
77
|
+
if (size <= 0) return lines
|
|
78
|
+
for (
|
|
79
|
+
let index = 0, x = grid.offset ?? 0;
|
|
80
|
+
x <= node.width && index < MAX_LAYOUT_GUIDE_LINES;
|
|
81
|
+
index++, x += size
|
|
82
|
+
) {
|
|
83
|
+
lines.push({ axis: 'x', position: x })
|
|
84
|
+
}
|
|
85
|
+
for (
|
|
86
|
+
let index = 0, y = grid.offset ?? 0;
|
|
87
|
+
y <= node.height && index < MAX_LAYOUT_GUIDE_LINES;
|
|
88
|
+
index++, y += size
|
|
89
|
+
) {
|
|
90
|
+
lines.push({ axis: 'y', position: y })
|
|
91
|
+
}
|
|
92
|
+
return lines
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const sections = layoutGuideSections(node, grid)
|
|
96
|
+
for (const section of sections) {
|
|
97
|
+
lines.push(
|
|
98
|
+
{ axis: section.axis, position: section.start },
|
|
99
|
+
{ axis: section.axis, position: section.end }
|
|
100
|
+
)
|
|
101
|
+
}
|
|
102
|
+
return lines
|
|
103
|
+
}
|
package/src/matrix.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { Vector } from './primitives'
|
|
2
|
+
|
|
3
|
+
export type Mat3 = number[]
|
|
4
|
+
|
|
5
|
+
const identity = (): Mat3 => [1, 0, 0, 0, 1, 0, 0, 0, 1]
|
|
6
|
+
|
|
7
|
+
const multiply2 = (m1: Mat3, m2: Mat3): Mat3 => {
|
|
8
|
+
// row-major general 3x3 multiply: out = m1 * m2
|
|
9
|
+
return [
|
|
10
|
+
m1[0] * m2[0] + m1[1] * m2[3] + m1[2] * m2[6],
|
|
11
|
+
m1[0] * m2[1] + m1[1] * m2[4] + m1[2] * m2[7],
|
|
12
|
+
m1[0] * m2[2] + m1[1] * m2[5] + m1[2] * m2[8],
|
|
13
|
+
|
|
14
|
+
m1[3] * m2[0] + m1[4] * m2[3] + m1[5] * m2[6],
|
|
15
|
+
m1[3] * m2[1] + m1[4] * m2[4] + m1[5] * m2[7],
|
|
16
|
+
m1[3] * m2[2] + m1[4] * m2[5] + m1[5] * m2[8],
|
|
17
|
+
|
|
18
|
+
m1[6] * m2[0] + m1[7] * m2[3] + m1[8] * m2[6],
|
|
19
|
+
m1[6] * m2[1] + m1[7] * m2[4] + m1[8] * m2[7],
|
|
20
|
+
m1[6] * m2[2] + m1[7] * m2[5] + m1[8] * m2[8]
|
|
21
|
+
]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const multiply = (...ms: Mat3[]): Mat3 => {
|
|
25
|
+
if (ms.length === 0) return identity()
|
|
26
|
+
let out = ms[0].slice() as Mat3
|
|
27
|
+
for (let i = 1; i < ms.length; i++) out = multiply2(out, ms[i])
|
|
28
|
+
return out
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const translated = (dx: number, dy: number): Mat3 => [1, 0, dx, 0, 1, dy, 0, 0, 1]
|
|
32
|
+
|
|
33
|
+
const rotated = (radians: number, px = 0, py = 0): Mat3 => {
|
|
34
|
+
const s = Math.sin(radians)
|
|
35
|
+
const c = Math.cos(radians)
|
|
36
|
+
// exactly like ck.Matrix.rotated
|
|
37
|
+
return [
|
|
38
|
+
c,
|
|
39
|
+
-s,
|
|
40
|
+
s * py + (1 - c) * px,
|
|
41
|
+
|
|
42
|
+
s,
|
|
43
|
+
c,
|
|
44
|
+
-s * px + (1 - c) * py,
|
|
45
|
+
|
|
46
|
+
0,
|
|
47
|
+
0,
|
|
48
|
+
1
|
|
49
|
+
]
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const scaled = (sx: number, sy: number, px = 0, py = 0): Mat3 => {
|
|
53
|
+
// match ck.Matrix.scaled
|
|
54
|
+
return [
|
|
55
|
+
sx,
|
|
56
|
+
0,
|
|
57
|
+
px - sx * px,
|
|
58
|
+
|
|
59
|
+
0,
|
|
60
|
+
sy,
|
|
61
|
+
py - sy * py,
|
|
62
|
+
|
|
63
|
+
0,
|
|
64
|
+
0,
|
|
65
|
+
1
|
|
66
|
+
]
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const invert = (m: Mat3): Mat3 | null => {
|
|
70
|
+
// match ck.Matrix.invert (Sarrus)
|
|
71
|
+
const det =
|
|
72
|
+
m[0] * m[4] * m[8] +
|
|
73
|
+
m[1] * m[5] * m[6] +
|
|
74
|
+
m[2] * m[3] * m[7] -
|
|
75
|
+
m[2] * m[4] * m[6] -
|
|
76
|
+
m[1] * m[3] * m[8] -
|
|
77
|
+
m[0] * m[5] * m[7]
|
|
78
|
+
if (!det) return null
|
|
79
|
+
|
|
80
|
+
return [
|
|
81
|
+
(m[4] * m[8] - m[5] * m[7]) / det,
|
|
82
|
+
(m[2] * m[7] - m[1] * m[8]) / det,
|
|
83
|
+
(m[1] * m[5] - m[2] * m[4]) / det,
|
|
84
|
+
|
|
85
|
+
(m[5] * m[6] - m[3] * m[8]) / det,
|
|
86
|
+
(m[0] * m[8] - m[2] * m[6]) / det,
|
|
87
|
+
(m[2] * m[3] - m[0] * m[5]) / det,
|
|
88
|
+
|
|
89
|
+
(m[3] * m[7] - m[4] * m[6]) / det,
|
|
90
|
+
(m[1] * m[6] - m[0] * m[7]) / det,
|
|
91
|
+
(m[0] * m[4] - m[1] * m[3]) / det
|
|
92
|
+
]
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const mapPoints = (matrix: Mat3, ptArr: number[]): number[] => {
|
|
96
|
+
if (ptArr.length % 2) throw new Error('mapPoints requires even length [x,y,...].')
|
|
97
|
+
const out = ptArr.slice()
|
|
98
|
+
for (let i = 0; i < out.length; i += 2) {
|
|
99
|
+
const x = out[i]
|
|
100
|
+
const y = out[i + 1]
|
|
101
|
+
const denom = matrix[6] * x + matrix[7] * y + matrix[8]
|
|
102
|
+
const xTrans = matrix[0] * x + matrix[1] * y + matrix[2]
|
|
103
|
+
const yTrans = matrix[3] * x + matrix[4] * y + matrix[5]
|
|
104
|
+
out[i] = xTrans / denom
|
|
105
|
+
out[i + 1] = yTrans / denom
|
|
106
|
+
}
|
|
107
|
+
return out
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const mapPoint = (m: Mat3, p: Vector): Vector => {
|
|
111
|
+
const arr = mapPoints(m, [p.x, p.y])
|
|
112
|
+
return { x: arr[0], y: arr[1] }
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const Matrix = { identity, multiply, translated, rotated, scaled, invert, mapPoints, mapPoint }
|
|
116
|
+
export default Matrix
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { SceneGraph } from './index'
|
|
2
|
+
|
|
3
|
+
export interface SceneMutationImpact {
|
|
4
|
+
changedNodeIds: Set<string>
|
|
5
|
+
previousParentIds: Set<string>
|
|
6
|
+
currentParentIds: Set<string>
|
|
7
|
+
createdNodeIds: Set<string>
|
|
8
|
+
deletedNodeIds: Set<string>
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function createSceneMutationImpact(): SceneMutationImpact {
|
|
12
|
+
return {
|
|
13
|
+
changedNodeIds: new Set(),
|
|
14
|
+
previousParentIds: new Set(),
|
|
15
|
+
currentParentIds: new Set(),
|
|
16
|
+
createdNodeIds: new Set(),
|
|
17
|
+
deletedNodeIds: new Set()
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type MutationImpactListener = (impact: SceneMutationImpact) => void
|
|
22
|
+
|
|
23
|
+
export interface CollectedSceneMutation<T> {
|
|
24
|
+
result: T
|
|
25
|
+
impact: SceneMutationImpact
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Collects the actual graph nodes and parent containers touched by an operation. */
|
|
29
|
+
export async function collectSceneMutation<T>(
|
|
30
|
+
graph: SceneGraph,
|
|
31
|
+
operation: () => T | Promise<T>
|
|
32
|
+
): Promise<CollectedSceneMutation<T>> {
|
|
33
|
+
const impact = createSceneMutationImpact()
|
|
34
|
+
const unbind = graph.onNodeEvents({
|
|
35
|
+
created: (node) => {
|
|
36
|
+
impact.createdNodeIds.add(node.id)
|
|
37
|
+
impact.changedNodeIds.add(node.id)
|
|
38
|
+
if (node.parentId) impact.currentParentIds.add(node.parentId)
|
|
39
|
+
},
|
|
40
|
+
updated: (id) => impact.changedNodeIds.add(id),
|
|
41
|
+
deleted: (id, parentId) => {
|
|
42
|
+
if (parentId) impact.previousParentIds.add(parentId)
|
|
43
|
+
impact.deletedNodeIds.add(id)
|
|
44
|
+
impact.changedNodeIds.add(id)
|
|
45
|
+
},
|
|
46
|
+
reparented: (nodeId, oldParentId, newParentId) => {
|
|
47
|
+
impact.changedNodeIds.add(nodeId)
|
|
48
|
+
if (oldParentId) impact.previousParentIds.add(oldParentId)
|
|
49
|
+
impact.currentParentIds.add(newParentId)
|
|
50
|
+
},
|
|
51
|
+
reordered: (nodeId, parentId, _index, previousParentId) => {
|
|
52
|
+
impact.changedNodeIds.add(nodeId)
|
|
53
|
+
if (previousParentId && previousParentId !== parentId) {
|
|
54
|
+
impact.previousParentIds.add(previousParentId)
|
|
55
|
+
}
|
|
56
|
+
impact.currentParentIds.add(parentId)
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
try {
|
|
60
|
+
return { result: await operation(), impact }
|
|
61
|
+
} finally {
|
|
62
|
+
unbind()
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function mutationLayoutScopeIds(impact: SceneMutationImpact): string[] {
|
|
67
|
+
return [
|
|
68
|
+
...new Set([...impact.changedNodeIds, ...impact.previousParentIds, ...impact.currentParentIds])
|
|
69
|
+
]
|
|
70
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { BLACK, DEFAULT_FONT_FAMILY, DEFAULT_STROKE_MITER_LIMIT } from './constants'
|
|
2
|
+
import { createInstanceOverrideState } from './instance-overrides'
|
|
3
|
+
import type { NodeType, SceneNode, SourceMetadata } from './types'
|
|
4
|
+
|
|
5
|
+
export function createDefaultSourceMetadata(): SourceMetadata {
|
|
6
|
+
return {
|
|
7
|
+
format: null,
|
|
8
|
+
id: null,
|
|
9
|
+
orderKey: null,
|
|
10
|
+
editedFields: [],
|
|
11
|
+
fig: {
|
|
12
|
+
rawSize: null,
|
|
13
|
+
rawTransform: null,
|
|
14
|
+
rawNodeFields: {},
|
|
15
|
+
layout: null,
|
|
16
|
+
symbolOverrides: [],
|
|
17
|
+
componentPropAssignments: [],
|
|
18
|
+
derivedSymbolData: [],
|
|
19
|
+
derivedSymbolDataLayoutVersion: null,
|
|
20
|
+
uniformScaleFactor: null
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function createDefaultNode(
|
|
26
|
+
generateId: () => string,
|
|
27
|
+
type: NodeType,
|
|
28
|
+
overrides: Partial<SceneNode> = {}
|
|
29
|
+
): SceneNode {
|
|
30
|
+
return {
|
|
31
|
+
id: generateId(),
|
|
32
|
+
type,
|
|
33
|
+
name: type.charAt(0) + type.slice(1).toLowerCase(),
|
|
34
|
+
parentId: null,
|
|
35
|
+
childIds: [],
|
|
36
|
+
x: 0,
|
|
37
|
+
y: 0,
|
|
38
|
+
width: 100,
|
|
39
|
+
height: 100,
|
|
40
|
+
rotation: 0,
|
|
41
|
+
source: createDefaultSourceMetadata(),
|
|
42
|
+
derivedLayout: null,
|
|
43
|
+
fills:
|
|
44
|
+
type === 'TEXT' ? [{ type: 'SOLID' as const, color: BLACK, opacity: 1, visible: true }] : [],
|
|
45
|
+
strokes: [],
|
|
46
|
+
effects: [],
|
|
47
|
+
layoutGrids: [],
|
|
48
|
+
guides: [],
|
|
49
|
+
fillStyleId: null,
|
|
50
|
+
strokeStyleId: null,
|
|
51
|
+
textStyleId: null,
|
|
52
|
+
effectStyleId: null,
|
|
53
|
+
gridStyleId: null,
|
|
54
|
+
sharedStyleType: null,
|
|
55
|
+
opacity: 1,
|
|
56
|
+
cornerRadius: 0,
|
|
57
|
+
topLeftRadius: 0,
|
|
58
|
+
topRightRadius: 0,
|
|
59
|
+
bottomRightRadius: 0,
|
|
60
|
+
bottomLeftRadius: 0,
|
|
61
|
+
independentCorners: false,
|
|
62
|
+
cornerSmoothing: 0,
|
|
63
|
+
visible: true,
|
|
64
|
+
locked: false,
|
|
65
|
+
clipsContent: false,
|
|
66
|
+
text: '',
|
|
67
|
+
fontSize: 14,
|
|
68
|
+
fontFamily: DEFAULT_FONT_FAMILY,
|
|
69
|
+
fontWeight: 400,
|
|
70
|
+
italic: false,
|
|
71
|
+
textAlignHorizontal: 'LEFT',
|
|
72
|
+
textDirection: 'AUTO',
|
|
73
|
+
textLanguage: null,
|
|
74
|
+
leadingTrim: 'NONE',
|
|
75
|
+
lineHeight: null,
|
|
76
|
+
letterSpacing: 0,
|
|
77
|
+
layoutMode: 'NONE',
|
|
78
|
+
layoutDirection: 'AUTO',
|
|
79
|
+
layoutWrap: 'NO_WRAP',
|
|
80
|
+
primaryAxisAlign: 'MIN',
|
|
81
|
+
counterAxisAlign: 'MIN',
|
|
82
|
+
primaryAxisSizing: 'FIXED',
|
|
83
|
+
counterAxisSizing: 'FIXED',
|
|
84
|
+
itemSpacing: 0,
|
|
85
|
+
counterAxisSpacing: 0,
|
|
86
|
+
paddingTop: 0,
|
|
87
|
+
paddingRight: 0,
|
|
88
|
+
paddingBottom: 0,
|
|
89
|
+
paddingLeft: 0,
|
|
90
|
+
blendMode: 'PASS_THROUGH',
|
|
91
|
+
layoutPositioning: 'AUTO',
|
|
92
|
+
layoutGrow: 0,
|
|
93
|
+
layoutAlignSelf: 'AUTO',
|
|
94
|
+
vectorNetwork: null,
|
|
95
|
+
handleMirroring: 'NONE',
|
|
96
|
+
fillGeometry: [],
|
|
97
|
+
strokeGeometry: [],
|
|
98
|
+
arcData: null,
|
|
99
|
+
textAlignVertical: 'TOP',
|
|
100
|
+
textAutoResize: 'NONE',
|
|
101
|
+
textCase: 'ORIGINAL',
|
|
102
|
+
textDecoration: 'NONE',
|
|
103
|
+
textDecorationStyle: 'SOLID',
|
|
104
|
+
textDecorationThickness: null,
|
|
105
|
+
textDecorationFills: [],
|
|
106
|
+
textDecorationSkipInk: true,
|
|
107
|
+
textUnderlineOffset: null,
|
|
108
|
+
maxLines: null,
|
|
109
|
+
styleRuns: [],
|
|
110
|
+
fontVariations: [],
|
|
111
|
+
fontFeatures: [],
|
|
112
|
+
horizontalConstraint: 'MIN',
|
|
113
|
+
verticalConstraint: 'MIN',
|
|
114
|
+
strokeCap: 'NONE',
|
|
115
|
+
strokeJoin: 'MITER',
|
|
116
|
+
dashPattern: [],
|
|
117
|
+
borderTopWeight: 0,
|
|
118
|
+
borderRightWeight: 0,
|
|
119
|
+
borderBottomWeight: 0,
|
|
120
|
+
borderLeftWeight: 0,
|
|
121
|
+
independentStrokeWeights: false,
|
|
122
|
+
strokeMiterLimit: DEFAULT_STROKE_MITER_LIMIT,
|
|
123
|
+
minWidth: null,
|
|
124
|
+
maxWidth: null,
|
|
125
|
+
minHeight: null,
|
|
126
|
+
maxHeight: null,
|
|
127
|
+
isMask: false,
|
|
128
|
+
maskType: 'ALPHA',
|
|
129
|
+
maskIsOutline: false,
|
|
130
|
+
gridTemplateColumns: [],
|
|
131
|
+
gridTemplateRows: [],
|
|
132
|
+
gridColumnGap: 0,
|
|
133
|
+
gridRowGap: 0,
|
|
134
|
+
gridPosition: null,
|
|
135
|
+
counterAxisAlignContent: 'AUTO',
|
|
136
|
+
itemReverseZIndex: false,
|
|
137
|
+
strokesIncludedInLayout: false,
|
|
138
|
+
expanded: true,
|
|
139
|
+
textTruncation: 'DISABLED',
|
|
140
|
+
autoRename: true,
|
|
141
|
+
pointCount: 5,
|
|
142
|
+
starInnerRadius: 0.38,
|
|
143
|
+
componentId: null,
|
|
144
|
+
instanceOverrides: createInstanceOverrideState(),
|
|
145
|
+
componentPropertyDefinitions: [],
|
|
146
|
+
componentPropertyReferences: [],
|
|
147
|
+
componentPropertyAssignments: {},
|
|
148
|
+
componentPropertyValues: {},
|
|
149
|
+
componentKey: null,
|
|
150
|
+
sourceLibraryKey: null,
|
|
151
|
+
publishId: null,
|
|
152
|
+
overrideKey: null,
|
|
153
|
+
sharedSymbolVersion: null,
|
|
154
|
+
publishedVersion: null,
|
|
155
|
+
librarySource: null,
|
|
156
|
+
isPublishable: false,
|
|
157
|
+
isSymbolPublishable: false,
|
|
158
|
+
symbolDescription: '',
|
|
159
|
+
symbolLinks: [],
|
|
160
|
+
variantPropSpecs: [],
|
|
161
|
+
boundVariables: {},
|
|
162
|
+
variableModes: {},
|
|
163
|
+
exportSettings: [],
|
|
164
|
+
pluginData: [],
|
|
165
|
+
pluginRelaunchData: [],
|
|
166
|
+
internalOnly: false,
|
|
167
|
+
flipX: false,
|
|
168
|
+
flipY: false,
|
|
169
|
+
textPicture: null,
|
|
170
|
+
derivedTextGlyphs: null,
|
|
171
|
+
textPathData: null,
|
|
172
|
+
textPathBox: null,
|
|
173
|
+
...overrides
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export const CONTAINER_TYPES = new Set<NodeType>([
|
|
178
|
+
'CANVAS',
|
|
179
|
+
'FRAME',
|
|
180
|
+
'GROUP',
|
|
181
|
+
'BOOLEAN_OPERATION',
|
|
182
|
+
'SECTION',
|
|
183
|
+
'COMPONENT',
|
|
184
|
+
'COMPONENT_SET',
|
|
185
|
+
'INSTANCE'
|
|
186
|
+
])
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import svgpath from 'svgpath'
|
|
2
|
+
|
|
3
|
+
import type { VectorNetwork, VectorVertex, VectorSegment, VectorRegion, WindingRule } from './index'
|
|
4
|
+
|
|
5
|
+
interface SubPath {
|
|
6
|
+
startVertexIndex: number
|
|
7
|
+
segmentIndices: number[]
|
|
8
|
+
closed: boolean
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type VectorPathWindingRule = WindingRule | 'NONE'
|
|
12
|
+
export type SVGPathParseResult = { ok: true; network: VectorNetwork } | { ok: false; error: string }
|
|
13
|
+
|
|
14
|
+
interface ParseOptions {
|
|
15
|
+
includeOpenRegions: boolean
|
|
16
|
+
strictCommands: boolean
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Parse an SVG path `d` attribute into a VectorNetwork.
|
|
21
|
+
*
|
|
22
|
+
* Uses `svgpath` to normalize all commands to absolute M/L/C/Z
|
|
23
|
+
* (arcs → cubics via `.unarc()`, smooth curves → explicit via `.unshort()`).
|
|
24
|
+
*/
|
|
25
|
+
export function parseSVGPath(d: string, windingRule: WindingRule = 'NONZERO'): VectorNetwork {
|
|
26
|
+
const parsed = parsePath(d, windingRule, {
|
|
27
|
+
includeOpenRegions: false,
|
|
28
|
+
strictCommands: false
|
|
29
|
+
})
|
|
30
|
+
return parsed.ok ? parsed.network : { vertices: [], segments: [], regions: [] }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Parse the strict absolute command subset accepted by Figma's VectorPath API. */
|
|
34
|
+
export function parsePluginVectorPath(
|
|
35
|
+
d: string,
|
|
36
|
+
windingRule: VectorPathWindingRule
|
|
37
|
+
): SVGPathParseResult {
|
|
38
|
+
return parsePath(d, windingRule, {
|
|
39
|
+
includeOpenRegions: windingRule !== 'NONE',
|
|
40
|
+
strictCommands: true
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function parsePath(
|
|
45
|
+
d: string,
|
|
46
|
+
windingRule: VectorPathWindingRule,
|
|
47
|
+
options: ParseOptions
|
|
48
|
+
): SVGPathParseResult {
|
|
49
|
+
const vertices: VectorVertex[] = []
|
|
50
|
+
const segments: VectorSegment[] = []
|
|
51
|
+
const subPaths: SubPath[] = []
|
|
52
|
+
|
|
53
|
+
let currentSubPath: SubPath | null = null
|
|
54
|
+
let cx = 0
|
|
55
|
+
let cy = 0
|
|
56
|
+
|
|
57
|
+
const vertexMap = new Map<string, number>()
|
|
58
|
+
|
|
59
|
+
function getOrCreateVertex(x: number, y: number): number {
|
|
60
|
+
const key = `${x},${y}`
|
|
61
|
+
const existing = vertexMap.get(key)
|
|
62
|
+
if (existing !== undefined) return existing
|
|
63
|
+
const idx = vertices.length
|
|
64
|
+
vertices.push({ x, y })
|
|
65
|
+
vertexMap.set(key, idx)
|
|
66
|
+
return idx
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function addSegment(
|
|
70
|
+
x1: number,
|
|
71
|
+
y1: number,
|
|
72
|
+
x2: number,
|
|
73
|
+
y2: number,
|
|
74
|
+
tx1: number,
|
|
75
|
+
ty1: number,
|
|
76
|
+
tx2: number,
|
|
77
|
+
ty2: number
|
|
78
|
+
): void {
|
|
79
|
+
const startIdx = getOrCreateVertex(x1, y1)
|
|
80
|
+
const endIdx = getOrCreateVertex(x2, y2)
|
|
81
|
+
const segIdx = segments.length
|
|
82
|
+
segments.push({
|
|
83
|
+
start: startIdx,
|
|
84
|
+
end: endIdx,
|
|
85
|
+
tangentStart: { x: tx1 - x1, y: ty1 - y1 },
|
|
86
|
+
tangentEnd: { x: tx2 - x2, y: ty2 - y2 }
|
|
87
|
+
})
|
|
88
|
+
if (currentSubPath) {
|
|
89
|
+
currentSubPath.segmentIndices.push(segIdx)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function addLine(x1: number, y1: number, x2: number, y2: number): void {
|
|
94
|
+
addSegment(x1, y1, x2, y2, x1, y1, x2, y2)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function addCubic(
|
|
98
|
+
x1: number,
|
|
99
|
+
y1: number,
|
|
100
|
+
cp1x: number,
|
|
101
|
+
cp1y: number,
|
|
102
|
+
cp2x: number,
|
|
103
|
+
cp2y: number,
|
|
104
|
+
x2: number,
|
|
105
|
+
y2: number
|
|
106
|
+
): void {
|
|
107
|
+
addSegment(x1, y1, x2, y2, cp1x, cp1y, cp2x, cp2y)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const parsed = svgpath(d)
|
|
111
|
+
const parseError = 'err' in parsed && typeof parsed.err === 'string' ? parsed.err : null
|
|
112
|
+
if (parseError) return { ok: false, error: parseError }
|
|
113
|
+
if (options.strictCommands) {
|
|
114
|
+
const unsupportedCommands = new Set<string>()
|
|
115
|
+
parsed.iterate((segment) => {
|
|
116
|
+
if (!['M', 'L', 'Q', 'C', 'Z'].includes(segment[0])) unsupportedCommands.add(segment[0])
|
|
117
|
+
})
|
|
118
|
+
const unsupportedCommand = unsupportedCommands.values().next().value
|
|
119
|
+
if (unsupportedCommand !== undefined) {
|
|
120
|
+
return { ok: false, error: `Unsupported path command ${unsupportedCommand}` }
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const normalized = parsed.abs().unshort().unarc()
|
|
124
|
+
|
|
125
|
+
normalized.iterate((seg) => {
|
|
126
|
+
const cmd = seg[0]
|
|
127
|
+
|
|
128
|
+
if (cmd === 'M') {
|
|
129
|
+
cx = seg[1]
|
|
130
|
+
cy = seg[2]
|
|
131
|
+
currentSubPath = {
|
|
132
|
+
startVertexIndex: getOrCreateVertex(cx, cy),
|
|
133
|
+
segmentIndices: [],
|
|
134
|
+
closed: false
|
|
135
|
+
}
|
|
136
|
+
subPaths.push(currentSubPath)
|
|
137
|
+
} else if (cmd === 'L') {
|
|
138
|
+
addLine(cx, cy, seg[1], seg[2])
|
|
139
|
+
cx = seg[1]
|
|
140
|
+
cy = seg[2]
|
|
141
|
+
} else if (cmd === 'H') {
|
|
142
|
+
addLine(cx, cy, seg[1], cy)
|
|
143
|
+
cx = seg[1]
|
|
144
|
+
} else if (cmd === 'V') {
|
|
145
|
+
addLine(cx, cy, cx, seg[1])
|
|
146
|
+
cy = seg[1]
|
|
147
|
+
} else if (cmd === 'C') {
|
|
148
|
+
addCubic(cx, cy, seg[1], seg[2], seg[3], seg[4], seg[5], seg[6])
|
|
149
|
+
cx = seg[5]
|
|
150
|
+
cy = seg[6]
|
|
151
|
+
} else if (cmd === 'Q') {
|
|
152
|
+
const qx = seg[1]
|
|
153
|
+
const qy = seg[2]
|
|
154
|
+
const ex = seg[3]
|
|
155
|
+
const ey = seg[4]
|
|
156
|
+
const cp1x = cx + (2 / 3) * (qx - cx)
|
|
157
|
+
const cp1y = cy + (2 / 3) * (qy - cy)
|
|
158
|
+
const cp2x = ex + (2 / 3) * (qx - ex)
|
|
159
|
+
const cp2y = ey + (2 / 3) * (qy - ey)
|
|
160
|
+
addCubic(cx, cy, cp1x, cp1y, cp2x, cp2y, ex, ey)
|
|
161
|
+
cx = ex
|
|
162
|
+
cy = ey
|
|
163
|
+
} else if (cmd === 'Z' || cmd === 'z') {
|
|
164
|
+
if (currentSubPath) {
|
|
165
|
+
const startVert = vertices[currentSubPath.startVertexIndex]
|
|
166
|
+
if (Math.abs(cx - startVert.x) > 0.001 || Math.abs(cy - startVert.y) > 0.001) {
|
|
167
|
+
addLine(cx, cy, startVert.x, startVert.y)
|
|
168
|
+
}
|
|
169
|
+
currentSubPath.closed = true
|
|
170
|
+
cx = startVert.x
|
|
171
|
+
cy = startVert.y
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
const regions: VectorRegion[] = []
|
|
177
|
+
const regionPaths = subPaths.filter(
|
|
178
|
+
(subPath) =>
|
|
179
|
+
windingRule !== 'NONE' &&
|
|
180
|
+
subPath.segmentIndices.length > 0 &&
|
|
181
|
+
(subPath.closed || options.includeOpenRegions)
|
|
182
|
+
)
|
|
183
|
+
if (regionPaths.length > 0 && windingRule !== 'NONE') {
|
|
184
|
+
regions.push({
|
|
185
|
+
windingRule,
|
|
186
|
+
loops: regionPaths.map((subPath) => subPath.segmentIndices)
|
|
187
|
+
})
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return { ok: true, network: { vertices, segments, regions } }
|
|
191
|
+
}
|