@open-pencil/scene-graph 0.17.0-beta.3 → 0.17.0-beta.5

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.
Files changed (283) hide show
  1. package/dist/arrays.d.ts +17 -0
  2. package/dist/arrays.d.ts.map +1 -0
  3. package/dist/arrays.js +38 -0
  4. package/dist/arrays.js.map +1 -0
  5. package/dist/buffered-events.js +1 -1
  6. package/dist/buffered-events.js.map +1 -1
  7. package/dist/clipping.d.ts +7 -1
  8. package/dist/clipping.d.ts.map +1 -1
  9. package/dist/clipping.js +23 -19
  10. package/dist/clipping.js.map +1 -1
  11. package/dist/components/index.d.ts +2 -2
  12. package/dist/components/index.js +2 -2
  13. package/dist/components/properties.d.ts +1 -3
  14. package/dist/components/properties.d.ts.map +1 -1
  15. package/dist/components/properties.js +2 -9
  16. package/dist/components/properties.js.map +1 -1
  17. package/dist/corners.d.ts +22 -0
  18. package/dist/corners.d.ts.map +1 -0
  19. package/dist/corners.js +52 -0
  20. package/dist/corners.js.map +1 -0
  21. package/dist/css/grid.d.ts +4 -1
  22. package/dist/css/grid.d.ts.map +1 -1
  23. package/dist/css/grid.js +16 -7
  24. package/dist/css/grid.js.map +1 -1
  25. package/dist/css/index.d.ts +2 -2
  26. package/dist/css/index.js +2 -2
  27. package/dist/css/values.d.ts +6 -1
  28. package/dist/css/values.d.ts.map +1 -1
  29. package/dist/font-style.d.ts +2 -1
  30. package/dist/font-style.d.ts.map +1 -1
  31. package/dist/font-style.js +31 -6
  32. package/dist/font-style.js.map +1 -1
  33. package/dist/geometry.d.ts +68 -3
  34. package/dist/geometry.d.ts.map +1 -1
  35. package/dist/geometry.js +155 -5
  36. package/dist/geometry.js.map +1 -1
  37. package/dist/gradient.d.ts +9 -2
  38. package/dist/gradient.d.ts.map +1 -1
  39. package/dist/gradient.js +29 -1
  40. package/dist/gradient.js.map +1 -1
  41. package/dist/graph.d.ts +4 -0
  42. package/dist/graph.d.ts.map +1 -1
  43. package/dist/graph.js +30 -4
  44. package/dist/graph.js.map +1 -1
  45. package/dist/group-bounds.d.ts.map +1 -1
  46. package/dist/group-bounds.js +4 -3
  47. package/dist/group-bounds.js.map +1 -1
  48. package/dist/guides.d.ts +11 -2
  49. package/dist/guides.d.ts.map +1 -1
  50. package/dist/guides.js +30 -1
  51. package/dist/guides.js.map +1 -1
  52. package/dist/hit-test.d.ts.map +1 -1
  53. package/dist/hit-test.js +57 -5
  54. package/dist/hit-test.js.map +1 -1
  55. package/dist/icons/fit.d.ts +17 -0
  56. package/dist/icons/fit.d.ts.map +1 -0
  57. package/dist/icons/fit.js +21 -0
  58. package/dist/icons/fit.js.map +1 -0
  59. package/dist/icons/glyph.d.ts.map +1 -1
  60. package/dist/icons/glyph.js +5 -4
  61. package/dist/icons/glyph.js.map +1 -1
  62. package/dist/icons/index.d.ts +2 -1
  63. package/dist/icons/index.js +2 -1
  64. package/dist/images.d.ts +14 -4
  65. package/dist/images.d.ts.map +1 -1
  66. package/dist/images.js +45 -5
  67. package/dist/images.js.map +1 -1
  68. package/dist/instances/assignments.js +39 -6
  69. package/dist/instances/assignments.js.map +1 -1
  70. package/dist/instances/index.d.ts.map +1 -1
  71. package/dist/instances/index.js +39 -2
  72. package/dist/instances/index.js.map +1 -1
  73. package/dist/instances/kept-overrides.js +2 -2
  74. package/dist/instances/kept-overrides.js.map +1 -1
  75. package/dist/instances/matching.js +68 -0
  76. package/dist/instances/matching.js.map +1 -0
  77. package/dist/instances/overrides.d.ts +4 -1
  78. package/dist/instances/overrides.d.ts.map +1 -1
  79. package/dist/instances/overrides.js +13 -1
  80. package/dist/instances/overrides.js.map +1 -1
  81. package/dist/instances/protection.js +67 -0
  82. package/dist/instances/protection.js.map +1 -0
  83. package/dist/instances/swap.js +7 -5
  84. package/dist/instances/swap.js.map +1 -1
  85. package/dist/instances/sync.js +10 -116
  86. package/dist/instances/sync.js.map +1 -1
  87. package/dist/layout/guides.d.ts +17 -1
  88. package/dist/layout/guides.d.ts.map +1 -1
  89. package/dist/layout/guides.js +31 -11
  90. package/dist/layout/guides.js.map +1 -1
  91. package/dist/layout/index.d.ts +4 -2
  92. package/dist/layout/index.js +4 -2
  93. package/dist/layout/limits.d.ts +40 -0
  94. package/dist/layout/limits.d.ts.map +1 -0
  95. package/dist/layout/limits.js +54 -0
  96. package/dist/layout/limits.js.map +1 -0
  97. package/dist/layout/sizing.d.ts.map +1 -1
  98. package/dist/layout/sizing.js +0 -1
  99. package/dist/layout/sizing.js.map +1 -1
  100. package/dist/layout/spacing.d.ts +10 -0
  101. package/dist/layout/spacing.d.ts.map +1 -0
  102. package/dist/layout/spacing.js +12 -0
  103. package/dist/layout/spacing.js.map +1 -0
  104. package/dist/masks.d.ts +28 -0
  105. package/dist/masks.d.ts.map +1 -0
  106. package/dist/masks.js +53 -0
  107. package/dist/masks.js.map +1 -0
  108. package/dist/matrix.d.ts +2 -1
  109. package/dist/matrix.d.ts.map +1 -1
  110. package/dist/matrix.js +14 -1
  111. package/dist/matrix.js.map +1 -1
  112. package/dist/node-defaults.d.ts +9 -1
  113. package/dist/node-defaults.d.ts.map +1 -1
  114. package/dist/node-defaults.js +16 -1
  115. package/dist/node-defaults.js.map +1 -1
  116. package/dist/outline/flatten.d.ts.map +1 -1
  117. package/dist/outline/flatten.js +24 -17
  118. package/dist/outline/flatten.js.map +1 -1
  119. package/dist/outline/hit.d.ts +2 -8
  120. package/dist/outline/hit.d.ts.map +1 -1
  121. package/dist/outline/hit.js +16 -25
  122. package/dist/outline/hit.js.map +1 -1
  123. package/dist/outline/index.d.ts +3 -3
  124. package/dist/outline/index.js +3 -3
  125. package/dist/outline/shapes.d.ts +3 -1
  126. package/dist/outline/shapes.d.ts.map +1 -1
  127. package/dist/outline/shapes.js +18 -39
  128. package/dist/outline/shapes.js.map +1 -1
  129. package/dist/paints/index.d.ts +3 -1
  130. package/dist/paints/index.js +3 -1
  131. package/dist/paints/pattern.d.ts +13 -0
  132. package/dist/paints/pattern.d.ts.map +1 -0
  133. package/dist/paints/pattern.js +47 -0
  134. package/dist/paints/pattern.js.map +1 -0
  135. package/dist/paints/shows.d.ts +11 -0
  136. package/dist/paints/shows.d.ts.map +1 -0
  137. package/dist/paints/shows.js +16 -0
  138. package/dist/paints/shows.js.map +1 -0
  139. package/dist/paths/arc.d.ts +32 -0
  140. package/dist/paths/arc.d.ts.map +1 -0
  141. package/dist/paths/arc.js +49 -0
  142. package/dist/paths/arc.js.map +1 -0
  143. package/dist/paths/index.d.ts +5 -0
  144. package/dist/paths/index.js +5 -0
  145. package/dist/paths/network.d.ts +43 -0
  146. package/dist/paths/network.d.ts.map +1 -0
  147. package/dist/paths/network.js +249 -0
  148. package/dist/paths/network.js.map +1 -0
  149. package/dist/paths/sink.d.ts +20 -0
  150. package/dist/paths/sink.d.ts.map +1 -0
  151. package/dist/paths/sink.js +32 -0
  152. package/dist/paths/sink.js.map +1 -0
  153. package/dist/paths/smooth-corners.d.ts +25 -0
  154. package/dist/paths/smooth-corners.d.ts.map +1 -0
  155. package/dist/paths/smooth-corners.js +196 -0
  156. package/dist/paths/smooth-corners.js.map +1 -0
  157. package/dist/plugin-data/fields.d.ts +4 -0
  158. package/dist/plugin-data/fields.d.ts.map +1 -1
  159. package/dist/plugin-data/fields.js +2 -0
  160. package/dist/plugin-data/fields.js.map +1 -1
  161. package/dist/polygon.d.ts +1 -3
  162. package/dist/polygon.d.ts.map +1 -1
  163. package/dist/polygon.js +1 -16
  164. package/dist/polygon.js.map +1 -1
  165. package/dist/resize.d.ts.map +1 -1
  166. package/dist/resize.js +5 -5
  167. package/dist/resize.js.map +1 -1
  168. package/dist/scaling/node.d.ts.map +1 -1
  169. package/dist/scaling/node.js +25 -3
  170. package/dist/scaling/node.js.map +1 -1
  171. package/dist/text/auto-resize.d.ts +15 -0
  172. package/dist/text/auto-resize.d.ts.map +1 -0
  173. package/dist/text/auto-resize.js +23 -0
  174. package/dist/text/auto-resize.js.map +1 -0
  175. package/dist/text/index.d.ts +4 -3
  176. package/dist/text/index.js +4 -3
  177. package/dist/text/lengths.d.ts +6 -1
  178. package/dist/text/lengths.d.ts.map +1 -1
  179. package/dist/text/lengths.js +10 -1
  180. package/dist/text/lengths.js.map +1 -1
  181. package/dist/text/overflow.d.ts +14 -5
  182. package/dist/text/overflow.d.ts.map +1 -1
  183. package/dist/text/overflow.js +24 -6
  184. package/dist/text/overflow.js.map +1 -1
  185. package/dist/text/path.js +10 -0
  186. package/dist/text/path.js.map +1 -0
  187. package/dist/text/picture.js +5 -5
  188. package/dist/text/picture.js.map +1 -1
  189. package/dist/transfer/dependencies.d.ts.map +1 -1
  190. package/dist/transfer/dependencies.js +3 -2
  191. package/dist/transfer/dependencies.js.map +1 -1
  192. package/dist/transfer/nodes.js +2 -1
  193. package/dist/transfer/nodes.js.map +1 -1
  194. package/dist/types.d.ts +21 -8
  195. package/dist/types.d.ts.map +1 -1
  196. package/dist/types.js.map +1 -1
  197. package/dist/variables/index.d.ts +4 -3
  198. package/dist/variables/index.d.ts.map +1 -1
  199. package/dist/variables/index.js +4 -3
  200. package/dist/variables/index.js.map +1 -1
  201. package/dist/variables/lookup.d.ts +20 -0
  202. package/dist/variables/lookup.d.ts.map +1 -0
  203. package/dist/variables/lookup.js +34 -0
  204. package/dist/variables/lookup.js.map +1 -0
  205. package/dist/variables/paints.d.ts +35 -4
  206. package/dist/variables/paints.d.ts.map +1 -1
  207. package/dist/variables/paints.js +67 -16
  208. package/dist/variables/paints.js.map +1 -1
  209. package/dist/variables/resolution.d.ts +3 -1
  210. package/dist/variables/resolution.d.ts.map +1 -1
  211. package/dist/variables/resolution.js +8 -3
  212. package/dist/variables/resolution.js.map +1 -1
  213. package/dist/variables/subtree.js +1 -1
  214. package/dist/variables/subtree.js.map +1 -1
  215. package/dist/vector-network.d.ts.map +1 -1
  216. package/dist/vector-network.js +12 -76
  217. package/dist/vector-network.js.map +1 -1
  218. package/package.json +25 -1
  219. package/src/arrays.ts +37 -0
  220. package/src/buffered-events.ts +2 -1
  221. package/src/clipping.ts +11 -8
  222. package/src/components/properties.ts +1 -13
  223. package/src/corners.ts +93 -0
  224. package/src/css/grid.ts +21 -7
  225. package/src/css/index.ts +1 -1
  226. package/src/font-style.ts +38 -4
  227. package/src/geometry.ts +219 -6
  228. package/src/gradient.ts +23 -1
  229. package/src/graph.ts +22 -4
  230. package/src/group-bounds.ts +7 -3
  231. package/src/guides.ts +26 -1
  232. package/src/hit-test.ts +68 -10
  233. package/src/icons/fit.ts +24 -0
  234. package/src/icons/glyph.ts +5 -4
  235. package/src/icons/index.ts +1 -0
  236. package/src/images.ts +67 -7
  237. package/src/instances/assignments.ts +65 -7
  238. package/src/instances/index.ts +53 -4
  239. package/src/instances/kept-overrides.ts +2 -5
  240. package/src/instances/matching.ts +101 -0
  241. package/src/instances/overrides.ts +18 -0
  242. package/src/instances/protection.ts +103 -0
  243. package/src/instances/swap.ts +11 -9
  244. package/src/instances/sync.ts +11 -184
  245. package/src/layout/guides.ts +42 -9
  246. package/src/layout/index.ts +2 -0
  247. package/src/layout/limits.ts +64 -0
  248. package/src/layout/sizing.ts +0 -2
  249. package/src/layout/spacing.ts +11 -0
  250. package/src/masks.ts +51 -0
  251. package/src/matrix.ts +15 -2
  252. package/src/node-defaults.ts +23 -3
  253. package/src/outline/flatten.ts +30 -18
  254. package/src/outline/hit.ts +16 -33
  255. package/src/outline/index.ts +2 -2
  256. package/src/outline/shapes.ts +19 -36
  257. package/src/paints/index.ts +2 -0
  258. package/src/paints/pattern.ts +45 -0
  259. package/src/paints/shows.ts +13 -0
  260. package/src/paths/arc.ts +59 -0
  261. package/src/paths/index.ts +4 -0
  262. package/src/paths/network.ts +334 -0
  263. package/src/paths/sink.ts +50 -0
  264. package/src/paths/smooth-corners.ts +356 -0
  265. package/src/plugin-data/fields.ts +2 -0
  266. package/src/polygon.ts +0 -17
  267. package/src/resize.ts +6 -7
  268. package/src/scaling/node.ts +29 -3
  269. package/src/text/auto-resize.ts +25 -0
  270. package/src/text/index.ts +1 -0
  271. package/src/text/lengths.ts +11 -0
  272. package/src/text/overflow.ts +32 -6
  273. package/src/text/path.ts +13 -0
  274. package/src/text/picture.ts +5 -5
  275. package/src/transfer/dependencies.ts +3 -2
  276. package/src/transfer/nodes.ts +2 -1
  277. package/src/types.ts +25 -8
  278. package/src/variables/index.ts +1 -0
  279. package/src/variables/lookup.ts +31 -0
  280. package/src/variables/paints.ts +101 -20
  281. package/src/variables/resolution.ts +14 -2
  282. package/src/variables/subtree.ts +1 -1
  283. package/src/vector-network.ts +17 -138
