swf2js 0.0.0-stage → 0.1.1
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/LICENSE-GPL +680 -0
- package/README.md +208 -2
- package/package.json +24 -4
- package/src/cli.js +272 -0
- package/src/decoder/button-decoder.js +136 -0
- package/src/decoder/shape-decoder.js +786 -0
- package/src/decoder/symbol-table.js +36 -0
- package/src/decoder/tag-decoder.js +1190 -0
- package/src/decoder/tag-types.js +258 -0
- package/src/emitter/asset-encoder.js +708 -0
- package/src/emitter/bundle-emitter.js +596 -0
- package/src/emitter/embedded-swf.js +246 -0
- package/src/emitter/font-encoder.js +365 -0
- package/src/emitter/timeline-data-codec.js +58 -0
- package/src/model/swf-document.js +59 -0
- package/src/model/transpile-result.js +32 -0
- package/src/parser/bit-reader.js +237 -0
- package/src/parser/decompressor.js +91 -0
- package/src/parser/swf-parser.js +164 -0
- package/src/pretty-printer/pretty-printer.js +137 -0
- package/src/runtime/avm1-runtime.js +4907 -0
- package/src/runtime/avm2-runtime.js +1591 -0
- package/src/runtime/embedded-movie-runtime.js +208 -0
- package/src/runtime/index.js +17 -0
- package/src/runtime/movie-runtime.js +306 -0
- package/src/runtime/swflib.js +14050 -0
- package/src/transpiler/actionscript.js +390 -0
- package/src/transpiler/avm1/avm1-ir.js +205 -0
- package/src/transpiler/avm1/avm1-opcodes.js +149 -0
- package/src/transpiler/avm1/avm1-rewrite.js +291 -0
- package/src/transpiler/avm1/avm1-transpiler.js +2082 -0
- package/src/transpiler/avm2/abc-reader.js +416 -0
- package/src/transpiler/avm2/avm2-transpiler.js +2560 -0
- package/src/transpiler/cfg-structurer.js +876 -0
- package/src/transpiler/timeline/display-list.js +643 -0
- package/src/transpiler/timeline/timeline-codegen.js +6354 -0
- package/src/util/diagnostics.js +50 -0
- package/src/util/logger.js +40 -0
|
@@ -0,0 +1,1190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Tag_Decoder — dispatches raw SWF tags to typed decoder functions,
|
|
3
|
+
* populates the SymbolTable with character definitions.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { BitReader } from '../parser/bit-reader.js';
|
|
7
|
+
import { SymbolTable } from './symbol-table.js';
|
|
8
|
+
import { TAG } from './tag-types.js';
|
|
9
|
+
import { inflateSync } from 'zlib';
|
|
10
|
+
import { decodeGlyphShape } from './shape-decoder.js';
|
|
11
|
+
|
|
12
|
+
/** @import { SwfDocument, RawTag } from '../model/swf-document.js' */
|
|
13
|
+
/** @import { DiagnosticsCollector } from '../util/diagnostics.js' */
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Decode all tags in a SwfDocument.
|
|
17
|
+
* @param {SwfDocument} doc
|
|
18
|
+
* @param {DiagnosticsCollector} diagnostics
|
|
19
|
+
* @returns {{ tags: object[], symbolTable: SymbolTable }}
|
|
20
|
+
*/
|
|
21
|
+
export function decode(doc, diagnostics) {
|
|
22
|
+
const symbolTable = new SymbolTable();
|
|
23
|
+
const tags = [];
|
|
24
|
+
|
|
25
|
+
for (const rawTag of doc.tags) {
|
|
26
|
+
try {
|
|
27
|
+
const decoded = decodeTag(rawTag, doc, diagnostics);
|
|
28
|
+
tags.push(decoded);
|
|
29
|
+
|
|
30
|
+
// Register only character-definition tags (never PlaceObject / control tags)
|
|
31
|
+
if (
|
|
32
|
+
decoded &&
|
|
33
|
+
decoded.characterId !== undefined &&
|
|
34
|
+
CHARACTER_TYPES.has(decoded.type)
|
|
35
|
+
) {
|
|
36
|
+
symbolTable.set(decoded.characterId, decoded);
|
|
37
|
+
}
|
|
38
|
+
} catch (err) {
|
|
39
|
+
diagnostics.error(
|
|
40
|
+
`Failed to decode tag code ${rawTag.code} at offset ${rawTag.offset}: ${err.message}`,
|
|
41
|
+
'decode',
|
|
42
|
+
{ code: rawTag.code, offset: rawTag.offset }
|
|
43
|
+
);
|
|
44
|
+
tags.push({ type: 'Unknown', code: rawTag.code, data: rawTag.data });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Resolve supplementary font and text data after all character definitions.
|
|
49
|
+
for (const tag of tags) {
|
|
50
|
+
if (tag?.type === 'CSMTextSettings' && tag.csmTextSettings) {
|
|
51
|
+
const text = symbolTable.get(tag.textId);
|
|
52
|
+
if (text?.type === 'Text' || text?.type === 'EditText') text.csmTextSettings = tag.csmTextSettings;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (tag?.type !== 'FontSupplementary' || tag.fontId == null) continue;
|
|
56
|
+
const font = symbolTable.get(tag.fontId);
|
|
57
|
+
if (font?.type !== 'Font') continue;
|
|
58
|
+
if (tag.code === TAG.DefineFontName && tag.fullName != null) {
|
|
59
|
+
font.fullName = tag.fullName;
|
|
60
|
+
font.copyright = tag.copyright;
|
|
61
|
+
} else if (tag.code === TAG.DefineFontAlignZones && tag.alignmentZones && font.version === 3) {
|
|
62
|
+
if (tag.alignmentZones.records.length === font.glyphs.length) font.alignmentZones = tag.alignmentZones;
|
|
63
|
+
else diagnostics?.warn('DefineFontAlignZones record count does not match the font', 'decode', { fontId: tag.fontId });
|
|
64
|
+
} else if (tag.codes) {
|
|
65
|
+
if (tag.codes.length && !font.codes?.length) font.codes = tag.codes;
|
|
66
|
+
if (!font.name && tag.name) font.name = tag.name;
|
|
67
|
+
if (font.version === 1) font.fontInfoFlags = tag.flags;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return { tags, symbolTable };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Tag types that define reusable characters (eligible for SymbolTable). */
|
|
75
|
+
const CHARACTER_TYPES = new Set([
|
|
76
|
+
'Shape', 'MorphShape', 'Bitmap', 'Sound', 'Sprite', 'Font',
|
|
77
|
+
'Text', 'EditText', 'Button', 'VideoStream', 'DefineBinaryData',
|
|
78
|
+
]);
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Decode a single raw tag (used for root tags and Sprite children).
|
|
82
|
+
* @param {RawTag} raw
|
|
83
|
+
* @param {SwfDocument|null} [doc]
|
|
84
|
+
* @param {DiagnosticsCollector|null} [diagnostics]
|
|
85
|
+
* @returns {object}
|
|
86
|
+
*/
|
|
87
|
+
export function decodeTag(raw, doc = null, diagnostics = null) {
|
|
88
|
+
return decodeTagInner(raw, doc, diagnostics);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Dispatch a single RawTag to its typed decoder.
|
|
93
|
+
* @param {RawTag} raw
|
|
94
|
+
* @param {SwfDocument|null} doc
|
|
95
|
+
* @param {DiagnosticsCollector|null} diagnostics
|
|
96
|
+
* @returns {object}
|
|
97
|
+
*/
|
|
98
|
+
function decodeTagInner(raw, doc, diagnostics) {
|
|
99
|
+
const r = new BitReader(raw.data);
|
|
100
|
+
|
|
101
|
+
switch (raw.code) {
|
|
102
|
+
// --- Control tags ---
|
|
103
|
+
case TAG.ShowFrame:
|
|
104
|
+
return { type: 'ShowFrame' };
|
|
105
|
+
|
|
106
|
+
case TAG.End:
|
|
107
|
+
return { type: 'End' };
|
|
108
|
+
|
|
109
|
+
case TAG.SetBackgroundColor:
|
|
110
|
+
return { type: 'SetBackgroundColor', color: r.readRGB() };
|
|
111
|
+
|
|
112
|
+
case TAG.FrameLabel: {
|
|
113
|
+
const utf8 = doc?.header?.version >= 6;
|
|
114
|
+
const label = { type: 'FrameLabel', name: r.readString(utf8) };
|
|
115
|
+
if (utf8 && !r.eof && r.readUI8() !== 0) label.namedAnchor = true;
|
|
116
|
+
return label;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
case TAG.DefineSceneAndFrameLabelData:
|
|
120
|
+
return decodeSceneAndFrameLabels(r, doc?.header?.version >= 6);
|
|
121
|
+
|
|
122
|
+
case TAG.FileAttributes:
|
|
123
|
+
return { type: 'FileAttributes', flags: r.readUI32() };
|
|
124
|
+
|
|
125
|
+
case TAG.Metadata:
|
|
126
|
+
return { type: 'Metadata', xml: new TextDecoder().decode(raw.data) };
|
|
127
|
+
|
|
128
|
+
case TAG.Protect:
|
|
129
|
+
return { type: 'Protect' };
|
|
130
|
+
|
|
131
|
+
case TAG.ScriptLimits:
|
|
132
|
+
return { type: 'ScriptLimits', maxRecursionDepth: r.readUI16(), scriptTimeoutSeconds: r.readUI16() };
|
|
133
|
+
|
|
134
|
+
// --- PlaceObject ---
|
|
135
|
+
case TAG.PlaceObject:
|
|
136
|
+
return decodePlaceObject1(r);
|
|
137
|
+
|
|
138
|
+
case TAG.PlaceObject2:
|
|
139
|
+
return decodePlaceObject2(r, doc && doc.header && doc.header.version);
|
|
140
|
+
|
|
141
|
+
case TAG.PlaceObject3:
|
|
142
|
+
return decodePlaceObject3(r, doc && doc.header && doc.header.version);
|
|
143
|
+
|
|
144
|
+
// --- RemoveObject ---
|
|
145
|
+
case TAG.RemoveObject:
|
|
146
|
+
return { type: 'RemoveObject', characterId: r.readUI16(), depth: r.readUI16() };
|
|
147
|
+
|
|
148
|
+
case TAG.RemoveObject2:
|
|
149
|
+
return { type: 'RemoveObject2', depth: r.readUI16() };
|
|
150
|
+
|
|
151
|
+
// --- ActionScript ---
|
|
152
|
+
case TAG.DoAction:
|
|
153
|
+
return { type: 'DoAction', bytecode: raw.data };
|
|
154
|
+
|
|
155
|
+
case TAG.DoInitAction:
|
|
156
|
+
return { type: 'DoInitAction', spriteId: r.readUI16(), bytecode: raw.data.slice(2) };
|
|
157
|
+
|
|
158
|
+
case TAG.DoABC: {
|
|
159
|
+
const flags = r.readUI32();
|
|
160
|
+
const name = r.readString();
|
|
161
|
+
const abcData = raw.data.slice(r.bytePos);
|
|
162
|
+
return { type: 'DoABC', flags, name, abcData };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// --- Shape characters ---
|
|
166
|
+
case TAG.DefineShape:
|
|
167
|
+
return decodeDefineShape(r, raw.data, 1);
|
|
168
|
+
|
|
169
|
+
case TAG.DefineShape2:
|
|
170
|
+
return decodeDefineShape(r, raw.data, 2);
|
|
171
|
+
|
|
172
|
+
case TAG.DefineShape3:
|
|
173
|
+
return decodeDefineShape(r, raw.data, 3);
|
|
174
|
+
|
|
175
|
+
case TAG.DefineShape4:
|
|
176
|
+
return decodeDefineShape(r, raw.data, 4);
|
|
177
|
+
|
|
178
|
+
// --- Morph shapes ---
|
|
179
|
+
case TAG.DefineMorphShape:
|
|
180
|
+
return { type: 'MorphShape', characterId: r.readUI16(), version: 1, rawData: raw.data };
|
|
181
|
+
|
|
182
|
+
case TAG.DefineMorphShape2:
|
|
183
|
+
return { type: 'MorphShape', characterId: r.readUI16(), version: 2, rawData: raw.data };
|
|
184
|
+
|
|
185
|
+
// --- Bitmap characters ---
|
|
186
|
+
case TAG.DefineBits:
|
|
187
|
+
return { type: 'Bitmap', characterId: r.readUI16(), format: 'jpeg1', rawData: raw.data.slice(2) };
|
|
188
|
+
|
|
189
|
+
case TAG.DefineBitsJPEG2:
|
|
190
|
+
return { type: 'Bitmap', characterId: r.readUI16(), format: 'jpeg2', rawData: raw.data.slice(2) };
|
|
191
|
+
|
|
192
|
+
case TAG.DefineBitsJPEG3: {
|
|
193
|
+
const characterId = r.readUI16();
|
|
194
|
+
const alphaDataOffset = r.readUI32();
|
|
195
|
+
const imageStart = r.bytePos;
|
|
196
|
+
const jpegData = raw.data.slice(imageStart, imageStart + alphaDataOffset);
|
|
197
|
+
const alphaData = raw.data.slice(imageStart + alphaDataOffset);
|
|
198
|
+
return { type: 'Bitmap', characterId, format: 'jpeg3', rawData: jpegData, alphaData };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
case TAG.DefineBitsJPEG4: {
|
|
202
|
+
const characterId = r.readUI16();
|
|
203
|
+
const alphaDataOffset = r.readUI32();
|
|
204
|
+
const deblockParam = r.readUI16();
|
|
205
|
+
const imageStart = r.bytePos;
|
|
206
|
+
const jpegData = raw.data.slice(imageStart, imageStart + alphaDataOffset);
|
|
207
|
+
const alphaData = raw.data.slice(imageStart + alphaDataOffset);
|
|
208
|
+
return { type: 'Bitmap', characterId, format: 'jpeg4', rawData: jpegData, alphaData, deblockParam };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
case TAG.DefineBitsLossless:
|
|
212
|
+
return decodeDefineBitsLossless(r, raw.data, 1, diagnostics);
|
|
213
|
+
|
|
214
|
+
case TAG.DefineBitsLossless2:
|
|
215
|
+
return decodeDefineBitsLossless(r, raw.data, 2, diagnostics);
|
|
216
|
+
|
|
217
|
+
case TAG.JPEGTables:
|
|
218
|
+
return { type: 'JPEGTables', data: raw.data };
|
|
219
|
+
|
|
220
|
+
// --- Sound ---
|
|
221
|
+
case TAG.DefineSound:
|
|
222
|
+
return decodeDefineSound(r, raw.data);
|
|
223
|
+
|
|
224
|
+
case TAG.StartSound:
|
|
225
|
+
return { type: 'StartSound', soundId: r.readUI16(), soundInfo: decodeSoundInfo(r) };
|
|
226
|
+
|
|
227
|
+
case TAG.StartSound2:
|
|
228
|
+
return { type: 'StartSound2', soundClassName: r.readString(), soundInfo: decodeSoundInfo(r) };
|
|
229
|
+
|
|
230
|
+
case TAG.SoundStreamHead:
|
|
231
|
+
case TAG.SoundStreamHead2: {
|
|
232
|
+
const first = r.readUI8();
|
|
233
|
+
const second = r.readUI8();
|
|
234
|
+
const streamCompression = second >> 4;
|
|
235
|
+
const sampleCount = r.readUI16();
|
|
236
|
+
const latencySeek = streamCompression === 2 ? r.readSI16() : null;
|
|
237
|
+
return {
|
|
238
|
+
type: 'SoundStreamHead',
|
|
239
|
+
playbackRate: (first >> 2) & 3,
|
|
240
|
+
playback16bit: !!(first & 2),
|
|
241
|
+
playbackStereo: !!(first & 1),
|
|
242
|
+
streamCompression,
|
|
243
|
+
streamRate: (second >> 2) & 3,
|
|
244
|
+
stream16bit: !!(second & 2),
|
|
245
|
+
streamStereo: !!(second & 1),
|
|
246
|
+
sampleCount,
|
|
247
|
+
latencySeek,
|
|
248
|
+
rawData: raw.data,
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
case TAG.SoundStreamBlock:
|
|
253
|
+
return { type: 'SoundStreamBlock', rawData: raw.data };
|
|
254
|
+
|
|
255
|
+
// --- Sprite ---
|
|
256
|
+
case TAG.DefineSprite:
|
|
257
|
+
return decodeDefineSprite(r, raw.data, doc);
|
|
258
|
+
|
|
259
|
+
// --- Font ---
|
|
260
|
+
case TAG.DefineFont:
|
|
261
|
+
return decodeDefineFont1(r, raw.data);
|
|
262
|
+
|
|
263
|
+
case TAG.DefineFont2:
|
|
264
|
+
return decodeDefineFont2(r, raw.data, 2, doc?.header?.version || 6);
|
|
265
|
+
|
|
266
|
+
case TAG.DefineFont3:
|
|
267
|
+
return decodeDefineFont2(r, raw.data, 3, 8);
|
|
268
|
+
|
|
269
|
+
case TAG.DefineFont4:
|
|
270
|
+
return decodeDefineFont4(r, raw.data, diagnostics);
|
|
271
|
+
|
|
272
|
+
case TAG.DefineFontInfo:
|
|
273
|
+
return decodeFontInfo(r, raw.data, false, doc?.header?.version || 6);
|
|
274
|
+
|
|
275
|
+
case TAG.DefineFontInfo2:
|
|
276
|
+
return decodeFontInfo(r, raw.data, true, 6);
|
|
277
|
+
|
|
278
|
+
case TAG.DefineFontName:
|
|
279
|
+
return decodeFontNameMetadata(r, raw.data, diagnostics);
|
|
280
|
+
|
|
281
|
+
case TAG.DefineFontAlignZones:
|
|
282
|
+
return decodeFontAlignZones(r, raw.data, diagnostics);
|
|
283
|
+
|
|
284
|
+
case TAG.CSMTextSettings:
|
|
285
|
+
return decodeCSMTextSettings(raw.data, diagnostics);
|
|
286
|
+
|
|
287
|
+
// --- Text ---
|
|
288
|
+
case TAG.DefineText:
|
|
289
|
+
return decodeDefineText(r, raw.data, 1);
|
|
290
|
+
|
|
291
|
+
case TAG.DefineText2:
|
|
292
|
+
return decodeDefineText(r, raw.data, 2);
|
|
293
|
+
|
|
294
|
+
case TAG.DefineEditText:
|
|
295
|
+
return decodeDefineEditText(r, raw.data, doc?.header?.version >= 6);
|
|
296
|
+
|
|
297
|
+
// --- Button ---
|
|
298
|
+
case TAG.DefineButton:
|
|
299
|
+
return { type: 'Button', characterId: r.readUI16(), version: 1, rawData: raw.data };
|
|
300
|
+
|
|
301
|
+
case TAG.DefineButton2:
|
|
302
|
+
return { type: 'Button', characterId: r.readUI16(), version: 2, rawData: raw.data };
|
|
303
|
+
|
|
304
|
+
case TAG.DefineButtonSound:
|
|
305
|
+
return decodeDefineButtonSound(r);
|
|
306
|
+
|
|
307
|
+
case TAG.DefineButtonCxform: {
|
|
308
|
+
const buttonId = r.readUI16();
|
|
309
|
+
const colorTransforms = [];
|
|
310
|
+
while (!r.eof) colorTransforms.push(readColorTransform(r, false));
|
|
311
|
+
return { type: 'ButtonColorTransforms', buttonId, colorTransforms };
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// --- Video ---
|
|
315
|
+
case TAG.DefineVideoStream: {
|
|
316
|
+
const characterId = r.readUI16();
|
|
317
|
+
const frameCount = r.readUI16();
|
|
318
|
+
const width = r.readUI16();
|
|
319
|
+
const height = r.readUI16();
|
|
320
|
+
r.readUI8(); // video flags
|
|
321
|
+
const codec = r.readUI8();
|
|
322
|
+
return { type: 'VideoStream', characterId, frameCount, width, height, codec };
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
case TAG.VideoFrame:
|
|
326
|
+
return { type: 'VideoFrame', streamId: r.readUI16(), frameNum: r.readUI16(), rawData: raw.data.slice(4) };
|
|
327
|
+
|
|
328
|
+
// --- Misc ---
|
|
329
|
+
case TAG.ExportAssets: {
|
|
330
|
+
// UI16 count, then (UI16 id, STRING name)* — linkage names for attachMovie
|
|
331
|
+
const count = r.readUI16();
|
|
332
|
+
/** @type {{ id: number, name: string }[]} */
|
|
333
|
+
const assets = [];
|
|
334
|
+
for (let i = 0; i < count; i++) {
|
|
335
|
+
const id = r.readUI16();
|
|
336
|
+
const name = r.readString();
|
|
337
|
+
assets.push({ id, name });
|
|
338
|
+
}
|
|
339
|
+
return { type: 'ExportAssets', assets };
|
|
340
|
+
}
|
|
341
|
+
case TAG.ImportAssets:
|
|
342
|
+
case TAG.ImportAssets2: {
|
|
343
|
+
const url = r.readString();
|
|
344
|
+
if (raw.code === TAG.ImportAssets2) {
|
|
345
|
+
r.readUI8();
|
|
346
|
+
r.readUI8();
|
|
347
|
+
}
|
|
348
|
+
const count = r.readUI16();
|
|
349
|
+
const assets = [];
|
|
350
|
+
for (let i = 0; i < count; i++) assets.push({ id: r.readUI16(), name: r.readString() });
|
|
351
|
+
return { type: raw.code === TAG.ImportAssets2 ? 'ImportAssets2' : 'ImportAssets', url, assets };
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
case TAG.SymbolClass:
|
|
355
|
+
return { type: 'SymbolClass', rawData: raw.data };
|
|
356
|
+
|
|
357
|
+
case TAG.DefineSceneAndFrameLabelData:
|
|
358
|
+
return { type: 'DefineSceneAndFrameLabelData', rawData: raw.data };
|
|
359
|
+
|
|
360
|
+
case TAG.DefineScalingGrid:
|
|
361
|
+
return { type: 'DefineScalingGrid', characterId: r.readUI16(), splitter: r.readRect(), rawData: raw.data };
|
|
362
|
+
|
|
363
|
+
case TAG.DefineBinaryData: {
|
|
364
|
+
const characterId = r.readUI16();
|
|
365
|
+
r.readUI32(); // reserved
|
|
366
|
+
return { type: 'DefineBinaryData', characterId, rawData: r.readBytes(raw.data.length - 6) };
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
case TAG.SetTabIndex:
|
|
370
|
+
return { type: 'SetTabIndex', depth: r.readUI16(), tabIndex: r.readUI16() };
|
|
371
|
+
|
|
372
|
+
case TAG.EnableDebugger2:
|
|
373
|
+
case TAG.EnableTelemetry:
|
|
374
|
+
return { type: 'Misc', code: raw.code, rawData: raw.data };
|
|
375
|
+
|
|
376
|
+
default:
|
|
377
|
+
diagnostics.warn(
|
|
378
|
+
`Unknown tag code ${raw.code} at offset ${raw.offset} (${raw.length} bytes)`,
|
|
379
|
+
'decode',
|
|
380
|
+
{ code: raw.code, offset: raw.offset }
|
|
381
|
+
);
|
|
382
|
+
return { type: 'Unknown', code: raw.code, data: raw.data };
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// --- Individual tag decoders ---
|
|
387
|
+
|
|
388
|
+
function decodePlaceObject1(r) {
|
|
389
|
+
const characterId = r.readUI16();
|
|
390
|
+
const depth = r.readUI16();
|
|
391
|
+
const matrix = r.readMatrix();
|
|
392
|
+
const colorTransform = r.eof ? undefined : readColorTransform(r, false);
|
|
393
|
+
return {
|
|
394
|
+
type: 'PlaceObject',
|
|
395
|
+
characterId,
|
|
396
|
+
depth,
|
|
397
|
+
matrix,
|
|
398
|
+
colorTransform,
|
|
399
|
+
move: false,
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function decodePlaceObject2(r, swfVersion) {
|
|
404
|
+
const flags = r.readUI8();
|
|
405
|
+
const move = !!(flags & 0x01);
|
|
406
|
+
const hasCharId = !!(flags & 0x02);
|
|
407
|
+
const hasMatrix = !!(flags & 0x04);
|
|
408
|
+
const hasCxform = !!(flags & 0x08);
|
|
409
|
+
const hasRatio = !!(flags & 0x10);
|
|
410
|
+
const hasName = !!(flags & 0x20);
|
|
411
|
+
const hasClip = !!(flags & 0x40);
|
|
412
|
+
const hasActions = !!(flags & 0x80);
|
|
413
|
+
|
|
414
|
+
const depth = r.readUI16();
|
|
415
|
+
const characterId = hasCharId ? r.readUI16() : undefined;
|
|
416
|
+
const matrix = hasMatrix ? r.readMatrix() : undefined;
|
|
417
|
+
const colorTransform = hasCxform ? readColorTransform(r, true) : undefined;
|
|
418
|
+
const ratio = hasRatio ? r.readUI16() : undefined;
|
|
419
|
+
const name = hasName ? r.readString() : undefined;
|
|
420
|
+
const clipDepth = hasClip ? r.readUI16() : undefined;
|
|
421
|
+
let clipActions;
|
|
422
|
+
if (hasActions) {
|
|
423
|
+
try { clipActions = decodeClipActions(r, swfVersion); }
|
|
424
|
+
catch (_) { clipActions = undefined; }
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
return {
|
|
428
|
+
type: 'PlaceObject2', depth, characterId, matrix, colorTransform, ratio, clipDepth, move, name, clipActions,
|
|
429
|
+
...(clipActions?.length ? { clipActionTagLength: r.length } : {}),
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
function decodePlaceObject3(r, swfVersion) {
|
|
434
|
+
const flags1 = r.readUI8();
|
|
435
|
+
const flags2 = r.readUI8();
|
|
436
|
+
const move = !!(flags1 & 0x01);
|
|
437
|
+
const hasCharId = !!(flags1 & 0x02);
|
|
438
|
+
const hasMatrix = !!(flags1 & 0x04);
|
|
439
|
+
const hasCxform = !!(flags1 & 0x08);
|
|
440
|
+
const hasRatio = !!(flags1 & 0x10);
|
|
441
|
+
const hasName = !!(flags1 & 0x20);
|
|
442
|
+
const hasClip = !!(flags1 & 0x40);
|
|
443
|
+
const hasActions = !!(flags1 & 0x80);
|
|
444
|
+
|
|
445
|
+
const depth = r.readUI16();
|
|
446
|
+
const hasImage = !!(flags2 & 0x10);
|
|
447
|
+
// Authored bitmap placements can supply the image by character ID.
|
|
448
|
+
const hasClassName = (flags2 & 0x08) || (hasImage && !hasCharId);
|
|
449
|
+
const className = hasClassName ? r.readString() : undefined;
|
|
450
|
+
const characterId = hasCharId ? r.readUI16() : undefined;
|
|
451
|
+
const matrix = hasMatrix ? r.readMatrix() : undefined;
|
|
452
|
+
const colorTransform = hasCxform ? readColorTransform(r, true) : undefined;
|
|
453
|
+
const ratio = hasRatio ? r.readUI16() : undefined;
|
|
454
|
+
const name = hasName ? r.readString() : undefined;
|
|
455
|
+
const clipDepth = hasClip ? r.readUI16() : undefined;
|
|
456
|
+
const filters = flags2 & 0x01 ? decodeFilters(r) : undefined;
|
|
457
|
+
const blendMode = flags2 & 0x02 ? r.readUI8() : undefined;
|
|
458
|
+
const cacheAsBitmap = flags2 & 0x04 ? !!r.readUI8() : undefined;
|
|
459
|
+
const visible = flags2 & 0x20 ? !!r.readUI8() : undefined;
|
|
460
|
+
const opaqueBackground = flags2 & 0x40 ? r.readRGBA() : undefined;
|
|
461
|
+
let clipActions;
|
|
462
|
+
if (hasActions) {
|
|
463
|
+
try { clipActions = decodeClipActions(r, swfVersion); }
|
|
464
|
+
catch (_) { clipActions = undefined; }
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
return {
|
|
468
|
+
type: 'PlaceObject3', depth, className, hasImage, characterId, matrix, colorTransform, ratio, clipDepth,
|
|
469
|
+
filters, blendMode, cacheAsBitmap, visible, opaqueBackground, move, name, clipActions,
|
|
470
|
+
...(clipActions?.length ? { clipActionTagLength: r.length } : {}),
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* SWF CLIPACTIONS → [{ eventFlags, keyCode?, bytecodeOffset, bytecode }]
|
|
476
|
+
* bytecodeOffset is relative to the start of the PlaceObject tag body.
|
|
477
|
+
* Event flag bits (SWF6+ UI32): press=0x400, release=0x800, enterFrame=0x2, …
|
|
478
|
+
* @param {BitReader} r
|
|
479
|
+
* @param {number} [swfVersion]
|
|
480
|
+
*/
|
|
481
|
+
function decodeClipActions(r, swfVersion) {
|
|
482
|
+
const v = swfVersion || 9;
|
|
483
|
+
const wide = v >= 6;
|
|
484
|
+
try { r.readUI16(); } catch (_) { return []; } // reserved
|
|
485
|
+
try {
|
|
486
|
+
if (wide) r.readUI32();
|
|
487
|
+
else r.readUI16();
|
|
488
|
+
} catch (_) { return []; } // allEventFlags
|
|
489
|
+
|
|
490
|
+
/** @type {{ eventFlags: number, keyCode?: number, bytecodeOffset: number, bytecode: Uint8Array }[]} */
|
|
491
|
+
const out = [];
|
|
492
|
+
for (;;) {
|
|
493
|
+
let eventFlags;
|
|
494
|
+
try {
|
|
495
|
+
eventFlags = wide ? r.readUI32() : r.readUI16();
|
|
496
|
+
} catch (_) { break; }
|
|
497
|
+
if (!eventFlags) break;
|
|
498
|
+
let size;
|
|
499
|
+
try { size = r.readUI32(); } catch (_) { break; }
|
|
500
|
+
if (size < 0 || size > 1e7) break;
|
|
501
|
+
let keyCode;
|
|
502
|
+
let payload = size;
|
|
503
|
+
if (eventFlags & 0x20000) { // ClipEventKeyPress
|
|
504
|
+
try { keyCode = r.readUI8(); } catch (_) { break; }
|
|
505
|
+
payload = size - 1;
|
|
506
|
+
}
|
|
507
|
+
if (payload < 0) break;
|
|
508
|
+
const start = r.bytePos;
|
|
509
|
+
const end = start + payload;
|
|
510
|
+
if (end > r.length) break;
|
|
511
|
+
const bytecode = r._bytes.slice(start, end);
|
|
512
|
+
r.seek(end);
|
|
513
|
+
out.push(keyCode != null ? { eventFlags, keyCode, bytecodeOffset: start, bytecode }
|
|
514
|
+
: { eventFlags, bytecodeOffset: start, bytecode });
|
|
515
|
+
}
|
|
516
|
+
return out;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
const FILTER_TYPES = ['dropShadow', 'blur', 'glow', 'bevel', 'gradientGlow', 'convolution', 'colorMatrix', 'gradientBevel'];
|
|
520
|
+
|
|
521
|
+
export function decodeFilters(r) {
|
|
522
|
+
const count = r.readUI8();
|
|
523
|
+
return Array.from({ length: count }, () => decodeFilter(r));
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
function decodeFilter(r) {
|
|
527
|
+
const id = r.readUI8();
|
|
528
|
+
const type = FILTER_TYPES[id];
|
|
529
|
+
if (!type) throw new Error(`Unknown SWF filter type ${id}`);
|
|
530
|
+
|
|
531
|
+
if (id === 5 || id === 6) {
|
|
532
|
+
const floatBuffer = new DataView(new ArrayBuffer(4));
|
|
533
|
+
const readFloat = () => {
|
|
534
|
+
floatBuffer.setUint32(0, r.readUI32());
|
|
535
|
+
return floatBuffer.getFloat32(0);
|
|
536
|
+
};
|
|
537
|
+
if (id === 6) return { type, matrix: Array.from({ length: 20 }, readFloat) };
|
|
538
|
+
|
|
539
|
+
const matrixX = r.readUI8();
|
|
540
|
+
const matrixY = r.readUI8();
|
|
541
|
+
const divisor = readFloat();
|
|
542
|
+
const bias = readFloat();
|
|
543
|
+
const matrix = Array.from({ length: matrixX * matrixY }, readFloat);
|
|
544
|
+
const defaultColor = r.readRGBA();
|
|
545
|
+
const flags = r.readUI8();
|
|
546
|
+
return { type, matrixX, matrixY, divisor, bias, matrix, defaultColor, clamp: !!(flags & 2), preserveAlpha: !!(flags & 1) };
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
const filter = { type };
|
|
550
|
+
const gradient = id === 4 || id === 7;
|
|
551
|
+
if (gradient) {
|
|
552
|
+
const colorCount = r.readUI8();
|
|
553
|
+
filter.colors = Array.from({ length: colorCount }, () => r.readRGBA());
|
|
554
|
+
filter.ratios = Array.from({ length: colorCount }, () => r.readUI8());
|
|
555
|
+
} else if (id === 3) {
|
|
556
|
+
filter.shadowColor = r.readRGBA();
|
|
557
|
+
filter.highlightColor = r.readRGBA();
|
|
558
|
+
} else if (id !== 1) {
|
|
559
|
+
filter.color = r.readRGBA();
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
filter.blurX = (r.readUI32() | 0) / 65536;
|
|
563
|
+
filter.blurY = (r.readUI32() | 0) / 65536;
|
|
564
|
+
if (id === 1) {
|
|
565
|
+
filter.passes = r.readUI8() >> 3;
|
|
566
|
+
return filter;
|
|
567
|
+
}
|
|
568
|
+
if (id !== 2) {
|
|
569
|
+
filter.angle = (r.readUI32() | 0) / 65536;
|
|
570
|
+
filter.distance = (r.readUI32() | 0) / 65536;
|
|
571
|
+
}
|
|
572
|
+
filter.strength = r.readSI16() / 256;
|
|
573
|
+
const flags = r.readUI8();
|
|
574
|
+
filter.inner = !!(flags & 0x80);
|
|
575
|
+
filter.knockout = !!(flags & 0x40);
|
|
576
|
+
filter.compositeSource = !!(flags & 0x20);
|
|
577
|
+
if (gradient || id === 3) {
|
|
578
|
+
filter.onTop = !!(flags & 0x10);
|
|
579
|
+
filter.passes = flags & 0x0f;
|
|
580
|
+
} else {
|
|
581
|
+
filter.passes = flags & 0x1f;
|
|
582
|
+
}
|
|
583
|
+
return filter;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
function decodeDefineShape(r, rawData, version) {
|
|
587
|
+
const characterId = r.readUI16();
|
|
588
|
+
const bounds = r.readRect();
|
|
589
|
+
r.align();
|
|
590
|
+
return { type: 'Shape', characterId, version, bounds, rawData };
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
function decodeDefineSound(r, rawData) {
|
|
594
|
+
const characterId = r.readUI16();
|
|
595
|
+
const flags = r.readUI8();
|
|
596
|
+
const codecId = (flags >> 4) & 0x0F;
|
|
597
|
+
const sampleRate = [5512, 11025, 22050, 44100][(flags >> 2) & 0x03];
|
|
598
|
+
const is16bit = !!(flags & 0x02);
|
|
599
|
+
const isStereo = !!(flags & 0x01);
|
|
600
|
+
const sampleCount = r.readUI32();
|
|
601
|
+
return { type: 'Sound', characterId, codecId, sampleRate, is16bit, isStereo, sampleCount, rawData: rawData.slice(r.bytePos) };
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
function decodeDefineButtonSound(r) {
|
|
605
|
+
const buttonId = r.readUI16();
|
|
606
|
+
const sounds = {};
|
|
607
|
+
for (const transition of ['overUpToIdle', 'idleToOverUp', 'overUpToOverDown', 'overDownToOverUp']) {
|
|
608
|
+
const soundId = r.readUI16();
|
|
609
|
+
if (soundId) sounds[transition] = { soundId, soundInfo: decodeSoundInfo(r) };
|
|
610
|
+
}
|
|
611
|
+
return { type: 'ButtonSound', buttonId, sounds };
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
function decodeSoundInfo(r) {
|
|
615
|
+
const flags = r.readUI8();
|
|
616
|
+
const info = {
|
|
617
|
+
syncStop: !!(flags & 0x20),
|
|
618
|
+
syncNoMultiple: !!(flags & 0x10),
|
|
619
|
+
inPoint: null,
|
|
620
|
+
outPoint: null,
|
|
621
|
+
loopCount: null,
|
|
622
|
+
envelope: [],
|
|
623
|
+
};
|
|
624
|
+
if (flags & 0x01) info.inPoint = r.readUI32();
|
|
625
|
+
if (flags & 0x02) info.outPoint = r.readUI32();
|
|
626
|
+
if (flags & 0x04) info.loopCount = r.readUI16();
|
|
627
|
+
if (flags & 0x08) {
|
|
628
|
+
const count = r.readUI8();
|
|
629
|
+
for (let i = 0; i < count; i++) {
|
|
630
|
+
info.envelope.push({
|
|
631
|
+
position: r.readUI32(),
|
|
632
|
+
leftLevel: r.readUI16(),
|
|
633
|
+
rightLevel: r.readUI16(),
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
return info;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
function decodeDefineSprite(r, rawData, doc) {
|
|
641
|
+
const characterId = r.readUI16();
|
|
642
|
+
const frameCount = r.readUI16();
|
|
643
|
+
|
|
644
|
+
// Parse the sprite's internal tag list
|
|
645
|
+
const spriteTags = [];
|
|
646
|
+
while (r.bytePos < rawData.length) {
|
|
647
|
+
const tagStart = r.bytePos;
|
|
648
|
+
let tagCodeAndLength;
|
|
649
|
+
try { tagCodeAndLength = r.readUI16(); } catch (_) { break; }
|
|
650
|
+
|
|
651
|
+
const code = (tagCodeAndLength >> 6) & 0x3FF;
|
|
652
|
+
let length = tagCodeAndLength & 0x3F;
|
|
653
|
+
if (length === 0x3F) { length = r.readUI32(); }
|
|
654
|
+
if (code === TAG.End) break;
|
|
655
|
+
|
|
656
|
+
const dataStart = r.bytePos;
|
|
657
|
+
const dataEnd = dataStart + length;
|
|
658
|
+
if (dataEnd > rawData.length) break;
|
|
659
|
+
|
|
660
|
+
spriteTags.push({ code, length, data: rawData.slice(dataStart, dataEnd), offset: tagStart });
|
|
661
|
+
r.seek(dataEnd);
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
return { type: 'Sprite', characterId, frameCount, tags: spriteTags, tagStreamBytes: rawData.length - 4 };
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
function decodeDefineFont4(r, rawData, diagnostics) {
|
|
668
|
+
const characterId = r.readUI16();
|
|
669
|
+
const flags = r.readUI8();
|
|
670
|
+
const nameEnd = rawData.indexOf(0, r.bytePos);
|
|
671
|
+
if (nameEnd < 0) throw new Error('Unterminated DefineFont4 name');
|
|
672
|
+
const name = new TextDecoder().decode(rawData.subarray(r.bytePos, nameEnd));
|
|
673
|
+
const hasFontData = !!(flags & 4);
|
|
674
|
+
let fontData;
|
|
675
|
+
if (hasFontData) {
|
|
676
|
+
const bytes = rawData.subarray(nameEnd + 1);
|
|
677
|
+
if (hasOpenTypeCffTables(bytes)) fontData = bytes;
|
|
678
|
+
else diagnostics?.warn('Invalid DefineFont4 OpenType CFF data', 'decode', { characterId });
|
|
679
|
+
}
|
|
680
|
+
return { type: 'Font', characterId, version: 4, name, flags, hasFontData, fontData, rawData };
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
// Validate the SFNT directory and the required SWF table structure. The
|
|
684
|
+
// browser's font sanitizer validates the glyph programs when FontFace loads.
|
|
685
|
+
function hasOpenTypeCffTables(bytes) {
|
|
686
|
+
if (bytes.length < 12) return false;
|
|
687
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
688
|
+
if (view.getUint32(0) !== 0x4f54544f) return false; // OTTO
|
|
689
|
+
const count = view.getUint16(4);
|
|
690
|
+
const directoryEnd = 12 + 16 * count;
|
|
691
|
+
if (directoryEnd > bytes.length) return false;
|
|
692
|
+
const tables = new Map();
|
|
693
|
+
for (let entry = 12; entry < directoryEnd; entry += 16) {
|
|
694
|
+
const tag = String.fromCharCode(...bytes.subarray(entry, entry + 4));
|
|
695
|
+
const offset = view.getUint32(entry + 8);
|
|
696
|
+
const length = view.getUint32(entry + 12);
|
|
697
|
+
if (tables.has(tag) || offset < directoryEnd || offset % 4 || offset + length > bytes.length) return false;
|
|
698
|
+
tables.set(tag, { offset, length });
|
|
699
|
+
}
|
|
700
|
+
const hasTables = names => names.every(name => tables.get(name)?.length);
|
|
701
|
+
if (!hasTables(['CFF ', 'cmap', 'head', 'maxp', 'OS/2', 'post']) ||
|
|
702
|
+
!(hasTables(['hhea', 'hmtx']) || hasTables(['vhea', 'vmtx', 'VORG']))) return false;
|
|
703
|
+
if (tables.get('head').length < 54) return false;
|
|
704
|
+
const cff = tables.get('CFF ');
|
|
705
|
+
if (cff.length < 4 || bytes[cff.offset] !== 1 ||
|
|
706
|
+
bytes[cff.offset + 2] < 4 || bytes[cff.offset + 2] >= cff.length) return false;
|
|
707
|
+
const { offset, length } = tables.get('cmap');
|
|
708
|
+
if (length < 4 || view.getUint16(offset) !== 0) return false;
|
|
709
|
+
const end = 4 + 8 * view.getUint16(offset + 2);
|
|
710
|
+
if (end > length) return false;
|
|
711
|
+
for (let entry = 4; entry < end; entry += 8) {
|
|
712
|
+
const platform = view.getUint16(offset + entry);
|
|
713
|
+
const encoding = view.getUint16(offset + entry + 2);
|
|
714
|
+
if (!(platform === 0 && (encoding === 3 || encoding === 4)) &&
|
|
715
|
+
!(platform === 3 && (encoding === 0 || encoding === 1 || encoding === 10))) continue;
|
|
716
|
+
const start = view.getUint32(offset + entry + 4);
|
|
717
|
+
if (start < end || start + 4 > length) continue;
|
|
718
|
+
const format = view.getUint16(offset + start);
|
|
719
|
+
if ([0, 2, 4, 6].includes(format)) {
|
|
720
|
+
const tableLength = view.getUint16(offset + start + 2);
|
|
721
|
+
if (tableLength >= 6 && start + tableLength <= length) return true;
|
|
722
|
+
} else if ([8, 10, 12, 13].includes(format) && start + 8 <= length) {
|
|
723
|
+
const tableLength = view.getUint32(offset + start + 4);
|
|
724
|
+
if (tableLength >= 12 && start + tableLength <= length) return true;
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
return false;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
function decodeFontNameMetadata(r, rawData, diagnostics) {
|
|
731
|
+
const tag = { type: 'FontSupplementary', code: TAG.DefineFontName, rawData };
|
|
732
|
+
const fontId = rawData.length >= 2 ? r.readUI16() : undefined;
|
|
733
|
+
const nameEnd = rawData.indexOf(0, r.bytePos);
|
|
734
|
+
const copyrightEnd = nameEnd < 0 ? -1 : rawData.indexOf(0, nameEnd + 1);
|
|
735
|
+
if (fontId == null || copyrightEnd < 0) {
|
|
736
|
+
diagnostics?.warn('Truncated DefineFontName metadata', 'decode', { fontId });
|
|
737
|
+
return tag;
|
|
738
|
+
}
|
|
739
|
+
const utf8 = new TextDecoder();
|
|
740
|
+
return { ...tag, fontId, fullName: utf8.decode(rawData.subarray(r.bytePos, nameEnd)),
|
|
741
|
+
copyright: utf8.decode(rawData.subarray(nameEnd + 1, copyrightEnd)) };
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
function readFontZoneValue(r) {
|
|
745
|
+
const bits = r.readUI16();
|
|
746
|
+
const sign = bits & 0x8000 ? -1 : 1;
|
|
747
|
+
const exponent = (bits >>> 10) & 0x1f;
|
|
748
|
+
const fraction = (bits & 0x3ff) / 1024;
|
|
749
|
+
if (exponent === 31) return fraction ? NaN : sign * Infinity;
|
|
750
|
+
// The SWF FLOAT16 exponent bias is 16.
|
|
751
|
+
if (exponent === 0) return sign * fraction * 2 ** -15;
|
|
752
|
+
return sign * (1 + fraction) * 2 ** (exponent - 16);
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
function decodeFontAlignZones(r, rawData, diagnostics) {
|
|
756
|
+
const tag = { type: 'FontSupplementary', code: TAG.DefineFontAlignZones, rawData };
|
|
757
|
+
if (r.length < 3) {
|
|
758
|
+
diagnostics?.warn('Truncated DefineFontAlignZones header', 'decode');
|
|
759
|
+
return tag;
|
|
760
|
+
}
|
|
761
|
+
const fontId = r.readUI16();
|
|
762
|
+
const csmTableHint = r.readUI8() >>> 6;
|
|
763
|
+
const records = [];
|
|
764
|
+
while (!r.eof) {
|
|
765
|
+
const count = r.readUI8();
|
|
766
|
+
if (count !== 2 || r.bytePos + 4 * count + 1 > r.length) {
|
|
767
|
+
diagnostics?.warn('Invalid DefineFontAlignZones record', 'decode', { fontId, glyphIndex: records.length });
|
|
768
|
+
return { ...tag, fontId };
|
|
769
|
+
}
|
|
770
|
+
const data = Array.from({ length: count }, () => ({ coordinate: readFontZoneValue(r), range: readFontZoneValue(r) }));
|
|
771
|
+
const mask = r.readUI8();
|
|
772
|
+
records.push({ data, maskX: !!(mask & 1), maskY: !!(mask & 2) });
|
|
773
|
+
}
|
|
774
|
+
return { ...tag, fontId, alignmentZones: { csmTableHint, records } };
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
function decodeCSMTextSettings(rawData, diagnostics) {
|
|
778
|
+
const tag = { type: 'CSMTextSettings', code: TAG.CSMTextSettings, rawData };
|
|
779
|
+
if (rawData.length < 12) {
|
|
780
|
+
diagnostics?.warn('Truncated CSMTextSettings record', 'decode');
|
|
781
|
+
return tag;
|
|
782
|
+
}
|
|
783
|
+
const view = new DataView(rawData.buffer, rawData.byteOffset, rawData.byteLength);
|
|
784
|
+
const textId = view.getUint16(0, true);
|
|
785
|
+
const flags = view.getUint8(2);
|
|
786
|
+
const thickness = view.getFloat32(3, true);
|
|
787
|
+
const sharpness = view.getFloat32(7, true);
|
|
788
|
+
if (!Number.isFinite(thickness) || !Number.isFinite(sharpness)) {
|
|
789
|
+
diagnostics?.warn('Invalid CSMTextSettings thickness or sharpness', 'decode', { textId });
|
|
790
|
+
return { ...tag, textId };
|
|
791
|
+
}
|
|
792
|
+
return { ...tag, textId, csmTextSettings: { useFlashType: flags >>> 6, gridFit: (flags >>> 3) & 7, thickness, sharpness } };
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
function decodeDefineFont2(r, rawData, version, swfVersion) {
|
|
796
|
+
const characterId = r.readUI16();
|
|
797
|
+
const flags = r.readUI8();
|
|
798
|
+
const languageCode = r.readUI8();
|
|
799
|
+
const nameLength = r.readUI8();
|
|
800
|
+
const name = decodeFontName(r.readBytes(nameLength), swfVersion, flags & 0x40, flags & 0x10);
|
|
801
|
+
const glyphCount = r.readUI16();
|
|
802
|
+
// Font subsets with no glyphs end immediately after the count.
|
|
803
|
+
if (!glyphCount) return { type: 'Font', characterId, version, name, languageCode, flags, codes: [], glyphs: [], rawData };
|
|
804
|
+
// DefineFont2/3 flags are MSB-first in the spec: WideOffsets is bit 3
|
|
805
|
+
// (0x08), WideCodes is bit 4 (0x04).
|
|
806
|
+
const wideOffsets = !!(flags & 0x08);
|
|
807
|
+
const wideCodes = !!(flags & 0x04);
|
|
808
|
+
const offsetTableStart = r.bytePos;
|
|
809
|
+
const offsets = [];
|
|
810
|
+
for (let i = 0; i < glyphCount; i++) offsets.push(wideOffsets ? r.readUI32() : r.readUI16());
|
|
811
|
+
const codeTableOffset = wideOffsets ? r.readUI32() : r.readUI16();
|
|
812
|
+
const codeTableStart = offsetTableStart + codeTableOffset;
|
|
813
|
+
const codes = [];
|
|
814
|
+
if (codeTableStart >= offsetTableStart && codeTableStart <= rawData.length) {
|
|
815
|
+
r.seek(codeTableStart);
|
|
816
|
+
for (let i = 0; i < glyphCount && r.bytePos < rawData.length; i++) {
|
|
817
|
+
codes.push(wideCodes ? r.readUI16() : r.readUI8());
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
let layout;
|
|
821
|
+
if ((flags & 0x80) && codes.length === glyphCount && r.bytePos + 6 + glyphCount * 2 <= rawData.length) {
|
|
822
|
+
const ascent = r.readUI16();
|
|
823
|
+
const descent = r.readUI16();
|
|
824
|
+
const leading = r.readSI16();
|
|
825
|
+
// Wide Font3 glyphs can exceed the signed 16-bit range.
|
|
826
|
+
const advances = Array.from({ length: glyphCount }, () => r.readUI16());
|
|
827
|
+
const kerning = [];
|
|
828
|
+
// Some older producers omit the optional bounds and kerning tables.
|
|
829
|
+
if (!r.eof) {
|
|
830
|
+
try {
|
|
831
|
+
for (let i = 0; i < glyphCount; i++) { r.readRect(); r.align(); }
|
|
832
|
+
if (!r.eof) {
|
|
833
|
+
const count = r.readUI16();
|
|
834
|
+
for (let i = 0; i < count; i++) {
|
|
835
|
+
const left = wideCodes ? r.readUI16() : r.readUI8();
|
|
836
|
+
const right = wideCodes ? r.readUI16() : r.readUI8();
|
|
837
|
+
kerning.push([left, right, r.readSI16()]);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
} catch (_) { kerning.length = 0; }
|
|
841
|
+
}
|
|
842
|
+
layout = { ascent, descent, leading, advances, kerning };
|
|
843
|
+
}
|
|
844
|
+
const glyphs = decodeGlyphTable(rawData, offsetTableStart, offsets, codeTableStart);
|
|
845
|
+
return { type: 'Font', characterId, version, name, languageCode, flags, codes, glyphs, layout, rawData };
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
function decodeDefineFont1(r, rawData) {
|
|
849
|
+
const characterId = r.readUI16();
|
|
850
|
+
if (r.bytePos + 2 > rawData.length) return { type: 'Font', characterId, version: 1, codes: [], glyphs: [], rawData };
|
|
851
|
+
const offsetTableStart = r.bytePos;
|
|
852
|
+
const firstOffset = r.readUI16();
|
|
853
|
+
const glyphCount = firstOffset / 2;
|
|
854
|
+
if (!Number.isInteger(glyphCount) || glyphCount < 1 || glyphCount > 32767) {
|
|
855
|
+
return { type: 'Font', characterId, version: 1, codes: [], glyphs: [], rawData };
|
|
856
|
+
}
|
|
857
|
+
const offsets = [firstOffset];
|
|
858
|
+
for (let i = 1; i < glyphCount; i++) offsets.push(r.readUI16());
|
|
859
|
+
return {
|
|
860
|
+
type: 'Font', characterId, version: 1, codes: [],
|
|
861
|
+
glyphs: decodeGlyphTable(rawData, offsetTableStart, offsets, rawData.length), rawData,
|
|
862
|
+
};
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
function decodeGlyphTable(rawData, offsetTableStart, offsets, endOffset) {
|
|
866
|
+
const glyphs = [];
|
|
867
|
+
for (let i = 0; i < offsets.length; i++) {
|
|
868
|
+
const start = offsetTableStart + offsets[i];
|
|
869
|
+
const end = i + 1 < offsets.length ? offsetTableStart + offsets[i + 1] : endOffset;
|
|
870
|
+
if (start < offsetTableStart || end <= start || end > rawData.length) {
|
|
871
|
+
glyphs.push(null);
|
|
872
|
+
continue;
|
|
873
|
+
}
|
|
874
|
+
try { glyphs.push(decodeGlyphShape(rawData.subarray(start, end))); }
|
|
875
|
+
catch (_) { glyphs.push(null); }
|
|
876
|
+
}
|
|
877
|
+
return glyphs;
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
function decodeFontName(bytes, swfVersion, shiftJis, ansi) {
|
|
881
|
+
// Font names are length-prefixed bytes and use UTF-8 from SWF 6 onward.
|
|
882
|
+
// Earlier movies depend on the authoring code page, with these two hints.
|
|
883
|
+
const encoding = swfVersion >= 6 ? 'utf-8' : shiftJis ? 'shift_jis' : ansi ? 'windows-1252' : null;
|
|
884
|
+
const name = encoding ? new TextDecoder(encoding).decode(bytes) : String.fromCharCode(...bytes);
|
|
885
|
+
return name.replace(/\0+$/, '');
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
function decodeFontInfo(r, rawData, version2, swfVersion) {
|
|
889
|
+
const fontId = r.readUI16();
|
|
890
|
+
const nameLength = r.readUI8();
|
|
891
|
+
const nameBytes = r.readBytes(nameLength);
|
|
892
|
+
const flags = r.readUI8();
|
|
893
|
+
const name = decodeFontName(nameBytes, swfVersion, flags & 0x10, flags & 0x08);
|
|
894
|
+
const languageCode = version2 ? r.readUI8() : 0;
|
|
895
|
+
const wideCodes = !!(flags & 0x01);
|
|
896
|
+
const codes = [];
|
|
897
|
+
while (r.bytePos < rawData.length) codes.push(wideCodes ? r.readUI16() : r.readUI8());
|
|
898
|
+
return { type: 'FontSupplementary', fontId, name, languageCode, flags, codes, rawData };
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
function decodeDefineText(r, rawData, version) {
|
|
902
|
+
const characterId = r.readUI16();
|
|
903
|
+
const bounds = r.readRect();
|
|
904
|
+
r.align();
|
|
905
|
+
const matrix = r.readMatrix();
|
|
906
|
+
r.align();
|
|
907
|
+
const glyphBits = r.readUI8();
|
|
908
|
+
const advanceBits = r.readUI8();
|
|
909
|
+
const records = [];
|
|
910
|
+
let fontId = 0;
|
|
911
|
+
let fontHeight = 240;
|
|
912
|
+
let color = '#000000';
|
|
913
|
+
let x = 0, y = 0;
|
|
914
|
+
while (r.bytePos < rawData.length) {
|
|
915
|
+
const type = r.readUB(1);
|
|
916
|
+
// A zero TextRecordType is the EndOfRecordsFlag. Text records always have
|
|
917
|
+
// type 1; the following three bits are reserved, then come four style flags.
|
|
918
|
+
if (!type) break;
|
|
919
|
+
r.readUB(3); // StyleFlagsReserved
|
|
920
|
+
const hasFont = !!r.readUB(1);
|
|
921
|
+
const hasColor = !!r.readUB(1);
|
|
922
|
+
const hasYOffset = !!r.readUB(1);
|
|
923
|
+
const hasXOffset = !!r.readUB(1);
|
|
924
|
+
if (hasFont) fontId = r.readUI16();
|
|
925
|
+
if (hasColor) {
|
|
926
|
+
const c = version === 2 ? r.readRGBA() : r.readRGB();
|
|
927
|
+
color = version === 2
|
|
928
|
+
? `rgba(${c.r},${c.g},${c.b},${c.a / 255})`
|
|
929
|
+
: `rgb(${c.r},${c.g},${c.b})`;
|
|
930
|
+
}
|
|
931
|
+
if (hasXOffset) x = r.readSI16();
|
|
932
|
+
if (hasYOffset) y = r.readSI16();
|
|
933
|
+
if (hasFont) fontHeight = r.readUI16();
|
|
934
|
+
const runX = x;
|
|
935
|
+
const count = r.readUI8();
|
|
936
|
+
const glyphs = [];
|
|
937
|
+
for (let i = 0; i < count; i++) {
|
|
938
|
+
const glyphIndex = r.readUB(glyphBits);
|
|
939
|
+
const advance = r.readSB(advanceBits);
|
|
940
|
+
glyphs.push({ glyphIndex, advance });
|
|
941
|
+
x += advance;
|
|
942
|
+
}
|
|
943
|
+
r.align();
|
|
944
|
+
records.push({ fontId, fontHeight, color, x: runX, y, glyphs });
|
|
945
|
+
}
|
|
946
|
+
return { type: 'Text', characterId, version, bounds, matrix, records, rawData };
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
function decodeSceneAndFrameLabels(r, utf8) {
|
|
950
|
+
const sceneCount = readEncodedU32(r);
|
|
951
|
+
const scenes = [];
|
|
952
|
+
for (let i = 0; i < sceneCount; i++) scenes.push({ offset: readEncodedU32(r), name: r.readString(utf8) });
|
|
953
|
+
const labelCount = readEncodedU32(r);
|
|
954
|
+
const labels = [];
|
|
955
|
+
for (let i = 0; i < labelCount; i++) labels.push({ frame: readEncodedU32(r), name: r.readString(utf8) });
|
|
956
|
+
return { type: 'SceneAndFrameLabels', scenes, labels };
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
function readEncodedU32(r) {
|
|
960
|
+
let value = 0;
|
|
961
|
+
for (let i = 0; i < 5; i++) {
|
|
962
|
+
const byte = r.readUI8();
|
|
963
|
+
value |= (byte & 0x7f) << (i * 7);
|
|
964
|
+
if (!(byte & 0x80)) return value >>> 0;
|
|
965
|
+
}
|
|
966
|
+
return value >>> 0;
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
function decodeDefineEditText(r, rawData, utf8) {
|
|
970
|
+
// EditTextFlag uses the low bits for the first fields in the SWF tag:
|
|
971
|
+
// HasFont is bit 0 and HasText is bit 7; higher bits carry layout flags.
|
|
972
|
+
const characterId = r.readUI16();
|
|
973
|
+
const bounds = r.readRect();
|
|
974
|
+
r.align();
|
|
975
|
+
const flags = r.readUI16();
|
|
976
|
+
const hasText = !!(flags & 0x0080);
|
|
977
|
+
const wordWrap = !!(flags & 0x0040);
|
|
978
|
+
const multiline = !!(flags & 0x0020);
|
|
979
|
+
const password = !!(flags & 0x0010);
|
|
980
|
+
const readOnly = !!(flags & 0x0008);
|
|
981
|
+
const hasTextColor = !!(flags & 0x0004);
|
|
982
|
+
const hasMaxLength = !!(flags & 0x0002);
|
|
983
|
+
const hasFont = !!(flags & 0x0001);
|
|
984
|
+
const hasFontClass = !!(flags & 0x8000);
|
|
985
|
+
const autoSize = !!(flags & 0x4000);
|
|
986
|
+
const hasLayout = !!(flags & 0x2000);
|
|
987
|
+
const noSelect = !!(flags & 0x1000);
|
|
988
|
+
const border = !!(flags & 0x0800);
|
|
989
|
+
const wasStatic = !!(flags & 0x0400);
|
|
990
|
+
const html = !!(flags & 0x0200);
|
|
991
|
+
const useOutlines = !!(flags & 0x0100);
|
|
992
|
+
|
|
993
|
+
let fontId = 0;
|
|
994
|
+
let fontClass = '';
|
|
995
|
+
let fontHeight = 240; // twips
|
|
996
|
+
let color = '#000000';
|
|
997
|
+
let maxLength = 0;
|
|
998
|
+
let align = 0;
|
|
999
|
+
let leftMargin = 0, rightMargin = 0, indent = 0, leading = 0;
|
|
1000
|
+
if (hasFont) fontId = r.readUI16();
|
|
1001
|
+
if (hasFontClass) fontClass = r.readString();
|
|
1002
|
+
if (hasFont || hasFontClass) fontHeight = r.readUI16();
|
|
1003
|
+
if (hasTextColor) {
|
|
1004
|
+
const red = r.readUI8();
|
|
1005
|
+
const green = r.readUI8();
|
|
1006
|
+
const blue = r.readUI8();
|
|
1007
|
+
const alpha = r.readUI8();
|
|
1008
|
+
color = `rgba(${red},${green},${blue},${alpha / 255})`;
|
|
1009
|
+
}
|
|
1010
|
+
if (hasMaxLength) maxLength = r.readUI16();
|
|
1011
|
+
if (hasLayout) {
|
|
1012
|
+
align = r.readUI8();
|
|
1013
|
+
leftMargin = r.readUI16();
|
|
1014
|
+
rightMargin = r.readUI16();
|
|
1015
|
+
indent = r.readSI16();
|
|
1016
|
+
leading = r.readSI16();
|
|
1017
|
+
}
|
|
1018
|
+
const variableName = r.readString();
|
|
1019
|
+
const initialText = hasText ? r.readString(utf8) : '';
|
|
1020
|
+
return {
|
|
1021
|
+
type: 'EditText',
|
|
1022
|
+
characterId,
|
|
1023
|
+
bounds,
|
|
1024
|
+
variableName,
|
|
1025
|
+
initialText,
|
|
1026
|
+
hasText,
|
|
1027
|
+
wordWrap,
|
|
1028
|
+
password,
|
|
1029
|
+
readOnly,
|
|
1030
|
+
noSelect,
|
|
1031
|
+
border,
|
|
1032
|
+
wasStatic,
|
|
1033
|
+
autoSize,
|
|
1034
|
+
useOutlines,
|
|
1035
|
+
maxLength,
|
|
1036
|
+
fontId,
|
|
1037
|
+
fontClass,
|
|
1038
|
+
fontHeight,
|
|
1039
|
+
color,
|
|
1040
|
+
align,
|
|
1041
|
+
leftMargin,
|
|
1042
|
+
rightMargin,
|
|
1043
|
+
indent,
|
|
1044
|
+
leading,
|
|
1045
|
+
multiline,
|
|
1046
|
+
html,
|
|
1047
|
+
rawData,
|
|
1048
|
+
};
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
function decodeDefineBitsLossless(r, rawData, version, diagnostics) {
|
|
1052
|
+
const characterId = r.readUI16();
|
|
1053
|
+
const bitmapFormat = r.readUI8(); // 3=8-bit paletted, 4=15-bit RGB, 5=24-bit RGB or 32-bit ARGB
|
|
1054
|
+
const width = r.readUI16();
|
|
1055
|
+
const height = r.readUI16();
|
|
1056
|
+
|
|
1057
|
+
let colorTableSize = 0;
|
|
1058
|
+
if (bitmapFormat === 3) {
|
|
1059
|
+
colorTableSize = r.readUI8() + 1;
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
const compressedData = rawData.slice(r.bytePos);
|
|
1063
|
+
let pixelData;
|
|
1064
|
+
try {
|
|
1065
|
+
pixelData = new Uint8Array(inflateSync(Buffer.from(compressedData)));
|
|
1066
|
+
} catch (err) {
|
|
1067
|
+
diagnostics.warn(`DefineBitsLossless${version} characterId ${characterId}: zlib failed: ${err.message}`, 'decode');
|
|
1068
|
+
pixelData = new Uint8Array(0);
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
// Convert to RGBA
|
|
1072
|
+
const rgba = losslessToRGBA(pixelData, width, height, bitmapFormat, colorTableSize, version);
|
|
1073
|
+
|
|
1074
|
+
return {
|
|
1075
|
+
type: 'Bitmap',
|
|
1076
|
+
characterId,
|
|
1077
|
+
format: version === 1 ? 'lossless1' : 'lossless2',
|
|
1078
|
+
width, height,
|
|
1079
|
+
rawData: rgba, // store decoded RGBA pixels
|
|
1080
|
+
};
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
function straightColorChannel(channel, alpha) {
|
|
1084
|
+
return Math.min(255, Math.round(channel * 255 / alpha));
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
/**
|
|
1088
|
+
* Convert DefineBitsLossless pixel data to a flat RGBA Uint8Array.
|
|
1089
|
+
* @param {Uint8Array} data
|
|
1090
|
+
* @param {number} width
|
|
1091
|
+
* @param {number} height
|
|
1092
|
+
* @param {number} format
|
|
1093
|
+
* @param {number} colorTableSize
|
|
1094
|
+
* @param {number} losslessVersion
|
|
1095
|
+
* @returns {Uint8Array}
|
|
1096
|
+
*/
|
|
1097
|
+
function losslessToRGBA(data, width, height, format, colorTableSize, losslessVersion) {
|
|
1098
|
+
const out = new Uint8Array(width * height * 4);
|
|
1099
|
+
let i = 0;
|
|
1100
|
+
|
|
1101
|
+
if (format === 3) {
|
|
1102
|
+
// 8-bit paletted: colorTableSize entries of RGB (v1) or RGBA (v2), then indices
|
|
1103
|
+
const bytesPerColor = losslessVersion === 2 ? 4 : 3;
|
|
1104
|
+
const palette = [];
|
|
1105
|
+
for (let c = 0; c < colorTableSize; c++) {
|
|
1106
|
+
const r = data[c * bytesPerColor];
|
|
1107
|
+
const g = data[c * bytesPerColor + 1];
|
|
1108
|
+
const b = data[c * bytesPerColor + 2];
|
|
1109
|
+
const a = losslessVersion === 2 ? data[c * bytesPerColor + 3] : 255;
|
|
1110
|
+
// Lossless2 palettes also store premultiplied color, as in Tron's indexed artwork.
|
|
1111
|
+
palette.push(a > 0 && a < 255
|
|
1112
|
+
? [straightColorChannel(r, a), straightColorChannel(g, a), straightColorChannel(b, a), a]
|
|
1113
|
+
: [r, g, b, a]);
|
|
1114
|
+
}
|
|
1115
|
+
const indexOffset = colorTableSize * bytesPerColor;
|
|
1116
|
+
// indices are padded to 4-byte row alignment
|
|
1117
|
+
const rowBytes = Math.ceil(width / 4) * 4;
|
|
1118
|
+
for (let y = 0; y < height; y++) {
|
|
1119
|
+
for (let x = 0; x < width; x++) {
|
|
1120
|
+
const idx = data[indexOffset + y * rowBytes + x];
|
|
1121
|
+
const col = palette[idx] || [0, 0, 0, 0];
|
|
1122
|
+
out[i++] = col[0]; out[i++] = col[1]; out[i++] = col[2]; out[i++] = col[3];
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
} else if (format === 4) {
|
|
1126
|
+
// Two bytes per PIX15, with each row padded to a 32-bit boundary.
|
|
1127
|
+
const rowBytes = Math.ceil(width / 2) * 4;
|
|
1128
|
+
for (let y = 0; y < height; y++) {
|
|
1129
|
+
const rowOffset = y * rowBytes;
|
|
1130
|
+
for (let x = 0; x < width; x++) {
|
|
1131
|
+
const offset = rowOffset + x * 2;
|
|
1132
|
+
const pixel = (data[offset] << 8) | data[offset + 1];
|
|
1133
|
+
out[i++] = ((pixel >> 10) & 0x1F) << 3;
|
|
1134
|
+
out[i++] = ((pixel >> 5) & 0x1F) << 3;
|
|
1135
|
+
out[i++] = (pixel & 0x1F) << 3;
|
|
1136
|
+
out[i++] = 255;
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
} else if (format === 5) {
|
|
1140
|
+
// 24-bit (v1): 0RGB — 4 bytes, first byte ignored
|
|
1141
|
+
// 32-bit (v2): premultiplied ARGB
|
|
1142
|
+
for (let p = 0; p < width * height; p++) {
|
|
1143
|
+
const base = p * 4;
|
|
1144
|
+
const alpha = losslessVersion === 2 ? data[base] : 255;
|
|
1145
|
+
if (alpha > 0 && alpha < 255) {
|
|
1146
|
+
// PNG stores RGB independently of alpha.
|
|
1147
|
+
for (let channel = 1; channel <= 3; channel++) {
|
|
1148
|
+
out[i++] = straightColorChannel(data[base + channel], alpha);
|
|
1149
|
+
}
|
|
1150
|
+
} else {
|
|
1151
|
+
out[i++] = data[base + 1];
|
|
1152
|
+
out[i++] = data[base + 2];
|
|
1153
|
+
out[i++] = data[base + 3];
|
|
1154
|
+
}
|
|
1155
|
+
out[i++] = alpha;
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
return out;
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
/**
|
|
1163
|
+
* Read a CXFORM or CXFORMWITHALPHA structure.
|
|
1164
|
+
* @param {BitReader} r
|
|
1165
|
+
* @param {boolean} withAlpha
|
|
1166
|
+
* @returns {import('../model/swf-document.js').ColorTransform}
|
|
1167
|
+
*/
|
|
1168
|
+
export function readColorTransform(r, withAlpha) {
|
|
1169
|
+
const hasAdd = r.readUB(1);
|
|
1170
|
+
const hasMult = r.readUB(1);
|
|
1171
|
+
const nbits = r.readUB(4);
|
|
1172
|
+
|
|
1173
|
+
let rMult = 256, gMult = 256, bMult = 256, aMult = 256;
|
|
1174
|
+
let rAdd = 0, gAdd = 0, bAdd = 0, aAdd = 0;
|
|
1175
|
+
|
|
1176
|
+
if (hasMult) {
|
|
1177
|
+
rMult = r.readSB(nbits);
|
|
1178
|
+
gMult = r.readSB(nbits);
|
|
1179
|
+
bMult = r.readSB(nbits);
|
|
1180
|
+
if (withAlpha) aMult = r.readSB(nbits);
|
|
1181
|
+
}
|
|
1182
|
+
if (hasAdd) {
|
|
1183
|
+
rAdd = r.readSB(nbits);
|
|
1184
|
+
gAdd = r.readSB(nbits);
|
|
1185
|
+
bAdd = r.readSB(nbits);
|
|
1186
|
+
if (withAlpha) aAdd = r.readSB(nbits);
|
|
1187
|
+
}
|
|
1188
|
+
r.align();
|
|
1189
|
+
return { rMult, gMult, bMult, aMult, rAdd, gAdd, bAdd, aAdd };
|
|
1190
|
+
}
|