@pairdraw/core 0.19.0 → 0.19.2

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 (263) hide show
  1. package/dist/canvas/overlays/index.d.ts +2 -2
  2. package/dist/canvas/overlays/index.js +2 -2
  3. package/dist/canvas/overlays/selection.d.ts +1 -2
  4. package/dist/canvas/overlays/selection.d.ts.map +1 -1
  5. package/dist/canvas/overlays/selection.js +2 -9
  6. package/dist/canvas/overlays/selection.js.map +1 -1
  7. package/dist/canvas/renderer/fonts.d.ts.map +1 -1
  8. package/dist/canvas/renderer/fonts.js +2 -0
  9. package/dist/canvas/renderer/fonts.js.map +1 -1
  10. package/dist/canvas/renderer/methods.d.ts +0 -1
  11. package/dist/canvas/renderer/methods.d.ts.map +1 -1
  12. package/dist/canvas/renderer/methods.js +1 -4
  13. package/dist/canvas/renderer/methods.js.map +1 -1
  14. package/dist/canvas/renderer/overlay-pass.d.ts.map +1 -1
  15. package/dist/canvas/renderer/overlay-pass.js +0 -1
  16. package/dist/canvas/renderer/overlay-pass.js.map +1 -1
  17. package/dist/canvas/renderer/pipeline.d.ts.map +1 -1
  18. package/dist/canvas/renderer/pipeline.js +0 -1
  19. package/dist/canvas/renderer/pipeline.js.map +1 -1
  20. package/dist/canvas/renderer/types.d.ts +0 -1
  21. package/dist/canvas/renderer/types.d.ts.map +1 -1
  22. package/dist/canvas/renderer.d.ts +0 -1
  23. package/dist/canvas/renderer.d.ts.map +1 -1
  24. package/dist/canvas/renderer.js.map +1 -1
  25. package/dist/design-jsx/index.d.ts +2 -2
  26. package/dist/design-jsx/index.js +2 -2
  27. package/dist/design-jsx/renderer.d.ts +3 -2
  28. package/dist/design-jsx/renderer.d.ts.map +1 -1
  29. package/dist/design-jsx/renderer.js +2 -2
  30. package/dist/design-jsx/renderer.js.map +1 -1
  31. package/dist/editor/clipboard/snapshot.d.ts.map +1 -1
  32. package/dist/editor/clipboard/snapshot.js +12 -4
  33. package/dist/editor/clipboard/snapshot.js.map +1 -1
  34. package/dist/editor/clipboard.d.ts.map +1 -1
  35. package/dist/editor/clipboard.js +2 -7
  36. package/dist/editor/clipboard.js.map +1 -1
  37. package/dist/editor/color-space.d.ts.map +1 -1
  38. package/dist/editor/color-space.js +33 -38
  39. package/dist/editor/color-space.js.map +1 -1
  40. package/dist/editor/create.d.ts +9 -5
  41. package/dist/editor/create.d.ts.map +1 -1
  42. package/dist/editor/create.js +10 -1
  43. package/dist/editor/create.js.map +1 -1
  44. package/dist/editor/history/edit.d.ts.map +1 -1
  45. package/dist/editor/history/edit.js +2 -1
  46. package/dist/editor/history/edit.js.map +1 -1
  47. package/dist/editor/nudge.js +1 -1
  48. package/dist/editor/nudge.js.map +1 -1
  49. package/dist/editor/read-only/actions.js +2 -0
  50. package/dist/editor/read-only/actions.js.map +1 -1
  51. package/dist/editor/selection/container.d.ts +2 -0
  52. package/dist/editor/selection/container.d.ts.map +1 -1
  53. package/dist/editor/selection/container.js +17 -1
  54. package/dist/editor/selection/container.js.map +1 -1
  55. package/dist/editor/selection/navigation.js +7 -4
  56. package/dist/editor/selection/navigation.js.map +1 -1
  57. package/dist/editor/selection/read.d.ts +2 -2
  58. package/dist/editor/selection.d.ts +4 -2
  59. package/dist/editor/selection.d.ts.map +1 -1
  60. package/dist/editor/shapes/with-text.d.ts.map +1 -1
  61. package/dist/editor/shapes/with-text.js +2 -5
  62. package/dist/editor/shapes/with-text.js.map +1 -1
  63. package/dist/editor/structure/auto-layout-wrap.js +1 -1
  64. package/dist/editor/structure/auto-layout-wrap.js.map +1 -1
  65. package/dist/editor/structure/boolean.d.ts +2 -3
  66. package/dist/editor/structure/boolean.d.ts.map +1 -1
  67. package/dist/editor/structure/boolean.js.map +1 -1
  68. package/dist/editor/structure/group/bounds.d.ts.map +1 -1
  69. package/dist/editor/structure/group/bounds.js +1 -3
  70. package/dist/editor/structure/group/bounds.js.map +1 -1
  71. package/dist/editor/structure/outline-stroke.js +3 -3
  72. package/dist/editor/structure/outline-stroke.js.map +1 -1
  73. package/dist/editor/structure/reorder.d.ts.map +1 -1
  74. package/dist/editor/structure/reorder.js +7 -14
  75. package/dist/editor/structure/reorder.js.map +1 -1
  76. package/dist/editor/structure/state.js +1 -1
  77. package/dist/editor/structure/state.js.map +1 -1
  78. package/dist/editor/structure.d.ts +1 -2
  79. package/dist/editor/structure.d.ts.map +1 -1
  80. package/dist/editor/structure.js +3 -3
  81. package/dist/editor/structure.js.map +1 -1
  82. package/dist/editor/types.d.ts +4 -1
  83. package/dist/editor/types.d.ts.map +1 -1
  84. package/dist/editor/variables.d.ts.map +1 -1
  85. package/dist/editor/variables.js +57 -1
  86. package/dist/editor/variables.js.map +1 -1
  87. package/dist/figma-api/accessors/text/index.d.ts.map +1 -1
  88. package/dist/figma-api/accessors/text/index.js +4 -1
  89. package/dist/figma-api/accessors/text/index.js.map +1 -1
  90. package/dist/figma-api/fonts/loaded.d.ts +3 -1
  91. package/dist/figma-api/fonts/loaded.d.ts.map +1 -1
  92. package/dist/figma-api/fonts/loaded.js +8 -1
  93. package/dist/figma-api/fonts/loaded.js.map +1 -1
  94. package/dist/figma-api/index.d.ts +13 -0
  95. package/dist/figma-api/index.d.ts.map +1 -1
  96. package/dist/figma-api/index.js +13 -0
  97. package/dist/figma-api/index.js.map +1 -1
  98. package/dist/figma-api/serialization.d.ts.map +1 -1
  99. package/dist/figma-api/serialization.js +2 -1
  100. package/dist/figma-api/serialization.js.map +1 -1
  101. package/dist/figma-api/shape-details.d.ts +26 -0
  102. package/dist/figma-api/shape-details.d.ts.map +1 -0
  103. package/dist/figma-api/shape-details.js +28 -0
  104. package/dist/figma-api/shape-details.js.map +1 -0
  105. package/dist/figma-api/shape-with-text.js +2 -5
  106. package/dist/figma-api/shape-with-text.js.map +1 -1
  107. package/dist/figma-api/surface.d.ts +1 -1
  108. package/dist/figma-api/surface.d.ts.map +1 -1
  109. package/dist/figma-api/surface.js +1 -0
  110. package/dist/figma-api/surface.js.map +1 -1
  111. package/dist/icons/svg.js +1 -1
  112. package/dist/icons/svg.js.map +1 -1
  113. package/dist/io/formats/design-tokens/apply.d.ts +1 -1
  114. package/dist/io/formats/drawio/index.d.ts.map +1 -1
  115. package/dist/io/formats/drawio/index.js +1 -1
  116. package/dist/io/formats/drawio/index.js.map +1 -1
  117. package/dist/io/formats/html/layers.d.ts +7 -4
  118. package/dist/io/formats/html/layers.d.ts.map +1 -1
  119. package/dist/io/formats/html/layers.js +4 -1
  120. package/dist/io/formats/html/layers.js.map +1 -1
  121. package/dist/io/formats/pdf/dom.d.ts +6 -1
  122. package/dist/io/formats/pdf/dom.d.ts.map +1 -1
  123. package/dist/io/formats/pdf/dom.js +9 -22
  124. package/dist/io/formats/pdf/dom.js.map +1 -1
  125. package/dist/io/formats/pdf/export.js +1 -1
  126. package/dist/io/formats/pdf/export.js.map +1 -1
  127. package/dist/io/formats/svg/defs.d.ts.map +1 -1
  128. package/dist/io/formats/svg/defs.js +3 -1
  129. package/dist/io/formats/svg/defs.js.map +1 -1
  130. package/dist/io/formats/svg/export.d.ts.map +1 -1
  131. package/dist/io/formats/svg/export.js +52 -16
  132. package/dist/io/formats/svg/export.js.map +1 -1
  133. package/dist/layout/apply.d.ts.map +1 -1
  134. package/dist/layout/apply.js +50 -12
  135. package/dist/layout/apply.js.map +1 -1
  136. package/dist/layout/derived.d.ts +7 -1
  137. package/dist/layout/derived.d.ts.map +1 -1
  138. package/dist/layout/derived.js +11 -1
  139. package/dist/layout/derived.js.map +1 -1
  140. package/dist/layout/grid.d.ts +7 -2
  141. package/dist/layout/grid.d.ts.map +1 -1
  142. package/dist/layout/grid.js +27 -9
  143. package/dist/layout/grid.js.map +1 -1
  144. package/dist/layout/mutations.d.ts +1 -0
  145. package/dist/layout/mutations.d.ts.map +1 -1
  146. package/dist/layout/mutations.js +10 -0
  147. package/dist/layout/mutations.js.map +1 -1
  148. package/dist/layout/strokes.d.ts +9 -1
  149. package/dist/layout/strokes.d.ts.map +1 -1
  150. package/dist/layout/strokes.js +12 -1
  151. package/dist/layout/strokes.js.map +1 -1
  152. package/dist/layout/yoga-helpers.d.ts +3 -1
  153. package/dist/layout/yoga-helpers.d.ts.map +1 -1
  154. package/dist/layout/yoga-helpers.js +16 -2
  155. package/dist/layout/yoga-helpers.js.map +1 -1
  156. package/dist/layout.d.ts.map +1 -1
  157. package/dist/layout.js +71 -38
  158. package/dist/layout.js.map +1 -1
  159. package/dist/lint/linter.d.ts +1 -0
  160. package/dist/lint/linter.d.ts.map +1 -1
  161. package/dist/lint/linter.js +13 -1
  162. package/dist/lint/linter.js.map +1 -1
  163. package/dist/lint/rules/effect-style-required.js +1 -0
  164. package/dist/lint/rules/effect-style-required.js.map +1 -1
  165. package/dist/lint/rules/text-style-required.js +1 -0
  166. package/dist/lint/rules/text-style-required.js.map +1 -1
  167. package/dist/lint/types.d.ts +7 -0
  168. package/dist/lint/types.d.ts.map +1 -1
  169. package/dist/rpc/commands.d.ts +2 -2
  170. package/dist/rpc/index.d.ts +2 -2
  171. package/dist/rpc/read-commands.d.ts +18 -8
  172. package/dist/rpc/read-commands.d.ts.map +1 -1
  173. package/dist/rpc/read-commands.js +18 -9
  174. package/dist/rpc/read-commands.js.map +1 -1
  175. package/dist/text/font/faces.d.ts +12 -1
  176. package/dist/text/font/faces.d.ts.map +1 -1
  177. package/dist/text/font/faces.js +15 -1
  178. package/dist/text/font/faces.js.map +1 -1
  179. package/dist/text/font/manager.d.ts.map +1 -1
  180. package/dist/text/font/manager.js +6 -11
  181. package/dist/text/font/manager.js.map +1 -1
  182. package/dist/text/index.d.ts +2 -2
  183. package/dist/text/index.js +2 -2
  184. package/dist/tools/modify/layout.d.ts.map +1 -1
  185. package/dist/tools/modify/layout.js +1 -0
  186. package/dist/tools/modify/layout.js.map +1 -1
  187. package/dist/tools/read/nodes.d.ts.map +1 -1
  188. package/dist/tools/read/nodes.js.map +1 -1
  189. package/dist/tools/structure/batch.d.ts.map +1 -1
  190. package/dist/tools/structure/batch.js +14 -0
  191. package/dist/tools/structure/batch.js.map +1 -1
  192. package/dist/tools/vector/path.d.ts.map +1 -1
  193. package/dist/tools/vector/path.js +39 -7
  194. package/dist/tools/vector/path.js.map +1 -1
  195. package/dist/xpath.d.ts +3 -0
  196. package/dist/xpath.d.ts.map +1 -1
  197. package/dist/xpath.js +3 -1
  198. package/dist/xpath.js.map +1 -1
  199. package/package.json +9 -9
  200. package/src/canvas/overlays/index.ts +0 -1
  201. package/src/canvas/overlays/selection.ts +1 -12
  202. package/src/canvas/renderer/fonts.ts +2 -0
  203. package/src/canvas/renderer/methods.ts +0 -9
  204. package/src/canvas/renderer/overlay-pass.ts +0 -1
  205. package/src/canvas/renderer/pipeline.ts +0 -1
  206. package/src/canvas/renderer/types.ts +0 -1
  207. package/src/canvas/renderer.ts +0 -1
  208. package/src/design-jsx/index.ts +1 -1
  209. package/src/design-jsx/renderer.ts +1 -1
  210. package/src/editor/clipboard/snapshot.ts +21 -4
  211. package/src/editor/clipboard.ts +4 -14
  212. package/src/editor/color-space.ts +47 -38
  213. package/src/editor/create.ts +14 -2
  214. package/src/editor/history/edit.ts +5 -1
  215. package/src/editor/nudge.ts +1 -1
  216. package/src/editor/read-only/actions.ts +2 -0
  217. package/src/editor/selection/container.ts +24 -1
  218. package/src/editor/selection/navigation.ts +8 -6
  219. package/src/editor/shapes/with-text.ts +2 -5
  220. package/src/editor/structure/auto-layout-wrap.ts +1 -1
  221. package/src/editor/structure/boolean.ts +1 -3
  222. package/src/editor/structure/group/bounds.ts +1 -5
  223. package/src/editor/structure/outline-stroke.ts +3 -3
  224. package/src/editor/structure/reorder.ts +8 -14
  225. package/src/editor/structure/state.ts +1 -1
  226. package/src/editor/structure.ts +5 -8
  227. package/src/editor/types.ts +4 -1
  228. package/src/editor/variables.ts +74 -2
  229. package/src/figma-api/accessors/text/index.ts +10 -1
  230. package/src/figma-api/fonts/loaded.ts +9 -0
  231. package/src/figma-api/index.ts +25 -0
  232. package/src/figma-api/serialization.ts +2 -1
  233. package/src/figma-api/shape-details.ts +50 -0
  234. package/src/figma-api/shape-with-text.ts +2 -5
  235. package/src/figma-api/surface.ts +1 -0
  236. package/src/global.d.ts +5 -0
  237. package/src/icons/svg.ts +3 -1
  238. package/src/io/formats/drawio/index.ts +3 -1
  239. package/src/io/formats/html/layers.ts +11 -2
  240. package/src/io/formats/pdf/dom.ts +10 -18
  241. package/src/io/formats/pdf/export.ts +1 -1
  242. package/src/io/formats/svg/defs.ts +10 -1
  243. package/src/io/formats/svg/export.ts +94 -19
  244. package/src/layout/apply.ts +87 -21
  245. package/src/layout/derived.ts +14 -0
  246. package/src/layout/grid.ts +33 -9
  247. package/src/layout/mutations.ts +13 -1
  248. package/src/layout/strokes.ts +12 -0
  249. package/src/layout/yoga-helpers.ts +18 -1
  250. package/src/layout.ts +94 -49
  251. package/src/lint/linter.ts +17 -2
  252. package/src/lint/rules/effect-style-required.ts +2 -0
  253. package/src/lint/rules/text-style-required.ts +2 -0
  254. package/src/lint/types.ts +8 -0
  255. package/src/rpc/index.ts +1 -0
  256. package/src/rpc/read-commands.ts +40 -23
  257. package/src/text/font/faces.ts +22 -0
  258. package/src/text/font/manager.ts +9 -11
  259. package/src/tools/modify/layout.ts +5 -0
  260. package/src/tools/read/nodes.ts +0 -1
  261. package/src/tools/structure/batch.ts +22 -0
  262. package/src/tools/vector/path.ts +30 -6
  263. package/src/xpath.ts +6 -1