@@ -1,7 +1,7 @@
1
1
  // Child cloning and property synchronization shared by instance creation, swap, and sync.
2
2
  import { uniq } from 'es-toolkit/array'
3
3
  import { pick } from 'es-toolkit/object'
4
- import { isEmptyObject, isEqual } from 'es-toolkit/predicate'
4
+ import { isEqual } from 'es-toolkit/predicate'
5
5
 
6
6
  import { cloneNodeProps, copyEffects, copyFills, copyStrokes, copyStyleRuns } from '../copy'
7
7
  import type { NodeCloneMode } from '../copy'
@@ -10,11 +10,7 @@ import { scaleNodeChanges } from '../scaling/node'
10
10
  import { ownsSlotContent, slotPropertyId } from '../slots/frames'
11
11
  import type { SceneNode } from '../types'
12
12
  import { resolvedNumericBindings } from '../variables/bindings'
13
- import {
14
- effectBindingTarget,
15
- isNumericVariableBindingField,
16
- paintBindingTarget
17
- } from '../variables/fields'
13
+ import { isNumericVariableBindingField } from '../variables/fields'
18
14
  import {
19
15
  applyBoundPaintColorsIn,
20
16
  copyAppliedBoundPaints,
@@ -31,16 +27,20 @@ import {
31
27
  type InstanceSyncContext
32
28
  } from './copies'
33
29
  import { INSTANCE_SYNC_FIELD_OWNERS, INSTANCE_SYNC_FIELDS } from './fields'
34
- import { ownsSelfFillField, sourceWithEffectiveFill } from './main-component'
30
+ import { sourceWithEffectiveFill } from './main-component'
31
+ import { matchFallbackChildren, matchLinkedChildren } from './matching'
35
32
  import {
36
- bindingOverrideField,
37
33
  copiedComponentLayerId,
38
34
  hasInstanceOverride as hasNodeInstanceOverride,
39
- overriddenBindingField,
40
- setInstanceOverride,
41
35
  type InstanceOverrideState
42
36
  } from './overrides'
43
37
  import { fitCopiedGroup, PLACEMENT_FIELDS, placedByConstraints } from './placement'
38
+ import {
39
+ childBindingProtection,
40
+ isProtectedSyncField,
41
+ NOTHING_PROTECTED,
42
+ syncBindingFields
43
+ } from './protection'
44
44
  import { followVectorGeometry } from './vector-geometry'
45
45
 
46
46
  function setSceneProp<K extends keyof SceneNode>(
@@ -83,67 +83,6 @@ export function copyProp(
83
83
  }
84
84
  }
85
85
 
86
- export function bindingProtection(scopes: ReadonlySet<string>[]): (field: string) => boolean {
87
- const fields = new Set<string>()
88
- for (const scope of scopes) {
89
- const perField = [...scope].some((field) => overriddenBindingField(field) !== null)
90
- for (const field of scope) {
91
- if (field !== 'boundVariables' || !perField) fields.add(field)
92
- }
93
- }
94
- return (field) => {
95
- if (fields.has(field)) return true
96
- // Paints and effects an instance set itself own their bindings too, bound or not.
97
- const binding = overriddenBindingField(field)
98
- if (binding === null) return false
99
- const paint = paintBindingTarget(binding)
100
- if (paint) return fields.has(paint.list)
101
- return fields.has('effects') && effectBindingTarget(binding) !== null
102
- }
103
- }
104
-
105
- function isUnbound(node: SceneNode): boolean {
106
- return isEmptyObject(node.boundVariables) && isEmptyObject(node.variableBindingScales)
107
- }
108
-
109
- export function syncBindingFields(
110
- target: SceneNode,
111
- source: SceneNode,
112
- updates: Partial<SceneNode>,
113
- protectedField: (field: string) => boolean
114
- ): void {
115
- if (protectedField('boundVariables') || (isUnbound(source) && isUnbound(target))) {
116
- return
117
- }
118
- const bindings = { ...source.boundVariables }
119
- const scales = { ...source.variableBindingScales }
120
- for (const field of new Set([
121
- ...Object.keys(source.boundVariables),
122
- ...Object.keys(target.boundVariables),
123
- ...Object.keys(target.variableBindingScales)
124
- ])) {
125
- if (!protectedField(bindingOverrideField(field))) continue
126
- if (!Object.hasOwn(target.boundVariables, field)) Reflect.deleteProperty(bindings, field)
127
- else bindings[field] = target.boundVariables[field]
128
- if (target.variableBindingScales[field] === undefined) Reflect.deleteProperty(scales, field)
129
- else scales[field] = target.variableBindingScales[field]
130
- }
131
- updates.boundVariables = bindings
132
- updates.variableBindingScales = scales
133
- }
134
-
135
- export function isProtectedSyncField(
136
- node: SceneNode,
137
- key: string,
138
- protectedField: (field: string) => boolean
139
- ): boolean {
140
- return (
141
- protectedField(key) ||
142
- (key in node.boundVariables &&
143
- (protectedField('boundVariables') || protectedField(bindingOverrideField(key))))
144
- )
145
- }
146
-
147
86
  export function sourceInTargetCoordinates(source: SceneNode, targetScale: number): SceneNode {
148
87
  const factor = targetScale / source.componentScale
149
88
  if (!Number.isFinite(factor) || factor <= 0) throw new Error('Invalid component coordinate scale')
@@ -162,8 +101,6 @@ const SYNCED_FIELDS = INSTANCE_SYNC_FIELDS.filter((key) => key !== 'boundVariabl
162
101
  position: key === 'x' || key === 'y'
163
102
  }))
