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,144 @@
1
+ import { displayObjectMatrix, localDisplayBounds, rectangleFromPoints } from './display.js';
2
+ import { Matrix, Point, Rectangle } from './geom.js';
3
+
4
+ export function matrix3DProjectPoint(matrix3D, projection, x, y, depth = 0) {
5
+ var data = matrix3D && matrix3D.rawData;
6
+ if (!data || data.length < 16) return null;
7
+ var focal = projection && +projection.focalLength;
8
+ if (!(focal > 0)) return null;
9
+ var center = projection.projectionCenter || {};
10
+ var cx = +center.x || 0, cy = +center.y || 0;
11
+ var X = (+data[0] || 0) * x + (+data[4] || 0) * y + (+data[12] || 0);
12
+ var Y = (+data[1] || 0) * x + (+data[5] || 0) * y + (+data[13] || 0);
13
+ var Z = (+data[2] || 0) * x + (+data[6] || 0) * y + (+data[14] || 0);
14
+ var W = (+data[3] || 0) * x + (+data[7] || 0) * y + (data[15] == null ? 1 : (+data[15] || 0));
15
+ if (depth) {
16
+ X += (+data[8] || 0) * depth;
17
+ Y += (+data[9] || 0) * depth;
18
+ Z += (+data[10] || 0) * depth;
19
+ W += (+data[11] || 0) * depth;
20
+ }
21
+ var denominator = focal * W + Z;
22
+ if (!(denominator > 0.0001)) return null;
23
+ var px = (focal * X + cx * Z) / denominator;
24
+ var py = (focal * Y + cy * Z) / denominator;
25
+ return Number.isFinite(px) && Number.isFinite(py) ? [px, py] : null;
26
+ }
27
+
28
+ export function matrix3DProjectionDenominator(matrix3D, projection, x, y, depth = 0) {
29
+ var data = matrix3D && matrix3D.rawData;
30
+ var focal = projection && +projection.focalLength;
31
+ if (!data || data.length < 16 || !(focal > 0)) return null;
32
+ var z = (+data[2] || 0) * x + (+data[6] || 0) * y + (+data[14] || 0);
33
+ var w = (+data[3] || 0) * x + (+data[7] || 0) * y + (data[15] == null ? 1 : (+data[15] || 0));
34
+ return focal * w + z + (depth ? (focal * (+data[11] || 0) + (+data[10] || 0)) * depth : 0);
35
+ }
36
+
37
+ export function matrix3DNearPlane(projection) {
38
+ return Math.max(0.01, (+projection.focalLength || 0) * 0.001);
39
+ }
40
+
41
+ export function displayMatrix3DInfo(object) {
42
+ var matrix3D = object?.transform?.matrix3D;
43
+ if (!matrix3D) return null;
44
+ for (var node = object; node; node = node.parent) {
45
+ var projection = node.transform?.perspectiveProjection;
46
+ if (projection) {
47
+ return +projection.focalLength > 0
48
+ ? { matrix: matrix3D, projection: projectionInParentSpace(projection, node, object) } : null;
49
+ }
50
+ }
51
+ return null;
52
+ }
53
+
54
+ export function projectionInParentSpace(projection, owner, object) {
55
+ if (owner === object || owner === object.parent) return projection;
56
+ var relation = new Matrix();
57
+ for (var node = object.parent; node && node !== owner; node = node.parent) {
58
+ if (node.transform?.matrix3D) return projection;
59
+ var viewport = node._scrollRect;
60
+ if (viewport) relation.translate(-Math.trunc(viewport.x), -Math.trunc(viewport.y));
61
+ relation.concat(displayObjectMatrix(node));
62
+ }
63
+ var determinant = relation.a * relation.d - relation.b * relation.c;
64
+ if (!Number.isFinite(determinant) || Math.abs(determinant) < 1e-12) return projection;
65
+ return { focalLength: projection.focalLength,
66
+ projectionCenter: relation.invert().transformPoint(projection.projectionCenter || new Point()) };
67
+ }
68
+
69
+ export function matrix3DProjectedPolygon(object, bounds) {
70
+ var info = displayMatrix3DInfo(object);
71
+ if (!info || !(bounds.width > 0 && bounds.height > 0)) return null;
72
+ var near = matrix3DNearPlane(info.projection);
73
+ var poly = [
74
+ { x: bounds.x, y: bounds.y }, { x: bounds.x + bounds.width, y: bounds.y },
75
+ { x: bounds.x + bounds.width, y: bounds.y + bounds.height }, { x: bounds.x, y: bounds.y + bounds.height },
76
+ ];
77
+ var clipped = [];
78
+ for (var i = 0; i < poly.length; i++) {
79
+ var a = poly[i], b = poly[(i + 1) % poly.length];
80
+ var da = matrix3DProjectionDenominator(info.matrix, info.projection, a.x, a.y);
81
+ var db = matrix3DProjectionDenominator(info.matrix, info.projection, b.x, b.y);
82
+ var insideA = da != null && da >= near, insideB = db != null && db >= near;
83
+ if (insideA) clipped.push(a);
84
+ if (insideA !== insideB && da != null && db != null && db !== da) {
85
+ var t = (near - da) / (db - da);
86
+ clipped.push({ x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t });
87
+ }
88
+ }
89
+ var result = [];
90
+ clipped.forEach(function (p) {
91
+ var projected = matrix3DProjectPoint(info.matrix, info.projection, p.x, p.y);
92
+ if (projected) result.push(new Point(projected[0], projected[1]));
93
+ });
94
+ return result;
95
+ }
96
+
97
+ export function matrix3DProjectedBounds(object) {
98
+ if (!displayMatrix3DInfo(object)) return null;
99
+ var polygon = matrix3DProjectedPolygon(object, localDisplayBounds(object));
100
+ if (!polygon || !polygon.length) return new Rectangle(0, 0, 0, 0);
101
+ return rectangleFromPoints(polygon);
102
+ }
103
+
104
+ export function matrix3DUnprojectPoint(object, x, y) {
105
+ var info = displayMatrix3DInfo(object);
106
+ if (!info) return null;
107
+ if (object.parent && object.parent.globalToLocal) {
108
+ var parentPoint = object.parent.globalToLocal(new Point(x, y));
109
+ x = parentPoint.x; y = parentPoint.y;
110
+ }
111
+ return matrix3DUnprojectInParent(info, x, y, true);
112
+ }
113
+
114
+ export function matrix3DUnprojectInParent(info, x, y, requireVisible) {
115
+ var d = info.matrix.rawData, projection = info.projection;
116
+ var f = +projection.focalLength, center = projection.projectionCenter || {};
117
+ var cx = +center.x || 0, cy = +center.y || 0;
118
+ // Invert the planar projective transform as two linear equations in x/y.
119
+ var wx = f * (+d[3] || 0) + (+d[2] || 0);
120
+ var wy = f * (+d[7] || 0) + (+d[6] || 0);
121
+ var w0 = f * (d[15] == null ? 1 : (+d[15] || 0)) + (+d[14] || 0);
122
+ var a = f * (+d[0] || 0) + cx * (+d[2] || 0) - x * wx;
123
+ var b = f * (+d[4] || 0) + cx * (+d[6] || 0) - x * wy;
124
+ var c = f * (+d[12] || 0) + cx * (+d[14] || 0) - x * w0;
125
+ var e = f * (+d[1] || 0) + cy * (+d[2] || 0) - y * wx;
126
+ var g = f * (+d[5] || 0) + cy * (+d[6] || 0) - y * wy;
127
+ var h = f * (+d[13] || 0) + cy * (+d[14] || 0) - y * w0;
128
+ var det = a * g - b * e;
129
+ if (!Number.isFinite(det) || Math.abs(det) < 1e-10) return null;
130
+ var lx = (b * h - c * g) / det, ly = (c * e - a * h) / det;
131
+ var denominator = matrix3DProjectionDenominator(info.matrix, projection, lx, ly);
132
+ if (!Number.isFinite(lx) || !Number.isFinite(ly) || denominator == null ||
133
+ (requireVisible && denominator < matrix3DNearPlane(projection)) || Math.abs(denominator) < 1e-10) return null;
134
+ return new Point(lx, ly);
135
+ }
136
+
137
+ export function maskAncestorMatrix(parent) {
138
+ var matrix = new Matrix();
139
+ for (var current = parent; current; current = current.parent) {
140
+ if (displayMatrix3DInfo(current)) return null;
141
+ matrix.concat(displayObjectMatrix(current));
142
+ }
143
+ return matrix;
144
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Display-list hooks implemented by the pointer module.
3
+ * The display list calls these; input.js fills them in once pointer state exists.
4
+ */
5
+ export const pointerBridge = {
6
+ childRemoved() {},
7
+ maskContains() { return true; },
8
+ };
@@ -0,0 +1,158 @@
1
+ import { DisplayObject } from './display.js';
2
+ import { StageScaleMode } from './events.js';
3
+ import { Matrix3D, Point } from './geom.js';
4
+ import { StageClass, stageSingleton } from './stage.js';
5
+ import { stageRef } from './stage-ref.js';
6
+
7
+ export class PerspectiveProjection {
8
+ constructor() {
9
+ this._fieldOfView = 55;
10
+ this._focalLength = 1;
11
+ this.projectionCenter = new Point((stageSingleton.stageWidth || 0) / 2, (stageSingleton.stageHeight || 0) / 2);
12
+ this._updateFocalLength();
13
+ }
14
+
15
+ _updateFocalLength() {
16
+ var width = +(this._defaultStage || stageSingleton).stageWidth || 0;
17
+ var halfAngle = Math.max(0.001, Math.min(179.999, +this._fieldOfView || 55)) * Math.PI / 360;
18
+ this._focalLength = width > 0 ? (width / 2) / Math.tan(halfAngle) : 1;
19
+ }
20
+
21
+ get fieldOfView() { return this._fieldOfView; }
22
+ set fieldOfView(value) {
23
+ this._fieldOfView = Math.max(0.001, Math.min(179.999, +value || 55));
24
+ this._manualFocalLength = false;
25
+ this._updateFocalLength();
26
+ }
27
+
28
+ get focalLength() { return this._focalLength; }
29
+ set focalLength(value) {
30
+ this._focalLength = Math.max(0.001, +value || 1);
31
+ this._manualFocalLength = true;
32
+ }
33
+
34
+ clone() {
35
+ var copy = new PerspectiveProjection();
36
+ copy.fieldOfView = this.fieldOfView;
37
+ copy.focalLength = this.focalLength;
38
+ copy.projectionCenter = this.projectionCenter ? this.projectionCenter.clone() : null;
39
+ return copy;
40
+ }
41
+
42
+ toMatrix3D() { return this.toMatrix3DToOutput(); }
43
+ toMatrix3DToOutput(output) {
44
+ if (output != null && !(output instanceof Matrix3D)) throw new TypeError('Expected a Matrix3D output');
45
+ if (!output) output = new Matrix3D();
46
+ var f = this.focalLength, center = this.projectionCenter;
47
+ output.rawData = [f, 0, 0, 0, 0, f, 0, 0, center?.x || 0, center?.y || 0, 1, 1, 0, 0, 0, f];
48
+ return output;
49
+ }
50
+ }
51
+ export var defaultPerspectiveByRoot = new WeakMap();
52
+ export function defaultPerspectiveProjection(owner) {
53
+ if (!owner || owner._rootOverride !== owner) return null;
54
+ var stage = owner.stage;
55
+ if (!stage) return null;
56
+ var width = Number(stage.stageWidth) || 0, height = Number(stage.stageHeight) || 0;
57
+ var state = defaultPerspectiveByRoot.get(owner);
58
+ if (!state) {
59
+ var projection = new PerspectiveProjection();
60
+ state = { projection: projection, width: null, height: null };
61
+ defaultPerspectiveByRoot.set(owner, state);
62
+ }
63
+ state.projection._defaultStage = stage;
64
+ if (state.width !== width || state.height !== height) {
65
+ var center = state.projection.projectionCenter;
66
+ if (state.width === null || (center?.x === state.width / 2 && center.y === state.height / 2)) {
67
+ state.projection.projectionCenter = new Point(width / 2, height / 2);
68
+ }
69
+ if (!state.projection._manualFocalLength) state.projection._updateFocalLength();
70
+ state.width = width;
71
+ state.height = height;
72
+ }
73
+ return state.projection;
74
+ }
75
+ export var stageLayoutCanvas = null, stageLayoutBaseWidth = 0, stageLayoutBaseHeight = 0;
76
+ export function applyStageLayout() {
77
+ if (typeof document === 'undefined') return;
78
+ var canvas = document.getElementById && document.getElementById('stage');
79
+ var wrap = document.getElementById && document.getElementById('stage-wrap');
80
+ if (!canvas || !wrap || !canvas.style || !wrap.style) return;
81
+ if (canvas !== stageLayoutCanvas) {
82
+ stageLayoutCanvas = canvas;
83
+ stageLayoutBaseWidth = canvas.width || stageSingleton.stageWidth || 1;
84
+ stageLayoutBaseHeight = canvas.height || stageSingleton.stageHeight || 1;
85
+ }
86
+ var body = document.body;
87
+ var width = stageLayoutBaseWidth || stageSingleton.stageWidth || 1;
88
+ var height = stageLayoutBaseHeight || stageSingleton.stageHeight || 1;
89
+ var viewWidth = typeof window !== 'undefined' ? window.innerWidth : width;
90
+ var viewHeight = typeof window !== 'undefined' ? window.innerHeight : height;
91
+ viewWidth = Math.max(1, viewWidth || width); viewHeight = Math.max(1, viewHeight || height);
92
+ var mode = stageSingleton.scaleMode;
93
+ var cssWidth = width, cssHeight = height, wrapWidth = width, wrapHeight = height;
94
+ var left = 0, top = 0;
95
+ if (mode === StageScaleMode.NO_SCALE) {
96
+ if (canvas.width !== viewWidth) canvas.width = viewWidth;
97
+ if (canvas.height !== viewHeight) canvas.height = viewHeight;
98
+ stageSingleton.stageWidth = viewWidth;
99
+ stageSingleton.stageHeight = viewHeight;
100
+ cssWidth = wrapWidth = viewWidth; cssHeight = wrapHeight = viewHeight;
101
+ } else {
102
+ if (canvas.width !== width) canvas.width = width;
103
+ if (canvas.height !== height) canvas.height = height;
104
+ stageSingleton.stageWidth = width;
105
+ stageSingleton.stageHeight = height;
106
+ }
107
+ if (mode === StageScaleMode.EXACT_FIT) {
108
+ cssWidth = wrapWidth = viewWidth; cssHeight = wrapHeight = viewHeight;
109
+ } else if (mode === StageScaleMode.NO_BORDER) {
110
+ var cover = Math.max(viewWidth / width, viewHeight / height);
111
+ cssWidth = width * cover; cssHeight = height * cover;
112
+ wrapWidth = viewWidth; wrapHeight = viewHeight;
113
+ left = (viewWidth - cssWidth) / 2; top = (viewHeight - cssHeight) / 2;
114
+ } else if (mode === StageScaleMode.SHOW_ALL) {
115
+ var fit = Math.min(1, viewWidth / width, viewHeight / height);
116
+ cssWidth = wrapWidth = width * fit; cssHeight = wrapHeight = height * fit;
117
+ }
118
+ wrap.style.aspectRatio = 'auto';
119
+ wrap.style.width = wrapWidth + 'px'; wrap.style.height = wrapHeight + 'px';
120
+ wrap.style.overflow = mode === StageScaleMode.NO_BORDER ? 'hidden' : 'visible';
121
+ canvas.style.width = cssWidth + 'px'; canvas.style.height = cssHeight + 'px';
122
+ canvas.style.marginLeft = left + 'px'; canvas.style.marginTop = top + 'px';
123
+ if (body && body.style) {
124
+ var align = String(stageSingleton.align || '').toUpperCase();
125
+ body.style.justifyContent = align.indexOf('L') >= 0 ? 'flex-start' : align.indexOf('R') >= 0 ? 'flex-end' : 'center';
126
+ body.style.alignItems = align.indexOf('T') >= 0 ? 'flex-start' : align.indexOf('B') >= 0 ? 'flex-end' : 'center';
127
+ }
128
+ }
129
+ Object.defineProperty(StageClass.prototype, 'scaleMode', {
130
+ get: function () { return this._scaleMode; },
131
+ set: function (value) {
132
+ var mode = String(value || StageScaleMode.SHOW_ALL);
133
+ this._scaleMode = Object.keys(StageScaleMode).some(function (key) { return StageScaleMode[key] === mode; }) ? mode : StageScaleMode.SHOW_ALL;
134
+ if (this === stageSingleton) applyStageLayout();
135
+ },
136
+ configurable: true,
137
+ });
138
+ Object.defineProperty(StageClass.prototype, 'align', {
139
+ get: function () { return this._align; },
140
+ set: function (value) { this._align = String(value || '').toUpperCase(); if (this === stageSingleton) applyStageLayout(); },
141
+ configurable: true,
142
+ });
143
+ if (typeof window !== 'undefined' && window.addEventListener) window.addEventListener('resize', applyStageLayout);
144
+ Object.defineProperty(DisplayObject.prototype, 'mouseX', {
145
+ get: function () {
146
+ var st = this.stage || stageSingleton;
147
+ var g = this.globalToLocal(new Point(st.mouseX || 0, st.mouseY || 0));
148
+ return g.x;
149
+ },
150
+ });
151
+ Object.defineProperty(DisplayObject.prototype, 'mouseY', {
152
+ get: function () {
153
+ var st = this.stage || stageSingleton;
154
+ var g = this.globalToLocal(new Point(st.mouseX || 0, st.mouseY || 0));
155
+ return g.y;
156
+ },
157
+ });
158
+ stageRef.defaultPerspective = defaultPerspectiveProjection;