@nodetool-ai/timeline 0.8.1 → 0.8.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 (138) hide show
  1. package/dist/.nodetool-build-stamp +1 -1
  2. package/dist/authoring.d.ts.map +1 -1
  3. package/dist/authoring.js +46 -6
  4. package/dist/authoring.js.map +1 -1
  5. package/dist/composition.d.ts +11 -1
  6. package/dist/composition.d.ts.map +1 -1
  7. package/dist/composition.js +58 -12
  8. package/dist/composition.js.map +1 -1
  9. package/dist/examples/node.d.ts +18 -0
  10. package/dist/examples/node.d.ts.map +1 -1
  11. package/dist/examples/node.js +26 -0
  12. package/dist/examples/node.js.map +1 -1
  13. package/dist/finish-storyboard.d.ts +36 -0
  14. package/dist/finish-storyboard.d.ts.map +1 -0
  15. package/dist/finish-storyboard.js +554 -0
  16. package/dist/finish-storyboard.js.map +1 -0
  17. package/dist/fonts/google-fonts-fetch.d.ts +53 -0
  18. package/dist/fonts/google-fonts-fetch.d.ts.map +1 -0
  19. package/dist/fonts/google-fonts-fetch.js +184 -0
  20. package/dist/fonts/google-fonts-fetch.js.map +1 -0
  21. package/dist/fonts/google-fonts-node.d.ts +32 -0
  22. package/dist/fonts/google-fonts-node.d.ts.map +1 -0
  23. package/dist/fonts/google-fonts-node.js +60 -0
  24. package/dist/fonts/google-fonts-node.js.map +1 -0
  25. package/dist/fonts/google-fonts.d.ts +105 -0
  26. package/dist/fonts/google-fonts.d.ts.map +1 -0
  27. package/dist/fonts/google-fonts.js +178 -0
  28. package/dist/fonts/google-fonts.js.map +1 -0
  29. package/dist/fonts/index.d.ts +1 -0
  30. package/dist/fonts/index.d.ts.map +1 -1
  31. package/dist/fonts/index.js +1 -0
  32. package/dist/fonts/index.js.map +1 -1
  33. package/dist/index.d.ts +5 -0
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +5 -0
  36. package/dist/index.js.map +1 -1
  37. package/dist/linked.d.ts +2 -1
  38. package/dist/linked.d.ts.map +1 -1
  39. package/dist/linked.js +18 -10
  40. package/dist/linked.js.map +1 -1
  41. package/dist/midi/engines/kits.d.ts +2 -0
  42. package/dist/midi/engines/kits.d.ts.map +1 -1
  43. package/dist/midi/engines/kits.js +146 -0
  44. package/dist/midi/engines/kits.js.map +1 -1
  45. package/dist/midi/index.d.ts +1 -0
  46. package/dist/midi/index.d.ts.map +1 -1
  47. package/dist/midi/index.js +1 -0
  48. package/dist/midi/index.js.map +1 -1
  49. package/dist/midi/instrument.d.ts.map +1 -1
  50. package/dist/midi/instrument.js +5 -0
  51. package/dist/midi/instrument.js.map +1 -1
  52. package/dist/midi/presets.d.ts.map +1 -1
  53. package/dist/midi/presets.js +251 -1
  54. package/dist/midi/presets.js.map +1 -1
  55. package/dist/midi/sampler.d.ts +15 -0
  56. package/dist/midi/sampler.d.ts.map +1 -0
  57. package/dist/midi/sampler.js +72 -0
  58. package/dist/midi/sampler.js.map +1 -0
  59. package/dist/midi/voice.d.ts +4 -1
  60. package/dist/midi/voice.d.ts.map +1 -1
  61. package/dist/midi/voice.js +8 -4
  62. package/dist/midi/voice.js.map +1 -1
  63. package/dist/ops/apply.d.ts +6 -1
  64. package/dist/ops/apply.d.ts.map +1 -1
  65. package/dist/ops/apply.js +843 -201
  66. package/dist/ops/apply.js.map +1 -1
  67. package/dist/ops/index.d.ts +3 -2
  68. package/dist/ops/index.d.ts.map +1 -1
  69. package/dist/ops/index.js +3 -2
  70. package/dist/ops/index.js.map +1 -1
  71. package/dist/ops/op.d.ts +15 -3
  72. package/dist/ops/op.d.ts.map +1 -1
  73. package/dist/ops/op.js +17 -8
  74. package/dist/ops/op.js.map +1 -1
  75. package/dist/ops/parse.d.ts +6 -0
  76. package/dist/ops/parse.d.ts.map +1 -0
  77. package/dist/ops/parse.js +390 -0
  78. package/dist/ops/parse.js.map +1 -0
  79. package/dist/ops/serialize.d.ts +9 -1
  80. package/dist/ops/serialize.d.ts.map +1 -1
  81. package/dist/ops/serialize.js +19 -2
  82. package/dist/ops/serialize.js.map +1 -1
  83. package/dist/ops/types.d.ts +30 -8
  84. package/dist/ops/types.d.ts.map +1 -1
  85. package/dist/ops/types.js +2 -5
  86. package/dist/ops/types.js.map +1 -1
  87. package/dist/production.d.ts.map +1 -1
  88. package/dist/production.js +7 -0
  89. package/dist/production.js.map +1 -1
  90. package/dist/reassemble.d.ts +3 -1
  91. package/dist/reassemble.d.ts.map +1 -1
  92. package/dist/reassemble.js +7 -0
  93. package/dist/reassemble.js.map +1 -1
  94. package/dist/render/canvas2d.d.ts +3 -3
  95. package/dist/render/canvas2d.d.ts.map +1 -1
  96. package/dist/render/draw.d.ts +29 -2
  97. package/dist/render/draw.d.ts.map +1 -1
  98. package/dist/render/draw.js +47 -3
  99. package/dist/render/draw.js.map +1 -1
  100. package/dist/render/layout.d.ts +27 -3
  101. package/dist/render/layout.d.ts.map +1 -1
  102. package/dist/render/layout.js +464 -103
  103. package/dist/render/layout.js.map +1 -1
  104. package/dist/render/sceneModel.d.ts.map +1 -1
  105. package/dist/render/sceneModel.js +76 -27
  106. package/dist/render/sceneModel.js.map +1 -1
  107. package/dist/render/svgPath.d.ts +36 -8
  108. package/dist/render/svgPath.d.ts.map +1 -1
  109. package/dist/render/svgPath.js +303 -30
  110. package/dist/render/svgPath.js.map +1 -1
  111. package/dist/render/textLayout.d.ts +36 -0
  112. package/dist/render/textLayout.d.ts.map +1 -1
  113. package/dist/render/textLayout.js +46 -3
  114. package/dist/render/textLayout.js.map +1 -1
  115. package/dist/render/transform.d.ts +10 -0
  116. package/dist/render/transform.d.ts.map +1 -1
  117. package/dist/render/transform.js +15 -5
  118. package/dist/render/transform.js.map +1 -1
  119. package/dist/source-merge.d.ts +84 -0
  120. package/dist/source-merge.d.ts.map +1 -0
  121. package/dist/source-merge.js +200 -0
  122. package/dist/source-merge.js.map +1 -0
  123. package/dist/storyboard.d.ts +20 -3
  124. package/dist/storyboard.d.ts.map +1 -1
  125. package/dist/storyboard.js +61 -27
  126. package/dist/storyboard.js.map +1 -1
  127. package/dist/storyboardDesignFrame.d.ts +15 -0
  128. package/dist/storyboardDesignFrame.d.ts.map +1 -0
  129. package/dist/storyboardDesignFrame.js +44 -0
  130. package/dist/storyboardDesignFrame.js.map +1 -0
  131. package/dist/storyboardValidation.d.ts +10 -0
  132. package/dist/storyboardValidation.d.ts.map +1 -0
  133. package/dist/storyboardValidation.js +47 -0
  134. package/dist/storyboardValidation.js.map +1 -0
  135. package/dist/types.d.ts +161 -20
  136. package/dist/types.d.ts.map +1 -1
  137. package/dist/types.js.map +1 -1
  138. package/package.json +28 -4
