swf2js 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +9 -9
  2. package/package.json +2 -1
  3. package/src/cli.js +15 -6
  4. package/src/emitter/bundle-emitter.js +56 -16
  5. package/src/runtime/avm1-runtime.js +329 -84
  6. package/src/runtime/avm2-runtime.js +56 -6
  7. package/src/runtime/embedded-movie-runtime.js +6 -1
  8. package/src/runtime/index.js +2 -6
  9. package/src/runtime/movie-runtime.js +2 -1
  10. package/src/runtime/swflib/assemble.js +34 -0
  11. package/src/runtime/swflib/bitmap-image.js +4 -0
  12. package/src/runtime/swflib/bitmap.js +1004 -0
  13. package/src/runtime/swflib/builtins.js +156 -0
  14. package/src/runtime/swflib/bytearray.js +949 -0
  15. package/src/runtime/swflib/compiled-swf.js +3 -0
  16. package/src/runtime/swflib/context-menu.js +245 -0
  17. package/src/runtime/swflib/cursor-flag.js +12 -0
  18. package/src/runtime/swflib/dictionary.js +121 -0
  19. package/src/runtime/swflib/display.js +823 -0
  20. package/src/runtime/swflib/events.js +754 -0
  21. package/src/runtime/swflib/filters.js +298 -0
  22. package/src/runtime/swflib/flash.js +130 -0
  23. package/src/runtime/swflib/geom.js +759 -0
  24. package/src/runtime/swflib/glyph-metrics.js +14 -0
  25. package/src/runtime/swflib/graphics.js +735 -0
  26. package/src/runtime/swflib/host.js +2 -0
  27. package/src/runtime/swflib/inherit.js +22 -0
  28. package/src/runtime/swflib/input.js +1085 -0
  29. package/src/runtime/swflib/loader.js +687 -0
  30. package/src/runtime/swflib/marks.js +16 -0
  31. package/src/runtime/swflib/matrix3d.js +144 -0
  32. package/src/runtime/swflib/pointer-bridge.js +8 -0
  33. package/src/runtime/swflib/projection.js +158 -0
  34. package/src/runtime/swflib/render.js +1240 -0
  35. package/src/runtime/swflib/sound.js +1166 -0
  36. package/src/runtime/swflib/sprite-drag.js +116 -0
  37. package/src/runtime/swflib/stage-ref.js +9 -0
  38. package/src/runtime/swflib/stage.js +484 -0
  39. package/src/runtime/swflib/system.js +987 -0
  40. package/src/runtime/swflib/text-field.js +519 -0
  41. package/src/runtime/swflib/text-paint.js +797 -0
  42. package/src/runtime/swflib/text.js +1387 -0
  43. package/src/runtime/swflib/transform.js +133 -0
  44. package/src/runtime/swflib/xml.js +268 -0
  45. package/src/transpiler/avm1/avm1-ir.js +2 -1
  46. package/src/transpiler/avm1/avm1-rewrite.js +23 -2
  47. package/src/transpiler/avm1/avm1-transpiler.js +4 -1
  48. package/src/transpiler/avm2/avm2-transpiler.js +38 -7
  49. package/src/transpiler/timeline/timeline-codegen.js +366 -123
  50. package/src/runtime/swflib.js +0 -14050