@@ -1,5 +1,5 @@
1
1
  import type { SceneGraph, SceneNode } from '@pairdraw/scene-graph'
2
- import { createShapeWithText, shapeWithTextFrameFields } from '@pairdraw/scene-graph/generated'
2
+ import { createShapeWithText, shapeWithTextKindFields } from '@pairdraw/scene-graph/generated'
3
3
  import {
4
4
  isShapeWithTextType,
5
5
  SHAPE_WITH_TEXT_TYPES,
@@ -57,10 +57,7 @@ export function installShapeWithTextAccessors(
57
57
  const kind = checkedShapeType(node, value)
58
58
  prepareFollowedFieldEdit(graph(this, internals), node, 'shapeType', 'shape-type')
59
59
  // The new kind lays the label out in its own area, as FigJam's shape switcher does.
60
- updateNode(this, internals, {
61
- shapeWithTextType: kind,
62
- ...shapeWithTextFrameFields(kind, node.width, node.height)
63
- })
60
+ updateNode(this, internals, shapeWithTextKindFields(node, kind))
64
61
  }
65
62
  }
66
63
  })
@@ -30,6 +30,7 @@ export const FIGMA_API_INCOMPLETE_MEMBERS = [
30
30
  'getNodeByIdAsync',
31
31
  'mixed',
32
32
  'root',
33
+ 'ui',
33
34
  'variables',
34
35
  'viewport'
35
36
  ] as const
