@open-pencil/scene-graph 0.17.0-beta.4 → 0.17.0-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (345) hide show
  1. package/dist/buffered-events.js +1 -1
  2. package/dist/buffered-events.js.map +1 -1
  3. package/dist/clipping.d.ts +7 -1
  4. package/dist/clipping.d.ts.map +1 -1
  5. package/dist/clipping.js +23 -19
  6. package/dist/clipping.js.map +1 -1
  7. package/dist/comments/schema.d.ts.map +1 -1
  8. package/dist/comments/schema.js +1 -0
  9. package/dist/comments/schema.js.map +1 -1
  10. package/dist/comments/threads.d.ts +2 -0
  11. package/dist/comments/threads.d.ts.map +1 -1
  12. package/dist/comments/threads.js +11 -5
  13. package/dist/comments/threads.js.map +1 -1
  14. package/dist/components/index.d.ts +2 -2
  15. package/dist/components/index.js +2 -2
  16. package/dist/components/properties.d.ts +1 -3
  17. package/dist/components/properties.d.ts.map +1 -1
  18. package/dist/components/properties.js +3 -10
  19. package/dist/components/properties.js.map +1 -1
  20. package/dist/constants.d.ts +12 -2
  21. package/dist/constants.d.ts.map +1 -1
  22. package/dist/constants.js +42 -1
  23. package/dist/constants.js.map +1 -1
  24. package/dist/copy.d.ts.map +1 -1
  25. package/dist/copy.js +2 -0
  26. package/dist/copy.js.map +1 -1
  27. package/dist/corners.d.ts +22 -0
  28. package/dist/corners.d.ts.map +1 -0
  29. package/dist/corners.js +52 -0
  30. package/dist/corners.js.map +1 -0
  31. package/dist/css/grid.d.ts +4 -1
  32. package/dist/css/grid.d.ts.map +1 -1
  33. package/dist/css/grid.js +40 -7
  34. package/dist/css/grid.js.map +1 -1
  35. package/dist/css/index.d.ts +3 -3
  36. package/dist/css/index.js +3 -3
  37. package/dist/css/layout.d.ts +13 -1
  38. package/dist/css/layout.d.ts.map +1 -1
  39. package/dist/css/layout.js +26 -1
  40. package/dist/css/layout.js.map +1 -1
  41. package/dist/css/values.d.ts +23 -1
  42. package/dist/css/values.d.ts.map +1 -1
  43. package/dist/css/values.js +39 -1
  44. package/dist/css/values.js.map +1 -1
  45. package/dist/font-style.d.ts +2 -1
  46. package/dist/font-style.d.ts.map +1 -1
  47. package/dist/font-style.js +31 -6
  48. package/dist/font-style.js.map +1 -1
  49. package/dist/geometry.d.ts +68 -3
  50. package/dist/geometry.d.ts.map +1 -1
  51. package/dist/geometry.js +159 -25
  52. package/dist/geometry.js.map +1 -1
  53. package/dist/gradient.d.ts +9 -2
  54. package/dist/gradient.d.ts.map +1 -1
  55. package/dist/gradient.js +29 -1
  56. package/dist/gradient.js.map +1 -1
  57. package/dist/graph.d.ts +9 -2
  58. package/dist/graph.d.ts.map +1 -1
  59. package/dist/graph.js +46 -10
  60. package/dist/graph.js.map +1 -1
  61. package/dist/group-bounds.d.ts.map +1 -1
  62. package/dist/group-bounds.js +4 -3
  63. package/dist/group-bounds.js.map +1 -1
  64. package/dist/guides.d.ts +11 -2
  65. package/dist/guides.d.ts.map +1 -1
  66. package/dist/guides.js +30 -1
  67. package/dist/guides.js.map +1 -1
  68. package/dist/hit-test.d.ts.map +1 -1
  69. package/dist/hit-test.js +57 -5
  70. package/dist/hit-test.js.map +1 -1
  71. package/dist/icons/fit.d.ts +17 -0
  72. package/dist/icons/fit.d.ts.map +1 -0
  73. package/dist/icons/fit.js +21 -0
  74. package/dist/icons/fit.js.map +1 -0
  75. package/dist/icons/glyph.d.ts.map +1 -1
  76. package/dist/icons/glyph.js +5 -4
  77. package/dist/icons/glyph.js.map +1 -1
  78. package/dist/icons/index.d.ts +2 -1
  79. package/dist/icons/index.js +2 -1
  80. package/dist/ids.js +1 -1
  81. package/dist/ids.js.map +1 -1
  82. package/dist/images.d.ts +14 -4
  83. package/dist/images.d.ts.map +1 -1
  84. package/dist/images.js +45 -5
  85. package/dist/images.js.map +1 -1
  86. package/dist/index.d.ts +2 -2
  87. package/dist/instances/assignments.js +39 -6
  88. package/dist/instances/assignments.js.map +1 -1
  89. package/dist/instances/index.d.ts +6 -1
  90. package/dist/instances/index.d.ts.map +1 -1
  91. package/dist/instances/index.js +58 -9
  92. package/dist/instances/index.js.map +1 -1
  93. package/dist/instances/kept-overrides.js +2 -2
  94. package/dist/instances/kept-overrides.js.map +1 -1
  95. package/dist/instances/matching.js +68 -0
  96. package/dist/instances/matching.js.map +1 -0
  97. package/dist/instances/overrides.d.ts +4 -1
  98. package/dist/instances/overrides.d.ts.map +1 -1
  99. package/dist/instances/overrides.js +13 -1
  100. package/dist/instances/overrides.js.map +1 -1
  101. package/dist/instances/protection.js +67 -0
  102. package/dist/instances/protection.js.map +1 -0
  103. package/dist/instances/swap.js +7 -5
  104. package/dist/instances/swap.js.map +1 -1
  105. package/dist/instances/sync.js +34 -116
  106. package/dist/instances/sync.js.map +1 -1
  107. package/dist/layout/guides.d.ts +17 -1
  108. package/dist/layout/guides.d.ts.map +1 -1
  109. package/dist/layout/guides.js +31 -11
  110. package/dist/layout/guides.js.map +1 -1
  111. package/dist/layout/index.d.ts +5 -2
  112. package/dist/layout/index.js +5 -2
  113. package/dist/layout/limits.d.ts +40 -0
  114. package/dist/layout/limits.d.ts.map +1 -0
  115. package/dist/layout/limits.js +54 -0
  116. package/dist/layout/limits.js.map +1 -0
  117. package/dist/layout/sizing.d.ts.map +1 -1
  118. package/dist/layout/sizing.js +0 -1
  119. package/dist/layout/sizing.js.map +1 -1
  120. package/dist/layout/spacing.d.ts +10 -0
  121. package/dist/layout/spacing.d.ts.map +1 -0
  122. package/dist/layout/spacing.js +12 -0
  123. package/dist/layout/spacing.js.map +1 -0
  124. package/dist/layout/strokes.d.ts +10 -0
  125. package/dist/layout/strokes.d.ts.map +1 -0
  126. package/dist/layout/strokes.js +30 -0
  127. package/dist/layout/strokes.js.map +1 -0
  128. package/dist/masks.d.ts +28 -0
  129. package/dist/masks.d.ts.map +1 -0
  130. package/dist/masks.js +53 -0
  131. package/dist/masks.js.map +1 -0
  132. package/dist/matrix.d.ts +2 -1
  133. package/dist/matrix.d.ts.map +1 -1
  134. package/dist/matrix.js +14 -1
  135. package/dist/matrix.js.map +1 -1
  136. package/dist/node-defaults.d.ts +9 -1
  137. package/dist/node-defaults.d.ts.map +1 -1
  138. package/dist/node-defaults.js +65 -38
  139. package/dist/node-defaults.js.map +1 -1
  140. package/dist/outline/flatten.d.ts.map +1 -1
  141. package/dist/outline/flatten.js +24 -17
  142. package/dist/outline/flatten.js.map +1 -1
  143. package/dist/outline/hit.d.ts +2 -8
  144. package/dist/outline/hit.d.ts.map +1 -1
  145. package/dist/outline/hit.js +16 -25
  146. package/dist/outline/hit.js.map +1 -1
  147. package/dist/outline/index.d.ts +3 -3
  148. package/dist/outline/index.js +3 -3
  149. package/dist/outline/shapes.d.ts +3 -1
  150. package/dist/outline/shapes.d.ts.map +1 -1
  151. package/dist/outline/shapes.js +18 -39
  152. package/dist/outline/shapes.js.map +1 -1
  153. package/dist/paints/index.d.ts +3 -1
  154. package/dist/paints/index.js +3 -1
  155. package/dist/paints/pattern.d.ts +13 -0
  156. package/dist/paints/pattern.d.ts.map +1 -0
  157. package/dist/paints/pattern.js +47 -0
  158. package/dist/paints/pattern.js.map +1 -0
  159. package/dist/paints/shows.d.ts +11 -0
  160. package/dist/paints/shows.d.ts.map +1 -0
  161. package/dist/paints/shows.js +16 -0
  162. package/dist/paints/shows.js.map +1 -0
  163. package/dist/parse-path.d.ts +4 -1
  164. package/dist/parse-path.d.ts.map +1 -1
  165. package/dist/parse-path.js +15 -1
  166. package/dist/parse-path.js.map +1 -1
  167. package/dist/paths/arc.d.ts +32 -0
  168. package/dist/paths/arc.d.ts.map +1 -0
  169. package/dist/paths/arc.js +49 -0
  170. package/dist/paths/arc.js.map +1 -0
  171. package/dist/paths/index.d.ts +5 -0
  172. package/dist/paths/index.js +5 -0
  173. package/dist/paths/network.d.ts +63 -0
  174. package/dist/paths/network.d.ts.map +1 -0
  175. package/dist/paths/network.js +315 -0
  176. package/dist/paths/network.js.map +1 -0
  177. package/dist/paths/sink.d.ts +20 -0
  178. package/dist/paths/sink.d.ts.map +1 -0
  179. package/dist/paths/sink.js +32 -0
  180. package/dist/paths/sink.js.map +1 -0
  181. package/dist/paths/smooth-corners.d.ts +25 -0
  182. package/dist/paths/smooth-corners.d.ts.map +1 -0
  183. package/dist/paths/smooth-corners.js +196 -0
  184. package/dist/paths/smooth-corners.js.map +1 -0
  185. package/dist/plugin-data/fields.d.ts +4 -0
  186. package/dist/plugin-data/fields.d.ts.map +1 -1
  187. package/dist/plugin-data/fields.js +2 -0
  188. package/dist/plugin-data/fields.js.map +1 -1
  189. package/dist/polygon.d.ts +1 -3
  190. package/dist/polygon.d.ts.map +1 -1
  191. package/dist/polygon.js +1 -16
  192. package/dist/polygon.js.map +1 -1
  193. package/dist/primitives/index.d.ts +1 -7
  194. package/dist/primitives/index.d.ts.map +1 -1
  195. package/dist/resize.d.ts.map +1 -1
  196. package/dist/resize.js +5 -5
  197. package/dist/resize.js.map +1 -1
  198. package/dist/scaling/node.d.ts.map +1 -1
  199. package/dist/scaling/node.js +25 -3
  200. package/dist/scaling/node.js.map +1 -1
  201. package/dist/shaders/model.d.ts.map +1 -1
  202. package/dist/shaders/model.js +1 -1
  203. package/dist/shaders/model.js.map +1 -1
  204. package/dist/shared-styles.d.ts.map +1 -1
  205. package/dist/shared-styles.js +2 -9
  206. package/dist/shared-styles.js.map +1 -1
  207. package/dist/strokes.d.ts +39 -0
  208. package/dist/strokes.d.ts.map +1 -0
  209. package/dist/strokes.js +104 -0
  210. package/dist/strokes.js.map +1 -0
  211. package/dist/text/auto-resize.d.ts +15 -0
  212. package/dist/text/auto-resize.d.ts.map +1 -0
  213. package/dist/text/auto-resize.js +23 -0
  214. package/dist/text/auto-resize.js.map +1 -0
  215. package/dist/text/index.d.ts +5 -3
  216. package/dist/text/index.js +5 -3
  217. package/dist/text/lengths.d.ts +6 -1
  218. package/dist/text/lengths.d.ts.map +1 -1
  219. package/dist/text/lengths.js +10 -1
  220. package/dist/text/lengths.js.map +1 -1
  221. package/dist/text/overflow.d.ts +14 -5
  222. package/dist/text/overflow.d.ts.map +1 -1
  223. package/dist/text/overflow.js +24 -6
  224. package/dist/text/overflow.js.map +1 -1
  225. package/dist/text/path.js +10 -0
  226. package/dist/text/path.js.map +1 -0
  227. package/dist/text/picture.js +5 -5
  228. package/dist/text/picture.js.map +1 -1
  229. package/dist/text/runs.d.ts +29 -0
  230. package/dist/text/runs.d.ts.map +1 -0
  231. package/dist/text/runs.js +72 -0
  232. package/dist/text/runs.js.map +1 -0
  233. package/dist/transfer/dependencies.d.ts.map +1 -1
  234. package/dist/transfer/dependencies.js +3 -2
  235. package/dist/transfer/dependencies.js.map +1 -1
  236. package/dist/transfer/nodes.js +2 -1
  237. package/dist/transfer/nodes.js.map +1 -1
  238. package/dist/types.d.ts +56 -10
  239. package/dist/types.d.ts.map +1 -1
  240. package/dist/types.js.map +1 -1
  241. package/dist/variables/index.d.ts +4 -3
  242. package/dist/variables/index.d.ts.map +1 -1
  243. package/dist/variables/index.js +4 -3
  244. package/dist/variables/index.js.map +1 -1
  245. package/dist/variables/lookup.d.ts +20 -0
  246. package/dist/variables/lookup.d.ts.map +1 -0
  247. package/dist/variables/lookup.js +34 -0
  248. package/dist/variables/lookup.js.map +1 -0
  249. package/dist/variables/paints.d.ts +35 -4
  250. package/dist/variables/paints.d.ts.map +1 -1
  251. package/dist/variables/paints.js +67 -16
  252. package/dist/variables/paints.js.map +1 -1
  253. package/dist/variables/resolution.d.ts +3 -1
  254. package/dist/variables/resolution.d.ts.map +1 -1
  255. package/dist/variables/resolution.js +8 -3
  256. package/dist/variables/resolution.js.map +1 -1
  257. package/dist/variables/subtree.js +1 -1
  258. package/dist/variables/subtree.js.map +1 -1
  259. package/dist/vector-network.d.ts.map +1 -1
  260. package/dist/vector-network.js +13 -79
  261. package/dist/vector-network.js.map +1 -1
  262. package/package.json +28 -7
  263. package/src/buffered-events.ts +2 -1
  264. package/src/clipping.ts +11 -8
  265. package/src/comments/schema.ts +1 -0
  266. package/src/comments/threads.ts +13 -5
  267. package/src/components/properties.ts +3 -14
  268. package/src/constants.ts +25 -1
  269. package/src/copy.ts +2 -0
  270. package/src/corners.ts +93 -0
  271. package/src/css/grid.ts +49 -7
  272. package/src/css/index.ts +13 -2
  273. package/src/css/layout.ts +36 -0
  274. package/src/css/values.ts +49 -0
  275. package/src/font-style.ts +38 -4
  276. package/src/geometry.ts +223 -28
  277. package/src/gradient.ts +23 -1
  278. package/src/graph.ts +35 -10
  279. package/src/group-bounds.ts +7 -3
  280. package/src/guides.ts +26 -1
  281. package/src/hit-test.ts +68 -10
  282. package/src/icons/fit.ts +24 -0
  283. package/src/icons/glyph.ts +6 -4
  284. package/src/icons/index.ts +1 -0
  285. package/src/ids.ts +1 -1
  286. package/src/images.ts +67 -7
  287. package/src/instances/assignments.ts +65 -7
  288. package/src/instances/index.ts +77 -13
  289. package/src/instances/kept-overrides.ts +2 -5
  290. package/src/instances/matching.ts +101 -0
  291. package/src/instances/overrides.ts +18 -0
  292. package/src/instances/protection.ts +103 -0
  293. package/src/instances/swap.ts +12 -9
  294. package/src/instances/sync.ts +52 -184
  295. package/src/layout/guides.ts +42 -9
  296. package/src/layout/index.ts +3 -0
  297. package/src/layout/limits.ts +64 -0
  298. package/src/layout/sizing.ts +0 -2
  299. package/src/layout/spacing.ts +11 -0
  300. package/src/layout/strokes.ts +23 -0
  301. package/src/masks.ts +51 -0
  302. package/src/matrix.ts +15 -2
  303. package/src/node-defaults.ts +71 -36
  304. package/src/outline/flatten.ts +31 -18
  305. package/src/outline/hit.ts +16 -33
  306. package/src/outline/index.ts +2 -2
  307. package/src/outline/shapes.ts +19 -36
  308. package/src/paints/index.ts +2 -0
  309. package/src/paints/pattern.ts +45 -0
  310. package/src/paints/shows.ts +13 -0
  311. package/src/parse-path.ts +14 -0
  312. package/src/paths/arc.ts +59 -0
  313. package/src/paths/index.ts +4 -0
  314. package/src/paths/network.ts +411 -0
  315. package/src/paths/sink.ts +50 -0
  316. package/src/paths/smooth-corners.ts +357 -0
  317. package/src/plugin-data/fields.ts +2 -0
  318. package/src/polygon.ts +0 -17
  319. package/src/primitives/index.ts +0 -5
  320. package/src/resize.ts +6 -7
  321. package/src/scaling/node.ts +29 -3
  322. package/src/shaders/model.ts +2 -1
  323. package/src/shared-styles.ts +3 -4
  324. package/src/strokes.ts +121 -0
  325. package/src/text/auto-resize.ts +25 -0
  326. package/src/text/index.ts +2 -0
  327. package/src/text/lengths.ts +11 -0
  328. package/src/text/overflow.ts +32 -6
  329. package/src/text/path.ts +13 -0
  330. package/src/text/picture.ts +5 -5
  331. package/src/text/runs.ts +80 -0
  332. package/src/transfer/dependencies.ts +4 -2
  333. package/src/transfer/nodes.ts +2 -1
  334. package/src/types.ts +62 -8
  335. package/src/variables/index.ts +1 -0
  336. package/src/variables/lookup.ts +31 -0
  337. package/src/variables/paints.ts +101 -20
  338. package/src/variables/resolution.ts +15 -2
  339. package/src/variables/subtree.ts +1 -1
  340. package/src/vector-network.ts +19 -142
  341. package/dist/random.d.ts +0 -7
  342. package/dist/random.d.ts.map +0 -1
  343. package/dist/random.js +0 -25
  344. package/dist/random.js.map +0 -1
  345. package/src/random.ts +0 -28
