@pinepaper.studio/mcp-server 1.6.13 → 1.6.14

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 (87) hide show
  1. package/README.de.md +1 -1
  2. package/README.es.md +1 -1
  3. package/README.fr.md +1 -1
  4. package/README.hi.md +1 -1
  5. package/README.ja.md +1 -1
  6. package/README.ko.md +1 -1
  7. package/README.md +179 -4
  8. package/README.pt-BR.md +1 -1
  9. package/README.zh-CN.md +1 -1
  10. package/dist/build-info.json +5 -0
  11. package/dist/design/design-systems.js +1 -1
  12. package/dist/design/design-systems.js.map +1 -1
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +3 -2
  15. package/dist/index.js.map +1 -1
  16. package/dist/ontology/upstream-vocabulary.d.ts +23 -0
  17. package/dist/ontology/upstream-vocabulary.d.ts.map +1 -0
  18. package/dist/ontology/upstream-vocabulary.js +1172 -0
  19. package/dist/ontology/upstream-vocabulary.js.map +1 -0
  20. package/dist/ontology/vocabulary.d.ts.map +1 -1
  21. package/dist/ontology/vocabulary.js +19 -0
  22. package/dist/ontology/vocabulary.js.map +1 -1
  23. package/dist/tools/definitions.d.ts.map +1 -1
  24. package/dist/tools/definitions.js +263 -70
  25. package/dist/tools/definitions.js.map +1 -1
  26. package/dist/tools/engine-methods.d.ts.map +1 -1
  27. package/dist/tools/engine-methods.js +23 -2
  28. package/dist/tools/engine-methods.js.map +1 -1
  29. package/dist/tools/engine-surface.d.ts +1 -1
  30. package/dist/tools/engine-surface.d.ts.map +1 -1
  31. package/dist/tools/engine-surface.js +6 -5
  32. package/dist/tools/engine-surface.js.map +1 -1
  33. package/dist/tools/handlers/camera.d.ts.map +1 -1
  34. package/dist/tools/handlers/camera.js +17 -2
  35. package/dist/tools/handlers/camera.js.map +1 -1
  36. package/dist/tools/handlers/font.d.ts.map +1 -1
  37. package/dist/tools/handlers/font.js +48 -3
  38. package/dist/tools/handlers/font.js.map +1 -1
  39. package/dist/tools/handlers/maps.d.ts.map +1 -1
  40. package/dist/tools/handlers/maps.js +16 -0
  41. package/dist/tools/handlers/maps.js.map +1 -1
  42. package/dist/tools/handlers.d.ts +11 -1
  43. package/dist/tools/handlers.d.ts.map +1 -1
  44. package/dist/tools/handlers.js +426 -14
  45. package/dist/tools/handlers.js.map +1 -1
  46. package/dist/tools/minimal-descriptions.d.ts.map +1 -1
  47. package/dist/tools/minimal-descriptions.js +2 -0
  48. package/dist/tools/minimal-descriptions.js.map +1 -1
  49. package/dist/tools/shape-params.d.ts +13 -2
  50. package/dist/tools/shape-params.d.ts.map +1 -1
  51. package/dist/tools/shape-params.js +80 -2
  52. package/dist/tools/shape-params.js.map +1 -1
  53. package/dist/tools/stick-vocabulary.d.ts +1 -1
  54. package/dist/tools/stick-vocabulary.js +1 -1
  55. package/dist/tools/toolkits.d.ts.map +1 -1
  56. package/dist/tools/toolkits.js +2 -0
  57. package/dist/tools/toolkits.js.map +1 -1
  58. package/dist/types/code-generator.d.ts +27 -1
  59. package/dist/types/code-generator.d.ts.map +1 -1
  60. package/dist/types/code-generator.js +2454 -273
  61. package/dist/types/code-generator.js.map +1 -1
  62. package/dist/types/schemas.d.ts +1478 -239
  63. package/dist/types/schemas.d.ts.map +1 -1
  64. package/dist/types/schemas.js +255 -26
  65. package/dist/types/schemas.js.map +1 -1
  66. package/dist/utils/a11y.d.ts +67 -0
  67. package/dist/utils/a11y.d.ts.map +1 -0
  68. package/dist/utils/a11y.js +117 -0
  69. package/dist/utils/a11y.js.map +1 -0
  70. package/dist/utils/ad-package.d.ts +44 -0
  71. package/dist/utils/ad-package.d.ts.map +1 -0
  72. package/dist/utils/ad-package.js +121 -0
  73. package/dist/utils/ad-package.js.map +1 -0
  74. package/dist/utils/scc.d.ts +41 -0
  75. package/dist/utils/scc.d.ts.map +1 -0
  76. package/dist/utils/scc.js +185 -0
  77. package/dist/utils/scc.js.map +1 -0
  78. package/dist/utils/zip.d.ts +7 -0
  79. package/dist/utils/zip.d.ts.map +1 -0
  80. package/dist/utils/zip.js +82 -0
  81. package/dist/utils/zip.js.map +1 -0
  82. package/dist/version.d.ts +3 -0
  83. package/dist/version.d.ts.map +1 -1
  84. package/dist/version.js +31 -1
  85. package/dist/version.js.map +1 -1
  86. package/manifest.json +9 -1
  87. package/package.json +6 -4
@@ -5,6 +5,7 @@
5
5
  * This code is designed to run in the browser context where window.PinePaper is available.
6
6
  */
7
7
  import { REQUIRED_ENGINE_METHODS, OPTIONAL_ENGINE_METHODS } from '../tools/engine-methods.js';
8
+ import { ACCEPTED_CREATE_PARAMS, NORMALIZE_PARAM_READS, MODIFY_CHANGE_READS } from '../tools/shape-params.js';
8
9
  import { generateP5DrawCode } from '../tools/p5-compat/p5-helpers.js';
