@open-pencil/scene-graph 0.13.2 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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,226 @@
|
|
|
1
|
+
import type { SceneGraph, SceneNode } from './index'
|
|
2
|
+
import Matrix, { type Mat3 } from './matrix'
|
|
3
|
+
import type { Rect, Vector } from './primitives'
|
|
4
|
+
|
|
5
|
+
export function getWorldMatrix(node: SceneNode, graph: Pick<SceneGraph, 'getNode'>): Mat3 {
|
|
6
|
+
const chain: SceneNode[] = []
|
|
7
|
+
let current: SceneNode | undefined = node
|
|
8
|
+
|
|
9
|
+
while (current) {
|
|
10
|
+
chain.unshift(current)
|
|
11
|
+
if (!current.parentId) break
|
|
12
|
+
current = graph.getNode(current.parentId)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
let matrix = Matrix.identity()
|
|
16
|
+
|
|
17
|
+
for (const n of chain) {
|
|
18
|
+
const local = getNodeLocalMatrix(n)
|
|
19
|
+
matrix = Matrix.multiply(matrix, local)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return matrix
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function getAxisAlignedWorldBounds(node: SceneNode, graph: Pick<SceneGraph, 'getNode'>) {
|
|
26
|
+
const matrix = getWorldMatrix(node, graph)
|
|
27
|
+
const points = Matrix.mapPoints(matrix, [
|
|
28
|
+
0,
|
|
29
|
+
0,
|
|
30
|
+
node.width,
|
|
31
|
+
0,
|
|
32
|
+
node.width,
|
|
33
|
+
node.height,
|
|
34
|
+
0,
|
|
35
|
+
node.height
|
|
36
|
+
])
|
|
37
|
+
const xs = [points[0], points[2], points[4], points[6]]
|
|
38
|
+
const ys = [points[1], points[3], points[5], points[7]]
|
|
39
|
+
const minX = Math.min(...xs)
|
|
40
|
+
const maxX = Math.max(...xs)
|
|
41
|
+
const minY = Math.min(...ys)
|
|
42
|
+
const maxY = Math.max(...ys)
|
|
43
|
+
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function getAbsolutePosition(node: SceneNode, graph: SceneGraph): Vector {
|
|
47
|
+
const matrix = getWorldMatrix(node, graph)
|
|
48
|
+
const p = Matrix.mapPoints(matrix, [0, 0])
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
x: p[0],
|
|
52
|
+
y: p[1]
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export function getAbsoluteRotation(node: SceneNode, graph: SceneGraph): number {
|
|
56
|
+
const matrix = getWorldMatrix(node, graph)
|
|
57
|
+
const a = matrix[0]
|
|
58
|
+
const b = matrix[1]
|
|
59
|
+
const angle = Math.atan2(b, a)
|
|
60
|
+
let deg = (angle * 180) / Math.PI
|
|
61
|
+
deg = (deg + 360) % 360
|
|
62
|
+
|
|
63
|
+
return deg
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function getAbsolutePositionFull(node: SceneNode, graph: SceneGraph) {
|
|
67
|
+
const matrix = getWorldMatrix(node, graph)
|
|
68
|
+
|
|
69
|
+
const origin = Matrix.mapPoints(matrix, [0, 0])
|
|
70
|
+
const x = origin[0]
|
|
71
|
+
const y = origin[1]
|
|
72
|
+
|
|
73
|
+
const pts = Matrix.mapPoints(matrix, [
|
|
74
|
+
0,
|
|
75
|
+
0,
|
|
76
|
+
node.width,
|
|
77
|
+
0,
|
|
78
|
+
node.width,
|
|
79
|
+
node.height,
|
|
80
|
+
0,
|
|
81
|
+
node.height
|
|
82
|
+
])
|
|
83
|
+
|
|
84
|
+
const [x1, y1, x2, y2, x3, y3, x4, y4] = pts
|
|
85
|
+
|
|
86
|
+
const minX = Math.min(x1, x2, x3, x4)
|
|
87
|
+
const maxX = Math.max(x1, x2, x3, x4)
|
|
88
|
+
const minY = Math.min(y1, y2, y3, y4)
|
|
89
|
+
const maxY = Math.max(y1, y2, y3, y4)
|
|
90
|
+
|
|
91
|
+
const width = maxX - minX
|
|
92
|
+
const height = maxY - minY
|
|
93
|
+
|
|
94
|
+
let angle = Math.atan2(matrix[3], matrix[0])
|
|
95
|
+
|
|
96
|
+
const det = matrix[0] * matrix[4] - matrix[1] * matrix[3]
|
|
97
|
+
if (det < 0) {
|
|
98
|
+
angle = -angle
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const rotation = angle * (180 / Math.PI)
|
|
102
|
+
|
|
103
|
+
const center = Matrix.mapPoints(matrix, [node.width / 2, node.height / 2])
|
|
104
|
+
|
|
105
|
+
const centerX = center[0]
|
|
106
|
+
const centerY = center[1]
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
x,
|
|
110
|
+
y,
|
|
111
|
+
|
|
112
|
+
// AABB
|
|
113
|
+
boundX: minX,
|
|
114
|
+
boundY: minY,
|
|
115
|
+
width,
|
|
116
|
+
height,
|
|
117
|
+
|
|
118
|
+
rotation,
|
|
119
|
+
|
|
120
|
+
centerX,
|
|
121
|
+
centerY
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/** Lines retain their origin pivot; other shapes rotate around their center. */
|
|
125
|
+
export function getNodeRotationOrigin(node: SceneNode): Vector {
|
|
126
|
+
return node.type === 'LINE' ? { x: 0, y: 0 } : { x: node.width / 2, y: node.height / 2 }
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Position can be applied separately by renderers before setting up local opacity layers. */
|
|
130
|
+
export function getNodeLocalMatrix(n: SceneNode, position: Vector = n) {
|
|
131
|
+
let matrix = Matrix.translated(position.x, position.y)
|
|
132
|
+
if (n.flipX || n.flipY) {
|
|
133
|
+
matrix = Matrix.multiply(
|
|
134
|
+
matrix,
|
|
135
|
+
Matrix.scaled(n.flipX ? -1 : 1, n.flipY ? -1 : 1, n.width / 2, n.height / 2)
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
if (n.rotation) {
|
|
139
|
+
const pivot = getNodeRotationOrigin(n)
|
|
140
|
+
matrix = Matrix.multiply(matrix, Matrix.rotated((n.rotation * Math.PI) / 180, pivot.x, pivot.y))
|
|
141
|
+
}
|
|
142
|
+
return matrix
|
|
143
|
+
}
|
|
144
|
+
export function getNodeWorldBounds(node: SceneNode) {
|
|
145
|
+
const m = getNodeLocalMatrix(node)
|
|
146
|
+
|
|
147
|
+
const points = Matrix.mapPoints(m, [0, 0, node.width, 0, node.width, node.height, 0, node.height])
|
|
148
|
+
|
|
149
|
+
let minX = Infinity
|
|
150
|
+
let minY = Infinity
|
|
151
|
+
let maxX = -Infinity
|
|
152
|
+
let maxY = -Infinity
|
|
153
|
+
|
|
154
|
+
for (let i = 0; i < points.length; i += 2) {
|
|
155
|
+
const x = points[i]
|
|
156
|
+
const y = points[i + 1]
|
|
157
|
+
|
|
158
|
+
minX = Math.min(minX, x)
|
|
159
|
+
minY = Math.min(minY, y)
|
|
160
|
+
maxX = Math.max(maxX, x)
|
|
161
|
+
maxY = Math.max(maxY, y)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
x: minX,
|
|
166
|
+
y: minY,
|
|
167
|
+
width: maxX - minX,
|
|
168
|
+
height: maxY - minY
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* World-space positions of the 8 selection handles. `localRect` overrides the
|
|
174
|
+
* node-local box the handles sit on — text-on-path draws its handles on the
|
|
175
|
+
* glyph-fitted path box, not node bounds, so its hit-test must pass that box
|
|
176
|
+
* or the visible handles are ~25px off and unclickable. Defaults to full node
|
|
177
|
+
* bounds.
|
|
178
|
+
*/
|
|
179
|
+
export function getWorldHandles(
|
|
180
|
+
node: SceneNode,
|
|
181
|
+
graph: Pick<SceneGraph, 'getNode'>,
|
|
182
|
+
localRect?: Rect
|
|
183
|
+
) {
|
|
184
|
+
const matrix = getWorldMatrix(node, graph)
|
|
185
|
+
|
|
186
|
+
const x0 = localRect?.x ?? 0
|
|
187
|
+
const y0 = localRect?.y ?? 0
|
|
188
|
+
const w = localRect?.width ?? node.width
|
|
189
|
+
const h = localRect?.height ?? node.height
|
|
190
|
+
const mx = x0 + w / 2
|
|
191
|
+
const my = y0 + h / 2
|
|
192
|
+
const x1 = x0 + w
|
|
193
|
+
const y1 = y0 + h
|
|
194
|
+
|
|
195
|
+
const localPts = [
|
|
196
|
+
x0,
|
|
197
|
+
y0, // nw
|
|
198
|
+
mx,
|
|
199
|
+
y0, // n
|
|
200
|
+
x1,
|
|
201
|
+
y0, // ne
|
|
202
|
+
x1,
|
|
203
|
+
my, // e
|
|
204
|
+
x1,
|
|
205
|
+
y1, // se
|
|
206
|
+
mx,
|
|
207
|
+
y1, // s
|
|
208
|
+
x0,
|
|
209
|
+
y1, // sw
|
|
210
|
+
x0,
|
|
211
|
+
my // w
|
|
212
|
+
]
|
|
213
|
+
|
|
214
|
+
const pts = Matrix.mapPoints(matrix, localPts)
|
|
215
|
+
|
|
216
|
+
return {
|
|
217
|
+
nw: { x: pts[0], y: pts[1] },
|
|
218
|
+
n: { x: pts[2], y: pts[3] },
|
|
219
|
+
ne: { x: pts[4], y: pts[5] },
|
|
220
|
+
e: { x: pts[6], y: pts[7] },
|
|
221
|
+
se: { x: pts[8], y: pts[9] },
|
|
222
|
+
s: { x: pts[10], y: pts[11] },
|
|
223
|
+
sw: { x: pts[12], y: pts[13] },
|
|
224
|
+
w: { x: pts[14], y: pts[15] }
|
|
225
|
+
}
|
|
226
|
+
}
|
package/src/copy.ts
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed shallow-copy helpers for Fill, Stroke, Effect, and StyleRun.
|
|
3
|
+
*
|
|
4
|
+
* These replace `structuredClone` for known scene-graph array types,
|
|
5
|
+
* avoiding the ~24× overhead of the generic deep-clone algorithm.
|
|
6
|
+
* Each helper spreads the top-level object and any nested objects
|
|
7
|
+
* (color, offset, gradientStops, dashPattern, style) to ensure
|
|
8
|
+
* no shared references between source and copy.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type {
|
|
12
|
+
ArcData,
|
|
13
|
+
ComponentPropertyDefinition,
|
|
14
|
+
Effect,
|
|
15
|
+
DerivedTextGlyph,
|
|
16
|
+
Fill,
|
|
17
|
+
GeometryPath,
|
|
18
|
+
GradientStop,
|
|
19
|
+
LayoutGrid,
|
|
20
|
+
SceneNode,
|
|
21
|
+
Stroke,
|
|
22
|
+
StyleRun
|
|
23
|
+
} from './'
|
|
24
|
+
import { geometryCommandCoordCount } from './geometry'
|
|
25
|
+
import { cloneInstanceOverrideState } from './instance-overrides'
|
|
26
|
+
import { createDefaultSourceMetadata } from './node-defaults'
|
|
27
|
+
import { cloneVectorNetwork } from './vector-network'
|
|
28
|
+
|
|
29
|
+
// --- Individual copy functions ---
|
|
30
|
+
|
|
31
|
+
export function copyFill(f: Fill): Fill {
|
|
32
|
+
const copy: Fill = { ...f, color: { ...f.color } }
|
|
33
|
+
if (f.gradientStops) copy.gradientStops = f.gradientStops.map(copyGradientStop)
|
|
34
|
+
if (f.gradientTransform) copy.gradientTransform = { ...f.gradientTransform }
|
|
35
|
+
if (f.imageTransform) copy.imageTransform = { ...f.imageTransform }
|
|
36
|
+
if (f.patternSpacing) copy.patternSpacing = { ...f.patternSpacing }
|
|
37
|
+
if (f.noiseSize) copy.noiseSize = { ...f.noiseSize }
|
|
38
|
+
return copy
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function copyStroke(s: Stroke): Stroke {
|
|
42
|
+
const copy: Stroke = { ...s, color: { ...s.color } }
|
|
43
|
+
if (s.dashPattern) {
|
|
44
|
+
copy.dashPattern = [...s.dashPattern]
|
|
45
|
+
}
|
|
46
|
+
return copy
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function copyEffect(e: Effect): Effect {
|
|
50
|
+
return {
|
|
51
|
+
...e,
|
|
52
|
+
color: { ...e.color },
|
|
53
|
+
offset: { ...e.offset }
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function copyStyleRun(r: StyleRun): StyleRun {
|
|
58
|
+
return {
|
|
59
|
+
...r,
|
|
60
|
+
style: {
|
|
61
|
+
...r.style,
|
|
62
|
+
fills: r.style.fills ? r.style.fills.map(copyFill) : undefined,
|
|
63
|
+
textDecorationFills: r.style.textDecorationFills
|
|
64
|
+
? r.style.textDecorationFills.map(copyFill)
|
|
65
|
+
: undefined,
|
|
66
|
+
fontVariations: r.style.fontVariations
|
|
67
|
+
? r.style.fontVariations.map((v) => ({ ...v }))
|
|
68
|
+
: undefined,
|
|
69
|
+
fontFeatures: r.style.fontFeatures ? r.style.fontFeatures.map((v) => ({ ...v })) : undefined
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// --- Array copy functions ---
|
|
75
|
+
|
|
76
|
+
const internalCopySources = new WeakMap<object, object>()
|
|
77
|
+
|
|
78
|
+
/** Record immutable lineage for an internal deep copy without sharing mutable values. */
|
|
79
|
+
export function markCopySource<T extends object>(source: T, copy: T): T {
|
|
80
|
+
internalCopySources.set(copy, internalCopySources.get(source) ?? source)
|
|
81
|
+
return copy
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Compare internal deep copies in O(1) without traversing large paint or text payloads. */
|
|
85
|
+
export function hasSameCopySource(left: object, right: object): boolean {
|
|
86
|
+
if (left === right) return true
|
|
87
|
+
return (internalCopySources.get(left) ?? left) === (internalCopySources.get(right) ?? right)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function copyFills(fills: Fill[]): Fill[] {
|
|
91
|
+
return fills.map(copyFill)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function copyStrokes(strokes: Stroke[]): Stroke[] {
|
|
95
|
+
return strokes.map(copyStroke)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function copyEffects(effects: Effect[]): Effect[] {
|
|
99
|
+
return effects.map(copyEffect)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function copyLayoutGrids(grids: LayoutGrid[]): LayoutGrid[] {
|
|
103
|
+
return grids.map((grid) => ({ ...grid, color: grid.color ? { ...grid.color } : undefined }))
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function copyStyleRuns(runs: StyleRun[]): StyleRun[] {
|
|
107
|
+
return runs.map(copyStyleRun)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Keep path-level fills across copy/scale/transform (resize snapshots). */
|
|
111
|
+
function withPathPaintMeta(path: GeometryPath, commandsBlob: Uint8Array): GeometryPath {
|
|
112
|
+
return {
|
|
113
|
+
windingRule: path.windingRule,
|
|
114
|
+
commandsBlob,
|
|
115
|
+
...(path.fills ? { fills: copyFills(path.fills) } : {})
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function copyGeometryPaths(paths: GeometryPath[]): GeometryPath[] {
|
|
120
|
+
return paths.map((p) => withPathPaintMeta(p, p.commandsBlob.slice()))
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Affine-transform every coordinate pair in a path command blob (returns a
|
|
125
|
+
* copy): (x, y) → (m00·x + m01·y + tx, m10·x + m11·y + ty).
|
|
126
|
+
* Command layout: 1=move/2=line (1 pair), 3=quad (2 pairs), 4=cubic (3 pairs)
|
|
127
|
+
* — font-glyph blobs are quad-heavy, so skipping command 3 desyncs the walk.
|
|
128
|
+
*/
|
|
129
|
+
export function transformGeometryBlob(
|
|
130
|
+
blob: Uint8Array,
|
|
131
|
+
m00: number,
|
|
132
|
+
m01: number,
|
|
133
|
+
m10: number,
|
|
134
|
+
m11: number,
|
|
135
|
+
tx = 0,
|
|
136
|
+
ty = 0
|
|
137
|
+
): Uint8Array {
|
|
138
|
+
const out = blob.slice()
|
|
139
|
+
const dv = new DataView(out.buffer, out.byteOffset, out.byteLength)
|
|
140
|
+
let offset = 0
|
|
141
|
+
while (offset < out.length) {
|
|
142
|
+
const command = out[offset++]
|
|
143
|
+
const coords = geometryCommandCoordCount(command)
|
|
144
|
+
if (coords == null) break
|
|
145
|
+
for (let i = 0; i < coords; i++) {
|
|
146
|
+
if (offset + 8 > out.length) break
|
|
147
|
+
const x = dv.getFloat32(offset, true)
|
|
148
|
+
const y = dv.getFloat32(offset + 4, true)
|
|
149
|
+
dv.setFloat32(offset, m00 * x + m01 * y + tx, true)
|
|
150
|
+
dv.setFloat32(offset + 4, m10 * x + m11 * y + ty, true)
|
|
151
|
+
offset += 8
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return out
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function transformGeometryPaths(
|
|
158
|
+
paths: GeometryPath[],
|
|
159
|
+
m00: number,
|
|
160
|
+
m01: number,
|
|
161
|
+
m10: number,
|
|
162
|
+
m11: number,
|
|
163
|
+
tx = 0,
|
|
164
|
+
ty = 0
|
|
165
|
+
): GeometryPath[] {
|
|
166
|
+
return paths.map((g) =>
|
|
167
|
+
withPathPaintMeta(g, transformGeometryBlob(g.commandsBlob, m00, m01, m10, m11, tx, ty))
|
|
168
|
+
)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Scale path command blob coordinates by (sx, sy).
|
|
173
|
+
* Identity scale returns a deep copy (same as copyGeometryPaths).
|
|
174
|
+
*/
|
|
175
|
+
export function scaleGeometryPaths(paths: GeometryPath[], sx: number, sy: number): GeometryPath[] {
|
|
176
|
+
if (sx === 1 && sy === 1) return copyGeometryPaths(paths)
|
|
177
|
+
return transformGeometryPaths(paths, sx, 0, 0, sy)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// --- Internal helpers ---
|
|
181
|
+
|
|
182
|
+
/** Copy an optional array: non-empty → mapped, empty → [], undefined → undefined. */
|
|
183
|
+
function copyOpt<T, U>(arr: T[] | undefined, fn: (arr: T[]) => U[]): U[] | undefined {
|
|
184
|
+
if (arr === undefined) return undefined
|
|
185
|
+
return arr.length > 0 ? fn(arr) : []
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function copyGradientStop(gs: GradientStop): GradientStop {
|
|
189
|
+
return { color: { ...gs.color }, position: gs.position }
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function copySpread<T extends object>(arr: T[] | undefined): T[] {
|
|
193
|
+
return arr?.map((item) => ({ ...item })) ?? []
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function copyPropertyDefs(
|
|
197
|
+
defs: ComponentPropertyDefinition[] | undefined
|
|
198
|
+
): ComponentPropertyDefinition[] {
|
|
199
|
+
return (
|
|
200
|
+
defs?.map((d) => ({
|
|
201
|
+
...d,
|
|
202
|
+
variantOptions: d.variantOptions ? [...d.variantOptions] : undefined,
|
|
203
|
+
preferredValues: d.preferredValues ? [...d.preferredValues] : undefined
|
|
204
|
+
})) ?? []
|
|
205
|
+
)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Deep-copy path-text glyphs (fresh commandsBlob buffers). */
|
|
209
|
+
export function copyDerivedGlyphs(glyphs: DerivedTextGlyph[] | null): DerivedTextGlyph[] | null {
|
|
210
|
+
return glyphs ? glyphs.map((g) => ({ ...g, commandsBlob: new Uint8Array(g.commandsBlob) })) : null
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// --- Complex structure copy functions ---
|
|
214
|
+
// These replace structuredClone for known types, avoiding its ~24× overhead.
|
|
215
|
+
|
|
216
|
+
function copyArcData(a: ArcData): ArcData {
|
|
217
|
+
return { startingAngle: a.startingAngle, endingAngle: a.endingAngle, innerRadius: a.innerRadius }
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// --- Deep-copy clone props ---
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Build the init props for a deep-copy clone of `src`.
|
|
224
|
+
* Shares logic between SceneGraph.cloneTree and instance child cloning.
|
|
225
|
+
* Explicitly deep-copies all mutable object/array fields that `...rest`
|
|
226
|
+
* would otherwise share by reference. When adding a mutable SceneNode field,
|
|
227
|
+
* add its copy behavior here or document why sharing is intentional.
|
|
228
|
+
*/
|
|
229
|
+
export type NodeCloneMode = 'deep' | 'fig-import'
|
|
230
|
+
|
|
231
|
+
export function cloneNodeProps(
|
|
232
|
+
src: SceneNode,
|
|
233
|
+
componentId: string | null,
|
|
234
|
+
mode: NodeCloneMode = 'deep'
|
|
235
|
+
): Partial<SceneNode> {
|
|
236
|
+
const { id: _, parentId: _p, childIds: _c, ...rest } = src
|
|
237
|
+
if (mode === 'fig-import') {
|
|
238
|
+
return {
|
|
239
|
+
...rest,
|
|
240
|
+
...(componentId !== null ? { componentId } : {}),
|
|
241
|
+
source: createDefaultSourceMetadata(),
|
|
242
|
+
boundVariables: { ...src.boundVariables },
|
|
243
|
+
variableModes: { ...src.variableModes },
|
|
244
|
+
instanceOverrides: cloneInstanceOverrideState(src.instanceOverrides),
|
|
245
|
+
componentPropertyAssignments: { ...src.componentPropertyAssignments },
|
|
246
|
+
|
|
247
|
+
componentPropertyValues: { ...src.componentPropertyValues }
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return {
|
|
251
|
+
...rest,
|
|
252
|
+
...(componentId !== null ? { componentId } : {}),
|
|
253
|
+
boundVariables: { ...src.boundVariables },
|
|
254
|
+
variableModes: { ...src.variableModes },
|
|
255
|
+
instanceOverrides: cloneInstanceOverrideState(src.instanceOverrides),
|
|
256
|
+
fills: copyOpt(src.fills, (value) => markCopySource(value, copyFills(value))),
|
|
257
|
+
|
|
258
|
+
strokes: copyOpt(src.strokes, (value) => markCopySource(value, copyStrokes(value))),
|
|
259
|
+
effects: copyOpt(src.effects, (value) => markCopySource(value, copyEffects(value))),
|
|
260
|
+
layoutGrids: copyOpt(src.layoutGrids, copyLayoutGrids),
|
|
261
|
+
guides: copyOpt(src.guides, (guides) => guides.map((guide) => ({ ...guide }))),
|
|
262
|
+
styleRuns: copyOpt(src.styleRuns, (value) => markCopySource(value, copyStyleRuns(value))),
|
|
263
|
+
// Generated instance descendants have no independent Figma provenance. Retaining the source
|
|
264
|
+
// component's opaque raw payload here duplicates megabytes of metadata per instance.
|
|
265
|
+
source: componentId === null ? structuredClone(src.source) : createDefaultSourceMetadata(),
|
|
266
|
+
dashPattern: copyOpt(src.dashPattern, (a) => [...a]),
|
|
267
|
+
fontVariations: copyOpt(src.fontVariations, (a) => a.map((v) => ({ ...v }))),
|
|
268
|
+
fontFeatures: copyOpt(src.fontFeatures, (a) => a.map((v) => ({ ...v }))),
|
|
269
|
+
textDecorationFills: copyOpt(src.textDecorationFills, copyFills),
|
|
270
|
+
fillGeometry: copyOpt(src.fillGeometry, copyGeometryPaths),
|
|
271
|
+
strokeGeometry: copyOpt(src.strokeGeometry, copyGeometryPaths),
|
|
272
|
+
gridTemplateColumns: copySpread(src.gridTemplateColumns),
|
|
273
|
+
gridTemplateRows: copySpread(src.gridTemplateRows),
|
|
274
|
+
componentPropertyDefinitions: copyPropertyDefs(src.componentPropertyDefinitions),
|
|
275
|
+
componentPropertyReferences: copySpread(src.componentPropertyReferences),
|
|
276
|
+
componentPropertyAssignments: { ...src.componentPropertyAssignments },
|
|
277
|
+
symbolLinks: copySpread(src.symbolLinks),
|
|
278
|
+
variantPropSpecs: copySpread(src.variantPropSpecs),
|
|
279
|
+
pluginData: copySpread(src.pluginData),
|
|
280
|
+
pluginRelaunchData: copySpread(src.pluginRelaunchData),
|
|
281
|
+
exportSettings: copySpread(src.exportSettings),
|
|
282
|
+
componentPropertyValues: { ...src.componentPropertyValues },
|
|
283
|
+
derivedLayout: src.derivedLayout ? { ...src.derivedLayout } : null,
|
|
284
|
+
arcData: src.arcData ? copyArcData(src.arcData) : null,
|
|
285
|
+
vectorNetwork: src.vectorNetwork ? cloneVectorNetwork(src.vectorNetwork) : null,
|
|
286
|
+
textPicture: src.textPicture ? new Uint8Array(src.textPicture) : null,
|
|
287
|
+
derivedTextGlyphs: src.derivedTextGlyphs
|
|
288
|
+
? markCopySource(src.derivedTextGlyphs, copyDerivedGlyphs(src.derivedTextGlyphs) ?? [])
|
|
289
|
+
: null,
|
|
290
|
+
textPathData: src.textPathData ? structuredClone(src.textPathData) : null,
|
|
291
|
+
textPathBox: src.textPathBox ? { ...src.textPathBox } : null,
|
|
292
|
+
gridPosition: src.gridPosition ? { ...src.gridPosition } : null
|
|
293
|
+
}
|
|
294
|
+
}
|
package/src/events.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Emitter } from 'nanoevents'
|
|
2
|
+
|
|
3
|
+
import type { SceneGraphEventHandlers, SceneGraphEvents } from './types'
|
|
4
|
+
|
|
5
|
+
export function bindNodeEvents(
|
|
6
|
+
emitter: Emitter<SceneGraphEvents>,
|
|
7
|
+
handlers: SceneGraphEventHandlers
|
|
8
|
+
): () => void {
|
|
9
|
+
const unbinds = [
|
|
10
|
+
handlers.created ? emitter.on('node:created', handlers.created) : null,
|
|
11
|
+
handlers.updated ? emitter.on('node:updated', handlers.updated) : null,
|
|
12
|
+
handlers.previewUpdated ? emitter.on('node:previewUpdated', handlers.previewUpdated) : null,
|
|
13
|
+
handlers.deleted ? emitter.on('node:deleted', handlers.deleted) : null,
|
|
14
|
+
handlers.reparented ? emitter.on('node:reparented', handlers.reparented) : null,
|
|
15
|
+
handlers.reordered ? emitter.on('node:reordered', handlers.reordered) : null
|
|
16
|
+
].filter((unbind): unbind is () => void => !!unbind)
|
|
17
|
+
|
|
18
|
+
return () => {
|
|
19
|
+
for (const unbind of unbinds) unbind()
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bounds for export scale multipliers. A huge multiplier would allocate an
|
|
3
|
+
* enormous canvas and crash the renderer, so clamp at every boundary the value
|
|
4
|
+
* can enter from: the UI (edits) and the file format (imported/plugin .fig data).
|
|
5
|
+
*/
|
|
6
|
+
export const MIN_EXPORT_SCALE = 0.01
|
|
7
|
+
export const MAX_EXPORT_SCALE = 1024
|
|
8
|
+
|
|
9
|
+
export function clampExportScale(scale: number): number {
|
|
10
|
+
return Math.min(MAX_EXPORT_SCALE, Math.max(MIN_EXPORT_SCALE, scale))
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Accept a scale only if it is finite and within bounds (no silent clamping). */
|
|
14
|
+
export function isValidExportScale(scale: number): boolean {
|
|
15
|
+
return Number.isFinite(scale) && scale >= MIN_EXPORT_SCALE && scale <= MAX_EXPORT_SCALE
|
|
16
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export interface FontFamilyStyle {
|
|
2
|
+
family: string
|
|
3
|
+
style: string
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface ParsedFontStyle {
|
|
7
|
+
weight: number
|
|
8
|
+
italic: boolean
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const FONT_WEIGHT_NAMES: Record<number, string> = {
|
|
12
|
+
100: 'Thin',
|
|
13
|
+
200: 'Extra Light',
|
|
14
|
+
300: 'Light',
|
|
15
|
+
400: 'Regular',
|
|
16
|
+
500: 'Medium',
|
|
17
|
+
600: 'Semi Bold',
|
|
18
|
+
700: 'Bold',
|
|
19
|
+
800: 'Extra Bold',
|
|
20
|
+
900: 'Black'
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const FONT_WEIGHT_ALIASES = [
|
|
24
|
+
{ weight: 100, names: ['thin', 'hairline', 'extrathin', 'ultrathin'] },
|
|
25
|
+
{ weight: 200, names: ['extralight', 'ultralight'] },
|
|
26
|
+
{ weight: 300, names: ['light'] },
|
|
27
|
+
{ weight: 400, names: ['regular', 'normal', 'book', 'roman', 'plain'] },
|
|
28
|
+
{ weight: 500, names: ['medium'] },
|
|
29
|
+
{ weight: 600, names: ['semibold', 'demibold'] },
|
|
30
|
+
{ weight: 700, names: ['bold'] },
|
|
31
|
+
{ weight: 800, names: ['extrabold', 'ultrabold'] },
|
|
32
|
+
{ weight: 900, names: ['black', 'heavy'] }
|
|
33
|
+
] as const
|
|
34
|
+
|
|
35
|
+
const FONT_WEIGHT_BY_STYLE: ReadonlyMap<string, number> = new Map(
|
|
36
|
+
FONT_WEIGHT_ALIASES.flatMap(({ names, weight }) => names.map((name) => [name, weight] as const))
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
export function normalizeFontStyleName(style: string): string {
|
|
40
|
+
return style
|
|
41
|
+
.toLowerCase()
|
|
42
|
+
.replace(/italic|oblique/u, '')
|
|
43
|
+
.replace(/[^a-z0-9]+/gu, '')
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function parseFontStyle(style: string | undefined): ParsedFontStyle {
|
|
47
|
+
const raw = style ?? ''
|
|
48
|
+
const italic = /(?:italic|oblique)/iu.test(raw)
|
|
49
|
+
const normalized = normalizeFontStyleName(raw)
|
|
50
|
+
const numericWeight = normalized.match(/(?:^|[^0-9])([1-9]00)(?:[^0-9]|$)/u)?.[1]
|
|
51
|
+
return {
|
|
52
|
+
weight: numericWeight ? Number(numericWeight) : (FONT_WEIGHT_BY_STYLE.get(normalized) ?? 400),
|
|
53
|
+
italic
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function styleToWeight(style: string | undefined): number {
|
|
58
|
+
return parseFontStyle(style).weight
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function normalizeFontFamily(family: string): string {
|
|
62
|
+
return family.replace(/\s+(Variable|\d+(?:pt|px|em))$/i, '')
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function styleToVariant(style: string): string {
|
|
66
|
+
const weight = styleToWeight(style)
|
|
67
|
+
const italic = style.toLowerCase().includes('italic')
|
|
68
|
+
if (weight === 400 && !italic) return 'regular'
|
|
69
|
+
if (weight === 400 && italic) return 'italic'
|
|
70
|
+
return italic ? `${weight}italic` : `${weight}`
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function weightToStyle(weight: number, italic = false): string {
|
|
74
|
+
const rounded = Math.round(weight / 100) * 100
|
|
75
|
+
const label = (FONT_WEIGHT_NAMES[rounded] ?? 'Regular').replace(/ /g, '')
|
|
76
|
+
return italic ? `${label} Italic` : label
|
|
77
|
+
}
|