@@ -1,5 +1,7 @@
1
+ import Yoga, { Align, Direction, Display, Edge, FlexDirection, Gutter, Justify, MeasureMode, PositionType, Wrap } from "yoga-layout";
1
2
  import { layoutTextBlock, textFontSpec } from "./textLayout.js";
2
3
  import { IDENTITY_TRANSFORM, buildTransformMatrix, containBaseScale } from "./transform.js";
4
+ import { parseSvgPath, pathSegmentBounds } from "./svgPath.js";
3
5
  function projectedBox(box, transform, canvas, base = { x: 1, y: 1 }) {
4
6
  const matrix = buildTransformMatrix(transform, base, canvas.width, canvas.height);
5
7
  const corners = [
@@ -25,6 +27,51 @@ function projectedBox(box, transform, canvas, base = { x: 1, y: 1 }) {
25
27
  height: Math.max(...points.map((point) => point.y)) - top
26
28
  };
27
29
  }
30
+ /**
31
+ * A shape's own box before any transform, in surface pixels: the authored
32
+ * `x`/`y`/`width`/`height` for every kind except `"path"`, where those
33
+ * fields go unused by the draw code entirely (`buildShapeSegments` scales a
34
+ * path's `d` straight to the full canvas — see `shapeGeometry.ts`). A path
35
+ * with no authored `width`/`height` gets its real box from `d`'s own
36
+ * geometry — {@link pathSegmentBounds} of its parsed segments — instead of
37
+ * `fallback`, so an author does not have to hand-measure a path's extent
38
+ * just to give it a `flexItem` size. A path that fails to parse, or one
39
+ * whose `d` is empty, falls back like every other unset shape does; the
40
+ * validator's `shape_path_invalid` already reports the parse failure itself.
41
+ */
42
+ function shapeAuthoredBox(shape, canvas, fallback) {
43
+ if (shape.kind === "path" && shape.width === undefined && shape.height === undefined) {
44
+ const parsed = parseSvgPath(shape.d ?? "");
45
+ if (parsed.ok) {
46
+ const bounds = pathSegmentBounds(parsed.segments);
47
+ if (bounds) {
48
+ return {
49
+ x: bounds.minX * canvas.width,
50
+ y: bounds.minY * canvas.height,
51
+ width: (bounds.maxX - bounds.minX) * canvas.width,
52
+ height: (bounds.maxY - bounds.minY) * canvas.height
53
+ };
54
+ }
55
+ }
56
+ }
57
+ return {
58
+ x: (shape.x ?? fallback.x) * canvas.width,
59
+ y: (shape.y ?? fallback.y) * canvas.height,
60
+ width: (shape.width ?? fallback.width) * canvas.width,
61
+ height: (shape.height ?? fallback.height) * canvas.height
62
+ };
63
+ }
64
+ /**
65
+ * The box a clip's own content currently renders at (canvas-center-relative
66
+ * pixels): a group's children union, a text clip's wrapped block, a shape's
67
+ * authored rectangle, or a media clip's contain-fit frame — each placed by
68
+ * the clip's *current* `transform`. This is what {@link resolveClipLayouts}
69
+ * measures a leaf's flex intrinsic size from, and what the flex resolver
70
+ * diffs against a Yoga-computed target box to find how far `transform.position`
71
+ * has to move (see `moved` below) — so placing a clip is always "shift
72
+ * position by the same delta that would shift this box," regardless of scale,
73
+ * rotation or anchor.
74
+ */
28
75
  function measuredClipBox(clip, canvas, childrenByParent, visited) {
29
76
  const transform = clip.transform ?? IDENTITY_TRANSFORM;
30
77
  if (clip.mediaType === "group") {
@@ -52,12 +99,8 @@ function measuredClipBox(clip, canvas, childrenByParent, visited) {
52
99
  return projectedBox(box, transform, canvas);
53
100
  }
54
101
  if (clip.mediaType === "shape" && clip.shapeStyle) {
55
- const shape = clip.shapeStyle;
56
- const left = (shape.x ?? 0) * canvas.width;
57
- const top = (shape.y ?? 0) * canvas.height;
58
- const width = (shape.width ?? 1) * canvas.width;
59
- const height = (shape.height ?? 1) * canvas.height;
60
- return projectedBox({ x: left, y: top, width, height }, transform, canvas);
102
+ const box = shapeAuthoredBox(clip.shapeStyle, canvas, { x: 0, y: 0, width: 1, height: 1 });
103
+ return projectedBox(box, transform, canvas);
61
104
  }
62
105
  const size = containBaseScale(clip.width ?? canvas.width, clip.height ?? canvas.height, canvas.width, canvas.height);
63
106
  return projectedBox({ x: 0, y: 0, width: canvas.width, height: canvas.height }, transform, canvas, size);
@@ -84,114 +127,432 @@ function moved(base, x, y, scale) {
84
127
  const source = base ?? IDENTITY_TRANSFORM;
85
128
  return { ...source, position: { x, y }, scale: scale ?? source.scale };
86
129
  }
87
- /** Resolve rows, stacks, and box-relative placement from the current clip content. */
88
- export function resolveClipLayoutsWithDiagnostics(clips, canvas) {
89
- const byId = new Map(clips.map((clip) => [clip.id, clip]));
90
- const childrenByParent = indexChildren(clips);
91
- const result = new Map();
92
- for (const container of clips) {
93
- const layout = container.layout;
94
- if (!layout || layout.kind === "relative" || !layout.children?.length)
95
- continue;
96
- const children = layout.children.map((id) => byId.get(id)).filter((child) => child !== undefined);
97
- const boxes = children.map((child) => boxFor(child, canvas, childrenByParent));
98
- const gap = layout.gapPx ?? 0;
99
- const extent = boxes.reduce((sum, box) => sum + (layout.kind === "row" ? box.width : box.height), 0) + Math.max(0, boxes.length - 1) * gap;
100
- let cursor = -extent / 2;
101
- const origin = container.transform?.position ?? { x: 0, y: 0 };
130
+ function fourEdges(value, fallback = 0) {
131
+ if (value === undefined)
132
+ return { top: fallback, right: fallback, bottom: fallback, left: fallback };
133
+ if (typeof value === "number")
134
+ return { top: value, right: value, bottom: value, left: value };
135
+ return {
136
+ top: value.top ?? fallback,
137
+ right: value.right ?? fallback,
138
+ bottom: value.bottom ?? fallback,
139
+ left: value.left ?? fallback
140
+ };
141
+ }
142
+ /** `ClipLayoutDimension` (px number, `"N%"`, or a numeric string) as Yoga's setter wants it. */
143
+ function yogaDimension(value) {
144
+ if (value === undefined)
145
+ return undefined;
146
+ if (typeof value === "number")
147
+ return Number.isFinite(value) ? value : undefined;
148
+ const trimmed = value.trim();
149
+ if (trimmed.endsWith("%"))
150
+ return trimmed;
151
+ const n = Number(trimmed);
152
+ return Number.isFinite(n) ? n : undefined;
153
+ }
154
+ /** Root-only: a container with no flex parent resolves a `"N%"` dimension
155
+ * against the canvas, since it has no Yoga parent to resolve it against. */
156
+ function rootDimensionPx(value, basisPx) {
157
+ if (value === undefined)
158
+ return undefined;
159
+ if (typeof value === "number")
160
+ return value;
161
+ const trimmed = value.trim();
162
+ if (trimmed.endsWith("%")) {
163
+ const n = Number(trimmed.slice(0, -1));
164
+ return Number.isFinite(n) ? (n / 100) * basisPx : undefined;
165
+ }
166
+ const n = Number(trimmed);
167
+ return Number.isFinite(n) ? n : undefined;
168
+ }
169
+ const JUSTIFY_MAP = {
170
+ "flex-start": Justify.FlexStart,
171
+ "flex-end": Justify.FlexEnd,
172
+ center: Justify.Center,
173
+ "space-between": Justify.SpaceBetween,
174
+ "space-around": Justify.SpaceAround,
175
+ "space-evenly": Justify.SpaceEvenly
176
+ };
177
+ const ALIGN_MAP = {
178
+ "flex-start": Align.FlexStart,
179
+ "flex-end": Align.FlexEnd,
180
+ center: Align.Center,
181
+ stretch: Align.Stretch,
182
+ baseline: Align.Baseline
183
+ };
184
+ const ALIGN_CONTENT_MAP = {
185
+ "flex-start": Align.FlexStart,
186
+ "flex-end": Align.FlexEnd,
187
+ center: Align.Center,
188
+ "space-between": Align.SpaceBetween,
189
+ "space-around": Align.SpaceAround,
190
+ "space-evenly": Align.SpaceEvenly
191
+ };
192
+ const WRAP_MAP = {
193
+ nowrap: Wrap.NoWrap,
194
+ wrap: Wrap.Wrap,
195
+ "wrap-reverse": Wrap.WrapReverse
196
+ };
197
+ function applyContainerStyle(node, layout, isRoot, canvas) {
198
+ node.setDisplay(Display.Flex);
199
+ node.setFlexDirection(layout.flexDirection === "column" ? FlexDirection.Column : FlexDirection.Row);
200
+ if (layout.justifyContent)
201
+ node.setJustifyContent(JUSTIFY_MAP[layout.justifyContent]);
202
+ if (layout.alignItems)
203
+ node.setAlignItems(ALIGN_MAP[layout.alignItems]);
204
+ if (layout.alignContent)
205
+ node.setAlignContent(ALIGN_CONTENT_MAP[layout.alignContent]);
206
+ if (layout.flexWrap)
207
+ node.setFlexWrap(WRAP_MAP[layout.flexWrap]);
208
+ if (layout.gap !== undefined)
209
+ node.setGap(Gutter.All, layout.gap);
210
+ if (layout.columnGap !== undefined)
211
+ node.setGap(Gutter.Column, layout.columnGap);
212
+ if (layout.rowGap !== undefined)
213
+ node.setGap(Gutter.Row, layout.rowGap);
214
+ const padding = fourEdges(layout.padding);
215
+ node.setPadding(Edge.Top, padding.top);
216
+ node.setPadding(Edge.Right, padding.right);
217
+ node.setPadding(Edge.Bottom, padding.bottom);
218
+ node.setPadding(Edge.Left, padding.left);
219
+ if (isRoot) {
220
+ const w = rootDimensionPx(layout.width, canvas.width);
221
+ const h = rootDimensionPx(layout.height, canvas.height);
222
+ if (w !== undefined)
223
+ node.setWidth(w);
224
+ if (h !== undefined)
225
+ node.setHeight(h);
226
+ const minW = rootDimensionPx(layout.minWidth, canvas.width);
227
+ const maxW = rootDimensionPx(layout.maxWidth, canvas.width);
228
+ const minH = rootDimensionPx(layout.minHeight, canvas.height);
229
+ const maxH = rootDimensionPx(layout.maxHeight, canvas.height);
230
+ if (minW !== undefined)
231
+ node.setMinWidth(minW);
232
+ if (maxW !== undefined)
233
+ node.setMaxWidth(maxW);
234
+ if (minH !== undefined)
235
+ node.setMinHeight(minH);
236
+ if (maxH !== undefined)
237
+ node.setMaxHeight(maxH);
238
+ }
239
+ else {
240
+ const w = yogaDimension(layout.width);
241
+ const h = yogaDimension(layout.height);
242
+ if (w !== undefined)
243
+ node.setWidth(w);
244
+ if (h !== undefined)
245
+ node.setHeight(h);
246
+ const minW = yogaDimension(layout.minWidth);
247
+ const maxW = yogaDimension(layout.maxWidth);
248
+ const minH = yogaDimension(layout.minHeight);
249
+ const maxH = yogaDimension(layout.maxHeight);
250
+ if (minW !== undefined)
251
+ node.setMinWidth(minW);
252
+ if (maxW !== undefined)
253
+ node.setMaxWidth(maxW);
254
+ if (minH !== undefined)
255
+ node.setMinHeight(minH);
256
+ if (maxH !== undefined)
257
+ node.setMaxHeight(maxH);
258
+ }
259
+ }
260
+ function applyFlexItemStyle(node, item) {
261
+ // CSS's UA default is `flex-shrink: 1`; Yoga's own bare default is 0. Match
262
+ // CSS so a leaf with a large intrinsic (measure-func) size — the common
263
+ // case for an undecorated shape or media clip — still shrinks to fit a
264
+ // container instead of overflowing it. Applies to every child, not only
265
+ // one that authors a `flexItem`.
266
+ node.setFlexShrink(item?.shrink ?? 1);
267
+ if (!item)
268
+ return;
269
+ if (item.grow !== undefined)
270
+ node.setFlexGrow(item.grow);
271
+ const basis = yogaDimension(item.basis);
272
+ if (basis !== undefined)
273
+ node.setFlexBasis(basis);
274
+ if (item.alignSelf)
275
+ node.setAlignSelf(ALIGN_MAP[item.alignSelf]);
276
+ const w = yogaDimension(item.width);
277
+ const h = yogaDimension(item.height);
278
+ if (w !== undefined)
279
+ node.setWidth(w);
280
+ if (h !== undefined)
281
+ node.setHeight(h);
282
+ const margin = fourEdges(item.margin);
283
+ node.setMargin(Edge.Top, margin.top);
284
+ node.setMargin(Edge.Right, margin.right);
285
+ node.setMargin(Edge.Bottom, margin.bottom);
286
+ node.setMargin(Edge.Left, margin.left);
287
+ if (item.position === "absolute") {
288
+ node.setPositionType(PositionType.Absolute);
289
+ const inset = item.inset;
290
+ if (typeof inset === "number") {
291
+ node.setPosition(Edge.Top, inset);
292
+ node.setPosition(Edge.Right, inset);
293
+ node.setPosition(Edge.Bottom, inset);
294
+ node.setPosition(Edge.Left, inset);
295
+ }
296
+ else if (inset) {
297
+ if (inset.top !== undefined)
298
+ node.setPosition(Edge.Top, inset.top);
299
+ if (inset.right !== undefined)
300
+ node.setPosition(Edge.Right, inset.right);
301
+ if (inset.bottom !== undefined)
302
+ node.setPosition(Edge.Bottom, inset.bottom);
303
+ if (inset.left !== undefined)
304
+ node.setPosition(Edge.Left, inset.left);
305
+ }
306
+ }
307
+ }
308
+ const MAX_TEXT_WRAP_FRAC = 1;
309
+ /** Intrinsic (measure-func) size of a leaf clip at Yoga's offered width. Only
310
+ * a text clip actually reflows; every other kind returns a size independent
311
+ * of the offered constraint, which is what makes a fixed `flexItem.width`/
312
+ * `height` (set directly on the node, not through this function) the only
313
+ * way to resize it. */
314
+ function measureLeaf(clip, canvas, childrenByParent, offeredWidth, widthMode) {
315
+ if (clip.mediaType === "text" && clip.textStyle) {
316
+ const style = clip.textStyle;
317
+ const font = textFontSpec(style);
318
+ const measure = canvas.measureText
319
+ ? (text) => canvas.measureText?.(text, font) ?? 0
320
+ : (text) => text.length * style.fontSizePx * 0.6;
321
+ // A text clip ignores its own authored `maxWidthFrac` inside a flex tree
322
+ // (see AGENTS.md): the width it wraps to is whatever Yoga is offering,
323
+ // not what it was authored with. `MAX_TEXT_WRAP_FRAC` approximates an
324
+ // unconstrained (max-content) measurement — the widest this API can ask
325
+ // `layoutTextBlock` to try is the whole canvas.
326
+ const effectiveFrac = canvas.width > 0 && widthMode !== MeasureMode.Undefined && Number.isFinite(offeredWidth)
327
+ ? Math.min(MAX_TEXT_WRAP_FRAC, Math.max(0.05, offeredWidth / canvas.width))
328
+ : MAX_TEXT_WRAP_FRAC;
329
+ const effectiveStyle = { ...style, maxWidthFrac: effectiveFrac };
330
+ const box = layoutTextBlock(measure, effectiveStyle, canvas.width, canvas.height).box;
331
+ return { width: box.width, height: box.height };
332
+ }
333
+ if (clip.mediaType === "shape" && clip.shapeStyle) {
334
+ // Outside a flex tree an unset width/height defaults to the whole frame
335
+ // (`measuredClipBox` above). Inside one, that default would make an
336
+ // undecorated shape's mere presence claim the whole main axis as its
337
+ // flex-basis and force every sibling with `grow`/an explicit size into a
338
+ // shrink negotiation it was never meant to be in — so the fallback here
339
+ // is 0 (CSS's own `flex-basis: auto` on a sizeless replaced element),
340
+ // and `flexItem.grow`/`.width`/`.height` are what actually size it. A
341
+ // `"path"` shape's real geometry (its `d`) overrides that 0 fallback
342
+ // regardless — see `shapeAuthoredBox` — so an author gets a usable
343
+ // intrinsic size without hand-measuring the curve.
344
+ const box = shapeAuthoredBox(clip.shapeStyle, canvas, { x: 0, y: 0, width: 0, height: 0 });
345
+ return { width: box.width, height: box.height };
346
+ }
347
+ if (clip.mediaType === "group") {
348
+ // A plain (non-flex) group nested as a flex leaf: its intrinsic size is
349
+ // its own unmanaged children's union box, exactly as `clipLayoutBox`
350
+ // already reports it outside a flex tree.
351
+ const box = boxFor(clip, canvas, childrenByParent);
352
+ return { width: box.width || canvas.width, height: box.height || canvas.height };
353
+ }
354
+ // image / video / model3d / audio / adjustment / midi: no reflow, and no
355
+ // authored size beyond `clip.width`/`clip.height` (source pixels, not a
356
+ // layout hint) — the frame-filling default `measuredClipBox` already uses.
357
+ return { width: canvas.width, height: canvas.height };
358
+ }
359
+ /**
360
+ * Build one Yoga node per clip in a flex subtree, recursively, in document
361
+ * order. `visited` guards a `parentId` cycle: a clip already on the current
362
+ * path is treated as a childless leaf and reported back through `cycles`.
363
+ */
364
+ function buildTree(clip, canvas, childrenByParent, isRoot, visited, built, cycles) {
365
+ const node = Yoga.Node.create();
366
+ const isFlexContainer = clip.mediaType === "group" && clip.layout?.display === "flex";
367
+ built.set(clip.id, { node, clip, isFlexContainer });
368
+ if (isFlexContainer) {
369
+ applyContainerStyle(node, clip.layout, isRoot, canvas);
370
+ if (visited.has(clip.id)) {
371
+ cycles.add(clip.id);
372
+ return node;
373
+ }
374
+ visited.add(clip.id);
375
+ const children = childrenByParent.get(clip.id) ?? [];
102
376
  children.forEach((child, index) => {
103
- const box = boxes[index];
104
- if (!box)
105
- return;
106
- const center = cursor + (layout.kind === "row" ? box.width : box.height) / 2;
107
- const oldCenter = { x: box.x + box.width / 2, y: box.y + box.height / 2 };
108
- const nextX = layout.kind === "row" ? origin.x + center : origin.x;
109
- const nextY = layout.kind === "stack" ? origin.y + center : origin.y;
110
- const current = child.transform ?? IDENTITY_TRANSFORM;
111
- result.set(child.id, moved(current, current.position.x + nextX - oldCenter.x, current.position.y + nextY - oldCenter.y));
112
- cursor += (layout.kind === "row" ? box.width : box.height) + gap;
377
+ const childNode = buildTree(child, canvas, childrenByParent, false, visited, built, cycles);
378
+ applyFlexItemStyle(childNode, child.flexItem);
379
+ node.insertChild(childNode, index);
113
380
  });
381
+ visited.delete(clip.id);
114
382
  }
115
- const relative = clips.filter((clip) => clip.layout?.kind === "relative");
116
- const relativeIds = new Set(relative.map((clip) => clip.id));
117
- const dependents = new Map();
118
- const indegree = new Map(relative.map((clip) => [clip.id, 0]));
119
- for (const clip of relative) {
120
- const targetId = clip.layout?.targetClipId;
121
- if (!targetId || !relativeIds.has(targetId))
122
- continue;
123
- indegree.set(clip.id, 1);
124
- const siblings = dependents.get(targetId) ?? [];
125
- siblings.push(clip);
126
- dependents.set(targetId, siblings);
127
- }
128
- const queue = relative.filter((clip) => indegree.get(clip.id) === 0);
129
- for (let index = 0; index < queue.length; index++) {
130
- const clip = queue[index];
131
- const layout = clip.layout;
132
- if (!layout || layout.kind !== "relative" || !layout.targetClipId) {
133
- // A missing target leaves the authored transform in place.
383
+ else {
384
+ node.setMeasureFunc((width, widthMode, _height, _heightMode) => measureLeaf(clip, canvas, childrenByParent, width, widthMode));
385
+ }
386
+ return node;
387
+ }
388
+ function collectAbsolutePositions(clip, node, originX, originY, built, childrenByParent, out) {
389
+ const left = originX + node.getComputedLeft();
390
+ const top = originY + node.getComputedTop();
391
+ const width = node.getComputedWidth();
392
+ const height = node.getComputedHeight();
393
+ out.set(clip.id, { x: left, y: top, width, height });
394
+ const isFlexContainer = clip.mediaType === "group" && clip.layout?.display === "flex";
395
+ if (!isFlexContainer)
396
+ return;
397
+ const children = childrenByParent.get(clip.id) ?? [];
398
+ children.forEach((child) => {
399
+ const childBuilt = built.get(child.id);
400
+ if (!childBuilt)
401
+ return;
402
+ collectAbsolutePositions(child, childBuilt.node, left, top, built, childrenByParent, out);
403
+ });
404
+ }
405
+ /**
406
+ * A clip is a flex root when it is a flex container that no ancestor's flex
407
+ * tree already reaches — i.e. it has no `parentId`, or its parent is not
408
+ * itself a flex container.
409
+ */
410
+ function isFlexRoot(clip, byId) {
411
+ if (clip.mediaType !== "group" || clip.layout?.display !== "flex")
412
+ return false;
413
+ if (!clip.parentId)
414
+ return true;
415
+ const parent = byId.get(clip.parentId);
416
+ return !(parent && parent.mediaType === "group" && parent.layout?.display === "flex");
417
+ }
418
+ function computeClipLayoutsUncached(clips, canvas) {
419
+ const byId = new Map(clips.map((clip) => [clip.id, clip]));
420
+ const childrenByParent = indexChildren(clips);
421
+ const transforms = new Map();
422
+ const sizes = new Map();
423
+ const resolvedBoxes = new Map();
424
+ const allCycles = new Set();
425
+ const roots = clips.filter((clip) => isFlexRoot(clip, byId));
426
+ for (const root of roots) {
427
+ const built = new Map();
428
+ const visited = new Set();
429
+ const cycles = new Set();
430
+ const rootNode = buildTree(root, canvas, childrenByParent, true, visited, built, cycles);
431
+ for (const id of cycles)
432
+ allCycles.add(id);
433
+ const rootWidth = root.layout?.width !== undefined ? rootDimensionPx(root.layout.width, canvas.width) : undefined;
434
+ const rootHeight = root.layout?.height !== undefined ? rootDimensionPx(root.layout.height, canvas.height) : undefined;
435
+ rootNode.calculateLayout(rootWidth, rootHeight, Direction.LTR);
436
+ // Every resolved position below is PARENT-LOCAL, not canvas-absolute.
437
+ // `transform.position` is left out of `originX`/`originY` on purpose:
438
+ // the renderer's own `parentMatrix` composition (`resolveGroups`, from
439
+ // the root's own untouched `transform` — the root is a group like any
440
+ // other) already adds it once when it places the root itself, for a
441
+ // pure-translation matrix (no scale/rotation) at *every* point that
442
+ // matrix transforms, not only the root's own quad — so baking it in
443
+ // here too would apply it a second time to every descendant. A root at
444
+ // (0, 0) hid this: the double application added zero.
445
+ //
446
+ // `anchor` stays, though, and for a different reason than `position`:
447
+ // it says which point of the root's *computed flex box* is the one
448
+ // `position` places (AGENTS.md) — a purely local fact about where Yoga's
449
+ // own (top-left) origin sits inside that box, unrelated to how the
450
+ // parent matrix later moves the whole thing. Yoga's own coordinates are
451
+ // top-left-origin; subtracting `anchor.x * rootBoxWidth` re-centers them
452
+ // on the root's own anchor point before `canvas.width / 2` places that
453
+ // point at the frame's own center — the untranslated stand-in `position`
454
+ // will be added to afterward, by the parent matrix, for real.
455
+ const rootBoxWidth = rootNode.getComputedWidth();
456
+ const rootBoxHeight = rootNode.getComputedHeight();
457
+ const rootAnchor = root.transform?.anchor ?? { x: 0.5, y: 0.5 };
458
+ const originX = canvas.width / 2 - rootAnchor.x * rootBoxWidth;
459
+ const originY = canvas.height / 2 - rootAnchor.y * rootBoxHeight;
460
+ const boxes = new Map();
461
+ collectAbsolutePositions(root, rootNode, originX, originY, built, childrenByParent, boxes);
462
+ for (const [id, box] of boxes) {
463
+ resolvedBoxes.set(id, { x: box.x - canvas.width / 2, y: box.y - canvas.height / 2, width: box.width, height: box.height });
134
464
  }
135
- else {
136
- const target = byId.get(layout.targetClipId);
137
- if (target) {
138
- const targetTransform = result.get(target.id) ?? target.transform;
139
- const targetBox = boxFor({ ...target, transform: targetTransform }, canvas, childrenByParent);
140
- const own = boxFor(clip, canvas, childrenByParent);
141
- const gap = layout.gapPx ?? 0;
142
- const side = layout.side ?? "center";
143
- let centerX = targetBox.x + targetBox.width / 2;
144
- let centerY = targetBox.y + targetBox.height / 2;
145
- if (side === "left")
146
- centerX = targetBox.x - own.width / 2 - gap;
147
- if (side === "right")
148
- centerX = targetBox.x + targetBox.width + own.width / 2 + gap;
149
- if (side === "above")
150
- centerY = targetBox.y - own.height / 2 - gap;
151
- if (side === "below")
152
- centerY = targetBox.y + targetBox.height + own.height / 2 + gap;
153
- let scale;
154
- if (layout.fitText && target.mediaType === "text") {
155
- scale = {
156
- x: (targetBox.width + 2 * layout.fitText.paddingXPx) / Math.max(1, own.width) * (clip.transform?.scale.x ?? 1),
157
- y: (targetBox.height + 2 * layout.fitText.paddingYPx) / Math.max(1, own.height) * (clip.transform?.scale.y ?? 1)
158
- };
465
+ // Walk every clip this tree touched and turn its target box into a
466
+ // `transform.position` delta against its *current* rendered box — the
467
+ // primitive `moved()` already expresses. The root itself is placed by its
468
+ // own authored `transform` (position + anchor, above) rather than by this
469
+ // diff, so it is excluded here.
470
+ for (const [id, builtNode] of built) {
471
+ if (id === root.id)
472
+ continue;
473
+ const clip = builtNode.clip;
474
+ const target = boxes.get(id);
475
+ if (!target)
476
+ continue;
477
+ const isAbsoluteNoInset = clip.flexItem?.position === "absolute" && clip.flexItem?.inset === undefined;
478
+ let sizedClip = clip;
479
+ let sizeOverride;
480
+ if (!builtNode.isFlexContainer) {
481
+ if (clip.mediaType === "shape" && clip.shapeStyle && clip.shapeStyle.kind !== "path" && canvas.width > 0 && canvas.height > 0) {
482
+ sizeOverride = { width: target.width, height: target.height };
483
+ // `x`/`y` are pinned to the top-left too, not left at whatever the
484
+ // clip authored (or its own default, which `shapeBox` centers at
485
+ // 0.25/0.25 — not the 0/0 this box math assumes): the box we are
486
+ // resizing to is measured from the frame's top-left, via the same
487
+ // `x ?? 0` convention `measuredClipBox`'s shape branch uses above,
488
+ // so the drawn shape has to use that convention too or it renders
489
+ // centered inside its own resized box instead of filling it.
490
+ //
491
+ // Excluded: `"path"`. Its draw code (`buildShapeSegments`) ignores
492
+ // `x`/`y`/`width`/`height` entirely — it scales `d` straight to
493
+ // the full canvas — so writing a resized box into those fields
494
+ // would change nothing about what's drawn while corrupting the
495
+ // *position* diff below (`shapeAuthoredBox`'s real, `d`-derived
496
+ // current box would stop applying, since it only kicks in when
497
+ // width/height are unset). A path's flex-computed size only ever
498
+ // repositions it via the diff; it cannot resize the curve itself.
499
+ sizedClip = { ...clip, shapeStyle: { ...clip.shapeStyle, x: 0, y: 0, width: target.width / canvas.width, height: target.height / canvas.height } };
500
+ }
501
+ else if (clip.mediaType === "text" && clip.textStyle && canvas.width > 0) {
502
+ sizeOverride = { width: target.width, height: target.height };
503
+ sizedClip = { ...clip, textStyle: { ...clip.textStyle, maxWidthFrac: Math.min(1, Math.max(0.05, target.width / canvas.width)) } };
159
504
  }
160
- const current = clip.transform ?? IDENTITY_TRANSFORM;
161
- const sized = scale ? boxFor({ ...clip, transform: moved(current, current.position.x, current.position.y, scale) }, canvas, childrenByParent) : own;
162
- result.set(clip.id, moved(clip.transform, current.position.x + centerX - (sized.x + sized.width / 2), current.position.y + centerY - (sized.y + sized.height / 2), scale));
163
505
  }
164
- }
165
- indegree.delete(clip.id);
166
- for (const dependent of dependents.get(clip.id) ?? []) {
167
- indegree.set(dependent.id, (indegree.get(dependent.id) ?? 1) - 1);
168
- if (indegree.get(dependent.id) === 0)
169
- queue.push(dependent);
170
- }
171
- }
172
- const examined = new Set();
173
- const cycleIds = new Set();
174
- for (const clip of relative) {
175
- if (!indegree.has(clip.id) || examined.has(clip.id))
176
- continue;
177
- const path = [];
178
- const pathIndex = new Map();
179
- let cursor = clip.id;
180
- while (cursor && indegree.has(cursor) && !examined.has(cursor)) {
181
- const repeatedAt = pathIndex.get(cursor);
182
- if (repeatedAt !== undefined) {
183
- path.slice(repeatedAt).forEach((id) => cycleIds.add(id));
184
- break;
506
+ if (sizeOverride)
507
+ sizes.set(id, sizeOverride);
508
+ if (isAbsoluteNoInset) {
509
+ // Keeps its authored position; only a size override (if any) applies.
510
+ if (sizeOverride)
511
+ transforms.set(id, sizedClip.transform ?? IDENTITY_TRANSFORM);
512
+ continue;
185
513
  }
186
- pathIndex.set(cursor, path.length);
187
- path.push(cursor);
188
- cursor = byId.get(cursor)?.layout?.targetClipId;
514
+ const currentBox = boxFor(sizedClip, canvas, childrenByParent);
515
+ const currentCenter = { x: currentBox.x + currentBox.width / 2, y: currentBox.y + currentBox.height / 2 };
516
+ const targetCenter = { x: target.x + target.width / 2 - canvas.width / 2, y: target.y + target.height / 2 - canvas.height / 2 };
517
+ const current = sizedClip.transform ?? IDENTITY_TRANSFORM;
518
+ transforms.set(id, moved(current, current.position.x + targetCenter.x - currentCenter.x, current.position.y + targetCenter.y - currentCenter.y));
189
519
  }
190
- path.forEach((id) => examined.add(id));
520
+ // `freeRecursive` walks the tree it was inserted into, so freeing the
521
+ // root frees every child exactly once.
522
+ rootNode.freeRecursive();
191
523
  }
192
- return { transforms: result, cycles: relative.filter((clip) => cycleIds.has(clip.id)).map((clip) => clip.id) };
524
+ return { transforms, sizes, resolvedBoxes, cycles: [...allCycles] };
525
+ }
526
+ /**
527
+ * Layout is static per clip (AGENTS.md): resolving the same `clips` array
528
+ * against the same canvas size and measurer twice must not rebuild the Yoga
529
+ * tree twice. Every host calls this once per frame (`sceneModel.ts`'s
530
+ * `computeActiveLayers`), and a document's `clips` array is only ever
531
+ * replaced wholesale by an edit (the stores build a new array on every
532
+ * mutation) — never mutated in place — so a `WeakMap` keyed by that array
533
+ * identity, holding one entry good for one canvas size and measurer, is a
534
+ * correct cache: an unchanged document during scrubbing/playback hits it on
535
+ * every frame, and any edit (a new array) or a canvas resize misses it.
536
+ */
537
+ const layoutCache = new WeakMap();
538
+ export function resolveClipLayoutsWithDiagnostics(clips, canvas) {
539
+ const cached = layoutCache.get(clips);
540
+ if (cached &&
541
+ cached.width === canvas.width &&
542
+ cached.height === canvas.height &&
543
+ cached.measureText === canvas.measureText) {
544
+ return cached.result;
545
+ }
546
+ const result = computeClipLayoutsUncached(clips, canvas);
547
+ layoutCache.set(clips, { width: canvas.width, height: canvas.height, measureText: canvas.measureText, result });
548
+ return result;
193
549
  }
194
550
  export function resolveClipLayouts(clips, canvas) {
195
551
  return resolveClipLayoutsWithDiagnostics(clips, canvas).transforms;
196
552
  }
553
+ /** Sizes flex resolved for a leaf (rect/ellipse's box, or a text clip's
554
+ * effective wrap width) alongside {@link resolveClipLayouts}'s transforms. */
555
+ export function resolveClipLayoutSizes(clips, canvas) {
556
+ return resolveClipLayoutsWithDiagnostics(clips, canvas).sizes;
557
+ }
197
558
  //# sourceMappingURL=layout.js.map