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.
- package/README.md +9 -9
- package/package.json +2 -1
- package/src/cli.js +15 -6
- package/src/emitter/bundle-emitter.js +56 -16
- package/src/runtime/avm1-runtime.js +329 -84
- package/src/runtime/avm2-runtime.js +56 -6
- package/src/runtime/embedded-movie-runtime.js +6 -1
- package/src/runtime/index.js +2 -6
- package/src/runtime/movie-runtime.js +2 -1
- package/src/runtime/swflib/assemble.js +34 -0
- package/src/runtime/swflib/bitmap-image.js +4 -0
- package/src/runtime/swflib/bitmap.js +1004 -0
- package/src/runtime/swflib/builtins.js +156 -0
- package/src/runtime/swflib/bytearray.js +949 -0
- package/src/runtime/swflib/compiled-swf.js +3 -0
- package/src/runtime/swflib/context-menu.js +245 -0
- package/src/runtime/swflib/cursor-flag.js +12 -0
- package/src/runtime/swflib/dictionary.js +121 -0
- package/src/runtime/swflib/display.js +823 -0
- package/src/runtime/swflib/events.js +754 -0
- package/src/runtime/swflib/filters.js +298 -0
- package/src/runtime/swflib/flash.js +130 -0
- package/src/runtime/swflib/geom.js +759 -0
- package/src/runtime/swflib/glyph-metrics.js +14 -0
- package/src/runtime/swflib/graphics.js +735 -0
- package/src/runtime/swflib/host.js +2 -0
- package/src/runtime/swflib/inherit.js +22 -0
- package/src/runtime/swflib/input.js +1085 -0
- package/src/runtime/swflib/loader.js +687 -0
- package/src/runtime/swflib/marks.js +16 -0
- package/src/runtime/swflib/matrix3d.js +144 -0
- package/src/runtime/swflib/pointer-bridge.js +8 -0
- package/src/runtime/swflib/projection.js +158 -0
- package/src/runtime/swflib/render.js +1240 -0
- package/src/runtime/swflib/sound.js +1166 -0
- package/src/runtime/swflib/sprite-drag.js +116 -0
- package/src/runtime/swflib/stage-ref.js +9 -0
- package/src/runtime/swflib/stage.js +484 -0
- package/src/runtime/swflib/system.js +987 -0
- package/src/runtime/swflib/text-field.js +519 -0
- package/src/runtime/swflib/text-paint.js +797 -0
- package/src/runtime/swflib/text.js +1387 -0
- package/src/runtime/swflib/transform.js +133 -0
- package/src/runtime/swflib/xml.js +268 -0
- package/src/transpiler/avm1/avm1-ir.js +2 -1
- package/src/transpiler/avm1/avm1-rewrite.js +23 -2
- package/src/transpiler/avm1/avm1-transpiler.js +4 -1
- package/src/transpiler/avm2/avm2-transpiler.js +38 -7
- package/src/transpiler/timeline/timeline-codegen.js +366 -123
- package/src/runtime/swflib.js +0 -14050
|
@@ -0,0 +1,1240 @@
|
|
|
1
|
+
import { Bitmap, bitmapChannelIndex, bitmapContourPlan, bitmapDisplayImage, bitmapFilterMargins, bitmapShadowPlan } from './bitmap.js';
|
|
2
|
+
import { DisplayObject, authoredMaskSources, cssColor, detachedAuthoredMaskBases, displayMaskOwners, displayObjectMatrix, isStageDescendant, localDisplayBounds, readDisplayScale, rectangleFromPoints } from './display.js';
|
|
3
|
+
import { BlendMode, TextFieldType } from './events.js';
|
|
4
|
+
import { BevelFilter, BlurFilter, ColorMatrixFilter, ConvolutionFilter, DisplacementMapFilter, DisplacementMapFilterMode, DropShadowFilter, GlowFilter, GradientBevelFilter, GradientGlowFilter } from './filters.js';
|
|
5
|
+
import { Matrix, Point, Rectangle } from './geom.js';
|
|
6
|
+
import { Graphics } from './graphics.js';
|
|
7
|
+
import { displayMatrix3DInfo, maskAncestorMatrix, matrix3DNearPlane, matrix3DProjectPoint, matrix3DProjectionDenominator } from './matrix3d.js';
|
|
8
|
+
import { _buttonCapture, _hoveredButton, _keyboardButtonCapture } from './input.js';
|
|
9
|
+
import { SimpleButton } from './loader.js';
|
|
10
|
+
import { Mouse, stageSingleton } from './stage.js';
|
|
11
|
+
import { TextField } from './text-field.js';
|
|
12
|
+
import { paintTextField } from './text-paint.js';
|
|
13
|
+
import { TextLine, paintFteLine, textLineState } from './text.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Paint an AS3 display list subtree (graphics + Bitmap + children).
|
|
17
|
+
* @param {CanvasRenderingContext2D} ctx
|
|
18
|
+
* @param {object} node
|
|
19
|
+
* @param {number} [depth]
|
|
20
|
+
*/
|
|
21
|
+
export function matrix3DCanvasTransform(matrix3D, projection) {
|
|
22
|
+
var data = matrix3D && matrix3D.rawData;
|
|
23
|
+
if (!data || data.length < 16) return null;
|
|
24
|
+
var m0 = +data[0] || 0, m1 = +data[1] || 0, m2 = +data[2] || 0, m3 = +data[3] || 0;
|
|
25
|
+
var m4 = +data[4] || 0, m5 = +data[5] || 0, m6 = +data[6] || 0, m7 = +data[7] || 0;
|
|
26
|
+
var m12 = +data[12] || 0, m13 = +data[13] || 0, m14 = +data[14] || 0;
|
|
27
|
+
var m15 = data[15] == null ? 1 : (+data[15] || 0);
|
|
28
|
+
if (!projection) return [m0, m1, m4, m5, m12, m13];
|
|
29
|
+
|
|
30
|
+
var focal = +projection.focalLength || 0;
|
|
31
|
+
if (!(focal > 0)) return [m0, m1, m4, m5, m12, m13];
|
|
32
|
+
var center = projection.projectionCenter || {};
|
|
33
|
+
var cx = +center.x || 0, cy = +center.y || 0;
|
|
34
|
+
// Project the local z=0 plane into 2D. Canvas only accepts affine
|
|
35
|
+
// transforms, so evaluate the exact projective transform and its Jacobian
|
|
36
|
+
// at the local origin; this preserves 3D rotation, depth scaling, and
|
|
37
|
+
// translation while approximating perspective variation across the object.
|
|
38
|
+
var w = m15;
|
|
39
|
+
var denominator = focal * w + m14;
|
|
40
|
+
if (!(denominator > 0.0001)) return null;
|
|
41
|
+
var nx = focal * m12 + cx * m14;
|
|
42
|
+
var ny = focal * m13 + cy * m14;
|
|
43
|
+
var u = nx / denominator, v = ny / denominator;
|
|
44
|
+
var dwDx = focal * m3 + m2, dwDy = focal * m7 + m6;
|
|
45
|
+
var duDx = ((focal * m0 + cx * m2) * denominator - nx * dwDx) / (denominator * denominator);
|
|
46
|
+
var duDy = ((focal * m4 + cx * m6) * denominator - nx * dwDy) / (denominator * denominator);
|
|
47
|
+
var dvDx = ((focal * m1 + cy * m2) * denominator - ny * dwDx) / (denominator * denominator);
|
|
48
|
+
var dvDy = ((focal * m5 + cy * m6) * denominator - ny * dwDy) / (denominator * denominator);
|
|
49
|
+
if (![duDx, duDy, dvDx, dvDy, u, v].every(Number.isFinite)) return null;
|
|
50
|
+
return [duDx, dvDx, duDy, dvDy, u, v];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
export function drawAdaptivePerspectiveTriangle(ctx, image, matrix3D, projection, vertices, target, nearPlane, sourcePadding, depth, state) {
|
|
55
|
+
var midpoints = [
|
|
56
|
+
[0, 1], [1, 2], [2, 0],
|
|
57
|
+
].map(function (edge) {
|
|
58
|
+
var a = vertices[edge[0]], b = vertices[edge[1]];
|
|
59
|
+
var point = {
|
|
60
|
+
x: (a.x + b.x) / 2,
|
|
61
|
+
y: (a.y + b.y) / 2,
|
|
62
|
+
denominator: (a.denominator + b.denominator) / 2,
|
|
63
|
+
};
|
|
64
|
+
return { point: point, projected: matrix3DProjectPoint(matrix3D, projection, point.x, point.y), edge: edge };
|
|
65
|
+
});
|
|
66
|
+
var center = {
|
|
67
|
+
x: (vertices[0].x + vertices[1].x + vertices[2].x) / 3,
|
|
68
|
+
y: (vertices[0].y + vertices[1].y + vertices[2].y) / 3,
|
|
69
|
+
};
|
|
70
|
+
center.denominator = matrix3DProjectionDenominator(matrix3D, projection, center.x, center.y);
|
|
71
|
+
var projectedCenter = matrix3DProjectPoint(matrix3D, projection, center.x, center.y);
|
|
72
|
+
var error = 0;
|
|
73
|
+
for (var i = 0; i < midpoints.length; i++) {
|
|
74
|
+
var midpoint = midpoints[i];
|
|
75
|
+
if (!midpoint.projected) { error = Infinity; break; }
|
|
76
|
+
var a = target[midpoint.edge[0]], b = target[midpoint.edge[1]];
|
|
77
|
+
error = Math.max(error, Math.hypot(
|
|
78
|
+
midpoint.projected[0] - (a[0] + b[0]) / 2,
|
|
79
|
+
midpoint.projected[1] - (a[1] + b[1]) / 2
|
|
80
|
+
));
|
|
81
|
+
}
|
|
82
|
+
if (projectedCenter) {
|
|
83
|
+
var affineCenter = [
|
|
84
|
+
(target[0][0] + target[1][0] + target[2][0]) / 3,
|
|
85
|
+
(target[0][1] + target[1][1] + target[2][1]) / 3,
|
|
86
|
+
];
|
|
87
|
+
error = Math.max(error, Math.hypot(projectedCenter[0] - affineCenter[0], projectedCenter[1] - affineCenter[1]));
|
|
88
|
+
} else error = Infinity;
|
|
89
|
+
|
|
90
|
+
if (error > state.tolerance && depth < state.maxDepth && state.triangleCount + 4 <= state.maxTriangles) {
|
|
91
|
+
var ab = midpoints[0].point, bc = midpoints[1].point, ca = midpoints[2].point;
|
|
92
|
+
var pab = midpoints[0].projected, pbc = midpoints[1].projected, pca = midpoints[2].projected;
|
|
93
|
+
if (!pab || !pbc || !pca) return;
|
|
94
|
+
drawAdaptivePerspectiveTriangle(ctx, image, matrix3D, projection, [vertices[0], ab, ca], [target[0], pab, pca], nearPlane, sourcePadding, depth + 1, state);
|
|
95
|
+
drawAdaptivePerspectiveTriangle(ctx, image, matrix3D, projection, [ab, vertices[1], bc], [pab, target[1], pbc], nearPlane, sourcePadding, depth + 1, state);
|
|
96
|
+
drawAdaptivePerspectiveTriangle(ctx, image, matrix3D, projection, [ca, bc, vertices[2]], [pca, pbc, target[2]], nearPlane, sourcePadding, depth + 1, state);
|
|
97
|
+
drawAdaptivePerspectiveTriangle(ctx, image, matrix3D, projection, [ab, bc, ca], [pab, pbc, pca], nearPlane, sourcePadding, depth + 1, state);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (state.triangleCount >= state.maxTriangles) return;
|
|
101
|
+
state.triangleCount++;
|
|
102
|
+
drawPerspectiveTriangle(ctx, image,
|
|
103
|
+
vertices.map(function (point) { return [point.x + sourcePadding, point.y + sourcePadding]; }), target);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function drawClippedPerspectiveTriangle(ctx, image, matrix3D, projection, sourcePoints, nearPlane, sourcePadding, state) {
|
|
107
|
+
var polygon = sourcePoints.map(function (point) {
|
|
108
|
+
return { x: point[0], y: point[1], denominator: matrix3DProjectionDenominator(matrix3D, projection, point[0], point[1]) };
|
|
109
|
+
});
|
|
110
|
+
if (polygon.some(function (point) { return point.denominator == null || !Number.isFinite(point.denominator); })) return;
|
|
111
|
+
var clipped = [];
|
|
112
|
+
for (var i = 0; i < polygon.length; i++) {
|
|
113
|
+
var a = polygon[i], b = polygon[(i + 1) % polygon.length];
|
|
114
|
+
var aInside = a.denominator >= nearPlane, bInside = b.denominator >= nearPlane;
|
|
115
|
+
if (aInside) clipped.push(a);
|
|
116
|
+
if (aInside !== bInside) {
|
|
117
|
+
var denominatorDelta = b.denominator - a.denominator;
|
|
118
|
+
if (Math.abs(denominatorDelta) < 1e-12) continue;
|
|
119
|
+
var t = (nearPlane - a.denominator) / denominatorDelta;
|
|
120
|
+
clipped.push({
|
|
121
|
+
x: a.x + (b.x - a.x) * t,
|
|
122
|
+
y: a.y + (b.y - a.y) * t,
|
|
123
|
+
denominator: nearPlane,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (clipped.length < 3) return;
|
|
128
|
+
var first = clipped[0];
|
|
129
|
+
for (var fan = 1; fan + 1 < clipped.length; fan++) {
|
|
130
|
+
var vertices = [first, clipped[fan], clipped[fan + 1]];
|
|
131
|
+
var target = vertices.map(function (point) {
|
|
132
|
+
return matrix3DProjectPoint(matrix3D, projection, point.x, point.y);
|
|
133
|
+
});
|
|
134
|
+
if (target.some(function (point) { return !point || !Number.isFinite(point[0]) || !Number.isFinite(point[1]); })) continue;
|
|
135
|
+
drawAdaptivePerspectiveTriangle(ctx, image, matrix3D, projection, vertices, target, nearPlane, sourcePadding, 0, state);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function drawPerspectiveTriangle(ctx, image, source, target) {
|
|
140
|
+
var sx0 = source[0][0], sy0 = source[0][1];
|
|
141
|
+
var sx1 = source[1][0], sy1 = source[1][1];
|
|
142
|
+
var sx2 = source[2][0], sy2 = source[2][1];
|
|
143
|
+
var tx0 = target[0][0], ty0 = target[0][1];
|
|
144
|
+
var tx1 = target[1][0], ty1 = target[1][1];
|
|
145
|
+
var tx2 = target[2][0], ty2 = target[2][1];
|
|
146
|
+
var det = (sx1 - sx0) * (sy2 - sy0) - (sx2 - sx0) * (sy1 - sy0);
|
|
147
|
+
if (Math.abs(det) < 0.000001) return;
|
|
148
|
+
var a = ((tx1 - tx0) * (sy2 - sy0) - (tx2 - tx0) * (sy1 - sy0)) / det;
|
|
149
|
+
var c = ((tx2 - tx0) * (sx1 - sx0) - (tx1 - tx0) * (sx2 - sx0)) / det;
|
|
150
|
+
var b = ((ty1 - ty0) * (sy2 - sy0) - (ty2 - ty0) * (sy1 - sy0)) / det;
|
|
151
|
+
var d = ((ty2 - ty0) * (sx1 - sx0) - (ty1 - ty0) * (sx2 - sx0)) / det;
|
|
152
|
+
var e = tx0 - a * sx0 - c * sy0;
|
|
153
|
+
var f = ty0 - b * sx0 - d * sy0;
|
|
154
|
+
var centerX = (tx0 + tx1 + tx2) / 3, centerY = (ty0 + ty1 + ty2) / 3;
|
|
155
|
+
function expand(x, y) {
|
|
156
|
+
var dx = x - centerX, dy = y - centerY, length = Math.sqrt(dx * dx + dy * dy) || 1;
|
|
157
|
+
return [x + dx / length * 0.6, y + dy / length * 0.6];
|
|
158
|
+
}
|
|
159
|
+
var p0 = expand(tx0, ty0), p1 = expand(tx1, ty1), p2 = expand(tx2, ty2);
|
|
160
|
+
ctx.save();
|
|
161
|
+
try {
|
|
162
|
+
ctx.beginPath();
|
|
163
|
+
ctx.moveTo(p0[0], p0[1]); ctx.lineTo(p1[0], p1[1]); ctx.lineTo(p2[0], p2[1]); ctx.closePath();
|
|
164
|
+
ctx.clip();
|
|
165
|
+
ctx.transform(a, b, c, d, e, f);
|
|
166
|
+
ctx.drawImage(image, 0, 0);
|
|
167
|
+
} catch (_) {}
|
|
168
|
+
ctx.restore();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export var perspectiveSourceCanvasCache = typeof WeakMap !== 'undefined' ? new WeakMap() : null;
|
|
172
|
+
export function renderPerspectiveNode(ctx, node, depth, inheritedProjection, matrix3D, projection, maskPass, skipNodeAlpha) {
|
|
173
|
+
var width = ctx.canvas && (ctx.canvas.width | 0), height = ctx.canvas && (ctx.canvas.height | 0);
|
|
174
|
+
if (!(width > 0 && height > 0)) return false;
|
|
175
|
+
var pad = 4;
|
|
176
|
+
var source = perspectiveSourceCanvasCache && perspectiveSourceCanvasCache.get(node);
|
|
177
|
+
if (source && (source.width !== width + pad * 2 || source.height !== height + pad * 2)) source = null;
|
|
178
|
+
if (!source) {
|
|
179
|
+
source = makeEffectCanvas({ width: width + pad * 2, height: height + pad * 2 });
|
|
180
|
+
if (source && perspectiveSourceCanvasCache) perspectiveSourceCanvasCache.set(node, source);
|
|
181
|
+
}
|
|
182
|
+
if (!source) return false;
|
|
183
|
+
var sourceCtx = source.getContext('2d');
|
|
184
|
+
if (!sourceCtx) return false;
|
|
185
|
+
if (sourceCtx.setTransform) sourceCtx.setTransform(1, 0, 0, 1, 0, 0);
|
|
186
|
+
if (sourceCtx.clearRect) sourceCtx.clearRect(0, 0, source.width, source.height);
|
|
187
|
+
sourceCtx.save();
|
|
188
|
+
sourceCtx.translate(pad, pad);
|
|
189
|
+
renderDisplayTree(sourceCtx, node, depth, true, inheritedProjection, true, maskPass, false, null, skipNodeAlpha);
|
|
190
|
+
sourceCtx.restore();
|
|
191
|
+
|
|
192
|
+
// Subdivide only where the exact projective midpoint differs from the
|
|
193
|
+
// triangle's affine mapping by more than half a pixel. The depth and
|
|
194
|
+
// triangle limits bound work for transforms near the camera plane.
|
|
195
|
+
var nearPlane = matrix3DNearPlane(projection);
|
|
196
|
+
var state = { tolerance: 0.5, maxDepth: 5, maxTriangles: 4096, triangleCount: 0 };
|
|
197
|
+
var left = -pad, top = -pad, right = width + pad, bottom = height + pad;
|
|
198
|
+
drawClippedPerspectiveTriangle(ctx, source, matrix3D, projection,
|
|
199
|
+
[[left, top], [right, top], [right, bottom]], nearPlane, pad, state);
|
|
200
|
+
drawClippedPerspectiveTriangle(ctx, source, matrix3D, projection,
|
|
201
|
+
[[left, top], [right, bottom], [left, bottom]], nearPlane, pad, state);
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export function setCanvasMatrix(ctx, matrix) {
|
|
206
|
+
ctx.setTransform(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function maskCanvasMatrix(mask, target, canvasMatrix) {
|
|
210
|
+
var detached = !mask.parent && detachedAuthoredMaskBases.get(mask);
|
|
211
|
+
if (!detached && (!mask.parent || mask.parent === target.parent)) return canvasMatrix;
|
|
212
|
+
var maskParent = detached ? detached.matrix.clone() : maskAncestorMatrix(mask.parent);
|
|
213
|
+
var targetParent = maskAncestorMatrix(target.parent);
|
|
214
|
+
if (!maskParent || !targetParent || !(targetParent.a * targetParent.d - targetParent.b * targetParent.c)) {
|
|
215
|
+
return canvasMatrix;
|
|
216
|
+
}
|
|
217
|
+
return maskParent.concat(targetParent.invert()).concat(canvasMatrix);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export function paintStageMask(ctx, mask) {
|
|
221
|
+
ctx.save();
|
|
222
|
+
try {
|
|
223
|
+
setCanvasMatrix(ctx, maskCanvasMatrix(mask, { parent: null }, new Matrix()));
|
|
224
|
+
renderDisplayTree(ctx, mask, 0, true, null, false, true);
|
|
225
|
+
} finally {
|
|
226
|
+
ctx.restore();
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export function hasNativeDisplayContent(node) {
|
|
231
|
+
if (node.visible === false || displayMaskOwners.has(node)) return false;
|
|
232
|
+
var graphics = node.graphics;
|
|
233
|
+
if (graphics && (graphics._cmds?.length || graphics._paint && graphics._paint !== Graphics.prototype._paint) ||
|
|
234
|
+
node._swoofTimelineCanvas || node.bitmapData ||
|
|
235
|
+
node instanceof TextField || node instanceof TextLine || node instanceof SimpleButton) return true;
|
|
236
|
+
return !!node._children?.some(hasNativeDisplayContent);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export function cacheableVector(node) {
|
|
240
|
+
var graphics = node.graphics;
|
|
241
|
+
var requested = node instanceof DisplayObject ? node._cacheAsBitmap : node.cacheAsBitmap;
|
|
242
|
+
return !!(requested && !(node._children && node._children.length) && !(node instanceof SimpleButton) &&
|
|
243
|
+
!(node.transform && node.transform.matrix3D) && !node._swoofTimelineCanvas && !node.bitmapData &&
|
|
244
|
+
!(node instanceof TextField) && graphics && graphics._paint === Graphics.prototype._paint &&
|
|
245
|
+
Array.isArray(graphics._cmds) && graphics._cmds.every(function (command) {
|
|
246
|
+
return !command.fill || typeof command.fill.color === 'string';
|
|
247
|
+
}));
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export function renderAffineCache(ctx, node, depth, cacheVector, skipNodeTransform, skipNodeAlpha) {
|
|
251
|
+
var matrix = node.cacheAsBitmapMatrix;
|
|
252
|
+
if (!(matrix instanceof Matrix) || !ctx.setTransform || !ctx.transform) return false;
|
|
253
|
+
var determinant = matrix.a * matrix.d - matrix.b * matrix.c;
|
|
254
|
+
if (!determinant || !Number.isFinite(determinant)) return false;
|
|
255
|
+
var commands = cacheVector && node.graphics._cmds;
|
|
256
|
+
var bitmap = node instanceof Bitmap && !node._children?.length && !node._swoofTimelineCanvas && node.bitmapData;
|
|
257
|
+
var image = bitmap && (bitmap._lockedDisplayImage || (!bitmap._canvas && bitmap._img));
|
|
258
|
+
if (image && image.complete === false) image = null;
|
|
259
|
+
if ((!commands && !image) || node._swoofStandaloneArtworkId || node.transform?.matrix3D) return false;
|
|
260
|
+
// Strokes with device-space hinting or width need the final stage transform.
|
|
261
|
+
if (commands && commands.some(function (command) {
|
|
262
|
+
var line = command.line;
|
|
263
|
+
return line && (line.pixelHinting || !line.width || line.scaleMode !== 'normal');
|
|
264
|
+
})) return false;
|
|
265
|
+
var local = bitmap ? new Rectangle(0, 0, bitmap.width, bitmap.height) : node.graphics.getBounds();
|
|
266
|
+
var bounds = rasterBounds(local.x, local.y, local.width, local.height, matrix);
|
|
267
|
+
if (!bounds) return false;
|
|
268
|
+
var left = bounds.left - 1, top = bounds.top - 1;
|
|
269
|
+
var width = bounds.right - left + 1, height = bounds.bottom - top + 1;
|
|
270
|
+
if (!canCacheEffectRaster(width, height)) return false;
|
|
271
|
+
var key = ['affine', commands, commands && commands.length, image, width, height, left, top, !!node.smoothing,
|
|
272
|
+
matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty];
|
|
273
|
+
var canvas = effectRasterCache(node, key);
|
|
274
|
+
if (!canvas) {
|
|
275
|
+
canvas = makeEffectCanvas({ width: width, height: height });
|
|
276
|
+
var paint = canvas && canvas.getContext('2d');
|
|
277
|
+
if (!paint) return false;
|
|
278
|
+
setCanvasMatrix(paint, matrix.clone().translate(-left, -top));
|
|
279
|
+
renderDisplayTree(paint, node, depth, true, null, true, false, false, null, true);
|
|
280
|
+
effectRasterCache(node, key, canvas);
|
|
281
|
+
}
|
|
282
|
+
var uncache = new Matrix(1, 0, 0, 1, left, top).concat(matrix.clone().invert());
|
|
283
|
+
if (!skipNodeTransform) uncache.concat(displayObjectMatrix(node));
|
|
284
|
+
ctx.save();
|
|
285
|
+
try {
|
|
286
|
+
ctx.transform(uncache.a, uncache.b, uncache.c, uncache.d, uncache.tx, uncache.ty);
|
|
287
|
+
if (!skipNodeAlpha) ctx.globalAlpha *= Math.max(0, Math.min(1, +node.alpha || 0));
|
|
288
|
+
ctx.imageSmoothingEnabled = bitmap ? !!node.smoothing : true;
|
|
289
|
+
ctx.drawImage(canvas, 0, 0);
|
|
290
|
+
} catch (_) { return false; }
|
|
291
|
+
finally { ctx.restore(); }
|
|
292
|
+
return true;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export function renderLocalDisplacement(ctx, node, depth, projection, matrix, filters, sourceLocal, maskMatrix) {
|
|
296
|
+
var bounds;
|
|
297
|
+
if (node instanceof Bitmap) {
|
|
298
|
+
var data = node.bitmapData;
|
|
299
|
+
if (!data) return false;
|
|
300
|
+
bounds = new Rectangle(0, 0, data.width, data.height);
|
|
301
|
+
} else if (node.graphics?._paint === Graphics.prototype._paint) {
|
|
302
|
+
bounds = node.graphics.getBounds();
|
|
303
|
+
}
|
|
304
|
+
if (!bounds) return false;
|
|
305
|
+
var left = Math.floor(bounds.x), top = Math.floor(bounds.y);
|
|
306
|
+
var width = Math.ceil(bounds.x + bounds.width) - left;
|
|
307
|
+
var height = Math.ceil(bounds.y + bounds.height) - top;
|
|
308
|
+
if (!canCacheEffectRaster(width, height)) return false;
|
|
309
|
+
var canvas = makeEffectCanvas({ width: width, height: height });
|
|
310
|
+
var localCtx = canvas?.getContext('2d', { willReadFrequently: true });
|
|
311
|
+
if (!localCtx?.getImageData || !localCtx.putImageData) return false;
|
|
312
|
+
try {
|
|
313
|
+
localCtx.setTransform(1, 0, 0, 1, -left, -top);
|
|
314
|
+
renderDisplayTree(localCtx, node, depth, true, projection, true, false, sourceLocal);
|
|
315
|
+
for (var filter of filters) {
|
|
316
|
+
if (filter instanceof DisplacementMapFilter) {
|
|
317
|
+
var image = localCtx.getImageData(0, 0, width, height);
|
|
318
|
+
displaceBitmapPixels({ _ctx: localCtx, width: width, height: height }, image.data,
|
|
319
|
+
0, 0, width, height, filter, left, top);
|
|
320
|
+
localCtx.putImageData(image, 0, 0);
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
var effect = bitmapShadowPlan(filter) || bitmapContourPlan(filter);
|
|
324
|
+
var blur = effect?.blur || (filter instanceof BlurFilter ? effectBlurPlan(filter, false) : null);
|
|
325
|
+
var margins = bitmapFilterMargins(blur, effect);
|
|
326
|
+
var paddedWidth = width + margins.left + margins.right;
|
|
327
|
+
var paddedHeight = height + margins.top + margins.bottom;
|
|
328
|
+
if (paddedWidth !== width || paddedHeight !== height) {
|
|
329
|
+
if (!canCacheEffectRaster(paddedWidth, paddedHeight)) return false;
|
|
330
|
+
var expanded = makeEffectCanvas({ width: paddedWidth, height: paddedHeight });
|
|
331
|
+
var expandedCtx = expanded?.getContext('2d', { willReadFrequently: true });
|
|
332
|
+
if (!expandedCtx) return false;
|
|
333
|
+
expandedCtx.drawImage(canvas, margins.left, margins.top);
|
|
334
|
+
canvas = expanded;
|
|
335
|
+
localCtx = expandedCtx;
|
|
336
|
+
// Keep mapPoint in object-local coordinates as the buffer origin moves.
|
|
337
|
+
left -= margins.left;
|
|
338
|
+
top -= margins.top;
|
|
339
|
+
width = paddedWidth;
|
|
340
|
+
height = paddedHeight;
|
|
341
|
+
}
|
|
342
|
+
applyDisplayEffects(localCtx, null, [filter]);
|
|
343
|
+
}
|
|
344
|
+
if (maskMatrix) {
|
|
345
|
+
var maskCanvas = makeEffectCanvas({ width: width, height: height });
|
|
346
|
+
var maskCtx = maskCanvas?.getContext('2d');
|
|
347
|
+
if (!maskCtx) return false;
|
|
348
|
+
var stencilMatrix = maskMatrix.clone();
|
|
349
|
+
stencilMatrix.tx -= left;
|
|
350
|
+
stencilMatrix.ty -= top;
|
|
351
|
+
setCanvasMatrix(maskCtx, stencilMatrix);
|
|
352
|
+
renderDisplayTree(maskCtx, node.mask, depth + 1, true, projection, false, true);
|
|
353
|
+
localCtx.save();
|
|
354
|
+
try {
|
|
355
|
+
localCtx.setTransform(1, 0, 0, 1, 0, 0);
|
|
356
|
+
localCtx.globalCompositeOperation = 'destination-in';
|
|
357
|
+
localCtx.drawImage(maskCanvas, 0, 0);
|
|
358
|
+
} finally { localCtx.restore(); }
|
|
359
|
+
}
|
|
360
|
+
} catch (_) { return false; }
|
|
361
|
+
ctx.save();
|
|
362
|
+
try {
|
|
363
|
+
setCanvasMatrix(ctx, matrix);
|
|
364
|
+
var composite = sourceLocal ? null : blendModeToCanvas(node.blendMode);
|
|
365
|
+
if (composite) ctx.globalCompositeOperation = composite;
|
|
366
|
+
ctx.drawImage(canvas, left, top);
|
|
367
|
+
} catch (_) { return false; }
|
|
368
|
+
finally { ctx.restore(); }
|
|
369
|
+
return true;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
export function renderIsolatedDisplay(ctx, node, depth, projection, skipNodeTransform, ct, filters, sourceLocal, cacheVector, opaque, overlayText) {
|
|
373
|
+
// Rasterize in destination pixels so children outside their parent's local
|
|
374
|
+
// viewport are retained when the parent moves them back onto the stage.
|
|
375
|
+
var canvasMatrix = null;
|
|
376
|
+
if (ctx.getTransform && ctx.setTransform) {
|
|
377
|
+
var current = ctx.getTransform();
|
|
378
|
+
canvasMatrix = new Matrix(current.a, current.b, current.c, current.d, current.e, current.f);
|
|
379
|
+
}
|
|
380
|
+
var groupAlpha = opaque != null || cacheVector;
|
|
381
|
+
var colorAppliesAlpha = !!ct && node instanceof DisplayObject;
|
|
382
|
+
var transform = node.transform;
|
|
383
|
+
var leafMatrix = canvasMatrix && !(transform && transform.matrix3D) &&
|
|
384
|
+
(skipNodeTransform ? canvasMatrix : displayObjectMatrix(node).clone().concat(canvasMatrix));
|
|
385
|
+
var commands = node.graphics && node.graphics._cmds;
|
|
386
|
+
var leaf = !(node._children && node._children.length) && !(node instanceof SimpleButton);
|
|
387
|
+
if (leaf && leafMatrix && !ct && opaque == null &&
|
|
388
|
+
filters?.some(function (filter) { return filter instanceof DisplacementMapFilter; }) &&
|
|
389
|
+
filters.every(function (filter) {
|
|
390
|
+
return filter instanceof DisplacementMapFilter || filter instanceof ColorMatrixFilter || filter instanceof ConvolutionFilter ||
|
|
391
|
+
filter instanceof GlowFilter || filter instanceof BlurFilter || filter instanceof DropShadowFilter ||
|
|
392
|
+
filter instanceof BevelFilter || filter instanceof GradientGlowFilter || filter instanceof GradientBevelFilter;
|
|
393
|
+
})) {
|
|
394
|
+
var mask = node.mask, localMask = null;
|
|
395
|
+
var determinant = leafMatrix.a * leafMatrix.d - leafMatrix.b * leafMatrix.c;
|
|
396
|
+
if (mask && (!mask.parent || mask.parent === node.parent) && !displayMatrix3DInfo(mask) &&
|
|
397
|
+
!authoredMaskSources.has(mask) && !mask._children?.length && Number.isFinite(determinant) && determinant !== 0) {
|
|
398
|
+
localMask = canvasMatrix.clone().concat(leafMatrix.clone().invert());
|
|
399
|
+
}
|
|
400
|
+
if ((!mask || localMask) &&
|
|
401
|
+
renderLocalDisplacement(ctx, node, depth, projection, leafMatrix, filters, sourceLocal, localMask)) return true;
|
|
402
|
+
}
|
|
403
|
+
var cacheKey = null;
|
|
404
|
+
if (cacheVector && leafMatrix && !node.mask && !ct && !(filters && filters.length)) {
|
|
405
|
+
cacheKey = [ctx.canvas.width, ctx.canvas.height, commands, commands.length, opaque, node.width, node.height,
|
|
406
|
+
leafMatrix.a, leafMatrix.b, leafMatrix.c, leafMatrix.d, leafMatrix.tx, leafMatrix.ty];
|
|
407
|
+
}
|
|
408
|
+
var sourceCanvas = cacheKey && effectRasterCache(node, cacheKey);
|
|
409
|
+
if (!sourceCanvas) {
|
|
410
|
+
sourceCanvas = makeEffectCanvas(ctx.canvas);
|
|
411
|
+
var sourceCtx = sourceCanvas && sourceCanvas.getContext('2d', ct || filters?.length ? { willReadFrequently: true } : undefined);
|
|
412
|
+
if (!sourceCtx) return false;
|
|
413
|
+
if (canvasMatrix && sourceCtx.setTransform) setCanvasMatrix(sourceCtx, canvasMatrix);
|
|
414
|
+
var isolatedOverlay = null;
|
|
415
|
+
if (overlayText && opaque != null && !node.mask && !ct && !(filters && filters.length) &&
|
|
416
|
+
(!node.blendMode || node.blendMode === BlendMode.NORMAL)) {
|
|
417
|
+
isolatedOverlay = function (field, fieldCtx) {
|
|
418
|
+
var originalAlpha = fieldCtx.globalAlpha;
|
|
419
|
+
var rootAlpha = node.alpha == null ? 1 : Math.max(0, +node.alpha || 0);
|
|
420
|
+
fieldCtx.globalAlpha *= ctx.globalAlpha * rootAlpha;
|
|
421
|
+
try { return overlayText(field, fieldCtx); }
|
|
422
|
+
finally { fieldCtx.globalAlpha = originalAlpha; }
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
renderDisplayTree(sourceCtx, node, depth, true, projection, skipNodeTransform, false, sourceLocal, isolatedOverlay, groupAlpha || colorAppliesAlpha);
|
|
426
|
+
if (canvasMatrix && sourceCtx.setTransform) sourceCtx.setTransform(1, 0, 0, 1, 0, 0);
|
|
427
|
+
if (opaque != null) {
|
|
428
|
+
var bounds = null;
|
|
429
|
+
if (leafMatrix && leaf) {
|
|
430
|
+
var local = localDisplayBounds(node);
|
|
431
|
+
bounds = rasterBounds(local.x, local.y, local.width, local.height, leafMatrix);
|
|
432
|
+
}
|
|
433
|
+
paintOpaqueBackground(sourceCtx, opaque, bounds);
|
|
434
|
+
}
|
|
435
|
+
applyDisplayEffects(sourceCtx, ct, filters);
|
|
436
|
+
if (node.mask) {
|
|
437
|
+
var maskCanvas = makeEffectCanvas(ctx.canvas);
|
|
438
|
+
var maskCtx = maskCanvas && maskCanvas.getContext('2d');
|
|
439
|
+
if (maskCtx) {
|
|
440
|
+
if (canvasMatrix) setCanvasMatrix(maskCtx, maskCanvasMatrix(node.mask, node, canvasMatrix));
|
|
441
|
+
renderDisplayTree(maskCtx, node.mask, depth + 1, true, projection, false, true);
|
|
442
|
+
sourceCtx.save();
|
|
443
|
+
sourceCtx.globalCompositeOperation = 'destination-in';
|
|
444
|
+
try { sourceCtx.drawImage(maskCanvas, 0, 0); } catch (_) {}
|
|
445
|
+
sourceCtx.restore();
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
if (cacheKey) effectRasterCache(node, cacheKey, sourceCanvas);
|
|
449
|
+
}
|
|
450
|
+
ctx.save();
|
|
451
|
+
if (canvasMatrix) ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
452
|
+
var composite = sourceLocal ? null : blendModeToCanvas(node.blendMode);
|
|
453
|
+
if (composite) ctx.globalCompositeOperation = composite;
|
|
454
|
+
if (groupAlpha && !colorAppliesAlpha && !sourceLocal && node.alpha != null) {
|
|
455
|
+
ctx.globalAlpha *= Math.max(0, Math.min(1, +node.alpha || 0));
|
|
456
|
+
}
|
|
457
|
+
try { ctx.drawImage(sourceCanvas, 0, 0); } catch (_) {}
|
|
458
|
+
ctx.restore();
|
|
459
|
+
return true;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
export var skipTimelinePaintedBranches = false;
|
|
463
|
+
export function renderTimelineStage() {
|
|
464
|
+
var previous = skipTimelinePaintedBranches;
|
|
465
|
+
skipTimelinePaintedBranches = true;
|
|
466
|
+
try { renderDisplayTree.apply(null, arguments); }
|
|
467
|
+
finally { skipTimelinePaintedBranches = previous; }
|
|
468
|
+
}
|
|
469
|
+
export function renderDisplayTree(ctx, node, depth, skipEffects, inheritedProjection, skipNodeTransform, maskPass, sourceLocal, overlayText, skipNodeAlpha, authoredMaskPainted, recordOpaqueGraphics) {
|
|
470
|
+
if (!ctx || !node || node.visible === false || (!sourceLocal && !maskPass && displayMaskOwners.has(node))) return;
|
|
471
|
+
if (skipTimelinePaintedBranches && !maskPass && !sourceLocal && node._swoofTimelinePaintOwner &&
|
|
472
|
+
node._swoofTimelinePaintParent === node.parent) return;
|
|
473
|
+
if ((depth | 0) > 64) return;
|
|
474
|
+
var ct = sourceLocal ? null : (node.transform?._colorTransform || node.colorTransform);
|
|
475
|
+
var filters = node.filters;
|
|
476
|
+
var scroll = node._scrollRect;
|
|
477
|
+
if (scroll && !(scroll.width > 0 && scroll.height > 0)) return;
|
|
478
|
+
var hasCt = ct && (ct.redMultiplier !== 1 || ct.greenMultiplier !== 1 || ct.blueMultiplier !== 1 ||
|
|
479
|
+
ct.redOffset || ct.greenOffset || ct.blueOffset || ct.alphaOffset || ct.alphaMultiplier > 1 ||
|
|
480
|
+
(!(node instanceof DisplayObject) && ct.alphaMultiplier !== 1));
|
|
481
|
+
if (hasCt && node._swoofTimelineBitmapEffects && !hasNativeDisplayContent(node)) hasCt = false;
|
|
482
|
+
if (!skipEffects && !maskPass) {
|
|
483
|
+
var ownsSurface = sourceLocal || !node._swoofTimelineBitmapEffects;
|
|
484
|
+
var vectorLeaf = ownsSurface && !scroll && cacheableVector(node);
|
|
485
|
+
var opaque = ownsSurface ? node.opaqueBackground : null;
|
|
486
|
+
var needsMask = node.mask && (!authoredMaskSources.has(node) || hasNativeDisplayContent(node));
|
|
487
|
+
if (ownsSurface && !scroll && !sourceLocal && node._cacheAsBitmap && opaque == null && !node.mask && !hasCt &&
|
|
488
|
+
!filters?.length && (!node.blendMode || node.blendMode === BlendMode.NORMAL) &&
|
|
489
|
+
renderAffineCache(ctx, node, depth | 0, vectorLeaf, skipNodeTransform, skipNodeAlpha)) return;
|
|
490
|
+
var cacheVector = vectorLeaf && ctx.getTransform && ctx.setTransform && ctx.canvas &&
|
|
491
|
+
canCacheEffectRaster(ctx.canvas.width, ctx.canvas.height);
|
|
492
|
+
if (ownsSurface && !cacheVector) effectRasterCache.delete(node);
|
|
493
|
+
if (opaque != null || cacheVector || needsMask || hasCt || (filters && filters.length) ||
|
|
494
|
+
(!sourceLocal && node.blendMode && node.blendMode !== BlendMode.NORMAL)) {
|
|
495
|
+
if (renderIsolatedDisplay(ctx, node, depth | 0, inheritedProjection, skipNodeTransform, hasCt ? ct : null,
|
|
496
|
+
filters, sourceLocal, cacheVector, opaque, overlayText)) return;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
var x = +node.x || 0;
|
|
500
|
+
var y = +node.y || 0;
|
|
501
|
+
var sx = readDisplayScale(node, 'scaleX');
|
|
502
|
+
var sy = readDisplayScale(node, 'scaleY');
|
|
503
|
+
var rot = ((+node.rotation || 0) * Math.PI) / 180;
|
|
504
|
+
var tm = node.transform && node.transform.matrix;
|
|
505
|
+
var tm3d = node.transform && node.transform.matrix3D;
|
|
506
|
+
var projection = (tm3d && displayMatrix3DInfo(node)?.projection) || node.transform?.perspectiveProjection || inheritedProjection;
|
|
507
|
+
var m3dTransform = tm3d ? matrix3DCanvasTransform(tm3d, projection) : null;
|
|
508
|
+
if (!skipNodeTransform && tm3d && projection) {
|
|
509
|
+
var raw3d = tm3d.rawData || [];
|
|
510
|
+
if ((+raw3d[2] || +raw3d[6] || +raw3d[3] || +raw3d[7]) &&
|
|
511
|
+
renderPerspectiveNode(ctx, node, depth, projection, tm3d, projection, maskPass, skipNodeAlpha)) {
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
if (matrix3DProjectionDenominator(tm3d, projection, 0, 0) < matrix3DNearPlane(projection)) return;
|
|
515
|
+
}
|
|
516
|
+
if (maskPass || sourceLocal || tm3d || node.mask || hasCt ||
|
|
517
|
+
filters && filters.length || node.blendMode && node.blendMode !== BlendMode.NORMAL) {
|
|
518
|
+
overlayText = null;
|
|
519
|
+
recordOpaqueGraphics = null;
|
|
520
|
+
}
|
|
521
|
+
var useMatrix = !!(tm && (node.transform._matrixExplicit || tm.a !== 1 || tm.b || tm.c || tm.d !== 1 || tm.tx || tm.ty));
|
|
522
|
+
var alpha = sourceLocal || maskPass || skipNodeAlpha || node.alpha == null ? 1 : +node.alpha;
|
|
523
|
+
ctx.save();
|
|
524
|
+
ctx.globalAlpha *= alpha > 0 ? Math.min(1, alpha) : 0;
|
|
525
|
+
if (skipNodeTransform) {
|
|
526
|
+
// The caller is rasterizing this subtree before applying its root matrix.
|
|
527
|
+
} else if (m3dTransform) ctx.transform.apply(ctx, m3dTransform);
|
|
528
|
+
else if (useMatrix) ctx.transform(tm.a, tm.b, tm.c, tm.d, tm.tx, tm.ty);
|
|
529
|
+
else if (!skipNodeTransform) {
|
|
530
|
+
ctx.translate(x, y);
|
|
531
|
+
if (rot) ctx.rotate(rot);
|
|
532
|
+
if (sx !== 1 || sy !== 1) ctx.scale(sx, sy);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
if (scroll) {
|
|
536
|
+
ctx.beginPath();
|
|
537
|
+
ctx.rect(0, 0, scroll.width, scroll.height);
|
|
538
|
+
ctx.clip();
|
|
539
|
+
ctx.translate(-Math.trunc(scroll.x), -Math.trunc(scroll.y));
|
|
540
|
+
overlayText = null;
|
|
541
|
+
recordOpaqueGraphics = null;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
if (!maskPass && node._swoofStandaloneArtworkId && ctx.getTransform) {
|
|
545
|
+
var localScale = null;
|
|
546
|
+
if (!skipNodeTransform && !tm3d) {
|
|
547
|
+
if (useMatrix) localScale = tm;
|
|
548
|
+
else if (!rot) localScale = { a: sx, b: 0, c: 0, d: sy };
|
|
549
|
+
}
|
|
550
|
+
authoredMaskSources.get(node)?.paintStandalone(ctx, node, localScale);
|
|
551
|
+
}
|
|
552
|
+
var maskSource = maskPass && !authoredMaskPainted && authoredMaskSources.get(node);
|
|
553
|
+
if (maskSource && ctx.getTransform) {
|
|
554
|
+
maskSource.paint(ctx, node);
|
|
555
|
+
authoredMaskPainted = true;
|
|
556
|
+
}
|
|
557
|
+
if (node._swoofTimelineCanvas) {
|
|
558
|
+
try { ctx.drawImage(node._swoofTimelineCanvas, 0, 0); } catch (_) {}
|
|
559
|
+
}
|
|
560
|
+
if (!node._swoofAuthoredLeaf && node.graphics && typeof node.graphics._paint === 'function') {
|
|
561
|
+
try {
|
|
562
|
+
node.graphics._paint(ctx, maskPass);
|
|
563
|
+
if (recordOpaqueGraphics) recordOpaqueGraphics(node, ctx);
|
|
564
|
+
} catch (_) {}
|
|
565
|
+
}
|
|
566
|
+
if (!node._swoofAuthoredLeaf && (node instanceof Bitmap || (node.bitmapData && node.bitmapData._img))) {
|
|
567
|
+
var data = node.bitmapData;
|
|
568
|
+
var img = bitmapDisplayImage(data) || node._img;
|
|
569
|
+
if (img) {
|
|
570
|
+
ctx.imageSmoothingEnabled = !!node.smoothing;
|
|
571
|
+
try { ctx.drawImage(img, 0, 0); } catch (_) {}
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
if (node instanceof TextField && (!node._swoofAuthoredText || sourceLocal)) {
|
|
575
|
+
try {
|
|
576
|
+
if (!overlayText || node.type !== TextFieldType.INPUT || node.mouseEnabled === false ||
|
|
577
|
+
ctx.globalAlpha <= 0 || !overlayText(node, ctx)) paintTextField(ctx, node);
|
|
578
|
+
else node._swoofLinkRegions = [];
|
|
579
|
+
} catch (_) {}
|
|
580
|
+
}
|
|
581
|
+
if (node instanceof TextLine) paintFteLine(ctx, node);
|
|
582
|
+
if (node instanceof SimpleButton) {
|
|
583
|
+
var state = simpleButtonState(node);
|
|
584
|
+
if (state) renderDisplayTree(ctx, state, (depth | 0) + 1, false, projection, false, maskPass,
|
|
585
|
+
undefined, undefined, undefined, authoredMaskPainted, recordOpaqueGraphics);
|
|
586
|
+
}
|
|
587
|
+
var kids = node._children;
|
|
588
|
+
if (kids && kids.length) {
|
|
589
|
+
var inlineOpacity = maskPass ? null : textLineState.get(node)?.graphicOpacity;
|
|
590
|
+
for (var i = 0; i < kids.length; i++) {
|
|
591
|
+
var child = kids[i], formatAlpha = inlineOpacity?.get(child);
|
|
592
|
+
var parentAlpha = ctx.globalAlpha;
|
|
593
|
+
if (formatAlpha != null) ctx.globalAlpha *= formatAlpha;
|
|
594
|
+
renderDisplayTree(ctx, child, (depth | 0) + 1, false, projection, false, maskPass, undefined,
|
|
595
|
+
node.mouseChildren === false ? null : overlayText, undefined, authoredMaskPainted, recordOpaqueGraphics);
|
|
596
|
+
if (formatAlpha != null) ctx.globalAlpha = parentAlpha;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
ctx.restore();
|
|
600
|
+
if (node === stageSingleton && !(depth | 0) && !maskPass && !sourceLocal && !skipEffects) {
|
|
601
|
+
paintKeyboardFocusRect(ctx);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/** Yellow keyboard focus outline matching AVM1 timeline paint. */
|
|
606
|
+
export function paintKeyboardFocusRect(ctx) {
|
|
607
|
+
var stage = stageSingleton;
|
|
608
|
+
var target = stage && stage.focus;
|
|
609
|
+
if (!ctx || !target || !isStageDescendant(stage, target)) return;
|
|
610
|
+
if (target instanceof TextField) return;
|
|
611
|
+
var fr = target.focusRect;
|
|
612
|
+
if (fr === false) return;
|
|
613
|
+
if (fr == null && stage.stageFocusRect === false) return;
|
|
614
|
+
var bounds = typeof target.getBounds === 'function' ? target.getBounds(stage) : null;
|
|
615
|
+
if (!bounds || !(bounds.width > 0 && bounds.height > 0)) return;
|
|
616
|
+
if (typeof ctx.strokeRect !== 'function') return;
|
|
617
|
+
ctx.save();
|
|
618
|
+
if (typeof ctx.setTransform === 'function') ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
619
|
+
ctx.globalAlpha = 1;
|
|
620
|
+
ctx.strokeStyle = '#ff0';
|
|
621
|
+
ctx.lineWidth = 2;
|
|
622
|
+
ctx.strokeRect(bounds.x, bounds.y, bounds.width, bounds.height);
|
|
623
|
+
ctx.restore();
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
export function simpleButtonState(button) {
|
|
627
|
+
if (!button.enabled) return button.upState;
|
|
628
|
+
var over = _hoveredButton === button;
|
|
629
|
+
if ((Mouse._isDown && _buttonCapture === button && over) ||
|
|
630
|
+
(_keyboardButtonCapture && _keyboardButtonCapture.button === button)) {
|
|
631
|
+
return button.downState || button.overState || button.upState;
|
|
632
|
+
}
|
|
633
|
+
if (!over) return button.upState;
|
|
634
|
+
return button.overState || button.upState;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
export function makeEffectCanvas(reference) {
|
|
638
|
+
var width = reference && reference.width | 0;
|
|
639
|
+
var height = reference && reference.height | 0;
|
|
640
|
+
if (width <= 0 || height <= 0) return null;
|
|
641
|
+
var canvas;
|
|
642
|
+
try {
|
|
643
|
+
if (typeof OffscreenCanvas !== 'undefined') canvas = new OffscreenCanvas(width, height);
|
|
644
|
+
else if (typeof document !== 'undefined') {
|
|
645
|
+
canvas = document.createElement('canvas');
|
|
646
|
+
canvas.width = width; canvas.height = height;
|
|
647
|
+
}
|
|
648
|
+
} catch (_) { return null; }
|
|
649
|
+
return canvas || null;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
export function rasterBounds(x, y, width, height, matrix) {
|
|
653
|
+
if (!(width > 0 && height > 0)) return null;
|
|
654
|
+
var originX = matrix.a * x + matrix.c * y + matrix.tx;
|
|
655
|
+
var originY = matrix.b * x + matrix.d * y + matrix.ty;
|
|
656
|
+
var ax = matrix.a * width, ay = matrix.b * width;
|
|
657
|
+
var bx = matrix.c * height, by = matrix.d * height;
|
|
658
|
+
return {
|
|
659
|
+
left: Math.floor(originX + Math.min(0, ax) + Math.min(0, bx)),
|
|
660
|
+
top: Math.floor(originY + Math.min(0, ay) + Math.min(0, by)),
|
|
661
|
+
right: Math.ceil(originX + Math.max(0, ax) + Math.max(0, bx)),
|
|
662
|
+
bottom: Math.ceil(originY + Math.max(0, ay) + Math.max(0, by)),
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
export var effectRasters = new Map();
|
|
667
|
+
export function canCacheEffectRaster(width, height) {
|
|
668
|
+
return width > 0 && height > 0 && width * height <= 1048576;
|
|
669
|
+
}
|
|
670
|
+
export function effectRasterCache(owner, key, canvas) {
|
|
671
|
+
var record = effectRasters.get(owner);
|
|
672
|
+
if (record && !canvas) {
|
|
673
|
+
if (!key.every(function (value, i) { return value === record.key[i]; })) return null;
|
|
674
|
+
effectRasters.delete(owner);
|
|
675
|
+
effectRasters.set(owner, record);
|
|
676
|
+
return record.canvas;
|
|
677
|
+
}
|
|
678
|
+
if (!canvas || !canCacheEffectRaster(canvas.width, canvas.height)) return null;
|
|
679
|
+
effectRasters.delete(owner);
|
|
680
|
+
if (effectRasters.size >= 8) effectRasters.delete(effectRasters.keys().next().value);
|
|
681
|
+
effectRasters.set(owner, { key: key, canvas: canvas });
|
|
682
|
+
return canvas;
|
|
683
|
+
}
|
|
684
|
+
effectRasterCache.delete = function (owner) { effectRasters.delete(owner); };
|
|
685
|
+
|
|
686
|
+
export function paintOpaqueBackground(ctx, color, bounds) {
|
|
687
|
+
if (color == null) return;
|
|
688
|
+
if (!bounds) {
|
|
689
|
+
var width = ctx.canvas.width, height = ctx.canvas.height;
|
|
690
|
+
var pixels;
|
|
691
|
+
try { pixels = ctx.getImageData(0, 0, width, height).data; } catch (_) { return; }
|
|
692
|
+
bounds = { left: width, top: height, right: 0, bottom: 0 };
|
|
693
|
+
for (var i = 0; i < width * height; i++) {
|
|
694
|
+
if (!pixels[i * 4 + 3]) continue;
|
|
695
|
+
var x = i % width, y = Math.floor(i / width);
|
|
696
|
+
bounds.left = Math.min(bounds.left, x);
|
|
697
|
+
bounds.top = Math.min(bounds.top, y);
|
|
698
|
+
bounds.right = Math.max(bounds.right, x + 1);
|
|
699
|
+
bounds.bottom = y + 1;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
if (!(bounds.right > bounds.left && bounds.bottom > bounds.top)) return;
|
|
703
|
+
if (typeof color === 'object') color = (color.r << 16) | (color.g << 8) | color.b;
|
|
704
|
+
ctx.save();
|
|
705
|
+
ctx.globalCompositeOperation = 'destination-over';
|
|
706
|
+
ctx.fillStyle = cssColor(color, 1);
|
|
707
|
+
ctx.fillRect(bounds.left, bounds.top, bounds.right - bounds.left, bounds.bottom - bounds.top);
|
|
708
|
+
ctx.restore();
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
export function colorMatrixPixels(data, matrix, includeTransparent) {
|
|
712
|
+
for (var p = 0; p < data.length; p += 4) {
|
|
713
|
+
if (!includeTransparent && data[p + 3] === 0) continue;
|
|
714
|
+
var r = data[p], g = data[p + 1], b = data[p + 2], a = data[p + 3];
|
|
715
|
+
data[p] = matrix[0] * r + matrix[1] * g + matrix[2] * b + matrix[3] * a + matrix[4];
|
|
716
|
+
data[p + 1] = matrix[5] * r + matrix[6] * g + matrix[7] * b + matrix[8] * a + matrix[9];
|
|
717
|
+
data[p + 2] = matrix[10] * r + matrix[11] * g + matrix[12] * b + matrix[13] * a + matrix[14];
|
|
718
|
+
data[p + 3] = matrix[15] * r + matrix[16] * g + matrix[17] * b + matrix[18] * a + matrix[19];
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
export function applyPixelColorMatrix(ctx, matrix, bounds) {
|
|
723
|
+
try {
|
|
724
|
+
var left = bounds ? Math.max(0, bounds.left) : 0;
|
|
725
|
+
var top = bounds ? Math.max(0, bounds.top) : 0;
|
|
726
|
+
var right = bounds ? Math.min(ctx.canvas.width, bounds.right) : ctx.canvas.width;
|
|
727
|
+
var bottom = bounds ? Math.min(ctx.canvas.height, bounds.bottom) : ctx.canvas.height;
|
|
728
|
+
if (right <= left || bottom <= top) return;
|
|
729
|
+
var image = ctx.getImageData(left, top, right - left, bottom - top);
|
|
730
|
+
colorMatrixPixels(image.data, matrix, false);
|
|
731
|
+
ctx.putImageData(image, left, top);
|
|
732
|
+
} catch (error) {
|
|
733
|
+
return error?.name === 'SecurityError';
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
export function convolvePixels(data, width, filter, region) {
|
|
738
|
+
var columns = filter.matrixX | 0, rows = filter.matrixY | 0;
|
|
739
|
+
if (columns <= 0 || rows <= 0 || !Array.isArray(filter.matrix)) return;
|
|
740
|
+
var centerX = Math.floor(columns / 2), centerY = Math.floor(rows / 2);
|
|
741
|
+
var taps = [];
|
|
742
|
+
for (var i = 0; i < Math.min(filter.matrix.length, columns * rows); i++) {
|
|
743
|
+
var weight = +filter.matrix[i];
|
|
744
|
+
if (weight) taps.push({ x: i % columns - centerX, y: Math.floor(i / columns) - centerY, weight: weight });
|
|
745
|
+
}
|
|
746
|
+
var fill = filter.defaultColor;
|
|
747
|
+
if (!fill) {
|
|
748
|
+
var color = filter.color >>> 0;
|
|
749
|
+
fill = { r: (color >> 16) & 255, g: (color >> 8) & 255, b: color & 255,
|
|
750
|
+
a: Math.min(1, Math.max(0, +filter.alpha || 0)) * 255 };
|
|
751
|
+
}
|
|
752
|
+
var divisor = +filter.divisor || 1, bias = +filter.bias || 0;
|
|
753
|
+
var left = region.x, top = region.y;
|
|
754
|
+
var right = left + region.width - 1, bottom = top + region.height - 1;
|
|
755
|
+
|
|
756
|
+
var source = data.slice();
|
|
757
|
+
for (var y = top; y <= bottom; y++) {
|
|
758
|
+
for (var x = left; x <= right; x++) {
|
|
759
|
+
var red = 0, green = 0, blue = 0, alpha = 0;
|
|
760
|
+
for (var t = 0; t < taps.length; t++) {
|
|
761
|
+
var tap = taps[t], srcX = x + tap.x, srcY = y + tap.y;
|
|
762
|
+
if (srcX < left || srcX > right || srcY < top || srcY > bottom) {
|
|
763
|
+
if (filter.clamp) {
|
|
764
|
+
srcX = Math.max(left, Math.min(right, srcX));
|
|
765
|
+
srcY = Math.max(top, Math.min(bottom, srcY));
|
|
766
|
+
} else {
|
|
767
|
+
red += fill.r * tap.weight;
|
|
768
|
+
green += fill.g * tap.weight;
|
|
769
|
+
blue += fill.b * tap.weight;
|
|
770
|
+
alpha += fill.a * tap.weight;
|
|
771
|
+
continue;
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
var sample = (srcY * width + srcX) * 4;
|
|
775
|
+
red += source[sample] * tap.weight;
|
|
776
|
+
green += source[sample + 1] * tap.weight;
|
|
777
|
+
blue += source[sample + 2] * tap.weight;
|
|
778
|
+
alpha += source[sample + 3] * tap.weight;
|
|
779
|
+
}
|
|
780
|
+
var target = (y * width + x) * 4;
|
|
781
|
+
data[target] = red / divisor + bias;
|
|
782
|
+
data[target + 1] = green / divisor + bias;
|
|
783
|
+
data[target + 2] = blue / divisor + bias;
|
|
784
|
+
if (!filter.preserveAlpha) data[target + 3] = alpha / divisor + bias;
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
export function displacementSourceWindow(start, length, size, scale, wrap) {
|
|
790
|
+
var radius = Math.ceil(Math.abs(scale) / 2);
|
|
791
|
+
var left = start - radius, right = start + length + radius;
|
|
792
|
+
if (wrap && (left < 0 || right > size)) return [0, size];
|
|
793
|
+
return [Math.max(0, left), Math.min(size, right)];
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
export function displaceBitmapPixels(source, output, left, top, width, height, filter, localOriginX = 0, localOriginY = 0) {
|
|
797
|
+
var map = filter.mapBitmap;
|
|
798
|
+
var chX = bitmapChannelIndex(filter.componentX >>> 0), chY = bitmapChannelIndex(filter.componentY >>> 0);
|
|
799
|
+
var scaleX = chX >= 0 && Number.isFinite(+filter.scaleX) ? +filter.scaleX : 0;
|
|
800
|
+
var scaleY = chY >= 0 && Number.isFinite(+filter.scaleY) ? +filter.scaleY : 0;
|
|
801
|
+
if (!(scaleX || scaleY) || !map?._ensure?.()) return;
|
|
802
|
+
|
|
803
|
+
// Integer bitmap pixels sample their centers. Map coordinates stay in
|
|
804
|
+
// source space, independently of sourceRect clipping and destPoint.
|
|
805
|
+
var point = filter._mapPoint;
|
|
806
|
+
var mapLeft = Math.floor(left + localOriginX + 0.5 - point.x);
|
|
807
|
+
var mapTop = Math.floor(top + localOriginY + 0.5 - point.y);
|
|
808
|
+
var startX = Math.max(0, -mapLeft), startY = Math.max(0, -mapTop);
|
|
809
|
+
var endX = Math.min(width, map.width - mapLeft), endY = Math.min(height, map.height - mapTop);
|
|
810
|
+
if (!(startX < endX && startY < endY)) return;
|
|
811
|
+
var mapWidth = endX - startX;
|
|
812
|
+
var mapData = map._ctx.getImageData(mapLeft + startX, mapTop + startY, mapWidth, endY - startY).data;
|
|
813
|
+
|
|
814
|
+
var wrap = filter.mode === DisplacementMapFilterMode.WRAP;
|
|
815
|
+
var [sx, right] = displacementSourceWindow(left + startX, mapWidth, source.width, scaleX, wrap);
|
|
816
|
+
var [sy, bottom] = displacementSourceWindow(top + startY, endY - startY, source.height, scaleY, wrap);
|
|
817
|
+
var sourceWidth = right - sx;
|
|
818
|
+
var pixels = source._ctx.getImageData(sx, sy, sourceWidth, bottom - sy).data;
|
|
819
|
+
var fill = effectColor(filter.color, filter.alpha);
|
|
820
|
+
function edge(value, size) {
|
|
821
|
+
return wrap ? ((value % size) + size) % size : Math.min(size - 1, Math.max(0, value));
|
|
822
|
+
}
|
|
823
|
+
for (var row = startY; row < endY; row++) {
|
|
824
|
+
for (var col = startX; col < endX; col++) {
|
|
825
|
+
var mapIndex = ((row - startY) * mapWidth + col - startX) * 4;
|
|
826
|
+
var x = left + col + (scaleX ? (mapData[mapIndex + chX] - 128) * (scaleX / 256) : 0);
|
|
827
|
+
var y = top + row + (scaleY ? (mapData[mapIndex + chY] - 128) * (scaleY / 256) : 0);
|
|
828
|
+
var target = (row * width + col) * 4;
|
|
829
|
+
if (x < -0.5 || y < -0.5 || x > source.width - 0.5 || y > source.height - 0.5) {
|
|
830
|
+
if (filter.mode === DisplacementMapFilterMode.IGNORE) continue;
|
|
831
|
+
if (filter.mode === DisplacementMapFilterMode.COLOR) {
|
|
832
|
+
output[target] = fill.r; output[target + 1] = fill.g; output[target + 2] = fill.b;
|
|
833
|
+
output[target + 3] = fill.a * 255;
|
|
834
|
+
continue;
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
var baseX = Math.floor(x), baseY = Math.floor(y);
|
|
838
|
+
var fx = x - baseX, fy = y - baseY;
|
|
839
|
+
var red = 0, green = 0, blue = 0, alpha = 0;
|
|
840
|
+
for (var iy = 0; iy < 2; iy++) {
|
|
841
|
+
var sampleY = edge(baseY + iy, source.height) - sy;
|
|
842
|
+
for (var ix = 0; ix < 2; ix++) {
|
|
843
|
+
var weight = (ix ? fx : 1 - fx) * (iy ? fy : 1 - fy);
|
|
844
|
+
if (!weight) continue;
|
|
845
|
+
var sampleX = edge(baseX + ix, source.width) - sx;
|
|
846
|
+
var sample = (sampleY * sourceWidth + sampleX) * 4;
|
|
847
|
+
var opacity = pixels[sample + 3] * weight;
|
|
848
|
+
red += pixels[sample] * opacity;
|
|
849
|
+
green += pixels[sample + 1] * opacity;
|
|
850
|
+
blue += pixels[sample + 2] * opacity;
|
|
851
|
+
alpha += opacity;
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
output[target] = alpha ? red / alpha : 0;
|
|
855
|
+
output[target + 1] = alpha ? green / alpha : 0;
|
|
856
|
+
output[target + 2] = alpha ? blue / alpha : 0;
|
|
857
|
+
output[target + 3] = alpha;
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
export function applyPixelConvolution(ctx, filter) {
|
|
863
|
+
var width = ctx.canvas.width, height = ctx.canvas.height;
|
|
864
|
+
try {
|
|
865
|
+
var image = ctx.getImageData(0, 0, width, height);
|
|
866
|
+
// Display effects sample the visible rectangle, including holes inside it.
|
|
867
|
+
var region = effectPixelRegion(image.data, width, height, 0, 0);
|
|
868
|
+
if (!region) return;
|
|
869
|
+
convolvePixels(image.data, width, filter, region);
|
|
870
|
+
ctx.putImageData(image, 0, 0);
|
|
871
|
+
} catch (_) {}
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
// A centered box kernel with fractional edge taps and transparent padding.
|
|
875
|
+
export function blurRasterAxis(source, destination, length, lines, step, lineStep, radius, offset) {
|
|
876
|
+
var whole = Math.floor(radius), fraction = radius - whole;
|
|
877
|
+
var divisor = 2 * radius + 1;
|
|
878
|
+
for (var line = 0; line < lines; line++) {
|
|
879
|
+
var start = line * lineStep + (offset || 0);
|
|
880
|
+
var sum = 0;
|
|
881
|
+
for (var i = 0; i <= whole && i < length; i++) sum += source[start + i * step];
|
|
882
|
+
for (var i = 0; i < length; i++) {
|
|
883
|
+
var left = i - whole - 1, right = i + whole + 1;
|
|
884
|
+
var edge = fraction && ((left >= 0 ? source[start + left * step] : 0) +
|
|
885
|
+
(right < length ? source[start + right * step] : 0)) * fraction;
|
|
886
|
+
destination[start + i * step] = (sum + edge) / divisor;
|
|
887
|
+
if (i >= whole) sum -= source[start + (i - whole) * step];
|
|
888
|
+
if (right < length) sum += source[start + right * step];
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
export function effectBlurPlan(filter, authored) {
|
|
894
|
+
var quality = authored ? filter.passes : filter.quality;
|
|
895
|
+
return {
|
|
896
|
+
passes: Math.max(0, Math.min(authored ? 31 : 15, quality | 0)),
|
|
897
|
+
radiusX: (Math.max(1, Math.min(255, +filter.blurX || 0)) - 1) / 2,
|
|
898
|
+
radiusY: (Math.max(1, Math.min(255, +filter.blurY || 0)) - 1) / 2,
|
|
899
|
+
};
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
export function effectPixelRegion(data, width, height, padX, padY) {
|
|
903
|
+
var left = width, right = -1, top = height, bottom = -1;
|
|
904
|
+
for (var p = 3; p < data.length; p += 4) {
|
|
905
|
+
if (!data[p]) continue;
|
|
906
|
+
var index = p >> 2, x = index % width, y = Math.floor(index / width);
|
|
907
|
+
left = Math.min(left, x); right = Math.max(right, x);
|
|
908
|
+
top = Math.min(top, y); bottom = y;
|
|
909
|
+
}
|
|
910
|
+
if (right < left) return null;
|
|
911
|
+
left = Math.max(0, left - padX); right = Math.min(width - 1, right + padX);
|
|
912
|
+
top = Math.max(0, top - padY); bottom = Math.min(height - 1, bottom + padY);
|
|
913
|
+
return { x: left, y: top, width: right - left + 1, height: bottom - top + 1 };
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
export function blurEffectRaster(raster, width, height, channels, plan) {
|
|
917
|
+
if (!plan.passes || !(plan.radiusX || plan.radiusY)) return raster;
|
|
918
|
+
var scratch = new Float32Array(raster.length), swap;
|
|
919
|
+
var rowStride = width * channels;
|
|
920
|
+
for (var pass = 0; pass < plan.passes; pass++) {
|
|
921
|
+
if (plan.radiusX) {
|
|
922
|
+
for (var channel = 0; channel < channels; channel++) {
|
|
923
|
+
blurRasterAxis(raster, scratch, width, height, channels, rowStride, plan.radiusX, channel);
|
|
924
|
+
}
|
|
925
|
+
swap = raster; raster = scratch; scratch = swap;
|
|
926
|
+
}
|
|
927
|
+
if (plan.radiusY) {
|
|
928
|
+
for (var channel = 0; channel < channels; channel++) {
|
|
929
|
+
blurRasterAxis(raster, scratch, height, width, rowStride, channels, plan.radiusY, channel);
|
|
930
|
+
}
|
|
931
|
+
swap = raster; raster = scratch; scratch = swap;
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
return raster;
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
export function blurPixels(data, width, height, plan) {
|
|
938
|
+
var region = effectPixelRegion(data, width, height,
|
|
939
|
+
Math.ceil(plan.radiusX) * plan.passes, Math.ceil(plan.radiusY) * plan.passes);
|
|
940
|
+
if (!region) return;
|
|
941
|
+
var raster = new Float32Array(region.width * region.height * 4);
|
|
942
|
+
var i = 0;
|
|
943
|
+
for (var y = region.y; y < region.y + region.height; y++) {
|
|
944
|
+
for (var x = region.x; x < region.x + region.width; x++) {
|
|
945
|
+
var p = (y * width + x) * 4, alpha = data[p + 3] / 255;
|
|
946
|
+
raster[i++] = data[p] * alpha;
|
|
947
|
+
raster[i++] = data[p + 1] * alpha;
|
|
948
|
+
raster[i++] = data[p + 2] * alpha;
|
|
949
|
+
raster[i++] = alpha;
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
raster = blurEffectRaster(raster, region.width, region.height, 4, plan);
|
|
953
|
+
i = 0;
|
|
954
|
+
for (var y = region.y; y < region.y + region.height; y++) {
|
|
955
|
+
for (var x = region.x; x < region.x + region.width; x++) {
|
|
956
|
+
var p = (y * width + x) * 4, alpha = raster[i + 3];
|
|
957
|
+
data[p] = alpha > 0 ? raster[i] / alpha : 0;
|
|
958
|
+
data[p + 1] = alpha > 0 ? raster[i + 1] / alpha : 0;
|
|
959
|
+
data[p + 2] = alpha > 0 ? raster[i + 2] / alpha : 0;
|
|
960
|
+
data[p + 3] = alpha * 255;
|
|
961
|
+
i += 4;
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
return region;
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
export function applyPixelBlur(ctx, filter) {
|
|
968
|
+
var plan = effectBlurPlan(filter, filter.type === 'blur');
|
|
969
|
+
if (!plan.passes || !(plan.radiusX || plan.radiusY)) return;
|
|
970
|
+
try {
|
|
971
|
+
var width = ctx.canvas.width, height = ctx.canvas.height;
|
|
972
|
+
var image = ctx.getImageData(0, 0, width, height);
|
|
973
|
+
var region = blurPixels(image.data, width, height, plan);
|
|
974
|
+
if (region) ctx.putImageData(image, 0, 0, region.x, region.y, region.width, region.height);
|
|
975
|
+
} catch (_) {}
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
export function sampleAlpha(plane, width, height, x, y) {
|
|
979
|
+
var left = Math.floor(x), top = Math.floor(y);
|
|
980
|
+
var fx = x - left, fy = y - top;
|
|
981
|
+
var sum = 0;
|
|
982
|
+
for (var row = 0; row <= 1; row++) {
|
|
983
|
+
var sy = top + row;
|
|
984
|
+
if (sy < 0 || sy >= height) continue;
|
|
985
|
+
for (var col = 0; col <= 1; col++) {
|
|
986
|
+
var sx = left + col;
|
|
987
|
+
if (sx >= 0 && sx < width) sum += plane[sy * width + sx] * (col ? fx : 1 - fx) * (row ? fy : 1 - fy);
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
return sum;
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
export function effectColor(color, alpha) {
|
|
994
|
+
if (color && typeof color === 'object') return { r: color.r, g: color.g, b: color.b, a: color.a / 255 };
|
|
995
|
+
return { r: (color >>> 16) & 255, g: (color >>> 8) & 255, b: color & 255, a: Math.max(0, Math.min(1, +alpha || 0)) };
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
export function effectShadowPlan(filter, kind) {
|
|
999
|
+
var authored = filter.type === 'glow' || filter.type === 'dropShadow';
|
|
1000
|
+
var angle = (+filter.angle || 0) * (authored ? 1 : Math.PI / 180);
|
|
1001
|
+
var distance = kind === 'glow' ? 0 : (+filter.distance || 0);
|
|
1002
|
+
var dx = Math.cos(angle) * distance, dy = Math.sin(angle) * distance;
|
|
1003
|
+
var knockout = !!filter.knockout;
|
|
1004
|
+
var showSource = !knockout && !filter.hideObject && filter.compositeSource !== false;
|
|
1005
|
+
return {
|
|
1006
|
+
blur: effectBlurPlan(filter, authored),
|
|
1007
|
+
dx: Number.isFinite(dx) && Math.abs(dx) > 1e-8 ? dx : 0,
|
|
1008
|
+
dy: Number.isFinite(dy) && Math.abs(dy) > 1e-8 ? dy : 0,
|
|
1009
|
+
inner: !!filter.inner, showSource: showSource, cutOuter: showSource || knockout,
|
|
1010
|
+
color: effectColor(filter.color, filter.alpha),
|
|
1011
|
+
strength: Math.max(0, Math.min(255, +filter.strength || 0)),
|
|
1012
|
+
};
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
export function shadowCoverage(data, width, region, blur) {
|
|
1016
|
+
var coverage = new Float32Array(region.width * region.height);
|
|
1017
|
+
for (var y = 0; y < region.height; y++) {
|
|
1018
|
+
var row = (region.y + y) * width + region.x;
|
|
1019
|
+
for (var x = 0; x < region.width; x++) coverage[y * region.width + x] = data[(row + x) * 4 + 3] / 255;
|
|
1020
|
+
}
|
|
1021
|
+
return blurEffectRaster(coverage, region.width, region.height, 1, blur);
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
export function compositeShadowPixels(data, width, region, coverage, coverageRegion, shadow) {
|
|
1025
|
+
var color = shadow.color;
|
|
1026
|
+
for (var y = region.y; y < region.y + region.height; y++) {
|
|
1027
|
+
var row = y * width;
|
|
1028
|
+
for (var x = region.x; x < region.x + region.width; x++) {
|
|
1029
|
+
var p = (row + x) * 4, sourceAlpha = data[p + 3] / 255;
|
|
1030
|
+
var blur = sampleAlpha(coverage, coverageRegion.width, coverageRegion.height,
|
|
1031
|
+
x - shadow.dx - coverageRegion.x, y - shadow.dy - coverageRegion.y);
|
|
1032
|
+
var signal = shadow.inner ? 1 - blur : blur;
|
|
1033
|
+
var effectAlpha = color.a * Math.max(0, Math.min(1, signal * shadow.strength));
|
|
1034
|
+
// Hidden source may leave the full shadow. Knockout cuts its silhouette.
|
|
1035
|
+
var effectWeight = effectAlpha * (shadow.inner ? sourceAlpha : shadow.cutOuter ? 1 - sourceAlpha : 1);
|
|
1036
|
+
var sourceWeight = shadow.showSource ? sourceAlpha * (shadow.inner ? 1 - effectAlpha : 1) : 0;
|
|
1037
|
+
var alpha = effectWeight + sourceWeight;
|
|
1038
|
+
data[p + 3] = alpha * 255;
|
|
1039
|
+
if (data[p + 3]) {
|
|
1040
|
+
data[p] = (data[p] * sourceWeight + color.r * effectWeight) / alpha;
|
|
1041
|
+
data[p + 1] = (data[p + 1] * sourceWeight + color.g * effectWeight) / alpha;
|
|
1042
|
+
data[p + 2] = (data[p + 2] * sourceWeight + color.b * effectWeight) / alpha;
|
|
1043
|
+
} else data[p] = data[p + 1] = data[p + 2] = 0;
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
export function shiftedBitmapCoverage(ctx, left, top, width, height, dx, dy, blur) {
|
|
1049
|
+
var padX = Math.ceil(blur.radiusX) * blur.passes, padY = Math.ceil(blur.radiusY) * blur.passes;
|
|
1050
|
+
var maskLeft = Math.floor(left - dx) - padX, maskTop = Math.floor(top - dy) - padY;
|
|
1051
|
+
// A separate shifted alpha snapshot avoids allocating the intervening
|
|
1052
|
+
// pixels for large shadow distances and preserves in-place filtering.
|
|
1053
|
+
var maskWidth = Math.ceil(left + width - dx) + padX - maskLeft;
|
|
1054
|
+
var maskHeight = Math.ceil(top + height - dy) + padY - maskTop;
|
|
1055
|
+
var maskRegion = { x: 0, y: 0, width: maskWidth, height: maskHeight };
|
|
1056
|
+
var mask = ctx.getImageData(maskLeft, maskTop, maskWidth, maskHeight);
|
|
1057
|
+
var coverage = shadowCoverage(mask.data, maskWidth, maskRegion, blur);
|
|
1058
|
+
maskRegion.x = maskLeft - left; maskRegion.y = maskTop - top;
|
|
1059
|
+
maskRegion.coverage = coverage;
|
|
1060
|
+
return maskRegion;
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
export function filterBitmapShadow(ctx, data, left, top, width, height, shadow) {
|
|
1064
|
+
if (shadow.showSource && (!shadow.strength || !shadow.color.a)) return;
|
|
1065
|
+
var region = { x: 0, y: 0, width: width, height: height };
|
|
1066
|
+
var maskRegion = shiftedBitmapCoverage(ctx, left, top, width, height, shadow.dx, shadow.dy, shadow.blur);
|
|
1067
|
+
compositeShadowPixels(data, width, region, maskRegion.coverage, maskRegion, shadow);
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
export function applyPixelShadow(ctx, filter, kind) {
|
|
1071
|
+
var shadow = effectShadowPlan(filter, kind), blur = shadow.blur;
|
|
1072
|
+
if (shadow.showSource && (!shadow.strength || !shadow.color.a)) return;
|
|
1073
|
+
try {
|
|
1074
|
+
var width = ctx.canvas.width, height = ctx.canvas.height;
|
|
1075
|
+
var image = ctx.getImageData(0, 0, width, height), data = image.data;
|
|
1076
|
+
var region = effectPixelRegion(data, width, height,
|
|
1077
|
+
Math.ceil(blur.radiusX) * blur.passes + Math.ceil(Math.abs(shadow.dx)),
|
|
1078
|
+
Math.ceil(blur.radiusY) * blur.passes + Math.ceil(Math.abs(shadow.dy)));
|
|
1079
|
+
if (!region) return;
|
|
1080
|
+
var coverage = shadowCoverage(data, width, region, blur);
|
|
1081
|
+
compositeShadowPixels(data, width, region, coverage, region, shadow);
|
|
1082
|
+
ctx.putImageData(image, 0, 0, region.x, region.y, region.width, region.height);
|
|
1083
|
+
} catch (_) {}
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
export function filterGradient(filter, authored) {
|
|
1087
|
+
var colors = filter.colors || [], alphas = filter.alphas || [], ratios = filter.ratios || [];
|
|
1088
|
+
var count = Math.min(colors.length, ratios.length, authored ? colors.length : alphas.length);
|
|
1089
|
+
if (!count) return null;
|
|
1090
|
+
var stops = [];
|
|
1091
|
+
for (var i = 0; i < count; i++) {
|
|
1092
|
+
var color = effectColor(colors[i], alphas[i]);
|
|
1093
|
+
color.ratio = Math.max(0, Math.min(255, +ratios[i] || 0));
|
|
1094
|
+
stops.push(color);
|
|
1095
|
+
}
|
|
1096
|
+
stops.sort(function (a, b) { return a.ratio - b.ratio; });
|
|
1097
|
+
var lut = [], lower = 0;
|
|
1098
|
+
for (var ratio = 0; ratio <= 255; ratio++) {
|
|
1099
|
+
while (lower + 1 < stops.length && stops[lower + 1].ratio <= ratio) lower++;
|
|
1100
|
+
var a = stops[lower], b = stops[lower + 1];
|
|
1101
|
+
if (!b || ratio <= a.ratio) {
|
|
1102
|
+
lut.push(a);
|
|
1103
|
+
continue;
|
|
1104
|
+
}
|
|
1105
|
+
var t = (ratio - a.ratio) / (b.ratio - a.ratio);
|
|
1106
|
+
lut.push({ r: a.r + (b.r - a.r) * t, g: a.g + (b.g - a.g) * t,
|
|
1107
|
+
b: a.b + (b.b - a.b) * t, a: a.a + (b.a - a.a) * t });
|
|
1108
|
+
}
|
|
1109
|
+
return lut;
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
export function effectContourPlan(filter, kind) {
|
|
1113
|
+
var authored = filter.type === kind;
|
|
1114
|
+
var gradient = kind !== 'bevel';
|
|
1115
|
+
var mode = authored ? (filter.onTop ? 'full' : filter.inner ? 'inner' : 'outer') : filter.type;
|
|
1116
|
+
var omitSource = filter.knockout || (authored && filter.compositeSource === false);
|
|
1117
|
+
var angle = (+filter.angle || 0) * (authored ? 1 : Math.PI / 180);
|
|
1118
|
+
var distance = +filter.distance || 0;
|
|
1119
|
+
var dx = Math.cos(angle) * distance, dy = Math.sin(angle) * distance;
|
|
1120
|
+
var strength = Math.max(0, Math.min(255, +filter.strength || 0));
|
|
1121
|
+
var ramp = gradient ? filterGradient(filter, authored) : null;
|
|
1122
|
+
return {
|
|
1123
|
+
dx: Number.isFinite(dx) && Math.abs(dx) > 1e-8 ? dx : 0,
|
|
1124
|
+
dy: Number.isFinite(dy) && Math.abs(dy) > 1e-8 ? dy : 0,
|
|
1125
|
+
mode: mode, inner: mode === 'inner', twoDirections: kind !== 'gradientGlow',
|
|
1126
|
+
omitSource: omitSource, strength: strength, ramp: ramp,
|
|
1127
|
+
enabled: gradient ? strength > 0 && !!ramp : !!omitSource || (strength > 0 && !!distance),
|
|
1128
|
+
highlight: !gradient && effectColor(filter.highlightColor, filter.highlightAlpha),
|
|
1129
|
+
shadow: !gradient && effectColor(filter.shadowColor, filter.shadowAlpha),
|
|
1130
|
+
blur: {
|
|
1131
|
+
radiusX: Math.round(Math.max(0, Math.min(255, +filter.blurX || 0)) / 2),
|
|
1132
|
+
radiusY: Math.round(Math.max(0, Math.min(255, +filter.blurY || 0)) / 2),
|
|
1133
|
+
passes: Math.max(0, Math.min(15, (authored ? filter.passes : filter.quality) | 0)),
|
|
1134
|
+
},
|
|
1135
|
+
};
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
export function compositeContourPixels(data, width, region, shifted, opposite, effect) {
|
|
1139
|
+
var ramp = effect.ramp, mode = effect.mode;
|
|
1140
|
+
for (var y = region.y; y < region.y + region.height; y++) {
|
|
1141
|
+
for (var x = region.x; x < region.x + region.width; x++) {
|
|
1142
|
+
var p = (y * width + x) * 4, sourceAlpha = data[p + 3] / 255;
|
|
1143
|
+
var alphaShift = sampleAlpha(shifted.coverage, shifted.width, shifted.height,
|
|
1144
|
+
x - effect.dx - shifted.x, y - effect.dy - shifted.y);
|
|
1145
|
+
var contour = opposite ? sampleAlpha(opposite.coverage, opposite.width, opposite.height,
|
|
1146
|
+
x + effect.dx - opposite.x, y + effect.dy - opposite.y) - alphaShift : alphaShift;
|
|
1147
|
+
var signal = Math.max(opposite ? -1 : 0, Math.min(1, contour * effect.strength));
|
|
1148
|
+
var ratio = opposite ? 128 - signal * (signal >= 0 ? 128 : 127) : 255 * signal;
|
|
1149
|
+
var color = ramp ? ramp[Math.round(ratio)] : signal >= 0 ? effect.highlight : effect.shadow;
|
|
1150
|
+
var effectAlpha = ramp ? color.a : Math.abs(signal) * color.a;
|
|
1151
|
+
// Work in premultiplied contributions, then return Canvas straight RGB.
|
|
1152
|
+
var effectWeight = effectAlpha * (mode === 'inner' ? sourceAlpha : mode === 'outer' ? 1 - sourceAlpha : 1);
|
|
1153
|
+
var sourceWeight = effect.omitSource ? 0 : sourceAlpha * (mode === 'outer' ? 1 : 1 - effectAlpha);
|
|
1154
|
+
var alpha = effectWeight + sourceWeight;
|
|
1155
|
+
if (alpha) {
|
|
1156
|
+
data[p] = (data[p] * sourceWeight + color.r * effectWeight) / alpha;
|
|
1157
|
+
data[p + 1] = (data[p + 1] * sourceWeight + color.g * effectWeight) / alpha;
|
|
1158
|
+
data[p + 2] = (data[p + 2] * sourceWeight + color.b * effectWeight) / alpha;
|
|
1159
|
+
} else data[p] = data[p + 1] = data[p + 2] = 0;
|
|
1160
|
+
data[p + 3] = alpha * 255;
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
export function filterBitmapContour(ctx, data, left, top, width, height, effect) {
|
|
1166
|
+
if (!effect.enabled) return;
|
|
1167
|
+
var region = { x: 0, y: 0, width: width, height: height };
|
|
1168
|
+
var shifted = shiftedBitmapCoverage(ctx, left, top, width, height, effect.dx, effect.dy, effect.blur);
|
|
1169
|
+
var opposite = null;
|
|
1170
|
+
if (effect.twoDirections) opposite = effect.dx || effect.dy
|
|
1171
|
+
? shiftedBitmapCoverage(ctx, left, top, width, height, -effect.dx, -effect.dy, effect.blur) : shifted;
|
|
1172
|
+
// Match the display compositor's empty-contour behavior even when the
|
|
1173
|
+
// user's gradient assigns opacity to the neutral ramp position.
|
|
1174
|
+
if (effect.ramp?.[opposite ? 128 : 0].a &&
|
|
1175
|
+
!data.some(function (value, index) { return (index & 3) === 3 && value !== 0; }) &&
|
|
1176
|
+
!shifted.coverage.some(Boolean) && !opposite?.coverage.some(Boolean)) return;
|
|
1177
|
+
compositeContourPixels(data, width, region, shifted, opposite, effect);
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
export function applyPixelContourFilter(ctx, filter, kind) {
|
|
1181
|
+
var effect = effectContourPlan(filter, kind), blur = effect.blur;
|
|
1182
|
+
if (!effect.enabled) return;
|
|
1183
|
+
var width = ctx.canvas.width, height = ctx.canvas.height;
|
|
1184
|
+
try {
|
|
1185
|
+
var image = ctx.getImageData(0, 0, width, height), data = image.data;
|
|
1186
|
+
var region = effectPixelRegion(data, width, height,
|
|
1187
|
+
Math.ceil(Math.abs(effect.dx)) + blur.radiusX * blur.passes,
|
|
1188
|
+
Math.ceil(Math.abs(effect.dy)) + blur.radiusY * blur.passes);
|
|
1189
|
+
if (!region) return;
|
|
1190
|
+
region.coverage = shadowCoverage(data, width, region, blur);
|
|
1191
|
+
compositeContourPixels(data, width, region, region, effect.twoDirections ? region : null, effect);
|
|
1192
|
+
ctx.putImageData(image, 0, 0);
|
|
1193
|
+
} catch (_) {}
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
export function applyDisplayEffects(ctx, ct, filters, colorBounds) {
|
|
1197
|
+
if (!ctx || !ctx.canvas) return;
|
|
1198
|
+
var colorReadbackBlocked = false;
|
|
1199
|
+
if (ct && (ct.redMultiplier !== 1 || ct.greenMultiplier !== 1 || ct.blueMultiplier !== 1 ||
|
|
1200
|
+
ct.alphaMultiplier !== 1 || ct.redOffset || ct.greenOffset || ct.blueOffset || ct.alphaOffset)) {
|
|
1201
|
+
colorReadbackBlocked = applyPixelColorMatrix(ctx, [
|
|
1202
|
+
ct.redMultiplier, 0, 0, 0, ct.redOffset,
|
|
1203
|
+
0, ct.greenMultiplier, 0, 0, ct.greenOffset,
|
|
1204
|
+
0, 0, ct.blueMultiplier, 0, ct.blueOffset,
|
|
1205
|
+
0, 0, 0, ct.alphaMultiplier, ct.alphaOffset,
|
|
1206
|
+
], colorBounds) === true;
|
|
1207
|
+
}
|
|
1208
|
+
if (!Array.isArray(filters)) return colorReadbackBlocked;
|
|
1209
|
+
for (var i = 0; i < filters.length; i++) {
|
|
1210
|
+
var filter = filters[i];
|
|
1211
|
+
if (!filter) continue;
|
|
1212
|
+
var kind = filter._swoofFilterKind || filter.type;
|
|
1213
|
+
if (kind === 'blur') {
|
|
1214
|
+
applyPixelBlur(ctx, filter);
|
|
1215
|
+
} else if (kind === 'glow' || kind === 'shadow' || kind === 'dropShadow') {
|
|
1216
|
+
applyPixelShadow(ctx, filter, kind);
|
|
1217
|
+
} else if (kind === 'bevel' || kind === 'gradientGlow' || kind === 'gradientBevel') {
|
|
1218
|
+
applyPixelContourFilter(ctx, filter, kind);
|
|
1219
|
+
} else if (kind === 'convolution') {
|
|
1220
|
+
applyPixelConvolution(ctx, filter);
|
|
1221
|
+
} else if ((!filter.type || filter.type === 'colorMatrix') && Array.isArray(filter.matrix) && filter.matrix.length >= 20) {
|
|
1222
|
+
applyPixelColorMatrix(ctx, filter.matrix);
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
return colorReadbackBlocked;
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
export function blendModeToCanvas(mode) {
|
|
1229
|
+
switch (mode) {
|
|
1230
|
+
case BlendMode.MULTIPLY: case BlendMode.SCREEN: case BlendMode.LIGHTEN:
|
|
1231
|
+
case BlendMode.DARKEN: case BlendMode.DIFFERENCE: case BlendMode.OVERLAY:
|
|
1232
|
+
return mode;
|
|
1233
|
+
case BlendMode.ADD: return 'lighter';
|
|
1234
|
+
case BlendMode.SUBTRACT: return 'difference';
|
|
1235
|
+
case BlendMode.HARDLIGHT: return 'hard-light';
|
|
1236
|
+
case BlendMode.INVERT: return 'difference';
|
|
1237
|
+
default: return null;
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
|