164
103
 
165
- export const NOTHING_PROTECTED = (_field: string): boolean => false
166
-
167
104
  interface SyncedFieldsInput {
168
105
  compParent: SceneNode
169
106
  instParent: SceneNode
@@ -310,95 +247,6 @@ export function cloneChildrenWithMapping(
310
247
  }
311
248
  }
312
249
 
313
- export function enclosingInstanceOverrideFields(graph: SceneGraph, node: SceneNode): Set<string>[] {
314
- const fields: Set<string>[] = []
315
- let parent = node.parentId ? graph.nodes.get(node.parentId) : undefined
316
- while (parent) {
317
- if (parent.type === 'INSTANCE') {
318
- fields.push(new Set(parent.instanceOverrides.descendants.get(node.id)?.keys()))
319
- }
320
- parent = parent.parentId ? graph.nodes.get(parent.parentId) : undefined
321
- }
322
- return fields
323
- }
324
-
325
- function childBindingProtection(
326
- graph: SceneGraph,
327
- child: SceneNode,
328
- overrides: InstanceOverrideState
329
- ) {
330
- const fields = enclosingInstanceOverrideFields(graph, child)
331
- fields.push(new Set(overrides.descendants.get(child.id)?.keys()))
332
- if (child.instanceOverrides.self.size === 0 && fields.every((scope) => scope.size === 0)) {
333
- return NOTHING_PROTECTED
334
- }
335
- const protectedField = bindingProtection(fields)
336
- return (field: string) => protectedField(field) || ownsSelfFillField(child, field)
337
- }
338
-
339
- function linkMatchedChild(
340
- overrides: InstanceOverrideState,
341
- instParentId: string,
342
- instChild: SceneNode,
343
- compChildId: string
344
- ): void {
345
- if (instChild.type === 'INSTANCE') {
346
- setInstanceOverride(overrides, instParentId, instChild.id, 'sourceComponentId', compChildId)
347
- } else {
348
- instChild.componentId = compChildId
349
- }
350
- }
351
-
352
- function matchFallbackChildren(
353
- graph: SceneGraph,
354
- compParent: SceneNode,
355
- instParent: SceneNode,
356
- instParentId: string,
357
- overrides: InstanceOverrideState,
358
- instChildMap: Map<string, SceneNode>,
359
- usedInstChildIds: Set<string>
360
- ): void {
361
- const fallbackByType = new Map<SceneNode['type'], Map<string, SceneNode[]>>()
362
- const remainingCandidateCount = new Map<SceneNode['type'], number>()
363
- const unmatchedComponentCount = new Map<SceneNode['type'], number>()
364
- for (const compChildId of compParent.childIds) {
365
- if (instChildMap.has(compChildId)) continue
366
- const child = graph.nodes.get(compChildId)
367
- if (child) {
368
- unmatchedComponentCount.set(child.type, (unmatchedComponentCount.get(child.type) ?? 0) + 1)
369
- }
370
- }
371
- for (const childId of instParent.childIds) {
372
- const child = graph.nodes.get(childId)
373
- if (!child || usedInstChildIds.has(child.id)) continue
374
- remainingCandidateCount.set(child.type, (remainingCandidateCount.get(child.type) ?? 0) + 1)
375
- let byName = fallbackByType.get(child.type)
376
- if (!byName) {
377
- byName = new Map()
378
- fallbackByType.set(child.type, byName)
379
- }
380
- const queue = byName.get(child.name)
381
- if (queue) queue.push(child)
382
- else byName.set(child.name, [child])
383
- }
384
-
385
- // Match by name and type with FIFO queues to preserve sibling order in linear time.
386
- for (const compChildId of compParent.childIds) {
387
- if (instChildMap.has(compChildId)) continue
388
- const compChild = graph.nodes.get(compChildId)
389
- if (!compChild) continue
390
- const candidatesByName = fallbackByType.get(compChild.type)
391
- const candidateCount = remainingCandidateCount.get(compChild.type) ?? 0
392
- if (candidateCount > (unmatchedComponentCount.get(compChild.type) ?? 0)) continue
393
- const match = candidatesByName?.get(compChild.name)?.shift()
394
- if (!match) continue
395
- remainingCandidateCount.set(compChild.type, candidateCount - 1)
396
- instChildMap.set(compChildId, match)
397
- usedInstChildIds.add(match.id)
398
- linkMatchedChild(overrides, instParentId, match, compChildId)
399
- }
400
- }
401
-
402
250
  function sortInstanceChildren(
403
251
  graph: SceneGraph,
404
252
  instParent: SceneNode,
@@ -478,27 +326,6 @@ function isCyclicSync(graph: SceneGraph, compParentId: string, instParentId: str
478
326
  return compParentId === instParentId || graph.isDescendant(instParentId, compParentId)
479
327
  }
480
328
 
481
- /** Children already linked to a component child by sourceComponentId or componentId. */
482
- function matchLinkedChildren(
483
- graph: SceneGraph,
484
- instParent: SceneNode,
485
- instParentId: string,
486
- overrides: InstanceOverrideState,
487
- compChildIdSet: ReadonlySet<string>,
488
- instChildMap: Map<string, SceneNode>,
489
- usedInstChildIds: Set<string>
490
- ): void {
491
- for (const childId of instParent.childIds) {
492
- const child = graph.nodes.get(childId)
493
- if (!child) continue
494
- const mappedComponentId = copiedComponentLayerId(overrides, instParentId, child)
495
- if (mappedComponentId && compChildIdSet.has(mappedComponentId)) {
496
- instChildMap.set(mappedComponentId, child)
497
- usedInstChildIds.add(child.id)
498
- }
499
- }
500
- }
501
-
502
329
  /**
503
330
  * Pass 3 of a sync: bring along the copy of a layer moved here inside the component, bring back
504
331
  * one the sync removed when its layer returns, or clone a layer the instance has no copy of. A
@@ -579,7 +406,7 @@ export function syncChildren(
579
406
  compParent,
580
407
  instParent,
581
408
  instParentId,
582
- overrides,
409
+ context,
583
410
  instChildMap,
584
411
  usedInstChildIds
585
412
  )
@@ -1,3 +1,4 @@
1
+ import type { Color } from '../primitives'
1
2
  import type { LayoutGrid, SceneNode } from '../types'
2
3
 
3
4
  export interface LayoutGuideLine {
@@ -5,7 +6,8 @@ export interface LayoutGuideLine {
5
6
  position: number
6
7
  }
7
8
 
8
- const MAX_LAYOUT_GUIDE_LINES = 10_000
9
+ /** The most lines one guide draws or snaps to on an axis. */
10
+ export const MAX_LAYOUT_GUIDE_LINES = 10_000
9
11
 
10
12
  /** Figma's Auto count, as its archive stores it: as many sections as fit. */
11
13
  export const AUTO_LAYOUT_GRID_COUNT = 2_147_483_647
@@ -24,18 +26,49 @@ export function layoutGridCount(grid: LayoutGrid): number {
24
26
  return isAutoGridCount(count) ? AUTO_LAYOUT_GRID_COUNT : (count ?? 1)
25
27
  }
26
28
 
27
- function pattern(grid: LayoutGrid): 'COLUMNS' | 'ROWS' | 'GRID' {
28
- if (grid.pattern === 'GRID' || grid.pattern === 'ROWS') return grid.pattern
29
- if (grid.axis === 'Y') return 'ROWS'
30
- return 'COLUMNS'
29
+ /** Figma's color for a new guide: red at 10%. */
30
+ export const DEFAULT_LAYOUT_GRID_COLOR: Readonly<Color> = Object.freeze({
31
+ r: 1,
32
+ g: 0,
33
+ b: 0,
34
+ a: 0.1
35
+ })
36
+
37
+ /** A guide's pattern in Figma's plugin names, from either shape: the archive's `STRIPES` along an `axis`. */
38
+ export function layoutGridPattern(
39
+ grid: Pick<LayoutGrid, 'pattern' | 'axis'>
40
+ ): 'COLUMNS' | 'ROWS' | 'GRID' {
41
+ if (grid.pattern === 'GRID' || grid.pattern === 'ROWS' || grid.pattern === 'COLUMNS') {
42
+ return grid.pattern
43
+ }
44
+ return grid.axis === 'Y' ? 'ROWS' : 'COLUMNS'
31
45
  }
32
46
 
33
- function alignment(grid: LayoutGrid): 'MIN' | 'CENTER' | 'MAX' | 'STRETCH' {
34
- const value = grid.alignment ?? grid.type
35
- if (value === 'CENTER' || value === 'MAX' || value === 'STRETCH') return value
36
- return 'MIN'
47
+ /**
48
+ * A guide's alignment, from either shape. One set without any stretches, as the plugin API and
49
+ * export have read it; a file's guide always has one, as Figma drops a guide without it.
50
+ */
51
+ export function layoutGridAlignment(
52
+ grid: Pick<LayoutGrid, 'alignment' | 'type'>
53
+ ): 'MIN' | 'CENTER' | 'MAX' | 'STRETCH' {
54
+ return grid.alignment ?? grid.type ?? 'STRETCH'
37
55
  }
38
56
 
57
+ /** Whether a file's guide record names an alignment; Figma drops one that does not (live). */
58
+ export function hasLayoutGridAlignment(
59
+ grid: Partial<Pick<LayoutGrid, 'alignment' | 'type'>>
60
+ ): boolean {
61
+ return grid.type !== undefined || grid.alignment !== undefined
62
+ }
63
+
64
+ /** A guide's color, Figma's new-guide red for one set without any. */
65
+ export function layoutGridColor(grid: Pick<LayoutGrid, 'color'>): Color {
66
+ return { ...(grid.color ?? DEFAULT_LAYOUT_GRID_COLOR) }
67
+ }
68
+
69
+ const pattern = layoutGridPattern
70
+ const alignment = layoutGridAlignment
71
+
39
72
  /**
40
73
  * How many sections a guide draws. Auto fits as many fixed sections as the frame holds, counting
41
74
  * the offset once from the aligned edge, as Figma desktop 126 draws them.
@@ -1,4 +1,6 @@
1
1
  export * from './aspect-ratio'
2
2
  export * from './guides'
3
3
  export * from './keywords'
4
+ export * from './limits'
4
5
  export * from './sizing'
6
+ export * from './spacing'
@@ -0,0 +1,64 @@
1
+ import type { GridTrack } from '../types'
2
+
3
+ /**
4
+ * Bounds for the numbers layout computes with and for the values it writes back.
5
+ *
6
+ * Layout tolerates a span of zero or a track list a tool wrote by mistake, but not every
7
+ * consumer does: a document carrying absurd geometry cannot be rasterized, a grid template
8
+ * naming a billion tracks allocates one entry per track, and a text layer set at 1e9 px makes
9
+ * font shaping build a run of glyphs no memory holds. Every path that writes a layout value
10
+ * clamps to these bounds, and tool schemas reject values outside them.
11
+ */
12
+
13
+ /** The largest size or position, in pixels, that a layout value may carry. */
14
+ export const MAX_LAYOUT_LENGTH = 1_000_000
15
+
16
+ /** The largest gap, padding, or stroke a layout value may carry, in pixels. */
17
+ export const MAX_LAYOUT_SPACING = 100_000
18
+
19
+ /** The largest number of tracks one grid template may name. */
20
+ export const MAX_GRID_TRACKS = 1_000
21
+
22
+ /** The last grid line a placement may name; line 0 leaves placement to auto-flow. */
23
+ export const MAX_GRID_LINE = 1_000
24
+
25
+ /** The most tracks one grid item may span. */
26
+ export const MAX_GRID_SPAN = 1_000
27
+
28
+ /** The largest rotation, in degrees, a layout or transform may carry. */
29
+ export const MAX_LAYOUT_ANGLE = 360_000
30
+
31
+ /** The largest font size, in pixels, text may be set in. */
32
+ export const MAX_FONT_SIZE = 10_000
33
+
34
+ /** The largest ratio a layout factor, such as a flex grow share, may carry. */
35
+ export const MAX_LAYOUT_FACTOR = 1_000
36
+
37
+ /** The largest line height or letter spacing, in pixels or percent, a text may carry. */
38
+ export const MAX_TEXT_LENGTH = 10_000
39
+
40
+ /** A length that cannot be negative, such as a size, padding, gap, or radius. */
41
+ export function clampLayoutLength(value: number, max = MAX_LAYOUT_LENGTH): number {
42
+ if (!Number.isFinite(value)) return 0
43
+ return Math.min(max, Math.max(0, value))
44
+ }
45
+
46
+ /** A coordinate that may be negative, such as a position. */
47
+ export function clampLayoutPosition(value: number, max = MAX_LAYOUT_LENGTH): number {
48
+ if (!Number.isFinite(value)) return 0
49
+ return Math.min(max, Math.max(-max, value))
50
+ }
51
+
52
+ /** A font size in pixels, at least one and at most `MAX_FONT_SIZE`; fractions are kept. */
53
+ export function clampFontSize(value: number): number {
54
+ if (!Number.isFinite(value)) return 1
55
+ return Math.min(MAX_FONT_SIZE, Math.max(1, value))
56
+ }
57
+
58
+ /** A grid template's tracks, with at most `MAX_GRID_TRACKS` of them and no negative sizes. */
59
+ export function clampGridTracks(tracks: readonly GridTrack[]): GridTrack[] {
60
+ return tracks.slice(0, MAX_GRID_TRACKS).map((track) => ({
61
+ ...track,
62
+ value: clampLayoutLength(track.value)
63
+ }))
64
+ }
@@ -129,8 +129,6 @@ function textSizingUpdates(
129
129
  axis === 'HORIZONTAL'
130
130
  ? textAutoResizeFor(hugsAxis, hugsOther)
131
131
  : textAutoResizeFor(hugsOther, hugsAxis)
132
- // Truncated text keeps truncating when it stops hugging.
133
- if (autoResize === 'NONE' && node.textAutoResize === 'TRUNCATE') return {}
134
132
  return autoResize === node.textAutoResize ? {} : { textAutoResize: autoResize }
135
133
  }
136
134
 
@@ -0,0 +1,11 @@
1
+ import type { SceneNode } from '../types'
2
+
3
+ /**
4
+ * The space between wrapped lines of an auto layout. A frame without its own follows the item
5
+ * spacing, as Figma's `counterAxisSpacing: null` does.
6
+ */
7
+ export function counterAxisSpacingOf(
8
+ node: Pick<SceneNode, 'counterAxisSpacing' | 'itemSpacing'>
9
+ ): number {
10
+ return node.counterAxisSpacing ?? node.itemSpacing
11
+ }
package/src/masks.ts ADDED
@@ -0,0 +1,51 @@
1
+ import type { SceneNode } from './types'
2
+
3
+ /**
4
+ * The layer as it masks: an outline mask masks with its shape alone, so Figma ignores how opaque
5
+ * its fills are, and the canvas and hit testing take them as opaque.
6
+ */
7
+ export function asMaskShape(node: SceneNode): SceneNode {
8
+ if (node.maskType !== 'VECTOR') return node
9
+ return {
10
+ ...node,
11
+ fills: node.fills.map((fill) => ({ ...fill, opacity: 1, color: { ...fill.color, a: 1 } }))
12
+ }
13
+ }
14
+
15
+ /** Whether a layer masks its siblings: a mask that is shown, since a hidden mask masks nothing. */
16
+ export function isVisibleMask(node: Pick<SceneNode, 'visible' | 'isMask'>): boolean {
17
+ return node.visible && node.isMask
18
+ }
19
+
20
+ /**
21
+ * A run of masks and the siblings they mask, as indices into the children: the masks are
22
+ * `[start, maskedStart)` and what they mask `[maskedStart, end)`, which may be empty.
23
+ */
24
+ export interface MaskRun {
25
+ readonly start: number
26
+ readonly maskedStart: number
27
+ readonly end: number
28
+ }
29
+
30
+ /**
31
+ * Where masks act among a container's children, bottom to top. A visible mask, with the visible
32
+ * masks right after it, masks every sibling after them up to the next visible mask, which starts
33
+ * a run of its own; a sibling that is hidden or a hidden mask is masked like any other (Figma
34
+ * desktop, live). Children before the first mask are not in any run.
35
+ */
36
+ export function maskRuns<T>(children: readonly T[], isMask: (child: T) => boolean): MaskRun[] {
37
+ const runs: MaskRun[] = []
38
+ let index = 0
39
+ while (index < children.length) {
40
+ if (!isMask(children[index])) {
41
+ index++
42
+ continue
43
+ }
44
+ const start = index
45
+ while (index < children.length && isMask(children[index])) index++
46
+ const maskedStart = index
47
+ while (index < children.length && !isMask(children[index])) index++
48
+ runs.push({ start, maskedStart, end: index })
49
+ }
50
+ return runs
51
+ }
package/src/matrix.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Vector } from './primitives'
1
+ import type { Matrix as AffineTransform, Vector } from './primitives'
2
2
 
3
3
  export type Mat3 = number[]
4
4
 
@@ -112,5 +112,18 @@ const mapPoint = (m: Mat3, p: Vector): Vector => {
112
112
  return { x: arr[0], y: arr[1] }
113
113
  }
114
114
 
115
- const Matrix = { identity, multiply, translated, rotated, scaled, invert, mapPoints, mapPoint }
115
+ /** A 2 × 3 affine transform, as Figma stores gradient and image transforms, as a 3 × 3 matrix. */
116
+ const fromAffine = (t: AffineTransform): Mat3 => [t.m00, t.m01, t.m02, t.m10, t.m11, t.m12, 0, 0, 1]
117
+
118
+ const Matrix = {
119
+ identity,
120
+ multiply,
121
+ translated,
122
+ rotated,
123
+ scaled,
124
+ invert,
125
+ mapPoints,
126
+ mapPoint,
127
+ fromAffine
128
+ }
116
129
  export default Matrix
@@ -57,7 +57,10 @@ export function createDefaultNode(
57
57
  type: NodeType,
58
58
  overrides: Partial<SceneNode> = {}
59
59
  ): SceneNode {
60
- return {
60
+ // A literal ending in a spread is built one field at a time, and V8 keeps an object given this
61
+ // many fields that way in dictionary mode, about 3 KB more per layer; a spread copy of a
62
+ // finished literal keeps fast fields, in JavaScriptCore as well.
63
+ const defaults = {
61
64
  id: generateId(),
62
65
  type,
63
66
  name: type.charAt(0) + type.slice(1).toLowerCase(),
@@ -229,9 +232,9 @@ export function createDefaultNode(
229
232
  derivedTextHeight: null,
230
233
  textPathData: null,
231
234
  textPathBox: null,
232
- booleanOperation: undefined,
233
- ...overrides
235
+ booleanOperation: undefined
234
236
  } satisfies CompleteNodeFields
237
+ return { ...defaults, ...overrides }
235
238
  }
236
239
 
237
240
  /** Containers whose bounds follow their children and that set no coordinate space, as in Figma. */
@@ -263,6 +266,23 @@ export function isLayerContainer(node: Pick<SceneNode, 'type'>): boolean {
263
266
  return LAYER_CONTAINER_TYPES.has(node.type)
264
267
  }
265
268
 
269
+ /** The kinds that clip their children when their clip content is on. */
270
+ export const CLIPPING_TYPES: ReadonlySet<NodeType> = new Set<NodeType>([
271
+ 'FRAME',
272
+ 'COMPONENT',
273
+ 'COMPONENT_SET',
274
+ 'INSTANCE'
275
+ ])
276
+
277
+ /**
278
+ * Whether a layer clips its children: a frame, component, component set or instance with clip
279
+ * content on. A section or group has no such setting in Figma and never clips, whatever an import
280
+ * left in `clipsContent` (Figma desktop, live).
281
+ */
282
+ export function clipsChildren(node: Pick<SceneNode, 'type' | 'clipsContent'>): boolean {
283
+ return node.clipsContent && CLIPPING_TYPES.has(node.type)
284
+ }
285
+
266
286
  export const CONTAINER_TYPES = new Set<NodeType>([
267
287
  'CANVAS',
268
288
  'FRAME',
@@ -1,5 +1,7 @@
1
+ import { appendAll } from '../arrays'
1
2
  import { cubicPoint } from '../cubic'
2
3
  import { GeometryCommand, readGeometryBlob } from '../geometry'
4
+ import { buildChains } from '../paths/network'
3
5
  import type { PolygonOutlineCommand } from '../polygon'
4
6
  import type { Vector } from '../primitives'
5
7
  import type { VectorNetwork } from '../types'
@@ -72,31 +74,37 @@ export function loopPoints(network: VectorNetwork, loop: number[], tolerance: nu
72
74
  const along = segmentPoints(network, index, tolerance)
73
75
  if (!segment || !along) continue
74
76
  const forward = segment.start === current
75
- points.push(...(forward ? along : along.toReversed()))
77
+ appendAll(points, forward ? along : along.toReversed())
76
78
  current = forward ? segment.end : segment.start
77
79
  }
78
80
  return points
79
81
  }
80
82
 
81
83
  /**
82
- * The network's segments joined into runs, a run continuing while each segment starts where the
83
- * last ended, as a path draws them one after another.
84
+ * The network's segments joined into runs, chained through the vertices they share as the canvas
85
+ * draws an open network (`buildChains`), each run walked from its open end.
84
86
  */
85
87
  export function segmentRuns(network: VectorNetwork, tolerance: number): Vector[][] {
86
88
  const runs: Vector[][] = []
87
- let run: Vector[] = []
88
- let end = -1
89
- for (const [index, segment] of network.segments.entries()) {
90
- const along = segmentPoints(network, index, tolerance)
91
- if (!along) continue
92
- if (segment.start === end && run.length > 0) run.push(...along.slice(1))
93
- else {
94
- if (run.length > 1) runs.push(run)
95
- run = along
89
+ for (const chain of buildChains(network.segments)) {
90
+ let current = chain.start
91
+ let run: Vector[] = []
92
+ for (const index of chain.segments) {
93
+ const segment = network.segments[index]
94
+ const forward = segment.start === current
95
+ current = forward ? segment.end : segment.start
96
+ const along = segmentPoints(network, index, tolerance)
97
+ // A segment naming a missing vertex breaks the run: what follows starts a run of its own.
98
+ if (!along) {
99
+ if (run.length > 1) runs.push(run)
100
+ run = []
101
+ continue
102
+ }
103
+ const points = forward ? along : along.toReversed()
104
+ appendAll(run, run.length > 0 ? points.slice(1) : points)
96
105
  }
97
- end = segment.end
106
+ if (run.length > 1) runs.push(run)
98
107
  }
99
- if (run.length > 1) runs.push(run)
100
108
  return runs
101
109
  }
102
110
 
@@ -124,9 +132,12 @@ export function blobContours(blob: Uint8Array, tolerance: number): Vector[][] {
124
132
  const q = { x: c[0], y: c[1] }
125
133
  const c1 = { x: last.x + (2 / 3) * (q.x - last.x), y: last.y + (2 / 3) * (q.y - last.y) }
126
134
  const c2 = { x: end.x + (2 / 3) * (q.x - end.x), y: end.y + (2 / 3) * (q.y - end.y) }
127
- contour.push(...cubicPoints(last, c1, c2, end, tolerance))
135
+ appendAll(contour, cubicPoints(last, c1, c2, end, tolerance))
128
136
  } else {
129
- contour.push(...cubicPoints(last, { x: c[0], y: c[1] }, { x: c[2], y: c[3] }, end, tolerance))
137
+ appendAll(
138
+ contour,
139
+ cubicPoints(last, { x: c[0], y: c[1] }, { x: c[2], y: c[3] }, end, tolerance)
140
+ )
130
141
  }
131
142
  })
132
143
  flush()
@@ -149,8 +160,9 @@ export function commandContours(
149
160
  const last = contour.at(-1) ?? { x: 0, y: 0 }
150
161
  if (command.type !== 'C') contour.push({ x: command.x, y: command.y })
151
162
  else
152
- contour.push(
153
- ...cubicPoints(
163
+ appendAll(
164
+ contour,
165
+ cubicPoints(
154
166
  last,
155
167
  { x: command.x1, y: command.y1 },
156
168
  { x: command.x2, y: command.y2 },