package/src/graph.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /* eslint-disable max-lines -- SceneGraph is the facade over the domain modules */
2
2
  import { omitBy, pick } from 'es-toolkit/object'
3
- import { isUndefined } from 'es-toolkit/predicate'
3
+ import { isEmptyObject, isUndefined } from 'es-toolkit/predicate'
4
4
 
5
5
  import { removeStaleBindings } from './bindings'
6
6
  import { BufferedSceneEmitter } from './buffered-events'
@@ -25,6 +25,7 @@ import { CONTAINER_TYPES, createDefaultNode } from './node-defaults'
25
25
  import { updateNodePreview, type NodePreviewObserver } from './preview'
26
26
  import type { Color, Rect, Vector } from './primitives'
27
27
  import { markSourceFieldsEdited } from './source-metadata'
28
+ import { strokeLineChanges } from './strokes'
28
29
  import { textCacheInvalidationChanges } from './text/picture'
29
30
  import { remapCopiedReferences } from './transfer/copied'
30
31
  import type {
@@ -464,6 +465,7 @@ export class SceneGraph {
464
465
 
465
466
  createNode(type: NodeType, parentId: string, overrides: Partial<SceneNode> = {}): SceneNode {
466
467
  const node = createDefaultNode(() => this.generateEntityId(), type, overrides)
468
+ Object.assign(node, strokeLineChanges(node, overrides))
467
469
  const parent = this.nodes.get(parentId)
468
470
  this.willChangeNode(parent)
469
471
  parent?.childIds.push(node.id)
@@ -586,8 +588,9 @@ export class SceneGraph {
586
588
  }
587
589
  updateNodePreview(id: string, changes: Partial<SceneNode>): void {
588
590
  const node = this.nodes.get(id)
589
- if (node) changes = this.sizedChanges(node, changes)
591
+ if (node) changes = this.impliedChanges(node, changes)
590
592
  const appliedChanges = updateNodePreview(this, id, changes, (node, applied) => {
593
+ for (const observer of this.changeObservers) observer.previewed?.(node)
591
594
  for (const observe of this.previewObservers) observe(node, applied)
592
595
  })
593
596
  if (appliedChanges) this.emitter.emit('node:previewUpdated', id, appliedChanges)
@@ -605,12 +608,17 @@ export class SceneGraph {
605
608
 
606
609
  const node = this.nodes.get(id)
607
610
  if (!node) return
608
- this.applyNodeChanges(node, this.sizedChanges(node, omitBy(changes, isUndefined)))
611
+ this.applyNodeChanges(node, this.impliedChanges(node, omitBy(changes, isUndefined)))
609
612
  }
610
613
 
611
- /** A size set from outside layout, with what it also sets on a generated frame. */
612
- private sizedChanges(node: SceneNode, changes: Partial<SceneNode>): Partial<SceneNode> {
613
- return this.isApplyingLayout ? changes : withGeneratedFrameSize(node, changes)
614
+ /**
615
+ * `changes` with what they also set: a size from outside layout on a generated frame, and
616
+ * strokes and the layer's line fields kept in agreement (`strokeLineChanges`).
617
+ */
618
+ private impliedChanges(node: SceneNode, changes: Partial<SceneNode>): Partial<SceneNode> {
619
+ const sized = this.isApplyingLayout ? changes : withGeneratedFrameSize(node, changes)
620
+ const lines = strokeLineChanges(node, sized)
621
+ return isEmptyObject(lines) ? sized : { ...sized, ...lines }
614
622
  }
615
623
 
616
624
  /** Replay captured properties without dropping explicit undefined values or absent keys. */
@@ -919,16 +927,33 @@ export class SceneGraph {
919
927
  return Instances.getInstances(this, componentId)
920
928
  }
921
929
 
930
+ /**
931
+ * The descendants of a layer in depth-first order with their depths. Each layer with children is
932
+ * walked once, so a cycle or repeated links in bad data cannot hang it or repeat its subtree.
933
+ */
922
934
  flattenTree(parentId?: string, depth = 0): Array<{ node: SceneNode; depth: number }> {
923
935
  const id = parentId ?? this.rootId
924
936
  const parent = this.nodes.get(id)
925
937
  if (!parent) return []
926
938
  const result: Array<{ node: SceneNode; depth: number }> = []
927
- for (const childId of parent.childIds) {
928
- const child = this.nodes.get(childId)
939
+ const pending = [{ node: parent, depth, index: 0 }]
940
+ const walked = new Set([parent])
941
+ while (pending.length > 0) {
942
+ const frame = pending.at(-1)
943
+ if (!frame) break
944
+ if (frame.index >= frame.node.childIds.length) {
945
+ pending.pop()
946
+ continue
947
+ }
948
+ const child = this.nodes.get(frame.node.childIds[frame.index++])
929
949
  if (!child) continue
930
- result.push({ node: child, depth })
931
- if (child.childIds.length > 0) result.push(...this.flattenTree(childId, depth + 1))
950
+ const container = child.childIds.length > 0
951
+ if (container && walked.has(child)) continue
952
+ result.push({ node: child, depth: frame.depth })
953
+ if (container) {
954
+ walked.add(child)
955
+ pending.push({ node: child, depth: frame.depth + 1, index: 0 })
956
+ }
932
957
  }
933
958
  return result
934
959
  }
@@ -1,5 +1,6 @@
1
1
  import { getAxisAlignedBoundsInParent, getNodeLocalMatrix } from './coordinate'
2
2
  import type { SceneGraph } from './graph'
3
+ import { isVisibleMask, maskRuns } from './masks'
3
4
  import Matrix from './matrix'
4
5
  import { FITTED_CONTAINER_TYPES } from './node-defaults'
5
6
  import type { Rect, Vector } from './primitives'
@@ -77,9 +78,12 @@ function fittedAncestors(graph: SceneGraph, parentIds: Iterable<string>): SceneN
77
78
  */
78
79
  function groupBoundingLayers(children: readonly SceneNode[]): SceneNode[] {
79
80
  const shown = children.filter((child) => child.visible)
80
- const firstMask = shown.findIndex((child) => child.isMask)
81
- if (firstMask === -1) return shown
82
- return [...shown.slice(0, firstMask), ...shown.slice(firstMask).filter((c) => c.isMask)]
81
+ const runs = maskRuns(shown, isVisibleMask)
82
+ if (runs.length === 0) return shown
83
+ return [
84
+ ...shown.slice(0, runs[0].start),
85
+ ...runs.flatMap((run) => shown.slice(run.start, run.maskedStart))
86
+ ]
83
87
  }
84
88
 
85
89
  /**
package/src/guides.ts CHANGED
@@ -1,5 +1,7 @@
1
+ import { getWorldMatrix } from './coordinate'
1
2
  import type { SceneGraph } from './graph'
2
- import type { GUID } from './primitives'
3
+ import Matrix from './matrix'
4
+ import type { GUID, Vector } from './primitives'
3
5
  import type { SceneNode } from './types'
4
6
 
5
7
  export interface CanvasGuide {
@@ -50,3 +52,26 @@ export function guideOwnersOnPage(graph: SceneGraph, pageId: string): SceneNode[
50
52
  }
51
53
  return owners
52
54
  }
55
+
56
+ /**
57
+ * A guide or layout line `owner` carries, across its box from edge to edge at `position` along
58
+ * `axis` in its own axes, in page coordinates: where the canvas draws it, a pointer finds it, and
59
+ * a drag snaps to it.
60
+ */
61
+ export function guideLineInWorld(
62
+ graph: SceneGraph,
63
+ owner: SceneNode,
64
+ axis: CanvasGuide['axis'],
65
+ position: number
66
+ ): { start: Vector; end: Vector } {
67
+ const matrix = getWorldMatrix(owner, graph)
68
+ return axis === 'x'
69
+ ? {
70
+ start: Matrix.mapPoint(matrix, { x: position, y: 0 }),
71
+ end: Matrix.mapPoint(matrix, { x: position, y: owner.height })
72
+ }
73
+ : {
74
+ start: Matrix.mapPoint(matrix, { x: 0, y: position }),
75
+ end: Matrix.mapPoint(matrix, { x: owner.width, y: position })
76
+ }
77
+ }
package/src/hit-test.ts CHANGED
@@ -1,8 +1,11 @@
1
1
  import { hitsConnectorLine } from './connectors/hit'
2
2
  import { getWorldMatrix } from './coordinate'
3
3
  import type { SceneGraph } from './graph'
4
+ import { asMaskShape, isVisibleMask, maskRuns, type MaskRun } from './masks'
4
5
  import Matrix from './matrix'
5
- import { hitsShape, isHitPaint, strokeReach } from './outline/hit'
6
+ import { clipsChildren } from './node-defaults'
7
+ import { hitsShape, strokeReach } from './outline/hit'
8
+ import { paintShows } from './paints/shows'
6
9
  import type { SceneNode, NodeType } from './types'
7
10
 
8
11
  const CONTAINER_TYPES = new Set<NodeType>([
@@ -16,9 +19,9 @@ const CONTAINER_TYPES = new Set<NodeType>([
16
19
  ])
17
20
  const OPAQUE_CONTAINER_TYPES = new Set<NodeType>(['COMPONENT', 'INSTANCE'])
18
21
 
19
- /** Whether a container's own paints show, so its empty area is drawn (see `isHitPaint`). */
22
+ /** Whether a container's own paints show, so its empty area is drawn (`paintShows`). */
20
23
  function showsPaint(node: SceneNode): boolean {
21
- return node.fills.some(isHitPaint) || node.strokes.some(isHitPaint)
24
+ return node.fills.some(paintShows) || node.strokes.some(paintShows)
22
25
  }
23
26
 
24
27
  function isTopLevel(graph: SceneGraph, node: SceneNode): boolean {
@@ -145,6 +148,51 @@ function hitTestTransparentContainer(
145
148
  return null
146
149
  }
147
150
 
151
+ /**
152
+ * The mask run holding each child, asked for from the top child down: the walk steps back through
153
+ * the runs once rather than searching them for every child.
154
+ */
155
+ function maskRunCursor(
156
+ graph: SceneGraph,
157
+ childIds: readonly string[]
158
+ ): (index: number) => MaskRun | undefined {
159
+ const runs = maskRuns(childIds, (id) => {
160
+ const child = graph.nodes.get(id)
161
+ return child !== undefined && isVisibleMask(child)
162
+ })
163
+ let runIndex = runs.length - 1
164
+ return (index) => {
165
+ while (runIndex >= 0 && runs[runIndex].start > index) runIndex--
166
+ const run = runs.at(runIndex)
167
+ return run && runIndex >= 0 && index >= run.maskedStart ? run : undefined
168
+ }
169
+ }
170
+
171
+ /**
172
+ * Whether the point lies on what a run's masks draw, or there is no run to hold it to. A container
173
+ * mask draws its own paint and its layers, so a click between its layers is outside it.
174
+ */
175
+ function insideMasks(
176
+ graph: SceneGraph,
177
+ px: number,
178
+ py: number,
179
+ childIds: readonly string[],
180
+ run: MaskRun | undefined,
181
+ walk: HitWalk
182
+ ): boolean {
183
+ if (!run) return true
184
+ return childIds.slice(run.start, run.maskedStart).some((id) => {
185
+ const mask = graph.nodes.get(id)
186
+ if (!mask) return false
187
+ if (!CONTAINER_TYPES.has(mask.type)) {
188
+ return hitsLeaf(graph, px, py, asMaskShape(mask), walk.transformCache, 0)
189
+ }
190
+ if (showsPaint(mask) && containsPoint(px, py, mask, graph, walk.transformCache)) return true
191
+ const drawn: HitWalk = { ...walk, deep: true, pad: 0, locked: true }
192
+ return hitTestChildren(graph, px, py, mask.id, drawn) !== null
193
+ })
194
+ }
195
+
148
196
  /** The topmost layer under the point among a container's children. */
149
197
  function hitTestChildren(
150
198
  graph: SceneGraph,
@@ -157,15 +205,30 @@ function hitTestChildren(
157
205
  if (!parent) return null
158
206
  const { transformCache, pad } = walk
159
207
 
160
- if (parent.clipsContent) {
208
+ if (clipsChildren(parent)) {
161
209
  if (!containsPoint(px, py, parent, graph, transformCache)) return null
162
210
  }
163
211
 
212
+ const runAt = maskRunCursor(graph, parent.childIds)
213
+ // Each run's masks are tested once per query, however many siblings they mask.
214
+ const covered = new Map<MaskRun, boolean>()
215
+ const inside = (run: MaskRun | undefined) => {
216
+ if (!run) return true
217
+ let hit = covered.get(run)
218
+ if (hit === undefined) {
219
+ hit = insideMasks(graph, px, py, parent.childIds, run, walk)
220
+ covered.set(run, hit)
221
+ }
222
+ return hit
223
+ }
164
224
  for (let i = parent.childIds.length - 1; i >= 0; i--) {
225
+ const run = runAt(i)
165
226
  const child = graph.nodes.get(parent.childIds[i])
166
227
  // A locked layer and everything in it are invisible to a press and to hover, as in Figma: a
167
228
  // click falls through to what lies under it, and a drag from it draws a marquee.
168
229
  if (!child || child.internalOnly || !child.visible || (child.locked && !walk.locked)) continue
230
+ // Where its masks cut a layer away, a click falls through to what lies under it (Figma, live).
231
+ if (!inside(run)) continue
169
232
  if (CONTAINER_TYPES.has(child.type)) {
170
233
  if (OPAQUE_CONTAINER_TYPES.has(child.type) && !walk.deep) {
171
234
  // A plain click takes an instance or component anywhere in its box, with a fill or
@@ -430,12 +493,7 @@ function dropTargetIn(
430
493
  transformCache: Map<string, boolean>
431
494
  ): SceneNode | null {
432
495
  // A clipped-away part of a child is not under the cursor.
433
- if (
434
- parent.type !== 'CANVAS' &&
435
- parent.clipsContent &&
436
- !containsPoint(px, py, parent, graph, transformCache)
437
- )
438
- return null
496
+ if (clipsChildren(parent) && !containsPoint(px, py, parent, graph, transformCache)) return null
439
497
 
440
498
  for (let i = parent.childIds.length - 1; i >= 0; i--) {
441
499
  const childId = parent.childIds[i]
@@ -0,0 +1,24 @@
1
+ import type { SceneNode } from '../types'
2
+
3
+ /** What centering an icon's glyph needs of a graph. */
4
+ interface GlyphGraph {
5
+ getNode(id: string): SceneNode | undefined
6
+ getChildren(id: string): SceneNode[]
7
+ updateNode(id: string, changes: Partial<SceneNode>): void
8
+ }
9
+
10
+ /**
11
+ * Moves a glyph drawn `size` square at its frame's origin to the middle of the frame, as SVG fits
12
+ * a square view box into a box of another shape. An icon is drawn at its frame's shorter side, so
13
+ * it keeps its shape in a frame that is not square, whether placed or swapped.
14
+ */
15
+ export function centerGlyph(graph: GlyphGraph, frameId: string, size: number): void {
16
+ const frame = graph.getNode(frameId)
17
+ if (!frame) return
18
+ const x = (frame.width - size) / 2
19
+ const y = (frame.height - size) / 2
20
+ if (x === 0 && y === 0) return
21
+ for (const path of graph.getChildren(frameId)) {
22
+ graph.updateNode(path.id, { x: path.x + x, y: path.y + y })
23
+ }
24
+ }
@@ -1,5 +1,7 @@
1
1
  import { omit } from 'es-toolkit'
2
2
 
3
+ import { maxOf, minOf } from '@open-pencil/std/arrays'
4
+
3
5
  import type { Fill, SceneNode, Stroke, VectorNetwork } from '../types'
4
6
  import { readIcon, readIconTint } from './model'
5
7
 
@@ -25,10 +27,10 @@ function shape(network: VectorNetwork | null | undefined) {
25
27
  const f = Math.fround
26
28
  const xs = network.vertices.map((vertex) => f(vertex.x))
27
29
  const ys = network.vertices.map((vertex) => f(vertex.y))
28
- const minX = Math.min(...xs)
29
- const minY = Math.min(...ys)
30
- const sx = 1 / (Math.max(...xs) - minX || 1)
31
- const sy = 1 / (Math.max(...ys) - minY || 1)
30
+ const minX = minOf(xs)
31
+ const minY = minOf(ys)
32
+ const sx = 1 / (maxOf(xs) - minX || 1)
33
+ const sy = 1 / (maxOf(ys) - minY || 1)
32
34
  return {
33
35
  vertices: network.vertices.map((vertex) => [
34
36
  round((f(vertex.x) - minX) * sx),
@@ -1,3 +1,4 @@
1
+ export * from './fit'
1
2
  export * from './glyph'
2
3
  export * from './model'
3
4
  export * from './schema'
package/src/ids.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { randomIndex } from './random'
1
+ import { randomIndex } from '@open-pencil/std/random'
2
2
 
3
3
  /** Figma reads both parts of a GUID as signed 32-bit integers and drops records beyond them. */
4
4
  const MAX_ID_SESSION = 0x7fffffff
package/src/images.ts CHANGED
@@ -3,16 +3,25 @@ import { bytesToHex } from '@noble/hashes/utils.js'
3
3
  import { imageSize as readImageHeader } from 'image-size'
4
4
  import { fromUint8Array, isValid, toUint8Array } from 'js-base64'
5
5
 
6
+ import MatrixMath, { type Mat3 } from './matrix'
6
7
  import type { Matrix, Size } from './primitives'
7
- import type { Fill } from './types'
8
+ import type { Fill, SceneNode } from './types'
9
+
10
+ /** Hashes already computed, by the bytes hashed: a save would otherwise hash every image again. */
11
+ const imageHashes = new WeakMap<Uint8Array, string>()
8
12
 
9
13
  /**
10
14
  * The name an image's bytes are stored under: the hex SHA-1 of the bytes, as Figma names images,
11
15
  * so `.fig` files and the Figma clipboard can carry the name as it is. Documents saved before may
12
- * hold images under other names; anything that hands images to Figma still checks.
16
+ * hold images under other names; anything that hands images to Figma still checks. Each array is
17
+ * hashed once, as image bytes are never written into once read or stored.
13
18
  */
14
19
  export function computeImageHash(data: Uint8Array): string {
15
- return bytesToHex(sha1(data))
20
+ const known = imageHashes.get(data)
21
+ if (known) return known
22
+ const hash = bytesToHex(sha1(data))
23
+ imageHashes.set(data, hash)
24
+ return hash
16
25
  }
17
26
 
18
27
  export type ImageMimeType = 'image/png' | 'image/jpeg' | 'image/gif' | 'image/webp' | 'image/avif'
@@ -134,11 +143,62 @@ export function coverImageTransform(
134
143
  return { m00, m01: 0, m02: (1 - m00) / 2, m10: 0, m11, m12: (1 - m11) / 2 }
135
144
  }
136
145
 
137
- /** The images `paints` show, by their hashes. */
138
- export function imagePaintHashes(paints: Iterable<Fill>): string[] {
139
- return [...paints].flatMap((paint) =>
140
- paint.type === 'IMAGE' && paint.imageHash ? [paint.imageHash] : []
146
+ /**
147
+ * Maps `img`'s pixels onto `node` as `fill` places them. `size` is the image's own size when `img`
148
+ * is a downscaled preview, so a tile keeps its size. A crop places the image by its transform;
149
+ * Fill, Fit and Tile ignore it, as Figma does, and turn the image by `imageRotation` first.
150
+ */
151
+ export function imageFillMatrix(
152
+ fill: Fill,
153
+ node: Pick<SceneNode, 'width' | 'height'>,
154
+ imgW: number,
155
+ imgH: number,
156
+ size: Size = { width: imgW, height: imgH }
157
+ ): Mat3 {
158
+ const scaleMode = fill.imageScaleMode ?? 'FILL'
159
+ if (scaleMode === 'CROP' && fill.imageTransform) {
160
+ const t = fill.imageTransform
161
+ const inverse = MatrixMath.invert(MatrixMath.fromAffine(t))
162
+ if (inverse)
163
+ return MatrixMath.multiply(
164
+ MatrixMath.scaled(node.width, node.height),
165
+ inverse,
166
+ MatrixMath.scaled(1 / imgW, 1 / imgH)
167
+ )
168
+ }
169
+
170
+ const radians = ((fill.imageRotation ?? 0) * Math.PI) / 180
171
+ const cos = Math.abs(Math.cos(radians))
172
+ const sin = Math.abs(Math.sin(radians))
173
+ const turnedW = size.width * cos + size.height * sin
174
+ const turnedH = size.width * sin + size.height * cos
175
+ const turned = MatrixMath.multiply(
176
+ MatrixMath.translated(turnedW / 2, turnedH / 2),
177
+ MatrixMath.rotated(radians),
178
+ MatrixMath.translated(-size.width / 2, -size.height / 2),
179
+ MatrixMath.scaled(size.width / imgW, size.height / imgH)
141
180
  )
181
+ if (scaleMode === 'TILE') {
182
+ const scale = fill.scale && fill.scale > 0 ? fill.scale : 1
183
+ return MatrixMath.multiply(MatrixMath.scaled(scale, scale), turned)
184
+ }
185
+ const fit = scaleMode === 'FIT'
186
+ const scale = (fit ? Math.min : Math.max)(node.width / turnedW, node.height / turnedH)
187
+ return MatrixMath.multiply(
188
+ MatrixMath.translated((node.width - turnedW * scale) / 2, (node.height - turnedH * scale) / 2),
189
+ MatrixMath.scaled(scale, scale),
190
+ turned
191
+ )
192
+ }
193
+
194
+ /** The image a paint draws: an IMAGE paint's own, or the poster frame of a VIDEO paint. */
195
+ export function drawnImageHash(paint: Pick<Fill, 'type' | 'imageHash'>): string | undefined {
196
+ return paint.type === 'IMAGE' || paint.type === 'VIDEO' ? paint.imageHash : undefined
197
+ }
198
+
199
+ /** The images `paints` show, by their hashes: a VIDEO paint shows its poster frame. */
200
+ export function imagePaintHashes(paints: Iterable<Fill>): string[] {
201
+ return [...paints].flatMap((paint) => drawnImageHash(paint) ?? [])
142
202
  }
143
203
 
144
204
  /**
@@ -1,6 +1,7 @@
1
1
  // What enclosing instances assign the component properties that drive a copy's layers.
2
2
  import type { SceneGraph } from '../graph'
3
3
  import type { ComponentPropertyReferenceField, SceneNode } from '../types'
4
+ import { assignmentOverrideField } from './overrides'
4
5
 
5
6
  /** SLOT_CONTENT is absent because it drives children, which `ownsSlotContent` keeps instead. */
6
7
  const PROPERTY_REFERENCE_FIELDS: Partial<Record<ComponentPropertyReferenceField, string>> = {
@@ -30,22 +31,28 @@ function definesProperty(graph: SceneGraph, instance: SceneNode, propertyId: str
30
31
  .some((definition) => definition.id === propertyId)
31
32
  }
32
33
 
34
+ interface Assignment {
35
+ /** The instance whose assignment it is. */
36
+ instance: SceneNode
37
+ value: string
38
+ }
39
+
33
40
  /**
34
41
  * What the nearest enclosing instance assigns `propertyId`, if any does. An instance assigns its
35
42
  * children, not itself, so the walk starts at the parent. A property id belongs to the component
36
43
  * that defines it, so the walk stops at an instance of that component even when it assigns
37
44
  * nothing; otherwise an outer instance's unrelated property of the same id wins.
38
45
  */
39
- function enclosingAssignment(
46
+ function findEnclosingAssignment(
40
47
  graph: SceneGraph,
41
48
  node: SceneNode,
42
49
  propertyId: string
43
- ): string | undefined {
50
+ ): Assignment | undefined {
44
51
  let current = node.parentId ? graph.nodes.get(node.parentId) : undefined
45
52
  while (current) {
46
53
  if (current.type === 'INSTANCE') {
47
54
  if (Object.hasOwn(current.componentPropertyAssignments, propertyId))
48
- return current.componentPropertyAssignments[propertyId]
55
+ return { instance: current, value: current.componentPropertyAssignments[propertyId] }
49
56
  if (definesProperty(graph, current, propertyId)) return undefined
50
57
  }
51
58
  current = current.parentId ? graph.nodes.get(current.parentId) : undefined
@@ -53,6 +60,55 @@ function enclosingAssignment(
53
60
  return undefined
54
61
  }
55
62
 
63
+ function enclosingAssignment(
64
+ graph: SceneGraph,
65
+ node: SceneNode,
66
+ propertyId: string
67
+ ): string | undefined {
68
+ return findEnclosingAssignment(graph, node, propertyId)?.value
69
+ }
70
+
71
+ /** The instances around `node`, nearest first. */
72
+ function enclosingInstances(graph: SceneGraph, node: SceneNode): SceneNode[] {
73
+ const instances: SceneNode[] = []
74
+ if (node.parentId)
75
+ graph.closest(node.parentId, (ancestor) => {
76
+ if (ancestor.type === 'INSTANCE') instances.push(ancestor)
77
+ return false
78
+ })
79
+ return instances
80
+ }
81
+
82
+ /** The outermost of `instances` that records `field` as an edit of `node`, or -1. */
83
+ function outermostEdit(instances: SceneNode[], node: SceneNode, field: string): number {
84
+ return instances.findLastIndex((instance) =>
85
+ instance.instanceOverrides.descendants.get(node.id)?.has(field)
86
+ )
87
+ }
88
+
89
+ /**
90
+ * Whether a text edit of `layer` outranks the assignment that drives it. As in Figma, the edit
91
+ * made in the outer instance wins, and at the same level the assignment does. An assignment is
92
+ * made where an override of it is recorded, or else by the instance that holds it.
93
+ */
94
+ function textEditOutranks(
95
+ graph: SceneGraph,
96
+ layer: SceneNode,
97
+ propertyId: string,
98
+ assignment: Assignment
99
+ ): boolean {
100
+ const instances = enclosingInstances(graph, layer)
101
+ const textLevel = outermostEdit(instances, layer, 'text')
102
+ if (textLevel < 0) return false
103
+ const overridden = outermostEdit(
104
+ instances,
105
+ assignment.instance,
106
+ assignmentOverrideField(propertyId)
107
+ )
108
+ const assignmentLevel = overridden < 0 ? instances.indexOf(assignment.instance) : overridden
109
+ return textLevel > assignmentLevel
110
+ }
111
+
56
112
  function hasEnclosingAssignment(graph: SceneGraph, node: SceneNode, propertyId: string): boolean {
57
113
  return enclosingAssignment(graph, node, propertyId) !== undefined
58
114
  }
@@ -107,10 +163,12 @@ export function assignedFieldValues(
107
163
  ): Partial<SceneNode> {
108
164
  const values: Partial<SceneNode> = {}
109
165
  for (const reference of compChild.componentPropertyReferences) {
110
- const value = enclosingAssignment(graph, instChild, reference.propertyId)
111
- if (value === undefined) continue
112
- if (reference.field === 'TEXT' && instChild.type === 'TEXT') values.text = value
113
- else if (reference.field === 'VISIBLE') values.visible = value === 'true'
166
+ const assignment = findEnclosingAssignment(graph, instChild, reference.propertyId)
167
+ if (!assignment) continue
168
+ if (reference.field === 'TEXT' && instChild.type === 'TEXT') {
169
+ if (!textEditOutranks(graph, instChild, reference.propertyId, assignment))
170
+ values.text = assignment.value
171
+ } else if (reference.field === 'VISIBLE') values.visible = assignment.value === 'true'
114
172
  }
115
173
  return values
116
174
  }