package/src/global.d.ts CHANGED
@@ -20,3 +20,8 @@ declare module 'opencc-js/dict/TSCharacters' {
20
20
  const entries: string
21
21
  export default entries
22
22
  }
23
+
24
+ /** svg2pdf.js declares types for its package root only; its ES build is the same module. */
25
+ declare module 'svg2pdf.js/dist/svg2pdf.es.min.js' {
26
+ export * from 'svg2pdf.js'
27
+ }
package/src/icons/svg.ts CHANGED
@@ -391,7 +391,9 @@ export function scalePathInfos(
391
391
  if (scaleX !== 1 || scaleY !== 1) transformedPath = transformedPath.scale(scaleX, scaleY)
392
392
  if (offsetX !== 0 || offsetY !== 0)
393
393
  transformedPath = transformedPath.translate(offsetX, offsetY)
394
- const scaledD = transformedPath.round(2).toString()
394
+ // Arcs become curves before rounding, as the path parser would turn them anyway: rounding an
395
+ // arc's own end can put it on its start, and a degenerate arc draws nothing.
396
+ const scaledD = transformedPath.abs().unshort().unarc().round(2).toString()
395
397
  const fill = normalizeSVGPaint(path.fill)
396
398
  const stroke = normalizeSVGPaint(path.stroke)
397
399
  return {
@@ -119,7 +119,9 @@ class PageBuilder {
119
119
  for (const extra of bindEdge(graph, pending, layerOf)) this.edgeText(pending, extra)
120
120
  }
121
121
  // Groups fit around what they hold, the innermost first.
122
- for (const id of this.groups.toReversed()) fitEnclosingGroups(graph, [id])
122
+ // One pass fits every group and its ancestors once; fitting per group walks the chain above
123
+ // that group again for each of them, which on a deep draw.io drawing is most of the import.
124
+ fitEnclosingGroups(graph, this.groups)
123
125
  }
124
126
 
125
127
  private vertex(cell: DrawIOCell, parentId: string, hasChildren: boolean): SceneNode {
@@ -11,6 +11,12 @@ export interface StyleHTMLOptions {
11
11
  tailwind?: boolean
12
12
  }
13
13
 
14
+ export interface SceneGraphFromStyledHTMLOptions {
15
+ pageName?: string
16
+ /** The family a CSS stack should draw; null keeps its first family. See the dom-css option. */
17
+ resolveFontFamily?: (value: string) => string | null
18
+ }
19
+
14
20
  /**
15
21
  * Styles HTML with the CSS runtime a host has: the browser's in the app, the headless runtime
16
22
  * in the CLI. Hosts hand it to tools, which build the layers with `sceneGraphFromStyledHTML`.
@@ -44,9 +50,12 @@ export function htmlClassNames(html: string): string[] {
44
50
  */
45
51
  export function sceneGraphFromStyledHTML(
46
52
  styled: DesignDocument,
47
- options: { pageName?: string } = {}
53
+ options: SceneGraphFromStyledHTMLOptions = {}
48
54
  ): SceneGraph {
49
- const graph = designDocumentToSceneGraph(styled, { pageName: options.pageName })
55
+ const graph = designDocumentToSceneGraph(styled, {
56
+ pageName: options.pageName,
57
+ resolveFontFamily: options.resolveFontFamily
58
+ })
50
59
  layoutAuthoredNodes(
51
60
  graph,
52
61
  graph.getPages().map((page) => page.id)
@@ -201,23 +201,15 @@ async function withHappyDOM<T>(draw: (parse: SVGPageParser) => Promise<T>): Prom
201
201
  * The two globals exist only while the import evaluates, and the function the module made keeps
202
202
  * the jsPDF it read, so the rest of the export needs nothing from them.
203
203
  */
204
- export async function loadSVG2PDF(jsPDF: unknown): Promise<typeof svg2pdf> {
205
- const scope = globalThis as { jspdf?: unknown; svg2pdf?: { svg2pdf?: typeof svg2pdf } }
206
- const previousJSPDF = Object.getOwnPropertyDescriptor(globalThis, 'jspdf')
207
- const previousSVG2PDF = Object.getOwnPropertyDescriptor(globalThis, 'svg2pdf')
208
- const set = (name: string, value: unknown): void => {
209
- Object.defineProperty(globalThis, name, { value, configurable: true, writable: true })
210
- }
211
- set('jspdf', { jsPDF })
212
- try {
213
- const module = (await import('svg2pdf.js')) as { svg2pdf?: typeof svg2pdf }
214
- const draw = module.svg2pdf ?? scope.svg2pdf?.svg2pdf
215
- if (!draw) throw new Error('svg2pdf.js exported no svg2pdf function')
216
- return draw
217
- } finally {
218
- if (previousJSPDF) Object.defineProperty(globalThis, 'jspdf', previousJSPDF)
219
- else delete scope.jspdf
220
- if (previousSVG2PDF) Object.defineProperty(globalThis, 'svg2pdf', previousSVG2PDF)
221
- else delete scope.svg2pdf
204
+ /**
205
+ * svg2pdf.js's `main` is a UMD build in a `"type": "module"` package with no `exports`, so Node
206
+ * loads it as ESM and it throws for want of a global jsPDF. Its ES build, the `module` file
207
+ * bundlers and Bun already pick, loads everywhere.
208
+ */
209
+ export async function loadSVG2PDF(): Promise<typeof svg2pdf> {
210
+ const module = (await import('svg2pdf.js/dist/svg2pdf.es.min.js')) as {
211
+ svg2pdf?: typeof svg2pdf
222
212
  }
213
+ if (!module.svg2pdf) throw new Error('svg2pdf.js exported no svg2pdf function')
214
+ return module.svg2pdf
223
215
  }
@@ -98,7 +98,7 @@ async function renderPages(
98
98
  const first = prepared[0]
99
99
 
100
100
  const { jsPDF } = await import('jspdf')
101
- const svg2pdf = await loadSVG2PDF(jsPDF)
101
+ const svg2pdf = await loadSVG2PDF()
102
102
 
103
103
  const doc = new jsPDF({
104
104
  orientation: orientationOf(first),
@@ -204,7 +204,10 @@ function createImagePattern(
204
204
  id,
205
205
  patternUnits: 'objectBoundingBox',
206
206
  width: 1,
207
- height: 1
207
+ height: 1,
208
+ patternTransform: fill.imageRotation
209
+ ? `rotate(${round(fill.imageRotation)} 0.5 0.5)`
210
+ : undefined
208
211
  },
209
212
  svg('image', {
210
213
  href: `data:${mime};base64,${base64}`,
@@ -292,6 +295,12 @@ export function resolveFill(fill: Fill, node: SceneNode, ctx: SVGExportContext):
292
295
  return formatSolidColor(fill.color, fill.opacity, ctx.colorSpace)
293
296
  }
294
297
 
298
+ // The canvas draws a pattern, noise or custom paint as its own colour when the format has no
299
+ // element for it, so the export must not drop the layer.
300
+ if (fill.type === 'PATTERN' || fill.type === 'NOISE' || fill.type === 'CUSTOM') {
301
+ return formatSolidColor(fill.color, fill.opacity, ctx.colorSpace)
302
+ }
303
+
295
304
  if (fill.type.startsWith('GRADIENT')) {
296
305
  const grad = createGradientDef(fill, node, ctx)
297
306
  if (grad) {
@@ -56,6 +56,46 @@ function networkElements(
56
56
  return [...regions, ...openPathElements(node, network, common, looseSegments)]
57
57
  }
58
58
 
59
+ /** Paints one path's fill stack; a blended fill draws its stroke as a separate path. */
60
+ function vectorFillElements(
61
+ attrs: Record<string, string | number | undefined>,
62
+ visibleFills: Fill[],
63
+ strokeAttrs: Record<string, string | number | undefined>,
64
+ common: Record<string, string | number | undefined>,
65
+ node: SceneNode,
66
+ ctx: SVGExportContext
67
+ ): SVGNode[] {
68
+ const elements: SVGNode[] = []
69
+ for (const [index, fill] of visibleFills.entries()) {
70
+ const fillAttr = resolveFill(fill, node, ctx)
71
+ if (!fillAttr) continue
72
+ const style = paintBlendStyle(fill)
73
+ const last = index === visibleFills.length - 1
74
+ const splitStroke = style !== undefined && last && common.stroke !== 'none'
75
+ const stroke = last && !splitStroke ? common.stroke : 'none'
76
+ elements.push(
77
+ svg('path', {
78
+ ...attrs,
79
+ fill: fillAttr,
80
+ 'fill-opacity': solidFillOpacity(fill, ctx.colorSpace),
81
+ style,
82
+ stroke
83
+ })
84
+ )
85
+ if (splitStroke) {
86
+ elements.push(
87
+ svg('path', {
88
+ d: attrs.d,
89
+ 'fill-rule': attrs['fill-rule'],
90
+ fill: 'none',
91
+ ...strokeAttrs
92
+ })
93
+ )
94
+ }
95
+ }
96
+ return elements
97
+ }
98
+
59
99
  function vectorShapeElements(
60
100
  node: SceneNode,
61
101
  common: Record<string, string | number | undefined>,
@@ -83,18 +123,7 @@ function vectorShapeElements(
83
123
  elements.push(svg('path', { ...attrs, fill: 'none' }))
84
124
  continue
85
125
  }
86
- for (const [index, fill] of visibleFills.entries()) {
87
- const fillAttr = resolveFill(fill, node, ctx)
88
- if (!fillAttr) continue
89
- elements.push(
90
- svg('path', {
91
- ...attrs,
92
- fill: fillAttr,
93
- 'fill-opacity': solidFillOpacity(fill, ctx.colorSpace),
94
- stroke: index === visibleFills.length - 1 ? common.stroke : 'none'
95
- })
96
- )
97
- }
126
+ elements.push(...vectorFillElements(attrs, visibleFills, strokeAttrs, common, node, ctx))
98
127
  }
99
128
  } else if (node.vectorNetwork) {
100
129
  elements.push(...networkElements(node, node.vectorNetwork, common))
@@ -286,16 +315,19 @@ function buildGroupAttrs(
286
315
 
287
316
  function buildSVGStrokeAttrs(
288
317
  visibleStrokes: Stroke[],
289
- colorSpace: 'srgb' | 'display-p3',
290
- nodeCap: SceneNode['strokeCap']
318
+ node: SceneNode,
319
+ ctx: SVGExportContext
291
320
  ): Record<string, string | number | undefined> {
292
321
  if (visibleStrokes.length === 0) return {}
293
322
  const stroke = visibleStrokes[0]
294
- const cap = stroke.cap ?? nodeCap
323
+ const cap = stroke.cap ?? node.strokeCap
295
324
  const attrs: Record<string, string | number | undefined> = {
296
325
  // Opacity 1 keeps the color opaque. Display P3 must not also keep color alpha here, or a
297
326
  // viewer multiplies it by stroke-opacity; the canvas paints stroke.opacity alone.
298
- stroke: formatSolidColor(stroke.color, 1, colorSpace),
327
+ stroke:
328
+ stroke.type === 'SOLID'
329
+ ? formatSolidColor(stroke.color, 1, ctx.colorSpace)
330
+ : (resolveFill(stroke, node, ctx) ?? formatSolidColor(stroke.color, 1, ctx.colorSpace)),
299
331
  'stroke-width': round(stroke.weight)
300
332
  }
301
333
  if (stroke.opacity < 1) attrs['stroke-opacity'] = round(stroke.opacity)
@@ -309,6 +341,41 @@ function buildSVGStrokeAttrs(
309
341
  return attrs
310
342
  }
311
343
 
344
+ /** The blend CSS a paint adds, when the canvas would not composite it source-over. */
345
+ function paintBlendStyle(fill: Fill | undefined): string | undefined {
346
+ const blend = fill?.blendMode ? SVG_BLEND_MODE[fill.blendMode] : undefined
347
+ return blend && blend !== 'normal' ? `mix-blend-mode: ${blend}` : undefined
348
+ }
349
+
350
+ /** Adds a paint's blend mode to the elements that draw it. */
351
+ function withPaintBlend(elements: SVGNode[], fill: Fill | undefined): SVGNode[] {
352
+ const style = paintBlendStyle(fill)
353
+ if (style) for (const element of elements) element.attrs.style = style
354
+ return elements
355
+ }
356
+
357
+ /**
358
+ * Draws the fill and the stroke as separate elements when the fill blends, so the stroke keeps the
359
+ * source-over canvas gives it instead of inheriting the fill's blend mode.
360
+ */
361
+ function shapeWithPaint(
362
+ node: SceneNode,
363
+ fillAttr: string | null,
364
+ fillOpacity: number | undefined,
365
+ strokeAttrs: Record<string, string | number | undefined>,
366
+ fill: Fill | undefined,
367
+ ctx: SVGExportContext
368
+ ): SVGNode[] {
369
+ const blends = paintBlendStyle(fill) !== undefined
370
+ if (!blends || !strokeAttrs.stroke) {
371
+ return withPaintBlend(nodeShapeElements(node, fillAttr, fillOpacity, strokeAttrs, ctx), fill)
372
+ }
373
+ return [
374
+ ...withPaintBlend(nodeShapeElements(node, fillAttr, fillOpacity, {}, ctx), fill),
375
+ ...nodeShapeElements(node, null, undefined, strokeAttrs, ctx)
376
+ ]
377
+ }
378
+
312
379
  function hasPathLevelFills(node: SceneNode): boolean {
313
380
  return node.type === 'VECTOR' && node.fillGeometry.some((geometry) => geometry.fills?.length)
314
381
  }
@@ -367,11 +434,12 @@ function fillAndStrokeElements(
367
434
  const ref = resolveFill(fill, node, ctx)
368
435
  if (ref) {
369
436
  elements.push(
370
- ...nodeShapeElements(
437
+ ...shapeWithPaint(
371
438
  node,
372
439
  ref,
373
440
  solidFillOpacity(fill, ctx.colorSpace),
374
441
  fill === visibleFills[visibleFills.length - 1] ? strokeAttrs : {},
442
+ fill,
375
443
  ctx
376
444
  )
377
445
  )
@@ -382,7 +450,14 @@ function fillAndStrokeElements(
382
450
 
383
451
  const hasFillOrStroke = fillAttr || visibleStrokeCount > 0
384
452
  if (hasFillOrStroke && !isGroupLike(node)) {
385
- return nodeShapeElements(node, fillAttr, fillOpacity, strokeAttrs, ctx)
453
+ return shapeWithPaint(
454
+ node,
455
+ fillAttr,
456
+ fillOpacity,
457
+ strokeAttrs,
458
+ visibleFills.length === 1 ? visibleFills[0] : undefined,
459
+ ctx
460
+ )
386
461
  }
387
462
 
388
463
  return []
@@ -428,7 +503,7 @@ function renderNode(node: SceneNode, ctx: SVGExportContext): SVGNode | null {
428
503
  // `currentColor` has no alpha. Display P3 normally bakes solid opacity into the color, which a
429
504
  // tint throws away, so a tinted solid keeps `fill-opacity` in every color space.
430
505
  const fillOpacity = ownFill ? solidFillOpacity(ownFill, ctx.colorSpace, tintedFill) : fill.opacity
431
- const ownStrokes = buildSVGStrokeAttrs(visibleStrokes, ctx.colorSpace, node.strokeCap)
506
+ const ownStrokes = buildSVGStrokeAttrs(visibleStrokes, node, ctx)
432
507
  const strokeAttrs =
433
508
  ownStrokes.stroke && tint.includes('stroke')
434
509
  ? { ...ownStrokes, stroke: CURRENT_COLOR }
@@ -5,8 +5,9 @@ import { enforcedAspectRatio } from '@pairdraw/scene-graph/layout'
5
5
  import type { Vector } from '@pairdraw/scene-graph/primitives'
6
6
  import { layoutConstrainedChildRect, shapeGeometryChanges } from '@pairdraw/scene-graph/resize'
7
7
 
8
- import { keepsSavedLayout, usesDetachedDerivedLayout } from './derived'
8
+ import { keepsSavedFrameSize, keepsSavedLayout, usesDetachedDerivedLayout } from './derived'
9
9
  import { laidOutFlowBox } from './flow-edges'
10
+ import { contentInsetsSize } from './strokes'
10
11
 
11
12
  export type ComputeLayoutFn = (graph: SceneGraph, frameId: string) => void
12
13
 
@@ -47,7 +48,10 @@ function writeLayoutGeometry(
47
48
  const changes: LayoutGeometry = {}
48
49
  for (const key of ['x', 'y', 'width', 'height'] as const) {
49
50
  const value = geometry[key]
50
- if (value !== undefined && value !== node[key]) changes[key] = value
51
+ // A container whose size no arithmetic can use makes Yoga answer NaN for a child's place
52
+ // (BASELINE alignment in a wrap is one); the layer stays where it was.
53
+ if (value === undefined || !Number.isFinite(value) || value === node[key]) continue
54
+ changes[key] = value
51
55
  }
52
56
  const resized = changes.width !== undefined || changes.height !== undefined
53
57
  const outlines = resized
@@ -150,6 +154,74 @@ function hugStart(frame: SceneNode): { width: number; height: number } | null {
150
154
  }
151
155
  }
152
156
 
157
+ /**
158
+ * The smallest size a layout frame holds: what Yoga computed for it, or the room its padding and
159
+ * included inside strokes take, which Yoga's fixed size does not count. A saved frame nobody
160
+ * edited holds exactly what Yoga computed, so opening a file never grows it.
161
+ */
162
+ function heldFrameSize(
163
+ frame: SceneNode,
164
+ yogaNode: YogaNode,
165
+ holdsSavedSize: boolean
166
+ ): { width: number; height: number } {
167
+ const insets = holdsSavedSize ? null : contentInsetsSize(frame)
168
+ return {
169
+ width: Math.max(yogaNode.getComputedWidth(), insets?.width ?? 0),
170
+ height: Math.max(yogaNode.getComputedHeight(), insets?.height ?? 0)
171
+ }
172
+ }
173
+
174
+ /** A grid's size: automatic rows take their height, and a hugged axis takes what Yoga computed. */
175
+ function applyGridFrameSize(
176
+ graph: SceneGraph,
177
+ frame: SceneNode,
178
+ yogaNode: YogaNode,
179
+ computeLayout: ComputeLayoutFn
180
+ ): void {
181
+ const updates: LayoutGeometry = {}
182
+ const holds = keepsSavedFrameSize(graph, frame)
183
+ const held = heldFrameSize(frame, yogaNode, holds)
184
+ if (frame.primaryAxisSizing === 'HUG') updates.width = held.width
185
+ else if (!holds && held.width > frame.width) updates.width = held.width
186
+ const automaticRows = frame.gridTemplateRows.length === 0
187
+ if (automaticRows || frame.counterAxisSizing === 'HUG') updates.height = held.height
188
+ else if (!holds && held.height > frame.height) updates.height = held.height
189
+ writeLayoutGeometry(graph, frame.id, updates, computeLayout)
190
+ }
191
+
192
+ /** The sizes a flex frame takes: a fixed one grows to its padding, and a hugged one hugs. */
193
+ function flexFrameSizeUpdates(
194
+ frame: SceneNode,
195
+ held: { width: number; height: number },
196
+ computed: (axis: 'width' | 'height') => number,
197
+ derived: Partial<Record<'width' | 'height', number>> | null,
198
+ keepsSavedSize: (axis: 'width' | 'height') => boolean,
199
+ holdsSavedSize: boolean
200
+ ): LayoutGeometry {
201
+ const primary = frame.layoutMode === 'HORIZONTAL' ? 'width' : 'height'
202
+ const counter = primary === 'width' ? 'height' : 'width'
203
+ const updates: LayoutGeometry = {}
204
+ // The padding floor is an edit: a saved frame nobody touched keeps the size it has.
205
+ if (frame.primaryAxisSizing === 'FIXED' && !holdsSavedSize && held[primary] > frame[primary]) {
206
+ updates[primary] = held[primary]
207
+ }
208
+ if (frame.counterAxisSizing === 'FIXED' && !holdsSavedSize && held[counter] > frame[counter]) {
209
+ updates[counter] = held[counter]
210
+ }
211
+ if (frame.primaryAxisSizing === 'HUG') {
212
+ const floor = holdsSavedSize ? 0 : held[primary]
213
+ updates[primary] = Math.max(derived?.[primary] ?? computed(primary), floor)
214
+ }
215
+ if (frame.counterAxisSizing === 'HUG') {
216
+ const floor = holdsSavedSize ? 0 : held[counter]
217
+ updates[counter] = Math.max(
218
+ keepsSavedSize(counter) ? frame[counter] : (derived?.[counter] ?? computed(counter)),
219
+ floor
220
+ )
221
+ }
222
+ return updates
223
+ }
224
+
153
225
  function applyFrameSize(
154
226
  graph: SceneGraph,
155
227
  frame: SceneNode,
@@ -158,31 +230,25 @@ function applyFrameSize(
158
230
  computeLayout: ComputeLayoutFn
159
231
  ): void {
160
232
  if (frame.layoutMode === 'GRID') {
161
- if (frame.gridTemplateRows.length === 0) {
162
- writeLayoutGeometry(graph, frame.id, { height: yogaNode.getComputedHeight() }, computeLayout)
163
- }
233
+ applyGridFrameSize(graph, frame, yogaNode, computeLayout)
164
234
  return
165
235
  }
166
236
 
167
237
  const before = hugStart(frame)
168
- if (!before) return
169
-
170
- const updates: LayoutGeometry = {}
171
- const derived = keepsSaved ? frame.derivedLayout : null
172
- const primary = frame.layoutMode === 'HORIZONTAL' ? 'width' : 'height'
173
- const counter = primary === 'width' ? 'height' : 'width'
174
238
  const computed = (axis: 'width' | 'height') =>
175
239
  axis === 'width' ? yogaNode.getComputedWidth() : yogaNode.getComputedHeight()
176
- if (frame.primaryAxisSizing === 'HUG') updates[primary] = derived?.[primary] ?? computed(primary)
177
- if (frame.counterAxisSizing === 'HUG') {
178
- updates[counter] =
179
- keepsSaved && preservesImportedHugCrossSize(graph, frame, counter)
180
- ? frame[counter]
181
- : (derived?.[counter] ?? computed(counter))
182
- }
183
-
184
- writeLayoutGeometry(graph, frame.id, updates, computeLayout, before)
185
- huggedSizes.set(frame, { width: frame.width, height: frame.height })
240
+ const holds = keepsSavedFrameSize(graph, frame)
241
+ const updates = flexFrameSizeUpdates(
242
+ frame,
243
+ heldFrameSize(frame, yogaNode, holds),
244
+ computed,
245
+ keepsSaved ? frame.derivedLayout : null,
246
+ (axis) => keepsSaved && preservesImportedHugCrossSize(graph, frame, axis),
247
+ holds
248
+ )
249
+ if (Object.keys(updates).length === 0) return
250
+ writeLayoutGeometry(graph, frame.id, updates, computeLayout, before ?? undefined)
251
+ if (before) huggedSizes.set(frame, { width: frame.width, height: frame.height })
186
252
  }
187
253
 
188
254
  function computedChildPosition(
@@ -1,5 +1,6 @@
1
1
  import type { SceneGraph, SceneNode } from '@pairdraw/scene-graph'
2
2
  import { LAYOUT_AFFECTING_KEYS } from '@pairdraw/scene-graph/fields'
3
+ import { takesOwnSize } from '@pairdraw/scene-graph/instances'
3
4
 
4
5
  /** Edits that make saved geometry stale: layout fields, visibility, and text laid out again. */
5
6
  const LAYOUT_EDIT_FIELDS: ReadonlySet<string> = new Set([
@@ -95,3 +96,16 @@ export function usesDetachedDerivedLayout(graph: SceneGraph, child: SceneNode):
95
96
  keepsSavedLayout(graph, child)
96
97
  )
97
98
  }
99
+
100
+ /**
101
+ * Whether layout holds a frame's saved size exactly: a frame read from a file whose flow and
102
+ * layout are still untouched by the user or a tool. Growing it to its padding belongs to an
103
+ * edit, so opening or re-laying out a file leaves the saved size alone.
104
+ */
105
+ export function keepsSavedFrameSize(graph: SceneGraph, frame: SceneNode): boolean {
106
+ // An instance shows the size its main or an override gives it, as do the layers inside it; their
107
+ // own padding is not an edit this layout may grow them from.
108
+ if (frame.type === 'INSTANCE' || !takesOwnSize(graph, frame)) return true
109
+ const readFromFile = frame.source.format === 'fig' || frame.derivedLayout != null
110
+ return readFromFile && keepsSavedLayout(graph, frame)
111
+ }
@@ -1,35 +1,59 @@
1
1
  import { Direction, Display, Gutter, type Node as YogaNode } from 'yoga-layout'
2
2
 
3
- import type { SceneGraph, SceneNode } from '@pairdraw/scene-graph'
3
+ import type { GridTrack, SceneGraph, SceneNode } from '@pairdraw/scene-graph'
4
4
  import { layoutSizing } from '@pairdraw/scene-graph/layout'
5
5
  import { resolveNodeLayoutDirection } from '@pairdraw/scene-graph/text/direction'
6
6
 
7
- import { applyContentInsets, applyStrokeMargins } from './strokes'
8
- import { configureAbsoluteChild, createYogaNode, mapGridTrack } from './yoga-helpers'
7
+ import { ownAxisSizing } from './axis-sizing'
8
+ import { keepsSavedFrameSize } from './derived'
9
+ import { applyContentInsets, applyStrokeMargins, contentInsetsSize } from './strokes'
10
+ import {
11
+ applyMinMaxConstraints,
12
+ configureAbsoluteChild,
13
+ createYogaNode,
14
+ mapGridTrack
15
+ } from './yoga-helpers'
16
+
17
+ /**
18
+ * A grid frame that hugs an axis has no space to share out, so its fractional tracks take their
19
+ * content's size there and the frame keeps the smallest box around them, as Figma hugs a grid.
20
+ */
21
+ export function gridTracksForAxis(node: SceneNode, axis: 'width' | 'height'): GridTrack[] {
22
+ const tracks = axis === 'width' ? node.gridTemplateColumns : node.gridTemplateRows
23
+ if (ownAxisSizing(node, axis) !== 'HUG') return tracks
24
+ return tracks.map((track) => (track.sizing === 'FR' ? { sizing: 'AUTO', value: 0 } : track))
25
+ }
9
26
 
10
27
  function configureAsGrid(
28
+ graph: SceneGraph,
11
29
  yogaNode: YogaNode,
12
30
  node: SceneNode,
13
31
  direction: Exclude<SceneNode['layoutDirection'], 'AUTO'>
14
32
  ): void {
15
33
  yogaNode.setDisplay(Display.Grid)
16
34
  yogaNode.setDirection(direction === 'RTL' ? Direction.RTL : Direction.LTR)
17
- yogaNode.setWidth(node.width)
18
- if (node.gridTemplateRows.length > 0 || node.height > 0) {
19
- yogaNode.setHeight(node.height)
35
+ const insets = keepsSavedFrameSize(graph, node) ? null : contentInsetsSize(node)
36
+ if (ownAxisSizing(node, 'width') !== 'HUG') {
37
+ yogaNode.setWidth(insets ? Math.max(node.width, insets.width) : node.width)
38
+ }
39
+ if (ownAxisSizing(node, 'height') !== 'HUG') {
40
+ if (node.gridTemplateRows.length > 0 || node.height > 0) {
41
+ yogaNode.setHeight(insets ? Math.max(node.height, insets.height) : node.height)
42
+ }
20
43
  }
21
44
 
22
45
  if (node.gridTemplateColumns.length > 0) {
23
- yogaNode.setGridTemplateColumns(node.gridTemplateColumns.map(mapGridTrack))
46
+ yogaNode.setGridTemplateColumns(gridTracksForAxis(node, 'width').map(mapGridTrack))
24
47
  }
25
48
  if (node.gridTemplateRows.length > 0) {
26
- yogaNode.setGridTemplateRows(node.gridTemplateRows.map(mapGridTrack))
49
+ yogaNode.setGridTemplateRows(gridTracksForAxis(node, 'height').map(mapGridTrack))
27
50
  }
28
51
 
29
52
  yogaNode.setGap(Gutter.Column, node.gridColumnGap)
30
53
  yogaNode.setGap(Gutter.Row, node.gridRowGap)
31
54
 
32
55
  applyContentInsets(yogaNode, node)
56
+ applyMinMaxConstraints(yogaNode, node)
33
57
  }
34
58
 
35
59
  export function createGridChildNode(graph: SceneGraph, child: SceneNode): YogaNode {
@@ -70,7 +94,7 @@ export function buildGridTree(
70
94
  ): YogaNode {
71
95
  const root = createYogaNode()
72
96
  const direction = resolveNodeLayoutDirection(frame, inheritedDirection)
73
- configureAsGrid(root, frame, direction)
97
+ configureAsGrid(graph, root, frame, direction)
74
98
 
75
99
  const children = graph.getChildren(frame.id)
76
100
  for (const child of children) {
@@ -27,6 +27,18 @@ export function createLayoutRunner(getGraph: () => SceneGraph) {
27
27
  }
28
28
  }
29
29
 
30
+ /**
31
+ * Lays out the parents whose children were added, removed, reordered, shown, or hidden. Only an
32
+ * auto layout places its children, so other parents are skipped: laying out a page reaches every
33
+ * frame on it. Hugging auto layout ancestors still follow, as `runLayoutForNode` walks up.
34
+ */
35
+ function runLayoutForParents(parentIds: Iterable<string>) {
36
+ const graph = getGraph()
37
+ for (const id of uniq([...parentIds])) {
38
+ if (graph.getNode(id)?.layoutMode !== 'NONE') runLayoutForNode(id)
39
+ }
40
+ }
41
+
30
42
  function compactLayoutScope(impact: SceneMutationImpact): string[] {
31
43
  const graph = getGraph()
32
44
  const candidates = new Set(mutationLayoutScopeIds(impact).filter((id) => graph.getNode(id)))
@@ -87,5 +99,5 @@ export function createLayoutRunner(getGraph: () => SceneGraph) {
87
99
  })
88
100
  }
89
101
 
90
- return { runLayoutForNode, runLayoutForImpact, runMutationWithLayout }
102
+ return { runLayoutForNode, runLayoutForParents, runLayoutForImpact, runMutationWithLayout }
91
103
  }
@@ -73,6 +73,18 @@ export function applyContentInsets(yogaNode: YogaNode, node: SceneNode): void {
73
73
  applyStrokeBorders(yogaNode, node)
74
74
  }
75
75
 
76
+ /**
77
+ * The room a horizontal and vertical layout frame's padding and included inside strokes take,
78
+ * which is the smallest size it holds even when set smaller, as Figma grows such a frame.
79
+ */
80
+ export function contentInsetsSize(node: SceneNode): { width: number; height: number } {
81
+ const inside = node.strokesIncludedInLayout ? strokeExtent(node, 'inside') : null
82
+ return {
83
+ width: node.paddingLeft + node.paddingRight + (inside ? inside[1] + inside[3] : 0),
84
+ height: node.paddingTop + node.paddingBottom + (inside ? inside[0] + inside[2] : 0)
85
+ }
86
+ }
87
+
76
88
  /**
77
89
  * With strokes included in its parent's layout, the part of a child's stroke that falls outside
78
90
  * its box takes room beside it, so neighbours and a hugging parent clear the stroke.
@@ -13,7 +13,20 @@ import { enforcedAspectRatio, layoutSizingInParent } from '@pairdraw/scene-graph
13
13
  const yogaConfig = Yoga.Config.create()
14
14
  yogaConfig.setPointScaleFactor(0)
15
15
 
16
+ /**
17
+ * Yoga nodes built and not freed yet. Yoga's nodes live in WebAssembly memory the caller owns, so a
18
+ * pass that leaves its tree behind holds that memory and the nodes it holds; a test reads this to
19
+ * see one freed on every path.
20
+ */
21
+ let liveYogaNodes = 0
22
+
23
+ /** Yoga nodes created and not yet freed by `freeYogaTree`. */
24
+ export function liveYogaNodeCount(): number {
25
+ return liveYogaNodes
26
+ }
27
+
16
28
  export function createYogaNode(): YogaNode {
29
+ liveYogaNodes += 1
17
30
  return Yoga.Node.create(yogaConfig)
18
31
  }
19
32
 
@@ -100,7 +113,11 @@ export function freeYogaTree(node: YogaNode): void {
100
113
  for (let i = node.getChildCount() - 1; i >= 0; i--) {
101
114
  freeYogaTree(node.getChild(i))
102
115
  }
103
- if ('free' in node) (node as { free(): void }).free()
116
+ // Wasm-backed nodes carry `free`, which `yoga-layout`'s type leaves out.
117
+ if ('free' in node && typeof node.free === 'function') {
118
+ node.free()
119
+ liveYogaNodes -= 1
120
+ }
104
121
  }
105
122
 
106
123
  export function mapJustify(align: string): Justify {