@haiyue/animation-spec 0.1.0 → 0.2.0
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 +40 -6
- package/SPECIFICATION.md +12 -2
- package/bin/hya-live2d-convert.mjs +76 -0
- package/bin/{hya-convert.mjs → hya-lottie-convert.mjs} +1 -1
- package/dist/chunks/binary-DvuTJ_hy.js +1 -0
- package/dist/chunks/clip-baked-Dv-wjX9d.js +1 -0
- package/dist/chunks/errors-Bzs3GOD9.js +1 -0
- package/dist/chunks/expression-D1cKtNwM.js +1 -0
- package/dist/chunks/extensions-DxL4AxOC.js +1 -0
- package/dist/conversion.d.ts +1 -0
- package/dist/conversion.js +1 -0
- package/dist/cubism.d.ts +1 -0
- package/dist/cubism.js +1 -0
- package/dist/deformable2d.d.ts +1 -0
- package/dist/deformable2d.js +1 -0
- package/dist/index.d.ts +1 -10
- package/dist/index.js +1 -2927
- package/dist/live2d/clip-baked.d.ts +1 -0
- package/dist/live2d/clip-baked.js +1 -0
- package/dist/live2d.d.ts +1 -0
- package/dist/live2d.js +1 -0
- package/dist/lottie.d.ts +1 -62
- package/dist/lottie.js +1 -2642
- package/dist/native3d.d.ts +1 -2
- package/dist/native3d.js +1 -907
- package/dist/shared.d.ts +1493 -0
- package/package.json +30 -10
- package/schema/animation.schema.json +92 -0
- package/schema/deformable-mesh-2d-extension.schema.json +18 -0
- package/schema/deformable-mesh-2d.contract.json +58 -0
- package/schema/parameterized-rig-2d.contract.json +63 -0
- package/schema/parameterized-rig-2d.schema.json +57 -0
- package/schema/parameterized-rig-census-map.json +128 -0
- package/schema/responsive-layout-2d-census-map.json +49 -0
- package/schema/responsive-layout-2d.contract.json +58 -0
- package/schema/responsive-layout-2d.schema.json +34 -0
- package/schema/state-machine-v2-census-map.json +1 -0
- package/schema/state-machine-v2.contract.json +66 -0
- package/schema/state-machine-v2.schema.json +55 -0
- package/schema/vector-visual-census-map.json +120 -0
- package/schema/vector-visual.contract.json +47 -0
- package/schema/vector-visual.schema.json +72 -0
- package/dist/binary.d.ts +0 -7
- package/dist/chunks/types-l6nBVYwx.js +0 -8
- package/dist/chunks/types-l6nBVYwx.js.map +0 -1
- package/dist/errors.d.ts +0 -7
- package/dist/extensions.d.ts +0 -19
- package/dist/index.js.map +0 -1
- package/dist/lottie-merge-path.d.ts +0 -11
- package/dist/lottie.js.map +0 -1
- package/dist/native3d/Animation3DError.d.ts +0 -8
- package/dist/native3d/Animation3DParser.d.ts +0 -7
- package/dist/native3d/Animation3DTypes.d.ts +0 -285
- package/dist/native3d/index.d.ts +0 -5
- package/dist/native3d.js.map +0 -1
- package/dist/parser.d.ts +0 -2
- package/dist/state-machine.d.ts +0 -130
- package/dist/text-selector.d.ts +0 -2
- package/dist/types.d.ts +0 -419
- package/dist/validation.d.ts +0 -32
- package/src/binary.ts +0 -1441
- package/src/errors.ts +0 -18
- package/src/extensions.ts +0 -55
- package/src/index.ts +0 -90
- package/src/lottie-merge-path.ts +0 -204
- package/src/lottie.ts +0 -3073
- package/src/native3d/Animation3DError.ts +0 -20
- package/src/native3d/Animation3DParser.ts +0 -964
- package/src/native3d/Animation3DTypes.ts +0 -322
- package/src/native3d/index.ts +0 -14
- package/src/native3d.ts +0 -2
- package/src/parser.ts +0 -41
- package/src/state-machine.ts +0 -645
- package/src/text-selector.ts +0 -7
- package/src/types.ts +0 -483
- package/src/validation.ts +0 -1286
- package/test/fixtures/basic-lottie.json +0 -31
package/src/binary.ts
DELETED
|
@@ -1,1441 +0,0 @@
|
|
|
1
|
-
import { AnimationFormatError } from './errors';
|
|
2
|
-
import type {
|
|
3
|
-
AnimationComponent,
|
|
4
|
-
AnimationComposite,
|
|
5
|
-
AnimationCompositeLayer,
|
|
6
|
-
AnimationDocument,
|
|
7
|
-
AnimationLayerEffect,
|
|
8
|
-
AnimationNode,
|
|
9
|
-
AnimationParseOptions,
|
|
10
|
-
AnimationPath2DComponent,
|
|
11
|
-
AnimationResource,
|
|
12
|
-
AnimationSprite2DComponent,
|
|
13
|
-
AnimationTransform2D,
|
|
14
|
-
AnimationText2DComponent,
|
|
15
|
-
AnimationTextAnimator,
|
|
16
|
-
AnimationVectorGradientPaint,
|
|
17
|
-
AnimationVectorShapeComponent,
|
|
18
|
-
AnimationVectorSolidPaint,
|
|
19
|
-
AnimationVectorStrokePaint,
|
|
20
|
-
AnimationVectorValueTrack,
|
|
21
|
-
ParsedAnimation,
|
|
22
|
-
} from './types';
|
|
23
|
-
import { ANIMATION_VECTOR_SHAPE_EXTENSION_ID } from './types';
|
|
24
|
-
import { TEXT_SELECTOR_BASES, TEXT_SELECTOR_SHAPES } from './text-selector';
|
|
25
|
-
import {
|
|
26
|
-
finalizeParsedAnimation,
|
|
27
|
-
parseAnimationValue,
|
|
28
|
-
parseBinaryComponentValue,
|
|
29
|
-
parseBinaryEffectsValue,
|
|
30
|
-
parseBinaryResourceValue,
|
|
31
|
-
parseBinaryTrackValue,
|
|
32
|
-
parseBinaryTransformValue,
|
|
33
|
-
resolveAnimationParseLimits,
|
|
34
|
-
} from './validation';
|
|
35
|
-
|
|
36
|
-
const HEADER_BYTES = 24;
|
|
37
|
-
const MAGIC = 0x31415948;
|
|
38
|
-
const CONTAINER_MAJOR = 2;
|
|
39
|
-
const CONTAINER_MINOR = 0;
|
|
40
|
-
const LEGACY_CONTAINER_MAJOR = 1;
|
|
41
|
-
const DEFAULT_PARSE_OPTIONS: AnimationParseOptions = Object.freeze({});
|
|
42
|
-
const UTF8_DECODER = new TextDecoder();
|
|
43
|
-
const UTF8_ENCODER = new TextEncoder();
|
|
44
|
-
|
|
45
|
-
const TRACK_PROPERTIES = ['position', 'rotation', 'scale', 'opacity'] as const;
|
|
46
|
-
const INTERPOLATIONS = ['step', 'linear', 'cubic-bezier'] as const;
|
|
47
|
-
const END_BEHAVIORS = ['loop', 'hold', 'destroy'] as const;
|
|
48
|
-
const COMPOSITE_KINDS = ['mask', 'matte'] as const;
|
|
49
|
-
const COMPOSITE_MODES = ['alpha', 'alpha-inverted', 'luma', 'luma-inverted'] as const;
|
|
50
|
-
const COMPOSITE_OPERATIONS = ['add', 'subtract', 'intersect', 'difference'] as const;
|
|
51
|
-
const VECTOR_STROKE_EXTENSION = 'org.haiyue.vector-stroke@1';
|
|
52
|
-
const VECTOR_PATH_MORPH_EXTENSION = 'org.haiyue.vector-path-morph@1';
|
|
53
|
-
const VECTOR_STROKE_KEYS = new Set([
|
|
54
|
-
'type', 'commands', 'values', 'sourceComponent', 'color', 'width',
|
|
55
|
-
'lineCap', 'lineJoin', 'miterLimit', 'tolerance',
|
|
56
|
-
]);
|
|
57
|
-
const VECTOR_PATH_MORPH_KEYS = new Set([
|
|
58
|
-
'type', 'commands', 'times', 'values', 'valueSize', 'interpolation',
|
|
59
|
-
'easings', 'fill', 'fillRule', 'tolerance',
|
|
60
|
-
]);
|
|
61
|
-
|
|
62
|
-
interface BinaryFloatRange {
|
|
63
|
-
offset: number;
|
|
64
|
-
length: number;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
interface LegacyBinaryTrackMetadata {
|
|
68
|
-
node: string;
|
|
69
|
-
property: string;
|
|
70
|
-
interpolation: string;
|
|
71
|
-
times: BinaryFloatRange;
|
|
72
|
-
values: BinaryFloatRange;
|
|
73
|
-
easings?: BinaryFloatRange;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
interface BinaryFloatBlock {
|
|
77
|
-
readonly range: readonly [number, number];
|
|
78
|
-
readonly values: Float32Array;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
class BinaryFloatPoolBuilder {
|
|
82
|
-
readonly blocks: BinaryFloatBlock[] = [];
|
|
83
|
-
private readonly _deduplicated = new Map<string, readonly [number, number]>();
|
|
84
|
-
count = 0;
|
|
85
|
-
|
|
86
|
-
add(values: readonly number[] | Float32Array, deduplicate = false): readonly [number, number] {
|
|
87
|
-
const floats = values instanceof Float32Array ? values : new Float32Array(values);
|
|
88
|
-
if (deduplicate) {
|
|
89
|
-
const key = floatBlockKey(floats);
|
|
90
|
-
const existing = this._deduplicated.get(key);
|
|
91
|
-
if (existing) return existing;
|
|
92
|
-
const range = this._append(floats);
|
|
93
|
-
this._deduplicated.set(key, range);
|
|
94
|
-
return range;
|
|
95
|
-
}
|
|
96
|
-
return this._append(floats);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
private _append(values: Float32Array): readonly [number, number] {
|
|
100
|
-
const result = [this.count, values.length] as const;
|
|
101
|
-
this.count += values.length;
|
|
102
|
-
this.blocks.push({ range: result, values });
|
|
103
|
-
return result;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
class BinaryStringTableBuilder {
|
|
108
|
-
readonly values: string[] = [];
|
|
109
|
-
private readonly _indices = new Map<string, number>();
|
|
110
|
-
|
|
111
|
-
add(value: string | undefined): number {
|
|
112
|
-
if (value === undefined) return -1;
|
|
113
|
-
const existing = this._indices.get(value);
|
|
114
|
-
if (existing !== undefined) return existing;
|
|
115
|
-
const index = this.values.length;
|
|
116
|
-
this.values.push(value);
|
|
117
|
-
this._indices.set(value, index);
|
|
118
|
-
return index;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export function isAnimationBinary(buffer: ArrayBuffer): boolean {
|
|
123
|
-
return buffer.byteLength >= 4 && new DataView(buffer).getUint32(0, true) === MAGIC;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/** Encodes compact indexed metadata plus one contiguous zero-copy Float32 track/path pool. */
|
|
127
|
-
export function encodeAnimationBinary(
|
|
128
|
-
document: AnimationDocument | ParsedAnimation,
|
|
129
|
-
options: AnimationParseOptions = DEFAULT_PARSE_OPTIONS,
|
|
130
|
-
): ArrayBuffer {
|
|
131
|
-
const parsed = isParsedAnimation(document)
|
|
132
|
-
? document
|
|
133
|
-
: parseAnimationValue(document, options, 'json');
|
|
134
|
-
const strings = new BinaryStringTableBuilder();
|
|
135
|
-
const floats = new BinaryFloatPoolBuilder();
|
|
136
|
-
const nodeIndices = new Map(parsed.nodes.map((node, index) => [node.id, index]));
|
|
137
|
-
|
|
138
|
-
const resources = parsed.resources.map(resource => encodeResource(resource, strings));
|
|
139
|
-
const nodes = parsed.nodes.map(node => [
|
|
140
|
-
strings.add(node.id),
|
|
141
|
-
strings.add(node.name),
|
|
142
|
-
node.parent === undefined ? -1 : requiredMapIndex(nodeIndices, node.parent, 'node parent'),
|
|
143
|
-
node.start ?? -1,
|
|
144
|
-
node.duration ?? -1,
|
|
145
|
-
node.transform ? encodeTransform(node.transform) : 0,
|
|
146
|
-
node.composite ? encodeComposite(node.composite, nodeIndices, floats) : 0,
|
|
147
|
-
node.components ? node.components.map(component => encodeComponent(component, floats, strings)) : 0,
|
|
148
|
-
node.extensions ?? 0,
|
|
149
|
-
node.effects ? encodeLayerEffects(node.effects, floats) : 0,
|
|
150
|
-
]);
|
|
151
|
-
const tracks = parsed.tracks.map(track => [
|
|
152
|
-
requiredMapIndex(nodeIndices, track.node, 'track node'),
|
|
153
|
-
TRACK_PROPERTIES.indexOf(track.property),
|
|
154
|
-
INTERPOLATIONS.indexOf(track.interpolation),
|
|
155
|
-
floats.add(track.times, true),
|
|
156
|
-
floats.add(track.values),
|
|
157
|
-
track.easings ? floats.add(track.easings, true) : 0,
|
|
158
|
-
track.spatialTangents ? floats.add(track.spatialTangents) : 0,
|
|
159
|
-
]);
|
|
160
|
-
const metadata = [
|
|
161
|
-
strings.add(parsed.name),
|
|
162
|
-
parsed.canvas.width,
|
|
163
|
-
parsed.canvas.height,
|
|
164
|
-
parsed.duration,
|
|
165
|
-
parsed.frameRate ?? 0,
|
|
166
|
-
END_BEHAVIORS.indexOf(parsed.endBehavior),
|
|
167
|
-
strings.values,
|
|
168
|
-
resources,
|
|
169
|
-
nodes,
|
|
170
|
-
tracks,
|
|
171
|
-
parsed.extensionsUsed.map(value => strings.add(value)),
|
|
172
|
-
parsed.extensionsRequired.map(value => strings.add(value)),
|
|
173
|
-
Object.keys(parsed.extensions).length === 0 ? 0 : parsed.extensions,
|
|
174
|
-
];
|
|
175
|
-
return writeBinary(JSON.stringify(metadata), floats, options);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
export function decodeAnimationBinary(
|
|
179
|
-
buffer: ArrayBuffer,
|
|
180
|
-
options: AnimationParseOptions = DEFAULT_PARSE_OPTIONS,
|
|
181
|
-
): ParsedAnimation {
|
|
182
|
-
if (buffer.byteLength < 4) invalidBinary('Header is truncated.');
|
|
183
|
-
const header = new DataView(buffer);
|
|
184
|
-
if (header.getUint32(0, true) !== MAGIC) invalidBinary('Magic must be HYA1.');
|
|
185
|
-
return decodeAnimationBinaryHeader(buffer, header, options);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/** @internal Parser entry point that checks and consumes the magic with one DataView. */
|
|
189
|
-
export function tryDecodeAnimationBinary(buffer: ArrayBuffer, options: AnimationParseOptions): ParsedAnimation | undefined {
|
|
190
|
-
if (buffer.byteLength < 4) return undefined;
|
|
191
|
-
const header = new DataView(buffer);
|
|
192
|
-
if (header.getUint32(0, true) !== MAGIC) return undefined;
|
|
193
|
-
return decodeAnimationBinaryHeader(buffer, header, options);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
function decodeAnimationBinaryHeader(
|
|
197
|
-
buffer: ArrayBuffer,
|
|
198
|
-
header: DataView,
|
|
199
|
-
options: AnimationParseOptions,
|
|
200
|
-
): ParsedAnimation {
|
|
201
|
-
const limits = resolveAnimationParseLimits(options);
|
|
202
|
-
if (buffer.byteLength > limits.maxInputBytes) {
|
|
203
|
-
throw new AnimationFormatError('E_ANIMATION_LIMIT_EXCEEDED', `Binary input exceeds ${limits.maxInputBytes} bytes.`, '$binary');
|
|
204
|
-
}
|
|
205
|
-
if (buffer.byteLength < HEADER_BYTES) invalidBinary('Header is truncated.');
|
|
206
|
-
const major = header.getUint16(4, true);
|
|
207
|
-
const minor = header.getUint16(6, true);
|
|
208
|
-
if (major !== LEGACY_CONTAINER_MAJOR && major !== CONTAINER_MAJOR) {
|
|
209
|
-
invalidBinary(`Unsupported container version ${major}.${minor}.`);
|
|
210
|
-
}
|
|
211
|
-
if (minor !== 0) invalidBinary(`Unsupported container version ${major}.${minor}.`);
|
|
212
|
-
const metadataOffset = header.getUint32(8, true);
|
|
213
|
-
const metadataLength = header.getUint32(12, true);
|
|
214
|
-
const floatOffset = header.getUint32(16, true);
|
|
215
|
-
const floatCount = header.getUint32(20, true);
|
|
216
|
-
if (metadataLength > limits.maxMetadataBytes) {
|
|
217
|
-
throw new AnimationFormatError('E_ANIMATION_LIMIT_EXCEEDED', `Binary metadata exceeds ${limits.maxMetadataBytes} bytes.`, '$binary.metadata');
|
|
218
|
-
}
|
|
219
|
-
if (metadataOffset < HEADER_BYTES || metadataOffset + metadataLength > buffer.byteLength) invalidBinary('Metadata range is outside the buffer.');
|
|
220
|
-
if ((floatOffset & 3) !== 0 || floatOffset < metadataOffset + metadataLength || floatOffset + floatCount * 4 > buffer.byteLength) {
|
|
221
|
-
invalidBinary('Float pool range is invalid or unaligned.');
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
let metadata: unknown;
|
|
225
|
-
try {
|
|
226
|
-
metadata = JSON.parse(UTF8_DECODER.decode(new Uint8Array(buffer, metadataOffset, metadataLength)));
|
|
227
|
-
} catch (error) {
|
|
228
|
-
invalidBinary(`Metadata JSON cannot be decoded: ${error instanceof Error ? error.message : String(error)}.`);
|
|
229
|
-
}
|
|
230
|
-
const floatPool = new Float32Array(buffer, floatOffset, floatCount);
|
|
231
|
-
return major === LEGACY_CONTAINER_MAJOR
|
|
232
|
-
? decodeLegacyMetadata(metadata, floatPool, buffer, options)
|
|
233
|
-
: decodeCompactMetadata(metadata, floatPool, buffer, options, limits);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
function writeBinary(metadata: string, floatPool: BinaryFloatPoolBuilder, options: AnimationParseOptions): ArrayBuffer {
|
|
237
|
-
const metadataBytes = UTF8_ENCODER.encode(metadata);
|
|
238
|
-
const metadataOffset = HEADER_BYTES;
|
|
239
|
-
const floatOffset = align4(metadataOffset + metadataBytes.byteLength);
|
|
240
|
-
const totalBytes = floatOffset + floatPool.count * 4;
|
|
241
|
-
const limits = resolveAnimationParseLimits(options);
|
|
242
|
-
if (metadataBytes.byteLength > limits.maxMetadataBytes) {
|
|
243
|
-
throw new AnimationFormatError('E_ANIMATION_LIMIT_EXCEEDED', `Binary metadata exceeds ${limits.maxMetadataBytes} bytes.`, '$binary.metadata');
|
|
244
|
-
}
|
|
245
|
-
if (totalBytes > limits.maxInputBytes) {
|
|
246
|
-
throw new AnimationFormatError('E_ANIMATION_LIMIT_EXCEEDED', `Binary output exceeds ${limits.maxInputBytes} bytes.`, '$binary');
|
|
247
|
-
}
|
|
248
|
-
const buffer = new ArrayBuffer(totalBytes);
|
|
249
|
-
const header = new DataView(buffer);
|
|
250
|
-
header.setUint32(0, MAGIC, true);
|
|
251
|
-
header.setUint16(4, CONTAINER_MAJOR, true);
|
|
252
|
-
header.setUint16(6, CONTAINER_MINOR, true);
|
|
253
|
-
header.setUint32(8, metadataOffset, true);
|
|
254
|
-
header.setUint32(12, metadataBytes.byteLength, true);
|
|
255
|
-
header.setUint32(16, floatOffset, true);
|
|
256
|
-
header.setUint32(20, floatPool.count, true);
|
|
257
|
-
new Uint8Array(buffer, metadataOffset, metadataBytes.byteLength).set(metadataBytes);
|
|
258
|
-
const output = new Float32Array(buffer, floatOffset, floatPool.count);
|
|
259
|
-
for (const block of floatPool.blocks) output.set(block.values, block.range[0]);
|
|
260
|
-
return buffer;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
function encodeResource(resource: Readonly<AnimationResource>, strings: BinaryStringTableBuilder): unknown[] {
|
|
264
|
-
return [
|
|
265
|
-
resource.type === 'image' ? 0 : resource.type === 'audio' ? 1 : 2,
|
|
266
|
-
strings.add(resource.id),
|
|
267
|
-
strings.add(resource.uri),
|
|
268
|
-
resource.type === 'image' ? resource.width ?? 0 : 0,
|
|
269
|
-
resource.type === 'image' ? resource.height ?? 0 : 0,
|
|
270
|
-
strings.add(resource.mimeType),
|
|
271
|
-
strings.add(resource.integrity),
|
|
272
|
-
resource.type === 'image' ? resource.colorSpace === undefined ? -1 : resource.colorSpace === 'srgb' ? 0 : 1 : -1,
|
|
273
|
-
];
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
function encodeTransform(transform: Readonly<AnimationTransform2D>): unknown[] {
|
|
277
|
-
return [
|
|
278
|
-
transform.position ?? 0,
|
|
279
|
-
transform.rotation ?? null,
|
|
280
|
-
transform.scale ?? 0,
|
|
281
|
-
transform.anchor ?? 0,
|
|
282
|
-
transform.opacity ?? -1,
|
|
283
|
-
];
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
function encodeComposite(
|
|
287
|
-
composite: Readonly<AnimationComposite>,
|
|
288
|
-
nodeIndices: ReadonlyMap<string, number>,
|
|
289
|
-
floats: BinaryFloatPoolBuilder,
|
|
290
|
-
): unknown[] {
|
|
291
|
-
const layers = 'layers' in composite ? composite.layers : [composite];
|
|
292
|
-
// Keep the original three-field encoding for the common legacy-compatible case.
|
|
293
|
-
if (layers.length === 1 && layers[0]!.operation === undefined
|
|
294
|
-
&& layers[0]!.feather === undefined && layers[0]!.expansion === undefined
|
|
295
|
-
&& layers[0]!.expansionTrack === undefined
|
|
296
|
-
&& (layers[0]!.mode === 'alpha' || layers[0]!.mode === 'alpha-inverted')) {
|
|
297
|
-
const layer = layers[0]!;
|
|
298
|
-
return [
|
|
299
|
-
COMPOSITE_KINDS.indexOf(layer.kind),
|
|
300
|
-
requiredMapIndex(nodeIndices, layer.source, 'composite source'),
|
|
301
|
-
COMPOSITE_MODES.indexOf(layer.mode),
|
|
302
|
-
];
|
|
303
|
-
}
|
|
304
|
-
return layers.map(layer => [
|
|
305
|
-
COMPOSITE_KINDS.indexOf(layer.kind),
|
|
306
|
-
requiredMapIndex(nodeIndices, layer.source, 'composite source'),
|
|
307
|
-
COMPOSITE_MODES.indexOf(layer.mode),
|
|
308
|
-
COMPOSITE_OPERATIONS.indexOf(layer.operation ?? 'add'),
|
|
309
|
-
layer.feather ?? 0,
|
|
310
|
-
layer.expansion ?? 0,
|
|
311
|
-
encodeOptionalVectorTrack(layer.expansionTrack, floats),
|
|
312
|
-
]);
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
function encodeComponent(
|
|
316
|
-
component: Readonly<AnimationComponent>,
|
|
317
|
-
floats: BinaryFloatPoolBuilder,
|
|
318
|
-
strings: BinaryStringTableBuilder,
|
|
319
|
-
): unknown[] {
|
|
320
|
-
if (component.type === 'shape2d') {
|
|
321
|
-
return [
|
|
322
|
-
0,
|
|
323
|
-
component.shape === 'rect' ? 0 : 1,
|
|
324
|
-
component.size,
|
|
325
|
-
component.position ?? 0,
|
|
326
|
-
component.fill,
|
|
327
|
-
];
|
|
328
|
-
}
|
|
329
|
-
if (isPath2DComponent(component)) {
|
|
330
|
-
return [
|
|
331
|
-
1,
|
|
332
|
-
strings.add(component.commands),
|
|
333
|
-
floats.add(component.values, true),
|
|
334
|
-
component.fill,
|
|
335
|
-
component.fillRule === 'evenodd' ? 1 : 0,
|
|
336
|
-
component.tolerance ?? -1,
|
|
337
|
-
];
|
|
338
|
-
}
|
|
339
|
-
if (isCompactVectorStrokeComponent(component)) {
|
|
340
|
-
return [
|
|
341
|
-
3,
|
|
342
|
-
strings.add(component.commands),
|
|
343
|
-
component.values === undefined ? 0 : floats.add(component.values, true),
|
|
344
|
-
component.sourceComponent ?? -1,
|
|
345
|
-
component.color,
|
|
346
|
-
component.width,
|
|
347
|
-
component.lineCap === 'butt' ? 0 : component.lineCap === 'round' ? 1 : 2,
|
|
348
|
-
component.lineJoin === 'miter' ? 0 : component.lineJoin === 'round' ? 1 : 2,
|
|
349
|
-
component.miterLimit,
|
|
350
|
-
component.tolerance ?? -1,
|
|
351
|
-
];
|
|
352
|
-
}
|
|
353
|
-
if (isCompactVectorPathMorphComponent(component)) {
|
|
354
|
-
return [
|
|
355
|
-
4,
|
|
356
|
-
strings.add(component.commands),
|
|
357
|
-
floats.add(component.times),
|
|
358
|
-
floats.add(component.values, true),
|
|
359
|
-
component.valueSize,
|
|
360
|
-
component.interpolation === 'step' ? 0 : component.interpolation === 'linear' ? 1 : 2,
|
|
361
|
-
component.easings === undefined ? 0 : floats.add(component.easings),
|
|
362
|
-
component.fill,
|
|
363
|
-
component.fillRule === 'evenodd' ? 1 : 0,
|
|
364
|
-
component.tolerance ?? -1,
|
|
365
|
-
];
|
|
366
|
-
}
|
|
367
|
-
if (isAnimationVectorShapeComponent(component)) {
|
|
368
|
-
return [
|
|
369
|
-
5,
|
|
370
|
-
strings.add(component.commands),
|
|
371
|
-
floats.add(component.values, true),
|
|
372
|
-
encodeOptionalVectorTrack(component.morph, floats, true),
|
|
373
|
-
component.fill ? encodeVectorFill(component.fill, floats) : 0,
|
|
374
|
-
component.stroke ? encodeVectorStroke(component.stroke, floats) : 0,
|
|
375
|
-
component.fillRule === 'evenodd' ? 1 : 0,
|
|
376
|
-
component.tolerance ?? -1,
|
|
377
|
-
component.modifiers ? component.modifiers.map(modifier => encodeVectorModifier(modifier, floats)) : 0,
|
|
378
|
-
component.morphRelative === true ? 1 : 0,
|
|
379
|
-
];
|
|
380
|
-
}
|
|
381
|
-
if (component.type === 'sprite2d') return encodeSpriteComponent(
|
|
382
|
-
component as Readonly<AnimationSprite2DComponent>,
|
|
383
|
-
floats,
|
|
384
|
-
strings,
|
|
385
|
-
);
|
|
386
|
-
if (component.type === 'text2d') return encodeTextComponent(component as AnimationText2DComponent, floats, strings);
|
|
387
|
-
return [2, component];
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
function encodeSpriteComponent(
|
|
391
|
-
component: Readonly<AnimationSprite2DComponent>,
|
|
392
|
-
floats: BinaryFloatPoolBuilder,
|
|
393
|
-
strings: BinaryStringTableBuilder,
|
|
394
|
-
): unknown[] {
|
|
395
|
-
return [
|
|
396
|
-
7,
|
|
397
|
-
strings.add(component.resource),
|
|
398
|
-
component.size,
|
|
399
|
-
component.position ?? 0,
|
|
400
|
-
component.tint ?? 0,
|
|
401
|
-
component.uvRect ?? 0,
|
|
402
|
-
encodeOptionalVectorTrack(component.uvRectTrack, floats),
|
|
403
|
-
];
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
function encodeTextComponent(
|
|
407
|
-
component: Readonly<AnimationText2DComponent>,
|
|
408
|
-
floats: BinaryFloatPoolBuilder,
|
|
409
|
-
strings: BinaryStringTableBuilder,
|
|
410
|
-
): unknown[] {
|
|
411
|
-
return [
|
|
412
|
-
6,
|
|
413
|
-
strings.add(component.text), component.size, component.position ?? 0,
|
|
414
|
-
strings.add(component.fontFamily), component.fontSize ?? 0,
|
|
415
|
-
encodeFontWeight(component.fontWeight, strings),
|
|
416
|
-
component.fontStyle === undefined ? -1 : component.fontStyle === 'normal' ? 0 : 1,
|
|
417
|
-
strings.add(component.fontResource), component.lineHeight ?? 0, component.tracking ?? null,
|
|
418
|
-
component.textAlign === undefined ? -1 : ['left', 'center', 'right'].indexOf(component.textAlign),
|
|
419
|
-
component.verticalAlign === undefined ? -1 : ['top', 'middle', 'bottom'].indexOf(component.verticalAlign),
|
|
420
|
-
component.color, component.backgroundColor ?? 0, component.padding ?? -1,
|
|
421
|
-
component.resolutionScale ?? 0,
|
|
422
|
-
component.documents?.map(document => [
|
|
423
|
-
document.time, strings.add(document.text), strings.add(document.fontFamily), document.fontSize ?? 0,
|
|
424
|
-
encodeFontWeight(document.fontWeight, strings),
|
|
425
|
-
document.fontStyle === undefined ? -1 : document.fontStyle === 'normal' ? 0 : 1,
|
|
426
|
-
strings.add(document.fontResource), document.lineHeight ?? 0, document.tracking ?? null,
|
|
427
|
-
document.textAlign === undefined ? -1 : ['left', 'center', 'right'].indexOf(document.textAlign),
|
|
428
|
-
document.color ?? 0,
|
|
429
|
-
]) ?? 0,
|
|
430
|
-
component.animators?.map(animator => encodeTextAnimator(animator, floats)) ?? 0,
|
|
431
|
-
];
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
function encodeFontWeight(value: string | number | undefined, strings: BinaryStringTableBuilder): unknown {
|
|
435
|
-
if (value === undefined) return null;
|
|
436
|
-
return typeof value === 'number' ? value : [-1, strings.add(value)];
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
function encodeTextAnimator(animator: Readonly<AnimationTextAnimator>, floats: BinaryFloatPoolBuilder): unknown[] {
|
|
440
|
-
const selector = animator.selector;
|
|
441
|
-
return [[
|
|
442
|
-
selector.start, selector.end, selector.offset ?? 0,
|
|
443
|
-
selector.units === 'index' ? 1 : 0, selector.amount ?? 1,
|
|
444
|
-
TEXT_SELECTOR_SHAPES.indexOf(selector.shape ?? 'square'),
|
|
445
|
-
encodeOptionalVectorTrack(selector.startTrack, floats),
|
|
446
|
-
encodeOptionalVectorTrack(selector.endTrack, floats),
|
|
447
|
-
encodeOptionalVectorTrack(selector.offsetTrack, floats),
|
|
448
|
-
encodeOptionalVectorTrack(selector.amountTrack, floats),
|
|
449
|
-
selector.basedOn === undefined ? -1 : TEXT_SELECTOR_BASES.indexOf(selector.basedOn),
|
|
450
|
-
selector.easing ?? 0,
|
|
451
|
-
selector.smoothness ?? -1,
|
|
452
|
-
selector.randomSeed ?? null,
|
|
453
|
-
],
|
|
454
|
-
animator.position ?? 0, animator.scale ?? 0, animator.rotation ?? null,
|
|
455
|
-
animator.opacity ?? -1, animator.fillColor ?? 0, animator.tracking ?? null,
|
|
456
|
-
encodeOptionalVectorTrack(animator.positionTrack, floats),
|
|
457
|
-
encodeOptionalVectorTrack(animator.scaleTrack, floats),
|
|
458
|
-
encodeOptionalVectorTrack(animator.rotationTrack, floats),
|
|
459
|
-
encodeOptionalVectorTrack(animator.opacityTrack, floats),
|
|
460
|
-
encodeOptionalVectorTrack(animator.fillColorTrack, floats),
|
|
461
|
-
encodeOptionalVectorTrack(animator.trackingTrack, floats)];
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
function encodeLayerEffects(effects: readonly Readonly<AnimationLayerEffect>[], floats: BinaryFloatPoolBuilder): unknown[] {
|
|
465
|
-
return effects.map(effect => {
|
|
466
|
-
if (effect.kind === 'tint') return [
|
|
467
|
-
0, effect.black, effect.white, effect.amount,
|
|
468
|
-
encodeOptionalVectorTrack(effect.blackTrack, floats),
|
|
469
|
-
encodeOptionalVectorTrack(effect.whiteTrack, floats),
|
|
470
|
-
encodeOptionalVectorTrack(effect.amountTrack, floats),
|
|
471
|
-
];
|
|
472
|
-
if (effect.kind === 'fill') return [
|
|
473
|
-
1, effect.color, effect.opacity ?? 1,
|
|
474
|
-
encodeOptionalVectorTrack(effect.colorTrack, floats),
|
|
475
|
-
encodeOptionalVectorTrack(effect.opacityTrack, floats),
|
|
476
|
-
];
|
|
477
|
-
if (effect.kind === 'opacity') return [2, effect.opacity, encodeOptionalVectorTrack(effect.opacityTrack, floats)];
|
|
478
|
-
if (effect.kind === 'color-matrix') return [
|
|
479
|
-
3, floats.add(effect.matrix), encodeOptionalVectorTrack(effect.matrixTrack, floats),
|
|
480
|
-
];
|
|
481
|
-
if (effect.kind === 'blur') return [4, effect.radius, encodeOptionalVectorTrack(effect.radiusTrack, floats)];
|
|
482
|
-
return [
|
|
483
|
-
5, effect.color, effect.opacity, effect.offset, effect.blur,
|
|
484
|
-
encodeOptionalVectorTrack(effect.colorTrack, floats),
|
|
485
|
-
encodeOptionalVectorTrack(effect.opacityTrack, floats),
|
|
486
|
-
encodeOptionalVectorTrack(effect.offsetTrack, floats),
|
|
487
|
-
encodeOptionalVectorTrack(effect.blurTrack, floats),
|
|
488
|
-
];
|
|
489
|
-
});
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
function encodeVectorModifier(
|
|
493
|
-
modifier: Readonly<import('./types').AnimationVectorPathModifier>,
|
|
494
|
-
floats: BinaryFloatPoolBuilder,
|
|
495
|
-
): unknown[] {
|
|
496
|
-
if (modifier.kind === 'round-corners') return [
|
|
497
|
-
1, modifier.radius, encodeOptionalVectorTrack(modifier.radiusTrack, floats),
|
|
498
|
-
];
|
|
499
|
-
return [
|
|
500
|
-
0, modifier.start, modifier.end, modifier.offset, modifier.mode === 'individual' ? 1 : 0,
|
|
501
|
-
encodeOptionalVectorTrack(modifier.startTrack, floats),
|
|
502
|
-
encodeOptionalVectorTrack(modifier.endTrack, floats),
|
|
503
|
-
encodeOptionalVectorTrack(modifier.offsetTrack, floats),
|
|
504
|
-
];
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
function isAnimationVectorShapeComponent(component: AnimationComponent): component is AnimationVectorShapeComponent {
|
|
508
|
-
return component.type === ANIMATION_VECTOR_SHAPE_EXTENSION_ID
|
|
509
|
-
&& typeof (component as Partial<AnimationVectorShapeComponent>).commands === 'string'
|
|
510
|
-
&& (Array.isArray((component as Partial<AnimationVectorShapeComponent>).values)
|
|
511
|
-
|| (component as Partial<AnimationVectorShapeComponent>).values instanceof Float32Array);
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
function encodeVectorTrack(
|
|
515
|
-
track: Readonly<AnimationVectorValueTrack>,
|
|
516
|
-
floats: BinaryFloatPoolBuilder,
|
|
517
|
-
deduplicateValues = false,
|
|
518
|
-
): unknown[] {
|
|
519
|
-
return [
|
|
520
|
-
floats.add(track.times, true),
|
|
521
|
-
floats.add(track.values, deduplicateValues),
|
|
522
|
-
track.valueSize,
|
|
523
|
-
INTERPOLATIONS.indexOf(track.interpolation),
|
|
524
|
-
track.easings ? floats.add(track.easings, true) : 0,
|
|
525
|
-
];
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
function encodeOptionalVectorTrack(
|
|
529
|
-
track: Readonly<AnimationVectorValueTrack> | undefined,
|
|
530
|
-
floats: BinaryFloatPoolBuilder,
|
|
531
|
-
deduplicateValues = false,
|
|
532
|
-
): unknown[] | 0 {
|
|
533
|
-
return track ? encodeVectorTrack(track, floats, deduplicateValues) : 0;
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
function encodeVectorFill(
|
|
537
|
-
fill: Readonly<AnimationVectorSolidPaint | AnimationVectorGradientPaint>,
|
|
538
|
-
floats: BinaryFloatPoolBuilder,
|
|
539
|
-
): unknown[] {
|
|
540
|
-
if (fill.kind === 'solid') return [
|
|
541
|
-
0,
|
|
542
|
-
fill.color,
|
|
543
|
-
encodeOptionalVectorTrack(fill.colorTrack, floats),
|
|
544
|
-
encodeOptionalVectorTrack(fill.opacityTrack, floats),
|
|
545
|
-
fill.opacity ?? 1,
|
|
546
|
-
];
|
|
547
|
-
return [
|
|
548
|
-
fill.kind === 'linear-gradient' ? 1 : 2,
|
|
549
|
-
fill.start,
|
|
550
|
-
fill.end,
|
|
551
|
-
floats.add(fill.stops),
|
|
552
|
-
encodeOptionalVectorTrack(fill.startTrack, floats),
|
|
553
|
-
encodeOptionalVectorTrack(fill.endTrack, floats),
|
|
554
|
-
encodeOptionalVectorTrack(fill.stopsTrack, floats),
|
|
555
|
-
encodeOptionalVectorTrack(fill.opacityTrack, floats),
|
|
556
|
-
fill.opacity ?? 1,
|
|
557
|
-
];
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
function encodeVectorStroke(stroke: Readonly<AnimationVectorStrokePaint>, floats: BinaryFloatPoolBuilder): unknown[] {
|
|
561
|
-
return [
|
|
562
|
-
stroke.color,
|
|
563
|
-
stroke.width,
|
|
564
|
-
['butt', 'round', 'square'].indexOf(stroke.lineCap),
|
|
565
|
-
['miter', 'round', 'bevel'].indexOf(stroke.lineJoin),
|
|
566
|
-
stroke.miterLimit,
|
|
567
|
-
stroke.dash ? floats.add(stroke.dash) : 0,
|
|
568
|
-
stroke.dashOffset ?? 0,
|
|
569
|
-
encodeOptionalVectorTrack(stroke.colorTrack, floats),
|
|
570
|
-
encodeOptionalVectorTrack(stroke.opacityTrack, floats),
|
|
571
|
-
encodeOptionalVectorTrack(stroke.widthTrack, floats),
|
|
572
|
-
encodeOptionalVectorTrack(stroke.dashOffsetTrack, floats),
|
|
573
|
-
stroke.gradient ? encodeVectorFill(stroke.gradient, floats) : 0,
|
|
574
|
-
stroke.opacity ?? 1,
|
|
575
|
-
];
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
function decodeCompactMetadata(
|
|
579
|
-
value: unknown,
|
|
580
|
-
floatPool: Float32Array,
|
|
581
|
-
buffer: ArrayBuffer,
|
|
582
|
-
options: AnimationParseOptions,
|
|
583
|
-
limits: ReturnType<typeof resolveAnimationParseLimits>,
|
|
584
|
-
): ParsedAnimation {
|
|
585
|
-
const root = compactArray(value, '$binary.metadata', 13);
|
|
586
|
-
const strings = compactArray(root[6], '$binary.metadata[6]').map((item, index) => (
|
|
587
|
-
requiredString(item, `$binary.metadata[6][${index}]`)
|
|
588
|
-
));
|
|
589
|
-
const encodedResources = compactArray(root[7], '$binary.metadata[7]');
|
|
590
|
-
binaryLimit(encodedResources.length, limits.maxResources, 'resources', '$binary.metadata[7]');
|
|
591
|
-
const resourceIds = new Set<string>();
|
|
592
|
-
const resources = encodedResources.map((item, index) => parseBinaryResourceValue(
|
|
593
|
-
decodeResource(item, strings, `$binary.metadata[7][${index}]`), index, resourceIds,
|
|
594
|
-
));
|
|
595
|
-
const encodedNodeValues = compactArray(root[8], '$binary.metadata[8]');
|
|
596
|
-
const encodedNodes = encodedNodeValues.map((item, index) => (
|
|
597
|
-
compactArray(item, `$binary.metadata[8][${index}]`, 9)
|
|
598
|
-
));
|
|
599
|
-
binaryLimit(encodedNodes.length, limits.maxNodes, 'nodes', '$binary.metadata[8]');
|
|
600
|
-
const nodeIds = encodedNodes.map((node, index) => requiredNonEmptyString(
|
|
601
|
-
indexedString(strings, node[0], `$binary.metadata[8][${index}][0]`),
|
|
602
|
-
`$binary.metadata[8][${index}][0]`,
|
|
603
|
-
));
|
|
604
|
-
const parsedNodeIds = new Set<string>();
|
|
605
|
-
for (let index = 0; index < nodeIds.length; index++) {
|
|
606
|
-
const id = nodeIds[index]!;
|
|
607
|
-
if (parsedNodeIds.has(id)) invalidBinary(`Duplicate node id "${id}" at $binary.metadata[8][${index}][0].`);
|
|
608
|
-
parsedNodeIds.add(id);
|
|
609
|
-
}
|
|
610
|
-
const parentIndices = validateCompactNodeGraph(encodedNodes, nodeIds.length);
|
|
611
|
-
let componentCount = 0;
|
|
612
|
-
let pathValueCount = 0;
|
|
613
|
-
let textCharacterCount = 0;
|
|
614
|
-
let particleCapacity = 0;
|
|
615
|
-
const nodes = encodedNodes.map((item, index) => {
|
|
616
|
-
const node = decodeNode(
|
|
617
|
-
item, index, strings, nodeIds, parentIndices[index], floatPool, options,
|
|
618
|
-
(kind, count) => {
|
|
619
|
-
if (kind === 'path') {
|
|
620
|
-
pathValueCount += count;
|
|
621
|
-
binaryLimit(pathValueCount, limits.maxPathValues, 'path values', `$.nodes[${index}].components`);
|
|
622
|
-
} else if (kind === 'text') {
|
|
623
|
-
textCharacterCount += count;
|
|
624
|
-
binaryLimit(textCharacterCount, limits.maxTextCharacters, 'text characters', `$.nodes[${index}].components`);
|
|
625
|
-
} else {
|
|
626
|
-
particleCapacity += count;
|
|
627
|
-
binaryLimit(particleCapacity, limits.maxParticleCapacity, 'particle capacity', `$.nodes[${index}].components`);
|
|
628
|
-
}
|
|
629
|
-
},
|
|
630
|
-
);
|
|
631
|
-
componentCount += node.components?.length ?? 0;
|
|
632
|
-
binaryLimit(componentCount, limits.maxComponents, 'components', `$.nodes[${index}].components`);
|
|
633
|
-
return node;
|
|
634
|
-
});
|
|
635
|
-
const encodedTracks = compactArray(root[9], '$binary.metadata[9]');
|
|
636
|
-
binaryLimit(encodedTracks.length, limits.maxTracks, 'tracks', '$binary.metadata[9]');
|
|
637
|
-
const duration = positiveFiniteNumber(root[3], '$binary.metadata[3]');
|
|
638
|
-
let keyframeCount = 0;
|
|
639
|
-
const tracks = encodedTracks.map((item, index) => {
|
|
640
|
-
const path = `$binary.metadata[9][${index}]`;
|
|
641
|
-
const track = compactArray(item, path, 6);
|
|
642
|
-
const nodeIndex = boundedIndex(track[0], nodeIds.length, `${path}[0]`);
|
|
643
|
-
const parsed = parseBinaryTrackValue({
|
|
644
|
-
node: nodeIds[nodeIndex]!,
|
|
645
|
-
property: indexedLiteral(TRACK_PROPERTIES, track[1], `${path}[1]`),
|
|
646
|
-
interpolation: indexedLiteral(INTERPOLATIONS, track[2], `${path}[2]`),
|
|
647
|
-
times: compactFloatView(floatPool, track[3], `${path}[3]`),
|
|
648
|
-
values: compactFloatView(floatPool, track[4], `${path}[4]`),
|
|
649
|
-
...(track[5] === 0 ? {} : { easings: compactFloatView(floatPool, track[5], `${path}[5]`) }),
|
|
650
|
-
...(track.length < 7 || track[6] === 0 ? {} : {
|
|
651
|
-
spatialTangents: compactFloatView(floatPool, track[6], `${path}[6]`),
|
|
652
|
-
}),
|
|
653
|
-
}, index, parsedNodeIds, duration, options.copyFloatData === true);
|
|
654
|
-
keyframeCount += parsed.times.length;
|
|
655
|
-
binaryLimit(keyframeCount, limits.maxKeyframes, 'keyframes', `$.tracks[${index}].times`);
|
|
656
|
-
return parsed;
|
|
657
|
-
});
|
|
658
|
-
const frameRate = finiteNumber(root[4], '$binary.metadata[4]');
|
|
659
|
-
if (frameRate < 0) invalidBinary('Frame rate must be positive or zero when omitted.');
|
|
660
|
-
const extensions = root[12] === 0 ? {} : binaryRecord(root[12], '$binary.metadata[12]');
|
|
661
|
-
return finalizeParsedAnimation({
|
|
662
|
-
...(root[0] === -1 ? {} : { name: indexedString(strings, root[0], '$binary.metadata[0]') }),
|
|
663
|
-
canvas: Object.freeze({
|
|
664
|
-
width: positiveFiniteNumber(root[1], '$binary.metadata[1]'),
|
|
665
|
-
height: positiveFiniteNumber(root[2], '$binary.metadata[2]'),
|
|
666
|
-
coordinateSystem: 'screen-y-down',
|
|
667
|
-
}),
|
|
668
|
-
duration,
|
|
669
|
-
...(frameRate === 0 ? {} : { frameRate }),
|
|
670
|
-
endBehavior: indexedLiteral(END_BEHAVIORS, root[5], '$binary.metadata[5]'),
|
|
671
|
-
resources,
|
|
672
|
-
nodes,
|
|
673
|
-
tracks,
|
|
674
|
-
extensionsUsed: decodeStringIndices(root[10], strings, '$binary.metadata[10]'),
|
|
675
|
-
extensionsRequired: decodeStringIndices(root[11], strings, '$binary.metadata[11]'),
|
|
676
|
-
extensions,
|
|
677
|
-
}, options, 'binary', buffer, true);
|
|
678
|
-
}
|
|
679
|
-
|
|
680
|
-
function validateCompactNodeGraph(encodedNodes: readonly unknown[][], nodeCount: number): Array<number | undefined> {
|
|
681
|
-
const compositeSources: number[][] = Array.from({ length: nodeCount }, () => []);
|
|
682
|
-
const parents = encodedNodes.map((node, index) => {
|
|
683
|
-
const parent = optionalBoundedIndex(node[2], nodeCount, `$binary.metadata[8][${index}][2]`);
|
|
684
|
-
if (node[6] !== 0) {
|
|
685
|
-
const composite = compactArray(node[6], `$binary.metadata[8][${index}][6]`);
|
|
686
|
-
const layers = typeof composite[0] === 'number' ? [composite] : composite.map((value, layerIndex) => (
|
|
687
|
-
compactArray(value, `$binary.metadata[8][${index}][6][${layerIndex}]`, 3)
|
|
688
|
-
));
|
|
689
|
-
if (layers.length < 1 || layers.length > 8) invalidBinary(`Composite stack must contain between 1 and 8 layers at $binary.metadata[8][${index}][6].`);
|
|
690
|
-
for (let layerIndex = 0; layerIndex < layers.length; layerIndex++) {
|
|
691
|
-
const source = boundedIndex(layers[layerIndex]![1], nodeCount, `$binary.metadata[8][${index}][6][${layerIndex}][1]`);
|
|
692
|
-
if (source === index) invalidBinary(`Composite source cannot reference its own node at $binary.metadata[8][${index}][6][${layerIndex}][1].`);
|
|
693
|
-
compositeSources[index]!.push(source);
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
return parent;
|
|
697
|
-
});
|
|
698
|
-
const states = new Uint8Array(nodeCount);
|
|
699
|
-
for (let index = 0; index < nodeCount; index++) {
|
|
700
|
-
if (states[index] !== 0) continue;
|
|
701
|
-
let cursor: number | undefined = index;
|
|
702
|
-
while (cursor !== undefined && states[cursor] === 0) {
|
|
703
|
-
states[cursor] = 1;
|
|
704
|
-
cursor = parents[cursor];
|
|
705
|
-
}
|
|
706
|
-
if (cursor !== undefined && states[cursor] === 1) {
|
|
707
|
-
invalidBinary(`Node hierarchy contains a cycle at $binary.metadata[8][${index}][2].`);
|
|
708
|
-
}
|
|
709
|
-
cursor = index;
|
|
710
|
-
while (cursor !== undefined && states[cursor] === 1) {
|
|
711
|
-
states[cursor] = 2;
|
|
712
|
-
cursor = parents[cursor];
|
|
713
|
-
}
|
|
714
|
-
}
|
|
715
|
-
states.fill(0);
|
|
716
|
-
const visitComposite = (index: number): void => {
|
|
717
|
-
if (states[index] === 2) return;
|
|
718
|
-
if (states[index] === 1) invalidBinary(`Composite graph contains a cycle at $binary.metadata[8][${index}][6].`);
|
|
719
|
-
states[index] = 1;
|
|
720
|
-
for (const source of compositeSources[index]!) visitComposite(source);
|
|
721
|
-
states[index] = 2;
|
|
722
|
-
};
|
|
723
|
-
for (let index = 0; index < nodeCount; index++) visitComposite(index);
|
|
724
|
-
return parents;
|
|
725
|
-
}
|
|
726
|
-
|
|
727
|
-
function decodeResource(value: unknown, strings: readonly string[], path: string): Record<string, unknown> {
|
|
728
|
-
const resource = compactArray(value, path, 8);
|
|
729
|
-
const type = indexedLiteral(['image', 'audio', 'binary'] as const, resource[0], `${path}[0]`);
|
|
730
|
-
const id = indexedString(strings, resource[1], `${path}[1]`);
|
|
731
|
-
const uri = indexedString(strings, resource[2], `${path}[2]`);
|
|
732
|
-
const mimeType = optionalIndexedString(strings, resource[5], `${path}[5]`);
|
|
733
|
-
const integrity = optionalIndexedString(strings, resource[6], `${path}[6]`);
|
|
734
|
-
if (type === 'image') {
|
|
735
|
-
const colorCode = finiteInteger(resource[7], `${path}[7]`);
|
|
736
|
-
if (colorCode < -1 || colorCode > 1) invalidBinary(`Invalid image color-space code at ${path}[7].`);
|
|
737
|
-
return {
|
|
738
|
-
id, type, uri,
|
|
739
|
-
...(resource[3] === 0 ? {} : { width: resource[3] }),
|
|
740
|
-
...(resource[4] === 0 ? {} : { height: resource[4] }),
|
|
741
|
-
...(mimeType === undefined ? {} : { mimeType }),
|
|
742
|
-
...(integrity === undefined ? {} : { integrity }),
|
|
743
|
-
...(colorCode === -1 ? {} : { colorSpace: colorCode === 0 ? 'srgb' : 'linear' }),
|
|
744
|
-
};
|
|
745
|
-
}
|
|
746
|
-
return {
|
|
747
|
-
id, type, uri,
|
|
748
|
-
...(mimeType === undefined ? {} : { mimeType }),
|
|
749
|
-
...(integrity === undefined ? {} : { integrity }),
|
|
750
|
-
};
|
|
751
|
-
}
|
|
752
|
-
|
|
753
|
-
function decodeNode(
|
|
754
|
-
node: unknown[],
|
|
755
|
-
index: number,
|
|
756
|
-
strings: readonly string[],
|
|
757
|
-
nodeIds: readonly string[],
|
|
758
|
-
parentIndex: number | undefined,
|
|
759
|
-
floatPool: Float32Array,
|
|
760
|
-
options: AnimationParseOptions,
|
|
761
|
-
countBudget: (kind: 'path' | 'text' | 'particle', count: number) => void,
|
|
762
|
-
): Readonly<AnimationNode> {
|
|
763
|
-
const path = `$binary.metadata[8][${index}]`;
|
|
764
|
-
const name = optionalIndexedString(strings, node[1], `${path}[1]`);
|
|
765
|
-
const start = node[3] === -1 ? undefined : nonNegativeFiniteNumber(node[3], `${path}[3]`);
|
|
766
|
-
const duration = node[4] === -1 ? undefined : positiveFiniteNumber(node[4], `${path}[4]`);
|
|
767
|
-
const composite = node[6] === 0 ? undefined : decodeComposite(node[6], nodeIds, floatPool, `${path}[6]`);
|
|
768
|
-
const components = node[7] === 0 ? undefined : compactArray(node[7], `${path}[7]`).map((component, componentIndex) => (
|
|
769
|
-
decodeComponent(
|
|
770
|
-
component, strings, floatPool, `${path}[7][${componentIndex}]`,
|
|
771
|
-
`$.nodes[${index}].components[${componentIndex}]`, options, countBudget,
|
|
772
|
-
)
|
|
773
|
-
));
|
|
774
|
-
const effects = node.length < 10 || node[9] === 0 ? undefined : parseBinaryEffectsValue(
|
|
775
|
-
decodeLayerEffects(node[9], floatPool, `${path}[9]`),
|
|
776
|
-
`$.nodes[${index}].effects`,
|
|
777
|
-
options.copyFloatData === true,
|
|
778
|
-
);
|
|
779
|
-
return Object.freeze({
|
|
780
|
-
id: nodeIds[index]!,
|
|
781
|
-
...(name === undefined ? {} : { name }),
|
|
782
|
-
...(parentIndex === undefined ? {} : { parent: nodeIds[parentIndex]! }),
|
|
783
|
-
...(start === undefined || start === 0 ? {} : { start }),
|
|
784
|
-
...(duration === undefined ? {} : { duration }),
|
|
785
|
-
...(node[5] === 0 ? {} : { transform: parseBinaryTransformValue(decodeTransform(node[5], `${path}[5]`), `$.nodes[${index}].transform`) }),
|
|
786
|
-
...(composite === undefined ? {} : { composite }),
|
|
787
|
-
...(effects === undefined ? {} : { effects }),
|
|
788
|
-
...(components === undefined ? {} : { components: Object.freeze(components) }),
|
|
789
|
-
...(node[8] === 0 ? {} : { extensions: Object.freeze({ ...binaryRecord(node[8], `${path}[8]`) }) }),
|
|
790
|
-
});
|
|
791
|
-
}
|
|
792
|
-
|
|
793
|
-
function decodeTransform(value: unknown, path: string): Record<string, unknown> {
|
|
794
|
-
const transform = compactArray(value, path, 5);
|
|
795
|
-
return {
|
|
796
|
-
...(transform[0] === 0 ? {} : { position: transform[0] }),
|
|
797
|
-
...(transform[1] === null ? {} : { rotation: transform[1] }),
|
|
798
|
-
...(transform[2] === 0 ? {} : { scale: transform[2] }),
|
|
799
|
-
...(transform[3] === 0 ? {} : { anchor: transform[3] }),
|
|
800
|
-
...(transform[4] === -1 ? {} : { opacity: transform[4] }),
|
|
801
|
-
};
|
|
802
|
-
}
|
|
803
|
-
|
|
804
|
-
function decodeComponent(
|
|
805
|
-
value: unknown,
|
|
806
|
-
strings: readonly string[],
|
|
807
|
-
floatPool: Float32Array,
|
|
808
|
-
path: string,
|
|
809
|
-
validationPath: string,
|
|
810
|
-
options: AnimationParseOptions,
|
|
811
|
-
countBudget: (kind: 'path' | 'text' | 'particle', count: number) => void,
|
|
812
|
-
): AnimationComponent {
|
|
813
|
-
const component = compactArray(value, path, 2);
|
|
814
|
-
const code = finiteInteger(component[0], `${path}[0]`);
|
|
815
|
-
if (code === 0) {
|
|
816
|
-
if (component.length < 5) invalidBinary(`Compact shape component is truncated at ${path}.`);
|
|
817
|
-
return Object.freeze({
|
|
818
|
-
type: 'shape2d',
|
|
819
|
-
shape: indexedLiteral(['rect', 'ellipse'] as const, component[1], `${path}[1]`),
|
|
820
|
-
size: positiveFiniteTuple(component[2], 2, `${path}[2]`) as unknown as readonly [number, number],
|
|
821
|
-
...(component[3] === 0 ? {} : { position: finiteTuple(component[3], 2, `${path}[3]`) as unknown as readonly [number, number] }),
|
|
822
|
-
fill: unitFiniteTuple(component[4], 4, `${path}[4]`) as unknown as readonly [number, number, number, number],
|
|
823
|
-
});
|
|
824
|
-
}
|
|
825
|
-
if (code === 1) {
|
|
826
|
-
if (component.length < 6) invalidBinary(`Compact path component is truncated at ${path}.`);
|
|
827
|
-
return parseBinaryComponentValue({
|
|
828
|
-
type: 'path2d',
|
|
829
|
-
commands: indexedString(strings, component[1], `${path}[1]`),
|
|
830
|
-
values: compactFloatView(floatPool, component[2], `${path}[2]`),
|
|
831
|
-
fill: component[3],
|
|
832
|
-
fillRule: indexedLiteral(['nonzero', 'evenodd'] as const, component[4], `${path}[4]`),
|
|
833
|
-
...(component[5] === -1 ? {} : { tolerance: component[5] }),
|
|
834
|
-
}, validationPath, options, countBudget);
|
|
835
|
-
}
|
|
836
|
-
if (code === 2) {
|
|
837
|
-
return parseBinaryComponentValue(binaryRecord(component[1], `${path}[1]`), validationPath, options, countBudget);
|
|
838
|
-
}
|
|
839
|
-
if (code === 3) {
|
|
840
|
-
if (component.length < 10) invalidBinary(`Compact Lottie stroke component is truncated at ${path}.`);
|
|
841
|
-
const commands = optionalIndexedString(strings, component[1], `${path}[1]`);
|
|
842
|
-
const sourceComponent = finiteInteger(component[3], `${path}[3]`);
|
|
843
|
-
return parseBinaryComponentValue({
|
|
844
|
-
type: VECTOR_STROKE_EXTENSION,
|
|
845
|
-
...(commands === undefined ? {} : { commands }),
|
|
846
|
-
// The versioned Lottie stroke extension currently defines runtime values as an ordinary
|
|
847
|
-
// number array; keep the on-disk Float32 packing without changing that extension contract.
|
|
848
|
-
...(component[2] === 0 ? {} : { values: Array.from(compactFloatView(floatPool, component[2], `${path}[2]`)) }),
|
|
849
|
-
...(sourceComponent === -1 ? {} : { sourceComponent }),
|
|
850
|
-
color: component[4],
|
|
851
|
-
width: component[5],
|
|
852
|
-
lineCap: indexedLiteral(['butt', 'round', 'square'] as const, component[6], `${path}[6]`),
|
|
853
|
-
lineJoin: indexedLiteral(['miter', 'round', 'bevel'] as const, component[7], `${path}[7]`),
|
|
854
|
-
miterLimit: component[8],
|
|
855
|
-
...(component[9] === -1 ? {} : { tolerance: component[9] }),
|
|
856
|
-
}, validationPath, options, countBudget);
|
|
857
|
-
}
|
|
858
|
-
if (code === 4) {
|
|
859
|
-
if (component.length < 10) invalidBinary(`Compact Lottie path morph component is truncated at ${path}.`);
|
|
860
|
-
return parseBinaryComponentValue({
|
|
861
|
-
type: VECTOR_PATH_MORPH_EXTENSION,
|
|
862
|
-
commands: indexedString(strings, component[1], `${path}[1]`),
|
|
863
|
-
times: Array.from(compactFloatView(floatPool, component[2], `${path}[2]`)),
|
|
864
|
-
values: Array.from(compactFloatView(floatPool, component[3], `${path}[3]`)),
|
|
865
|
-
valueSize: finiteInteger(component[4], `${path}[4]`),
|
|
866
|
-
interpolation: indexedLiteral(INTERPOLATIONS, component[5], `${path}[5]`),
|
|
867
|
-
...(component[6] === 0 ? {} : { easings: Array.from(compactFloatView(floatPool, component[6], `${path}[6]`)) }),
|
|
868
|
-
fill: component[7],
|
|
869
|
-
fillRule: indexedLiteral(['nonzero', 'evenodd'] as const, component[8], `${path}[8]`),
|
|
870
|
-
...(component[9] === -1 ? {} : { tolerance: component[9] }),
|
|
871
|
-
}, validationPath, options, countBudget);
|
|
872
|
-
}
|
|
873
|
-
if (code === 5) {
|
|
874
|
-
if (component.length < 8) invalidBinary(`Compact vector shape component is truncated at ${path}.`);
|
|
875
|
-
return parseBinaryComponentValue({
|
|
876
|
-
type: ANIMATION_VECTOR_SHAPE_EXTENSION_ID,
|
|
877
|
-
commands: indexedString(strings, component[1], `${path}[1]`),
|
|
878
|
-
values: compactFloatView(floatPool, component[2], `${path}[2]`),
|
|
879
|
-
...decodeVectorTrackField(component, 3, 'morph', floatPool, path),
|
|
880
|
-
...(component[4] === 0 ? {} : { fill: decodeVectorFill(component[4], floatPool, `${path}[4]`) }),
|
|
881
|
-
...(component[5] === 0 ? {} : { stroke: decodeVectorStroke(component[5], floatPool, `${path}[5]`) }),
|
|
882
|
-
fillRule: indexedLiteral(['nonzero', 'evenodd'] as const, component[6], `${path}[6]`),
|
|
883
|
-
...(component[7] === -1 ? {} : { tolerance: component[7] }),
|
|
884
|
-
...(component.length < 9 || component[8] === 0 ? {} : {
|
|
885
|
-
modifiers: compactArray(component[8], `${path}[8]`).map((modifier, index) => (
|
|
886
|
-
decodeVectorModifier(modifier, floatPool, `${path}[8][${index}]`)
|
|
887
|
-
)),
|
|
888
|
-
}),
|
|
889
|
-
...(component.length < 10 || component[9] !== 1 ? {} : { morphRelative: true }),
|
|
890
|
-
}, validationPath, options, countBudget);
|
|
891
|
-
}
|
|
892
|
-
if (code === 6) {
|
|
893
|
-
if (component.length < 19) invalidBinary(`Compact text component is truncated at ${path}.`);
|
|
894
|
-
return parseBinaryComponentValue({
|
|
895
|
-
type: 'text2d',
|
|
896
|
-
text: indexedString(strings, component[1], `${path}[1]`),
|
|
897
|
-
size: component[2],
|
|
898
|
-
...(component[3] === 0 ? {} : { position: component[3] }),
|
|
899
|
-
...decodeOptionalStringField(component[4], 'fontFamily', strings, `${path}[4]`),
|
|
900
|
-
...(component[5] === 0 ? {} : { fontSize: component[5] }),
|
|
901
|
-
...decodeFontWeightField(component[6], strings, `${path}[6]`),
|
|
902
|
-
...(component[7] === -1 ? {} : { fontStyle: indexedLiteral(['normal', 'italic'] as const, component[7], `${path}[7]`) }),
|
|
903
|
-
...decodeOptionalStringField(component[8], 'fontResource', strings, `${path}[8]`),
|
|
904
|
-
...(component[9] === 0 ? {} : { lineHeight: component[9] }),
|
|
905
|
-
...(component[10] === null ? {} : { tracking: component[10] }),
|
|
906
|
-
...(component[11] === -1 ? {} : { textAlign: indexedLiteral(['left', 'center', 'right'] as const, component[11], `${path}[11]`) }),
|
|
907
|
-
...(component[12] === -1 ? {} : { verticalAlign: indexedLiteral(['top', 'middle', 'bottom'] as const, component[12], `${path}[12]`) }),
|
|
908
|
-
color: component[13],
|
|
909
|
-
...(component[14] === 0 ? {} : { backgroundColor: component[14] }),
|
|
910
|
-
...(component[15] === -1 ? {} : { padding: component[15] }),
|
|
911
|
-
...(component[16] === 0 ? {} : { resolutionScale: component[16] }),
|
|
912
|
-
...(component[17] === 0 ? {} : {
|
|
913
|
-
documents: compactArray(component[17], `${path}[17]`).map((document, index) => (
|
|
914
|
-
decodeTextDocument(document, strings, `${path}[17][${index}]`)
|
|
915
|
-
)),
|
|
916
|
-
}),
|
|
917
|
-
...(component[18] === 0 ? {} : {
|
|
918
|
-
animators: compactArray(component[18], `${path}[18]`).map((animator, index) => (
|
|
919
|
-
decodeTextAnimator(animator, floatPool, `${path}[18][${index}]`)
|
|
920
|
-
)),
|
|
921
|
-
}),
|
|
922
|
-
}, validationPath, options, countBudget);
|
|
923
|
-
}
|
|
924
|
-
if (code === 7) {
|
|
925
|
-
if (component.length < 7) invalidBinary(`Compact sprite component is truncated at ${path}.`);
|
|
926
|
-
return parseBinaryComponentValue({
|
|
927
|
-
type: 'sprite2d',
|
|
928
|
-
resource: indexedString(strings, component[1], `${path}[1]`),
|
|
929
|
-
size: component[2],
|
|
930
|
-
...(component[3] === 0 ? {} : { position: component[3] }),
|
|
931
|
-
...(component[4] === 0 ? {} : { tint: component[4] }),
|
|
932
|
-
...(component[5] === 0 ? {} : { uvRect: component[5] }),
|
|
933
|
-
...decodeVectorTrackField(component, 6, 'uvRectTrack', floatPool, path),
|
|
934
|
-
}, validationPath, options, countBudget);
|
|
935
|
-
}
|
|
936
|
-
invalidBinary(`Unknown compact component code ${code} at ${path}[0].`);
|
|
937
|
-
}
|
|
938
|
-
|
|
939
|
-
function decodeOptionalStringField(
|
|
940
|
-
value: unknown,
|
|
941
|
-
key: string,
|
|
942
|
-
strings: readonly string[],
|
|
943
|
-
path: string,
|
|
944
|
-
): Record<string, unknown> {
|
|
945
|
-
const decoded = optionalIndexedString(strings, value, path);
|
|
946
|
-
return decoded === undefined ? {} : { [key]: decoded };
|
|
947
|
-
}
|
|
948
|
-
|
|
949
|
-
function decodeFontWeightField(value: unknown, strings: readonly string[], path: string): Record<string, unknown> {
|
|
950
|
-
if (value === null) return {};
|
|
951
|
-
if (typeof value === 'number') return { fontWeight: value };
|
|
952
|
-
const encoded = compactArray(value, path, 2);
|
|
953
|
-
if (encoded[0] !== -1) invalidBinary(`Invalid compact font weight tag at ${path}[0].`);
|
|
954
|
-
return { fontWeight: indexedString(strings, encoded[1], `${path}[1]`) };
|
|
955
|
-
}
|
|
956
|
-
|
|
957
|
-
function decodeTextDocument(value: unknown, strings: readonly string[], path: string): Record<string, unknown> {
|
|
958
|
-
const document = compactArray(value, path, 11);
|
|
959
|
-
return {
|
|
960
|
-
time: document[0], text: indexedString(strings, document[1], `${path}[1]`),
|
|
961
|
-
...decodeOptionalStringField(document[2], 'fontFamily', strings, `${path}[2]`),
|
|
962
|
-
...(document[3] === 0 ? {} : { fontSize: document[3] }),
|
|
963
|
-
...decodeFontWeightField(document[4], strings, `${path}[4]`),
|
|
964
|
-
...(document[5] === -1 ? {} : { fontStyle: indexedLiteral(['normal', 'italic'] as const, document[5], `${path}[5]`) }),
|
|
965
|
-
...decodeOptionalStringField(document[6], 'fontResource', strings, `${path}[6]`),
|
|
966
|
-
...(document[7] === 0 ? {} : { lineHeight: document[7] }),
|
|
967
|
-
...(document[8] === null ? {} : { tracking: document[8] }),
|
|
968
|
-
...(document[9] === -1 ? {} : { textAlign: indexedLiteral(['left', 'center', 'right'] as const, document[9], `${path}[9]`) }),
|
|
969
|
-
...(document[10] === 0 ? {} : { color: document[10] }),
|
|
970
|
-
};
|
|
971
|
-
}
|
|
972
|
-
|
|
973
|
-
function decodeTextAnimator(value: unknown, floatPool: Float32Array, path: string): Record<string, unknown> {
|
|
974
|
-
const animator = compactArray(value, path, 13);
|
|
975
|
-
const selector = compactArray(animator[0], `${path}[0]`, 10);
|
|
976
|
-
return {
|
|
977
|
-
selector: {
|
|
978
|
-
start: selector[0], end: selector[1], offset: selector[2],
|
|
979
|
-
units: indexedLiteral(['percent', 'index'] as const, selector[3], `${path}[0][3]`),
|
|
980
|
-
amount: selector[4],
|
|
981
|
-
shape: indexedLiteral(TEXT_SELECTOR_SHAPES, selector[5], `${path}[0][5]`),
|
|
982
|
-
...decodeVectorTrackField(selector, 6, 'startTrack', floatPool, `${path}[0]`),
|
|
983
|
-
...decodeVectorTrackField(selector, 7, 'endTrack', floatPool, `${path}[0]`),
|
|
984
|
-
...decodeVectorTrackField(selector, 8, 'offsetTrack', floatPool, `${path}[0]`),
|
|
985
|
-
...decodeVectorTrackField(selector, 9, 'amountTrack', floatPool, `${path}[0]`),
|
|
986
|
-
basedOn: selector.length < 11 || selector[10] === -1
|
|
987
|
-
? undefined
|
|
988
|
-
: indexedLiteral(TEXT_SELECTOR_BASES, selector[10], `${path}[0][10]`),
|
|
989
|
-
easing: selector[11] === 0 ? undefined : selector[11],
|
|
990
|
-
smoothness: selector[12] === -1 ? undefined : selector[12],
|
|
991
|
-
randomSeed: selector[13] === null ? undefined : selector[13],
|
|
992
|
-
},
|
|
993
|
-
...(animator[1] === 0 ? {} : { position: animator[1] }),
|
|
994
|
-
...(animator[2] === 0 ? {} : { scale: animator[2] }),
|
|
995
|
-
...(animator[3] === null ? {} : { rotation: animator[3] }),
|
|
996
|
-
...(animator[4] === -1 ? {} : { opacity: animator[4] }),
|
|
997
|
-
...(animator[5] === 0 ? {} : { fillColor: animator[5] }),
|
|
998
|
-
...(animator[6] === null ? {} : { tracking: animator[6] }),
|
|
999
|
-
...decodeVectorTrackField(animator, 7, 'positionTrack', floatPool, path),
|
|
1000
|
-
...decodeVectorTrackField(animator, 8, 'scaleTrack', floatPool, path),
|
|
1001
|
-
...decodeVectorTrackField(animator, 9, 'rotationTrack', floatPool, path),
|
|
1002
|
-
...decodeVectorTrackField(animator, 10, 'opacityTrack', floatPool, path),
|
|
1003
|
-
...decodeVectorTrackField(animator, 11, 'fillColorTrack', floatPool, path),
|
|
1004
|
-
...decodeVectorTrackField(animator, 12, 'trackingTrack', floatPool, path),
|
|
1005
|
-
};
|
|
1006
|
-
}
|
|
1007
|
-
|
|
1008
|
-
function decodeLayerEffects(value: unknown, floatPool: Float32Array, path: string): Record<string, unknown>[] {
|
|
1009
|
-
return compactArray(value, path).map((value, index) => {
|
|
1010
|
-
const effectPath = `${path}[${index}]`;
|
|
1011
|
-
const effect = compactArray(value, effectPath, 2);
|
|
1012
|
-
const kind = indexedLiteral(['tint', 'fill', 'opacity', 'color-matrix', 'blur', 'drop-shadow'] as const, effect[0], `${effectPath}[0]`);
|
|
1013
|
-
if (kind === 'tint') {
|
|
1014
|
-
if (effect.length < 7) invalidBinary(`Compact tint effect is truncated at ${effectPath}.`);
|
|
1015
|
-
return {
|
|
1016
|
-
kind, black: effect[1], white: effect[2], amount: effect[3],
|
|
1017
|
-
...decodeVectorTrackField(effect, 4, 'blackTrack', floatPool, effectPath),
|
|
1018
|
-
...decodeVectorTrackField(effect, 5, 'whiteTrack', floatPool, effectPath),
|
|
1019
|
-
...decodeVectorTrackField(effect, 6, 'amountTrack', floatPool, effectPath),
|
|
1020
|
-
};
|
|
1021
|
-
}
|
|
1022
|
-
if (kind === 'fill') {
|
|
1023
|
-
if (effect.length < 5) invalidBinary(`Compact fill effect is truncated at ${effectPath}.`);
|
|
1024
|
-
return {
|
|
1025
|
-
kind, color: effect[1], opacity: effect[2],
|
|
1026
|
-
...decodeVectorTrackField(effect, 3, 'colorTrack', floatPool, effectPath),
|
|
1027
|
-
...decodeVectorTrackField(effect, 4, 'opacityTrack', floatPool, effectPath),
|
|
1028
|
-
};
|
|
1029
|
-
}
|
|
1030
|
-
if (kind === 'opacity') return {
|
|
1031
|
-
kind, opacity: effect[1], ...decodeVectorTrackField(effect, 2, 'opacityTrack', floatPool, effectPath),
|
|
1032
|
-
};
|
|
1033
|
-
if (kind === 'color-matrix') return {
|
|
1034
|
-
kind,
|
|
1035
|
-
matrix: compactFloatView(floatPool, effect[1], `${effectPath}[1]`),
|
|
1036
|
-
...decodeVectorTrackField(effect, 2, 'matrixTrack', floatPool, effectPath),
|
|
1037
|
-
};
|
|
1038
|
-
if (kind === 'blur') return {
|
|
1039
|
-
kind, radius: effect[1], ...decodeVectorTrackField(effect, 2, 'radiusTrack', floatPool, effectPath),
|
|
1040
|
-
};
|
|
1041
|
-
if (effect.length < 9) invalidBinary(`Compact drop-shadow effect is truncated at ${effectPath}.`);
|
|
1042
|
-
return {
|
|
1043
|
-
kind, color: effect[1], opacity: effect[2], offset: effect[3], blur: effect[4],
|
|
1044
|
-
...decodeVectorTrackField(effect, 5, 'colorTrack', floatPool, effectPath),
|
|
1045
|
-
...decodeVectorTrackField(effect, 6, 'opacityTrack', floatPool, effectPath),
|
|
1046
|
-
...decodeVectorTrackField(effect, 7, 'offsetTrack', floatPool, effectPath),
|
|
1047
|
-
...decodeVectorTrackField(effect, 8, 'blurTrack', floatPool, effectPath),
|
|
1048
|
-
};
|
|
1049
|
-
});
|
|
1050
|
-
}
|
|
1051
|
-
|
|
1052
|
-
function decodeVectorModifier(value: unknown, floatPool: Float32Array, path: string): Record<string, unknown> {
|
|
1053
|
-
const modifier = compactArray(value, path, 3);
|
|
1054
|
-
const kind = indexedLiteral(['trim-path', 'round-corners'] as const, modifier[0], `${path}[0]`);
|
|
1055
|
-
if (kind === 'round-corners') return {
|
|
1056
|
-
kind,
|
|
1057
|
-
radius: modifier[1],
|
|
1058
|
-
...decodeVectorTrackField(modifier, 2, 'radiusTrack', floatPool, path),
|
|
1059
|
-
};
|
|
1060
|
-
if (modifier.length < 8) invalidBinary(`Truncated trim-path at ${path}.`);
|
|
1061
|
-
return {
|
|
1062
|
-
kind,
|
|
1063
|
-
start: modifier[1], end: modifier[2], offset: modifier[3],
|
|
1064
|
-
mode: indexedLiteral(['simultaneous', 'individual'] as const, modifier[4], `${path}[4]`),
|
|
1065
|
-
...decodeVectorTrackField(modifier, 5, 'startTrack', floatPool, path),
|
|
1066
|
-
...decodeVectorTrackField(modifier, 6, 'endTrack', floatPool, path),
|
|
1067
|
-
...decodeVectorTrackField(modifier, 7, 'offsetTrack', floatPool, path),
|
|
1068
|
-
};
|
|
1069
|
-
}
|
|
1070
|
-
|
|
1071
|
-
function decodeVectorTrackField(
|
|
1072
|
-
owner: unknown[],
|
|
1073
|
-
index: number,
|
|
1074
|
-
key: string,
|
|
1075
|
-
floatPool: Float32Array,
|
|
1076
|
-
path: string,
|
|
1077
|
-
): Record<string, unknown> {
|
|
1078
|
-
return owner[index] === 0 ? {} : { [key]: decodeVectorTrack(owner[index], floatPool, `${path}[${index}]`) };
|
|
1079
|
-
}
|
|
1080
|
-
|
|
1081
|
-
function decodeVectorTrack(value: unknown, floatPool: Float32Array, path: string): Record<string, unknown> {
|
|
1082
|
-
const track = compactArray(value, path, 5);
|
|
1083
|
-
return {
|
|
1084
|
-
times: compactFloatView(floatPool, track[0], `${path}[0]`),
|
|
1085
|
-
values: compactFloatView(floatPool, track[1], `${path}[1]`),
|
|
1086
|
-
valueSize: track[2],
|
|
1087
|
-
interpolation: indexedLiteral(INTERPOLATIONS, track[3], `${path}[3]`),
|
|
1088
|
-
...(track[4] === 0 ? {} : { easings: compactFloatView(floatPool, track[4], `${path}[4]`) }),
|
|
1089
|
-
};
|
|
1090
|
-
}
|
|
1091
|
-
|
|
1092
|
-
function decodeVectorFill(value: unknown, floatPool: Float32Array, path: string): Record<string, unknown> {
|
|
1093
|
-
const fill = compactArray(value, path, 4);
|
|
1094
|
-
const kind = indexedLiteral(['solid', 'linear-gradient', 'radial-gradient'] as const, fill[0], `${path}[0]`);
|
|
1095
|
-
if (kind === 'solid') return {
|
|
1096
|
-
kind,
|
|
1097
|
-
color: fill[1],
|
|
1098
|
-
...decodeVectorTrackField(fill, 2, 'colorTrack', floatPool, path),
|
|
1099
|
-
...decodeVectorTrackField(fill, 3, 'opacityTrack', floatPool, path),
|
|
1100
|
-
...(fill.length < 5 || fill[4] === 1 ? {} : { opacity: fill[4] }),
|
|
1101
|
-
};
|
|
1102
|
-
if (fill.length < 8) invalidBinary(`Compact vector gradient is truncated at ${path}.`);
|
|
1103
|
-
return {
|
|
1104
|
-
kind,
|
|
1105
|
-
start: fill[1],
|
|
1106
|
-
end: fill[2],
|
|
1107
|
-
stops: compactFloatView(floatPool, fill[3], `${path}[3]`),
|
|
1108
|
-
...decodeVectorTrackField(fill, 4, 'startTrack', floatPool, path),
|
|
1109
|
-
...decodeVectorTrackField(fill, 5, 'endTrack', floatPool, path),
|
|
1110
|
-
...decodeVectorTrackField(fill, 6, 'stopsTrack', floatPool, path),
|
|
1111
|
-
...decodeVectorTrackField(fill, 7, 'opacityTrack', floatPool, path),
|
|
1112
|
-
...(fill.length < 9 || fill[8] === 1 ? {} : { opacity: fill[8] }),
|
|
1113
|
-
};
|
|
1114
|
-
}
|
|
1115
|
-
|
|
1116
|
-
function decodeVectorStroke(value: unknown, floatPool: Float32Array, path: string): Record<string, unknown> {
|
|
1117
|
-
const stroke = compactArray(value, path, 11);
|
|
1118
|
-
return {
|
|
1119
|
-
color: stroke[0],
|
|
1120
|
-
width: stroke[1],
|
|
1121
|
-
lineCap: indexedLiteral(['butt', 'round', 'square'] as const, stroke[2], `${path}[2]`),
|
|
1122
|
-
lineJoin: indexedLiteral(['miter', 'round', 'bevel'] as const, stroke[3], `${path}[3]`),
|
|
1123
|
-
miterLimit: stroke[4],
|
|
1124
|
-
...(stroke[5] === 0 ? {} : { dash: compactFloatView(floatPool, stroke[5], `${path}[5]`) }),
|
|
1125
|
-
...(stroke[6] === 0 ? {} : { dashOffset: stroke[6] }),
|
|
1126
|
-
...decodeVectorTrackField(stroke, 7, 'colorTrack', floatPool, path),
|
|
1127
|
-
...decodeVectorTrackField(stroke, 8, 'opacityTrack', floatPool, path),
|
|
1128
|
-
...decodeVectorTrackField(stroke, 9, 'widthTrack', floatPool, path),
|
|
1129
|
-
...decodeVectorTrackField(stroke, 10, 'dashOffsetTrack', floatPool, path),
|
|
1130
|
-
...(stroke.length < 12 || stroke[11] === 0 ? {} : { gradient: decodeVectorFill(stroke[11], floatPool, `${path}[11]`) }),
|
|
1131
|
-
...(stroke.length < 13 || stroke[12] === 1 ? {} : { opacity: stroke[12] }),
|
|
1132
|
-
};
|
|
1133
|
-
}
|
|
1134
|
-
|
|
1135
|
-
function decodeComposite(
|
|
1136
|
-
value: unknown,
|
|
1137
|
-
nodeIds: readonly string[],
|
|
1138
|
-
floatPool: Float32Array,
|
|
1139
|
-
path: string,
|
|
1140
|
-
): NonNullable<AnimationNode['composite']> {
|
|
1141
|
-
const composite = compactArray(value, path);
|
|
1142
|
-
if (typeof composite[0] === 'number') return decodeCompositeLayer(composite, nodeIds, floatPool, path);
|
|
1143
|
-
if (composite.length < 1 || composite.length > 8) invalidBinary(`Composite stack must contain between 1 and 8 layers at ${path}.`);
|
|
1144
|
-
return Object.freeze({
|
|
1145
|
-
layers: Object.freeze(composite.map((layer, index) => decodeCompositeLayer(
|
|
1146
|
-
compactArray(layer, `${path}[${index}]`, 3), nodeIds, floatPool, `${path}[${index}]`,
|
|
1147
|
-
))),
|
|
1148
|
-
});
|
|
1149
|
-
}
|
|
1150
|
-
|
|
1151
|
-
function decodeCompositeLayer(
|
|
1152
|
-
composite: unknown[],
|
|
1153
|
-
nodeIds: readonly string[],
|
|
1154
|
-
floatPool: Float32Array,
|
|
1155
|
-
path: string,
|
|
1156
|
-
): Readonly<AnimationCompositeLayer> {
|
|
1157
|
-
const source = boundedIndex(composite[1], nodeIds.length, `${path}[1]`);
|
|
1158
|
-
return Object.freeze({
|
|
1159
|
-
kind: indexedLiteral(COMPOSITE_KINDS, composite[0], `${path}[0]`),
|
|
1160
|
-
source: nodeIds[source]!,
|
|
1161
|
-
mode: indexedLiteral(COMPOSITE_MODES, composite[2], `${path}[2]`),
|
|
1162
|
-
...(composite.length < 4 ? {} : { operation: indexedLiteral(COMPOSITE_OPERATIONS, composite[3], `${path}[3]`) }),
|
|
1163
|
-
...(composite.length < 5 || composite[4] === 0 ? {} : {
|
|
1164
|
-
feather: nonNegativeFiniteTuple(composite[4], 2, `${path}[4]`) as unknown as readonly [number, number],
|
|
1165
|
-
}),
|
|
1166
|
-
...(composite.length < 6 || composite[5] === 0 ? {} : { expansion: finiteNumber(composite[5], `${path}[5]`) }),
|
|
1167
|
-
...(composite.length < 7 ? {} : decodeVectorTrackField(composite, 6, 'expansionTrack', floatPool, path)),
|
|
1168
|
-
});
|
|
1169
|
-
}
|
|
1170
|
-
|
|
1171
|
-
function decodeLegacyMetadata(
|
|
1172
|
-
metadata: unknown,
|
|
1173
|
-
floatPool: Float32Array,
|
|
1174
|
-
buffer: ArrayBuffer,
|
|
1175
|
-
options: AnimationParseOptions,
|
|
1176
|
-
): ParsedAnimation {
|
|
1177
|
-
const root = binaryRecord(metadata, '$binary.metadata');
|
|
1178
|
-
const tracks = binaryArray(root.tracks, '$binary.metadata.tracks');
|
|
1179
|
-
const nodes = binaryArray(root.nodes, '$binary.metadata.nodes').map((value, nodeIndex) => {
|
|
1180
|
-
const node = binaryRecord(value, `$.nodes[${nodeIndex}]`);
|
|
1181
|
-
if (node.components === undefined) return node;
|
|
1182
|
-
const components = binaryArray(node.components, `$.nodes[${nodeIndex}].components`).map((componentValue, componentIndex) => {
|
|
1183
|
-
const component = binaryRecord(componentValue, `$.nodes[${nodeIndex}].components[${componentIndex}]`);
|
|
1184
|
-
if (component.type !== 'path2d') return component;
|
|
1185
|
-
return {
|
|
1186
|
-
...component,
|
|
1187
|
-
values: legacyFloatView(floatPool, component.values, `$.nodes[${nodeIndex}].components[${componentIndex}].values`),
|
|
1188
|
-
};
|
|
1189
|
-
});
|
|
1190
|
-
return { ...node, components };
|
|
1191
|
-
});
|
|
1192
|
-
const hydratedTracks = tracks.map((value, index) => {
|
|
1193
|
-
const track = binaryRecord(value, `$.tracks[${index}]`) as unknown as LegacyBinaryTrackMetadata;
|
|
1194
|
-
return {
|
|
1195
|
-
node: track.node,
|
|
1196
|
-
property: track.property,
|
|
1197
|
-
interpolation: track.interpolation,
|
|
1198
|
-
times: legacyFloatView(floatPool, track.times, `$.tracks[${index}].times`),
|
|
1199
|
-
values: legacyFloatView(floatPool, track.values, `$.tracks[${index}].values`),
|
|
1200
|
-
...(track.easings === undefined ? {} : { easings: legacyFloatView(floatPool, track.easings, `$.tracks[${index}].easings`) }),
|
|
1201
|
-
};
|
|
1202
|
-
});
|
|
1203
|
-
return parseAnimationValue({ ...root, nodes, tracks: hydratedTracks }, options, 'binary', buffer);
|
|
1204
|
-
}
|
|
1205
|
-
|
|
1206
|
-
function compactFloatView(pool: Float32Array, value: unknown, path: string): Float32Array {
|
|
1207
|
-
const descriptor = compactArray(value, path, 2);
|
|
1208
|
-
const offset = safeUint(descriptor[0], `${path}[0]`);
|
|
1209
|
-
const length = safeUint(descriptor[1], `${path}[1]`);
|
|
1210
|
-
if (offset + length > pool.length) invalidBinary(`Float range is outside the pool at ${path}.`);
|
|
1211
|
-
return pool.subarray(offset, offset + length);
|
|
1212
|
-
}
|
|
1213
|
-
|
|
1214
|
-
function legacyFloatView(pool: Float32Array, value: unknown, path: string): Float32Array {
|
|
1215
|
-
const descriptor = binaryRecord(value, path);
|
|
1216
|
-
const offset = safeUint(descriptor.offset, `${path}.offset`);
|
|
1217
|
-
const length = safeUint(descriptor.length, `${path}.length`);
|
|
1218
|
-
if (offset + length > pool.length) invalidBinary(`Float range is outside the pool at ${path}.`);
|
|
1219
|
-
return pool.subarray(offset, offset + length);
|
|
1220
|
-
}
|
|
1221
|
-
|
|
1222
|
-
function floatBlockKey(values: Float32Array): string {
|
|
1223
|
-
let result = `${values.length}:`;
|
|
1224
|
-
for (let index = 0; index < values.length; index++) result += `${values[index]},`;
|
|
1225
|
-
return result;
|
|
1226
|
-
}
|
|
1227
|
-
|
|
1228
|
-
function decodeStringIndices(value: unknown, strings: readonly string[], path: string): string[] {
|
|
1229
|
-
const seen = new Set<string>();
|
|
1230
|
-
return compactArray(value, path).map((item, index) => {
|
|
1231
|
-
const result = indexedString(strings, item, `${path}[${index}]`);
|
|
1232
|
-
if (!result.trim()) invalidBinary(`Expected a non-empty string at ${path}[${index}].`);
|
|
1233
|
-
if (seen.has(result)) invalidBinary(`Duplicate string-table value at ${path}[${index}].`);
|
|
1234
|
-
seen.add(result);
|
|
1235
|
-
return result;
|
|
1236
|
-
});
|
|
1237
|
-
}
|
|
1238
|
-
|
|
1239
|
-
function optionalIndexedString(strings: readonly string[], value: unknown, path: string): string | undefined {
|
|
1240
|
-
const index = finiteInteger(value, path);
|
|
1241
|
-
return index === -1 ? undefined : indexedString(strings, index, path);
|
|
1242
|
-
}
|
|
1243
|
-
|
|
1244
|
-
function indexedString(strings: readonly string[], value: unknown, path: string): string {
|
|
1245
|
-
const index = boundedIndex(value, strings.length, path);
|
|
1246
|
-
return strings[index]!;
|
|
1247
|
-
}
|
|
1248
|
-
|
|
1249
|
-
function indexedLiteral<const T extends readonly string[]>(values: T, value: unknown, path: string): T[number] {
|
|
1250
|
-
const index = boundedIndex(value, values.length, path);
|
|
1251
|
-
return values[index]!;
|
|
1252
|
-
}
|
|
1253
|
-
|
|
1254
|
-
function optionalBoundedIndex(value: unknown, length: number, path: string): number | undefined {
|
|
1255
|
-
const index = finiteInteger(value, path);
|
|
1256
|
-
return index === -1 ? undefined : boundedIndex(index, length, path);
|
|
1257
|
-
}
|
|
1258
|
-
|
|
1259
|
-
function boundedIndex(value: unknown, length: number, path: string): number {
|
|
1260
|
-
const index = finiteInteger(value, path);
|
|
1261
|
-
if (index < 0 || index >= length) invalidBinary(`Index is outside its table at ${path}.`);
|
|
1262
|
-
return index;
|
|
1263
|
-
}
|
|
1264
|
-
|
|
1265
|
-
function requiredMapIndex(map: ReadonlyMap<string, number>, key: string, label: string): number {
|
|
1266
|
-
const value = map.get(key);
|
|
1267
|
-
if (value === undefined) invalidBinary(`Missing ${label} "${key}" while encoding.`);
|
|
1268
|
-
return value;
|
|
1269
|
-
}
|
|
1270
|
-
|
|
1271
|
-
function align4(value: number): number {
|
|
1272
|
-
return (value + 3) & ~3;
|
|
1273
|
-
}
|
|
1274
|
-
|
|
1275
|
-
function isParsedAnimation(value: AnimationDocument | ParsedAnimation): value is ParsedAnimation {
|
|
1276
|
-
return 'source' in value && Array.isArray(value.tracks) && value.tracks.every(track => track.times instanceof Float32Array);
|
|
1277
|
-
}
|
|
1278
|
-
|
|
1279
|
-
function isPath2DComponent(component: AnimationComponent): component is AnimationPath2DComponent {
|
|
1280
|
-
return component.type === 'path2d' && typeof (component as Partial<AnimationPath2DComponent>).commands === 'string';
|
|
1281
|
-
}
|
|
1282
|
-
|
|
1283
|
-
interface CompactVectorStrokeComponent {
|
|
1284
|
-
readonly [key: string]: unknown;
|
|
1285
|
-
readonly type: typeof VECTOR_STROKE_EXTENSION;
|
|
1286
|
-
readonly commands?: string;
|
|
1287
|
-
readonly values?: readonly number[] | Float32Array;
|
|
1288
|
-
readonly sourceComponent?: number;
|
|
1289
|
-
readonly color: readonly [number, number, number, number];
|
|
1290
|
-
readonly width: number;
|
|
1291
|
-
readonly lineCap: 'butt' | 'round' | 'square';
|
|
1292
|
-
readonly lineJoin: 'miter' | 'round' | 'bevel';
|
|
1293
|
-
readonly miterLimit: number;
|
|
1294
|
-
readonly tolerance?: number;
|
|
1295
|
-
}
|
|
1296
|
-
|
|
1297
|
-
interface CompactVectorPathMorphComponent {
|
|
1298
|
-
readonly [key: string]: unknown;
|
|
1299
|
-
readonly type: typeof VECTOR_PATH_MORPH_EXTENSION;
|
|
1300
|
-
readonly commands: string;
|
|
1301
|
-
readonly times: readonly number[] | Float32Array;
|
|
1302
|
-
readonly values: readonly number[] | Float32Array;
|
|
1303
|
-
readonly valueSize: number;
|
|
1304
|
-
readonly interpolation: typeof INTERPOLATIONS[number];
|
|
1305
|
-
readonly easings?: readonly number[] | Float32Array;
|
|
1306
|
-
readonly fill: readonly [number, number, number, number];
|
|
1307
|
-
readonly fillRule: 'nonzero' | 'evenodd';
|
|
1308
|
-
readonly tolerance?: number;
|
|
1309
|
-
}
|
|
1310
|
-
|
|
1311
|
-
function isCompactVectorStrokeComponent(component: AnimationComponent): component is CompactVectorStrokeComponent {
|
|
1312
|
-
if (component.type !== VECTOR_STROKE_EXTENSION || !Object.keys(component).every(key => VECTOR_STROKE_KEYS.has(key))) return false;
|
|
1313
|
-
const candidate = component as Partial<CompactVectorStrokeComponent>;
|
|
1314
|
-
const inlinePath = typeof candidate.commands === 'string'
|
|
1315
|
-
&& (Array.isArray(candidate.values) || candidate.values instanceof Float32Array)
|
|
1316
|
-
&& Array.from(candidate.values).every(Number.isFinite);
|
|
1317
|
-
const sourcePath = Number.isSafeInteger(candidate.sourceComponent) && candidate.sourceComponent! >= 0;
|
|
1318
|
-
return (inlinePath || sourcePath)
|
|
1319
|
-
&& Array.isArray(candidate.color)
|
|
1320
|
-
&& candidate.color.length === 4
|
|
1321
|
-
&& candidate.color.every(value => Number.isFinite(value))
|
|
1322
|
-
&& typeof candidate.width === 'number' && Number.isFinite(candidate.width)
|
|
1323
|
-
&& (candidate.lineCap === 'butt' || candidate.lineCap === 'round' || candidate.lineCap === 'square')
|
|
1324
|
-
&& (candidate.lineJoin === 'miter' || candidate.lineJoin === 'round' || candidate.lineJoin === 'bevel')
|
|
1325
|
-
&& typeof candidate.miterLimit === 'number' && Number.isFinite(candidate.miterLimit)
|
|
1326
|
-
&& (candidate.tolerance === undefined || (typeof candidate.tolerance === 'number' && Number.isFinite(candidate.tolerance)));
|
|
1327
|
-
}
|
|
1328
|
-
|
|
1329
|
-
function isCompactVectorPathMorphComponent(component: AnimationComponent): component is CompactVectorPathMorphComponent {
|
|
1330
|
-
if (component.type !== VECTOR_PATH_MORPH_EXTENSION || !Object.keys(component).every(key => VECTOR_PATH_MORPH_KEYS.has(key))) return false;
|
|
1331
|
-
const candidate = component as Partial<CompactVectorPathMorphComponent>;
|
|
1332
|
-
const times = candidate.times;
|
|
1333
|
-
const values = candidate.values;
|
|
1334
|
-
const easings = candidate.easings;
|
|
1335
|
-
return typeof candidate.commands === 'string'
|
|
1336
|
-
&& (Array.isArray(times) || times instanceof Float32Array)
|
|
1337
|
-
&& Array.from(times).every(Number.isFinite)
|
|
1338
|
-
&& (Array.isArray(values) || values instanceof Float32Array)
|
|
1339
|
-
&& Array.from(values).every(Number.isFinite)
|
|
1340
|
-
&& Number.isSafeInteger(candidate.valueSize) && candidate.valueSize! > 0
|
|
1341
|
-
&& values.length === times.length * candidate.valueSize!
|
|
1342
|
-
&& INTERPOLATIONS.includes(candidate.interpolation as typeof INTERPOLATIONS[number])
|
|
1343
|
-
&& (easings === undefined || Array.isArray(easings) || easings instanceof Float32Array)
|
|
1344
|
-
&& Array.isArray(candidate.fill) && candidate.fill.length === 4
|
|
1345
|
-
&& (candidate.fillRule === 'nonzero' || candidate.fillRule === 'evenodd')
|
|
1346
|
-
&& (candidate.tolerance === undefined || (typeof candidate.tolerance === 'number' && Number.isFinite(candidate.tolerance)));
|
|
1347
|
-
}
|
|
1348
|
-
|
|
1349
|
-
function safeUint(value: unknown, path: string): number {
|
|
1350
|
-
const result = finiteInteger(value, path);
|
|
1351
|
-
if (result < 0 || result > 0xffffffff) invalidBinary(`Expected an unsigned integer at ${path}.`);
|
|
1352
|
-
return result;
|
|
1353
|
-
}
|
|
1354
|
-
|
|
1355
|
-
function finiteInteger(value: unknown, path: string): number {
|
|
1356
|
-
if (typeof value !== 'number' || !Number.isSafeInteger(value)) invalidBinary(`Expected an integer at ${path}.`);
|
|
1357
|
-
return value;
|
|
1358
|
-
}
|
|
1359
|
-
|
|
1360
|
-
function finiteNumber(value: unknown, path: string): number {
|
|
1361
|
-
if (typeof value !== 'number' || !Number.isFinite(value)) invalidBinary(`Expected a finite number at ${path}.`);
|
|
1362
|
-
return value;
|
|
1363
|
-
}
|
|
1364
|
-
|
|
1365
|
-
function positiveFiniteNumber(value: unknown, path: string): number {
|
|
1366
|
-
const result = finiteNumber(value, path);
|
|
1367
|
-
if (result <= 0) invalidBinary(`Expected a positive number at ${path}.`);
|
|
1368
|
-
return result;
|
|
1369
|
-
}
|
|
1370
|
-
|
|
1371
|
-
function nonNegativeFiniteNumber(value: unknown, path: string): number {
|
|
1372
|
-
const result = finiteNumber(value, path);
|
|
1373
|
-
if (result < 0) invalidBinary(`Expected a non-negative number at ${path}.`);
|
|
1374
|
-
return result;
|
|
1375
|
-
}
|
|
1376
|
-
|
|
1377
|
-
function finiteTuple(value: unknown, length: number, path: string): number[] {
|
|
1378
|
-
const tuple = compactArray(value, path, length);
|
|
1379
|
-
if (tuple.length !== length) invalidBinary(`Expected exactly ${length} entries at ${path}.`);
|
|
1380
|
-
return tuple.map((item, index) => finiteNumber(item, `${path}[${index}]`));
|
|
1381
|
-
}
|
|
1382
|
-
|
|
1383
|
-
function positiveFiniteTuple(value: unknown, length: number, path: string): readonly number[] {
|
|
1384
|
-
const tuple = finiteTuple(value, length, path);
|
|
1385
|
-
for (let index = 0; index < tuple.length; index++) {
|
|
1386
|
-
if (tuple[index]! <= 0) invalidBinary(`Expected positive tuple values at ${path}[${index}].`);
|
|
1387
|
-
}
|
|
1388
|
-
return Object.freeze(tuple);
|
|
1389
|
-
}
|
|
1390
|
-
|
|
1391
|
-
function nonNegativeFiniteTuple(value: unknown, length: number, path: string): readonly number[] {
|
|
1392
|
-
const tuple = finiteTuple(value, length, path);
|
|
1393
|
-
for (let index = 0; index < tuple.length; index++) {
|
|
1394
|
-
if (tuple[index]! < 0) invalidBinary(`Expected non-negative tuple values at ${path}[${index}].`);
|
|
1395
|
-
}
|
|
1396
|
-
return Object.freeze(tuple);
|
|
1397
|
-
}
|
|
1398
|
-
|
|
1399
|
-
function unitFiniteTuple(value: unknown, length: number, path: string): readonly number[] {
|
|
1400
|
-
const tuple = finiteTuple(value, length, path);
|
|
1401
|
-
for (let index = 0; index < tuple.length; index++) {
|
|
1402
|
-
if (tuple[index]! < 0 || tuple[index]! > 1) invalidBinary(`Expected tuple values in [0, 1] at ${path}[${index}].`);
|
|
1403
|
-
}
|
|
1404
|
-
return Object.freeze(tuple);
|
|
1405
|
-
}
|
|
1406
|
-
|
|
1407
|
-
function requiredString(value: unknown, path: string): string {
|
|
1408
|
-
if (typeof value !== 'string') invalidBinary(`Expected a string at ${path}.`);
|
|
1409
|
-
return value;
|
|
1410
|
-
}
|
|
1411
|
-
|
|
1412
|
-
function requiredNonEmptyString(value: string, path: string): string {
|
|
1413
|
-
if (!value.trim()) invalidBinary(`Expected a non-empty string at ${path}.`);
|
|
1414
|
-
return value;
|
|
1415
|
-
}
|
|
1416
|
-
|
|
1417
|
-
function compactArray(value: unknown, path: string, minimumLength = 0): unknown[] {
|
|
1418
|
-
const result = binaryArray(value, path);
|
|
1419
|
-
if (result.length < minimumLength) invalidBinary(`Expected at least ${minimumLength} entries at ${path}.`);
|
|
1420
|
-
return result;
|
|
1421
|
-
}
|
|
1422
|
-
|
|
1423
|
-
function binaryRecord(value: unknown, path: string): Record<string, unknown> {
|
|
1424
|
-
if (!value || typeof value !== 'object' || Array.isArray(value)) invalidBinary(`Expected an object at ${path}.`);
|
|
1425
|
-
return value as Record<string, unknown>;
|
|
1426
|
-
}
|
|
1427
|
-
|
|
1428
|
-
function binaryArray(value: unknown, path: string): unknown[] {
|
|
1429
|
-
if (!Array.isArray(value)) invalidBinary(`Expected an array at ${path}.`);
|
|
1430
|
-
return value;
|
|
1431
|
-
}
|
|
1432
|
-
|
|
1433
|
-
function binaryLimit(actual: number, maximum: number, label: string, path: string): void {
|
|
1434
|
-
if (actual > maximum) {
|
|
1435
|
-
throw new AnimationFormatError('E_ANIMATION_LIMIT_EXCEEDED', `${label} count ${actual} exceeds limit ${maximum}.`, path);
|
|
1436
|
-
}
|
|
1437
|
-
}
|
|
1438
|
-
|
|
1439
|
-
function invalidBinary(message: string): never {
|
|
1440
|
-
throw new AnimationFormatError('E_ANIMATION_INVALID_BINARY', message, '$binary');
|
|
1441
|
-
}
|