swf2js 0.1.0 → 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 +3 -2
  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
@@ -0,0 +1,823 @@
1
+ import { Event, EventDispatcher } from './events.js';
2
+ import { Matrix, Point, Rectangle, Vector3D } from './geom.js';
3
+ import { displayMatrix3DInfo, maskAncestorMatrix, matrix3DNearPlane, matrix3DProjectPoint, matrix3DProjectedBounds, matrix3DProjectionDenominator, matrix3DUnprojectInParent, matrix3DUnprojectPoint } from './matrix3d.js';
4
+ import { displayObjectMark, isSprite, isTextLine } from './marks.js';
5
+ import { pointerBridge } from './pointer-bridge.js';
6
+ import { Transform } from './transform.js';
7
+
8
+ // ---- display ----
9
+ export var displayMasks = new WeakMap();
10
+ export var displayMaskOwners = new WeakMap();
11
+ export var detachedAuthoredMaskBases = new WeakMap();
12
+ export var authoredMaskSources = new WeakMap();
13
+ export var authoredSpriteSizing = new WeakSet();
14
+
15
+ export function isStageDescendant(stage, target) {
16
+ var node = target;
17
+ while (node) {
18
+ if (node === stage) return true;
19
+ node = node.parent || null;
20
+ }
21
+ return false;
22
+ }
23
+
24
+ export class DisplayObject extends EventDispatcher {
25
+ constructor() {
26
+ super();
27
+ initializeDisplayGeometry(this);
28
+ this.z = 0;
29
+ this.scaleZ = 1;
30
+ this.rotation = 0; this.rotationX = 0; this.rotationY = 0; this.rotationZ = 0;
31
+ this.name = '';
32
+ this.parent = null; this._stageOverride = null; this.filters = null;
33
+ this._cacheAsBitmap = false;
34
+ this.cacheAsBitmapMatrix = null;
35
+ this.opaqueBackground = null;
36
+ this._rootOverride = null;
37
+ this._colorTransform = null;
38
+ this.transform = new Transform(this);
39
+ this._loaderInfo = null;
40
+ }
41
+
42
+ getBounds(target) {
43
+ if (this._scrollRect || target) return displayBoundsInTarget(this, target, true);
44
+ var projected = matrix3DProjectedBounds(this);
45
+ if (projected) return projected;
46
+ return new Rectangle(this.x, this.y, this.width || 0, this.height || 0);
47
+ }
48
+
49
+ getRect(target) {
50
+ if (this._scrollRect || target) return displayBoundsInTarget(this, target, false);
51
+ return this.getBounds();
52
+ }
53
+
54
+ hitTestObject(obj) {
55
+ if (!obj || this.visible === false || obj.visible === false) return false;
56
+ var a = displayTreePixelBounds(this), b = displayTreePixelBounds(obj);
57
+ return !!(a && b && a.intersects(b));
58
+ }
59
+
60
+ hitTestPoint(x, y, shapeFlag) {
61
+ if (this.visible === false || !pointPassesScrollRects(this, x, y)) return false;
62
+ var authored = authoredMaskSources.get(this);
63
+ var authoredButtonHit = authored?.hitButton?.(this, x, y, shapeFlag);
64
+ if (authoredButtonHit !== undefined) return authoredButtonHit;
65
+ var hasProjectedPlane = !!displayMatrix3DInfo(this);
66
+ var projectedPoint = matrix3DUnprojectPoint(this, x, y);
67
+ if (hasProjectedPlane) {
68
+ if (!projectedPoint) return false;
69
+ var projectedBounds = localDisplayBounds(this);
70
+ var pbx = projectedBounds.x, pby = projectedBounds.y;
71
+ if (!(projectedPoint.x >= pbx && projectedPoint.y >= pby && projectedPoint.x < pbx + projectedBounds.width && projectedPoint.y < pby + projectedBounds.height)) return false;
72
+ if (shapeFlag && this.graphics && this.graphics.containsPoint) return this.graphics.containsPoint(projectedPoint.x, projectedPoint.y);
73
+ if (shapeFlag && this.bitmapData && this.bitmapData.getPixel32 && this.bitmapData._ctx) {
74
+ return (this.bitmapData.getPixel32(Math.floor(projectedPoint.x), Math.floor(projectedPoint.y)) >>> 24) !== 0;
75
+ }
76
+ return true;
77
+ }
78
+ if (shapeFlag && authored?.hitArtwork?.(this, x, y)) return true;
79
+ var p = this.globalToLocal(new Point(x, y));
80
+ if (shapeFlag && this.graphics && this.graphics.containsPoint && typeof document !== 'undefined' &&
81
+ typeof Path2D !== 'undefined' && typeof Path2D.prototype?.moveTo === 'function') {
82
+ var graphicsMatrix = new Matrix();
83
+ for (var node = this; node; node = node.parent) {
84
+ if (displayMatrix3DInfo(node)) { graphicsMatrix = null; break; }
85
+ if (node._scrollRect) {
86
+ graphicsMatrix.translate(-Math.trunc(node._scrollRect.x), -Math.trunc(node._scrollRect.y));
87
+ }
88
+ graphicsMatrix.concat(displayObjectMatrix(node));
89
+ }
90
+ if (graphicsMatrix) return this.graphics.containsPoint(p.x, p.y, graphicsMatrix);
91
+ }
92
+ var bounds = authoredSpriteSizing.has(this) ? displayBoundsInTarget(this, this, true) : localDisplayBounds(this);
93
+ if (!(p.x >= bounds.x && p.y >= bounds.y && p.x < bounds.right && p.y < bounds.bottom)) {
94
+ if (shapeFlag || !isSprite(this)) return false;
95
+ // Unnamed timeline artwork may have no native child or Graphics commands.
96
+ var authoredBounds = authored?.bounds?.(this);
97
+ return !!authoredBounds && p.x >= authoredBounds.x && p.y >= authoredBounds.y &&
98
+ p.x < authoredBounds.x + authoredBounds.w && p.y < authoredBounds.y + authoredBounds.h;
99
+ }
100
+ if (shapeFlag && this.graphics && this.graphics.containsPoint && typeof document !== 'undefined' &&
101
+ typeof Path2D !== 'undefined' && typeof Path2D.prototype?.moveTo === 'function') {
102
+ return this.graphics.containsPoint(p.x, p.y);
103
+ }
104
+ if (shapeFlag && this.bitmapData && this.bitmapData.getPixel32 && this.bitmapData._ctx) {
105
+ return (this.bitmapData.getPixel32(Math.floor(p.x), Math.floor(p.y)) >>> 24) !== 0;
106
+ }
107
+ return true;
108
+ }
109
+
110
+ localToGlobal(p) {
111
+ return localPointToStage(this, p ? new Point(p.x, p.y) : new Point(), 0);
112
+ }
113
+
114
+ local3DToGlobal(point) {
115
+ if (point == null) throw new TypeError('Error #2007: Parameter point3d must be non-null.');
116
+ return localPointToStage(this, new Point(point.x, point.y), Number(point.z) || 0);
117
+ }
118
+
119
+ globalToLocal(p) {
120
+ p = p ? new Point(p.x, p.y) : new Point();
121
+ var chain = [];
122
+ var node = this;
123
+ var avm1LocalMatrix = node._swoofAvm1LocalMatrix;
124
+ while (node) { chain.push(node); node = node.parent; }
125
+ for (var i = chain.length - 1; i >= 0; i--) {
126
+ node = chain[i];
127
+ var info3d = displayMatrix3DInfo(node);
128
+ if (info3d) {
129
+ var local = matrix3DUnprojectInParent(info3d, p.x, p.y, true);
130
+ if (!local) return invalidPoint();
131
+ p = local;
132
+ } else {
133
+ var m = avm1LocalMatrix?.(node) || displayObjectMatrix(node);
134
+ var det = m.a * m.d - m.b * m.c;
135
+ var x = p.x - m.tx, y = p.y - m.ty;
136
+ if (Math.abs(det) < 1e-12) return invalidPoint();
137
+ p.x = (m.d * x - m.c * y) / det;
138
+ p.y = (-m.b * x + m.a * y) / det;
139
+ }
140
+ var rect = node._scrollRect;
141
+ if (rect) {
142
+ p.x += Math.trunc(rect.x);
143
+ p.y += Math.trunc(rect.y);
144
+ }
145
+ }
146
+ return p;
147
+ }
148
+
149
+ globalToLocal3D(point) {
150
+ if (point == null) throw new TypeError('Error #2007: Parameter point must be non-null.');
151
+ var local = this.globalToLocal(point), result = new Vector3D();
152
+ result.x = local.x;
153
+ result.y = local.y;
154
+ return result;
155
+ }
156
+ }
157
+ DisplayObject.prototype[displayObjectMark] = true;
158
+ export var displayGeometryDefaults = { x: 0, y: 0, scaleX: 1, scaleY: 1, width: 0, height: 0, visible: true };
159
+ export var displayGeometrySlots = Object.create(null);
160
+ export var displayGeometryDescriptors = new WeakMap();
161
+ export function displayScaleProperty(object, name) {
162
+ var slot = displayGeometrySlots[name];
163
+ return Object.hasOwn(object, slot) ? slot : name;
164
+ }
165
+ export function readDisplayScale(object, name) {
166
+ var value = object[displayScaleProperty(object, name)];
167
+ return value == null ? 1 : +value;
168
+ }
169
+ for (let [name, initial] of Object.entries(displayGeometryDefaults)) {
170
+ let slot = displayGeometrySlots[name] = Symbol(name);
171
+ Object.defineProperty(DisplayObject.prototype, name, {
172
+ enumerable: true, configurable: true,
173
+ get: function () {
174
+ if (Object.hasOwn(this, name)) return this[name];
175
+ return Object.hasOwn(this, slot) ? this[slot] : initial;
176
+ },
177
+ set: function (value) {
178
+ if (Object.hasOwn(this, name)) this[name] = value;
179
+ else this[slot] = value;
180
+ },
181
+ });
182
+ }
183
+ export function initializeDisplayGeometry(object) {
184
+ var prototype = Object.getPrototypeOf(object);
185
+ var descriptors = displayGeometryDescriptors.get(prototype);
186
+ if (!descriptors) {
187
+ descriptors = Object.create(null);
188
+ for (var name of Object.keys(displayGeometryDefaults)) {
189
+ var owner = prototype;
190
+ while (owner && owner !== DisplayObject.prototype && !Object.hasOwn(owner, name)) {
191
+ owner = Object.getPrototypeOf(owner);
192
+ }
193
+ // A scripted accessor can depend on state initialized only after super().
194
+ // Seed its native value privately so it stays available through super.x, etc.
195
+ var overridden = owner && owner !== DisplayObject.prototype;
196
+ var key = overridden ? displayGeometrySlots[name] : name;
197
+ descriptors[key] = {
198
+ value: displayGeometryDefaults[name], writable: true, configurable: true, enumerable: !overridden,
199
+ };
200
+ }
201
+ displayGeometryDescriptors.set(prototype, descriptors);
202
+ }
203
+ Object.defineProperties(object, descriptors);
204
+ }
205
+ Object.defineProperty(DisplayObject.prototype, 'alpha', {
206
+ enumerable: true, configurable: true,
207
+ get: function () { return this.transform?._colorTransform.alphaMultiplier ?? 1; },
208
+ set: function (value) { this.transform._colorTransform.alphaMultiplier = +value; },
209
+ });
210
+ Object.defineProperty(DisplayObject.prototype, 'cacheAsBitmap', {
211
+ enumerable: true, configurable: true,
212
+ get: function () { return this._cacheAsBitmap || !!(this.filters && this.filters.length); },
213
+ set: function (value) { this._cacheAsBitmap = !!value; },
214
+ });
215
+ Object.defineProperty(DisplayObject.prototype, 'scrollRect', {
216
+ enumerable: true, configurable: true,
217
+ get: function () { return this._scrollRect?.clone() || null; },
218
+ set: function (value) {
219
+ this._scrollRect = value == null ? null : new Rectangle(value.x, value.y, value.width, value.height);
220
+ },
221
+ });
222
+ Object.defineProperty(DisplayObject.prototype, 'mask', {
223
+ enumerable: true, configurable: true,
224
+ get: function () { return displayMasks.get(this) || null; },
225
+ set: function (value) {
226
+ if (!value || typeof value !== 'object' || value === this) value = null;
227
+ var previous = displayMasks.get(this);
228
+ if (previous === value) return;
229
+ if (previous) {
230
+ displayMaskOwners.delete(previous);
231
+ detachedAuthoredMaskBases.delete(previous);
232
+ }
233
+ displayMasks.delete(this);
234
+ if (value) {
235
+ var previousOwner = displayMaskOwners.get(value);
236
+ if (previousOwner) {
237
+ displayMasks.delete(previousOwner);
238
+ detachedAuthoredMaskBases.delete(value);
239
+ }
240
+ displayMasks.set(this, value);
241
+ displayMaskOwners.set(value, this);
242
+ }
243
+ },
244
+ });
245
+ Object.defineProperty(DisplayObject.prototype, 'loaderInfo', {
246
+ enumerable: true, configurable: true,
247
+ get: function () {
248
+ if (this._loaderInfo) return this._loaderInfo;
249
+ var root = this.root;
250
+ return root && root !== this ? root.loaderInfo || null : null;
251
+ },
252
+ set: function (value) { this._loaderInfo = value == null ? null : value; },
253
+ });
254
+ Object.defineProperty(DisplayObject.prototype, 'root', {
255
+ enumerable: true, configurable: true,
256
+ get: function () {
257
+ return this._rootOverride || (this.parent && this.parent.root) || null;
258
+ },
259
+ set: function (value) { this._rootOverride = value == null ? null : value; },
260
+ });
261
+ Object.defineProperty(DisplayObject.prototype, 'stage', {
262
+ enumerable: true, configurable: true,
263
+ get: function () {
264
+ if (this._stageOverride != null) return this._stageOverride;
265
+ return this.parent && this.parent.stage ? this.parent.stage : null;
266
+ },
267
+ set: function (value) { this._stageOverride = value == null ? null : value; },
268
+ });
269
+ Object.defineProperty(DisplayObject.prototype, 'colorTransform', {
270
+ enumerable: true, configurable: true,
271
+ get: function () { return this.transform ? this.transform.colorTransform : this._colorTransform; },
272
+ set: function (value) {
273
+ if (this.transform) this.transform.colorTransform = value;
274
+ else this._colorTransform = value;
275
+ },
276
+ });
277
+ export function displayObjectMatrix(node) {
278
+ var matrix = node.transform?.matrix;
279
+ if (matrix) return matrix;
280
+ var rot = ((+node.rotation || 0) * Math.PI) / 180;
281
+ var sx = readDisplayScale(node, 'scaleX');
282
+ var sy = readDisplayScale(node, 'scaleY');
283
+ return new Matrix(Math.cos(rot) * sx, Math.sin(rot) * sx,
284
+ -Math.sin(rot) * sy, Math.cos(rot) * sy, +node.x || 0, +node.y || 0);
285
+ }
286
+ export function pointPassesScrollRect(node, x, y) {
287
+ var rect = node._scrollRect;
288
+ if (!rect) return true;
289
+ var point = node.globalToLocal(new Point(x, y));
290
+ var dx = point.x - Math.trunc(rect.x), dy = point.y - Math.trunc(rect.y);
291
+ return dx >= 0 && dy >= 0 && dx < rect.width && dy < rect.height;
292
+ }
293
+ export function pointPassesScrollRects(node, x, y) {
294
+ for (; node; node = node.parent) {
295
+ if (!pointPassesScrollRect(node, x, y)) return false;
296
+ }
297
+ return true;
298
+ }
299
+ export function localPointToStage(node, p, z) {
300
+ var avm1LocalMatrix = node?._swoofAvm1LocalMatrix;
301
+ while (node) {
302
+ var rect = node._scrollRect;
303
+ if (rect) {
304
+ p.x -= Math.trunc(rect.x);
305
+ p.y -= Math.trunc(rect.y);
306
+ }
307
+ var info3d = displayMatrix3DInfo(node);
308
+ if (info3d) {
309
+ var projected = matrix3DProjectPoint(info3d.matrix, info3d.projection, p.x, p.y, z);
310
+ if (!projected || matrix3DProjectionDenominator(info3d.matrix, info3d.projection, p.x, p.y, z) < matrix3DNearPlane(info3d.projection)) return invalidPoint();
311
+ p.x = projected[0]; p.y = projected[1];
312
+ z = 0;
313
+ node = node.parent;
314
+ continue;
315
+ }
316
+ var camera = z && node.transform?.perspectiveProjection;
317
+ if (camera) {
318
+ var distance = camera.focalLength + z;
319
+ if (!(distance >= matrix3DNearPlane(camera))) return invalidPoint();
320
+ var center = camera.projectionCenter;
321
+ p.x = (camera.focalLength * p.x + (center?.x || 0) * z) / distance;
322
+ p.y = (camera.focalLength * p.y + (center?.y || 0) * z) / distance;
323
+ z = 0;
324
+ }
325
+ var m = avm1LocalMatrix?.(node) || displayObjectMatrix(node);
326
+ var x = p.x, y = p.y;
327
+ p.x = m.a * x + m.c * y + m.tx;
328
+ p.y = m.b * x + m.d * y + m.ty;
329
+ node = node.parent;
330
+ }
331
+ return p;
332
+ }
333
+ export function invalidPoint() { var p = new Point(); p.x = NaN; p.y = NaN; return p; }
334
+
335
+ /** Sprite → InteractiveObject that designated it as hitArea (Flash: that Sprite receives no mouse). */
336
+ export const hitAreaOwners = new WeakMap();
337
+
338
+ export class InteractiveObject extends DisplayObject {
339
+ constructor() {
340
+ super();
341
+ this.mouseEnabled = true;
342
+ this.contextMenu = null;
343
+ this.doubleClickEnabled = false;
344
+ this._hitArea = null;
345
+ /** Flash default null → obey Stage.stageFocusRect. */
346
+ this._focusRect = null;
347
+ }
348
+ }
349
+ Object.defineProperties(InteractiveObject.prototype, {
350
+ tabEnabled: {
351
+ get: function () { return this._tabEnabled ?? (isSprite(this) && this.buttonMode); },
352
+ set: function (value) {
353
+ var previous = this.tabEnabled;
354
+ this._tabEnabled = !!value;
355
+ if (previous !== this._tabEnabled) this.dispatchEvent(new Event(Event.TAB_ENABLED_CHANGE, true, false));
356
+ },
357
+ configurable: true,
358
+ },
359
+ tabIndex: {
360
+ get: function () { return this._tabIndex ?? -1; },
361
+ set: function (value) {
362
+ value |= 0;
363
+ if (value === this.tabIndex) return;
364
+ this._tabIndex = value;
365
+ this.dispatchEvent(new Event(Event.TAB_INDEX_CHANGE, true, false));
366
+ },
367
+ configurable: true,
368
+ },
369
+ hitArea: {
370
+ get: function () { return this._hitArea; },
371
+ set: function (value) {
372
+ var previous = this._hitArea;
373
+ if (previous && hitAreaOwners.get(previous) === this) hitAreaOwners.delete(previous);
374
+ var next = value && isSprite(value) ? value : null;
375
+ this._hitArea = next;
376
+ if (next) hitAreaOwners.set(next, this);
377
+ },
378
+ configurable: true,
379
+ },
380
+ focusRect: {
381
+ get: function () { return this._focusRect; },
382
+ set: function (value) {
383
+ if (value === null || value === undefined) this._focusRect = null;
384
+ else this._focusRect = !!value;
385
+ },
386
+ configurable: true,
387
+ },
388
+ });
389
+
390
+ export class DisplayObjectContainer extends InteractiveObject {
391
+ constructor() {
392
+ super();
393
+ this._children = [];
394
+ this.mouseChildren = true;
395
+ }
396
+
397
+ addChild(c) {
398
+ return this.addChildAt(c, this._children.length, true);
399
+ }
400
+
401
+ addChildAt(c, i, appending) {
402
+ if (!c) return c;
403
+ // addChild appends to the live list and leaves unnamed authored sprites
404
+ // lazy. An explicit index counts those sprites, so the added listener
405
+ // sees the place the caller asked for.
406
+ if (!appending) materializeIndexedAuthoredChildren(this);
407
+ i = i | 0;
408
+ if (i < 0 || i > this._children.length) throw new RangeError('The supplied index is out of bounds.');
409
+ if (isStageDescendant(c, this)) throw new Error('A display object cannot be added to its own subtree.');
410
+
411
+ var oldParent = c.parent;
412
+ var oldIndex = oldParent === this ? this._children.indexOf(c) : -1;
413
+ if (oldIndex >= 0) {
414
+ moveDisplayChildToIndex(this, c, oldIndex, Math.min(i, this._children.length - 1));
415
+ return c;
416
+ }
417
+ if (oldParent && typeof oldParent.removeChild === 'function') oldParent.removeChild(c);
418
+ // The old container remains responsible for a removal already in progress.
419
+ if (c.parent && c.parent._children && c.parent._children.indexOf(c) >= 0) return c;
420
+ if (isStageDescendant(c, this)) throw new Error('A display object cannot be added to its own subtree.');
421
+ c.parent = this;
422
+ if (c._swoofReleasedFromTimeline && c._swoofCharacterId != null && !c._swoofStandaloneArtworkId) {
423
+ c._swoofStandaloneArtworkId = 'released';
424
+ }
425
+ detachedAuthoredMaskBases.delete(c);
426
+ this._children.splice(i, 0, c);
427
+ try {
428
+ if (typeof globalThis.__swoof_bindAs3SymbolInstance === 'function') globalThis.__swoof_bindAs3SymbolInstance(c);
429
+ } catch (_) {}
430
+ var stage = c.stage;
431
+ var stageTree = stage && stageLifecycleTree(c);
432
+ dispatchChildEvent(c, Event.ADDED, true);
433
+ if (stageTree) dispatchStageLifecycle(stageTree, Event.ADDED_TO_STAGE, stage, this);
434
+ return c;
435
+ }
436
+
437
+ removeChild(c) {
438
+ var i = this._children.indexOf(c);
439
+ if (i < 0 || childrenBeingRemoved.has(c)) return c;
440
+ childrenBeingRemoved.add(c);
441
+ var stage = c.stage;
442
+ try {
443
+ pointerBridge.childRemoved(c);
444
+ dispatchChildEvent(c, Event.REMOVED, true);
445
+ if (stage) {
446
+ // Flash clears focus (focusOut) after removed, before removedFromStage.
447
+ if (isStageDescendant(c, stage.focus)) stage.focus = null;
448
+ dispatchStageLifecycle(stageLifecycleTree(c), Event.REMOVED_FROM_STAGE, stage, this);
449
+ }
450
+ } finally {
451
+ childrenBeingRemoved.delete(c);
452
+ }
453
+ i = this._children.indexOf(c);
454
+ if (i >= 0) this._children.splice(i, 1);
455
+ if (c.parent === this) {
456
+ if (c._swoofAs3TimelineAttached) {
457
+ c._swoofReleasedFromTimeline = true;
458
+ c._swoofAs3Placement = null;
459
+ }
460
+ // The assigned stencil can outlive its placement in the authored timeline.
461
+ if (c._swoofAs3TimelineAttached && displayMaskOwners.has(c)) {
462
+ var matrix = maskAncestorMatrix(this);
463
+ var determinant = matrix && matrix.a * matrix.d - matrix.b * matrix.c;
464
+ if (Number.isFinite(determinant) && determinant !== 0 && Number.isFinite(matrix.tx) && Number.isFinite(matrix.ty)) {
465
+ detachedAuthoredMaskBases.set(c, { matrix: matrix, inverse: matrix.clone().invert() });
466
+ }
467
+ }
468
+ c.parent = null;
469
+ }
470
+ return c;
471
+ }
472
+
473
+ removeChildAt(i) {
474
+ materializeIndexedAuthoredChildren(this);
475
+ return this.removeChild(this._children[i | 0]);
476
+ }
477
+
478
+ removeChildren(beginIndex, endIndex) {
479
+ var begin = beginIndex | 0;
480
+ var end = endIndex === undefined ? 0x7fffffff : endIndex | 0;
481
+ materializeIndexedAuthoredChildren(this);
482
+ var length = this._children.length;
483
+ if (begin > end || end !== 0x7fffffff && (begin < 0 || end >= length)) {
484
+ throw new RangeError('Error #2006: The supplied index is out of bounds.');
485
+ }
486
+ if (begin < 0 || begin >= length) return;
487
+ for (var child of this._children.slice(begin, end + 1)) child.parent?.removeChild(child);
488
+ }
489
+
490
+ getChildAt(i) {
491
+ materializeIndexedAuthoredChildren(this);
492
+ return this._children[i | 0];
493
+ }
494
+
495
+ getChildIndex(c) {
496
+ materializeIndexedAuthoredChildren(this);
497
+ return this._children.indexOf(c);
498
+ }
499
+
500
+ getChildByName(n) {
501
+ for (var i = 0; i < this._children.length; i++) {
502
+ if (this._children[i].name === n) return this._children[i];
503
+ }
504
+ return null;
505
+ }
506
+
507
+ contains(c) {
508
+ return c instanceof DisplayObject && isStageDescendant(this, c);
509
+ }
510
+
511
+ setChildIndex(c, i) {
512
+ materializeIndexedAuthoredChildren(this);
513
+ var old = this._children.indexOf(c);
514
+ i = i | 0;
515
+ if (old < 0) throw new Error('The supplied DisplayObject must be a child of the caller.');
516
+ if (i < 0 || i >= this._children.length) throw new RangeError('The supplied index is out of bounds.');
517
+ moveDisplayChildToIndex(this, c, old, i);
518
+ }
519
+
520
+ swapChildren(a, b) {
521
+ materializeIndexedAuthoredChildren(this);
522
+ var ai = this._children.indexOf(a), bi = this._children.indexOf(b);
523
+ if (ai < 0 || bi < 0) throw new Error('Both DisplayObjects must be children of the caller.');
524
+ this.swapChildrenAt(ai, bi);
525
+ }
526
+
527
+ swapChildrenAt(a, b) {
528
+ materializeIndexedAuthoredChildren(this);
529
+ a = a | 0; b = b | 0;
530
+ if (a < 0 || b < 0 || a >= this._children.length || b >= this._children.length) {
531
+ throw new RangeError('The supplied index is out of bounds.');
532
+ }
533
+ var tmp = this._children[a];
534
+ this._children[a] = this._children[b];
535
+ this._children[b] = tmp;
536
+ }
537
+
538
+ getObjectsUnderPoint(point) {
539
+ materializeIndexedAuthoredChildren(this);
540
+ var out = [];
541
+ if (!point) return out;
542
+ var x = +point.x || 0, y = +point.y || 0;
543
+ var self = this;
544
+ function visit(container) {
545
+ var children = container && container._children;
546
+ if (!children) return;
547
+ for (var i = children.length - 1; i >= 0; i--) {
548
+ var child = children[i];
549
+ if (!child || child.visible === false || displayMaskOwners.has(child)) continue;
550
+ if (child.mask && !pointerBridge.maskContains(child.mask, x, y)) continue;
551
+ if (child instanceof DisplayObjectContainer) visit(child);
552
+ if (typeof child.hitTestPoint === 'function' && child.hitTestPoint(x, y, true)) out.push(child);
553
+ }
554
+ }
555
+ visit(self);
556
+ return out;
557
+ }
558
+
559
+ hitTestPoint(x, y, shapeFlag) {
560
+ materializeIndexedAuthoredChildren(this);
561
+ for (var i = this._children.length - 1; i >= 0; i--) {
562
+ var child = this._children[i];
563
+ if (child && child.visible !== false && !displayMaskOwners.has(child) && child.hitTestPoint(x, y, shapeFlag)) return true;
564
+ }
565
+ return super.hitTestPoint(x, y, shapeFlag);
566
+ }
567
+ }
568
+ export var indexedAuthoredChildren = new WeakMap();
569
+ export function materializeIndexedAuthoredChildren(container) {
570
+ var materialize = indexedAuthoredChildren.get(container);
571
+ if (!materialize) return;
572
+ indexedAuthoredChildren.delete(container);
573
+ materialize();
574
+ }
575
+ Object.defineProperty(DisplayObjectContainer.prototype, 'numChildren', {
576
+ get: function () {
577
+ materializeIndexedAuthoredChildren(this);
578
+ return this._children.length;
579
+ },
580
+ });
581
+ Object.defineProperty(DisplayObjectContainer.prototype, 'tabChildren', {
582
+ get: function () { return this._tabChildren !== false; },
583
+ set: function (value) {
584
+ value = !!value;
585
+ if (value === this.tabChildren) return;
586
+ this._tabChildren = value;
587
+ this.dispatchEvent(new Event(Event.TAB_CHILDREN_CHANGE, true, false));
588
+ },
589
+ configurable: true,
590
+ });
591
+ export var childrenBeingRemoved = new WeakSet();
592
+ export function dispatchChildEvent(child, type, bubbles) {
593
+ if (typeof child.dispatchEvent === 'function') child.dispatchEvent(new Event(type, bubbles, false));
594
+ }
595
+ export function stageLifecycleTree(child) {
596
+ return { child: child, children: child._children ? child._children.map(stageLifecycleTree) : [] };
597
+ }
598
+ export function dispatchStageLifecycle(tree, type, stage, parent) {
599
+ var child = tree.child;
600
+ if (child.parent !== parent || child.stage !== stage) return;
601
+ dispatchChildEvent(child, type, false);
602
+ if (child.parent !== parent || child.stage !== stage) return;
603
+ for (var i = 0; i < tree.children.length; i++) {
604
+ dispatchStageLifecycle(tree.children[i], type, stage, child);
605
+ }
606
+ }
607
+ export function moveDisplayChildToIndex(container, child, old, i) {
608
+ if (old === i) return;
609
+ container._children.splice(old, 1);
610
+ container._children.splice(i, 0, child);
611
+ }
612
+
613
+ export function globalBounds(object, localBounds) {
614
+ if (!object) return null;
615
+ var projected = !localBounds && matrix3DProjectedBounds(object);
616
+ if (projected) {
617
+ if (object.parent && object.parent.localToGlobal) {
618
+ var projectedCorners = [
619
+ object.parent.localToGlobal(new Point(projected.x, projected.y)),
620
+ object.parent.localToGlobal(new Point(projected.x + projected.width, projected.y)),
621
+ object.parent.localToGlobal(new Point(projected.x, projected.y + projected.height)),
622
+ object.parent.localToGlobal(new Point(projected.x + projected.width, projected.y + projected.height)),
623
+ ];
624
+ return rectangleFromPoints(projectedCorners);
625
+ }
626
+ return projected;
627
+ }
628
+ var bounds = localBounds || localDisplayBounds(object);
629
+ if (!bounds.width || !bounds.height) return null;
630
+ return mapBounds(bounds, function (point) { return object.localToGlobal(point); });
631
+ }
632
+
633
+ export function scrollViewportBounds(object) {
634
+ var rect = object._scrollRect;
635
+ return new Rectangle(0, 0, Math.max(0, rect.width), Math.max(0, rect.height));
636
+ }
637
+
638
+ export function mapBounds(bounds, mapPoint) {
639
+ var x = bounds.x, y = bounds.y, right = x + bounds.width, bottom = y + bounds.height;
640
+ return rectangleFromPoints([mapPoint(new Point(x, y)), mapPoint(new Point(right, y)),
641
+ mapPoint(new Point(x, bottom)), mapPoint(new Point(right, bottom))]);
642
+ }
643
+
644
+ export function displayBoundsInTarget(object, target, includeStrokes) {
645
+ materializeIndexedAuthoredChildren(object);
646
+ function subtreeBounds(node) {
647
+ if (node._scrollRect) return scrollViewportBounds(node);
648
+ var bounds = localDisplayBounds(node, includeStrokes);
649
+ if (!(bounds.width || bounds.height)) bounds = null;
650
+ var authored = !node._swoofAuthoredText && authoredMaskSources.get(node)?.bounds?.(node);
651
+ if (authored) {
652
+ var placed = new Rectangle(authored.x, authored.y, authored.w, authored.h);
653
+ bounds = bounds ? bounds.union(placed) : placed;
654
+ }
655
+ for (var child of node._children || []) {
656
+ var childBounds = subtreeBounds(child);
657
+ if (!childBounds) continue;
658
+ var matrix = displayObjectMatrix(child);
659
+ var transformed = mapBounds(childBounds, function (point) { return matrix.transformPoint(point); });
660
+ bounds = bounds ? bounds.union(transformed) : transformed;
661
+ }
662
+ return bounds;
663
+ }
664
+
665
+ var bounds = subtreeBounds(object);
666
+ if (!bounds) return new Rectangle();
667
+ if (!target || target === object) return bounds;
668
+ return mapBounds(bounds, function (point) { return target.globalToLocal(object.localToGlobal(point)); });
669
+ }
670
+
671
+ export function displayTreePixelBounds(object) {
672
+ if (!object) return null;
673
+ var scroll = object._scrollRect;
674
+ if (scroll) {
675
+ var viewport = scrollViewportBounds(object);
676
+ if (!viewport.width || !viewport.height) return null;
677
+ return mapBounds(viewport, function (point) {
678
+ // Screen-space bounds use the stationary viewport. Only the own
679
+ // content scroll is canceled here. Ancestor scroll translations apply.
680
+ point.x += Math.trunc(scroll.x);
681
+ point.y += Math.trunc(scroll.y);
682
+ return object.localToGlobal(point);
683
+ });
684
+ }
685
+ var bounds = globalBounds(object);
686
+ var authored = !object._swoofAuthoredText && authoredMaskSources.get(object)?.bounds?.(object);
687
+ if (authored) {
688
+ var placed = globalBounds(object, new Rectangle(authored.x, authored.y, authored.w, authored.h));
689
+ if (placed) bounds = bounds ? bounds.union(placed) : placed;
690
+ }
691
+ for (var child of object._children || []) {
692
+ var childBounds = displayTreePixelBounds(child);
693
+ if (childBounds) bounds = bounds ? bounds.union(childBounds) : childBounds;
694
+ }
695
+ return bounds;
696
+ }
697
+
698
+ export function rectangleFromPoints(pts) {
699
+ if (!pts || !pts.length) return new Rectangle(0, 0, 0, 0);
700
+ var xs = pts.map(function (p) { return p.x; }), ys = pts.map(function (p) { return p.y; });
701
+ var left = Math.min.apply(Math, xs), top = Math.min.apply(Math, ys);
702
+ return new Rectangle(left, top, Math.max.apply(Math, xs) - left, Math.max.apply(Math, ys) - top);
703
+ }
704
+
705
+ export function authoredTextLocalBounds(field) {
706
+ return field._swoofAuthoredText && authoredMaskSources.get(field)?.bounds?.(field);
707
+ }
708
+
709
+ export function localDisplayBounds(object, includeStrokes) {
710
+ if (isTextLine(object)) return new Rectangle(0, -object.ascent, object.width, object.height);
711
+ var textBounds = authoredTextLocalBounds(object);
712
+ if (textBounds) return new Rectangle(textBounds.x, textBounds.y, textBounds.w, textBounds.h);
713
+ var bounds = object.graphics && object.graphics.getBounds ? object.graphics.getBounds(includeStrokes) : null;
714
+ if (authoredSpriteSizing.has(object)) return bounds || new Rectangle();
715
+ var w = object.width || (bounds && bounds.width) || 0, h = object.height || (bounds && bounds.height) || 0;
716
+ return new Rectangle(bounds ? bounds.x : 0, bounds ? bounds.y : 0, w, h);
717
+ }
718
+
719
+ export function authoredSpriteDimension(size, matrixAxis, crossAxis, scale) {
720
+ return {
721
+ enumerable: true, configurable: true,
722
+ get: function () {
723
+ var bounds = displayBoundsInTarget(this, this, true);
724
+ var matrix = this.transform.matrix;
725
+ if (!matrix) return bounds[size];
726
+ return Math.abs(matrix[matrixAxis]) * bounds.width + Math.abs(matrix[crossAxis]) * bounds.height;
727
+ },
728
+ set: function (value) {
729
+ var target = Number(value), matrix = this.transform.matrix;
730
+ if (!Number.isFinite(target) || target < 0 || !matrix ||
731
+ matrix.b || matrix.c || matrix.a < 0 || matrix.d < 0) return;
732
+ var length = displayBoundsInTarget(this, this, true)[size];
733
+ if (length > 0) this[scale] = target / length;
734
+ },
735
+ };
736
+ }
737
+
738
+ export function registerIndexedAuthoredChildren(container, materialize) {
739
+ if (container instanceof DisplayObjectContainer) indexedAuthoredChildren.set(container, materialize);
740
+ }
741
+ export function isDisplayMask(node) { return displayMaskOwners.has(node); }
742
+ export function registerAuthoredMaskSource(node, source) { authoredMaskSources.set(node, source); }
743
+ export function assignedDisplayMask(node) { return displayMasks.get(node); }
744
+
745
+ export function registerAuthoredSpriteSizing(node, bounds) {
746
+ if (!isSprite(node) || authoredSpriteSizing.has(node)) return;
747
+ // Component constructors can read the linked artwork through super.width
748
+ // and super.height before initializing their own layout accessors.
749
+ var widthSlot = displayGeometrySlots.width, heightSlot = displayGeometrySlots.height;
750
+ if (bounds?.w > 0 && Object.hasOwn(node, widthSlot)) node[widthSlot] = bounds.w;
751
+ if (bounds?.h > 0 && Object.hasOwn(node, heightSlot)) node[heightSlot] = bounds.h;
752
+ // Linked skins use the native size slots. Component subclasses can supply
753
+ // their own layout dimensions and scales through inherited accessors.
754
+ for (var name of ['width', 'height', 'scaleX', 'scaleY']) {
755
+ var own = Object.getOwnPropertyDescriptor(node, name);
756
+ if (!own?.writable || !own.configurable) return;
757
+ for (var prototype = Object.getPrototypeOf(node); prototype && prototype !== DisplayObject.prototype;
758
+ prototype = Object.getPrototypeOf(prototype)) {
759
+ if (Object.prototype.hasOwnProperty.call(prototype, name)) return;
760
+ }
761
+ }
762
+ Object.defineProperties(node, {
763
+ width: authoredSpriteDimension('width', 'a', 'c', 'scaleX'),
764
+ height: authoredSpriteDimension('height', 'b', 'd', 'scaleY'),
765
+ });
766
+ authoredSpriteSizing.add(node);
767
+ }
768
+
769
+ export function cssColor(color, alpha) {
770
+ var c = (color | 0) >>> 0;
771
+ var a = alpha == null ? 1 : +alpha;
772
+ if (!(a >= 0)) a = 1;
773
+ if (a > 1) a = 1;
774
+ var r = (c >> 16) & 255;
775
+ var g = (c >> 8) & 255;
776
+ var b = c & 255;
777
+ if (a >= 1) {
778
+ return '#' + [r, g, b].map(function (v) { return v.toString(16).padStart(2, '0'); }).join('');
779
+ }
780
+ return 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')';
781
+ }
782
+
783
+ export function gradientStops(fill) {
784
+ var source = [], colors = fill.colors || [], ratios = fill.ratios || [], alphas = fill.alphas || [];
785
+ for (var i = 0; i < colors.length; i++) {
786
+ source.push({ ratio: Math.max(0, Math.min(1, (ratios[i] == null ? i * 255 / Math.max(1, colors.length - 1) : ratios[i]) / 255)), color: colors[i] >>> 0, alpha: alphas[i] == null ? 1 : +alphas[i] });
787
+ }
788
+ source.sort(function (a, b) { return a.ratio - b.ratio; });
789
+ var linear = String(fill.interpolationMethod || '').toLowerCase() === 'linearrgb' || fill.interpolationMethod === 1;
790
+ if (linear && source.length > 1) {
791
+ var dense = [];
792
+ for (var j = 0; j < source.length - 1; j++) {
793
+ var a = source[j], b = source[j + 1];
794
+ for (var k = 0; k < 8; k++) {
795
+ var t = k / 8, channels = [];
796
+ for (var ch = 0; ch < 3; ch++) {
797
+ var av = (a.color >>> (16 - ch * 8)) & 255, bv = (b.color >>> (16 - ch * 8)) & 255;
798
+ av /= 255; bv /= 255;
799
+ av = av <= 0.04045 ? av / 12.92 : Math.pow((av + 0.055) / 1.055, 2.4);
800
+ bv = bv <= 0.04045 ? bv / 12.92 : Math.pow((bv + 0.055) / 1.055, 2.4);
801
+ var v = av + (bv - av) * t;
802
+ v = v <= 0.0031308 ? 12.92 * v : 1.055 * Math.pow(v, 1 / 2.4) - 0.055;
803
+ channels.push(Math.max(0, Math.min(255, Math.round(v * 255))));
804
+ }
805
+ dense.push({ ratio: a.ratio + (b.ratio - a.ratio) * t, color: (channels[0] << 16) | (channels[1] << 8) | channels[2], alpha: a.alpha + (b.alpha - a.alpha) * t });
806
+ }
807
+ }
808
+ dense.push(source[source.length - 1]); source = dense;
809
+ }
810
+ var spread = String(fill.spreadMethod || 'pad').toLowerCase();
811
+ if (spread !== 'reflect' && spread !== 'repeat') return source;
812
+ var repeated = [];
813
+ for (var tile = -3; tile <= 4; tile++) {
814
+ for (var si = 0; si < source.length; si++) {
815
+ var stop = source[si], reverse = spread === 'reflect' && Math.abs(tile % 2) === 1;
816
+ var position = tile + (reverse ? 1 - stop.ratio : stop.ratio);
817
+ if (position >= 0 && position <= 1) repeated.push({ ratio: position, color: stop.color, alpha: stop.alpha });
818
+ }
819
+ }
820
+ repeated.sort(function (a, b) { return a.ratio - b.ratio; });
821
+ return repeated.length ? repeated : source;
822
+ }
823
+