@@ -1,3 +1,5 @@
1
+ import { defineTextAdvancePixels, fontOutlineScale } from '../../runtime/swflib/glyph-metrics.js';
2
+
1
3
  /**
2
4
  * @fileoverview Emit browser timeline + display-list runtime JS.
3
5
  *
@@ -330,7 +332,8 @@ export function hitTestButtonGeometry(characters, characterId, matrix, x, y, spr
330
332
  }
331
333
  var depths = Object.keys(entries).map(Number).sort(function (a, b) { return b - a; });
332
334
  var maskDepths = depths.filter(function (depth) {
333
- return entries[depth].clipDepth != null && !clip?._swoofSpriteChildren?.[characterId + ':' + depth]?._swoofTimelineMaskOverridden;
335
+ return entries[depth].clipDepth != null && !clip?._swoofSpriteChildren?.[characterId + ':' + depth]?._swoofTimelineMaskOverridden
336
+ && !clip?._swoofSpriteChildren?.[characterId + ':' + depth]?._swoofReleasedFromTimeline;
334
337
  });
335
338
  for (var ei = 0; ei < depths.length; ei++) {
336
339
  var childDepth = depths[ei], child = entries[childDepth];
@@ -462,7 +465,7 @@ export function collectClipDepthMasks(targetDepth, entries, parentMatrix, clipAt
462
465
  if (!mask || mask.clipDepth == null || depth >= targetDepth || (mask.clipDepth | 0) < targetDepth) continue;
463
466
  if (mask.characterId == null) continue;
464
467
  var clip = clipAtDepth ? clipAtDepth(depth) : mask._clip;
465
- if (clip?._swoofTimelineMaskOverridden) continue;
468
+ if (clip?._swoofTimelineMaskOverridden || clip?._swoofReleasedFromTimeline) continue;
466
469
  var local = (clip && resolveLocalMatrix && resolveLocalMatrix(clip)) ||
467
470
  mask.matrix || { a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0 };
468
471
  masks.push({
@@ -1176,6 +1179,9 @@ function timelineEngineBody(bridge) {
1176
1179
  function indexedInteractivePlace(entry, info) {
1177
1180
  return !!entry.tabIndexTag && (info?.type === 'sprite' || info?.type === 'button' || info?.kind === 'edit');
1178
1181
  }
1182
+ function indexedLeaf(info) {
1183
+ return info?.type === 'shape' || info?.type === 'bitmap';
1184
+ }
1179
1185
 
1180
1186
  function syncAs3PlacementColor(clip, value) {
1181
1187
  var transform = clip.transform;
@@ -1513,9 +1519,7 @@ function timelineEngineBody(bridge) {
1513
1519
  var stencilSurfaces = null;
1514
1520
  var timelinePaintedBranches = new Set();
1515
1521
  function mixedChildOrder(entries, owner, clipFor) {
1516
- if (!owner?._children?.length || owner.visible === false || owner._scrollRect || owner.transform?.matrix3D) return entries;
1517
- if (owner._swoofHostTimeline && (owner.mask || owner.filters?.length || owner.opaqueBackground != null ||
1518
- owner.blendMode && owner.blendMode !== 'normal')) return entries;
1522
+ if (!owner?._children?.length || owner.visible === false) return entries;
1519
1523
  var named = new Map();
1520
1524
  for (var entry of entries) {
1521
1525
  var clip = clipFor(entry);
@@ -2478,7 +2482,8 @@ function timelineEngineBody(bridge) {
2478
2482
  var parts = name.split('.');
2479
2483
  if (parts[0] === '_global') { host = textGlobalObject(swoof); parts.shift(); }
2480
2484
  else if (parts[0] === '_root' || parts[0] === 'root' || parts[0] === '_level0') { host = swoof.root || swoof.main; parts.shift(); }
2481
- if (!host || !parts.length || parts.some(function (part) { return !part; })) return null;
2485
+ parts = parts.filter(function (part) { return part; });
2486
+ if (!host || !parts.length) return null;
2482
2487
  for (var i = 0; i < parts.length - 1; i++) {
2483
2488
  if (parts[i] === '_parent' && host != null) { host = host.parent; continue; }
2484
2489
  if (host == null || !Object.prototype.hasOwnProperty.call(host, parts[i])) return null;
@@ -3145,22 +3150,28 @@ function timelineEngineBody(bridge) {
3145
3150
  wheelText = pickCanvasWheelText(point.x, point.y);
3146
3151
  wheelFocus = runtime.stage?.focus;
3147
3152
  }, true);
3148
- target._swoofAs3CanvasWheel = function (e, delta, x, y) {
3153
+ function applyCanvasWheel(e, delta, x, y, scroll) {
3149
3154
  if (e.defaultPrevented || e.ctrlKey || !wheelText ||
3150
3155
  runtime.stage?.focus !== wheelFocus && runtime.stage?.focus !== wheelText ||
3151
3156
  pickCanvasWheelText(x, y) !== wheelText) return;
3152
- if (runtime.scrollCanvasText?.(wheelText, delta)) {
3157
+ if (scroll?.(wheelText, delta)) {
3153
3158
  e.preventDefault?.();
3154
3159
  timeline.render();
3155
3160
  }
3161
+ }
3162
+ target._swoofAs3CanvasWheel = function (e, delta, x, y) {
3163
+ applyCanvasWheel(e, delta, x, y, runtime.scrollCanvasText);
3164
+ };
3165
+ target._swoofAs3CanvasWheelHorizontal = function (e, delta, x, y) {
3166
+ applyCanvasWheel(e, delta, x, y, runtime.scrollCanvasTextHorizontal);
3156
3167
  };
3157
3168
  }
3158
- if (runtime && runtime.setCanvasHoverRouting) {
3159
- runtime.setCanvasHoverRouting(function (clip) {
3169
+ if (runtime && runtime.setCanvasAuthoredTarget) {
3170
+ runtime.setCanvasAuthoredTarget(function (clip) {
3160
3171
  return _overlayItems.some(function (item) { return item.kind === 'button' && !item.nonPrimary && item.clip === clip; });
3161
3172
  });
3162
3173
  }
3163
- runtime?.setCanvasTouchRouting?.(function (x, y, touch = true, secondary) {
3174
+ runtime?.setCanvasAuthoredTouchPick?.(function (x, y, touch = true, secondary) {
3164
3175
  var hit = buttonAt(x, y, touch, null, secondary);
3165
3176
  return hit && { target: hit.clip, local: authoredPointerLocal(hit.clip, hit.matrix, { x: x, y: y }) };
3166
3177
  });
@@ -3169,8 +3180,8 @@ function timelineEngineBody(bridge) {
3169
3180
  return runtime.timelineAuthoredAbove?.(authored, candidate, timeline);
3170
3181
  });
3171
3182
  function programmaticTargetAt(point) {
3172
- if (!runtime?.canvasPointerTargetAt || !timelineViewportContains(point.x, point.y)) return null;
3173
- return runtime.canvasPointerTargetAt(point.x, point.y);
3183
+ if (!runtime?.canvasProgrammaticPointerAt || !timelineViewportContains(point.x, point.y)) return null;
3184
+ return runtime.canvasProgrammaticPointerAt(point.x, point.y);
3174
3185
  }
3175
3186
  function authoredButtonAt(point) {
3176
3187
  return programmaticTargetAt(point) ? null : buttonAt(point.x, point.y);
@@ -3351,6 +3362,7 @@ function timelineEngineBody(bridge) {
3351
3362
  else if (hit.avm1Drawing) target.focus?.();
3352
3363
  runtime?.setMovieClipButtonState?.(capturedClip, '_down');
3353
3364
  fireHudEvent(hit, 'press', { name: 'overUpToOverDown', from: 'overUp', to: 'overDown' });
3365
+ if (ev) ev._swoofTimelinePress = true;
3354
3366
  dispatchAs3Mouse(hit, point, 'mouseDown', ev);
3355
3367
  try { if (target.setPointerCapture && ev.pointerId != null) target.setPointerCapture(ev.pointerId); } catch (_) {}
3356
3368
  timeline.render();
@@ -3895,27 +3907,17 @@ function timelineEngineBody(bridge) {
3895
3907
  }
3896
3908
  // Dynamic EditText fields bind to their AVM1 variable.
3897
3909
  if (!info.editable && typeof window !== 'undefined' && window.__swoof) {
3898
- var swoof = window.__swoof;
3899
- var host = swoof.root || swoof.main;
3900
3910
  var liveText = null;
3901
- var foundLiveText = false;
3902
- var path = String(info.variableName || '').replace(/\\//g, '.').split('.');
3903
3911
  try {
3904
- if (path.length && (path[0] === '_root' || path[0] === 'root' || path[0] === '_level0')) path.shift();
3905
- else if (path.length && path[0] === '_global') { host = textGlobalObject(swoof); path.shift(); }
3906
- if (path.length && host) {
3907
- var value = host;
3908
- for (var pi = 0; pi < path.length; pi++) {
3909
- var part = path[pi];
3910
- if (!part || value == null || !Object.prototype.hasOwnProperty.call(value, part)) { value = undefined; break; }
3911
- value = value[part];
3912
- }
3913
- if (typeof value === 'string' || typeof value === 'number') { liveText = value; foundLiveText = true; }
3912
+ var binding = textVariableBinding(item, window.__swoof);
3913
+ if (binding && Object.prototype.hasOwnProperty.call(binding.object, binding.key)) {
3914
+ var bound = binding.object[binding.key];
3915
+ if (typeof bound === 'string' || typeof bound === 'number') liveText = bound;
3914
3916
  }
3915
3917
  // Older SWFs sometimes assign instance.text directly instead of using
3916
3918
  // EditText.variableName; retain that behavior as a fallback.
3917
- if (!foundLiveText && item.name) {
3918
- var inst = (swoof.main && swoof.main[item.name]) || (swoof.root && swoof.root[item.name]);
3919
+ if (liveText == null && item.name) {
3920
+ var inst = (window.__swoof.main && window.__swoof.main[item.name]) || (window.__swoof.root && window.__swoof.root[item.name]);
3919
3921
  if (inst && Object.prototype.hasOwnProperty.call(inst, 'text') &&
3920
3922
  (typeof inst.text === 'string' || typeof inst.text === 'number')) liveText = inst.text;
3921
3923
  }
@@ -4316,19 +4318,22 @@ function timelineEngineBody(bridge) {
4316
4318
  ctx.restore();
4317
4319
  }
4318
4320
 
4321
+ ${fontOutlineScale.toString()}
4322
+ ${defineTextAdvancePixels.toString()}
4323
+
4319
4324
  function drawStaticTextGlyphs(ctx, info, matrix) {
4320
4325
  var runs = info.outlineGlyphRuns || [];
4321
4326
  if (!canPaintOutlines(runs, CHARACTERS)) return false;
4322
4327
  var textMatrix = info.matrix ? mulMatrix(matrix || identityMatrix(), info.matrix) : (matrix || identityMatrix());
4323
4328
  for (var ri = 0; ri < runs.length; ri++) {
4324
4329
  var run = runs[ri], font = CHARACTERS[String(run.fontId)];
4325
- var units = 1024 * (font.version === 3 ? 20 : 1);
4326
- var scale = (run.fontSize || info.fontSize || 12) / units;
4330
+ var scale = fontOutlineScale(font, run.fontSize || info.fontSize || 12);
4327
4331
  var cursor = 0;
4328
4332
  for (var gi = 0; gi < (run.glyphs || []).length; gi++) {
4329
4333
  var entry = run.glyphs[gi], glyph = font.glyphs[entry.glyphIndex];
4334
+ var step = defineTextAdvancePixels(entry.advance);
4330
4335
  if (!glyph || !glyph.path || !glyph.path.length) {
4331
- cursor += (entry.advance || 0) / 20;
4336
+ cursor += step;
4332
4337
  continue;
4333
4338
  }
4334
4339
  var glyphMatrix = mulMatrix(textMatrix, {
@@ -4337,7 +4342,7 @@ function timelineEngineBody(bridge) {
4337
4342
  ty: (run.y || 0) * 20,
4338
4343
  });
4339
4344
  drawShapePaths(ctx, { draws: [{ path: glyph.path, fill: { type: 'solid', color: run.color || info.color || '#000' } }] }, glyphMatrix);
4340
- cursor += (entry.advance || 0) / 20;
4345
+ cursor += step;
4341
4346
  }
4342
4347
  }
4343
4348
  _stubCounts.glyphText = (_stubCounts.glyphText || 0) + 1;
@@ -4360,7 +4365,7 @@ function timelineEngineBody(bridge) {
4360
4365
  for (var gi = 0; gi < run.glyphs.length; gi++) {
4361
4366
  var glyph = run.glyphs[gi];
4362
4367
  if (glyph.text && glyph.text.trim()) ctx.fillText(glyph.text, x, run.y);
4363
- x += (glyph.advance || 0) / 20;
4368
+ x += defineTextAdvancePixels(glyph.advance);
4364
4369
  }
4365
4370
  }
4366
4371
  } finally {
@@ -4518,8 +4523,10 @@ function timelineEngineBody(bridge) {
4518
4523
  };
4519
4524
  }
4520
4525
 
4521
- function makePlainClip(info) {
4526
+ function makePlainClip(info, indexedLeafPlace) {
4522
4527
  var builtins = (typeof __swoof_swflib !== 'undefined' && __swoof_swflib.builtins) || {};
4528
+ if (indexedLeafPlace && info?.type === 'shape' && builtins.Shape) return new builtins.Shape();
4529
+ if (indexedLeafPlace && info?.type === 'bitmap' && builtins.Bitmap) return new builtins.Bitmap();
4523
4530
  if (info && info.type === 'text') {
4524
4531
  if (info.kind === 'edit' && builtins.TextField) {
4525
4532
  var field = new builtins.TextField();
@@ -4651,7 +4658,7 @@ function timelineEngineBody(bridge) {
4651
4658
  } else bitmapData._img = image;
4652
4659
  }
4653
4660
 
4654
- function createInstanceForPlace(entry, parent) {
4661
+ function createInstanceForPlace(entry, parent, indexedLeafPlace) {
4655
4662
  var characterId = entry.characterId, name = entry.name || '';
4656
4663
  var avm1 = typeof ${bridge}createMovieClip === 'function';
4657
4664
  var constructedWithPlacementName = false;
@@ -4702,7 +4709,7 @@ function timelineEngineBody(bridge) {
4702
4709
  }
4703
4710
  }
4704
4711
  var plainPlacement = !clip;
4705
- if (plainPlacement) clip = makePlainClip(info);
4712
+ if (plainPlacement) clip = makePlainClip(info, indexedLeafPlace);
4706
4713
  // Keep the SWF character id and frame count on generic swflib MovieClips
4707
4714
  // too; their placeholder gotoAndStop methods otherwise hide the local
4708
4715
  // sprite playhead and named children on nonzero frames never get bound.
@@ -4720,7 +4727,7 @@ function timelineEngineBody(bridge) {
4720
4727
  clip._swoofTimelinePlayhead = { frame: 0, playing: true, len: clip.totalFrames };
4721
4728
  if (typeof ${bridge}_avm1PrepareAuthoredClass === 'function') ${call('_avm1PrepareAuthoredClass')}(clip, characterId);
4722
4729
  }
4723
- } else if (!clip._swoofHasImage) attachTimelineMethods(clip, characterId);
4730
+ } else if (!clip._swoofHasImage && !indexedLeafPlace) attachTimelineMethods(clip, characterId);
4724
4731
  clip._swoofNamed = true;
4725
4732
  if (name && !constructedWithPlacementName) clip[authoredInstanceNameKey(clip, avm1)] = name;
4726
4733
  clip._swoofCharacterId = characterId;
@@ -4744,9 +4751,111 @@ function timelineEngineBody(bridge) {
4744
4751
  if (info.bold && clip.defaultTextFormat.bold == null) clip.defaultTextFormat.bold = true;
4745
4752
  if (info.italic && clip.defaultTextFormat.italic == null) clip.defaultTextFormat.italic = true;
4746
4753
  }
4754
+ if (indexedLeafPlace) seedIndexedLeafArtwork(clip, info, characterId, entry.ratio);
4747
4755
  return clip;
4748
4756
  }
4749
4757
 
4758
+ function seedIndexedLeafArtwork(clip, info, characterId, ratio) {
4759
+ clip._swoofAuthoredLeaf = true;
4760
+ if (info?.type === 'shape' && clip.graphics) {
4761
+ if (info.morph) {
4762
+ if (clip._swoofSeededRatio === ratio) return;
4763
+ clip._swoofSeededRatio = ratio;
4764
+ } else if (clip._swoofSeededArtwork) return;
4765
+ clip._swoofSeededArtwork = true;
4766
+ var graphics = clip.graphics;
4767
+ graphics.clear();
4768
+ var draws = info.morph ? morphDrawsFor(info, ratio) : (info.draws || []);
4769
+ for (var i = 0; i < draws.length; i++) appendIndexedShapeDraw(graphics, draws[i]);
4770
+ } else if (info?.type === 'bitmap') {
4771
+ var data = indexedBitmapData(info, characterId);
4772
+ if (data) {
4773
+ clip.bitmapData = data;
4774
+ seedPlacedBitmapData(clip.bitmapData, typeof loaded !== 'undefined' ? loaded[info.assetId ?? characterId] : null);
4775
+ }
4776
+ }
4777
+ }
4778
+
4779
+ function authoredColorValue(color) {
4780
+ if (typeof color === 'number' && Number.isFinite(color)) return { color: color >>> 0, alpha: 1 };
4781
+ var match = String(color || '').match(/^#([0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/);
4782
+ if (!match) return { color: 0, alpha: 1 };
4783
+ var hex = match[1];
4784
+ if (hex.length === 8) return { color: parseInt(hex.slice(2), 16), alpha: parseInt(hex.slice(0, 2), 16) / 255 };
4785
+ return { color: parseInt(hex, 16), alpha: 1 };
4786
+ }
4787
+
4788
+ function indexedBitmapData(info, characterId) {
4789
+ var bitmapType = typeof __swoof_swflib !== 'undefined' && __swoof_swflib.builtins?.BitmapData;
4790
+ var image = typeof loaded !== 'undefined' ? loaded[info?.assetId ?? characterId] : null;
4791
+ var width = info?.width || image?.width || 0;
4792
+ var height = info?.height || image?.height || 0;
4793
+ return bitmapType && width > 0 && height > 0 ? new bitmapType(width, height, true, 0) : null;
4794
+ }
4795
+
4796
+ function indexedFillDescriptor(fill) {
4797
+ if (!fill) return null;
4798
+ if (fill.type === 'solid') {
4799
+ var solid = authoredColorValue(fill.color);
4800
+ return { solid: true, color: solid.color, alpha: fill.alpha ?? solid.alpha };
4801
+ }
4802
+ if (fill.type === 'linear' || fill.type === 'radial') {
4803
+ var colors = [], alphas = [], ratios = [];
4804
+ for (var stop of fill.stops || []) {
4805
+ var value = authoredColorValue(stop.color);
4806
+ colors.push(value.color);
4807
+ alphas.push(stop.alpha ?? value.alpha);
4808
+ ratios.push(stop.ratio);
4809
+ }
4810
+ return {
4811
+ gradientType: fill.type, colors: colors, alphas: alphas, ratios: ratios, matrix: fill.matrix || null,
4812
+ spreadMethod: fill.spreadMode === 1 ? 'reflect' : fill.spreadMode === 2 ? 'repeat' : 'pad',
4813
+ interpolationMethod: fill.interpolationMode === 1 ? 'linearRGB' : 'rgb', focalPointRatio: fill.focal || 0,
4814
+ };
4815
+ }
4816
+ if (fill.type === 'bitmap') {
4817
+ var source = CHARACTERS[String(fill.bitmapId)];
4818
+ var data = indexedBitmapData(source, fill.bitmapId);
4819
+ var image = typeof loaded !== 'undefined' ? loaded[source?.assetId ?? fill.bitmapId] : null;
4820
+ if (data) seedPlacedBitmapData(data, image);
4821
+ return {
4822
+ bitmap: data, bitmapId: fill.bitmapId, matrix: fill.matrix || null,
4823
+ repeat: fill.repeat !== false, smooth: !!fill.smoothed,
4824
+ };
4825
+ }
4826
+ return null;
4827
+ }
4828
+
4829
+ function appendIndexedShapeDraw(graphics, draw) {
4830
+ if (!draw?.path) return;
4831
+ var fill = indexedFillDescriptor(draw.fill);
4832
+ var stroke = draw.stroke;
4833
+ var strokeFill = stroke?.fill ? indexedFillDescriptor(stroke.fill) : null;
4834
+ if (draw.fill && !fill) return;
4835
+ if (fill?.gradientType) {
4836
+ graphics.beginGradientFill(fill.gradientType, fill.colors, fill.alphas, fill.ratios, fill.matrix,
4837
+ fill.spreadMethod, fill.interpolationMethod, fill.focalPointRatio);
4838
+ } else if (fill?.bitmap || fill?.bitmapId != null) {
4839
+ if (fill.bitmap) graphics.beginBitmapFill(fill.bitmap, fill.matrix, fill.repeat, fill.smooth);
4840
+ else graphics._cmds.push({ op: 'fill', fill: fill });
4841
+ } else if (fill?.solid) graphics.beginFill(fill.color, fill.alpha);
4842
+ if (stroke) {
4843
+ var strokeColor = strokeFill?.solid ? strokeFill.color : (stroke.color ?? 0);
4844
+ graphics.lineStyle(stroke.width ?? stroke.thickness ?? 1, strokeColor, strokeFill?.solid ? strokeFill.alpha : stroke.alpha);
4845
+ if (strokeFill && !strokeFill.solid) graphics._cmds[graphics._cmds.length - 1].line.fill = strokeFill;
4846
+ }
4847
+ for (var c = 0; c < draw.path.length; c++) {
4848
+ var cmd = draw.path[c];
4849
+ var op = cmd && cmd[0];
4850
+ if (op === 'M') graphics.moveTo(cmd[1], cmd[2]);
4851
+ else if (op === 'L') graphics.lineTo(cmd[1], cmd[2]);
4852
+ else if (op === 'Q') graphics.curveTo(cmd[1], cmd[2], cmd[3], cmd[4]);
4853
+ else if (op === 'C') graphics.cubicCurveTo(cmd[1], cmd[2], cmd[3], cmd[4], cmd[5], cmd[6]);
4854
+ else if (op === 'Z') graphics._cmds.push({ op: 'Z' });
4855
+ }
4856
+ if (fill) graphics.endFill();
4857
+ }
4858
+
4750
4859
  function attachAuthoredChild(parent, child, avm1, indexedOrder, materializingIndexed) {
4751
4860
  // A retained placement does not undo a script's removal from the live list.
4752
4861
  if (authoredPlacementDetached(child, parent)) return;
@@ -4849,19 +4958,12 @@ function timelineEngineBody(bridge) {
4849
4958
  if (clip._swoofHostTimeline) return clip;
4850
4959
  var movieClipClass = typeof __swoof_swflib !== 'undefined' && __swoof_swflib.builtins.MovieClip;
4851
4960
  var info = CHARACTERS[String(id)];
4852
- if (movieClipClass && info?.type === 'sprite' && typeof ${bridge}createMovieClip !== 'function' &&
4853
- !indexedSpriteParents.has(clip)) {
4854
- __swoof_swflib.registerIndexedAuthoredChildren?.(clip, function () {
4855
- indexedSpriteParents.add(clip);
4856
- syncSpriteChildren(clip, id, 0, true);
4857
- });
4858
- }
4859
4961
  if (movieClipClass && clip instanceof movieClipClass && info && info.type === 'sprite') {
4860
4962
  var frames = info.frames || [];
4861
4963
  var head = clip._swoofTimelinePlayhead;
4862
4964
  if (!head) {
4863
4965
  head = clip._swoofTimelinePlayhead = { frame: 0, playing: clip._playing !== false, len: Math.max(1, frames.length) };
4864
- clip.currentLabels = frameLabelsFor(frames, 0, frames.length);
4966
+ clip._swoofCurrentLabels = frameLabelsFor(frames, 0, frames.length);
4865
4967
  }
4866
4968
  function gotoFrame(frame, playing) {
4867
4969
  var target = head.frame;
@@ -4964,7 +5066,7 @@ function timelineEngineBody(bridge) {
4964
5066
  return timeline;
4965
5067
  }
4966
5068
 
4967
- function syncLinkedSpriteFrame(clip, explicitSeek, clock) {
5069
+ function syncLinkedSpriteFrame(clip, explicitSeek, clock, constructOnly) {
4968
5070
  var id = clip._swoofSpriteId;
4969
5071
  var head = clip._swoofTimelinePlayhead;
4970
5072
  if (!head || clip._swoofLastEnteredFrame === head.frame || (clip._swoofScriptDepth | 0) >= 32) return;
@@ -4986,17 +5088,71 @@ function timelineEngineBody(bridge) {
4986
5088
  // A caller can draw a sought frame offstage after its children and scripts are ready.
4987
5089
  syncSpriteChildren(clip, id, 0);
4988
5090
  if (pendingAuthoredSpriteSizing.has(clip)) syncAuthoredSpriteSizing(clip, id);
4989
- if (explicitSeek && !clip.stage) {
4990
- __swoof_swflib.dispatchOffstageFrameConstructed?.(clip);
4991
- if (head.frame !== fi) return;
5091
+ clip._swoofEnteredOnStage = !!onStage;
5092
+ // Explicit seeks enter the frame offstage the same way as staged pulses, including StartSound.
5093
+ clip._swoofFrameSoundReady = !!(onStage || explicitSeek);
5094
+ __swoof_swflib.dispatchOffstageFrameConstructed?.(clip);
5095
+ if (head.frame !== fi) return;
5096
+ // The whole constructed tree hears frameConstructed before any of its scripts.
5097
+ constructPlacedChildren(clip, clock);
5098
+ if (constructOnly) {
5099
+ if (head.frame === fi) clip._swoofPendingScriptFrame = fi;
5100
+ return;
4992
5101
  }
4993
- if (onStage) playFrameSounds(frames[fi]);
5102
+ if (head.frame !== fi) return;
5103
+ if (clip._swoofFrameSoundReady) playFrameSounds(frames[fi]);
4994
5104
  runAs3FrameScripts(clip, fi);
5105
+ runPlacedSpriteFrames(clip);
4995
5106
  } finally {
4996
5107
  clip._swoofScriptDepth--;
4997
5108
  }
4998
5109
  }
4999
5110
 
5111
+ function placementKeyDepth(key) {
5112
+ var n = Number(String(key).slice(String(key).lastIndexOf(':') + 1));
5113
+ return Number.isFinite(n) ? n : 0;
5114
+ }
5115
+
5116
+ function constructPlacedChildren(parent, clock) {
5117
+ if (!parent || typeof ${bridge}createMovieClip === 'function') return;
5118
+ var placed = parent._swoofSpriteId != null ? parent._swoofSpriteChildren : parent._swoofRootChildren;
5119
+ if (!placed) return;
5120
+ var keys = Object.keys(placed).sort(function (a, b) { return placementKeyDepth(a) - placementKeyDepth(b); });
5121
+ for (var i = 0; i < keys.length; i++) {
5122
+ var clip = placed[keys[i]];
5123
+ if (!clip || clip.parent !== parent || !clip._swoofLocalTimeline) continue;
5124
+ syncLinkedSpriteFrame(clip, !!parent._swoofFrameSoundReady, clock, true);
5125
+ }
5126
+ }
5127
+
5128
+ function runPlacedSpriteFrames(parent) {
5129
+ if (!parent || typeof ${bridge}createMovieClip === 'function') return;
5130
+ var placed = parent._swoofSpriteId != null ? parent._swoofSpriteChildren : parent._swoofRootChildren;
5131
+ if (!placed) return;
5132
+ var keys = Object.keys(placed).sort(function (a, b) { return placementKeyDepth(a) - placementKeyDepth(b); });
5133
+ for (var i = 0; i < keys.length; i++) {
5134
+ var clip = placed[keys[i]];
5135
+ if (!clip || clip.parent !== parent) continue;
5136
+ var pending = clip._swoofPendingScriptFrame;
5137
+ if (pending != null) {
5138
+ clip._swoofPendingScriptFrame = null;
5139
+ var pendingHead = clip._swoofTimelinePlayhead;
5140
+ if (pendingHead && pendingHead.frame === pending) {
5141
+ var pendingFrames = CHARACTERS[String(clip._swoofSpriteId)].frames;
5142
+ if (clip._swoofFrameSoundReady) playFrameSounds(pendingFrames[pending]);
5143
+ try { runAs3FrameScripts(clip, pending); } catch (_) {}
5144
+ }
5145
+ runPlacedSpriteFrames(clip);
5146
+ continue;
5147
+ }
5148
+ if (clip._swoofLocalTimeline) syncLinkedSpriteFrame(clip);
5149
+ else if (as3DocumentReady() && clip.stage != null && !clip._swoofFrame0ScriptRun) {
5150
+ clip._swoofFrame0ScriptRun = true;
5151
+ try { runAs3FrameScripts(clip, 0); } catch (_) {}
5152
+ }
5153
+ }
5154
+ }
5155
+
5000
5156
  var spritePlacementInstances = new WeakMap();
5001
5157
  var indexedSpriteParents = new WeakSet();
5002
5158
  var indexedRootParents = new WeakSet();
@@ -5047,7 +5203,8 @@ function timelineEngineBody(bridge) {
5047
5203
  indexedInteractivePlace(p, childInfo) ||
5048
5204
  (avm1 && (childInfo?.type === 'sprite' || childInfo?.type === 'text' || childInfo?.type === 'button')) ||
5049
5205
  (linked && (p.characterId | 0) !== 0) || (!avm1 && spriteNeedsPlacementInstance(childInfo));
5050
- var indexedOnly = !eager && childInfo?.type === 'sprite' && indexedSpriteParents.has(parentClip);
5206
+ var indexedOnly = !eager && indexedSpriteParents.has(parentClip) &&
5207
+ (childInfo?.type === 'sprite' || indexedLeaf(childInfo));
5051
5208
  if (eager || indexedOnly) {
5052
5209
  children[key] = { placement: p, info: childInfo, nest: nest, indexedOnly: indexedOnly };
5053
5210
  }
@@ -5085,51 +5242,60 @@ function timelineEngineBody(bridge) {
5085
5242
  var placements = collectSpriteChildren(parentClip, characterId, nest, children);
5086
5243
  var placed = parentClip._swoofSpriteChildren || (parentClip._swoofSpriteChildren = Object.create(null));
5087
5244
  var retainedAliases = null;
5088
- if (!materializingIndexed) for (var key in placed) {
5089
- var oldClip = placed[key], current = children[key] && children[key].placement;
5090
- if (matchesAuthoredPlacement(oldClip, current)) continue;
5091
- delete placed[key];
5092
- oldClip._swoofTimelineBitmapEffects = false;
5093
- if (avm1 && typeof ${bridge}_avm1RetainSwappedClip === 'function' &&
5094
- ${call('_avm1RetainSwappedClip')}(oldClip, oldClip._swoofLastAuthoredPlacement)) continue;
5095
- if (oldClip.parent === parentClip) {
5096
- if (avm1 && typeof ${bridge}_removeMovieClipImpl === 'function') ${call('_removeMovieClipImpl')}(oldClip);
5097
- else if (typeof parentClip.removeChild === 'function') parentClip.removeChild(oldClip);
5098
- else oldClip.parent = null;
5099
- }
5100
- var oldName = authoredPlacementName(oldClip, avm1);
5101
- if (oldName && Object.prototype.hasOwnProperty.call(parentClip, oldName) && parentClip[oldName] === oldClip) {
5102
- if (avm1) delete parentClip[oldName];
5103
- else {
5104
- if (!retainedAliases) {
5105
- retainedAliases = new Map();
5106
- for (var remainingKey in children) {
5107
- var remaining = children[remainingKey].placement;
5108
- var retained = placed[remainingKey];
5109
- if (remaining.name && retained?.parent === parentClip && matchesAuthoredPlacement(retained, remaining)) {
5110
- retainedAliases.set(remaining.name, retained);
5245
+ var indexedOrder = indexedSpriteParents.has(parentClip) &&
5246
+ Object.values(children).some(function (item) { return item.indexedOnly; })
5247
+ ? indexedSpritePaintOrder(parentClip, characterId, placements) : null;
5248
+ var depthKeys = Object.keys(placed);
5249
+ for (var childKey in children) if (depthKeys.indexOf(childKey) < 0) depthKeys.push(childKey);
5250
+ depthKeys.sort(function (a, b) { return placementKeyDepth(a) - placementKeyDepth(b); });
5251
+ for (var ki = 0; ki < depthKeys.length; ki++) {
5252
+ var key = depthKeys[ki];
5253
+ if (!materializingIndexed) {
5254
+ var oldClip = placed[key], current = children[key] && children[key].placement;
5255
+ if (oldClip && !matchesAuthoredPlacement(oldClip, current)) {
5256
+ delete placed[key];
5257
+ oldClip._swoofTimelineBitmapEffects = false;
5258
+ if (!(avm1 && typeof ${bridge}_avm1RetainSwappedClip === 'function' &&
5259
+ ${call('_avm1RetainSwappedClip')}(oldClip, oldClip._swoofLastAuthoredPlacement))) {
5260
+ if (oldClip.parent === parentClip) {
5261
+ if (avm1 && typeof ${bridge}_removeMovieClipImpl === 'function') ${call('_removeMovieClipImpl')}(oldClip);
5262
+ else if (typeof parentClip.removeChild === 'function') parentClip.removeChild(oldClip);
5263
+ else oldClip.parent = null;
5264
+ }
5265
+ var oldName = authoredPlacementName(oldClip, avm1);
5266
+ if (oldName && Object.prototype.hasOwnProperty.call(parentClip, oldName) && parentClip[oldName] === oldClip) {
5267
+ if (avm1) delete parentClip[oldName];
5268
+ else {
5269
+ if (!retainedAliases) {
5270
+ retainedAliases = new Map();
5271
+ for (var remainingKey in children) {
5272
+ var remaining = children[remainingKey].placement;
5273
+ var retained = placed[remainingKey];
5274
+ if (remaining.name && retained?.parent === parentClip && matchesAuthoredPlacement(retained, remaining)) {
5275
+ retainedAliases.set(remaining.name, retained);
5276
+ }
5277
+ }
5278
+ }
5279
+ var sibling = retainedAliases.get(oldName);
5280
+ parentClip[oldName] = sibling?.parent === parentClip ? sibling : null;
5111
5281
  }
5112
5282
  }
5113
5283
  }
5114
- var sibling = retainedAliases.get(oldName);
5115
- parentClip[oldName] = sibling?.parent === parentClip ? sibling : null;
5116
5284
  }
5117
5285
  }
5118
- }
5119
- var indexedOrder = indexedSpriteParents.has(parentClip) &&
5120
- Object.values(children).some(function (item) { return item.indexedOnly; })
5121
- ? indexedSpritePaintOrder(parentClip, characterId, placements) : null;
5122
- for (var key in children) {
5123
- var item = children[key], p = item.placement;
5286
+ var item = children[key];
5287
+ if (!item) continue;
5288
+ var p = item.placement;
5124
5289
  if (materializingIndexed && !item.indexedOnly) continue;
5125
5290
  var clip = placed[key];
5126
5291
  if (!clip) {
5127
- clip = createInstanceForPlace(p, parentClip);
5292
+ clip = createInstanceForPlace(p, parentClip, item.indexedOnly && indexedLeaf(item.info));
5128
5293
  clip._swoofPlacementBirthFrame = p.birthFrame;
5129
5294
  placed[key] = clip;
5130
5295
  if (p.name) parentClip[p.name] = clip;
5131
5296
  }
5132
5297
  if (avm1 && clip._swoofDetached) continue;
5298
+ if (!avm1 && authoredPlacementDetached(clip, parentClip)) continue;
5133
5299
  syncPlacementName(parentClip, clip, p, avm1);
5134
5300
  if (avm1) clip._swoofLastAuthoredPlacement = p;
5135
5301
  if (avm1) syncAvm1PlaceMatrix(clip, p.matrix);
@@ -5142,20 +5308,21 @@ function timelineEngineBody(bridge) {
5142
5308
  syncPlacementTabIndex(clip, p, item.info, avm1);
5143
5309
  clip._swoofClipDepth = p.clipDepth;
5144
5310
  clip._swoofMaskRatio = p.ratio;
5311
+ if (clip._swoofAuthoredLeaf && item.info?.morph) seedIndexedLeafArtwork(clip, item.info, p.characterId, p.ratio);
5145
5312
  clip._swoofDepth = p.depth;
5146
5313
  attachAuthoredChild(parentClip, clip, avm1, item.indexedOnly ? indexedOrder : null, materializingIndexed);
5147
- if (materializingIndexed) continue;
5148
5314
  if (item.info && item.info.type === 'sprite') {
5149
5315
  syncSpriteChildren(clip, p.characterId, item.nest + 1);
5150
5316
  }
5151
- // Children must exist before a SymbolClass frame script refers to them.
5152
- // (For example Menu.frame1 navigates menfr then applies gunCont.mask.)
5153
- if (avm1) continue;
5154
- if (clip._swoofLocalTimeline) syncLinkedSpriteFrame(clip);
5155
- else if (as3DocumentReady() && clip.stage != null && !clip._swoofFrame0ScriptRun) {
5156
- clip._swoofFrame0ScriptRun = true;
5157
- try { runAs3FrameScripts(clip, 0); } catch (_) {}
5158
- }
5317
+ if (materializingIndexed) continue;
5318
+ // The child is on the list before this clip's frame script. Its own
5319
+ // frame script waits until that script returns.
5320
+ }
5321
+ if (!materializingIndexed && !avm1 && !indexedSpriteParents.has(parentClip)) {
5322
+ __swoof_swflib.registerIndexedAuthoredChildren?.(parentClip, function () {
5323
+ indexedSpriteParents.add(parentClip);
5324
+ syncSpriteChildren(parentClip, characterId, 0, true);
5325
+ });
5159
5326
  }
5160
5327
  }
5161
5328
 
@@ -5170,14 +5337,12 @@ function timelineEngineBody(bridge) {
5170
5337
  if (host.root == null) host.root = host;
5171
5338
  var avm1 = typeof ${bridge}createMovieClip === 'function';
5172
5339
  var placed = host._swoofRootChildren;
5340
+ var armIndexedChildren = false;
5173
5341
  if (!placed) {
5174
5342
  placed = host._swoofRootChildren = Object.create(null);
5175
- if (!avm1 && typeof __swoof_swflib !== 'undefined') {
5176
- __swoof_swflib.registerIndexedAuthoredChildren?.(host, function () {
5177
- indexedRootParents.add(host);
5178
- syncNamedInstances(host, true);
5179
- });
5180
- }
5343
+ // Arm after the eager placements. addChildAt during this pass would
5344
+ // otherwise pull the unnamed sprites in before they are lazy.
5345
+ armIndexedChildren = !avm1 && typeof __swoof_swflib !== 'undefined';
5181
5346
  }
5182
5347
  var retainedAliases = null;
5183
5348
  if (!materializingIndexed) for (var oldDepth in placed) {
@@ -5220,14 +5385,13 @@ function timelineEngineBody(bridge) {
5220
5385
  var rootDepth = depths[i];
5221
5386
  var e = displayList[rootDepth];
5222
5387
  if (!e || e.characterId == null || e._avm1Dynamic) continue;
5223
- if (!avm1 && e.clipDepth != null) continue;
5224
5388
  var linked = symbolClassNameFor(e.characterId);
5225
5389
  var info = CHARACTERS[String(e.characterId)];
5226
5390
  var eager = e.name || placed[rootDepth] || indexedInteractivePlace(e, info) ||
5227
5391
  (avm1 && (info?.type === 'sprite' || info?.type === 'text' || info?.type === 'button')) ||
5228
5392
  (linked && (e.characterId | 0) !== 0);
5229
- var indexedOnly = !eager && indexedRootParents.has(host) && info?.type === 'sprite' &&
5230
- !spriteNeedsPlacementInstance(info);
5393
+ var indexedOnly = !eager && indexedRootParents.has(host) &&
5394
+ (indexedLeaf(info) || info?.type === 'sprite' && !spriteNeedsPlacementInstance(info));
5231
5395
  if (!indexedOnly && (materializingIndexed || !eager)) continue;
5232
5396
  if (indexedOnly && !indexedOrder) {
5233
5397
  indexedOrder = indexedAuthoredPaintOrder(host, depths, function (depth) { return placed[depth]; });
@@ -5241,10 +5405,14 @@ function timelineEngineBody(bridge) {
5241
5405
  e._clip = clip;
5242
5406
  continue;
5243
5407
  }
5408
+ if (!avm1 && authoredPlacementDetached(clip, host)) {
5409
+ e._clip = clip;
5410
+ continue;
5411
+ }
5244
5412
  var authoredTextField = info && info.type === 'text' && clip &&
5245
5413
  typeof clip.setSelection === 'function' && typeof clip.selectionBeginIndex === 'number';
5246
5414
  if (!clip || (!clip._swoofNamed && !authoredTextField)) {
5247
- clip = createInstanceForPlace(e, host);
5415
+ clip = createInstanceForPlace(e, host, indexedOnly && indexedLeaf(info));
5248
5416
  if (e.name) host[e.name] = clip;
5249
5417
  } else if (!authoredTextField && !clip._swoofHasImage &&
5250
5418
  (typeof clip.gotoAndStop !== 'function' || typeof clip.play !== 'function')) {
@@ -5265,6 +5433,7 @@ function timelineEngineBody(bridge) {
5265
5433
  syncPlacementTabIndex(clip, e, info, avm1);
5266
5434
  clip._swoofClipDepth = e.clipDepth;
5267
5435
  clip._swoofMaskRatio = e.ratio;
5436
+ if (clip._swoofAuthoredLeaf && info?.morph) seedIndexedLeafArtwork(clip, info, e.characterId, e.ratio);
5268
5437
  clip._swoofCharacterId = e.characterId;
5269
5438
  clip._swoofDisplayDepth = depths[i];
5270
5439
  placed[rootDepth] = clip;
@@ -5274,12 +5443,14 @@ function timelineEngineBody(bridge) {
5274
5443
  if (info && info.type === 'sprite') {
5275
5444
  syncSpriteChildren(clip, e.characterId, 0);
5276
5445
  }
5277
- if (avm1) continue;
5278
- if (clip._swoofLocalTimeline) syncLinkedSpriteFrame(clip);
5279
- else if (!clip._swoofFrame0ScriptRun && as3DocumentReady()) {
5280
- clip._swoofFrame0ScriptRun = true;
5281
- try { runAs3FrameScripts(clip, 0); } catch (_) {}
5282
- }
5446
+ // The placement is addressable before the root frame script. Its frame
5447
+ // script runs from runPlacedSpriteFrames after that script returns.
5448
+ }
5449
+ if (armIndexedChildren) {
5450
+ __swoof_swflib.registerIndexedAuthoredChildren?.(host, function () {
5451
+ indexedRootParents.add(host);
5452
+ syncNamedInstances(host, true);
5453
+ });
5283
5454
  }
5284
5455
  }
5285
5456
 
@@ -5605,6 +5776,19 @@ function timelineEngineBody(bridge) {
5605
5776
  var maskPlacements = rootReplacement ? null : clipMaskPlacements(displayList);
5606
5777
  var as3Main = typeof ${bridge}createMovieClip !== 'function' &&
5607
5778
  typeof window !== 'undefined' && window.__swoof?.main;
5779
+ var stageNode = typeof __swoof_swflib !== 'undefined' && __swoof_swflib.stage;
5780
+ var stageKids = stageNode && stageNode._children;
5781
+ var documentIndex = as3Main && stageKids ? stageKids.indexOf(as3Main) : -1;
5782
+ function paintStageSlice(from, to) {
5783
+ if (!stageKids || typeof __swoof_swflib === 'undefined' || !__swoof_swflib.renderDisplayTree) return;
5784
+ var renderStage = __swoof_swflib.renderTimelineStage || __swoof_swflib.renderDisplayTree;
5785
+ for (var stageIndex = from; stageIndex < to && stageIndex < stageKids.length; stageIndex++) {
5786
+ draw.setTransform(1, 0, 0, 1, 0, 0);
5787
+ renderStage(draw, stageKids[stageIndex], 0, false, null, false, false, false, collectStageTextOverlay,
5788
+ undefined, undefined, collectStageTextCover);
5789
+ }
5790
+ }
5791
+ if (documentIndex > 0) paintStageSlice(0, documentIndex);
5608
5792
  if (as3Main) entries = mixedChildOrder(entries, as3Main, function (item) { return item.entry?._clip; });
5609
5793
  var inlineEntries = inlineAvm1Children && new Set();
5610
5794
  for (var item of inlineEntries ? entries : []) {
@@ -5665,9 +5849,11 @@ function timelineEngineBody(bridge) {
5665
5849
  drawCharacter(draw, e.characterId, placedMatrix, 0, e.name || '', rootEffects, instancePath, e._clip);
5666
5850
  draw.globalAlpha = 1;
5667
5851
  }
5668
- // AS3 stage tree (Plazma etc.) — drawn on top of PlaceObject list
5852
+ // Stage children behind the document paint first. The document and anything
5853
+ // added above it paint after the authored timeline.
5669
5854
  try {
5670
- if (typeof __swoof_swflib !== 'undefined' && __swoof_swflib.stage && __swoof_swflib.renderDisplayTree) {
5855
+ if (documentIndex > 0) paintStageSlice(documentIndex, stageKids.length);
5856
+ else if (typeof __swoof_swflib !== 'undefined' && __swoof_swflib.stage && __swoof_swflib.renderDisplayTree) {
5671
5857
  draw.setTransform(1, 0, 0, 1, 0, 0);
5672
5858
  var renderStage = __swoof_swflib.renderTimelineStage || __swoof_swflib.renderDisplayTree;
5673
5859
  renderStage(draw, __swoof_swflib.stage, 0, false, null, false, false, false, collectStageTextOverlay,
@@ -6004,8 +6190,8 @@ function timelineEngineBody(bridge) {
6004
6190
  var _spriteScriptDepth = 0;
6005
6191
  var _spriteScriptRunning = new WeakMap();
6006
6192
 
6007
- function runSpriteFrameScripts(spriteId, fi, instance) {
6008
- if (instance) {
6193
+ function runSpriteFrameScripts(spriteId, fi, instance, mode) {
6194
+ if (instance && mode !== 'scripts') {
6009
6195
  if (!timeline._entered) return;
6010
6196
  var head = instance._swoofTimelinePlayhead || instance._swoofClonePlayhead;
6011
6197
  if (!head.booted) head.startedTick = timeline._ticks;
@@ -6013,6 +6199,8 @@ function timelineEngineBody(bridge) {
6013
6199
  instance.currentFrame = fi + 1;
6014
6200
  syncSpriteChildren(instance, spriteId, 0);
6015
6201
  }
6202
+ if (mode === 'playhead') return;
6203
+ if (instance && !timeline._entered) return;
6016
6204
  var byFrame = SPRITE_FRAME_SCRIPTS[spriteId];
6017
6205
  if (!byFrame) return;
6018
6206
  var list = byFrame[fi];
@@ -6046,24 +6234,74 @@ function timelineEngineBody(bridge) {
6046
6234
  timeline.gotoAndStop(sceneNavigationFrame(timeline.frame, delta, SCENES) + 1);
6047
6235
  }
6048
6236
 
6237
+ var _publishedScenes = null;
6238
+ function publishedScenes() {
6239
+ if (_publishedScenes) return _publishedScenes;
6240
+ var Scene = typeof __swoof_swflib !== 'undefined' && __swoof_swflib.builtins && __swoof_swflib.builtins.Scene;
6241
+ _publishedScenes = SCENES.map(function (scene) {
6242
+ var labels = frameLabelsFor(FRAMES, scene.offset | 0, scene.numFrames | 0);
6243
+ return Scene ? new Scene(scene.name, labels, scene.numFrames, scene.offset) :
6244
+ { name: scene.name, labels: labels, numFrames: scene.numFrames, offset: scene.offset };
6245
+ });
6246
+ return _publishedScenes;
6247
+ }
6248
+
6249
+ function labelOnFrame(frame) {
6250
+ var start = 0;
6251
+ for (var s = 0; s < SCENES.length; s++) {
6252
+ if (frame >= SCENES[s].offset && frame < SCENES[s].offset + SCENES[s].numFrames) { start = SCENES[s].offset | 0; break; }
6253
+ }
6254
+ var name = null;
6255
+ for (var i = start; i <= frame && i < FRAMES.length; i++) {
6256
+ if (FRAMES[i] && FRAMES[i].label != null) name = FRAMES[i].label;
6257
+ }
6258
+ return name;
6259
+ }
6260
+
6261
+ function timelineSceneLocked(clip) {
6262
+ for (var proto = Object.getPrototypeOf(clip); proto; proto = Object.getPrototypeOf(proto)) {
6263
+ var desc = Object.getOwnPropertyDescriptor(proto, 'scenes');
6264
+ if (desc && (desc.get || desc.set)) return true;
6265
+ }
6266
+ return false;
6267
+ }
6268
+
6269
+ function publishSceneState(clip, scenes, active, labels, frameLabel) {
6270
+ if (!clip) return;
6271
+ clip._swoofScenes = scenes;
6272
+ clip._swoofCurrentScene = active;
6273
+ clip._swoofCurrentLabels = labels;
6274
+ clip.currentLabel = frameLabel;
6275
+ // MovieClip.scenes is read-only. A document subclass inherits that setter
6276
+ // from MovieClip, so the player updates the backing fields only. A plain
6277
+ // AVM1 root has no setter and still receives the public properties.
6278
+ if (!timelineSceneLocked(clip)) {
6279
+ clip.scenes = scenes;
6280
+ clip.currentScene = active;
6281
+ clip.currentLabels = labels;
6282
+ }
6283
+ }
6284
+
6049
6285
  function syncRootFrame() {
6050
- var activeScene = SCENES.find(function (scene) { return timeline.frame >= scene.offset && timeline.frame < scene.offset + scene.numFrames; }) || SCENES[0];
6051
- var labels = frameLabelsFor(FRAMES, activeScene.offset, activeScene.numFrames);
6286
+ var scenes = publishedScenes();
6287
+ var index = 0;
6288
+ for (var i = 0; i < SCENES.length; i++) {
6289
+ if (timeline.frame >= SCENES[i].offset && timeline.frame < SCENES[i].offset + SCENES[i].numFrames) { index = i; break; }
6290
+ }
6291
+ var activeScene = scenes[index] || scenes[0];
6292
+ var labels = activeScene.labels || [];
6293
+ var frameLabel = labelOnFrame(timeline.frame | 0);
6052
6294
  if (typeof ${bridge}root !== 'undefined' && ${bridge}root) {
6053
- ${bridge}root.currentFrame = (timeline.frame | 0) - activeScene.offset + 1;
6295
+ ${bridge}root.currentFrame = (timeline.frame | 0) - (activeScene.offset | 0) + 1;
6054
6296
  ${bridge}root.totalFrames = activeScene.numFrames || FRAME_COUNT;
6055
- ${bridge}root.scenes = SCENES;
6056
- ${bridge}root.currentScene = activeScene;
6057
- ${bridge}root.currentLabels = labels;
6297
+ publishSceneState(${bridge}root, scenes, activeScene, labels, frameLabel);
6058
6298
  }
6059
6299
  var main = (typeof window !== 'undefined' && window.__swoof && window.__swoof.main) || null;
6060
6300
  if (main) {
6061
6301
  if (main._swoofHostTimeline) main._swoofTimeline = timeline;
6062
- main.currentFrame = (timeline.frame | 0) - activeScene.offset + 1;
6302
+ main.currentFrame = (timeline.frame | 0) - (activeScene.offset | 0) + 1;
6063
6303
  main.totalFrames = activeScene.numFrames || FRAME_COUNT;
6064
- main.scenes = SCENES;
6065
- main.currentScene = activeScene;
6066
- main.currentLabels = labels;
6304
+ publishSceneState(main, scenes, activeScene, labels, frameLabel);
6067
6305
  if (main.framesLoaded < (activeScene.numFrames || FRAME_COUNT)) main.framesLoaded = activeScene.numFrames || FRAME_COUNT;
6068
6306
  }
6069
6307
  }
@@ -6266,7 +6504,9 @@ function timelineEngineBody(bridge) {
6266
6504
  if (typeof console !== 'undefined') console.warn('swoof: frame scripts', err && err.message ? err.message : err);
6267
6505
  }
6268
6506
  if (main) {
6507
+ constructPlacedChildren(main, timeline);
6269
6508
  try { runAs3FrameScripts(main, this.frame | 0); } catch (_) {}
6509
+ runPlacedSpriteFrames(main);
6270
6510
  }
6271
6511
  },
6272
6512
  /** Boot/land helper: scripts then paint. */
@@ -6285,6 +6525,9 @@ function timelineEngineBody(bridge) {
6285
6525
  },
6286
6526
  tick: function (skipPaint) {
6287
6527
  this._ticks = (this._ticks | 0) + 1;
6528
+ if (typeof __swoof_swflib !== 'undefined' && __swoof_swflib.beginFrameConstruction) {
6529
+ __swoof_swflib.beginFrameConstruction();
6530
+ }
6288
6531
  // 1) Advance main scene timeline only while playing
6289
6532
  if (this.playing) {
6290
6533
  if (this.frame < FRAME_COUNT - 1) landRootFrame(this.frame + 1, null, false);