9
10
  import { CreateItemInputSchema, ImportMotionCaptureInputSchema, ModifyItemInputSchema, AddRelationInputSchema, AnimateItemInputSchema, KeyframeAnimateInputSchema, ExecuteGeneratorInputSchema, ApplyEffectInputSchema, SetBackgroundColorInputSchema, SetCanvasSizeInputSchema, CreateGlossySphereInputSchema, CreateDiagonalStripesInputSchema, BatchCreateInputSchema, BatchModifyInputSchema, CreateGridInputSchema, CreateSceneInputSchema,
10
11
  // Diagram types
@@ -33,6 +34,38 @@ import { OntologyCompiler } from '../ontology/ontology-compiler.js';
33
34
  function isGradient(color) {
34
35
  return typeof color === 'object' && color !== null && 'type' in color && 'stops' in color;
35
36
  }
37
+ /**
38
+ * A colour OBJECT is a gradient spec, checked before it reaches the engine.
39
+ *
40
+ * {gradient: true, stops: ['#a', '#b']} — no type, bare-string stops, invalid
41
+ * hex — went to the engine as a plain colour and drew NOTHING, with success
42
+ * (round 9 ads, 1.82). The shape the tools document is {type: 'linear' |
43
+ * 'radial', stops: [{color, offset}], origin?, destination?}. Near-misses are
44
+ * normalised (a missing type is linear; bare stops get even offsets); what
45
+ * cannot be read is refused by name, with the shape.
46
+ */
47
+ function normalizeGradientSpec(v, key) {
48
+ const o = v;
49
+ const rawStops = Array.isArray(o.stops) ? o.stops : null;
50
+ const shape = `${key} as a gradient is {type: 'linear' | 'radial', stops: [{color: '#rrggbb', offset: 0..1}, …], origin?: [x, y], destination?: [x, y]}.`;
51
+ if (!rawStops || rawStops.length < 2)
52
+ return { error: `${key} is an object but not a gradient with at least 2 stops, so nothing would be drawn. ${shape}` };
53
+ const hexOk = (c) => !c.startsWith('#') || /^#([0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(c);
54
+ const n = rawStops.length;
55
+ const stops = [];
56
+ for (let i = 0; i < n; i++) {
57
+ const st = rawStops[i];
58
+ const color = typeof st === 'string' ? st : (st && typeof st.color === 'string' ? st.color : null);
59
+ const offset = typeof st === 'string' ? i / (n - 1) : Number(st.offset ?? i / (n - 1));
60
+ if (!color || !hexOk(color.trim()))
61
+ return { error: `${key} stop ${i} has colour ${JSON.stringify(color)}, which is not a colour (a hex needs 3, 4, 6 or 8 digits). ${shape}` };
62
+ if (!(offset >= 0 && offset <= 1))
63
+ return { error: `${key} stop ${i} has offset ${JSON.stringify(st.offset)}; offsets run 0 to 1. ${shape}` };
64
+ stops.push({ color: color.trim(), offset });
65
+ }
66
+ const type = o.type === 'radial' ? 'radial' : 'linear';
67
+ return { gradient: { ...o, type, stops } };
68
+ }
36
69
  /**
37
70
  * Generate Paper.js gradient code for a fill color
38
71
  */
@@ -80,19 +113,79 @@ function generateBlendModeCode(itemVarName, blendMode) {
80
113
  /**
81
114
  * Template for creating items with support for gradients, shadows, and blend modes
82
115
  */
83
- function generateCreateItemCode(itemType, position, properties) {
116
+ function generateCreateItemCode(itemType, position, properties, dataFlags, positionGiven = true) {
117
+ // Colour objects are gradient specs: normalised, or refused by name.
118
+ for (const key of ['color', 'fillColor', 'strokeColor']) {
119
+ const v = properties[key];
120
+ if (v && typeof v === 'object' && !Array.isArray(v)) {
121
+ const g = normalizeGradientSpec(v, key);
122
+ if ('error' in g) {
123
+ return `
124
+ // Create ${itemType} — refused: an unreadable gradient would draw nothing
125
+ ({ success: false, error: ${JSON.stringify(g.error)} });`.trim();
126
+ }
127
+ properties = { ...properties, [key]: g.gradient };
128
+ }
129
+ }
84
130
  // Extract special properties that need separate handling
85
131
  const { color, fillColor, strokeColor, shadowColor, shadowBlur, shadowOffset, blendMode, opacity, contentType, contentFormat, countdownTarget, countdownEndText, ...baseProperties } = properties;
132
+ // A PATH KEEPS ITS OWN COORDINATES WHEN NO POSITION WAS ASKED FOR (round 9
133
+ // HH, 1.81). The schema defaults position to (400, 300), and the engine
134
+ // places pathData AT x / y (so origin-centred data can be positioned) — so a
135
+ // pathData path authored in canvas coordinates was moved to the default,
136
+ // off by (+99.5, -170). The segments form was untouched only by accident of
137
+ // the engine's branch. With no position from the caller, a coordinate-built
138
+ // item (path from pathData / segments, line, arc) gets no x / y at all.
139
+ const omitXY = !positionGiven && isCoordinateBuilt(itemType, properties);
140
+ // `fit` is applied through app.fitText after creation (below), not sent to
141
+ // create(), so a studio without it cannot half-read it.
142
+ const textFit = itemType === 'text' && baseProperties.fit && typeof baseProperties.fit === 'object' ? baseProperties.fit : null;
143
+ const { fit: _fitDropped, ...createProps } = baseProperties;
144
+ void _fitDropped;
145
+ let textDirection = itemType === 'text' ? takeDirection(createProps) : null;
146
+ // RIGHT-TO-LEFT TEXT WITH NO DIRECTION GIVEN. The engine's default is auto,
147
+ // but it records nothing, so nothing could confirm it. For content with any
148
+ // RTL script, auto is sent explicitly — recorded, so checkable — and the
149
+ // result says which way it resolved.
150
+ if (itemType === 'text' && !textDirection && typeof createProps.content === 'string' && RTL_SCRIPT.test(createProps.content)) {
151
+ createProps.direction = 'auto';
152
+ textDirection = { value: 'auto' };
153
+ }
154
+ if (textDirection && 'value' in textDirection && textDirection.value === 'auto' && typeof createProps.content === 'string') {
155
+ textDirection.resolved = baseDirection(createProps.content);
156
+ }
157
+ const textTabular = itemType === 'text' && wantsTabular(createProps) === true;
158
+ const textStrokePos = itemType === 'text' ? takeStrokePosition(createProps, false) : null;
86
159
  const params = {
87
- x: position.x,
88
- y: position.y,
89
- ...withRadiusAxes(baseProperties),
160
+ ...(omitXY ? {} : { x: position.x, y: position.y }),
161
+ ...withRadiusAxes(createProps),
90
162
  };
163
+ // "NO FILL" HAS THREE SPELLINGS, AND NONE OF THEM WORKED (round 7 AA, 1.50).
164
+ // fillColor:null was dropped by a truthiness check, so the engine's default
165
+ // blue applied; 'transparent' reached Paper, which read it as black. A
166
+ // stroke-only rectangle — whiteboard line art — could not be made at create.
167
+ // All three now mean no fill, applied after the item exists.
168
+ const noFill = (v) => v === null || (typeof v === 'string' && /^(transparent|none)$/i.test(v.trim()));
169
+ const clearFill = (fillColor !== undefined && noFill(fillColor)) || (fillColor === undefined && color !== undefined && noFill(color));
170
+ // `color` ON A CLOSED PATH IS ITS FILL.
171
+ //
172
+ // The engine's path branch paints `color` as the STROKE whenever no fillColor
173
+ // is given — right for an open line, and the opposite of every shape, where
174
+ // color is the fill. So a closed blob or hand-drawn badge passed {color} came
175
+ // out as a hollow outline (round 6 U, 1.41). A closed path (closed:true, or
176
+ // pathData ending in Z) now takes color as its fill; an open one keeps the
177
+ // stroke it always had, so existing line drawings are unchanged.
178
+ const pathData = typeof baseProperties.pathData === 'string' ? baseProperties.pathData.trim() : '';
179
+ const closedPath = itemType === 'path'
180
+ && (baseProperties.closed === true || /z$/i.test(pathData));
181
+ if (closedPath && color && !isGradient(color) && fillColor === undefined && strokeColor === undefined) {
182
+ params.fillColor = color;
183
+ }
91
184
  // Handle simple solid colors in params
92
- if (color && !isGradient(color)) {
185
+ if (color && !isGradient(color) && !noFill(color)) {
93
186
  params.color = color;
94
187
  }
95
- if (fillColor && !isGradient(fillColor)) {
188
+ if (fillColor && !isGradient(fillColor) && !noFill(fillColor)) {
96
189
  params.fillColor = fillColor;
97
190
  }
98
191
  if (strokeColor && !isGradient(strokeColor)) {
@@ -126,28 +219,17 @@ function generateCreateItemCode(itemType, position, properties) {
126
219
  // ${itemType} — a render-time surface. Drawn per pixel by the cloud renderer;
127
220
  // stood in for locally by a flat plate so the layout still reads.
128
221
  const item = app.create('rectangle', ${JSON.stringify(plate, null, 2)});
129
- if (item && item.data) { item.data.renderAs = ${JSON.stringify(itemType)}; item.data.renderParams = ${JSON.stringify(params)}; }`;
130
- }
131
- // RTL IS NOT SUPPORTED, AND SILENCE WAS THE WORST WAY TO SAY SO.
132
- //
133
- // create('text', {direction:'rtl'}) dropped the property with no error and
134
- // left the base direction LTR, so an Arabic sentence's final period landed
135
- // at the reader's START — visibly wrong, and attributable to nothing. The
136
- // engine has no text-direction support at all: `direction` there is an arc
137
- // sweep and an animation direction, and `justification` is the only
138
- // text-related alignment it reads.
139
- //
140
- // Refused by name rather than accepted-and-dropped, and rather than adding a
141
- // prop that would be dropped one layer further on. The alignment half is
142
- // real and is named, because it is what a caller can actually use today.
143
- const rtlAsked = params.direction ?? params.textDirection;
144
- if (rtlAsked !== undefined) {
145
- return `
146
- // Create ${itemType} — refused, rather than silently left ltr
147
- ({ success: false, error: ${JSON.stringify(`direction / textDirection is not supported: this studio has no text-direction support, so ${String(rtlAsked)} would have been accepted and the text left LTR. `
148
- + 'That is visible as a right-to-left sentence whose final punctuation lands at the start. '
149
- + "You can align with justification: 'right', which is read, but the BASE direction needs engine support — "
150
- + 'pass pre-shaped text, or split the line, until it lands.')} });`.trim();
222
+ if (item && item.data) { item.data.renderAs = ${JSON.stringify(itemType)}; item.data.renderParams = ${JSON.stringify(params)}; }
223
+ // The snippet's value is its last statement's, which was the assignment above:
224
+ // the caller got renderParams back and no itemId to address the item by.
225
+ // ONE outer paren pair: the governor's wrapTrailingReturn reads a top-level
226
+ // \`}\` as a statement end, so a bare ternary of object literals is not wrapped
227
+ // and its value is lost.
228
+ ((item && item.data)
229
+ ? { itemId: item.data.registryId, type: ${JSON.stringify(itemType)}, position: { x: ${position.x}, y: ${position.y} },
230
+ localStandIn: true,
231
+ note: 'drawn here as a flat plate; the ${itemType} itself appears only in a cloud render. A local export shows the plate.' }
232
+ : { success: false, error: 'the stand-in plate for this ${itemType} was not created.' });`;
151
233
  }
152
234
  // AN UNKNOWN ANCHOR IS INVISIBLE OVER MCP.
153
235
  //
@@ -172,6 +254,17 @@ if (item && item.data) { item.data.renderAs = ${JSON.stringify(itemType)}; item.
172
254
  if (params.origin !== undefined)
173
255
  params.origin = normalized;
174
256
  }
257
+ // AN UNREAD PROPERTY IS DROPPED WITHOUT A WORD — SO SAY THE WORD HERE.
258
+ //
259
+ // `properties` is free-form and create() returns an item whatever it is
260
+ // given, so {blur: 12, backdropFilter: 'blur(8px)'} produced a crisp shape and
261
+ // a successful call; the caller learned only by rendering. The allowlist is
262
+ // generated from the engine (what create() and normalizeParams() read), and
263
+ // the handful this emitter consumes itself are added. Reported back, not
264
+ // refused: the item is still what the caller asked for in every other way,
265
+ // and a refusal would cost a re-issue over one typo.
266
+ const ignored = Object.keys(properties).filter((k) => !CREATE_KNOWN_KEYS.has(k)
267
+ && !(itemType === 'text' && (TEXT_STYLE_KEYS.includes(k) || k === 'fit' || DIRECTION_KEYS.includes(k) || TABULAR_KEYS.includes(k) || k === 'strokePosition')));
175
268
  // Build the code
176
269
  let code = `
177
270
  // Create ${itemType} item
@@ -195,12 +288,51 @@ const item = app.create('${itemType}', ${JSON.stringify(params, null, 2)});`;
195
288
  if (blendMode) {
196
289
  code += '\n' + generateBlendModeCode('item', blendMode);
197
290
  }
291
+ if (clearFill)
292
+ code += `\nitem.fillColor = null;`;
293
+ // DASHES, CAPS AND JOINS FOR EVERY SHAPE (round 7 Z, 1.54). create() applies
294
+ // them only in its path / line / pattern branches; the shared tail sets a
295
+ // stroke's colour and width for every type but not these, so a dashed circle
296
+ // rendered solid and nothing said so (the key is on create()'s read list, so
297
+ // the unread-property report could not catch it). Re-applied here for every
298
+ // type; on a path it repeats the engine's own value.
299
+ const strokeStyle = [];
300
+ if (Array.isArray(properties.dashArray))
301
+ strokeStyle.push(`item.dashArray = ${JSON.stringify(properties.dashArray)};`);
302
+ if (typeof properties.strokeCap === 'string')
303
+ strokeStyle.push(`item.strokeCap = ${JSON.stringify(properties.strokeCap)};`);
304
+ if (typeof properties.strokeJoin === 'string')
305
+ strokeStyle.push(`item.strokeJoin = ${JSON.stringify(properties.strokeJoin)};`);
306
+ if (strokeStyle.length)
307
+ code += `\n${strokeStyle.join('\n')}`;
198
308
  // Add opacity support
199
309
  if (opacity !== undefined) {
200
310
  code += `\nitem.opacity = ${opacity};`;
201
311
  }
312
+ if (itemType === 'text')
313
+ code += emitTextStyle('item', properties);
314
+ // The vertical hold follows the anchor, as the engine's own create step
315
+ // derives it (1.90): without it the fit held the centre, and a top-left
316
+ // anchored headline's top edge drifted on every refit.
317
+ if (textDirection)
318
+ code += emitDirectionCheck('item', textDirection);
319
+ if (textTabular)
320
+ code += emitTabularCheck('item');
321
+ if (textStrokePos)
322
+ code += emitStrokePositionCheck('item', textStrokePos);
323
+ if (textFit)
324
+ code += `\nlet __textFit = null;${emitTextFit('item', textFit, JSON.stringify(anchorHold(properties.anchor ?? properties.origin) ?? null))}`;
325
+ // After opacity, so the "on" level of the lifetime is the item's own.
326
+ const hasLifetime = properties.bornAt !== undefined || properties.ttl !== undefined;
327
+ if (hasLifetime) {
328
+ code += `\nlet __lifetime = null;${emitLifetime('item', properties.bornAt, properties.ttl)}`;
329
+ }
202
330
  // Ensure item is visible above backgrounds/generators
203
331
  code += `\nif (item.bringToFront) item.bringToFront();`;
332
+ // selectable / isDraggable / isDecorative live on item.data, not in params.
333
+ if (dataFlags && Object.keys(dataFlags).length > 0) {
334
+ code += `\nif (item.data) Object.assign(item.data, ${JSON.stringify(dataFlags)});`;
335
+ }
204
336
  // Add dynamic content support for text items
205
337
  if (contentType && itemType === 'text') {
206
338
  const opts = {};
@@ -212,19 +344,440 @@ const item = app.create('${itemType}', ${JSON.stringify(params, null, 2)});`;
212
344
  opts.countdownEndText = countdownEndText;
213
345
  code += `\nif (app.setDynamicContent) app.setDynamicContent(item, '${contentType}', ${JSON.stringify(opts)});`;
214
346
  }
347
+ const loadsFont = itemType === 'text' && typeof properties.fontFamily === 'string' && properties.fontFamily.trim() !== '';
215
348
  code += `
216
349
  const itemId = item.data.registryId;
217
350
  app.historyManager.saveState();
218
351
 
219
352
  // Return item info
220
- ({ itemId, type: '${itemType}', position: { x: ${position.x}, y: ${position.y} } });`;
353
+ ({ itemId, type: '${itemType}'${textFit ? ', textFit: __textFit' : ''}${textDirection ? ', direction: __direction' : ''}${textTabular ? ', tabularFigures: __tabular' : ''}${textStrokePos ? ', strokePosition: __strokePos' : ''}, position: ${omitXY ? `(item.position ? { x: item.position.x, y: item.position.y } : null)` : `{ x: ${position.x}, y: ${position.y} }`}${hasLifetime ? ', lifetime: __lifetime' : ''}${loadsFont ? ', ...(__font ? { font: __font } : {})' : ''}${ignored.length > 0
354
+ ? `, ignoredProperties: ${JSON.stringify(ignored)}, warning: ${JSON.stringify(`${ignored.join(', ')} ${ignored.length > 1 ? 'are' : 'is'} not read when creating an item, so ${ignored.length > 1 ? 'they had' : 'it had'} no effect. `
355
+ + 'Check the spelling against this item type\'s documented properties.')}`
356
+ : ''} });`;
357
+ // Loading a font is async, and it has to happen BEFORE create() measures the
358
+ // text — so only this case becomes an async IIFE; every other create stays a
359
+ // plain snippet ending in its (-led result.
360
+ if (loadsFont) {
361
+ const body = code.trim().replace(/\(\{ itemId([\s\S]*)\}\);$/, 'return { itemId$1 };');
362
+ return `(async function() {${emitEnsureFont(properties.fontFamily)}\n${body}\n})();`;
363
+ }
221
364
  return code.trim();
222
365
  }
366
+ /**
367
+ * Every `properties` key modify_item can act on: what the engine's modify path
368
+ * and normalizeParams() read (generated), plus what generateModifyItemCode
369
+ * applies itself (dynamic content, audio level, lifetime, raster smoothing,
370
+ * the text properties modifyItem does not pass on).
371
+ */
372
+ const MODIFY_KNOWN_KEYS = new Set([
373
+ ...MODIFY_CHANGE_READS,
374
+ ...NORMALIZE_PARAM_READS,
375
+ 'contentType', 'contentFormat', 'countdownTarget', 'countdownEndText',
376
+ 'audioGain', 'volume', 'gain', 'bornAt', 'ttl', 'smoothing',
377
+ 'fontStyle', 'leading', 'lineHeight', 'skewX', 'skewY', 'matrix', 'fit', 'direction', 'textDirection', 'dir', 'tabularFigures', 'fontVariantNumeric', 'fontFeatures', 'strokePosition',
378
+ ]);
379
+ /** Spellings callers reach for, and the key the engine actually reads. */
380
+ const MODIFY_DID_YOU_MEAN = {
381
+ scaling: 'scale', size: 'width / height', rotate: 'rotation', angle: 'rotation', shear: 'skewX / skewY', skew: 'skewX / skewY',
382
+ alpha: 'opacity', text: 'content', stroke_width: 'strokeWidth', lineWidth: 'strokeWidth',
383
+ };
384
+ /**
385
+ * Every `properties` key create_item can act on: what the engine's create()
386
+ * and normalizeParams() read (generated — see shape-params.ts), plus the keys
387
+ * generateCreateItemCode consumes itself before the engine sees them.
388
+ */
389
+ const CREATE_KNOWN_KEYS = new Set([
390
+ ...ACCEPTED_CREATE_PARAMS,
391
+ ...NORMALIZE_PARAM_READS,
392
+ // withRadiusAxes, gradients / shadow / blend / opacity emitted here, and
393
+ // setDynamicContent for text.
394
+ 'radiusX', 'radiusY', 'contentType', 'contentFormat', 'countdownTarget', 'countdownEndText',
395
+ // Lifetimes. The editor's create() does not read them, but they are
396
+ // documented on ANY item and the scene renderer schedules by them — calling
397
+ // them "no effect" would steer a caller off the only way to cut between shots.
398
+ 'bornAt', 'ttl',
399
+ ]);
400
+ /**
401
+ * Load a font family before text is drawn with it, and say if it still is not there.
402
+ *
403
+ * `font list_available` names ~60 families, and drawing with one (Anton,
404
+ * Playfair Display) came out in a serif fallback with a console warning
405
+ * nobody over MCP can read. They are real — the studio's decorative Google
406
+ * Fonts sheet carries them — but it is fetched only when the editor's own UI
407
+ * asks, and text measures its bounds at creation, so a face arriving later
408
+ * leaves the layout measured in the fallback.
409
+ *
410
+ * So: ask checkFont; if missing, ensureFontsLoaded('all') and force the fetch
411
+ * with document.fonts.load (a registered @font-face is not a fetched one),
412
+ * then ask again. `__font` is set only when the first answer was "missing",
413
+ * so an available font adds nothing to the result. Needs an async context.
414
+ */
415
+ export function emitEnsureFont(family) {
416
+ const primary = family.split(',')[0].trim().replace(/^['"]|['"]$/g, '');
417
+ return `
418
+ let __font = null;
419
+ if (typeof app.checkFont === 'function') {
420
+ const __v0 = app.checkFont(${JSON.stringify(family)});
421
+ if (__v0 && __v0.available === false) {
422
+ if (typeof app.ensureFontsLoaded === 'function') { try { await app.ensureFontsLoaded('all'); } catch (_) { /* reported below */ } }
423
+ try { if (typeof document !== 'undefined' && document.fonts) await document.fonts.load('16px ' + ${JSON.stringify(JSON.stringify(primary))}); } catch (_) { /* reported below */ }
424
+ let __v1 = app.checkFont(${JSON.stringify(family)});
425
+ let __via = 'studio';
426
+ // Not in the studio's own sheets (Monoton, a common neon face, is not):
427
+ // the page's CSP allows fonts.googleapis.com stylesheets and
428
+ // fonts.gstatic.com files, so ask Google Fonts for the family directly.
429
+ if (!(__v1 && __v1.available === true) && typeof document !== 'undefined' && document.head) {
430
+ __via = 'google-fonts';
431
+ await new Promise(function(res) {
432
+ const id = 'pp-gf-' + ${JSON.stringify(primary)}.replace(/[^a-z0-9]+/gi, '-');
433
+ if (document.getElementById(id)) return res();
434
+ const l = document.createElement('link');
435
+ l.id = id; l.rel = 'stylesheet';
436
+ l.href = 'https://fonts.googleapis.com/css2?family=' + encodeURIComponent(${JSON.stringify(primary)}).replace(/%20/g, '+') + '&display=swap';
437
+ l.onload = res; l.onerror = res; setTimeout(res, 5000);
438
+ document.head.appendChild(l);
439
+ });
440
+ try { if (document.fonts) await document.fonts.load('16px ' + ${JSON.stringify(JSON.stringify(primary))}); } catch (_) { /* reported below */ }
441
+ __v1 = app.checkFont(${JSON.stringify(family)});
442
+ }
443
+ __font = (__v1 && __v1.available === true)
444
+ ? { family: ${JSON.stringify(primary)}, available: true, loadedNow: true, via: __via }
445
+ : { family: ${JSON.stringify(primary)}, available: false,
446
+ warning: ${JSON.stringify(primary)} + ' could not be loaded, so this text is drawn in a fallback face. '
447
+ + ((__v1 && __v1.reason) || '') + ' font list_available names the families this studio can load.' };
448
+ }
449
+ }`;
450
+ }
451
+ /**
452
+ * Auto-fit a text item to a box (FxTool 661224ef: app.fitText). Text that
453
+ * overran its slot — a German translation, a long name on a certificate, a
454
+ * feed value in a variant matrix — was the #1 blocker in rounds 6–9. The box
455
+ * is remembered by the engine, so later content changes refit. On a studio
456
+ * without fitText the result says so rather than leaving the text as it was
457
+ * in silence. `__textFit` must be declared by the caller.
458
+ */
459
+ /**
460
+ * A text paragraph's base direction (FxTool a3b4e7c3, gap 1.16): 'auto' (the
461
+ * first strong character decides), 'ltr' or 'rtl', under three spellings.
462
+ *
463
+ * Pulled out of the changes and normalised to `direction`, the one spelling
464
+ * the engine reads on both create and modify (modify does not read `dir`). A
465
+ * value that is not a direction is refused here: the engine only console.warns
466
+ * it, which production strips.
467
+ */
468
+ const DIRECTION_KEYS = ['direction', 'textDirection', 'dir'];
469
+ // The engine's rule (FxTool js/core/TextDirection.js, UAX #9 P2/P3): the
470
+ // first STRONG character decides. Mirrored so a result can say which way
471
+ // 'auto' went.
472
+ const RTL_SCRIPT = /[\p{Script=Hebrew}\p{Script=Arabic}\p{Script=Syriac}\p{Script=Thaana}\p{Script=Nko}\p{Script=Samaritan}\p{Script=Mandaic}\p{Script=Adlam}\p{Script=Hanifi_Rohingya}]/u;
473
+ function baseDirection(text) {
474
+ for (const ch of text) {
475
+ if (RTL_SCRIPT.test(ch))
476
+ return 'rtl';
477
+ if (/\p{L}/u.test(ch))
478
+ return 'ltr';
479
+ }
480
+ return 'ltr';
481
+ }
482
+ function takeDirection(props) {
483
+ const key = DIRECTION_KEYS.find((k) => props[k] !== undefined && props[k] !== null && props[k] !== '');
484
+ const raw = key ? props[key] : undefined;
485
+ for (const k of DIRECTION_KEYS)
486
+ delete props[k];
487
+ if (raw === undefined)
488
+ return null;
489
+ const v = String(raw).trim().toLowerCase();
490
+ if (v === 'auto' || v === 'ltr' || v === 'rtl') {
491
+ props.direction = v;
492
+ return { value: v };
493
+ }
494
+ return { error: `direction ${JSON.stringify(String(raw))} is not one of auto, ltr, rtl — the text keeps its current direction.` };
495
+ }
496
+ /**
497
+ * Sets `__direction` (declared here) to whether the studio took the direction:
498
+ * an engine without RTL support accepts the key and draws left-to-right, so
499
+ * the only evidence is the value it records on the item.
500
+ */
501
+ function emitDirectionCheck(itemExpr, req) {
502
+ if ('error' in req)
503
+ return `
504
+ const __direction = ${JSON.stringify({ applied: false, error: req.error })};`;
505
+ const v = JSON.stringify(req.value);
506
+ return `
507
+ const __direction = (function(it) {
508
+ if (!it || it.className !== 'PointText') return { applied: false, note: 'direction applies to text items only.' };
509
+ return it.data && it.data.direction === ${v}
510
+ ? { applied: true, value: ${v}${req.resolved ? `, resolved: ${JSON.stringify(req.resolved)}` : ''} }
511
+ : { applied: false, note: 'this studio does not set a paragraph direction (it needs an engine with RTL text support): the text is laid out left-to-right.' };
512
+ })(${itemExpr});`;
513
+ }
514
+ /**
515
+ * Tabular (fixed-width) figures (FxTool 3354d51c): a counter or price column
516
+ * that does not jitter as its digits change. The engine reads all three
517
+ * spellings on create and modify itself; this only reports whether it took
518
+ * them, because an engine without them accepts the key and draws
519
+ * proportional digits.
520
+ */
521
+ const TABULAR_KEYS = ['tabularFigures', 'fontVariantNumeric', 'fontFeatures'];
522
+ function wantsTabular(p) {
523
+ if (!TABULAR_KEYS.some((k) => p[k] !== undefined))
524
+ return null;
525
+ if (p.tabularFigures === true)
526
+ return true;
527
+ if (typeof p.fontVariantNumeric === 'string' && /tabular-nums/.test(p.fontVariantNumeric))
528
+ return true;
529
+ const f = p.fontFeatures;
530
+ const list = Array.isArray(f) ? f.map(String) : typeof f === 'string' ? [f] : f && typeof f === 'object' ? Object.keys(f).filter((k) => f[k]) : [];
531
+ return list.some((x) => /\btnum\b/.test(x));
532
+ }
533
+ function emitTabularCheck(itemExpr) {
534
+ return `
535
+ const __tabular = (function(it) {
536
+ if (!it || it.className !== 'PointText') return { applied: false, note: 'tabular figures apply to text items only.' };
537
+ return it.data && it.data.tabularFigures === true
538
+ ? { applied: true }
539
+ : { applied: false, note: 'this studio does not lay out tabular figures (it needs an engine with them): digits keep their proportional widths. A monospaced font is the fallback.' };
540
+ })(${itemExpr});`;
541
+ }
542
+ function takeStrokePosition(props, allowNull) {
543
+ if (!('strokePosition' in props))
544
+ return null;
545
+ const raw = props.strokePosition;
546
+ if (raw === null && allowNull) {
547
+ props.strokePosition = 'center';
548
+ return { value: 'center' };
549
+ }
550
+ const v = String(raw ?? '').trim().toLowerCase();
551
+ if (v === 'outside') {
552
+ props.strokePosition = 'outside';
553
+ return { value: 'outside' };
554
+ }
555
+ if (v === 'center' || v === 'centre') {
556
+ props.strokePosition = 'center';
557
+ return { value: 'center' };
558
+ }
559
+ delete props.strokePosition;
560
+ return { error: `strokePosition ${JSON.stringify(raw)} is not 'outside' or 'center' — the stroke is unchanged.` };
561
+ }
562
+ function emitStrokePositionCheck(itemExpr, req) {
563
+ if ('error' in req)
564
+ return `\nconst __strokePos = ${JSON.stringify({ applied: false, error: req.error })};`;
565
+ if (req.value === 'center')
566
+ return `\nconst __strokePos = { applied: true, value: 'center' };`;
567
+ return `
568
+ const __strokePos = (function(it) {
569
+ if (!it || it.className !== 'PointText') return { applied: false, note: 'strokePosition applies to text items only.' };
570
+ if (!(it.data && it.data.strokePosition === 'outside')) return { applied: false, note: 'this studio does not draw text strokes outside the glyph (it needs an engine with strokePosition): the stroke is centred on the outline, so a thick stroke thins the letters.' };
571
+ return (it.strokeColor && it.strokeWidth > 0) ? { applied: true, value: 'outside' } : { applied: true, value: 'outside', note: 'the text has no stroke yet: set strokeColor and strokeWidth to see it.' };
572
+ })(${itemExpr});`;
573
+ }
574
+ /** 'top' / 'bottom' from a top-* / bottom-* anchor, as the engine derives it. */
575
+ function anchorHold(anchor) {
576
+ const a = String(anchor ?? '').toLowerCase();
577
+ return /^top/.test(a) ? 'top' : /^bottom/.test(a) ? 'bottom' : undefined;
578
+ }
579
+ function emitTextFit(itemExpr, box, holdFallback = 'undefined') {
580
+ return `
581
+ if (typeof app.fitText === 'function') {
582
+ const __fbox = ${JSON.stringify(box)};
583
+ if (__fbox.hold === undefined) { const __h = ${holdFallback}; if (__h) __fbox.hold = __h; }
584
+ const __fr = app.fitText(${itemExpr}, __fbox);
585
+ __textFit = __fr && __fr.ok !== false
586
+ ? Object.assign({ applied: true }, __fr, __fr.fits === false ? { warning: 'the text does not fit its box even at the minimum font size — shorten it or widen the box.' } : {})
587
+ : { applied: false, error: (__fr && __fr.error) || 'the studio refused the fit box' };
588
+ } else {
589
+ __textFit = { applied: false, note: 'this studio cannot auto-fit text (no app.fitText): the text keeps its font size and may overrun the box.' };
590
+ }`;
591
+ }
592
+ /**
593
+ * Paper text properties the engine's create() and modifyItem() never pass on.
594
+ *
595
+ * Line spacing, weight and italic are what editorial, Swiss and zine layouts
596
+ * are made of, and all three were dropped without a word: create('text')
597
+ * builds a PointText from content / fontFamily / fontSize / justification
598
+ * only. PointText itself supports them (`leading`, and `fontWeight`, which
599
+ * Paper writes into the canvas font shorthand — so italic rides in it as
600
+ * "italic 700"). Set after the fact, then re-anchored to the edge the engine
601
+ * anchored to, because a heavier weight or taller leading moves the bounds.
602
+ *
603
+ * lineHeight is the CSS habit: <= 4 is a multiple of fontSize, above that px.
604
+ */
605
+ const TEXT_STYLE_KEYS = ['fontWeight', 'fontStyle', 'leading', 'lineHeight'];
606
+ function emitTextStyle(itemExpr, props) {
607
+ const weight = props.fontWeight;
608
+ const italic = props.fontStyle === 'italic' || props.fontStyle === 'oblique';
609
+ const lh = typeof props.lineHeight === 'number' ? props.lineHeight : undefined;
610
+ const leading = typeof props.leading === 'number' ? props.leading : undefined;
611
+ if (weight === undefined && props.fontStyle === undefined && leading === undefined && lh === undefined)
612
+ return '';
613
+ return `
614
+ (function(it) {
615
+ if (!it || it.className !== 'PointText') return;
616
+ const j = it.justification;
617
+ const edge = function(b) { return j === 'left' ? b.left : j === 'right' ? b.right : b.center.x; };
618
+ const b0 = it.bounds, ax = edge(b0), ay = b0.center.y;
619
+ ${weight !== undefined || props.fontStyle !== undefined
620
+ ? `it.fontWeight = ${JSON.stringify(`${italic ? 'italic ' : ''}${weight ?? 'normal'}`)};`
621
+ : ''}
622
+ ${leading !== undefined ? `it.leading = ${leading};` : ''}
623
+ ${lh !== undefined ? `it.leading = ${lh} <= 4 ? ${lh} * it.fontSize : ${lh};` : ''}
624
+ const b1 = it.bounds;
625
+ it.position = it.position.add([ax - edge(b1), ay - b1.center.y]);
626
+ })(${itemExpr});`;
627
+ }
628
+ /**
629
+ * Which data keys a chart plots, when the caller did not say.
630
+ *
631
+ * ChartSystem defaults to fields named `x` and `y`. The tool's own documented
632
+ * example is [{category, value}], so that example drew NO bars — index labels
633
+ * "0 1 2 3 4" and success:true, dataPoints:5 (round 7 Z, 1.53). Inferred here
634
+ * from the data instead: x is a label-like key (category/label/name/…, else
635
+ * the first text key), y is value-like (value/count/…, else the first number
636
+ * key other than x). stackedBar picks its own series from every numeric key,
637
+ * so only x is inferred for it. A y that cannot be found is refused by name,
638
+ * listing the keys that were there, rather than drawn as nothing.
639
+ */
640
+ function resolveChartFields(chartType, data, options) {
641
+ // No rows yet (a chart created empty and filled by update): nothing to infer.
642
+ if (data.length === 0)
643
+ return { options };
644
+ const row = data.find((d) => d && typeof d === 'object') ?? {};
645
+ const keys = Object.keys(row);
646
+ const out = { ...options };
647
+ const inferred = {};
648
+ const has = (k) => keys.includes(k);
649
+ if (out.xField === undefined && !has('x')) {
650
+ const x = ['category', 'label', 'name', 'date', 'month', 'year', 'key'].find(has)
651
+ ?? keys.find((k) => typeof row[k] === 'string') ?? keys[0];
652
+ if (x !== undefined) {
653
+ out.xField = x;
654
+ inferred.xField = x;
655
+ }
656
+ }
657
+ const xf = out.xField ?? 'x';
658
+ if (chartType !== 'stackedBar' && out.yField === undefined && !has('y')) {
659
+ const numeric = keys.filter((k) => k !== xf && typeof row[k] === 'number');
660
+ const y = ['value', 'count', 'amount', 'total', 'score'].find((k) => numeric.includes(k)) ?? numeric[0];
661
+ if (y === undefined) {
662
+ return { error: `no numeric field to plot: the data rows have ${keys.length ? keys.join(', ') : 'no keys'}, and none but ${xf} is a number. Pass options.yField naming the numeric key.` };
663
+ }
664
+ out.yField = y;
665
+ inferred.yField = y;
666
+ }
667
+ return Object.keys(inferred).length ? { options: out, inferred } : { options: out };
668
+ }
669
+ /**
670
+ * A lifetime (bornAt / ttl, seconds) as a VISIBILITY WINDOW, [bornAt, bornAt + ttl).
671
+ *
672
+ * THE ENGINE HAS NO LIFETIME, and the first answer here (5bfc480) wrote one as
673
+ * hard-cut opacity keys merged into the item's own track. That failed four
674
+ * ways once items also had keyframes (ads production): keyframe_animate
675
+ * replaced the track and the ttl cut was lost (1.83); opacity-only cut keys
676
+ * broke the position track through the engine's missing-property rule (2.30 →
677
+ * 1.84); authored opacity and the cuts blended instead of multiplying (1.85);
678
+ * and exact-time hand-offs left a blank frame (1.86).
679
+ *
680
+ * So it is no longer keys. ONE frame callback ('pp_lifetimes') sets
681
+ * item.visible from the window for every item with a lifetime, reading each
682
+ * item's live data.bornAt / ttl, from the frame's own time: event.sceneTime,
683
+ * which both the export's step and sceneAt supply, else app.playbackTime. The
684
+ * export runs frame callbacks inside app.update() before it draws each step
685
+ * (VideoExporter._advanceToTime), so exports honour it. visible, not opacity,
686
+ * so it composes with any keyframes — the engine's own clip window hides the
687
+ * same way for the same reason. A 1-microsecond tolerance makes a hand-off at
688
+ * an exact frame time show exactly one item.
689
+ *
690
+ * Runtime-only: a scene saved and reopened keeps data.bornAt / ttl (the cloud
691
+ * renderer reads those) but not the callback, until the studio reads the
692
+ * fields itself — the result says so. Lifetimes applied the old way are
693
+ * migrated: their tagged opacity keys are removed.
694
+ */
695
+ function emitLifetime(itemExpr, bornAt, ttl) {
696
+ const b = Number(bornAt);
697
+ const t = Number(ttl);
698
+ const born = Number.isFinite(b) && b > 0 ? b : 0;
699
+ const span = Number.isFinite(t) && t > 0 ? t : null;
700
+ return `
701
+ (function(it) {
702
+ if (!it || !it.data) return;
703
+ it.data.bornAt = ${born};
704
+ ${span === null ? 'delete it.data.ttl;' : `it.data.ttl = ${span};`}
705
+ if (Array.isArray(it.data.keyframes) && it.data.keyframes.some(function(k) { return k && k._lifetime; })) {
706
+ it.data.keyframes = it.data.keyframes.filter(function(k) { return !(k && k._lifetime); });
707
+ }
708
+ // THE STUDIO'S OWN LIFETIME, WHEN IT HAS ONE (FxTool bf1a656b): same window,
709
+ // same tolerance, applied in the frame loop, on scrub and in export — and it
710
+ // PERSISTS through save / reload, which the callback below cannot. Passed
711
+ // straight through; the callback is only the fallback for older studios.
712
+ if (typeof app.setLifetime === 'function') {
713
+ const __r = app.setLifetime(it, ${born}, ${span === null ? 'null' : span});
714
+ if (!__r || __r.ok === false) {
715
+ __lifetime = { bornAt: ${born}, ttl: ${span}, applied: false, error: (__r && __r.error) || 'the studio refused the lifetime' };
716
+ return;
717
+ }
718
+ const __G = globalThis.__ppMcp;
719
+ if (__G && __G.lifetimeApp === app && __G.lifetimeIds) __G.lifetimeIds.delete(it.data.registryId || it.data.id);
720
+ __lifetime = { bornAt: ${born}, ttl: ${span}, applied: true, via: 'engine', persists: true };
721
+ return;
722
+ }
723
+ const rid = it.data.registryId || it.data.id;
724
+ // MCP-owned page state lives on globalThis, not on app: it is not an engine
725
+ // member, and the engine-surface guard reads app.X as a claim that it is.
726
+ const __pp = globalThis.__ppMcp = globalThis.__ppMcp || {};
727
+ // Keyed to THIS studio: a re-initialised app has none of the old one's
728
+ // callbacks, so the registration and the id set start again with it.
729
+ if (__pp.lifetimeApp !== app) { __pp.lifetimeApp = app; __pp.lifetimeIds = new Set(); }
730
+ if (typeof app.addOnFrameCallback !== 'function' || !rid) {
731
+ __lifetime = { bornAt: ${born}, ttl: ${span}, applied: false, note: 'stored on the item, but this studio has no frame callbacks, so it shows for the whole local render (a cloud render honours it).' };
732
+ return;
733
+ }
734
+ __pp.lifetimeIds = __pp.lifetimeIds || new Set();
735
+ __pp.lifetimeIds.add(rid);
736
+ const within = function(x, now) {
737
+ const b0 = x.data.bornAt, t0 = x.data.ttl;
738
+ return now >= b0 - 1e-6 && (typeof t0 !== 'number' || now < b0 + t0 - 1e-6);
739
+ };
740
+ // The visibility pass itself, kept where a still export can call it too
741
+ // (a seek alone does not run frame callbacks).
742
+ __pp.applyLifetimes = function(now) {
743
+ __pp.lifetimeIds.forEach(function(id) {
744
+ const e = app.itemRegistry && app.itemRegistry.get(id);
745
+ const x = e && e.item;
746
+ if (!x || !x.data || typeof x.data.bornAt !== 'number') { __pp.lifetimeIds.delete(id); return; }
747
+ const vis = within(x, now);
748
+ if (x.visible !== vis) x.visible = vis;
749
+ });
750
+ };
751
+ // Registered EVERY time, not once behind a flag: clearCanvas / start_job
752
+ // remove frame callbacks, and a stale "already registered" flag left every
753
+ // later scene's lifetimes doing nothing (retest of 28a4a0e). Re-adding the
754
+ // same id replaces it.
755
+ app.addOnFrameCallback('pp_lifetimes', function(ev) {
756
+ const now = ev && typeof ev.sceneTime === 'number' ? ev.sceneTime : (typeof app.playbackTime === 'number' ? app.playbackTime : 0);
757
+ __pp.applyLifetimes(now);
758
+ });
759
+ it.visible = within(it, typeof app.playbackTime === 'number' ? app.playbackTime : 0);
760
+ __lifetime = { bornAt: ${born}, ttl: ${span}, applied: true, via: 'visibility',
761
+ note: 'applied as visibility, so it composes with keyframes. Runtime-only until the studio reads bornAt / ttl itself: a scene reopened in the editor keeps the fields (and a cloud render honours them) but must be re-timed to play locally.' };
762
+ if (it.data.clipInPoint !== undefined || it.data.clipOutPoint !== undefined) {
763
+ __lifetime.warning = 'this item also has a clip window (clipInPoint / clipOutPoint), which hides it by the same means — the two can fight. Use one.';
764
+ }
765
+ })(${itemExpr});`;
766
+ }
223
767
  /**
224
768
  * Template for modifying items
225
769
  */
226
- function generateModifyItemCode(itemId, properties) {
770
+ function generateModifyItemCode(itemId, properties, dataFlags) {
227
771
  const { contentType, contentFormat, countdownTarget, countdownEndText, ...restProperties } = properties;
772
+ // A new fit box is applied here (emitTextFit, with the hold carried over),
773
+ // not by modifyItem: the engine's modify fits it with no hold, overwriting
774
+ // the one this reads back. fit: null still goes through — that is the
775
+ // engine's "stop fitting".
776
+ if (restProperties.fit && typeof restProperties.fit === 'object')
777
+ delete restProperties.fit;
778
+ const modDirection = takeDirection(restProperties);
779
+ const modTabular = wantsTabular(restProperties) === true;
780
+ const modStrokePos = takeStrokePosition(restProperties, true);
228
781
  // modifyItem(id, changes), NOT select() + modify().
229
782
  //
230
783
  // modify() operates on the SELECTION, so this selected an id and then edited
@@ -269,11 +822,101 @@ const entry = app.itemRegistry.get('${itemId}');
269
822
  if (entry && entry.item && app.setDynamicContent) app.setDynamicContent(entry.item, '${contentType}', ${JSON.stringify(opts)});`;
270
823
  }
271
824
  }
825
+ // AN AUDIO ITEM'S LEVEL IS NOT AN ITEM PROPERTY. modifyItem applies changes
826
+ // to the Paper item (an audio item's is an invisible sentinel), so
827
+ // {audioGain} / {volume} answered success and moved nothing (measured 0.0 dB).
828
+ // The mix reads registry properties.audioGain; the live player has its own
829
+ // volume. Both are set, and gain is kept in step for the editor.
830
+ const levelKey = ['audioGain', 'volume', 'gain'].find((k) => typeof properties[k] === 'number');
831
+ if (levelKey) {
832
+ const level = Math.max(0, Math.min(1, properties[levelKey]));
833
+ code += `
834
+ const _ae = app.itemRegistry.get('${itemId}');
835
+ if (_ae && _ae.type === 'audio') {
836
+ _ae.properties = _ae.properties || {};
837
+ _ae.properties.audioGain = ${level};
838
+ _ae.properties.gain = ${level};
839
+ const _A = (typeof window !== 'undefined') && window.PinePaperAgent;
840
+ const _m = _A && typeof _A.listMedia === 'function' ? _A.listMedia().find(function(x) { return x.registryId === '${itemId}'; }) : null;
841
+ if (_m && app.audioLayer && typeof app.audioLayer.setVolume === 'function') app.audioLayer.setVolume(${level}, _m.id);
842
+ }`;
843
+ }
844
+ // Raster resampling (pixel art: 'off'). A Paper Raster property modifyItem
845
+ // does not pass on; applied to a raster, or to the raster an image group holds.
846
+ if (typeof properties.smoothing === 'string') {
847
+ code += `
848
+ const _sm = app.itemRegistry.get('${itemId}');
849
+ const _smR = _sm && _sm.item && (_sm.item.className === 'Raster' ? _sm.item
850
+ : (typeof _sm.item.getItem === 'function' ? _sm.item.getItem({ className: 'Raster' }) : null));
851
+ if (_smR) _smR.smoothing = ${JSON.stringify(properties.smoothing)};`;
852
+ }
853
+ // No fill on modify too: null / 'transparent' / 'none' clear it outright,
854
+ // after modifyItem, which would otherwise read 'transparent' as black.
855
+ const fillNone = (v) => v === null || (typeof v === 'string' && /^(transparent|none)$/i.test(v.trim()));
856
+ if (fillNone(properties.fillColor) || (properties.fillColor === undefined && properties.color !== undefined && fillNone(properties.color))) {
857
+ code += `
858
+ const _nf = app.itemRegistry.get('${itemId}');
859
+ if (_nf && _nf.item) _nf.item.fillColor = null;`;
860
+ }
861
+ // SKEW AND AFFINE, FOR MOCKUPS (round 9 HH). A box face or a phone screen in
862
+ // perspective-ish mockups is a shear / affine map; modify_item reported
863
+ // skewX / shear / matrix as ignored and the only route was custom code.
864
+ // Paper does both: item.shear(tan x, tan y) about the centre, and
865
+ // item.transform(Matrix). They are APPLIED to the current shape — a second
866
+ // call compounds — not stored as settable properties; the result says so.
867
+ const skewX = typeof properties.skewX === 'number' ? properties.skewX : 0;
868
+ const skewY = typeof properties.skewY === 'number' ? properties.skewY : 0;
869
+ const matrix = Array.isArray(properties.matrix) && properties.matrix.length === 6 && properties.matrix.every((n) => typeof n === 'number')
870
+ ? properties.matrix : null;
871
+ const affine = skewX !== 0 || skewY !== 0 || matrix !== null;
872
+ if (affine) {
873
+ code += `
874
+ const _af = app.itemRegistry.get('${itemId}');
875
+ const _afi = _af && _af.item;
876
+ if (_afi) {
877
+ ${skewX !== 0 || skewY !== 0 ? `_afi.shear(Math.tan(${skewX} * Math.PI / 180), Math.tan(${skewY} * Math.PI / 180), _afi.bounds.center);` : ''}
878
+ ${matrix ? `_afi.transform(new paper.Matrix(${matrix.join(', ')}));` : ''}
879
+ }`;
880
+ }
881
+ const modFit = properties.fit && typeof properties.fit === 'object' ? properties.fit : null;
882
+ if (modFit) {
883
+ code += `
884
+ let __textFit = null;
885
+ const _tf = app.itemRegistry.get('${itemId}');${emitTextFit('_tf && _tf.item', modFit, JSON.stringify(anchorHold(properties.anchor ?? properties.origin) ?? null) + " || (_tf && _tf.item && _tf.item.data && _tf.item.data.fitBox && _tf.item.data.fitBox.hold)")}`;
886
+ }
887
+ const textStyle = emitTextStyle('_ts && _ts.item', properties);
888
+ if (textStyle) {
889
+ code += `
890
+ const _ts = app.itemRegistry.get('${itemId}');${textStyle}`;
891
+ }
892
+ const modLifetime = properties.bornAt !== undefined || properties.ttl !== undefined;
893
+ if (modLifetime) {
894
+ code += `
895
+ let __lifetime = null;
896
+ const _lt = app.itemRegistry.get('${itemId}');${emitLifetime('_lt && _lt.item', properties.bornAt, properties.ttl)}`;
897
+ }
898
+ if (dataFlags && Object.keys(dataFlags).length > 0) {
899
+ code += `
900
+ const _flagged = app.itemRegistry.get('${itemId}');
901
+ if (_flagged && _flagged.item && _flagged.item.data) Object.assign(_flagged.item.data, ${JSON.stringify(dataFlags)});`;
902
+ }
903
+ const modFont = typeof properties.fontFamily === 'string' && properties.fontFamily.trim() !== '';
904
+ // AN UNREAD KEY HAD NO EFFECT, AND THE CALL SAID SUCCESS. {scaling: 8} on a
905
+ // raster answered success and left it 16 px; the engine reads `scale`. Same
906
+ // report create_item gives, against the engine's modify path (generated).
907
+ const modIgnored = Object.keys(properties).filter((k) => !MODIFY_KNOWN_KEYS.has(k));
908
+ const modWarning = modIgnored.length === 0 ? '' : `, ignoredProperties: ${JSON.stringify(modIgnored)}, warning: ${JSON.stringify(`${modIgnored.join(', ')} ${modIgnored.length > 1 ? 'are' : 'is'} not read when modifying an item, so ${modIgnored.length > 1 ? 'they had' : 'it had'} no effect.`
909
+ + modIgnored.filter((k) => MODIFY_DID_YOU_MEAN[k]).map((k) => ` Did you mean ${MODIFY_DID_YOU_MEAN[k]} (not ${k})?`).join(''))}`;
272
910
  code += `
273
911
  app.historyManager.saveState();
274
- return { success: true, itemId: '${itemId}' };`;
912
+ ${modDirection ? emitDirectionCheck(`app.getItemById('${itemId}')`, modDirection) : ''}${modTabular ? emitTabularCheck(`app.getItemById('${itemId}')`) : ''}${modStrokePos ? emitStrokePositionCheck(`app.getItemById('${itemId}')`, modStrokePos) : ''}
913
+ return { success: true, itemId: '${itemId}'${modFit ? ', textFit: __textFit' : ''}${modDirection ? ', direction: __direction' : ''}${modTabular ? ', tabularFigures: __tabular' : ''}${modStrokePos ? ', strokePosition: __strokePos' : ''}${affine ? `, affine: ${JSON.stringify({ skewX, skewY, matrix, note: 'applied to the current shape: calling again compounds it. To undo, apply the inverse (negative skew, or the inverse matrix).' })}` : ''}${modLifetime ? ', lifetime: __lifetime' : ''}${modFont ? ', ...(__font ? { font: __font } : {})' : ''}${modWarning} };`;
275
914
  // Wrapped, because the body now RETURNS early on a miss rather than falling
276
- // through to an unconditional success.
915
+ // through to an unconditional success. Async only when a font must load
916
+ // before modifyItem re-measures the text.
917
+ if (modFont) {
918
+ return `(async function() {${emitEnsureFont(properties.fontFamily)}\n${code.trim()}\n})();`;
919
+ }
277
920
  return `(function() {\n${code.trim()}\n})();`;
278
921
  }
279
922
  /**
@@ -310,18 +953,143 @@ function generateDeleteItemCode(itemId) {
310
953
  * Template for adding relations
311
954
  */
312
955
  function generateAddRelationCode(sourceId, targetId, relationType, params) {
956
+ // AN UNKNOWN ENDPOINT IS REFUSED, NOT STORED (round 9 GG, 1.74). The engine
957
+ // stores a relation to an id it cannot resolve, returns true, and says "it
958
+ // will be skipped every frame and nothing will move" only in a warning this
959
+ // tool never sees. Its own test for that warning — _isKnownRelationEndpoint:
960
+ // an item, the camera, a cursor target — is asked first here, after the same
961
+ // '#id' forgiveness the engine applies.
313
962
  return `
314
963
  // Add ${relationType} relation: ${sourceId} -> ${targetId}
315
- const success = app.addRelation('${sourceId}', '${targetId}', '${relationType}', ${JSON.stringify(params, null, 2)});
316
- if (!success) {
317
- throw new Error('Failed to add relation');
318
- }
319
- app.historyManager.saveState();
320
-
321
- // Return success
322
- ({ success: true, sourceId: '${sourceId}', targetId: '${targetId}', relationType: '${relationType}' });
964
+ (function() {
965
+ const norm = function(id) { return typeof app._resolveRelationEndpoint === 'function' ? app._resolveRelationEndpoint(id) : id; };
966
+ if (typeof app._isKnownRelationEndpoint === 'function') {
967
+ const bad = [${JSON.stringify(sourceId)}, ${JSON.stringify(targetId)}].filter(function(id) { return !app._isKnownRelationEndpoint(norm(id)); });
968
+ if (bad.length) {
969
+ return { success: false, relationType: '${relationType}', error: bad.map(function(id) { return JSON.stringify(id); }).join(' and ')
970
+ + ' ' + (bad.length > 1 ? 'are not items' : 'is not an item') + ' on the canvas, so the relation would be stored and skipped every frame. Check the id (pinepaper_get_items lists them).' };
971
+ }
972
+ }
973
+ const success = app.addRelation('${sourceId}', '${targetId}', '${relationType}', ${JSON.stringify(params, null, 2)});
974
+ if (!success) {
975
+ return { success: false, relationType: '${relationType}', error: 'the studio refused the ${relationType} relation between ${sourceId} and ${targetId}.' };
976
+ }
977
+ app.historyManager.saveState();
978
+ return { success: true, sourceId: '${sourceId}', targetId: '${targetId}', relationType: '${relationType}' };
979
+ })();
323
980
  `.trim();
324
981
  }
982
+ /**
983
+ * moves_along_path, from the parameters a caller actually writes.
984
+ *
985
+ * The rule is a SELF relation reading params.path as a point array, with speed
986
+ * (1 = 150 px/s), closed, phase, easing, signal, equation. The natural call —
987
+ * targetId: <a path item>, params {pathId, duration, delay} — named none of
988
+ * those, so the relation had no path, returned success:true, and the whiteboard
989
+ * hand never moved (round 7 AA, 2.25). Here:
990
+ * - a path ITEM (targetId other than the source, or params.pathId) is
991
+ * sampled into points in the page, closed taken from the item;
992
+ * - duration (seconds for one traversal) becomes speed;
993
+ * - delay becomes the relation's window start (relation time is
994
+ * window-relative, so the ride starts at `delay`);
995
+ * - with neither points, a path item nor an equation, it is refused by name.
996
+ */
997
+ function generateMovesAlongPathCode(sourceId, targetId, raw) {
998
+ const { pathId, duration, delay, offset, ...params } = raw;
999
+ const ref = (typeof pathId === 'string' && pathId) || (targetId && targetId !== sourceId ? targetId : null);
1000
+ const off = Array.isArray(offset) && offset.length >= 2 ? [Number(offset[0]) || 0, Number(offset[1]) || 0] : null;
1001
+ const hasDuration = typeof duration === 'number' && duration > 0;
1002
+ const hasDelay = typeof delay === 'number' && delay > 0;
1003
+ return `
1004
+ // moves_along_path: ${sourceId}${ref ? ` along ${ref}` : ''}
1005
+ (function() {
1006
+ const params = ${JSON.stringify(params)};
1007
+ const hasPoints = Array.isArray(params.path) && params.path.length >= 2;
1008
+ // A STUDIO THAT RIDES THE PATH ITSELF. From FxTool c16c9dd3 the rule
1009
+ // declares duration / delay / offset and, with a path item as its target and
1010
+ // no points, follows that path's LIVE geometry on the playhead. Sampling it
1011
+ // into static points here would shadow that, so on such a build everything
1012
+ // is passed straight through and the target is the route.
1013
+ const rule = app.relationRegistry && typeof app.relationRegistry.getRule === 'function'
1014
+ ? app.relationRegistry.getRule('moves_along_path') : null;
1015
+ const native = !!(rule && rule.params && rule.params.duration);
1016
+ if (native && !hasPoints && !params.equation ${ref ? '' : '&& false'}) {
1017
+ ${hasDuration ? `params.duration = ${duration};` : ''}
1018
+ ${hasDelay ? `params.delay = ${delay};` : ''}
1019
+ ${off ? `params.offset = ${JSON.stringify(off)};` : ''}
1020
+ const okN = app.addRelation(${JSON.stringify(sourceId)}, ${JSON.stringify(ref ?? sourceId)}, 'moves_along_path', params);
1021
+ if (!okN) return { success: false, error: 'the studio refused the moves_along_path relation.' };
1022
+ app.historyManager.saveState();
1023
+ return { success: true, sourceId: ${JSON.stringify(sourceId)}, relationType: 'moves_along_path', route: 'live-path', target: ${JSON.stringify(ref)}, params: params };
1024
+ }
1025
+ if (!hasPoints && !params.equation) {
1026
+ ${ref ? `const e = app.itemRegistry && app.itemRegistry.get(${JSON.stringify(ref)});
1027
+ let p = e && e.item;
1028
+ if (p && p.className !== 'Path' && p.className !== 'CompoundPath' && typeof p.getItem === 'function') {
1029
+ p = p.getItem({ className: 'Path' });
1030
+ }
1031
+ if (!p || typeof p.getPointAt !== 'function' || !(p.length > 0)) {
1032
+ return { success: false, error: ${JSON.stringify(ref)} + ' is not a path with length — moves_along_path needs a path item, or params.path as points [[x, y], …].' };
1033
+ }
1034
+ const N = Math.min(240, Math.max(16, Math.ceil(p.length / 6)));
1035
+ params.path = Array.from({ length: N + 1 }, function(_, i) { const q = p.getPointAt(Math.min(p.length, p.length * i / N)); return [q.x, q.y]; });
1036
+ if (params.closed === undefined) params.closed = !!p.closed;` : `return { success: false, error: 'moves_along_path needs a path: pass targetId (or params.pathId) naming a path item, or params.path as points [[x, y], …], or params.equation. With none, the item would not move.' };`}
1037
+ }
1038
+ ${off ? `// offset, with the ENGINE's meaning (c16c9dd3): [dx, dy] is added to the
1039
+ // point on the path to place the item — so a hand whose pen tip sits
1040
+ // (-dx, -dy) from its centre keeps the tip on the stroke. The same value
1041
+ // therefore behaves the same on a build that reads offset natively.
1042
+ if (Array.isArray(params.path)) params.path = params.path.map(function(q) {
1043
+ const x = Array.isArray(q) ? q[0] : q.x, y = Array.isArray(q) ? q[1] : q.y;
1044
+ return [x + ${off[0]}, y + ${off[1]}];
1045
+ });` : ''}
1046
+ ${hasDuration ? `if (params.speed === undefined && Array.isArray(params.path)) {
1047
+ const pts = params.path.map(function(q) { return Array.isArray(q) ? { x: q[0], y: q[1] } : q; });
1048
+ const len = pts.slice(1).reduce(function(acc, q, i) { return acc + Math.hypot(q.x - pts[i].x, q.y - pts[i].y); }, 0);
1049
+ if (len > 0) params.speed = len / (150 * ${duration});
1050
+ if (params.closed === undefined) params.closed = false;
1051
+ }` : ''}
1052
+ ${hasDelay ? `if (!params.window) params.window = { start: ${delay} };` : ''}
1053
+ // Before its window opens the relation does not run, so the item stays where
1054
+ // it was left — the retest saw it parked at the path END. Put it at the start.
1055
+ if (Array.isArray(params.path) && params.path.length) {
1056
+ const src = app.itemRegistry && app.itemRegistry.get(${JSON.stringify(sourceId)});
1057
+ const q0 = params.path[0];
1058
+ if (src && src.item && src.item.position) { src.item.position.x = Array.isArray(q0) ? q0[0] : q0.x; src.item.position.y = Array.isArray(q0) ? q0[1] : q0.y; }
1059
+ }
1060
+ const ok = app.addRelation(${JSON.stringify(sourceId)}, ${JSON.stringify(sourceId)}, 'moves_along_path', params);
1061
+ if (!ok) return { success: false, error: 'the studio refused the moves_along_path relation.' };
1062
+ app.historyManager.saveState();
1063
+ return { success: true, sourceId: ${JSON.stringify(sourceId)}, relationType: 'moves_along_path', route: 'sampled-points',
1064
+ points: Array.isArray(params.path) ? params.path.length : 0, speed: params.speed, closed: params.closed,
1065
+ ${ref ? `pathFrom: ${JSON.stringify(ref)},` : ''} window: params.window };
1066
+ })();`.trim();
1067
+ }
1068
+ /**
1069
+ * Parameter names a generator's docs used that the engine does not read.
1070
+ *
1071
+ * drawWindField was documented with `direction` and `speed`; the engine reads
1072
+ * windDirection and windSpeed, so the documented call drew wind blowing the
1073
+ * default way (round 8 BB, 5.56). The docs are corrected, and the published
1074
+ * spellings are mapped because callers followed them. An explicit engine name
1075
+ * always wins.
1076
+ */
1077
+ const GENERATOR_PARAM_ALIASES = {
1078
+ drawWindField: { direction: 'windDirection', speed: 'windSpeed' },
1079
+ };
1080
+ function aliasGeneratorParams(name, params) {
1081
+ const map = GENERATOR_PARAM_ALIASES[name];
1082
+ if (!map)
1083
+ return params;
1084
+ const out = { ...params };
1085
+ for (const [from, to] of Object.entries(map)) {
1086
+ if (out[from] !== undefined && out[to] === undefined) {
1087
+ out[to] = out[from];
1088
+ delete out[from];
1089
+ }
1090
+ }
1091
+ return out;
1092
+ }
325
1093
  /**
326
1094
  * Template for removing relations
327
1095
  */
@@ -471,12 +1239,63 @@ function generateKeyframeAnimateCode(itemId, keyframes, duration, loop = false,
471
1239
  opts.clipOutPoint = clipOutPoint;
472
1240
  if (timeUnits !== undefined)
473
1241
  opts.timeUnits = timeUnits;
1242
+ // A FADE ON AN AUDIO ITEM WAS ACCEPTED AND NEVER HEARD. addAnimation keyframes
1243
+ // the audio item's sentinel, and the mix reads one static audioGain per clip,
1244
+ // so {audioGain: 0→1} answered success, duration 15, and exported at a flat
1245
+ // level (measured). Refused by name, before anything is added, and only for
1246
+ // audio items — a `volume` key on a shape is some other thing.
1247
+ const levelKeys = [...new Set(keyframes.flatMap((k) => Object.keys(k.properties ?? {})))]
1248
+ .filter((k) => k === 'audioGain' || k === 'volume' || k === 'gain');
1249
+ // UNLESS THE STUDIO ANIMATES IT. From FxTool fcec0194 keyed gain / volume
1250
+ // are real — fades and ducks bake into the export at the measured level —
1251
+ // and listAnimatableProperties lists them with appliesTo 'audio'. That list
1252
+ // is the feature check: a key it names for audio passes through, anything
1253
+ // else on an audio item is still refused (prod, until that build ships).
1254
+ const audioLevelGuard = levelKeys.length === 0 ? '' : `
1255
+ const __ae = app.itemRegistry && app.itemRegistry.get('${itemId}');
1256
+ let __audioKeyed = [];
1257
+ try {
1258
+ const __l = typeof app.listAnimatableProperties === 'function' ? app.listAnimatableProperties() : null;
1259
+ __audioKeyed = ((__l && __l.properties) || []).filter(function(p) { return p && p.appliesTo === 'audio'; }).map(function(p) { return p.name; });
1260
+ } catch (_) { /* treat as unsupported */ }
1261
+ const __unsupported = ${JSON.stringify(levelKeys)}.filter(function(k) { return __audioKeyed.indexOf(k) === -1; });
1262
+ if (__ae && __ae.type === 'audio' && __unsupported.length) {
1263
+ return { success: false, itemId: '${itemId}', error: __unsupported.join(', ') + ' cannot be keyframed on an audio item in this studio: it mixes each audio clip at one fixed level, so a fade would be accepted and not heard. Nothing was added. '
1264
+ + (__audioKeyed.length ? 'Keyable on audio here: ' + __audioKeyed.join(', ') + '. ' : '')
1265
+ + 'Set a static level with pinepaper_modify_item {audioGain: 0..1}.' };
1266
+ }`;
1267
+ // Easing is read from the DESTINATION keyframe, so easing on the first one
1268
+ // never applies — a Ken Burns eased only there rendered linear. Said in the
1269
+ // result rather than silently moved: moving it would change what the other
1270
+ // keys the caller wrote mean.
1271
+ // A KEY THAT LEAVES A PROPERTY OUT (round 9 HH, engine 2.30): the engine
1272
+ // treats the gap as a break in that property's track rather than holding or
1273
+ // interpolating across it, silently. Named here — which keys lack which
1274
+ // properties — with the fix a caller can make now: give every key the full set.
1275
+ const propSets = keyframes.map((k) => Object.keys(k.properties ?? {}));
1276
+ const allProps = [...new Set(propSets.flat())];
1277
+ const gaps = keyframes
1278
+ .map((k, i) => ({ time: k.time, missing: allProps.filter((p) => !propSets[i].includes(p)) }))
1279
+ .filter((g) => g.missing.length);
1280
+ const gapNote = gaps.length && keyframes.length > 1
1281
+ ? `, propertyGaps: ${JSON.stringify(gaps.slice(0, 10))}, gapWarning: ${JSON.stringify(`${gaps.length} keyframe(s) leave out a property other keys animate (${[...new Set(gaps.flatMap((g) => g.missing))].join(', ')}). The engine can break that property's track at such a key; repeat the value on every key — hold it by repeating the previous value.`)}`
1282
+ : '';
1283
+ const first = [...keyframes].sort((a, b) => a.time - b.time)[0];
1284
+ // loop:true repeats in the editor; an export of the measured build played
1285
+ // the keyframes ONCE and held (round 7, 2.24). Said where it is set, with
1286
+ // the two ways that do export, until the engine's export clock wraps them.
1287
+ const loopNote = loop
1288
+ ? `, loopNote: ${JSON.stringify('loop:true repeats in the editor, but has been measured playing ONCE in an exported video and then holding the last keyframe. For an export longer than one cycle, check a frame in the second cycle — if it holds, write the keyframes out per cycle, or use a loop preset (pinepaper_animate), which does cycle in export.')}`
1289
+ : '';
1290
+ const easingNote = first?.easing && first.easing !== 'linear'
1291
+ ? `, note: ${JSON.stringify(`easing '${first.easing}' is on the first keyframe, where it has no effect: easing shapes the segment ARRIVING at a keyframe. Put it on the keyframe you are moving to.`)}`
1292
+ : '';
474
1293
  return `
475
1294
  // Apply keyframe animation to ${itemId}
476
1295
  (function() {
477
- ${requireItem(itemId, 'the animation')}
1296
+ ${requireItem(itemId, 'the animation')}${audioLevelGuard}
478
1297
  app.addAnimation('${itemId}', ${keyframesJson}, ${JSON.stringify(opts)});
479
- return { success: true, itemId: '${itemId}', duration: ${calculatedDuration}, loop: ${loop}${timeOffset !== undefined ? `, timeOffset: ${timeOffset}` : ''}${clipInPoint !== undefined ? `, clipInPoint: ${clipInPoint}` : ''}${clipOutPoint !== undefined ? `, clipOutPoint: ${clipOutPoint}` : ''} };
1298
+ return { success: true, itemId: '${itemId}', duration: ${calculatedDuration}, loop: ${loop}${timeOffset !== undefined ? `, timeOffset: ${timeOffset}` : ''}${clipInPoint !== undefined ? `, clipInPoint: ${clipInPoint}` : ''}${clipOutPoint !== undefined ? `, clipOutPoint: ${clipOutPoint}` : ''}${easingNote}${loopNote}${gapNote} };
480
1299
  })();
481
1300
  `.trim();
482
1301
  }
@@ -514,17 +1333,41 @@ function generateImportMotionCaptureCode(bvh, mode, skeletonId, opts = {}) {
514
1333
  })()`;
515
1334
  }
516
1335
  function generateExecuteGeneratorCode(generatorName, params, region) {
517
- // FxTool signature: app.executeGenerator(name, params, options).
518
- // region lives in options (added in FxTool c81781c).
519
- const options = region ? { region } : {};
1336
+ const where = !region ? '' : region.width !== undefined
1337
+ ? ` in region ${region.width}×${region.height} @ (${region.x},${region.y})`
1338
+ : ` in the shape of ${region.shape.itemId}`;
520
1339
  return `
521
- // Execute ${generatorName} generator${region ? ` in region ${region.width}×${region.height} @ (${region.x},${region.y})` : ''}
522
- (async function() {
523
- await app.executeGenerator('${generatorName}', ${JSON.stringify(params, null, 2)}, ${JSON.stringify(options)});
524
- return { success: true, generator: '${generatorName}'${region ? ', region: ' + JSON.stringify(region) : ''} };
1340
+ // Execute ${generatorName} generator${where}
1341
+ (async function() {${emitGeneratorRun(generatorName, JSON.stringify(params, null, 2), region)}
525
1342
  })();
526
1343
  `.trim();
527
1344
  }
1345
+ /**
1346
+ * Run a generator, optionally into a region, and return its result. Shared by
1347
+ * execute_generator and the batch op. FxTool signature:
1348
+ * app.executeGenerator(name, params, options), region in options (c81781c).
1349
+ *
1350
+ * A SHAPED REGION (c68524a2) falls back to a plain rectangle with only a
1351
+ * console.warn — for a missing item, or one that is not a path — and an older
1352
+ * engine drops the shape without a word (and, given only {itemId}, has no box
1353
+ * at all and draws full-canvas). So the item is checked first and refused by
1354
+ * name, and the result says whether this studio clips to shapes.
1355
+ */
1356
+ function emitGeneratorRun(generatorName, paramsJson, region) {
1357
+ const options = JSON.stringify(region ? { region } : {});
1358
+ const itemId = region && typeof region.shape === 'object' ? region.shape.itemId : null;
1359
+ const shaped = !!region && region.shape !== undefined && region.shape !== 'rect';
1360
+ return `${itemId ? `
1361
+ {
1362
+ const __se = app.itemRegistry && app.itemRegistry.get(${JSON.stringify(itemId)});
1363
+ const __si = __se && __se.item;
1364
+ if (!__si) return { success: false, error: ${JSON.stringify(`region.shape names no item "${itemId}" — nothing was drawn. List items with pinepaper_get_items.`)} };
1365
+ if (__si.className !== 'Path' && __si.className !== 'CompoundPath') return { success: false, error: ${JSON.stringify(`region.shape item "${itemId}" is a `)} + __si.className + ', and a clip needs a path (a shape, not text, a group or an image) — nothing was drawn.' };
1366
+ }` : ''}
1367
+ await app.executeGenerator('${generatorName}', ${paramsJson}, ${options});${shaped ? `
1368
+ const __shapes = !!(app.generatorRegistry && typeof app.generatorRegistry._regionClipPath === 'function');` : ''}
1369
+ return { success: true, generator: '${generatorName}'${region ? `, region: ${JSON.stringify(region)}` : ''}${shaped ? `, ...(__shapes ? {} : { regionShapeApplied: false, warning: ${JSON.stringify(`this studio does not clip generator regions to a shape, so ${typeof region.shape === 'object' ? 'the region had no box and the generator drew across the whole canvas' : `the ${region.shape} was drawn as a rectangle`}. It needs an engine with region shapes.`)} })` : ''} };`;
1370
+ }
528
1371
  /**
529
1372
  * Template for applying effects
530
1373
  */
@@ -829,6 +1672,21 @@ const size = app.getCanvasSize();
829
1672
  ({ width: size.width, height: size.height });
830
1673
  `.trim();
831
1674
  }
1675
+ /**
1676
+ * Uploaded audio / video, removed through the media pipeline.
1677
+ *
1678
+ * clearCanvas empties the item groups and registries, but an uploaded clip
1679
+ * also lives in the audio / video layers — so a video from the previous job
1680
+ * survived agent_start_job {clearCanvas:true} and played in the next scene
1681
+ * (round 8 retest, 1.66). removeMedia is the full removal (registry entry,
1682
+ * stored blob, layer). Run before clearCanvas; forEach, not a generated loop.
1683
+ */
1684
+ const MEDIA_PURGE = `
1685
+ let __mediaRemoved = 0;
1686
+ const __PA = (typeof window !== 'undefined') && window.PinePaperAgent;
1687
+ if (__PA && typeof __PA.listMedia === 'function' && typeof __PA.removeMedia === 'function') {
1688
+ __PA.listMedia().forEach(function(m) { try { if (__PA.removeMedia(m.id)) __mediaRemoved++; } catch (_) { /* keep clearing */ } });
1689
+ }`;
832
1690
  /**
833
1691
  * Template for clearing all items from canvas
834
1692
  */
@@ -841,6 +1699,7 @@ function generateClearCanvasCode() {
841
1699
  // entries don't expose those — getItemById wants the registry id), so it
842
1700
  // removed 0 items, and it called the non-existent app.clearAllRelations.
843
1701
  const before = app.itemRegistry ? app.itemRegistry.getAll().length : 0;
1702
+ ${MEDIA_PURGE}
844
1703
 
845
1704
  if (typeof app.clearCanvas === 'function') {
846
1705
  app.clearCanvas();
@@ -856,7 +1715,7 @@ if (app.historyManager) app.historyManager.saveState();
856
1715
 
857
1716
  const remaining = app.itemRegistry ? app.itemRegistry.getAll().length : 0;
858
1717
 
859
- ({ success: true, removedCount: Math.max(0, before - remaining), remainingItems: remaining });
1718
+ ({ success: true, removedCount: Math.max(0, before - remaining), remainingItems: remaining, mediaRemoved: __mediaRemoved });
860
1719
  `.trim();
861
1720
  }
862
1721
  /**
@@ -1034,64 +1893,100 @@ export function planKeyframeMerges(operations) {
1034
1893
  return plan;
1035
1894
  }
1036
1895
  /**
1037
- * Template for batch create - creates multiple items with single history save
1896
+ * A create / modify snippet as the body of a function that RETURNS its value.
1897
+ * Such a snippet's value is its last top-level (-led statement; the
1898
+ * font-loading form is an async IIFE, which is awaited instead.
1899
+ */
1900
+ function asReturningBody(snippet) {
1901
+ const single = snippet.trim();
1902
+ if (single.startsWith('(async function()'))
1903
+ return `return await ${single.replace(/;\s*$/, '')};`;
1904
+ const lines = single.split('\n');
1905
+ let at = -1;
1906
+ lines.forEach((l, i) => { if (l.startsWith('('))
1907
+ at = i; });
1908
+ if (at >= 0)
1909
+ lines[at] = 'return ' + lines[at];
1910
+ return lines.join('\n');
1911
+ }
1912
+ /**
1913
+ * Template for batch create — create_item for each item, one history save.
1914
+ *
1915
+ * This called app.batchCreate, a third create path, and dropped fontWeight
1916
+ * (and every other create_item fix) with no ignoredProperties (round 9 II,
1917
+ * 1.79) — the same drift f1d0b05 removed from agent_batch_execute. Each item
1918
+ * now runs create_item's own emitter with its per-item history save taken
1919
+ * out, and the batch saves once at the end, which is what batchCreate was
1920
+ * for. An item that fails is reported by index; the rest are still created.
1038
1921
  */
1039
1922
  function generateBatchCreateCode(items) {
1040
- const itemsJson = JSON.stringify(items, null, 2);
1923
+ const blocks = items.map((it, i) => {
1924
+ const { x, y, position, ...props } = (it.params || {});
1925
+ const p = Array.isArray(position) ? { x: Number(position[0]), y: Number(position[1]) }
1926
+ : (position && typeof position === 'object') ? { x: Number(position.x), y: Number(position.y) }
1927
+ : { x: typeof x === 'number' ? x : 400, y: typeof y === 'number' ? y : 300 };
1928
+ const given = position !== undefined || typeof x === 'number' || typeof y === 'number';
1929
+ const body = asReturningBody(generateCreateItemCode(it.type, p, props, undefined, given))
1930
+ .replace(/app\.historyManager\.saveState\(\);/g, '');
1931
+ return `
1932
+ try {
1933
+ const r${i} = await (async function() {
1934
+ ${body}
1935
+ })();
1936
+ if (r${i} && r${i}.success === false) errors.push({ index: ${i}, error: r${i}.error });
1937
+ else results.push(Object.assign({ index: ${i} }, r${i}));
1938
+ } catch (e) { errors.push({ index: ${i}, error: (e && e.message) || String(e) }); }`;
1939
+ });
1041
1940
  return `
1042
- // Batch create ${items.length} items
1043
- const itemSpecs = ${itemsJson};
1044
- const createdItems = app.batchCreate(itemSpecs);
1045
-
1046
- // Return created item IDs and info
1047
- const results = createdItems.map(item => ({
1048
- itemId: item.data?.registryId || item.id,
1049
- type: item.className || 'unknown',
1050
- position: item.position ? { x: item.position.x, y: item.position.y } : null
1051
- }));
1052
-
1053
- ({ success: true, items: results, count: results.length });
1054
- `.trim();
1941
+ // Batch create ${items.length} items — create_item for each, one history save
1942
+ (async function() {
1943
+ const results = [];
1944
+ const errors = [];
1945
+ ${blocks.join('\n')}
1946
+ if (app.historyManager) app.historyManager.saveState();
1947
+ return { success: errors.length === 0, items: results, count: results.length, errors: errors };
1948
+ })();`.trim();
1055
1949
  }
1056
1950
  /**
1057
- * Template for batch modify - modifies multiple items with single history save
1951
+ * Template for batch modify — modify_item for each item, one history save.
1952
+ *
1953
+ * This called app.batchModify, which applies raw params and so missed every
1954
+ * modify_item fix (text weight / leading, font loading, audio level, no-fill,
1955
+ * lifetimes, smoothing, the unread-property report) — the drift f1d0b05 and
1956
+ * 8f9b075 removed from the other batch paths. Each item now runs modify_item's
1957
+ * own emitter with its history save removed; the batch saves once. The
1958
+ * result keeps the old shape (count, requested, skipped with reasons, and an
1959
+ * error naming the skipped when any were).
1058
1960
  */
1059
1961
  function generateBatchModifyCode(modifications) {
1060
- const modsJson = JSON.stringify(modifications, null, 2);
1962
+ const blocks = modifications.map((m, i) => {
1963
+ const snippet = generateModifyItemCode(m.itemId, (m.params || {})).trim()
1964
+ .replace(/app\.historyManager\.saveState\(\);/g, '')
1965
+ .replace(/;\s*$/, '');
1966
+ return `
1967
+ try {
1968
+ const r${i} = await ${snippet};
1969
+ if (r${i} && r${i}.success === false) skipped.push({ itemId: ${JSON.stringify(m.itemId)}, reason: r${i}.error || 'not modified' });
1970
+ else { count++; if (r${i} && r${i}.ignoredProperties) ignored.push({ itemId: ${JSON.stringify(m.itemId)}, ignoredProperties: r${i}.ignoredProperties }); }
1971
+ } catch (e) { skipped.push({ itemId: ${JSON.stringify(m.itemId)}, reason: (e && e.message) || String(e) }); }`;
1972
+ });
1061
1973
  return `
1062
- // Batch modify ${modifications.length} items
1063
- const modifications = ${modsJson};
1064
- // TWO BUGS, ONE CALL, AND BOTH FAILED LOUDLY IN THE WRONG PLACE.
1065
- //
1066
- // 1. The engine takes { item, params } with a LIVE ITEM, or { itemId, params }
1067
- // with a registry id. This put the id STRING into the item field, which is
1068
- // truthy — so the engine's own id lookup never ran, the string was treated
1069
- // as an item, and Object.assign got a string: "Cannot convert undefined or
1070
- // null to object", from a call that named the id correctly.
1071
- // 2. batchModify returns a COUNT, not an array. Calling .map on a number
1072
- // is the second reported error, and it fires even when the modify worked.
1073
- const results = app.batchModify(modifications.map(mod => ({
1074
- itemId: mod.itemId,
1075
- params: mod.params
1076
- })));
1077
-
1078
- // The engine records what it could not do rather than throwing — read it,
1079
- // instead of reporting a clean success over a half-applied batch.
1080
- //
1081
- // Read defensively (|| []): the property arrived with the engine-side
1082
- // batchModify fix, so an older studio reports a count and no skip list rather
1083
- // than failing. The batch still works; only the explanation is thinner.
1084
- const skipped = app.lastBatchModifySkipped || [];
1085
- const count = typeof results === 'number' ? results : (Array.isArray(results) ? results.length : 0);
1086
-
1087
- ({
1088
- success: skipped.length === 0 && count === modifications.length,
1089
- count,
1090
- requested: modifications.length,
1091
- skipped: skipped.map(s => ({ itemId: s.itemId, reason: s.reason })),
1092
- ...(skipped.length ? { error: 'batch_modify changed ' + count + ' of ' + modifications.length + ' items; skipped: ' + skipped.map(s => s.itemId + ' (' + s.reason + ')').join(', ') } : {}),
1093
- });
1094
- `.trim();
1974
+ // Batch modify ${modifications.length} items — modify_item for each, one history save
1975
+ (async function() {
1976
+ let count = 0;
1977
+ const skipped = [];
1978
+ const ignored = [];
1979
+ ${blocks.join('\n')}
1980
+ if (app.historyManager) app.historyManager.saveState();
1981
+ return {
1982
+ success: skipped.length === 0 && count === ${modifications.length},
1983
+ count: count,
1984
+ requested: ${modifications.length},
1985
+ skipped: skipped,
1986
+ ...(ignored.length ? { ignoredProperties: ignored } : {}),
1987
+ ...(skipped.length ? { error: 'batch_modify changed ' + count + ' of ${modifications.length} items; skipped: ' + skipped.map(function(s) { return s.itemId + ' (' + s.reason + ')'; }).join(', ') } : {}),
1988
+ };
1989
+ })();`.trim();
1095
1990
  }
1096
1991
  /**
1097
1992
  * Template for create grid - convenience method for creating line grids
@@ -1148,6 +2043,8 @@ const PLATFORM_TO_CANVAS_PRESET = {
1148
2043
  web: 'hd-720p', // 1280x720
1149
2044
  'print-a4': 'a4-portrait',
1150
2045
  'print-letter': 'letter-portrait',
2046
+ 'print-a4-landscape': 'a4-landscape',
2047
+ 'print-letter-landscape': 'letter-landscape',
1151
2048
  };
1152
2049
  /** The engine key for a platform, falling back to the value itself. */
1153
2050
  export function canvasPresetFor(platform) {
@@ -1293,6 +2190,7 @@ export class PinePaperCodeGenerator {
1293
2190
  */
1294
2191
  generateCreateItem(input) {
1295
2192
  const validated = CreateItemInputSchema.parse(input);
2193
+ const positionGiven = input.position !== undefined;
1296
2194
  const properties = { ...validated.properties };
1297
2195
  if (validated.animationType !== undefined)
1298
2196
  properties.animationType = validated.animationType;
@@ -1304,14 +2202,16 @@ export class PinePaperCodeGenerator {
1304
2202
  properties.animationDelay = validated.animationDelay;
1305
2203
  if (validated.keyframes !== undefined)
1306
2204
  properties.keyframes = validated.keyframes;
1307
- return generateCreateItemCode(validated.itemType, validated.position, properties);
2205
+ if (validated.anchor !== undefined && properties.anchor === undefined && properties.origin === undefined)
2206
+ properties.anchor = validated.anchor;
2207
+ return generateCreateItemCode(validated.itemType, validated.position, properties, validated.data, positionGiven);
1308
2208
  }
1309
2209
  /**
1310
2210
  * Generate code for modifying an item
1311
2211
  */
1312
2212
  generateModifyItem(input) {
1313
2213
  const validated = ModifyItemInputSchema.parse(input);
1314
- return generateModifyItemCode(validated.itemId, validated.properties);
2214
+ return generateModifyItemCode(validated.itemId, validated.properties, validated.data);
1315
2215
  }
1316
2216
  /**
1317
2217
  * Generate code for deleting an item
@@ -1346,6 +2246,9 @@ export class PinePaperCodeGenerator {
1346
2246
  return { success: true, presetId: ${JSON.stringify(validated.presetId)}, relationType: r.relationType, params: r.params, values: r.values };
1347
2247
  })();`.trim();
1348
2248
  }
2249
+ if (validated.relationType === 'moves_along_path') {
2250
+ return generateMovesAlongPathCode(validated.sourceId, validated.targetId ?? undefined, (validated.params ?? {}));
2251
+ }
1349
2252
  return generateAddRelationCode(validated.sourceId, validated.targetId || validated.sourceId, validated.relationType, validated.params);
1350
2253
  }
1351
2254
  /**
@@ -1400,7 +2303,7 @@ export class PinePaperCodeGenerator {
1400
2303
  */
1401
2304
  generateExecuteGenerator(input) {
1402
2305
  const validated = ExecuteGeneratorInputSchema.parse(input);
1403
- return generateExecuteGeneratorCode(validated.generatorName, validated.params, validated.region);
2306
+ return generateExecuteGeneratorCode(validated.generatorName, aliasGeneratorParams(validated.generatorName, (validated.params ?? {})), validated.region);
1404
2307
  }
1405
2308
  /**
1406
2309
  * Generate code for applying an effect
@@ -1932,72 +2835,41 @@ if (typeof _gen === 'function') {
1932
2835
  for (const item of items) {
1933
2836
  const { name, itemType, position = { x: 400, y: 300 }, properties = {} } = item;
1934
2837
  // Build properties string
1935
- const propsEntries = Object.entries(properties)
1936
- .map(([key, value]) => `${key}: ${JSON.stringify(value)}`)
1937
- .join(', ');
1938
- const propsStr = propsEntries ? `, ${propsEntries}` : '';
2838
+ // CREATE_SCENE HAND-BUILT ITS OWN SHAPES, AND MOST OF THEM WERE WRONG.
2839
+ //
2840
+ // This emitted a per-type switch of raw Paper constructors — a parallel
2841
+ // implementation of app.create covering four cases, whose DEFAULT branch
2842
+ // made a 30px circle. So ellipse, triangle, polygon, path, line and arc —
2843
+ // six of the ten types this schema declares — silently rendered as a
2844
+ // small blue circle, and every property the switch did not name was
2845
+ // dropped. A pilot reported ellipse width/height being ignored; the same
2846
+ // values work through batch because batch calls app.create.
2847
+ //
2848
+ // Routed through app.create now, which is the only thing that knows all
2849
+ // 28 item types, the shape registry's own parameters, anchor, label,
2850
+ // rotation and the registry id. Deleting the copy is the fix: a second
2851
+ // implementation of "make a shape" could only ever drift from the first.
2852
+ const sceneParams = {
2853
+ x: position.x,
2854
+ y: position.y,
2855
+ ...properties,
2856
+ };
1939
2857
  codeParts.push(`
1940
2858
  // Create item: ${name}
1941
2859
  (function() {
1942
- const itemParams = {
1943
- type: '${itemType}',
1944
- position: { x: ${position.x}, y: ${position.y} }${propsStr}
1945
- };
1946
-
1947
- let item;
1948
- switch ('${itemType}') {
1949
- case 'circle':
1950
- item = new paper.Path.Circle({
1951
- center: [${position.x}, ${position.y}],
1952
- radius: ${properties.radius || 50},
1953
- fillColor: '${properties.color || properties.fillColor || '#3b82f6'}',
1954
- parent: app.textItemGroup
1955
- });
1956
- break;
1957
- case 'rectangle':
1958
- item = new paper.Path.Rectangle({
1959
- point: [${position.x - (properties.width || 100) / 2}, ${position.y - (properties.height || 60) / 2}],
1960
- size: [${properties.width || 100}, ${properties.height || 60}],
1961
- fillColor: '${properties.color || properties.fillColor || '#3b82f6'}',
1962
- parent: app.textItemGroup
1963
- });
1964
- break;
1965
- case 'star':
1966
- item = new paper.Path.Star({
1967
- center: [${position.x}, ${position.y}],
1968
- points: ${properties.points || 5},
1969
- radius1: ${properties.radius1 || 40},
1970
- radius2: ${properties.radius2 || 20},
1971
- fillColor: '${properties.color || properties.fillColor || '#fbbf24'}',
1972
- parent: app.textItemGroup
1973
- });
1974
- break;
1975
- case 'text':
1976
- item = new paper.PointText({
1977
- point: [${position.x}, ${position.y}],
1978
- content: '${properties.content || 'Text'}',
1979
- fontSize: ${properties.fontSize || 24},
1980
- fillColor: '${properties.color || properties.fillColor || '#ffffff'}',
1981
- fontFamily: '${properties.fontFamily || 'Inter'}',
1982
- justification: 'center',
1983
- parent: app.textItemGroup
1984
- });
1985
- break;
1986
- default:
1987
- item = new paper.Path.Circle({
1988
- center: [${position.x}, ${position.y}],
1989
- radius: 30,
1990
- fillColor: '${properties.color || properties.fillColor || '#3b82f6'}',
1991
- parent: app.textItemGroup
1992
- });
2860
+ const item = app.create('${itemType}', ${JSON.stringify(sceneParams)});
2861
+ if (!item) {
2862
+ results.items.push({ name: '${name}', error: 'app.create returned nothing for type ${itemType} — check the type and its properties.' });
2863
+ return;
1993
2864
  }
1994
-
1995
- const itemId = app.registerItem(item, '${name}', { source: 'mcp-scene' });
2865
+ // create() registers and mints the id; only fall back for an older studio.
2866
+ const itemId = (item.data && item.data.id)
2867
+ ? item.data.id
2868
+ : app.registerItem(item, '${itemType}', { source: 'mcp-scene' });
1996
2869
  if (item.bringToFront) item.bringToFront();
1997
2870
  nameToId['${name}'] = itemId;
1998
2871
  results.items.push({ name: '${name}', itemId, type: '${itemType}' });
1999
- })();
2000
- `);
2872
+ })();`);
2001
2873
  }
2002
2874
  // Establish relations
2003
2875
  for (const relation of relations) {
@@ -2018,8 +2890,12 @@ if (typeof _gen === 'function') {
2018
2890
  }
2019
2891
  // Apply animations
2020
2892
  for (const animation of animations) {
2021
- const { target, type, speed = 1.0, params = {} } = animation;
2022
- const paramsStr = JSON.stringify({ ...params, speed });
2893
+ const { target, type, speed = 1.0, startTime, params = {} } = animation;
2894
+ const paramsStr = JSON.stringify({
2895
+ ...params,
2896
+ speed,
2897
+ ...(startTime !== undefined ? { animationDelay: startTime } : {}),
2898
+ });
2023
2899
  codeParts.push(`
2024
2900
  // Apply animation: ${type} to ${target}
2025
2901
  (function() {
@@ -2028,8 +2904,13 @@ if (typeof _gen === 'function') {
2028
2904
  const targetItem = app.itemRegistry.get(targetId);
2029
2905
  if (targetItem) {
2030
2906
  const params = ${paramsStr};
2907
+ // startTime becomes animationDelay, which is what the engine reads
2908
+ // (PinePaper.js stores it on item.data.animationDelay). These are ambient
2909
+ // loops with no end, so there is no duration to map and none is invented
2910
+ // — staggering the STARTS is what makes a scene read as choreographed
2911
+ // rather than everything moving at once from frame zero.
2031
2912
  app.animate(targetItem, { animationType: '${type}', ...params });
2032
- results.animations.push({ target: '${target}', type: '${type}' });
2913
+ results.animations.push({ target: '${target}', type: '${type}'${startTime !== undefined ? `, startTime: ${startTime}` : ''} });
2033
2914
  }
2034
2915
  }
2035
2916
  })();
@@ -2400,7 +3281,7 @@ throw new Error('Unknown diagram mode action: ${action}');
2400
3281
  `;
2401
3282
  if (shouldClear) {
2402
3283
  code += `
2403
- // Clear canvas
3284
+ // Clear canvas — uploaded media first (it lives outside the item groups)${MEDIA_PURGE}
2404
3285
  if (app.clearCanvas) {
2405
3286
  app.clearCanvas();
2406
3287
  } else {
@@ -2463,7 +3344,9 @@ throw new Error('Unknown diagram mode action: ${action}');
2463
3344
  */
2464
3345
  generateAgentEndJob(input) {
2465
3346
  const validated = AgentEndJobInputSchema.parse(input);
2466
- const { takeScreenshot, analyzeContent } = validated;
3347
+ const { analyzeContent } = validated;
3348
+ // includeScreenshot is the published name; takeScreenshot the older one.
3349
+ const takeScreenshot = validated.includeScreenshot ?? validated.takeScreenshot;
2467
3350
  const shouldAnalyze = analyzeContent !== false;
2468
3351
  let code = `
2469
3352
  // End agent job
@@ -2761,21 +3644,22 @@ throw new Error('Unknown diagram mode action: ${action}');
2761
3644
  generateBatchOperationCode(op, index, mergePlan) {
2762
3645
  switch (op.type) {
2763
3646
  case 'create': {
3647
+ // THE SAME CREATE AS create_item, NOT A SECOND ONE.
3648
+ //
3649
+ // This case was its own app.create call, and every create_item fix since
3650
+ // — fontWeight / leading, font loading, countdown content, no-fill,
3651
+ // dashes, lifetimes, the unread-property report — passed it by: a batch
3652
+ // read fontWeight 700 back as normal and a countdown stayed at 00:10
3653
+ // for a 12 s MP4 (round 8 DD, 1.67). It now runs generateCreateItemCode,
3654
+ // the exact emitter create_item uses, adapted to return its result
3655
+ // inside this op's function.
2764
3656
  const pos = op.position || { x: 400, y: 300 };
2765
3657
  const createProps = withRadiusAxes((op.properties || {}));
2766
- const props = JSON.stringify(createProps);
3658
+ const returning = asReturningBody(generateCreateItemCode(op.itemType, pos, { ...(op.properties || {}) }, undefined, op.position !== undefined));
2767
3659
  let createCode = `
2768
- const item = app.create('${op.itemType}', { position: { x: ${pos.x}, y: ${pos.y} }, ...${props} });
2769
- const itemId = item.data && item.data.id ? item.data.id : app.registerItem(item, '${op.itemType}', { source: 'mcp-batch' });`;
2770
- // app.create() does not read blendMode/opacity from its params, but the
2771
- // underlying Paper.js item supports them directly — apply post-create so
2772
- // callers can pass them inline in `properties` and have them take effect.
2773
- if (createProps.blendMode !== undefined) {
2774
- createCode += `\nif ('blendMode' in item) item.blendMode = ${JSON.stringify(createProps.blendMode)};`;
2775
- }
2776
- if (createProps.opacity !== undefined) {
2777
- createCode += `\nif ('opacity' in item) item.opacity = ${JSON.stringify(createProps.opacity)};`;
2778
- }
3660
+ const __created = await (async function() {
3661
+ ${returning}
3662
+ })();`;
2779
3663
  // Coordinate-built items (path from segments/pathData, line/arc from
2780
3664
  // from/through/to) derive their geometry from those coordinates and
2781
3665
  // IGNORE params.position — so `create` at a point silently produced an
@@ -2787,30 +3671,35 @@ const itemId = item.data && item.data.id ? item.data.id : app.registerItem(item,
2787
3671
  createCode += `
2788
3672
  // create() builds this item type from its own coordinates and ignores
2789
3673
  // params.position — apply the caller's explicit position after the fact.
2790
- if (item.position) item.position = new paper.Point(${pos.x}, ${pos.y});`;
3674
+ const __it = __created && __created.itemId ? app.getItemById(__created.itemId) : null;
3675
+ if (__it && __it.position) __it.position = new paper.Point(${pos.x}, ${pos.y});`;
2791
3676
  }
2792
3677
  createCode += `
2793
- // Ensure item is visible above backgrounds/generators
2794
- if (item.bringToFront) item.bringToFront();
2795
- return { itemId };
3678
+ return __created;
2796
3679
  `;
2797
3680
  return createCode;
2798
3681
  }
2799
- case 'modify':
3682
+ case 'modify': {
2800
3683
  const itemRef = op.itemId?.startsWith('$')
2801
3684
  ? `itemIds[${op.itemId.substring(1)}]`
2802
3685
  : `'${op.itemId}'`;
2803
- const modifyProps = JSON.stringify(op.properties || {});
2804
- return `
2805
- const targetId = ${itemRef};
2806
- // modifyItem(id, changes), not select() + modify(). modify() edits the
2807
- // SELECTION, so an id that does not resolve left the PREVIOUS selection to be
2808
- // edited instead — and this then reported modified:true either way.
2809
- if (app.modifyItem(targetId, ${modifyProps}) === false) {
2810
- return { success: false, itemId: targetId, error: 'no item ' + targetId + ' — nothing was modified' };
2811
- }
2812
- return { itemId: targetId, modified: true };
3686
+ // modify_item's own emitter, as create is create_item's (see above):
3687
+ // the batch's modifyItem call missed text styles, font loading, audio
3688
+ // level, no-fill, lifetimes, smoothing and the unread-property report.
3689
+ // The emitter embeds the id as a literal; a batch ref ($N) is only known
3690
+ // at run time, so it is generated with a placeholder and the runtime
3691
+ // variable put in its place.
3692
+ const single = generateModifyItemCode('__MID__', (op.properties || {})).trim()
3693
+ .replace(/'__MID__'/g, '__mid')
3694
+ .replace(/__MID__/g, "' + __mid + '")
3695
+ .replace(/;\s*$/, '');
3696
+ return `
3697
+ const __mid = ${itemRef};
3698
+ const __r = await ${single};
3699
+ if (__r && __r.success !== false) __r.modified = true;
3700
+ return __r;
2813
3701
  `;
3702
+ }
2814
3703
  case 'animate':
2815
3704
  const animItemRef = op.itemId?.startsWith('$')
2816
3705
  ? `itemIds[${op.itemId.substring(1)}]`
@@ -2872,11 +3761,8 @@ return { success: true, backgroundColor: '${bgColor}' };
2872
3761
  `;
2873
3762
  case 'execute_generator':
2874
3763
  const genName = op.generatorName || 'drawSunburst';
2875
- const genParams = JSON.stringify(op.generatorParams || {});
2876
- const genOptions = JSON.stringify(op.generatorRegion ? { region: op.generatorRegion } : {});
2877
- return `
2878
- await app.executeGenerator('${genName}', ${genParams}, ${genOptions});
2879
- return { success: true, generator: '${genName}'${op.generatorRegion ? ', region: ' + JSON.stringify(op.generatorRegion) : ''} };
3764
+ const genParams = JSON.stringify(aliasGeneratorParams(genName, (op.generatorParams || {})));
3765
+ return `${emitGeneratorRun(genName, genParams, op.generatorRegion)}
2880
3766
  `;
2881
3767
  case 'set_canvas_size': {
2882
3768
  const w = op.width || 1080;
@@ -3051,7 +3937,25 @@ return { success: true, action: 'seek', time: ${op.time || 0} };
3051
3937
  }
3052
3938
  generateAgentExport(input) {
3053
3939
  const validated = AgentExportInputSchema.parse(input);
3054
- const { platform, format, quality, framing, duration, estimateOnly, scale, fps } = validated;
3940
+ const { platform, format, quality, framing, duration, estimateOnly, scale, fps, pdf: pdfOpts, region, time: stillTime, maxBytes, loop: gifLoop, broadcast, broadcastHeadroom, bitrate, minBitrate, bitrateMode, transparent, ad: adOpts } = validated;
3941
+ // Encoder options (FxTool 16719759); the schema keeps them to mp4 / webm.
3942
+ const videoEncodeOpts = {};
3943
+ if (broadcast)
3944
+ videoEncodeOpts.broadcast = true;
3945
+ // Headroom defaults to 12 with broadcast (retest of caa9560): on a
3946
+ // hard-edged white-on-black scene it measured 17-233 with nothing out of
3947
+ // range, where 0 left 0.34% out and a post-hoc clip made it worse. 0 is
3948
+ // still available by asking for it.
3949
+ const headroomDefaulted = !!broadcast && broadcastHeadroom === undefined;
3950
+ if (broadcast)
3951
+ videoEncodeOpts.broadcastHeadroom = broadcastHeadroom ?? 12;
3952
+ if (bitrate !== undefined)
3953
+ videoEncodeOpts.bitrate = bitrate;
3954
+ if (minBitrate !== undefined)
3955
+ videoEncodeOpts.minBitrate = minBitrate;
3956
+ if (bitrateMode !== undefined)
3957
+ videoEncodeOpts.bitrateMode = bitrateMode;
3958
+ const encodeOptsJs = Object.keys(videoEncodeOpts).length ? `, ...${JSON.stringify(videoEncodeOpts)}` : '';
3055
3959
  const qualityLevel = quality || 'standard';
3056
3960
  const videoDuration = duration ?? 5;
3057
3961
  // Quality settings (bitrate in bps for VideoEncoder)
@@ -3072,6 +3976,8 @@ return { success: true, action: 'seek', time: ${op.time || 0} };
3072
3976
  'web': { width: 800, height: 600, staticFormat: 'svg', animatedFormat: 'svg' },
3073
3977
  'print-a4': { width: 2480, height: 3508, staticFormat: 'pdf', animatedFormat: 'pdf' },
3074
3978
  'print-letter': { width: 2550, height: 3300, staticFormat: 'pdf', animatedFormat: 'pdf' },
3979
+ 'print-a4-landscape': { width: 3508, height: 2480, staticFormat: 'pdf', animatedFormat: 'pdf' },
3980
+ 'print-letter-landscape': { width: 3300, height: 2550, staticFormat: 'pdf', animatedFormat: 'pdf' },
3075
3981
  };
3076
3982
  const preset = platformPresets[platform] || platformPresets['web'];
3077
3983
  // SCALE is the only honest size control this surface has.
@@ -3118,6 +4024,51 @@ return { success: true, action: 'seek', time: ${op.time || 0} };
3118
4024
  // for by name. Dimensions, framing, fps and bitrate are all absent rather
3119
4025
  // than passed and ignored.
3120
4026
  // @engine-methods renderSoundtrackWav exportEngine.sceneHasAudio ensureHeavyModules
4027
+ // CAPTIONS AS A FILE (round 8 EE, 8.19). format 'srt' was "Invalid input",
4028
+ // yet caption text staged with bornAt / ttl already carries frame-exact
4029
+ // times on item.data — the cues are in the scene. Every text item with a
4030
+ // lifetime becomes one cue, in start order; a text item without one is
4031
+ // on screen for the whole piece and is not a caption, so it is left out
4032
+ // and counted.
4033
+ // scc (CEA-608, 8.37) takes the same cues; the server encodes them
4034
+ // (src/utils/scc.ts), where the byte work is testable.
4035
+ if (exportFormat === 'srt' || exportFormat === 'vtt' || exportFormat === 'scc') {
4036
+ const fmt = exportFormat;
4037
+ return `
4038
+ // Export: captions (${fmt}) from text items with a lifetime
4039
+ (function() {
4040
+ if (!app.itemRegistry || typeof app.itemRegistry.getAll !== 'function') {
4041
+ return { success: false, format: '${fmt}', error: 'this studio cannot list items (app.itemRegistry.getAll) — update PinePaper Studio.' };
4042
+ }
4043
+ const texts = app.itemRegistry.getAll().filter(function(e) {
4044
+ const it = e && e.item;
4045
+ return it && it.className === 'PointText' && typeof it.content === 'string' && it.content.trim() !== '';
4046
+ });
4047
+ const timed = texts.filter(function(e) { return e.item.data && typeof e.item.data.bornAt === 'number'; });
4048
+ if (!timed.length) {
4049
+ return { success: false, format: '${fmt}', error: 'no captions to export: no text item has a lifetime. Stage each caption with properties.bornAt / ttl (seconds) on create_item or modify_item, then export ${fmt}.', untimedText: texts.length };
4050
+ }
4051
+ const end = ${videoDuration};
4052
+ const ts = function(t, sep) {
4053
+ const ms = Math.max(0, Math.round(t * 1000));
4054
+ const h = Math.floor(ms / 3600000), m = Math.floor(ms / 60000) % 60, s = Math.floor(ms / 1000) % 60, r = ms % 1000;
4055
+ const p = function(n, w) { return String(n).padStart(w, '0'); };
4056
+ return p(h, 2) + ':' + p(m, 2) + ':' + p(s, 2) + sep + p(r, 3);
4057
+ };
4058
+ const cues = timed.map(function(e) {
4059
+ const d = e.item.data;
4060
+ const start = d.bornAt;
4061
+ const stop = typeof d.ttl === 'number' ? start + d.ttl : end;
4062
+ return { start: start, stop: Math.max(start, stop), text: e.item.content.trim() };
4063
+ }).sort(function(a, b) { return a.start - b.start; });
4064
+ ${fmt === 'scc' ? ` return { success: true, format: 'scc', sccCues: cues, cues: cues.length, untimedText: texts.length - timed.length };
4065
+ ` : ''} const body = cues.map(function(c, i) {
4066
+ return ${fmt === 'srt' ? `(i + 1) + '\\n' + ts(c.start, ',') + ' --> ' + ts(c.stop, ',')` : `ts(c.start, '.') + ' --> ' + ts(c.stop, '.')`} + '\\n' + c.text;
4067
+ }).join('\\n\\n') + '\\n';
4068
+ const text = ${fmt === 'vtt' ? `'WEBVTT\\n\\n' + body` : 'body'};
4069
+ return { success: true, format: '${fmt}', data: text, mimeType: '${fmt === 'srt' ? 'application/x-subrip' : 'text/vtt'}', size: text.length, cues: cues.length, untimedText: texts.length - timed.length };
4070
+ })();`.trim();
4071
+ }
3121
4072
  if (exportFormat === 'wav') {
3122
4073
  const sr = validated.sampleRate ?? 48000;
3123
4074
  const bd = validated.bitDepth ?? 16;
@@ -3195,7 +4146,30 @@ ${usesCanvasSize ? ` // 'auto': the canvas's own size, with the preset only as
3195
4146
  const __even = (n) => Math.max(2, Math.round((n * ${scale !== undefined ? scale : 1}) / 2) * 2);
3196
4147
  const dimensions = (__canvasDims && __canvasDims.width && __canvasDims.height)
3197
4148
  ? { width: __even(__canvasDims.width), height: __even(__canvasDims.height) }
3198
- : __presetDims;` : ` const dimensions = ${JSON.stringify(scaled)};`}
4149
+ : __presetDims;
4150
+ const __fit = null;` : ` // A PRESET IS A SIZE, NOT PERMISSION TO STRETCH.
4151
+ //
4152
+ // 'instagram' is 1080x1080, and a 4:5 (1080x1350) canvas exported under it
4153
+ // came out squashed to 0.8 vertically, text distorted, with nothing said —
4154
+ // 4:5 is itself a valid Instagram feed size. When the canvas aspect differs
4155
+ // from the preset's, the canvas aspect is kept and its SHORT edge takes the
4156
+ // preset's short edge: 4:5 on instagram is 1080x1350, a square canvas on
4157
+ // youtube is 1080x1080. The result says so in platformFit.
4158
+ const __presetBox = ${JSON.stringify(scaled)};
4159
+ const __cv = app.canvasSize || (app.view && app.view.viewSize) || null;
4160
+ let dimensions = __presetBox;
4161
+ let __fit = null;
4162
+ if (__cv && __cv.width > 0 && __cv.height > 0) {
4163
+ const ca = __cv.width / __cv.height, pa = __presetBox.width / __presetBox.height;
4164
+ if (Math.abs(ca / pa - 1) > 0.01) {
4165
+ const short = Math.min(__presetBox.width, __presetBox.height);
4166
+ const ev = (n) => Math.max(2, Math.round(n / 2) * 2);
4167
+ dimensions = ca >= 1 ? { width: ev(short * ca), height: ev(short) } : { width: ev(short), height: ev(short / ca) };
4168
+ __fit = { preset: __presetBox.width + 'x' + __presetBox.height, used: dimensions.width + 'x' + dimensions.height,
4169
+ canvas: __cv.width + 'x' + __cv.height,
4170
+ note: 'the canvas aspect differs from the ' + platform + ' preset, so the canvas aspect was kept rather than stretched. Resize the canvas to the preset to get exactly ' + __presetBox.width + 'x' + __presetBox.height + '.' };
4171
+ }
4172
+ }`}
3199
4173
 
3200
4174
  // WHAT THIS SCENE LOSES TO THIS FORMAT, asked at the moment you would care.
3201
4175
  //
@@ -3204,17 +4178,97 @@ ${usesCanvasSize ? ` // 'auto': the canvas's own size, with the preset only as
3204
4178
  // silence. The engine derived it so that 117 tool descriptions would not
3205
4179
  // have to answer "does this survive export?" by hand and drift.
3206
4180
  //
3207
- // Reported ONLY when there is something to report. An empty list would read
3208
- // as "this format is lossless", and it does not mean that — it means this
3209
- // scene has nothing THIS format will lose. Omitting it entirely is the one
3210
- // shape that cannot be misread, and it keeps a clean export quiet.
4181
+ // THREE STATES, ALL DISTINGUISHABLE. This used to be reported only when
4182
+ // there was something to report, so that an empty list could not be misread
4183
+ // as "this format is lossless". That solved one misreading and created a
4184
+ // worse one: a caller could not tell "checked, nothing lost" from "never
4185
+ // checked", and a pilot reported fidelity as null in EVERY scene with no way
4186
+ // to know whether that was good news or a broken check.
4187
+ //
4188
+ // So it is always present: available:false when the studio cannot check,
4189
+ // warnings:[] plus a note when it checked and found nothing, and the
4190
+ // warnings themselves otherwise. The note carries the caveat that absence
4191
+ // was standing in for.
4192
+ //
4193
+ // STAND-INS ARE THIS TOOL'S OWN DOING, SO THIS TOOL REPORTS THEM. create_item
4194
+ // draws 'shader' and 'field' locally as a flat plate tagged data.renderAs; the
4195
+ // engine's check sees an ordinary rectangle and cannot know a surface was
4196
+ // meant. So a local export of a sea came out a flat blue slab with nothing
4197
+ // said. Reported whatever the engine check can or cannot do.
4198
+ function standIns() {
4199
+ try {
4200
+ // Probed with === : a studio without it degrades to "no stand-ins".
4201
+ const canList = app.itemRegistry && typeof app.itemRegistry.getAll === 'function';
4202
+ if (!canList) return [];
4203
+ const found = {};
4204
+ for (const entry of app.itemRegistry.getAll()) {
4205
+ const kind = entry && entry.item && entry.item.data && entry.item.data.renderAs;
4206
+ if (kind === 'shader' || kind === 'field') (found[kind] = found[kind] || []).push(entry.itemId);
4207
+ }
4208
+ return Object.keys(found).map(function(kind) {
4209
+ const ids = found[kind];
4210
+ return {
4211
+ code: 'render_time_surface_stand_in',
4212
+ message: ids.length + ' ' + kind + ' item' + (ids.length > 1 ? 's are' : ' is')
4213
+ + ' exported as the flat plate that stands in for ' + (ids.length > 1 ? 'them' : 'it')
4214
+ + ' on a local canvas. The surface itself is drawn only by a cloud render.',
4215
+ items: ids.slice(0, 6),
4216
+ };
4217
+ });
4218
+ } catch (e) { return []; }
4219
+ }
4220
+ // TRANSPARENCY THE FORMAT CANNOT CARRY. A scene with no background exported
4221
+ // to webm came out on BLACK while the engine's check said it lost nothing
4222
+ // (round 7, 8.12). Only said when the scene is evidently transparent — no
4223
+ // solid CSS background colour AND nothing in the background layer — so a
4224
+ // generator or pattern backdrop never trips it.
4225
+ function alphaLoss(fmt) {
4226
+ try {
4227
+ if (['mp4', 'webm', 'jpg'${format === 'apng' && transparent === false ? ", 'apng'" : ''}].indexOf(fmt) === -1) return [];
4228
+ const bg = app.canvasEl && app.canvasEl.style && app.canvasEl.style.backgroundColor;
4229
+ const hasColor = !!bg && !/^(transparent|rgba\\([^)]*,\\s*0\\))$/i.test(bg);
4230
+ const hasBgItems = !!(app.patternGroup && app.patternGroup.children && app.patternGroup.children.length);
4231
+ if (hasColor || hasBgItems) return [];
4232
+ return [{ code: 'alpha_dropped', message: fmt === 'apng'
4233
+ ? 'apng with transparent: false and no background colour: the frames are filled BLACK. Set a background colour, or drop transparent: false to keep the alpha.'
4234
+ : fmt === 'jpg'
4235
+ ? 'jpg has no transparency and this scene has no background, so transparent areas are filled white. Use png or webp to keep them.'
4236
+ : fmt + ' is exported without transparency and this scene has no background, so transparent areas come out BLACK. Set a background colour, or export apng (animated, full alpha) or png to keep it.' }];
4237
+ } catch (e) { return []; }
4238
+ }
4239
+ // TEXT THAT DOES NOT FIT (FxTool 661224ef: textOverflowReport) — text past
4240
+ // its fit box at the minimum size, or off the frame. The pre-batch check for
4241
+ // certificates, speaker cards and localised variants, on every export.
4242
+ function textOverflow() {
4243
+ try {
4244
+ const rep = (typeof app.textOverflowReport === 'function' && app.textOverflowReport()) || [];
4245
+ return rep.slice(0, 10).map(function(o) {
4246
+ return { code: 'text_overflow', message: (o.id || 'a text item') + ' (' + JSON.stringify(String(o.content || '').slice(0, 40)) + '): ' + (o.reason || 'overflows'), items: [o.id] };
4247
+ });
4248
+ } catch (e) { return []; }
4249
+ }
3211
4250
  function fidelity(fmt) {
4251
+ const own = standIns().concat(alphaLoss(fmt), textOverflow());
3212
4252
  try {
3213
- if (!app.exportEngine || typeof app.exportEngine.exportFidelity !== 'function') return {};
4253
+ if (!app.exportEngine || typeof app.exportEngine.exportFidelity !== 'function') {
4254
+ return { fidelity: { available: false, reason: 'this studio cannot check export fidelity — update PinePaper Studio.', ...(own.length ? { warnings: own } : {}) } };
4255
+ }
3214
4256
  const r = app.exportEngine.exportFidelity(fmt);
3215
- if (!r || !r.warnings || r.warnings.length === 0) return {};
3216
- return { fidelity: { warnings: r.warnings, checked: r.checked } };
3217
- } catch (e) { return {}; }
4257
+ if (!r) { return { fidelity: { available: false, reason: 'the fidelity check returned nothing.', ...(own.length ? { warnings: own } : {}) } }; }
4258
+ const warnings = (r.warnings || []).concat(own);
4259
+ return {
4260
+ fidelity: {
4261
+ available: true,
4262
+ warnings: warnings,
4263
+ checked: r.checked,
4264
+ ...(warnings.length === 0
4265
+ ? { note: 'this scene loses nothing to ' + fmt + '. That is a statement about THIS SCENE, not a claim that the format is lossless.' }
4266
+ : {}),
4267
+ },
4268
+ };
4269
+ } catch (e) {
4270
+ return { fidelity: { available: false, reason: (e && e.message) || 'the fidelity check threw.', ...(own.length ? { warnings: own } : {}) } };
4271
+ }
3218
4272
  }
3219
4273
 
3220
4274
  // Preflight: same resolved settings as the real export, but render nothing.
@@ -3256,8 +4310,8 @@ ${usesCanvasSize ? ` // 'auto': the canvas's own size, with the preset only as
3256
4310
  // drives motion during export — framing only fixes the output frame.
3257
4311
  let cameraDims = null;
3258
4312
  if (framing === 'camera') {
3259
- if (!['gif', 'mp4', 'webm'].includes(format)) {
3260
- return { success: false, platform, format, framing, error: 'framing: "camera" is only supported for video formats (gif, mp4, webm). Use format: "mp4" / "gif" / "webm" or omit framing.' };
4313
+ if (!['gif', 'apng', 'mp4', 'webm'].includes(format)) {
4314
+ return { success: false, platform, format, framing, error: 'framing: "camera" is only supported for animated formats (gif, apng, mp4, webm). Use format: "mp4" / "gif" / "webm" or omit framing.' };
3261
4315
  }
3262
4316
  const rr = app.relationRegistry;
3263
4317
  const hasWalkthrough = rr && typeof rr.hasCameraAnimation === 'function' && rr.hasCameraAnimation();
@@ -3302,10 +4356,65 @@ ${usesCanvasSize ? ` // 'auto': the canvas's own size, with the preset only as
3302
4356
  cameraDims = { width: camBase.width, height: camBase.height };
3303
4357
  }
3304
4358
 
3305
- let result = { success: false, platform, format, quality, framing };
4359
+ // AN EXPORTER ANSWERS A BLOB OR A RECORD AROUND ONE.
4360
+ //
4361
+ // Declared ABOVE the switch. They used to be consts inside the video case's
4362
+ // block, so the pdf case — which 04ea8d2 pointed at asBlob — threw
4363
+ // "asBlob is not defined" on every PDF export.
4364
+ //
4365
+ // exportPDF resolves to {blob, width, height, format}, and this passed
4366
+ // the whole record to FileReader.readAsDataURL — "parameter 1 is not of
4367
+ // type 'Blob'", every PDF export, with an error naming FileReader and
4368
+ // nothing about PDFs. The engine has the same normaliser for the same
4369
+ // reason (AgentMode._asBlob), so this matches its behaviour rather than
4370
+ // inventing a second convention.
4371
+ const asBlob = (r) => {
4372
+ if (typeof Blob !== 'undefined' && r instanceof Blob) return r;
4373
+ if (r && typeof Blob !== 'undefined' && r.blob instanceof Blob) return r.blob;
4374
+ // Duck-typed last resort: a studio may hand back something Blob-like
4375
+ // that fails instanceof across a realm boundary.
4376
+ if (r && typeof r.size === 'number' && typeof r.slice === 'function') return r;
4377
+ if (r && r.blob && typeof r.blob.size === 'number') return r.blob;
4378
+ return null;
4379
+ };
4380
+ const blobToDataUrl = (b) => new Promise(resolve => {
4381
+ const r = new FileReader();
4382
+ r.onloadend = () => resolve(r.result);
4383
+ r.readAsDataURL(b);
4384
+ });
3306
4385
 
4386
+ let result = { success: false, platform, format, quality, framing };
4387
+ // A STILL SHOWS WHAT IS ALIVE AT ITS MOMENT. Lifetimes that fall back to the
4388
+ // MCP's frame callback are applied by the frame loop, and a seek does not run
4389
+ // it — so a png at time 0.8 s drew nothing staged at 0.8 s, and an item past
4390
+ // its ttl still showed (retest). The visibility pass runs here for stills.
4391
+ if (['png', 'jpg', 'webp', 'svg', 'pdf'].indexOf(format) !== -1) {
4392
+ const __L = globalThis.__ppMcp;
4393
+ if (__L && __L.lifetimeApp === app && typeof __L.applyLifetimes === 'function') {
4394
+ try { __L.applyLifetimes(${stillTime !== undefined ? stillTime : "typeof app.playbackTime === 'number' ? app.playbackTime : 0"}); } catch (_) { /* the still renders either way */ }
4395
+ }
4396
+ }
4397
+ // The engine's report on the audio track of THIS export (FxTool b41860aa:
4398
+ // encoder delay compensated, padding past the picture dropped). Cleared
4399
+ // first so a report left by an earlier export is never read as this one's.
4400
+ const __vx = app.exportEngine && app.exportEngine.videoExporter;
4401
+ if (__vx && 'lastAudioReport' in __vx) __vx.lastAudioReport = null;
4402
+ if (__vx && 'lastVideoReport' in __vx) __vx.lastVideoReport = null;
4403
+ ${stillTime !== undefined ? `
4404
+ // A STILL AT A CHOSEN MOMENT (round 7 X, 1.57). Without this a png is
4405
+ // whatever frame the playhead is on — two identical builds gave PNGs that
4406
+ // differed in the animated streak while their MP4s were byte-identical.
4407
+ // Seek, render, and put the playhead back afterwards.
4408
+ const __prevT = typeof app.playbackTime === 'number' ? app.playbackTime : 0;
4409
+ if (typeof app.setPlaybackTime !== 'function') {
4410
+ return { success: false, platform, format, error: 'app.setPlaybackTime unavailable — this studio cannot render a still at a chosen time.' };
4411
+ }
4412
+ app.setPlaybackTime(${stillTime});
4413
+ if (typeof paper !== 'undefined' && paper.view && typeof paper.view.update === 'function') paper.view.update();
4414
+ ` : ''}
3307
4415
  try {
3308
- switch (format) {
4416
+ // jpg / webp render as png, then re-encode below.
4417
+ switch ((format === 'jpg' || format === 'webp') ? 'png' : format) {
3309
4418
  case 'svg':
3310
4419
  const svgString = app.exportAnimatedSVG ? app.exportAnimatedSVG() : app.exportSVGWithCSS();
3311
4420
  result = {
@@ -3320,6 +4429,62 @@ ${usesCanvasSize ? ` // 'auto': the canvas's own size, with the preset only as
3320
4429
  break;
3321
4430
 
3322
4431
  case 'png':
4432
+ ${region ? `// A REGION, not the whole board — carousel slices, crops. renderRegionToDataURL
4433
+ // re-renders that world rect offscreen without touching the live view;
4434
+ // it was reachable only from custom code. A differing output aspect is
4435
+ // covered by the engine, not stretched.
4436
+ if (typeof app.renderRegionToDataURL !== 'function') {
4437
+ result = { success: false, platform, format: 'png', error: 'app.renderRegionToDataURL unavailable — update PinePaper Studio to export a region.' };
4438
+ break;
4439
+ }
4440
+ {
4441
+ const rw = ${region.outputWidth ?? Math.round(region.width)}, rh = ${region.outputHeight ?? Math.round(region.height)};
4442
+ // OWNERSHIP BY CENTRE (1.78, FxTool 78ec5916): excludeForeign hides, for
4443
+ // this render, a top-level item that reaches in but is centred outside —
4444
+ // a neighbouring card's overflowing headline. Older studios ignore the
4445
+ // fourth argument, and leave lastRegionExcluded unset.
4446
+ // @engine-surface-exempt lastRegionExcluded — a report property, not a
4447
+ // method, so it cannot be typeof-probed; read only through Array.isArray.
4448
+ // Branch-only until 78ec5916 reaches origin/main.
4449
+ if ('lastRegionExcluded' in app) app.lastRegionExcluded = null;
4450
+ const regionUrl = app.renderRegionToDataURL(new paper.Rectangle(${region.x}, ${region.y}, ${region.width}, ${region.height}), rw, rh${region.excludeForeign === false ? '' : ', { excludeForeign: true }'});
4451
+ const __excluded = Array.isArray(app.lastRegionExcluded) ? app.lastRegionExcluded.filter(function(id) { return id; }) : null;
4452
+ // ITEMS THAT CROSS THE REGION'S EDGE (round 9 II, 1.78). In the sheet
4453
+ // recipe — one big canvas, one region per card — text that overflows
4454
+ // its cell spills into the next card's export (up to 5,826 foreign px),
4455
+ // silently. A region cannot know which card an item belongs to, so it
4456
+ // cannot clip per card; it can name every item that is only partly
4457
+ // inside it, which is exactly the overflow in either direction.
4458
+ const __rx = ${region.x}, __ry = ${region.y}, __rr = ${region.x + region.width}, __rb = ${region.y + region.height};
4459
+ const crossing = [], coveringExcluded = [];
4460
+ if (app.itemRegistry && typeof app.itemRegistry.getAll === 'function') {
4461
+ app.itemRegistry.getAll().forEach(function(e) {
4462
+ const b = e && e.item && e.item.visible !== false && e.item.bounds;
4463
+ if (!b || !(b.width > 0)) return;
4464
+ const r = b.x + b.width, bt = b.y + b.height;
4465
+ const intersects = b.x < __rr && r > __rx && b.y < __rb && bt > __ry;
4466
+ const inside = b.x >= __rx - 0.5 && r <= __rr + 0.5 && b.y >= __ry - 0.5 && bt <= __rb + 0.5;
4467
+ const covers = b.x <= __rx && r >= __rr && b.y <= __ry && bt >= __rb; // a full-bleed backdrop is not overflow
4468
+ if (intersects && !inside && !covers && crossing.length < 20) crossing.push(e.itemId);
4469
+ if (covers && __excluded && (__excluded.indexOf(e.itemId) >= 0 || (e.item.data && __excluded.indexOf(e.item.data.id) >= 0))) coveringExcluded.push(e.itemId);
4470
+ });
4471
+ }
4472
+ result = { success: true, platform, format: 'png', data: regionUrl, mimeType: 'image/png',
4473
+ size: Math.round(String(regionUrl).length * 0.75), dimensions: { width: rw, height: rh },
4474
+ region: ${JSON.stringify({ x: region.x, y: region.y, width: region.width, height: region.height })} };
4475
+ if (__excluded && __excluded.length) {
4476
+ result.excludedItems = __excluded;
4477
+ result.excludedNote = 'centred outside this region, so treated as belonging to a neighbouring region and left out of this image.';
4478
+ }
4479
+ if (coveringExcluded.length) {
4480
+ result.warning = 'backdrop item(s) ' + coveringExcluded.join(', ') + ' cover this whole region but are centred outside it, so they were left out as foreign — this image has no background from them. Re-export with region.excludeForeign: false, or give each card its own backdrop.';
4481
+ }
4482
+ if (crossing.length) {
4483
+ result.crossingItems = crossing;
4484
+ result.warning = (result.warning ? result.warning + ' ' : '') + crossing.length + ' item(s) are only partly inside this region, so part of them is cut off here or spills into a neighbouring region: ' + crossing.join(', ') + '. In a sheet of cards this is text overflowing its card — shorten it, shrink it, or wrap it.';
4485
+ }
4486
+ }
4487
+ break;` : ''}
3323
4488
  if (app.exportEngine && app.exportEngine.exportPNG) {
3324
4489
  // PNG IGNORED THE PLATFORM ENTIRELY.
3325
4490
  //
@@ -3367,10 +4532,52 @@ ${usesCanvasSize ? ` // 'auto': the canvas's own size, with the preset only as
3367
4532
  }
3368
4533
  break;
3369
4534
 
4535
+ case 'html5-ad':
4536
+ case 'playable': {
4537
+ // AN AD FROM THE WIDGET PAGE (8.21 / 8.24). The page is the studio's
4538
+ // standalone widget; the server adds the network's wrapper and builds
4539
+ // the zip (src/utils/ad-package.ts). Here: the page, its size, the CTA
4540
+ // box in percent of the canvas, and a backup image for html5-ad.
4541
+ if (!app.exportEngine || typeof app.exportEngine.exportWidgetHTML !== 'function') {
4542
+ result = { success: false, platform, format, error: 'this studio cannot export its standalone widget page (no exportWidgetHTML), which an ad is built from.' };
4543
+ break;
4544
+ }
4545
+ // The artboard, never the canvas element: its backing store is scaled
4546
+ // by the renderer, and an ad's size is what was asked for.
4547
+ const __as = (typeof app.getCanvasSize === 'function' && app.getCanvasSize()) || app.canvasSize || { width: 0, height: 0 };
4548
+ const __cw = __as.width, __ch = __as.height;
4549
+ let __cta = null;
4550
+ ${adOpts?.ctaItemId ? `{
4551
+ const __ce = app.itemRegistry && app.itemRegistry.get(${JSON.stringify(adOpts.ctaItemId)});
4552
+ const __cb = __ce && __ce.item && __ce.item.bounds;
4553
+ if (!__cb || !(__cb.width > 0) || !(__cw > 0)) {
4554
+ result = { success: false, platform, format, error: ${JSON.stringify(`ad.ctaItemId "${adOpts.ctaItemId}" is not an item on the canvas with a size — nothing was exported. List items with pinepaper_get_items.`)} };
4555
+ break;
4556
+ }
4557
+ __cta = { left: __cb.x / __cw * 100, top: __cb.y / __ch * 100, width: __cb.width / __cw * 100, height: __cb.height / __ch * 100 };
4558
+ }` : ''}
4559
+ const __w = await app.exportEngine.exportWidgetHTML({ download: false });
4560
+ if (!__w || typeof __w.html !== 'string' || !__w.html) {
4561
+ result = { success: false, platform, format, error: 'the studio returned no widget page to build the ad from.' };
4562
+ break;
4563
+ }
4564
+ let __backup = null;
4565
+ if (format === 'html5-ad' && typeof app.captureFrameDataURL === 'function') {
4566
+ try { __backup = app.captureFrameDataURL(1); } catch (_) { __backup = null; }
4567
+ }
4568
+ // The background the PNG export uses (the canvas element's). The widget
4569
+ // page prefers another record that can be stale, which served a white
4570
+ // page under white text (8.9 again); the server pins this one.
4571
+ const __bgc = (app.canvasEl && app.canvasEl.style && app.canvasEl.style.backgroundColor) || null;
4572
+ result = { success: true, platform, format, adPage: __w.html, adSize: { width: __cw, height: __ch }, cta: __cta, backupImage: __backup, background: __bgc };
4573
+ break;
4574
+ }
4575
+
3370
4576
  case 'gif':
4577
+ case 'apng':
3371
4578
  case 'mp4':
3372
4579
  case 'webm': {
3373
- const videoMimeType = { mp4: 'video/mp4', webm: 'video/webm', gif: 'image/gif' }[format];
4580
+ const videoMimeType = { mp4: 'video/mp4', webm: 'video/webm', gif: 'image/gif', apng: 'image/apng' }[format];
3374
4581
  // FxTool VideoExporter._calculateBitrate multiplies a per-resolution
3375
4582
  // base by settings.quality (0.5-1.0). Our existing per-tier compression
3376
4583
  // values (0.6 / 0.85 / 0.95) map directly to that scale. Without this,
@@ -3380,12 +4587,10 @@ ${usesCanvasSize ? ` // 'auto': the canvas's own size, with the preset only as
3380
4587
  // width/height ride along ONLY when a scale was asked for. The buffered
3381
4588
  // fallback paths otherwise export at canvas size, and quietly changing
3382
4589
  // that for every existing caller is not what a new optional knob does.
3383
- const baseVideoSettings = { format, fps: settings.fps, quality: settings.compression, duration: ${videoDuration}${scale !== undefined && scale !== 1 ? ', width: dimensions.width, height: dimensions.height' : ''} };
3384
- const blobToDataUrl = (b) => new Promise(resolve => {
3385
- const r = new FileReader();
3386
- r.onloadend = () => resolve(r.result);
3387
- r.readAsDataURL(b);
3388
- });
4590
+ // loop rides in the BASE settings for a gif, so every route carries it:
4591
+ // the direct one below, and also camera framing and a studio without
4592
+ // _quickExportVideo, which build from these and used to drop it.
4593
+ const baseVideoSettings = { format, fps: settings.fps, quality: settings.compression, duration: ${videoDuration}${scale !== undefined && scale !== 1 ? ', width: dimensions.width, height: dimensions.height' : ''}${gifLoop !== undefined ? `, ...(format === 'gif' || format === 'apng' ? { loop: ${JSON.stringify(gifLoop)} } : {})` : ''}${encodeOptsJs}${transparent !== undefined ? `, transparent: ${transparent}` : ''} };
3389
4594
 
3390
4595
  // THE EXPORT STORE. A long export cannot come back as one base64
3391
4596
  // string: base64 of a gigabyte is larger than the gigabyte, and it
@@ -3403,7 +4608,7 @@ ${usesCanvasSize ? ` // 'auto': the canvas's own size, with the preset only as
3403
4608
  && typeof app.exportEngine.readExport === 'function')
3404
4609
  ? app.exportEngine : null;
3405
4610
 
3406
- if (store && format !== 'gif') {
4611
+ if (store && format !== 'gif' && format !== 'apng') {
3407
4612
  const stored = await store.exportToStore({
3408
4613
  format,
3409
4614
  duration: baseVideoSettings.duration,
@@ -3414,7 +4619,10 @@ ${usesCanvasSize ? ` // 'auto': the canvas's own size, with the preset only as
3414
4619
  // VideoEncoder.configure.
3415
4620
  quality: settings.compression,
3416
4621
  width: cameraDims ? cameraDims.width : dimensions.width,
3417
- height: cameraDims ? cameraDims.height : dimensions.height,
4622
+ height: cameraDims ? cameraDims.height : dimensions.height,${encodeOptsJs ? `
4623
+ // Broadcast / bitrate. A store that rebuilds its options without
4624
+ // them is caught by the video report below, not assumed.
4625
+ ${encodeOptsJs.slice(2)},` : ''}
3418
4626
  });
3419
4627
  if (!stored || stored.ok === false) {
3420
4628
  result = { success: false, platform, format, error: (stored && stored.reason) || 'the export store refused the export without saying why' };
@@ -3450,7 +4658,11 @@ ${usesCanvasSize ? ` // 'auto': the canvas's own size, with the preset only as
3450
4658
  // refusing.
3451
4659
  const INLINE_MAX_BYTES = ${INLINE_MAX_BYTES};
3452
4660
  const mb = (n) => (n / (1024 * 1024)).toFixed(1);
3453
- const deliver = async (blob) => {
4661
+ const deliver = async (raw) => {
4662
+ // Normalise first: a {blob, …} record would otherwise be reported as
4663
+ // "streamed to a file instead of returning bytes", which is a true
4664
+ // sentence about the wrong problem.
4665
+ const blob = asBlob(raw) || raw;
3454
4666
  if (!blob || typeof blob.size !== 'number' || typeof blob.slice !== 'function') {
3455
4667
  return {
3456
4668
  success: false, platform, format,
@@ -3469,7 +4681,20 @@ ${usesCanvasSize ? ` // 'auto': the canvas's own size, with the preset only as
3469
4681
 
3470
4682
  // Camera framing requires going direct to videoExporter so width/height
3471
4683
  // pass through — _quickExportVideo strips dim fields.
3472
- if (cameraDims && app.exportEngine && app.exportEngine.videoExporter) {
4684
+ // ANIMATED PNG (8.12, FxTool 76312a55): stepped PNG frames with full
4685
+ // alpha, transparent unless transparent: false. Asked of the exporter
4686
+ // directly; a studio that lacks it says so in its capability table, and
4687
+ // is refused by name rather than handed a format it would reject.
4688
+ const __apngVx = format === 'apng' ? (app.exportEngine && app.exportEngine.videoExporter) : null;
4689
+ if (format === 'apng' && !(__apngVx && typeof __apngVx.export === 'function' && __apngVx.constructor
4690
+ && typeof __apngVx.constructor.getCapabilities === 'function' && __apngVx.constructor.getCapabilities().apng)) {
4691
+ result = { success: false, platform, format, error: 'this studio cannot export APNG (it needs an engine with animated PNG). For animation with alpha, export a png sequence (pinepaper_interchange export_png_sequence with options.transparent: true); gif keeps only 1-bit transparency.' };
4692
+ } else if (format === 'apng') {
4693
+ const blob = await __apngVx.export(cameraDims ? { ...baseVideoSettings, width: cameraDims.width, height: cameraDims.height } : baseVideoSettings);
4694
+ result = await deliver(blob);
4695
+ if (result.success) result.transparent = ${transparent !== false};
4696
+ if (result.success && cameraDims) result.dimensions = cameraDims;
4697
+ } else if (cameraDims && app.exportEngine && app.exportEngine.videoExporter) {
3473
4698
  const blob = await app.exportEngine.videoExporter.export({ ...baseVideoSettings, width: cameraDims.width, height: cameraDims.height });
3474
4699
  result = await deliver(blob);
3475
4700
  if (result.success) result.dimensions = cameraDims;
@@ -3481,12 +4706,43 @@ ${usesCanvasSize ? ` // 'auto': the canvas's own size, with the preset only as
3481
4706
  } else if (app.exportEngine && app.exportEngine._quickExportVideo) {
3482
4707
  // GIF path (working): _quickExportVideo forwards gifQuality to
3483
4708
  // gif.js. Also serves as the fallback if videoExporter is absent.
3484
- const videoResult = await app.exportEngine._quickExportVideo(format, baseVideoSettings, false);
4709
+ ${gifLoop !== undefined ? `// A LOOP COUNT (8.30): _quickExportVideo rebuilds its settings without
4710
+ // it, so a GIF with loop set goes straight to videoExporter.export.
4711
+ // true = forever, false / 0 / 1 = once, n = n plays (engine 952d6d35).
4712
+ const videoResult = (format === 'gif' && app.exportEngine.videoExporter && typeof app.exportEngine.videoExporter.export === 'function')
4713
+ ? { blob: await app.exportEngine.videoExporter.export(Object.assign({}, baseVideoSettings, { format: 'gif', loop: ${JSON.stringify(gifLoop)} })) }
4714
+ : await app.exportEngine._quickExportVideo(format, baseVideoSettings, false);` : `const videoResult = await app.exportEngine._quickExportVideo(format, baseVideoSettings, false);`}
3485
4715
  if (videoResult && videoResult.blob) {
3486
4716
  result = await deliver(videoResult.blob);
3487
4717
  } else {
3488
4718
  result = { success: false, error: format.toUpperCase() + ' export returned no data' };
3489
4719
  }
4720
+ ${maxBytes !== undefined ? `// A BYTE BUDGET (round 9 GG, 8.31 — email wants <= 1 MB). gif.js has no
4721
+ // colour-count knob, so size is the lever: GIF bytes scale roughly with
4722
+ // pixel count, so the frame is shrunk by sqrt(budget / size) with a
4723
+ // margin and re-encoded, at most twice. Every attempt is reported.
4724
+ if (format === 'gif' && result && result.success && result.size > ${maxBytes}) {
4725
+ const attempts = [{ width: dimensions.width, height: dimensions.height, bytes: result.size }];
4726
+ let w = dimensions.width, h = dimensions.height;
4727
+ for (let k = 0; k < 2 && result.success && result.size > ${maxBytes}; k++) {
4728
+ const f = Math.sqrt(${maxBytes} / result.size) * 0.9;
4729
+ w = Math.max(16, Math.round(w * f / 2) * 2); h = Math.max(16, Math.round(h * f / 2) * 2);
4730
+ // videoExporter.export, not _quickExportVideo: the latter rebuilds
4731
+ // its settings without width / height, so a "smaller" retry
4732
+ // through it came out the same size.
4733
+ if (!app.exportEngine.videoExporter || typeof app.exportEngine.videoExporter.export !== 'function') break;
4734
+ const again = await app.exportEngine.videoExporter.export(Object.assign({}, baseVideoSettings, { format: 'gif', width: w, height: h }${gifLoop !== undefined ? `, { loop: ${JSON.stringify(gifLoop)} }` : ''}));
4735
+ if (!again) break;
4736
+ result = await deliver(again);
4737
+ attempts.push({ width: w, height: h, bytes: result.size });
4738
+ }
4739
+ result.budget = { maxBytes: ${maxBytes}, met: !!(result.success && result.size <= ${maxBytes}), attempts: attempts };
4740
+ if (result.success && result.size > ${maxBytes}) {
4741
+ result.warning = 'still ' + result.size + ' bytes after shrinking to ' + w + 'x' + h + ' — over the ' + ${maxBytes} + '-byte budget. Lower fps, shorten duration, or use quality "draft".';
4742
+ } else if (result.success) {
4743
+ result.dimensions = { width: w, height: h };
4744
+ }
4745
+ }` : ''}
3490
4746
  } else if (app.exportEngine && app.exportEngine.videoExporter) {
3491
4747
  const blob = await app.exportEngine.videoExporter.export(baseVideoSettings);
3492
4748
  result = await deliver(blob);
@@ -3497,14 +4753,134 @@ ${usesCanvasSize ? ` // 'auto': the canvas's own size, with the preset only as
3497
4753
  }
3498
4754
 
3499
4755
  case 'pdf':
4756
+ ${pdfOpts?.pages ? `// ONE PAGE PER SCENE (round 7 Z, 8.16). exportPDF renders the current
4757
+ // canvas only, so a deck of 8 saved scenes exported 1 page. Built here
4758
+ // the way exportPDF builds its one page — the engine's jsPDF, a frame
4759
+ // from captureFrameDataURL, the scene's own size at the canvas DPI —
4760
+ // one scene at a time, and the scene the user was on is put back.
4761
+ {
4762
+ const SM = app.sceneManager;
4763
+ if (!SM || typeof SM.listScenes !== 'function' || typeof SM.loadScene !== 'function') {
4764
+ result = { success: false, platform, format: 'pdf', error: 'scenes are unavailable on this studio (app.sceneManager) — a multi-page PDF needs saved scenes.' }; break;
4765
+ }
4766
+ if (!app.exportEngine || typeof app.exportEngine._loadPDFLibraries !== 'function' || typeof app.captureFrameDataURL !== 'function') {
4767
+ result = { success: false, platform, format: 'pdf', error: 'this studio cannot build a multi-page PDF (no PDF library loader or frame capture) — update PinePaper Studio.' }; break;
4768
+ }
4769
+ const all = SM.listScenes();
4770
+ const wanted = ${JSON.stringify(pdfOpts.pages)};
4771
+ // A page is { id, size? }. Scenes do not record their canvas size —
4772
+ // their "dimensions" is the browser viewport and loadScene does not
4773
+ // resize the canvas — so without a size every page is the current
4774
+ // canvas (the retest: three pages, all square).
4775
+ const pageSpecs = wanted === 'scenes'
4776
+ ? all.map(function(sc) { return { id: sc.id }; })
4777
+ : wanted.map(function(w) { return typeof w === 'string' ? { id: w } : { id: w.sceneId, size: { width: w.width, height: w.height } }; });
4778
+ const ids = pageSpecs.map(function(p) { return p.id; });
4779
+ const sized = pageSpecs.some(function(p) { return p.size; });
4780
+ const missing = ids.filter(function(id) { return !all.some(function(sc) { return sc.id === id; }); });
4781
+ if (!ids.length || missing.length) {
4782
+ result = { success: false, platform, format: 'pdf', error: ids.length ? 'no saved scene ' + missing.join(', ') + '. Saved: ' + all.map(function(sc) { return sc.id; }).join(', ') : 'there are no saved scenes — save each page with pinepaper_manage_scenes first.' }; break;
4783
+ }
4784
+ const back = SM.currentSceneId;
4785
+ const backSize = app.canvasSize ? { width: app.canvasSize.width, height: app.canvasSize.height } : null;
4786
+ const lib = await app.exportEngine._loadPDFLibraries();
4787
+ const targetDpi = ${pdfOpts.dpi ?? 'settings.dpi'};
4788
+ // JPEG pages: a PNG per page made a 3-slide deck 34 MB.
4789
+ const toJpeg = function(url) {
4790
+ return new Promise(function(resolve) {
4791
+ const img = new Image();
4792
+ img.onload = function() {
4793
+ const c = document.createElement('canvas');
4794
+ c.width = img.naturalWidth; c.height = img.naturalHeight;
4795
+ const x = c.getContext('2d');
4796
+ x.fillStyle = '#ffffff'; x.fillRect(0, 0, c.width, c.height);
4797
+ x.drawImage(img, 0, 0);
4798
+ resolve(c.toDataURL('image/jpeg', settings.compression));
4799
+ };
4800
+ img.onerror = function() { resolve(null); };
4801
+ img.src = url;
4802
+ });
4803
+ };
4804
+ let doc = null;
4805
+ const pagesOut = [];
4806
+ // A SEARCHABLE TEXT LAYER PER PAGE (8.10, FxTool 3e22c0df): the
4807
+ // engine lays the loaded scene's text invisibly over the page box;
4808
+ // the per-page reports are summed into result.pdf.
4809
+ const __wantLayer = ${pdfOpts.searchableText !== false};
4810
+ const __layer = __wantLayer && typeof app.exportEngine.addTextLayer === 'function';
4811
+ const __tl = { written: 0, skipped: 0, reasons: [] };
4812
+ try {
4813
+ for (const spec of pageSpecs) {
4814
+ const id = spec.id;
4815
+ await SM.loadScene(id);
4816
+ if (spec.size && typeof app.setCanvasSize === 'function') app.setCanvasSize({ width: spec.size.width, height: spec.size.height });
4817
+ const cs = app.canvasSize || { width: app.canvasEl.width, height: app.canvasEl.height };
4818
+ const dpi = (typeof app.getDPI === 'function' && app.getDPI()) || 96;
4819
+ const wMM = cs.width / dpi * 25.4, hMM = cs.height / dpi * 25.4;
4820
+ const orient = wMM > hMM ? 'landscape' : 'portrait';
4821
+ const png = app.captureFrameDataURL(Math.max(1, targetDpi / dpi));
4822
+ const jpg = await toJpeg(png);
4823
+ if (!doc) doc = new lib.jsPDF({ orientation: orient, unit: 'mm', format: [wMM, hMM] });
4824
+ else doc.addPage([wMM, hMM], orient);
4825
+ if (jpg) doc.addImage(jpg, 'JPEG', 0, 0, wMM, hMM); else doc.addImage(png, 'PNG', 0, 0, wMM, hMM);
4826
+ if (__layer) {
4827
+ try {
4828
+ const tr = await app.exportEngine.addTextLayer(doc, { x: 0, y: 0, width: wMM, height: hMM });
4829
+ if (tr && tr.ok !== false) { __tl.written += tr.linesWritten || 0; __tl.skipped += tr.linesSkipped || 0; }
4830
+ else __tl.reasons.push(id + ': ' + ((tr && tr.reason) || 'refused'));
4831
+ } catch (e) { __tl.reasons.push(id + ': ' + e.message); }
4832
+ }
4833
+ pagesOut.push({ sceneId: id, widthMM: Math.round(wMM * 10) / 10, heightMM: Math.round(hMM * 10) / 10 });
4834
+ }
4835
+ } finally {
4836
+ if (sized && backSize && typeof app.setCanvasSize === 'function') { try { app.setCanvasSize(backSize); } catch (_) { /* best effort */ } }
4837
+ if (back && typeof SM.loadScene === 'function') { try { await SM.loadScene(back); } catch (_) { /* reported by the page count */ } }
4838
+ }
4839
+ const blob = doc.output('blob');
4840
+ const dataUrl = await blobToDataUrl(blob);
4841
+ result = { success: true, platform, format: 'pdf', data: dataUrl, mimeType: 'application/pdf', size: blob.size, pages: pagesOut.length, pageList: pagesOut,
4842
+ pdf: !__wantLayer ? { searchableText: false, reason: 'off' }
4843
+ : !__layer ? { searchableText: false, reason: 'this studio adds no text layer to multi-page PDFs' }
4844
+ : Object.assign({ searchableText: __tl.written > 0 || __tl.reasons.length < pagesOut.length, linesWritten: __tl.written, linesSkipped: __tl.skipped },
4845
+ __tl.reasons.length ? { pagesWithoutLayer: __tl.reasons } : {},
4846
+ __tl.written === 0 && __tl.reasons.length === pagesOut.length ? { reason: __tl.reasons.join('; ') } : {}) };
4847
+ if (!sized) result.warning = 'every page used the current canvas size: saved scenes do not record theirs. To give a page its own size, pass pages as [{sceneId, width, height}, …].';
4848
+ }
4849
+ break;` : ''}
3500
4850
  if (app.exportEngine && app.exportEngine.exportPDF) {
3501
- const blob = await app.exportEngine.exportPDF({ dpi: settings.dpi });
3502
- const reader = new FileReader();
3503
- const dataUrl = await new Promise(resolve => {
3504
- reader.onloadend = () => resolve(reader.result);
3505
- reader.readAsDataURL(blob);
4851
+ // Print options reach the engine (they were engine-only before), and
4852
+ // download:false, because the tool delivers the bytes itself.
4853
+ // lastPdfReport is a report PROPERTY (9917cfa5), read through the
4854
+ // engine object like the exporter's audio / video reports, and only
4855
+ // when present.
4856
+ const __pee = app.exportEngine;
4857
+ if ('lastPdfReport' in __pee) __pee.lastPdfReport = null;
4858
+ const pdfOut = await app.exportEngine.exportPDF({
4859
+ dpi: ${pdfOpts?.dpi ?? 'settings.dpi'},
4860
+ download: false,${pdfOpts?.searchableText === false ? '\n searchableText: false,' : ''}${pdfOpts?.paperFormat ? `\n format: ${JSON.stringify(pdfOpts.paperFormat)},` : ''}${pdfOpts?.orientation ? `\n orientation: ${JSON.stringify(pdfOpts.orientation)},` : ''}${pdfOpts?.bleed !== undefined ? `\n includeBleed: ${pdfOpts.bleed > 0}, bleed: ${pdfOpts.bleed},` : ''}${pdfOpts?.trimMarks !== undefined ? `\n trimMarks: ${pdfOpts.trimMarks},` : ''}
3506
4861
  });
3507
- result = { success: true, platform, format: 'pdf', data: dataUrl, mimeType: 'application/pdf', size: blob.size, dpi: settings.dpi };
4862
+ const blob = asBlob(pdfOut);
4863
+ if (!blob) {
4864
+ const got = pdfOut === null ? 'null'
4865
+ : typeof pdfOut === 'object' ? 'an object with keys [' + Object.keys(pdfOut).join(', ') + ']'
4866
+ : typeof pdfOut;
4867
+ result = { success: false, platform, format: 'pdf',
4868
+ error: 'the PDF export did not produce a file: expected a Blob, got ' + got + '.' };
4869
+ break;
4870
+ }
4871
+ const dataUrl = await blobToDataUrl(blob);
4872
+ result = {
4873
+ success: true, platform, format: 'pdf', data: dataUrl,
4874
+ mimeType: 'application/pdf', size: blob.size, dpi: settings.dpi,
4875
+ // The record carries the page geometry the engine chose; it is the
4876
+ // only place a caller can learn it.
4877
+ ...(pdfOut && pdfOut.width ? { dimensions: { width: pdfOut.width, height: pdfOut.height } } : {}),
4878
+ ...(pdfOut && pdfOut.format ? { paperFormat: pdfOut.format } : {}),${pdfOpts?.paperFormat && pdfOpts.paperFormat !== 'custom' && ((pdfOpts.bleed ?? 0) > 0 || pdfOpts.trimMarks) ? `
4879
+ // Bleed and trim marks on a NAMED paper size: the studio keeps the
4880
+ // page at that size and centres the art, so neither appears (prod
4881
+ // retest: MediaBox = TrimBox = A4, no marks). Said, not silent.
4882
+ warnings: [${JSON.stringify(`bleed / trimMarks were passed with paperFormat '${pdfOpts.paperFormat}', and this studio applies them only to paperFormat 'custom' (page = canvas + bleed): the page stayed ${pdfOpts.paperFormat} with no bleed and no marks. For print with bleed, size the canvas with a PRINT preset (agent_start_job canvasPreset 'print-a4' / 'print-letter' / 'print-a4-landscape' / 'print-letter-landscape', which carry 300 dpi — a plain 2480x3508 canvas is read at 96 dpi and comes out ~656 mm wide) and pass paperFormat 'custom'.`)}],` : ''}
4883
+ };
3508
4884
  } else {
3509
4885
  result = { success: false, error: 'PDF export failed' };
3510
4886
  }
@@ -3520,7 +4896,154 @@ ${usesCanvasSize ? ` // 'auto': the canvas's own size, with the preset only as
3520
4896
  // One site, after every branch has set its result: a successful export says
3521
4897
  // what this scene lost to this format, rather than the caller finding out by
3522
4898
  // opening the file. Only when there is something to say — see fidelity().
4899
+ // RE-ENCODE A STILL FOR A BYTE BUDGET. The png path rendered it (whole board
4900
+ // or region); the browser's own encoder makes jpg / webp at the tier's
4901
+ // compression. jpg has no alpha, so it is flattened onto the scene background
4902
+ // (white when there is none) rather than left to the encoder's black.
4903
+ if (result && result.success && (format === 'jpg' || format === 'webp') && typeof result.data === 'string') {
4904
+ const mime = format === 'jpg' ? 'image/jpeg' : 'image/webp';
4905
+ try {
4906
+ const encoded = await new Promise(function(resolve, reject) {
4907
+ const img = new Image();
4908
+ img.onload = function() {
4909
+ const c = document.createElement('canvas');
4910
+ c.width = img.naturalWidth; c.height = img.naturalHeight;
4911
+ const x = c.getContext('2d');
4912
+ // The scene's own background when one is set — white text on a
4913
+ // transparent board vanished into a white flatten. White otherwise.
4914
+ if (mime === 'image/jpeg') {
4915
+ const bg = app.canvasEl && app.canvasEl.style && app.canvasEl.style.backgroundColor;
4916
+ x.fillStyle = (bg && !/^(transparent|rgba\\([^)]*,\\s*0\\))$/i.test(bg)) ? bg : '#ffffff';
4917
+ x.fillRect(0, 0, c.width, c.height);
4918
+ }
4919
+ x.drawImage(img, 0, 0);
4920
+ resolve(c.toDataURL(mime, settings.compression));
4921
+ };
4922
+ img.onerror = function() { reject(new Error('the rendered frame could not be decoded for re-encoding')); };
4923
+ img.src = result.data;
4924
+ });
4925
+ if (String(encoded).indexOf('data:' + mime) !== 0) {
4926
+ result = { success: false, platform, format, error: 'this browser cannot encode ' + format + ' (it returned ' + String(encoded).slice(5, 20) + '…).' };
4927
+ } else {
4928
+ result = Object.assign({}, result, { format, data: encoded, mimeType: mime, size: Math.round(String(encoded).length * 0.75), encodeQuality: settings.compression });
4929
+ }
4930
+ } catch (e) {
4931
+ result = { success: false, platform, format, error: (e && e.message) || String(e) };
4932
+ }
4933
+ }
4934
+
3523
4935
  if (result && result.success) Object.assign(result, fidelity(format));
4936
+ if (result && result.success && __fit) result.platformFit = __fit;
4937
+ // AUDIO TIMING, WHERE THE STUDIO REPORTS IT (6.27). An MP4's AAC track used
4938
+ // to start ~44 ms late and run past the picture — a 6.000 s bumper with sound
4939
+ // then failed its cap. The engine now reports the track; it is surfaced as
4940
+ // result.audio, and its warning (sound in the trimmed lead-in) and any audio
4941
+ // longer than the picture go into fidelity.warnings, where a caller looks.
4942
+ // WHAT THE ENCODER WAS GIVEN AND WHAT IT MADE (FxTool 16719759, 8.34-8.36).
4943
+ // The browser's H.264 treats a bitrate as a ceiling, so an 8 Mbps broadcast
4944
+ // request can come out at 1.8 — the engine measures it and warns. A request
4945
+ // the report does not reflect (an engine without the options, or a route
4946
+ // that rebuilt its settings without them) is named, not assumed.
4947
+ // A PDF'S TEXT LAYER (8.10, FxTool 9917cfa5): invisible text at each line's
4948
+ // drawn position, so the PDF is searchable and screen-readable. The built-in
4949
+ // font is WinAnsi only, so Arabic / Hebrew / CJK lines are left out of the
4950
+ // layer (drawn, but not selectable) — counted by the engine, said here.
4951
+ const __ee = app.exportEngine;
4952
+ const __pr = format === 'pdf' && ((result && result.pdf) || (__ee && __ee.lastPdfReport));
4953
+ if (result && result.success && __pr && typeof __pr === 'object') {
4954
+ result.pdf = __pr;
4955
+ const __pw = [];
4956
+ // Which scripts a PDF can carry depends on the studio: older ones have
4957
+ // only the built-in Latin font, newer ones embed the scene's fonts. So the
4958
+ // cause is the engine's report (fonts it could not embed), not a claim here.
4959
+ const __ff = (__pr.fonts && Array.isArray(__pr.fonts.families)) ? __pr.fonts.families.filter(function(f) { return f && f.reason; }) : [];
4960
+ const __fnames = __ff.slice(0, 4).map(function(f) { return f.family + ' (' + f.reason + ')'; }).join('; ');
4961
+ if (__pr.searchableText && __pr.linesSkipped > 0) __pw.push({ code: 'pdf_text_lines_skipped', message: __pr.linesSkipped + ' text line(s) are not in the searchable layer — no font in the PDF covers their characters — so they are drawn but cannot be selected, searched or read by a screen reader.' + (__fnames ? ' Fonts that could not be embedded: ' + __fnames + '.' : '') });
4962
+ if (__ff.length && !(__pr.linesSkipped > 0)) __pw.push({ code: 'pdf_font_not_embedded', message: 'these fonts could not be embedded, so their text uses a fallback font in the searchable layer: ' + __fnames + '.' });
4963
+ if (__pr.searchableText && __pr.pagesWithoutLayer) __pw.push({ code: 'pdf_no_text_layer', message: 'some pages have no searchable text layer — ' + __pr.pagesWithoutLayer.join('; ') });
4964
+ if (!__pr.searchableText && __pr.reason && __pr.reason !== 'off') __pw.push({ code: 'pdf_no_text_layer', message: 'no searchable text layer was written (' + __pr.reason + '): the text is drawn but cannot be selected, searched or read by a screen reader.' });
4965
+ if (__pw.length) {
4966
+ result.fidelity = result.fidelity || { warnings: [] };
4967
+ result.fidelity.warnings = (result.fidelity.warnings || []).concat(__pw);
4968
+ if (result.fidelity.note) delete result.fidelity.note;
4969
+ }
4970
+ }
4971
+ const __vr = __vx && __vx.lastVideoReport;
4972
+ if (result && result.success && __vr && typeof __vr === 'object') result.video = __vr;${headroomDefaulted ? `
4973
+ // The applied value is the engine's own report where it gives one
4974
+ // (5adfcf1c: lastVideoReport.headroom), so the result and the report agree.
4975
+ if (result && result.success) result.broadcastHeadroom = { value: (__vr && typeof __vr.headroom === 'number') ? __vr.headroom : 12, defaulted: true, note: 'broadcast defaults to 12 luma codes of headroom, which keeps encoder ringing legal; pass broadcastHeadroom: 0 for the full range.' };` : ''}
4976
+ // AN ODD SIDE ROUNDED UP (FxTool e64a5260). H.264 needs even dimensions, so
4977
+ // 1200x675 came out 1200x676 — and platform specs are exact-pixel. The
4978
+ // result's dimensions become what the file IS, and fidelity says why.
4979
+ const __vd = __vr && __vr.dimensions;
4980
+ if (result && result.success && __vd && Array.isArray(__vd.requested) && Array.isArray(__vd.output)
4981
+ && (__vd.requested[0] !== __vd.output[0] || __vd.requested[1] !== __vd.output[1])) {
4982
+ result.dimensions = { width: __vd.output[0], height: __vd.output[1] };
4983
+ result.fidelity = result.fidelity || { warnings: [] };
4984
+ result.fidelity.warnings = (result.fidelity.warnings || []).concat([{ code: 'dimensions_rounded',
4985
+ message: 'the file is ' + __vd.output[0] + 'x' + __vd.output[1] + ', not the requested ' + __vd.requested[0] + 'x' + __vd.requested[1] + ': H.264 needs even dimensions, so an odd side was rounded up by 1 px (the extra row / column is background). For an exact-pixel spec, use an even canvas size, or crop the extra line when re-encoding (ffmpeg -vf crop=' + __vd.requested[0] + ':' + __vd.requested[1] + ':0:0).' }]);
4986
+ if (result.fidelity.note) delete result.fidelity.note;
4987
+ }
4988
+ // LUMA OUTSIDE 16-235 (8.35, FxTool 4f1ec3ae). The broadcast encode is
4989
+ // decoded back and measured: H.264 ringing at hard edges puts a fraction of
4990
+ // a percent of samples out of range, which QC rejects. broadcastHeadroom
4991
+ // cuts it but never to zero, so a legaliser pass is the requirement.
4992
+ if (result && result.success && __vr && __vr.luma && __vr.luma.outOfRange > 0 && result.fidelity) {
4993
+ result.fidelity.warnings = (result.fidelity.warnings || []).concat([{ code: 'luma_out_of_range',
4994
+ message: 'broadcast QC will reject this as delivered: ' + ((__vr.luma.outOfRangeFraction || 0) * 100).toFixed(3) + '% of decoded luma is outside 16-235 (min ' + __vr.luma.min + ', max ' + __vr.luma.max + ', broadcastHeadroom ' + (typeof __vr.headroom === 'number' ? __vr.headroom : ${JSON.stringify(broadcastHeadroom ?? 12)}) + '). First re-export with a larger broadcastHeadroom (12 measured clean on hard white-on-black edges; up to 40). Otherwise a legaliser pass is REQUIRED before delivery — clip into a NARROWER band and re-encode at high quality, because a plain 16-235 clip followed by a re-encode brings the edge overshoot back: ffmpeg -i in.mp4 -vf "lutyuv=y=clip(val\\\\,28\\\\,223):u=clip(val\\\\,16\\\\,240):v=clip(val\\\\,16\\\\,240)" -c:v libx264 -crf 18 -color_range tv -colorspace bt709 -color_primaries bt709 -color_trc bt709 -c:a copy out.mp4 (measured 20-229). Verify the luma range after re-encoding.' }]);
4995
+ if (result.fidelity.note) delete result.fidelity.note;
4996
+ }
4997
+ ${Object.keys(videoEncodeOpts).length ? ` if (result && result.success) {
4998
+ const __asked = ${JSON.stringify(videoEncodeOpts)};
4999
+ const __vw = [];
5000
+ // The engine joins its warnings into one string (bitrate floor, then
5001
+ // luma), so each is raised from its own numbers, not from that text.
5002
+ if (__vr && typeof __vr.achievedBitrate === 'number' && typeof __vr.bitrateFloor === 'number' && __vr.achievedBitrate < __vr.bitrateFloor * 0.95) {
5003
+ __vw.push({ code: 'bitrate_below_floor', message: 'achieved ' + (__vr.achievedBitrate / 1e6).toFixed(2) + ' Mbps against a ' + (__vr.bitrateFloor / 1e6).toFixed(1) + ' Mbps floor: the browser encoder treats the bitrate as a ceiling and does not pad simple content. Re-encode for delivery (e.g. ffmpeg -b:v with -minrate / -bufsize) if the spec requires the floor.' });
5004
+ }
5005
+ if (!__vr || typeof __vr !== 'object') {
5006
+ __vw.push({ code: 'encode_options_not_applied', message: 'this studio did not report its encode, so ' + Object.keys(__asked).join(', ') + ' cannot be confirmed — it likely has no broadcast / bitrate support, and the file is an ordinary web encode.' });
5007
+ } else {
5008
+ if (__asked.broadcast && __vr.broadcast !== true) __vw.push({ code: 'broadcast_not_applied', message: 'broadcast was asked for but the encode was not broadcast-safe (the export route dropped the option): full-range, untagged colour.' });
5009
+ if (__asked.bitrate !== undefined && typeof __vr.bitrate === 'number' && __vr.bitrate < __asked.bitrate * 0.99) __vw.push({ code: 'bitrate_not_applied', message: 'bitrate ' + __asked.bitrate + ' was asked for; the encoder was given ' + __vr.bitrate + '.' });
5010
+ }
5011
+ if (__vw.length && result.fidelity) {
5012
+ result.fidelity.warnings = (result.fidelity.warnings || []).concat(__vw);
5013
+ if (result.fidelity.note) delete result.fidelity.note;
5014
+ }
5015
+ }
5016
+ ` : ''} const __ar = __vx && __vx.lastAudioReport;
5017
+ if (result && result.success && __ar && typeof __ar === 'object') {
5018
+ result.audio = __ar;
5019
+ const __aw = [];
5020
+ if (__ar.warning) __aw.push({ code: 'audio_lead_trimmed', message: String(__ar.warning) });
5021
+ if (typeof __ar.audioDurationS === 'number' && typeof __ar.videoDurationS === 'number' && __ar.audioDurationS > __ar.videoDurationS + 0.0105) {
5022
+ __aw.push({ code: 'audio_longer_than_video', message: 'the audio track is ' + __ar.audioDurationS.toFixed(4) + ' s against ' + __ar.videoDurationS.toFixed(4) + ' s of picture — a platform with a hard length cap may read the longer one.' });
5023
+ }
5024
+ if (__aw.length && result.fidelity) {
5025
+ result.fidelity.warnings = (result.fidelity.warnings || []).concat(__aw);
5026
+ if (result.fidelity.note) delete result.fidelity.note;
5027
+ }
5028
+ }
5029
+ ${(() => {
5030
+ // A DURATION THAT OVERRUNS AT AN NTSC RATE (round 9 FF, 1.76): 6 s at
5031
+ // 29.97 is round(6 x 30000/1001) = 180 frames = 6.006 s, over a 6.000 s
5032
+ // bumper cap, silently. Said, with the longest duration that fits.
5033
+ if (fps === undefined || Number.isInteger(fps))
5034
+ return '';
5035
+ const frames = Math.round(videoDuration * fps);
5036
+ const actual = frames / fps;
5037
+ if (actual <= videoDuration + 1e-9)
5038
+ return '';
5039
+ const fit = Math.floor(videoDuration * fps) / fps;
5040
+ return ` if (result && result.success && ['mp4', 'webm', 'gif', 'apng'].indexOf(format) !== -1) result.timing = ${JSON.stringify({
5041
+ frames, seconds: Math.round(actual * 1e6) / 1e6, requested: videoDuration,
5042
+ warning: `${frames} frames at ${Math.round(fps * 1000) / 1000} fps is ${actual.toFixed(3)} s — ${((actual - videoDuration) * 1000).toFixed(1)} ms over the ${videoDuration} s asked for. For a hard cap (e.g. a 6 s bumper) use duration ${fit.toFixed(4)} (${Math.floor(videoDuration * fps)} frames).`,
5043
+ })};\n`;
5044
+ })()}
5045
+ ${stillTime !== undefined ? ` try { app.setPlaybackTime(__prevT); } catch (_) { /* the still is already rendered */ }
5046
+ if (result && result.success) result.time = ${stillTime};` : ''}
3524
5047
 
3525
5048
  return result;
3526
5049
  })();
@@ -5256,7 +6779,7 @@ ${usesCanvasSize ? ` // 'auto': the canvas's own size, with the preset only as
5256
6779
  `.trim();
5257
6780
  }
5258
6781
  generateImportImage(input) {
5259
- const { url, position, maxWidth, maxHeight, mask } = input;
6782
+ const { url, position, maxWidth, maxHeight, mask, smoothing } = input;
5260
6783
  // Each fragment used to be written with a LEADING comma, as if it followed
5261
6784
  // an entry that no longer exists — so the first one opened the object with
5262
6785
  // `{, position: …}` and the emitted code was a syntax error. Passing any
@@ -5325,7 +6848,10 @@ ${usesCanvasSize ? ` // 'auto': the canvas's own size, with the preset only as
5325
6848
  const entry = await app.imageTools.uploadFromURL(src);
5326
6849
  const opts = ${optsLiteral};
5327
6850
  const raster = await app.imageTools.placeImage(entry.id, Object.keys(opts).length > 0 ? opts : undefined);
5328
- ${mask ? ` app.imageTools.applyMask(raster, '${mask}');\n` : ''} // The REGISTRY id is \`data.id\`. \`data.itemId\` has never existed, so this
6851
+ ${smoothing ? ` // Pixel art wants 'off': Paper's Raster draws with smoothing 'low' by
6852
+ // default, which blurs every scaled-up pixel edge. Read at every draw,
6853
+ // export frames included. Set before a mask, which clones the raster.
6854
+ if (raster && 'smoothing' in raster) raster.smoothing = ${JSON.stringify(smoothing)};\n` : ''} // The REGISTRY id is \`data.id\`. \`data.itemId\` has never existed, so this
5329
6855
  // fell through to \`raster.id\` — a Paper.js NUMBER — and the handle the tool
5330
6856
  // returned could not be used as a relation endpoint or with modify/animate.
5331
6857
  // Register the raster if placeImage somehow left it unregistered, so the
@@ -5337,11 +6863,28 @@ ${mask ? ` app.imageTools.applyMask(raster, '${mask}');\n` : ''} // The RE
5337
6863
  if (!itemId) {
5338
6864
  return { error: 'Image placed but not registered — no usable item id. This is a bug; report the scene.' };
5339
6865
  }
6866
+ let placed = raster;
6867
+ ${mask ? ` // A MASK REPLACES THE RASTER (round 8 DD, 4.10). applyMask builds a
6868
+ // clipping group from a CLONE and removes the original, so the id and bounds
6869
+ // read off \`raster\` afterwards described an item no longer on the canvas.
6870
+ // The group is what is drawn: the registry id is rebound to it (the
6871
+ // registry's own method for exactly this), and a mask that did not apply is
6872
+ // refused instead of answered with the raster's bounds.
6873
+ const masked = app.imageTools.applyMask(raster, ${JSON.stringify(mask)});
6874
+ if (!masked) {
6875
+ return { success: false, itemId: itemId, error: 'the ${mask} mask was not applied — the image may not be a raster yet. The unmasked image is on the canvas as ' + itemId + '.' };
6876
+ }
6877
+ if (app.itemRegistry && typeof app.itemRegistry.rebind === 'function') app.itemRegistry.rebind(itemId, masked);
6878
+ placed = masked;
6879
+ ` : ''} const b = placed.bounds;
6880
+ if (!b || !(b.width > 0) || !(b.height > 0)) {
6881
+ return { success: false, itemId: itemId, error: 'the image was placed with no size (' + (b ? b.width + 'x' + b.height : 'no bounds') + ') — it may not have loaded. Nothing visible was drawn.' };
6882
+ }
5340
6883
  return {
5341
6884
  success: true,
5342
6885
  itemId: itemId,
5343
6886
  message: 'Image imported and placed on canvas.',
5344
- bounds: { x: raster.bounds.x, y: raster.bounds.y, width: raster.bounds.width, height: raster.bounds.height }${mask ? `,\n mask: '${mask}'` : ''}
6887
+ bounds: { x: b.x, y: b.y, width: b.width, height: b.height }${mask ? `,\n mask: '${mask}'` : ''}
5345
6888
  };
5346
6889
  } catch (e) {
5347
6890
  return { error: 'Failed to import image: ' + e.message };
@@ -5721,9 +7264,27 @@ ${mask ? ` app.imageTools.applyMask(raster, '${mask}');\n` : ''} // The RE
5721
7264
  return { success: false, error: 'apply needs itemIds, in the order the stagger should follow (row-major for a grid)' };
5722
7265
  }
5723
7266
  const r = app.staggerItems(ids, ${S(validated.opts || {})});
7267
+ // KEYFRAME TRACKS DO NOT READ animationDelay (round 7 X, 2.2). staggerItems
7268
+ // writes the delay to data.animationDelay, which the loop presets honour;
7269
+ // a tile that already had a keyframe fly-in ignored it, and all 64 tiles of
7270
+ // a shatter mosaic arrived on the same frames. A keyframed item's clock is
7271
+ // its clip offset, data.timeOffset, so the delay goes there too — on top of
7272
+ // the offset it had before the first stagger (remembered), so re-staggering
7273
+ // replaces rather than stacks.
7274
+ let shiftedTracks = 0;
7275
+ if (r && r.ok && Array.isArray(r.delays)) {
7276
+ const items = ids.map(function(id) { const e = app.itemRegistry && app.itemRegistry.get(id); return e && e.item; }).filter(Boolean);
7277
+ items.forEach(function(item, i) {
7278
+ if (!item.data || !Array.isArray(item.data.keyframes) || !item.data.keyframes.length) return;
7279
+ if (item.data._staggerBaseOffset === undefined) item.data._staggerBaseOffset = Number(item.data.timeOffset) || 0;
7280
+ item.data.timeOffset = item.data._staggerBaseOffset + (Number(r.delays[i]) || 0);
7281
+ shiftedTracks++;
7282
+ });
7283
+ }
5724
7284
  return {
5725
7285
  success: r && r.ok === true,
5726
7286
  action: 'apply',
7287
+ shiftedKeyframeTracks: shiftedTracks,
5727
7288
  // 'applied' can be lower than the ids given — an id that resolves to
5728
7289
  // nothing is skipped, and a stagger that silently covered fewer items than
5729
7290
  // asked for is the failure worth seeing.
@@ -5890,7 +7451,25 @@ ${mask ? ` app.imageTools.applyMask(raster, '${mask}');\n` : ''} // The RE
5890
7451
  generateBrandKit(input) {
5891
7452
  const opts = JSON.stringify({ ...(input.selectionOnly ? { selectionOnly: true } : {}) });
5892
7453
  const method = input.action === 'plan' ? 'planBrandKit' : 'applyBrandKit';
5893
- return this._facadeCall(method, `${JSON.stringify(input.kit)}, ${opts}`, `Brand kit: ${input.action}`);
7454
+ const facade = this._facadeCall(method, `${JSON.stringify(input.kit)}, ${opts}`, `Brand kit: ${input.action}`);
7455
+ // A KIT'S FONTS ARE LOADED BEFORE IT RESTYLES TEXT — the same fallback as
7456
+ // create_item (see emitEnsureFont): the kit named Anton, the headings were
7457
+ // re-measured in Times. Each family is checked, loaded if missing, and the
7458
+ // outcome for any that needed it rides on the facade's own result.
7459
+ const fonts = input.kit.fonts;
7460
+ const families = [...new Set(Object.values(fonts ?? {}).filter((f) => typeof f === 'string' && f.trim() !== ''))];
7461
+ if (input.action !== 'apply' || families.length === 0)
7462
+ return facade;
7463
+ const loads = families.map((f) => ` r = await (async function() {${emitEnsureFont(f).replace(/\n/g, '\n ')}\n return __font;\n })();\n if (r) __fonts.push(r);`).join('\n');
7464
+ return `
7465
+ // Brand kit: load the kit's fonts, then apply
7466
+ (async function() {
7467
+ const __fonts = [];
7468
+ let r;
7469
+ ${loads}
7470
+ const out = await ${facade.replace(/^\/\/[^\n]*\n/, '').replace(/;\s*$/, '')};
7471
+ return __fonts.length && out && typeof out === 'object' ? { ...out, fonts: __fonts } : out;
7472
+ })();`.trim();
5894
7473
  }
5895
7474
  generateComponent(input) {
5896
7475
  const ids = JSON.stringify(input.itemIds || []);
@@ -5909,13 +7488,55 @@ ${mask ? ` app.imageTools.applyMask(raster, '${mask}');\n` : ''} // The RE
5909
7488
  const r = app.defineComponent(items, ${JSON.stringify({ name: input.name })});
5910
7489
  return { success: r.ok !== false, ...r };
5911
7490
  })();`.trim();
7491
+ // A COMPONENT'S PARTS, BY THE KEY AN OVERRIDE NEEDS (round 9 II, 1.80).
7492
+ // Overrides are {componentKey: {prop: value}}, and the keys are generated
7493
+ // names (ck_12) — list gave only a count and instantiate returned a raw
7494
+ // Paper item with no id, so the component route to batch creative was
7495
+ // unusable. Both now return parts: each key with its part's type, name and
7496
+ // text, which is how a caller finds the headline's key.
5912
7497
  case 'list':
5913
- return this._facadeCall('listComponents', '', 'Component: list');
7498
+ return `
7499
+ // Component: list, with each definition's parts
7500
+ (function() {
7501
+ if (typeof app.listComponents !== 'function') return { success: false, error: 'app.listComponents() unavailable — update PinePaper Studio.' };
7502
+ const partsOf = function(root) {
7503
+ const out = [];
7504
+ const walk = function(n) { if (!n) return; if (n.data && n.data.componentKey) out.push(Object.assign({ key: n.data.componentKey, type: n.className }, typeof n.content === 'string' ? { content: n.content } : {}, n.name ? { name: n.name } : {})); (n.children || []).forEach(walk); };
7505
+ (root && root.children || []).forEach(walk);
7506
+ return out;
7507
+ };
7508
+ const defs = app._components;
7509
+ const components = app.listComponents().map(function(c) {
7510
+ let parts = null;
7511
+ try {
7512
+ const d = defs && typeof defs.get === 'function' ? defs.get(c.id) : null;
7513
+ if (d && d.json && typeof paper !== 'undefined') {
7514
+ const g = new paper.Group({ insert: false });
7515
+ g.importJSON(d.json);
7516
+ parts = partsOf(g.children.length === 1 && g.children[0].children ? g.children[0] : g);
7517
+ g.remove();
7518
+ }
7519
+ } catch (_) { /* parts unknown on this build */ }
7520
+ return Object.assign({}, c, parts ? { parts: parts } : {});
7521
+ });
7522
+ return { success: true, components: components, count: components.length };
7523
+ })();`.trim();
5914
7524
  case 'instantiate':
5915
- return this._facadeCall('instantiateComponent', `${JSON.stringify(input.componentId || '')}, ${JSON.stringify({
7525
+ return `
7526
+ // Component: instantiate — the instance id, and the keys its overrides use
7527
+ (function() {
7528
+ if (typeof app.instantiateComponent !== 'function') return { success: false, error: 'app.instantiateComponent() unavailable — update PinePaper Studio.' };
7529
+ const r = app.instantiateComponent(${JSON.stringify(input.componentId || '')}, ${JSON.stringify({
5916
7530
  ...(input.position ? { position: input.position } : {}),
5917
7531
  ...(input.overrides ? { overrides: input.overrides } : {}),
5918
- })}`, 'Component: instantiate');
7532
+ })});
7533
+ if (!r || r.ok === false) return { success: false, error: (r && r.error) || 'the component was not placed' };
7534
+ const parts = [];
7535
+ const walk = function(n) { if (!n) return; if (n.data && n.data.componentKey) parts.push(Object.assign({ key: n.data.componentKey, type: n.className }, typeof n.content === 'string' ? { content: n.content } : {}, n.name ? { name: n.name } : {})); (n.children || []).forEach(walk); };
7536
+ walk(r.item);
7537
+ return { success: true, instanceId: r.id, itemId: r.id, parts: parts, orphaned: r.orphaned || [],
7538
+ ...(r.orphaned && r.orphaned.length ? { warning: 'overrides for ' + r.orphaned.map(function(o) { return o.key; }).join(', ') + ' matched no part of this component and were not applied. parts lists the keys it has.' } : {}) };
7539
+ })();`.trim();
5919
7540
  case 'set_override':
5920
7541
  return this._facadeCall('setComponentOverride', `${JSON.stringify(input.instanceId || '')}, ${JSON.stringify(input.componentKey || '')}, ${JSON.stringify(input.prop || '')}, ${JSON.stringify(input.value ?? null)}`, 'Component: set override');
5921
7542
  case 'sync':
@@ -6018,14 +7639,48 @@ ${mask ? ` app.imageTools.applyMask(raster, '${mask}');\n` : ''} // The RE
6018
7639
  }
6019
7640
  }
6020
7641
  generateAudioBeats(input) {
7642
+ // UPLOADED MEDIA IS THE AUDIO A CALLER HAS, AND IT WAS NOT A SOURCE.
7643
+ //
7644
+ // analyzeAudio treats a bare id as a storage asset and wants its data as a
7645
+ // string, so the id or registryId that upload_audio returned gave "could not
7646
+ // decode audio" — while the detector itself is accurate (3.4 ms mean error,
7647
+ // measured). An uploaded clip's player already holds its bytes at a blob:
7648
+ // URL, which analyzeAudio does accept; either id resolves to that here.
7649
+ // Anything else (asset id, data:, URL) passes through untouched.
7650
+ // @engine-methods analyzeAudio animateToBeat
7651
+ const resolveSource = ` let __src = ${JSON.stringify(input.source || '')};
7652
+ const __A = (typeof window !== 'undefined') && window.PinePaperAgent;
7653
+ const __m = (__src && __A && typeof __A.listMedia === 'function')
7654
+ ? __A.listMedia().find(function(x) { return x.id === __src || x.registryId === __src; })
7655
+ : null;
7656
+ if (__m) {
7657
+ const layer = __m.kind === 'video' ? app.videoLayer && app.videoLayer.videos : app.audioLayer && app.audioLayer.audios;
7658
+ const raster = layer && typeof layer.get === 'function' ? layer.get(__m.id) : null;
7659
+ if (!raster || !raster.url) {
7660
+ return { success: false, error: 'uploaded media ' + JSON.stringify(__src) + ' has no playable source to analyse in this studio.' };
7661
+ }
7662
+ __src = raster.url;
7663
+ }`;
7664
+ const call = (method, args, label) => `
7665
+ // ${label}
7666
+ (async function() {
7667
+ if (typeof app.${method} !== 'function') {
7668
+ return { success: false, error: 'app.${method}() unavailable — update PinePaper Studio to a build that has it.' };
7669
+ }
7670
+ ${resolveSource}
7671
+ const r = await app.${method}(${args});
7672
+ if (r && typeof r === 'object' && 'ok' in r) {
7673
+ return { success: r.ok !== false, ...r };
7674
+ }
7675
+ return { success: true, result: r };
7676
+ })();`.trim();
6021
7677
  if (input.action === 'analyze') {
6022
- return this._facadeCall('analyzeAudio', `${JSON.stringify(input.source || '')}, ${JSON.stringify({
7678
+ return call('analyzeAudio', `__src, ${JSON.stringify({
6023
7679
  ...(input.sensitivity !== undefined ? { sensitivity: input.sensitivity } : {}),
6024
7680
  ...(input.minGap !== undefined ? { minGap: input.minGap } : {}),
6025
7681
  })}`, 'Audio: analyze beats');
6026
7682
  }
6027
- return this._facadeCall('animateToBeat', `${JSON.stringify(input.itemId || '')}, ${JSON.stringify({
6028
- ...(input.source ? { source: input.source } : {}),
7683
+ const opts = JSON.stringify({
6029
7684
  ...(input.beats ? { beats: input.beats } : {}),
6030
7685
  ...(input.grid ? { grid: true } : {}),
6031
7686
  ...(input.property ? { property: input.property } : {}),
@@ -6033,19 +7688,111 @@ ${mask ? ` app.imageTools.applyMask(raster, '${mask}');\n` : ''} // The RE
6033
7688
  ...(input.accent !== undefined ? { accent: input.accent } : {}),
6034
7689
  ...(input.decay !== undefined ? { decay: input.decay } : {}),
6035
7690
  ...(input.sensitivity !== undefined ? { sensitivity: input.sensitivity } : {}),
6036
- })}`, 'Audio: animate to beat');
7691
+ });
7692
+ return call('animateToBeat', `${JSON.stringify(input.itemId || '')}, Object.assign(${opts}, __src ? { source: __src } : {})`, 'Audio: animate to beat');
6037
7693
  }
6038
7694
  generateTemplateParams(input) {
6039
7695
  if (input.action === 'get') {
6040
7696
  return this._facadeCall('getTemplateParams', JSON.stringify(input.templateId), 'Template params: get');
6041
7697
  }
6042
- return this._facadeCall('applyTemplateWithParams', `${JSON.stringify(input.templateId)}, ${JSON.stringify(input.params || {})}`, 'Template params: apply');
7698
+ // AN UNDECLARED PARAM CHANGED NOTHING, AND THE CALL SAID SUCCESS.
7699
+ // apply {templateId:'sale-announcement', params:{headline, date}} loaded
7700
+ // the template, left its text untouched, and answered success:true — and 0
7701
+ // of 195 templates declare any params (round 7 Z, 1.52). The template's own
7702
+ // declaration is read first and anything outside it is named in the result;
7703
+ // the template still loads, because the caller did ask for it.
7704
+ // @engine-methods getTemplateParams applyTemplateWithParams
7705
+ const params = input.params || {};
7706
+ return `
7707
+ // Template params: apply, naming params the template does not declare
7708
+ (async function() {
7709
+ if (typeof app.applyTemplateWithParams !== 'function') {
7710
+ return { success: false, error: 'app.applyTemplateWithParams() unavailable — update PinePaper Studio to a build that has it.' };
7711
+ }
7712
+ const given = ${JSON.stringify(Object.keys(params))};
7713
+ let declared = null;
7714
+ if (typeof app.getTemplateParams === 'function') {
7715
+ try {
7716
+ const d = await app.getTemplateParams(${JSON.stringify(input.templateId)});
7717
+ if (d && Array.isArray(d.params)) declared = d.params.map(function(p) { return p && (p.name || p.key || p.id); }).filter(Boolean);
7718
+ } catch (_) { /* reported as unknown below */ }
7719
+ }
7720
+ const r = await app.applyTemplateWithParams(${JSON.stringify(input.templateId)}, ${JSON.stringify(params)});
7721
+ const out = (r && typeof r === 'object' && 'ok' in r) ? Object.assign({ success: r.ok !== false }, r)
7722
+ : { success: r !== false, templateId: ${JSON.stringify(input.templateId)} };
7723
+ if (given.length && declared) {
7724
+ const ignored = given.filter(function(k) { return declared.indexOf(k) === -1; });
7725
+ if (ignored.length) {
7726
+ out.ignoredParams = ignored;
7727
+ out.declaredParams = declared;
7728
+ out.warning = declared.length === 0
7729
+ ? 'this template declares no parameters, so ' + ignored.join(', ') + ' changed nothing — the template loaded as designed. Edit its text items with pinepaper_modify_item instead.'
7730
+ : ignored.join(', ') + ' ' + (ignored.length > 1 ? 'are' : 'is') + ' not declared by this template and changed nothing. It declares: ' + declared.join(', ') + '.';
7731
+ }
7732
+ } else if (given.length && !declared) {
7733
+ out.warning = 'could not read which parameters this template declares, so whether ' + given.join(', ') + ' applied is unverified.';
7734
+ }
7735
+ return out;
7736
+ })();`.trim();
6043
7737
  }
6044
7738
  generateSceneDiff(input) {
6045
7739
  if (input.action === 'version') {
6046
7740
  return this._facadeCall('diffAgainstVersion', JSON.stringify(input.versionId || ''), 'Scene diff: vs version');
6047
7741
  }
6048
- return this._facadeCall('diffHistoryStates', `${input.indexA ?? 0}, ${input.indexB ?? 0}`, 'Scene diff: history');
7742
+ // "NO CHANGES" AND "THE SNAPSHOTS DO NOT REFLECT THE CANVAS" ARE DIFFERENT
7743
+ // ANSWERS.
7744
+ //
7745
+ // A pilot added six items and six tracks and was told nothing had changed,
7746
+ // with unchanged:0 — the tell, since a real "no changes" compares items and
7747
+ // finds them equal, so it can never report zero of everything.
7748
+ //
7749
+ // I FIRST DIAGNOSED THIS WRONG, and the wrong cause was in this file's
7750
+ // emitted error for one commit. I read that HistoryManager stores entries
7751
+ // as JSON strings and concluded diffScenes never parsed them. It does:
7752
+ // SceneDiff's asObject() JSON.parses a string at the extraction entry, and
7753
+ // fxtool-f2 verified it two ways — a stringified Paper tree diffs correctly,
7754
+ // and a live browser run on string-stored history returned real added ids.
7755
+ //
7756
+ // The actual cause was GeneratedCodeRunner writing its single history entry
7757
+ // only when items were CREATED, so a run that merely applied an effect, a
7758
+ // style or a keyframe track wrote none. The two entries in those histories
7759
+ // were both empty scenes, and two empty snapshots honestly diffed ARE "no
7760
+ // changes". Fixed in FxTool c95c1d72.
7761
+ //
7762
+ // The backstop stays, for a reason that outlives that fix: a snapshot pair
7763
+ // that does not reflect the canvas must never present as an affirmative
7764
+ // nothing-changed. HistoryManager's own _itemIdsOf states the rule —
7765
+ // "null means could not tell, never empty" — and this is the same rule at
7766
+ // the tool boundary. It no longer guesses WHY, because guessing why is
7767
+ // exactly what went wrong.
7768
+ return `
7769
+ // Scene diff: what changed between two history states
7770
+ (function() {
7771
+ if (typeof app.diffHistoryStates !== 'function') {
7772
+ return { success: false, error: 'app.diffHistoryStates unavailable — update PinePaper Studio.' };
7773
+ }
7774
+ const d = app.diffHistoryStates(${input.indexA ?? 0}, ${input.indexB ?? 0});
7775
+ if (!d || d.ok === false) {
7776
+ return { success: false, error: (d && (d.error || d.reason)) || 'the diff refused without saying why' };
7777
+ }
7778
+ const saw = (d.added || []).length + (d.removed || []).length + (d.changed || []).length + (d.unchanged || 0);
7779
+ const live = (app.itemRegistry && typeof app.itemRegistry.getAll === 'function')
7780
+ ? (app.itemRegistry.getAll() || []).length
7781
+ : 0;
7782
+ if (saw === 0 && live > 0) {
7783
+ return {
7784
+ success: false,
7785
+ error: 'the diff read NO items from either history state while the canvas holds ' + live
7786
+ + ' — so those snapshots do not reflect this canvas, which is not the same as "nothing changed". '
7787
+ + 'The usual cause is that no history entry was written for the changes you made: some studio builds '
7788
+ + 'snapshot only when items are CREATED, so applying an effect, a style or a keyframe track records nothing. '
7789
+ + 'Compare with pinepaper_get_items before and after instead, or use action "version".',
7790
+ liveItems: live,
7791
+ raw: d,
7792
+ };
7793
+ }
7794
+ return { success: true, ...d };
7795
+ })();`.trim();
6049
7796
  }
6050
7797
  generateTransform(input) {
6051
7798
  switch (input.action) {
@@ -6167,6 +7914,28 @@ ${mask ? ` app.imageTools.applyMask(raster, '${mask}');\n` : ''} // The RE
6167
7914
  // IMAGE PROCESSING
6168
7915
  // ===========================================================================
6169
7916
  generateImageFilter(input) {
7917
+ // BLUR TAKES `radius`, AND THIS TOOL'S OWN EXAMPLE SAID `amount`.
7918
+ //
7919
+ // FilterSystem's blur reads params.radius and does Math.floor(undefined) =
7920
+ // NaN, which passes its own `radius <= 0` guard and then blurs by nothing.
7921
+ // So {amount: 18} was accepted, ran, and changed no pixels — while the
7922
+ // schema example told callers to write exactly that. The example is fixed;
7923
+ // the alias stays because the wrong spelling was documented and anyone who
7924
+ // followed it should not have to discover the difference twice.
7925
+ //
7926
+ // BLUR ONLY. grain, bloom, chromaticAberration, displace, datamosh and
7927
+ // paletteMap all read a real `amount`; renaming it for them would turn a
7928
+ // working call into the same silent no-op this alias exists to prevent.
7929
+ const blurAlias = (name, params) => {
7930
+ const out = { ...(params ?? {}) };
7931
+ if (name === 'blur' && out.amount !== undefined && out.radius === undefined) {
7932
+ out.radius = out.amount;
7933
+ delete out.amount;
7934
+ }
7935
+ return out;
7936
+ };
7937
+ const filterParams = blurAlias(input.filterName, input.params);
7938
+ const chainFilters = (input.filters || []).map((f) => ({ ...f, params: blurAlias(f.name, f.params) }));
6170
7939
  // Resolve the raster from the registry id (unwrapping groups when the
6171
7940
  // build has the _resolveRaster helper), then use the real GPU-filter
6172
7941
  // facades. The previous emitter called app.imageTools.applyFilter, a
@@ -6183,7 +7952,13 @@ ${mask ? ` app.imageTools.applyMask(raster, '${mask}');\n` : ''} // The RE
6183
7952
  (async function() {
6184
7953
  ${resolve}
6185
7954
  try {
6186
- await app.applyImageFilter(item, ${JSON.stringify(input.filterName || '')}, ${JSON.stringify(input.params || {})});
7955
+ const r = await app.applyImageFilter(item, ${JSON.stringify(input.filterName || '')}, ${JSON.stringify(filterParams)});
7956
+ // applyImageFilter THROWS for a missing raster and answers the item
7957
+ // otherwise; a falsy answer is a refusal it did not raise.
7958
+ if (r === false || r === null) {
7959
+ return { success: false, action: 'apply', itemId: ${JSON.stringify(input.itemId)},
7960
+ error: 'the filter did not apply — ${input.filterName || ''} may not be available on this studio.' };
7961
+ }
6187
7962
  return { success: true, action: 'apply', itemId: ${JSON.stringify(input.itemId)}, filter: ${JSON.stringify(input.filterName || '')} };
6188
7963
  } catch (e) {
6189
7964
  return { error: 'Failed to apply filter: ' + e.message };
@@ -6195,7 +7970,7 @@ ${resolve}
6195
7970
  (async function() {
6196
7971
  ${resolve}
6197
7972
  try {
6198
- const filters = ${JSON.stringify(input.filters || [])};
7973
+ const filters = ${JSON.stringify(chainFilters)};
6199
7974
  await app.applyImageFilterChain(item, filters);
6200
7975
  return { success: true, action: 'chain', itemId: ${JSON.stringify(input.itemId)}, filterCount: filters.length };
6201
7976
  } catch (e) {
@@ -6243,11 +8018,84 @@ ${resolve}
6243
8018
  // a caller in a mode it cannot get out of or complete.
6244
8019
  //
6245
8020
  // Refused by name, with the tools that do the same job without a pointer.
6246
- const why = 'the lasso is an interactive mouse tool (app.lassoTool: activate, then startStroke/continueStroke/endStroke as the user draws). '
6247
- + 'There is no headless call that completes an extraction, so this tool could never work over MCP. '
8021
+ const why = 'activate / apply drive the interactive, mouse-based lasso. Use action "cut" with itemId and points (a polygon in canvas coordinates) — it drives the same lasso headlessly. '
6248
8022
  + 'To cut a region without a pointer, use pinepaper_extract_object (detects and extracts by description), '
6249
8023
  + 'or pinepaper_cutout_style / the mask actions on pinepaper_image_tools to shape an existing raster.';
6250
8024
  switch (input.action) {
8025
+ case 'cut': {
8026
+ // A POLYGON CUT, DRIVEN THROUGH THE LASSO'S OWN CLICK PATH (round 7 X,
8027
+ // 1.55). The interactive tool has no headless form, but its polygon
8028
+ // mode is click-by-click: startStroke/endStroke at a point adds a vertex,
8029
+ // and a click within 12 px of the first vertex closes the shape. That is
8030
+ // public API, so this drives it exactly as a user would, then applies.
8031
+ //
8032
+ // applyLasso DELETES the image it cut from ("chosen behaviour" in the
8033
+ // editor). Over MCP that removed the photo a parallax needs underneath,
8034
+ // silently. keepOriginal (default) cuts from a temporary clone, which is
8035
+ // what gets consumed; the result says which happened.
8036
+ const pts = (input.points ?? []).map((p) => (Array.isArray(p) ? { x: p[0], y: p[1] } : p));
8037
+ if (!input.itemId)
8038
+ return `({ success: false, action: 'cut', error: 'cut needs itemId — the image to cut from.' });`;
8039
+ if (pts.length < 3)
8040
+ return `({ success: false, action: 'cut', error: 'cut needs at least 3 points (canvas coordinates) around the region.' });`;
8041
+ const keep = input.keepOriginal !== false;
8042
+ return `
8043
+ // Lasso cut along a polygon
8044
+ (async function() {
8045
+ const L = app.lassoTool;
8046
+ if (!L || typeof L.activate !== 'function' || typeof L.applyLasso !== 'function') {
8047
+ return { success: false, action: 'cut', error: 'app.lassoTool unavailable — update PinePaper Studio.' };
8048
+ }
8049
+ const entry = app.itemRegistry && app.itemRegistry.get(${JSON.stringify(input.itemId)});
8050
+ const original = entry && entry.item;
8051
+ if (!original) return { success: false, action: 'cut', error: 'no item ' + ${JSON.stringify(input.itemId)} };
8052
+ const target = ${keep} ? original.clone() : original;
8053
+ // A clone carries a COPY of data — including the registry id — and
8054
+ // applyLasso deletes by that id, which would take the original anyway.
8055
+ if (${keep} && target.data) {
8056
+ target.data = Object.assign({}, target.data);
8057
+ delete target.data.registryId; delete target.data.id;
8058
+ }
8059
+ const pts = ${JSON.stringify(pts)}.map(function(p) { return new paper.Point(p.x, p.y); });
8060
+ const first = pts[0];
8061
+ let skipped = 0;
8062
+ L.activate(target);
8063
+ // POLYGON MODE, SET — not assumed. On production the lasso's remembered mode
8064
+ // was 'freehand', where a click adds no vertex, so every cut was refused as
8065
+ // "did not close". The user's own mode is put back afterwards.
8066
+ const __prevMode = L._forcedMode;
8067
+ if (typeof L.setMode === 'function') L.setMode('polygon');
8068
+ try {
8069
+ L.startStroke(first); L.endStroke(first);
8070
+ for (let i = 1; i < pts.length; i++) {
8071
+ if (pts[i].getDistance(first) < 12) { skipped++; continue; }
8072
+ L.startStroke(pts[i]); L.endStroke(pts[i]);
8073
+ }
8074
+ L.startStroke(first); L.endStroke(first); // a click at the first vertex closes it
8075
+ if (!L.isDrawn()) {
8076
+ L.cancel();
8077
+ if (${keep}) target.remove();
8078
+ return { success: false, action: 'cut', error: 'the polygon did not close — it needs at least 3 vertices at least 12 px from the first one.', skipped: skipped };
8079
+ }
8080
+ const cutout = await L.applyLasso();
8081
+ if (!cutout) {
8082
+ if (${keep} && target.parent) target.remove();
8083
+ return { success: false, action: 'cut', error: 'the lasso produced no cutout — the item may not be an image.' };
8084
+ }
8085
+ let cutoutId = cutout.data && (cutout.data.registryId || cutout.data.id);
8086
+ if (!cutoutId && typeof app.registerItem === 'function') cutoutId = app.registerItem(cutout, 'image', { source: 'mcp-lasso' });
8087
+ return { success: true, action: 'cut', cutoutId: cutoutId, originalId: ${JSON.stringify(input.itemId)},
8088
+ originalKept: ${keep}, vertices: pts.length - skipped, skipped: skipped,
8089
+ bounds: { x: cutout.bounds.x, y: cutout.bounds.y, width: cutout.bounds.width, height: cutout.bounds.height } };
8090
+ } catch (e) {
8091
+ try { L.cancel(); } catch (_) { /* already down */ }
8092
+ if (${keep} && target && target.parent) target.remove();
8093
+ return { success: false, action: 'cut', error: (e && e.message) || String(e) };
8094
+ } finally {
8095
+ if (typeof L.setMode === 'function' && __prevMode) { try { L.setMode(__prevMode); } catch (_) { /* cosmetic */ } }
8096
+ }
8097
+ })();`.trim();
8098
+ }
6251
8099
  case 'activate':
6252
8100
  case 'apply':
6253
8101
  return `
@@ -6914,7 +8762,28 @@ if (!app.spriteSystem) return { error: 'SpriteSheetSystem not available' };`;
6914
8762
  (async function() {
6915
8763
  if (!app.exportEngine || !app.exportEngine.exportWidget) return { error: 'Widget export not available' };
6916
8764
  const result = await app.exportEngine.exportWidget(${JSON.stringify(opts)});
6917
- return { success: true, json: result.json, filename: result.filename, embedCode: result.embedCode, metadata: result.data?.metadata };
8765
+ // THE EMBED BOX WAS ALWAYS 16:9, AND IT IS NOT SELF-CONTAINED (round 9 GG,
8766
+ // 8.32). The engine writes aspect-ratio: 16/9 and max-width: 800px whatever
8767
+ // the scene, so a 300x250 ad sat letterboxed in a wide box. The box is sized
8768
+ // from the canvas here. The embed also loads PineWidget.js from
8769
+ // pinepaper.studio and fetches the scene JSON at runtime — said, with the
8770
+ // zero-dependency alternative.
8771
+ const cs = (typeof app.getCanvasSize === 'function' && app.getCanvasSize()) || app.canvasSize || null;
8772
+ let embedCode = String(result.embedCode || '');
8773
+ if (cs && cs.width > 0 && cs.height > 0) {
8774
+ // RegExp from strings with no backslashes — a template literal eats them.
8775
+ embedCode = embedCode
8776
+ .replace(new RegExp('aspect-ratio:[ ]*[0-9.]+[ ]*/[ ]*[0-9.]+'), 'aspect-ratio: ' + cs.width + '/' + cs.height)
8777
+ .replace(new RegExp('max-width:[ ]*[0-9.]+px'), 'max-width: ' + cs.width + 'px');
8778
+ }
8779
+ const deps = [];
8780
+ const scriptSrc = /<script[^>]+src="([^"]+)"/.exec(embedCode);
8781
+ if (scriptSrc) deps.push({ kind: 'script', url: scriptSrc[1] });
8782
+ if (result.filename) deps.push({ kind: 'scene-json', url: result.filename, note: 'fetched at runtime — host it beside the page' });
8783
+ return { success: true, json: result.json, filename: result.filename, embedCode: embedCode, metadata: result.data?.metadata,
8784
+ size: cs ? { width: cs.width, height: cs.height } : null,
8785
+ dependencies: deps,
8786
+ note: 'this embed needs the files in dependencies at runtime. For a single self-contained file with no network calls (ads, email-safe hosting), use pinepaper_export_widget_html.' };
6918
8787
  })();`.trim();
6919
8788
  }
6920
8789
  generateExportWidgetHtml(input) {
@@ -6928,7 +8797,26 @@ if (!app.spriteSystem) return { error: 'SpriteSheetSystem not available' };`;
6928
8797
  (async function() {
6929
8798
  if (!app.exportEngine || !app.exportEngine.exportWidgetHTML) return { error: 'Widget HTML export not available' };
6930
8799
  const result = await app.exportEngine.exportWidgetHTML(${JSON.stringify(opts)});
6931
- return { success: true, html: result.html, estimatedSize: result.estimatedSize, analysis: { itemTypes: [...result.analysis.itemTypes], relationTypes: [...result.analysis.relationTypes], hasSimpleAnimations: result.analysis.hasSimpleAnimations, hasKeyframeAnimations: result.analysis.hasKeyframeAnimations, hasMasks: result.analysis.hasMasks } };
8800
+ ${input.lang || input.dir || input.alt ? `// lang / dir / alt on the engine's page (it writes <html lang="en"> and no
8801
+ // text alternative). Applied as string edits on the one <html> tag and the
8802
+ // first <canvas>; the result says which landed.
8803
+ const __esc = function(v) { return String(v).replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;'); };
8804
+ let __html = String(result.html || '');
8805
+ const __applied = [];
8806
+ ${input.lang || input.dir ? `__html = __html.replace(/<html\\b[^>]*>/i, function() { __applied.push('html'); return '<html lang="' + __esc(${JSON.stringify(input.lang ?? 'en')}) + '"' + ${JSON.stringify(input.dir ? ` dir="${input.dir}"` : '')} + '>'; });` : ''}
8807
+ ${input.alt ? `// The engine's widget draws its canvas from script, so there is no <canvas>
8808
+ // tag to label (prod retest): its accessible name is div#w's aria-label,
8809
+ // mirrored in the screen-reader text #w-desc. Both carry the alt; a literal
8810
+ // <canvas> is labelled too when a page has one. RegExp from strings, so no
8811
+ // backslash escapes pass through this template.
8812
+ const __alt = __esc(${JSON.stringify(input.alt)});
8813
+ __html = __html.replace(new RegExp('<div id="w"([^>]*?)aria-label="[^"]*"', 'i'), function(m, mid) { __applied.push('widget-label'); return '<div id="w"' + mid + 'aria-label="' + __alt + '"'; });
8814
+ __html = __html.replace(new RegExp('(<div[^>]*id="w-desc"[^>]*>)[^<]*(</div>)', 'i'), function(m, open, close) { __applied.push('widget-desc'); return open + __alt + close; });
8815
+ __html = __html.replace(new RegExp('<canvas(?=[\\\\s>])', 'i'), function(m) { __applied.push('canvas'); return m + ' role="img" aria-label="' + __alt + '"'; });
8816
+ __html = __html.replace(/<head>/i, function(m) { __applied.push('meta'); return m + '<meta name="description" content="' + __esc(${JSON.stringify(input.alt)}) + '">'; });` : ''}
8817
+ result.html = __html;
8818
+ result.a11y = __applied;` : ''}
8819
+ return { success: true, html: result.html,${input.lang || input.dir || input.alt ? ' a11y: result.a11y,' : ''} estimatedSize: result.estimatedSize, analysis: { itemTypes: [...result.analysis.itemTypes], relationTypes: [...result.analysis.relationTypes], hasSimpleAnimations: result.analysis.hasSimpleAnimations, hasKeyframeAnimations: result.analysis.hasKeyframeAnimations, hasMasks: result.analysis.hasMasks } };
6932
8820
  })();`.trim();
6933
8821
  }
6934
8822
  // ===========================================================================
@@ -6942,8 +8830,11 @@ if (!app.spriteSystem) return { error: 'SpriteSheetSystem not available' };`;
6942
8830
  return `(function() { return { error: 'chartType is required for create' }; })();`;
6943
8831
  if (!input.data)
6944
8832
  return `(function() { return { error: 'data is required for create' }; })();`;
8833
+ const fields = resolveChartFields(input.chartType, input.data, input.options || {});
8834
+ if ('error' in fields)
8835
+ return `(function() { return { success: false, error: ${JSON.stringify(fields.error)} }; })();`;
6945
8836
  const dataStr = JSON.stringify(input.data);
6946
- const optsStr = JSON.stringify(input.options || {});
8837
+ const optsStr = JSON.stringify(fields.options);
6947
8838
  return `
6948
8839
  // Create chart
6949
8840
  (function() {
@@ -6951,7 +8842,7 @@ if (!app.spriteSystem) return { error: 'SpriteSheetSystem not available' };`;
6951
8842
  const group = app.createChart(${JSON.stringify(input.chartType)}, ${dataStr}, ${optsStr});
6952
8843
  if (!group) return { error: 'Failed to create chart' };
6953
8844
  const chartId = app.chartSystem.getLastChartId();
6954
- return { success: true, action: 'create', chartType: ${JSON.stringify(input.chartType)}, chartId, dataPoints: ${input.data.length} };
8845
+ return { success: true, action: 'create', chartType: ${JSON.stringify(input.chartType)}, chartId, dataPoints: ${input.data.length}${fields.inferred ? `, fields: ${JSON.stringify(fields.inferred)}` : ''} };
6955
8846
  })();`.trim();
6956
8847
  }
6957
8848
  case 'update': {
@@ -7270,6 +9161,23 @@ if (!app.spriteSystem) return { error: 'SpriteSheetSystem not available' };`;
7270
9161
  * with asNodes promotes each detection to a typed, image-anchored design node
7271
9162
  * (pp:Detected*) instead of a labeled box. Async (runs an on-device ML model).
7272
9163
  */
9164
+ // THE MODEL CANNOT BE DOWNLOADED, AND THE ERROR SAID ONLY "Failed to fetch".
9165
+ //
9166
+ // Detection runs transformers.js in the page, which pulls the model weights
9167
+ // from huggingface.co. pinepaper.studio's connect-src lists neither that host
9168
+ // nor its CDN (checked against the served header), so the download is refused
9169
+ // for every page on production — an agent read "Failed to fetch
9170
+ // (huggingface.co)" and could not tell whether to retry, pass another image,
9171
+ // or give up. It is a deployment fix (allow the host, or serve the weights
9172
+ // from the studio's own origin), and the error now says so.
9173
+ static MODEL_FETCH_HINT = `function(msg) {
9174
+ if (/fail(ed)? to fetch|networkerror|load failed/i.test(msg) && /huggingface|hf\\.co/i.test(msg)) {
9175
+ return 'the detection model could not be downloaded: the page could not fetch its weights from huggingface.co. '
9176
+ + 'The studio has to allow that host in its content-security policy, or serve the weights itself — no argument to this call works around it, and retrying will not help. '
9177
+ + 'Other image tools are unaffected. (' + msg + ')';
9178
+ }
9179
+ return msg;
9180
+ }`;
7273
9181
  generateDetectObjects(input) {
7274
9182
  const args = JSON.stringify({
7275
9183
  ...(input.itemId !== undefined ? { itemId: input.itemId } : {}),
@@ -7285,8 +9193,10 @@ if (!app.spriteSystem) return { error: 'SpriteSheetSystem not available' };`;
7285
9193
  }
7286
9194
  try {
7287
9195
  const res = await app.detectObjects(${args});
7288
- return Object.assign({ success: !!(res && res.ok) }, res || {});
7289
- } catch (e) { return { success: false, error: String((e && e.message) || e) }; }
9196
+ const out = Object.assign({ success: !!(res && res.ok) }, res || {});
9197
+ if (!out.success && typeof out.error === 'string') out.error = (${PinePaperCodeGenerator.MODEL_FETCH_HINT})(out.error);
9198
+ return out;
9199
+ } catch (e) { return { success: false, error: (${PinePaperCodeGenerator.MODEL_FETCH_HINT})(String((e && e.message) || e)) }; }
7290
9200
  })();`.trim();
7291
9201
  }
7292
9202
  /**
@@ -7309,8 +9219,10 @@ if (!app.spriteSystem) return { error: 'SpriteSheetSystem not available' };`;
7309
9219
  }
7310
9220
  try {
7311
9221
  const res = await app.extractObject(${args});
7312
- return Object.assign({ success: !!(res && res.ok) }, res || {});
7313
- } catch (e) { return { success: false, error: String((e && e.message) || e) }; }
9222
+ const out = Object.assign({ success: !!(res && res.ok) }, res || {});
9223
+ if (!out.success && typeof out.error === 'string') out.error = (${PinePaperCodeGenerator.MODEL_FETCH_HINT})(out.error);
9224
+ return out;
9225
+ } catch (e) { return { success: false, error: (${PinePaperCodeGenerator.MODEL_FETCH_HINT})(String((e && e.message) || e)) }; }
7314
9226
  })();`.trim();
7315
9227
  }
7316
9228
  /**
@@ -7367,7 +9279,10 @@ if (!app.spriteSystem) return { error: 'SpriteSheetSystem not available' };`;
7367
9279
  app.groupManager.addItemsToGroup(items, group);
7368
9280
  if (app.historyManager) app.historyManager.saveState();
7369
9281
  const groupId = (group.data && (group.data.registryId || group.data.groupId)) || null;
7370
- return { success: true, groupId: groupId, groupName: group.data && group.data.groupName, itemCount: items.length };`;
9282
+ // itemId too (round 8 BB, 1.70): every other create answers itemId, and a
9283
+ // group is an item every other tool addresses by that id. In a batch this
9284
+ // does not claim a $N slot — only create ops do.
9285
+ return { success: true, groupId: groupId, itemId: groupId, groupName: group.data && group.data.groupName, itemCount: items.length };`;
7371
9286
  }
7372
9287
  /**
7373
9288
  * Group a set of items into one draggable entity, or ungroup one back into loose
@@ -7386,7 +9301,7 @@ if (!app.spriteSystem) return { error: 'SpriteSheetSystem not available' };`;
7386
9301
  if (!result || !result.parts || !result.parts.length) return { success: false, error: 'Nothing to break apart (no sub-parts found)' };
7387
9302
  const partIds = result.parts.map(function(p) { return p && p.data && (p.data.registryId || p.data.id); }).filter(Boolean);
7388
9303
  const groupId = (result.group && result.group.data && (result.group.data.registryId || result.group.data.groupId)) || null;
7389
- return { success: true, action: 'break_apart', groupId: groupId, partIds: partIds, partCount: partIds.length };
9304
+ return { success: true, action: 'break_apart', groupId: groupId, itemId: groupId, partIds: partIds, partCount: partIds.length };
7390
9305
  })();`.trim();
7391
9306
  }
7392
9307
  if (input.action === 'ungroup') {
@@ -7442,6 +9357,68 @@ if (!app.spriteSystem) return { error: 'SpriteSheetSystem not available' };`;
7442
9357
  * sceneAt(t). Returns a cheap per-frame hash by default (token-light) so an agent can
7443
9358
  * verify determinism (re-run → identical hashes) and whether frames actually change.
7444
9359
  */
9360
+ /**
9361
+ * Measurements for pinepaper_accessibility_check; the verdicts are made on
9362
+ * the server (src/utils/a11y.ts). Text: fill, size, weight, and the solid
9363
+ * fill of the topmost item under its centre. Flash: mean relative luminance
9364
+ * of a 3 x 3 grid per sampled frame, through captureFramesAt (deterministic
9365
+ * seek). Pixel work is native reduce / Array.from, not generated loops, so
9366
+ * the loop budget does not count it.
9367
+ */
9368
+ generateAccessibilityCheck(input) {
9369
+ const checks = input.checks ?? ['contrast', 'flash'];
9370
+ const fps = input.fps ?? 20;
9371
+ const duration = input.duration ?? 5;
9372
+ const n = Math.min(1200, Math.max(3, Math.round(duration * fps)));
9373
+ return `
9374
+ // Accessibility measurements (${checks.join(', ')})
9375
+ (function() {
9376
+ const out = { success: true, fps: ${fps}, duration: ${duration} };
9377
+ const rgba = function(c) {
9378
+ if (!c || c.gradient || typeof c.red !== 'number') return null;
9379
+ return { r: Math.round(c.red * 255), g: Math.round(c.green * 255), b: Math.round(c.blue * 255), a: typeof c.alpha === 'number' ? c.alpha : 1 };
9380
+ };
9381
+ out.page = (app.canvasEl && app.canvasEl.style && app.canvasEl.style.backgroundColor) || null;
9382
+ ${checks.includes('contrast') ? ` const all = (app.itemRegistry && typeof app.itemRegistry.getAll === 'function') ? app.itemRegistry.getAll() : [];
9383
+ const texts = all.filter(function(e) { return e && e.item && e.item.className === 'PointText' && e.item.visible !== false && String(e.item.content || '').trim() !== ''; });
9384
+ const solids = all.filter(function(e) { return e && e.item && e.item.className !== 'PointText' && e.item.visible !== false && rgba(e.item.fillColor) && e.item.bounds; });
9385
+ out.texts = texts.map(function(e) {
9386
+ const it = e.item, c = it.bounds.center;
9387
+ const under = solids.filter(function(s) { return s.item.bounds.contains(c) && typeof s.item.isBelow === 'function' && s.item.isBelow(it); });
9388
+ const top = under.reduce(function(best, s) { return (!best || (typeof s.item.isAbove === 'function' && s.item.isAbove(best.item))) ? s : best; }, null);
9389
+ const w = String(it.fontWeight || '');
9390
+ return { id: e.itemId, content: String(it.content), fill: rgba(it.fillColor), fontSize: it.fontSize || 0,
9391
+ bold: w === 'bold' || Number(w) >= 700, behind: top ? rgba(top.item.fillColor) : null, behindId: top ? top.itemId : undefined };
9392
+ });
9393
+ ` : ''}${checks.includes('flash') ? ` if (typeof app.captureFramesAt !== 'function') {
9394
+ out.flashUnavailable = 'this studio has no deterministic frame capture (app.captureFramesAt), so flashing was not checked.';
9395
+ } else {
9396
+ const W = 48, H = 27;
9397
+ const small = document.createElement('canvas');
9398
+ small.width = W; small.height = H;
9399
+ const ctx = small.getContext('2d', { willReadFrequently: true });
9400
+ const lut = Array.from({ length: 256 }, function(_, v) { const x = v / 255; return x <= 0.04045 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4); });
9401
+ const times = Array.from({ length: ${n} }, function(_, i) { return i / ${fps}; });
9402
+ out.series = app.captureFramesAt(times, { capture: function(c) {
9403
+ // Transparent canvas shows the page's CSS background, so composite on it.
9404
+ ctx.fillStyle = out.page || '#ffffff';
9405
+ ctx.fillRect(0, 0, W, H);
9406
+ ctx.drawImage(c, 0, 0, W, H);
9407
+ const px = ctx.getImageData(0, 0, W, H).data;
9408
+ const acc = Array.prototype.reduce.call(px, function(a, v, k) {
9409
+ if (k % 4 !== 3) return a;
9410
+ const p = (k - 3) / 4, x = p % W, y = (p - x) / W;
9411
+ const cell = Math.min(2, Math.floor(y * 3 / H)) * 3 + Math.min(2, Math.floor(x * 3 / W));
9412
+ a.sum[cell] += 0.2126 * lut[px[k - 3]] + 0.7152 * lut[px[k - 2]] + 0.0722 * lut[px[k - 1]];
9413
+ a.n[cell] += 1;
9414
+ return a;
9415
+ }, { sum: [0, 0, 0, 0, 0, 0, 0, 0, 0], n: [0, 0, 0, 0, 0, 0, 0, 0, 0] });
9416
+ return acc.sum.map(function(s, i) { return acc.n[i] ? s / acc.n[i] : 0; });
9417
+ } });
9418
+ }
9419
+ ` : ''} return out;
9420
+ })();`.trim();
9421
+ }
7445
9422
  generateCaptureFrames(input) {
7446
9423
  const timesJson = JSON.stringify(input.times);
7447
9424
  const seed = input.seed !== undefined ? input.seed : 0;
@@ -7453,7 +9430,18 @@ if (!app.spriteSystem) return { error: 'SpriteSheetSystem not available' };`;
7453
9430
  return { success: false, error: 'app.captureFramesAt unavailable — update FxTool to a build with the deterministic capture entrypoint' };
7454
9431
  }
7455
9432
  const includeDataUrls = ${includeDataUrls};
7456
- const hashStr = function(s) { let h = 5381; for (let i = 0; i < s.length; i++) { h = (((h << 5) + h) ^ s.charCodeAt(i)) >>> 0; } return h.toString(16); };
9433
+ // THE HASH LOOP RAN ONCE PER CHARACTER OF A DATA URL, inside generated code,
9434
+ // where every loop site is budgeted at 2,000,000 iterations — so any frame
9435
+ // over ~2 MB (three photos, a shader backdrop) died "Loop budget exceeded".
9436
+ // app.hashFrame is the same djb2-xor in engine code, outside the budget, so
9437
+ // hashes stay comparable with builds that only have the fallback.
9438
+ const hashStr = (typeof app.hashFrame === 'function')
9439
+ ? function(s) { return app.hashFrame(s); }
9440
+ // The fallback must not be a for loop either: the budget is per loop SITE
9441
+ // across the whole run, so 50 frames of ~40 KB data URLs exhausted it on a
9442
+ // plain 9-text scene (round 7 X, #7) on studios without hashFrame. A native
9443
+ // reduce is not a generated loop and is not counted; same djb2-xor.
9444
+ : function(s) { return Array.prototype.reduce.call(s, function(h, c) { return (((h << 5) + h) ^ c.charCodeAt(0)) >>> 0; }, 5381).toString(16); };
7457
9445
  const frames = app.captureFramesAt(${timesJson}, {
7458
9446
  seed: ${seed},
7459
9447
  capture: function(c, t, i) {
@@ -7571,6 +9559,47 @@ if (!app.spriteSystem) return { error: 'SpriteSheetSystem not available' };`;
7571
9559
  generateMedia(input) {
7572
9560
  const guard = ` const A = (typeof window !== 'undefined') && window.PinePaperAgent;
7573
9561
  if (!A || typeof A.uploadVideo !== 'function') { return { success: false, error: 'window.PinePaperAgent media API unavailable — update FxTool to a media-capable build' }; }`;
9562
+ // The URL to upload from: the staged bytes when the handler put a large
9563
+ // data: URL on window.__ppStage (see pinepaper_media in handlers.ts).
9564
+ //
9565
+ // A data: URL IS DECODED HERE INTO A File, NOT HANDED OVER AS A URL. The
9566
+ // engine's upload fetch()es a string, and production's CSP refuses fetch()
9567
+ // of data: — so every data: upload failed on prod with "Failed to fetch ()"
9568
+ // at every size (a local engine without the CSP accepted small ones, which
9569
+ // is what made it look like a size limit). uploadAudio/uploadVideo take a
9570
+ // File directly. Uint8Array.from with a map, not a for loop: a generated
9571
+ // loop is budgeted per iteration and a few MB of audio would exhaust it.
9572
+ const srcExpr = (url, kind) => `(function(u) {
9573
+ if (u.indexOf('__ppStage:') === 0) {
9574
+ const k = u.slice('__ppStage:'.length);
9575
+ const v = window.__ppStage && window.__ppStage[k];
9576
+ if (!v) throw new Error('the media bytes were staged as ' + k + ' and are not on the page — a bug in the MCP server, not your call.');
9577
+ u = v;
9578
+ }
9579
+ if (u.indexOf('data:') !== 0) return u;
9580
+ const comma = u.indexOf(',');
9581
+ const meta = u.slice(5, comma);
9582
+ const mime = meta.split(';')[0] || '${kind === 'audio' ? 'audio/mpeg' : 'video/mp4'}';
9583
+ const body = u.slice(comma + 1);
9584
+ const bin = /;base64/i.test(meta) ? atob(body) : decodeURIComponent(body);
9585
+ const bytes = Uint8Array.from(bin, function(c) { return c.charCodeAt(0); });
9586
+ const ext = (mime.split('/')[1] || '${kind === 'audio' ? 'mp3' : 'mp4'}').replace(/[^a-z0-9]/gi, '');
9587
+ return new File([bytes], '${kind}.' + ext, { type: mime });
9588
+ })(${JSON.stringify(url ?? '')})`;
9589
+ // TWO ID SPACES, AND THE CALLER HOLDS THE WRONG ONE.
9590
+ //
9591
+ // upload answers {id, registryId}; every other tool on this surface speaks
9592
+ // registry ids (item_N), so that is the one callers keep. But remove,
9593
+ // setMediaPlaybackRate and setMediaClip look the id up in the audio/video
9594
+ // layers, which are keyed by MEDIA id — so set_playback_rate with the
9595
+ // registryId returned false, reported as a failure with no reason. Either
9596
+ // form is resolved here; a miss names the id and what was looked for.
9597
+ const resolveMedia = ` const __ref = ${JSON.stringify(input.id ?? '')};
9598
+ const __m = (typeof A.listMedia === 'function')
9599
+ ? A.listMedia().find(function(x) { return x.id === __ref || x.registryId === __ref; })
9600
+ : null;
9601
+ const __mid = __m ? __m.id : __ref;
9602
+ const __miss = { success: false, error: 'no uploaded audio or video matches ' + JSON.stringify(__ref) + ' — pass the id or registryId that upload returned (media list shows both).' };`;
7574
9603
  switch (input.action) {
7575
9604
  case 'upload_video': {
7576
9605
  const opts = JSON.stringify({
@@ -7584,7 +9613,7 @@ if (!app.spriteSystem) return { error: 'SpriteSheetSystem not available' };`;
7584
9613
  // Upload video from URL
7585
9614
  (async function() {
7586
9615
  ${guard}
7587
- const info = await A.uploadVideo(${JSON.stringify(input.url)}, ${opts});
9616
+ const info = await A.uploadVideo(${srcExpr(input.url, 'video')}, ${opts});
7588
9617
  return { success: true, action: 'upload_video', media: info };
7589
9618
  })();`.trim();
7590
9619
  }
@@ -7599,7 +9628,13 @@ ${guard}
7599
9628
  // Upload audio from URL
7600
9629
  (async function() {
7601
9630
  ${guard}
7602
- const info = await A.uploadAudio(${JSON.stringify(input.url)}, ${opts});
9631
+ const info = await A.uploadAudio(${srcExpr(input.url, 'audio')}, ${opts});${input.volume !== undefined ? `
9632
+ // THE LEVEL IS STORED UNDER ONE KEY AND EXPORTED FROM ANOTHER. The upload
9633
+ // records volume as \`gain\` on the registry entry; the video exporter's mix
9634
+ // reads \`audioGain\`. So a bed uploaded at 0.25 exported at unity (measured:
9635
+ // 0.0 dB change). Written to both until the engine reads one.
9636
+ const __ae = info && info.registryId && app.itemRegistry ? app.itemRegistry.get(info.registryId) : null;
9637
+ if (__ae) { __ae.properties = __ae.properties || {}; __ae.properties.audioGain = ${input.volume}; }` : ''}
7603
9638
  return { success: true, action: 'upload_audio', media: info };
7604
9639
  })();`.trim();
7605
9640
  }
@@ -7615,16 +9650,24 @@ ${guard}
7615
9650
  // Remove media
7616
9651
  (function() {
7617
9652
  ${guard}
7618
- const removed = A.removeMedia(${JSON.stringify(input.id)});
7619
- return { success: removed, action: 'remove', id: ${JSON.stringify(input.id)}, removed: removed };
9653
+ ${resolveMedia}
9654
+ if (!__m) return __miss;
9655
+ const removed = A.removeMedia(__mid);
9656
+ return removed
9657
+ ? { success: true, action: 'remove', id: __mid, registryId: __m.registryId, removed: true }
9658
+ : { success: false, action: 'remove', id: __mid, error: 'the studio did not remove ' + __mid + '.' };
7620
9659
  })();`.trim();
7621
9660
  case 'set_playback_rate':
7622
9661
  return `
7623
9662
  // Set media playback rate
7624
9663
  (function() {
7625
9664
  ${guard}
7626
- const ok = A.setMediaPlaybackRate(${JSON.stringify(input.id)}, ${input.rate});
7627
- return { success: ok, action: 'set_playback_rate', id: ${JSON.stringify(input.id)}, rate: ${input.rate} };
9665
+ ${resolveMedia}
9666
+ if (!__m) return __miss;
9667
+ const ok = A.setMediaPlaybackRate(__mid, ${input.rate});
9668
+ return ok
9669
+ ? { success: true, action: 'set_playback_rate', id: __mid, registryId: __m.registryId, rate: ${input.rate} }
9670
+ : { success: false, action: 'set_playback_rate', id: __mid, error: 'the studio refused rate ${input.rate} for ' + __mid + '.' };
7628
9671
  })();`.trim();
7629
9672
  case 'set_clip':
7630
9673
  return `
@@ -7632,8 +9675,12 @@ ${guard}
7632
9675
  (function() {
7633
9676
  ${guard}
7634
9677
  if (typeof A.setMediaClip !== 'function') { return { success: false, error: 'setMediaClip unavailable — update FxTool' }; }
7635
- const ok = A.setMediaClip(${JSON.stringify(input.id)}, ${input.inPoint}, ${input.outPoint});
7636
- return { success: ok, action: 'set_clip', id: ${JSON.stringify(input.id)}, inPoint: ${input.inPoint}, outPoint: ${input.outPoint} };
9678
+ ${resolveMedia}
9679
+ if (!__m) return __miss;
9680
+ const ok = A.setMediaClip(__mid, ${input.inPoint}, ${input.outPoint});
9681
+ return ok
9682
+ ? { success: true, action: 'set_clip', id: __mid, registryId: __m.registryId, inPoint: ${input.inPoint}, outPoint: ${input.outPoint} }
9683
+ : { success: false, action: 'set_clip', id: __mid, error: 'the studio refused the clip window ${input.inPoint}–${input.outPoint} for ' + __mid + '.' };
7637
9684
  })();`.trim();
7638
9685
  // ── Video-editing actions (v1.6.4) — these live on `app` (PinePaper
7639
9686
  // facades), not the agent media API, so they carry their own guards.
@@ -7645,7 +9692,33 @@ ${guard}
7645
9692
  (function() {
7646
9693
  if (typeof app.setTimeRemap !== 'function') { return { success: false, error: 'app.setTimeRemap unavailable — update FxTool to a time-remap-capable build' }; }
7647
9694
  const r = app.setTimeRemap(${S(input.id)}, ${S(input.remapTrack ?? null)});
7648
- return r && r.ok ? { success: true, action: 'set_time_remap', points: r.points } : { success: false, error: (r && r.error) || 'remap failed' };
9695
+ if (!(r && r.ok)) return { success: false, error: (r && r.error) || 'remap failed' };
9696
+ // WHAT THE REMAP CANNOT DO, READ BACK FROM WHAT WAS STORED. The clip is on
9697
+ // the timeline only for its trimmed window, and past that it is HIDDEN
9698
+ // whatever the curve says — a remap to 6.5 s on a 5 s clip cut to black at
9699
+ // 5.0 s. Source values outside [inPoint, outPoint] are clamped, so that part
9700
+ // of the curve is a hold. Both answered success before.
9701
+ const __rid = typeof app._resolveId === 'function' ? app._resolveId(${S(input.id)}) : ${S(input.id)};
9702
+ const __e = __rid && app.itemRegistry && app.itemRegistry.get(__rid);
9703
+ const __p = (__e && __e.properties) || {};
9704
+ const __tr = Array.isArray(__p.timeRemap) ? __p.timeRemap : [];
9705
+ const __warnings = [];
9706
+ const __out = typeof __p.outPoint === 'number' ? __p.outPoint : __p.duration;
9707
+ if (__tr.length > 1 && Number.isFinite(__out)) {
9708
+ const __in = typeof __p.inPoint === 'number' ? __p.inPoint : 0;
9709
+ const __start = typeof __p.clipStartTime === 'number' ? __p.clipStartTime : 0;
9710
+ const __end = __start + Math.max(0.001, __out - __in);
9711
+ const __last = __tr[__tr.length - 1].time;
9712
+ if (__last > __end + 0.001) {
9713
+ __warnings.push('the remap runs to ' + __last + ' s but the clip is on the timeline only until ' + __end.toFixed(3) + ' s (start + trimmed length); after that it is hidden, not extended. Lengthen the clip window first.');
9714
+ }
9715
+ const __vals = __tr.map(function(k) { return k.value; });
9716
+ const __hi = Math.max.apply(null, __vals), __lo = Math.min.apply(null, __vals);
9717
+ if (__hi > __out + 0.001 || __lo < __in - 0.001) {
9718
+ __warnings.push('remap source times run ' + __lo.toFixed(3) + '–' + __hi.toFixed(3) + ' s but the clip only has ' + __in.toFixed(3) + '–' + __out.toFixed(3) + ' s; the part outside is clamped, which plays as a hold.');
9719
+ }
9720
+ }
9721
+ return Object.assign({ success: true, action: 'set_time_remap', points: r.points }, __warnings.length ? { warnings: __warnings } : {});
7649
9722
  })();`.trim();
7650
9723
  }
7651
9724
  case 'speed_ramp': {
@@ -7655,7 +9728,33 @@ ${guard}
7655
9728
  (function() {
7656
9729
  if (typeof app.speedRamp !== 'function') { return { success: false, error: 'app.speedRamp unavailable — update FxTool to a time-remap-capable build' }; }
7657
9730
  const r = app.speedRamp(${S(input.id)}, ${S(input.segments)});
7658
- return r && r.ok ? { success: true, action: 'speed_ramp', points: r.points } : { success: false, error: (r && r.error) || 'speed ramp failed' };
9731
+ if (!(r && r.ok)) return { success: false, error: (r && r.error) || 'speed ramp failed' };
9732
+ // WHAT THE REMAP CANNOT DO, READ BACK FROM WHAT WAS STORED. The clip is on
9733
+ // the timeline only for its trimmed window, and past that it is HIDDEN
9734
+ // whatever the curve says — a remap to 6.5 s on a 5 s clip cut to black at
9735
+ // 5.0 s. Source values outside [inPoint, outPoint] are clamped, so that part
9736
+ // of the curve is a hold. Both answered success before.
9737
+ const __rid = typeof app._resolveId === 'function' ? app._resolveId(${S(input.id)}) : ${S(input.id)};
9738
+ const __e = __rid && app.itemRegistry && app.itemRegistry.get(__rid);
9739
+ const __p = (__e && __e.properties) || {};
9740
+ const __tr = Array.isArray(__p.timeRemap) ? __p.timeRemap : [];
9741
+ const __warnings = [];
9742
+ const __out = typeof __p.outPoint === 'number' ? __p.outPoint : __p.duration;
9743
+ if (__tr.length > 1 && Number.isFinite(__out)) {
9744
+ const __in = typeof __p.inPoint === 'number' ? __p.inPoint : 0;
9745
+ const __start = typeof __p.clipStartTime === 'number' ? __p.clipStartTime : 0;
9746
+ const __end = __start + Math.max(0.001, __out - __in);
9747
+ const __last = __tr[__tr.length - 1].time;
9748
+ if (__last > __end + 0.001) {
9749
+ __warnings.push('the remap runs to ' + __last + ' s but the clip is on the timeline only until ' + __end.toFixed(3) + ' s (start + trimmed length); after that it is hidden, not extended. Lengthen the clip window first.');
9750
+ }
9751
+ const __vals = __tr.map(function(k) { return k.value; });
9752
+ const __hi = Math.max.apply(null, __vals), __lo = Math.min.apply(null, __vals);
9753
+ if (__hi > __out + 0.001 || __lo < __in - 0.001) {
9754
+ __warnings.push('remap source times run ' + __lo.toFixed(3) + '–' + __hi.toFixed(3) + ' s but the clip only has ' + __in.toFixed(3) + '–' + __out.toFixed(3) + ' s; the part outside is clamped, which plays as a hold.');
9755
+ }
9756
+ }
9757
+ return Object.assign({ success: true, action: 'speed_ramp', points: r.points }, __warnings.length ? { warnings: __warnings } : {});
7659
9758
  })();`.trim();
7660
9759
  }
7661
9760
  case 'match_cut': {
@@ -7797,9 +9896,19 @@ ${guard}
7797
9896
  // Text: write it as STROKED handwriting — a path, not a glyph
7798
9897
  (function() {
7799
9898
  if (typeof app.createCursiveText !== 'function') { return { success: false, error: 'app.createCursiveText unavailable — update FxTool' }; }
7800
- const item = app.createCursiveText(${S(input.text)}, ${S(input.cursiveOptions ?? {})});
7801
- if (!item) { return { success: false, error: 'the cursive text produced no path' }; }
7802
- return { success: true, itemId: item.data && item.data.id };
9899
+ // createCursiveText answers { items, group, totalWidth, bounds } — not an
9900
+ // item — so reading .data.id off the answer returned no id, every time. The
9901
+ // group is registered only on one of its two branches; the other is
9902
+ // registered here so the caller always gets an addressable id.
9903
+ const r = app.createCursiveText(${S(input.text)}, ${S(input.cursiveOptions ?? {})});
9904
+ const g = r && (r.group || (r.data ? r : null));
9905
+ if (!g) { return { success: false, error: 'the cursive text produced no path' }; }
9906
+ let itemId = g.data && (g.data.id || g.data.registryId);
9907
+ if (!itemId && app.itemRegistry && typeof app.itemRegistry.register === 'function') {
9908
+ itemId = app.itemRegistry.register(g, 'cursiveText', { content: ${S(input.text)} }, 'user');
9909
+ }
9910
+ if (!itemId) { return { success: false, error: 'the cursive text was drawn but not registered — no usable item id.' }; }
9911
+ return { success: true, itemId: itemId, strokes: (r.items || []).length, width: r.totalWidth };
7803
9912
  })();`.trim();
7804
9913
  case 'wrap':
7805
9914
  return `
@@ -8283,6 +10392,25 @@ ${needWorld}
8283
10392
  mesh.rotY = mesh.rotYDegrees * Math.PI / 180;
8284
10393
  delete mesh.rotYDegrees;
8285
10394
  }
10395
+ // A COLOUR, NOT A BLACK MESH (round 9 HH, 5.64). The default mesh
10396
+ // shader reads `uniform vec3 diffuse` (0..1) plus metalness / roughness,
10397
+ // and extrude / lathe passed no uniforms — so every authored mesh
10398
+ // rendered black and there was no parameter to change it. color (hex,
10399
+ // [r,g,b] 0..255 or 0..1) becomes diffuse; with none given it is a
10400
+ // neutral light grey, which lights readably. Explicit uniforms win.
10401
+ {
10402
+ const uniforms = { ...(mesh.uniforms ?? {}) };
10403
+ if (uniforms.diffuse === undefined)
10404
+ uniforms.diffuse = mesh.color !== undefined ? world3dColor(mesh.color) : [0.8, 0.8, 0.8];
10405
+ if (uniforms.metalness === undefined && typeof mesh.metalness === 'number')
10406
+ uniforms.metalness = mesh.metalness;
10407
+ if (uniforms.roughness === undefined)
10408
+ uniforms.roughness = typeof mesh.roughness === 'number' ? mesh.roughness : 0.6;
10409
+ delete mesh.color;
10410
+ delete mesh.metalness;
10411
+ delete mesh.roughness;
10412
+ mesh.uniforms = uniforms;
10413
+ }
8286
10414
  return `
8287
10415
  // World3D: ${input.action} — a canvas path becomes real geometry
8288
10416
  (async function() {
@@ -8784,6 +10912,14 @@ ${guard('exportBVH')}
8784
10912
  // Interchange: one PNG per frame
8785
10913
  (async function() {
8786
10914
  ${guard('exportPNGSequence')}
10915
+ // The studio does not size a sequence to the scene: without duration / fps
10916
+ // it writes its own default (measured 45 frames = 3 s at 15 fps for a 1 s
10917
+ // scene, 29 of them duplicates). Said in the result when that happened.
10918
+ const __pngSeqDefaults = ${JSON.stringify((() => {
10919
+ const o = (input.options ?? {});
10920
+ const missing = ['duration', 'fps'].filter((k) => o[k] === undefined);
10921
+ return missing.length ? { defaultsUsed: missing, note: `${missing.join(' and ')} not given, so the studio's own default length was used — it does not follow the scene (runs have measured 45 and 90 frames). Pass options.duration and options.fps to match the scene.` } : {};
10922
+ })())};
8787
10923
  const out = await app.exportPNGSequence(${opts});
8788
10924
  if (!out) { return { success: false, error: 'PNG sequence produced nothing' }; }
8789
10925
  // A BLOB SERIALISES TO {}. exporter.export() answers a Blob, and returning it
@@ -8798,7 +10934,7 @@ ${guard('exportPNGSequence')}
8798
10934
  fr.onerror = function() { rej(new Error('could not read the PNG sequence')); };
8799
10935
  fr.readAsDataURL(out);
8800
10936
  });
8801
- return { success: true, format: 'png-sequence', data: dataUrl, mimeType: out.type || 'application/zip', size: out.size };
10937
+ return Object.assign({ success: true, format: 'png-sequence', data: dataUrl, mimeType: out.type || 'application/zip', size: out.size }, __pngSeqDefaults);
8802
10938
  }
8803
10939
  if (Array.isArray(out)) {
8804
10940
  return { success: true, format: 'png-sequence', frames: out.length,
@@ -8967,6 +11103,51 @@ ${guard('createSound')}
8967
11103
  // setSoundPlacement the separate action calls.
8968
11104
  ${placement ? `if (_sid && app.setSoundPlacement) { app.setSoundPlacement(_sid, ${S(placement)}); }` : ''}
8969
11105
  return { success: true, itemId: _sid, itemType: 'sound'${placement ? `, placement: ${S(placement)}` : ''} };
11106
+ })();`.trim();
11107
+ }
11108
+ case 'sequence': {
11109
+ // ONE CALL FOR A WHOLE BED.
11110
+ //
11111
+ // Every cue was create + set_placement, so a 112-cue music bed cost 112
11112
+ // round trips once visual.startTime landed and 224 before it. Nothing
11113
+ // about the work needs a trip each: the cues are independent, they
11114
+ // differ only in time and spec, and the page can loop.
11115
+ //
11116
+ // Each cue is reported individually. A bed where three cues failed and
11117
+ // 109 worked is not a failure, and it is not a success either — the
11118
+ // caller needs to know WHICH, so placed/failed come back by index.
11119
+ const cues = (input.cues ?? []);
11120
+ return `
11121
+ // Sound: a whole sequence of cues, created and placed in one pass
11122
+ (function() {
11123
+ ${guard('createSound')}
11124
+ const cues = ${S(cues)};
11125
+ const placed = [];
11126
+ const failed = [];
11127
+ for (let i = 0; i < cues.length; i++) {
11128
+ const c = cues[i];
11129
+ try {
11130
+ const spec = c.spec || (c.preset ? { preset: c.preset, note: c.note } : null);
11131
+ if (!spec) { failed.push({ index: i, t: c.t, error: 'neither spec nor preset' }); continue; }
11132
+ const item = app.createSound(spec, c.visual || {});
11133
+ if (!item) { failed.push({ index: i, t: c.t, error: 'the cue produced no waveform path' }); continue; }
11134
+ const id = item.data && item.data.id;
11135
+ if (id && app.setSoundPlacement) {
11136
+ app.setSoundPlacement(id, { startTime: c.t, ...(c.duration ? { duration: c.duration } : {}) });
11137
+ }
11138
+ placed.push({ index: i, itemId: id, t: c.t });
11139
+ } catch (e) {
11140
+ failed.push({ index: i, t: c.t, error: (e && e.message) || 'cue failed' });
11141
+ }
11142
+ }
11143
+ return {
11144
+ success: failed.length === 0,
11145
+ action: 'sequence',
11146
+ placed: placed.length,
11147
+ itemIds: placed.map(function(p) { return p.itemId; }),
11148
+ cues: placed,
11149
+ ...(failed.length ? { failed: failed, error: failed.length + ' of ' + cues.length + ' cues did not sound — see failed[] for which.' } : {}),
11150
+ };
8970
11151
  })();`.trim();
8971
11152
  }
8972
11153
  case 'timbre_from_path':