@openfairygui/functions 0.2.0-alpha.8 → 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 +52 -6
- package/dist/atlas-C6tbl7nn.d.ts +193 -0
- package/dist/atlas-CHsu2Y8i.d.cts +193 -0
- package/dist/index.cjs +17 -3603
- package/dist/index.d.cts +5 -294
- package/dist/index.d.ts +5 -294
- package/dist/index.js +4 -3595
- package/dist/node.cjs +256 -0
- package/dist/node.d.cts +36 -0
- package/dist/node.d.ts +36 -0
- package/dist/node.js +254 -0
- package/dist/publish-CykUJfVa.cjs +3269 -0
- package/dist/publish-DXoaC1Nl.js +3174 -0
- package/dist/restore-BQp01WY3.js +914 -0
- package/dist/restore-BeWaJNjR.d.cts +288 -0
- package/dist/restore-CEywQUHz.cjs +919 -0
- package/dist/restore-Dh0-Nvms.d.ts +288 -0
- package/dist/uam-transaction.cjs +29 -14
- package/dist/uam-transaction.d.cts +2 -1
- package/dist/uam-transaction.d.ts +2 -1
- package/dist/uam-transaction.js +30 -16
- package/dist/web.cjs +440 -0
- package/dist/web.d.cts +44 -0
- package/dist/web.d.ts +44 -0
- package/dist/web.js +439 -0
- package/package.json +29 -4
- package/src/adapters/node/plugins.ts +82 -0
- package/src/adapters/node/publish.ts +130 -0
- package/src/adapters/node/restore.ts +187 -0
- package/src/adapters/web/publish.ts +196 -0
- package/src/adapters/web/raster.ts +421 -0
- package/src/atlas/font.ts +95 -0
- package/src/atlas/inputs.ts +445 -0
- package/src/atlas/jta.ts +157 -0
- package/src/atlas/packing.ts +762 -0
- package/src/atlas.ts +129 -1221
- package/src/codegen.ts +108 -82
- package/src/index.ts +43 -3
- package/src/node.ts +8 -0
- package/src/path-utils.ts +40 -0
- package/src/plugins/types.ts +56 -0
- package/src/publish/contracts.ts +80 -0
- package/src/publish/external-resources.ts +117 -0
- package/src/publish/options.ts +180 -0
- package/src/publish/package-context.ts +608 -0
- package/src/publish/resource-references.ts +210 -0
- package/src/publish.ts +327 -975
- package/src/restore-internals/font.ts +100 -0
- package/src/restore-internals/movie-clip.ts +104 -0
- package/src/restore-internals/output-transaction.ts +124 -0
- package/src/restore.ts +122 -311
- package/src/shared-types.ts +4 -8
- package/src/uam-transaction.ts +34 -17
- package/src/utils.ts +28 -0
- package/src/web.ts +11 -0
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Document,
|
|
3
|
+
FontResource,
|
|
4
|
+
ILogger,
|
|
5
|
+
ImageResource,
|
|
6
|
+
MovieClipResource,
|
|
7
|
+
Package,
|
|
8
|
+
} from '@openfairygui/core';
|
|
9
|
+
import type { AtlasOptions } from '../atlas.js';
|
|
10
|
+
import type {
|
|
11
|
+
AtlasRasterBackend,
|
|
12
|
+
AtlasRasterInput,
|
|
13
|
+
AtlasRasterResolvedBuffer,
|
|
14
|
+
} from '../publish/contracts.js';
|
|
15
|
+
import {
|
|
16
|
+
isFontResource,
|
|
17
|
+
isImageResource,
|
|
18
|
+
isMovieClipResource,
|
|
19
|
+
resolveImageFileName,
|
|
20
|
+
resolveImagePath,
|
|
21
|
+
} from '../publish/package-context.js';
|
|
22
|
+
import type { ExtrasMap } from '../shared-types.js';
|
|
23
|
+
import { parseFnt } from './font.js';
|
|
24
|
+
import { prepareJtaForPublish, type PreparedJtaData } from './jta.js';
|
|
25
|
+
|
|
26
|
+
/** Trim info for a single image. */
|
|
27
|
+
interface TrimInfo {
|
|
28
|
+
/** Trimmed pixel data (PNG). */
|
|
29
|
+
buffer: Uint8Array;
|
|
30
|
+
/** Trimmed width. */
|
|
31
|
+
width: number;
|
|
32
|
+
/** Trimmed height. */
|
|
33
|
+
height: number;
|
|
34
|
+
/** Offset from original left edge. */
|
|
35
|
+
offsetX: number;
|
|
36
|
+
/** Offset from original top edge. */
|
|
37
|
+
offsetY: number;
|
|
38
|
+
/** Original width before trim. */
|
|
39
|
+
originalWidth: number;
|
|
40
|
+
/** Original height before trim. */
|
|
41
|
+
originalHeight: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export type PackageResource = ReturnType<Package['listResources']>[number];
|
|
45
|
+
export type PackableResource = ImageResource | MovieClipResource | FontResource;
|
|
46
|
+
export type PackInputResource = ImageResource | MovieClipResource;
|
|
47
|
+
|
|
48
|
+
export function getPublishedItemId(resource: { getId(): string; getExtras(): ExtrasMap | undefined }): string {
|
|
49
|
+
return ((resource.getExtras() as ImageResourceExtras | undefined) ?? {})._publishedId ?? resource.getId();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
interface ImageResourceExtras extends ExtrasMap {
|
|
53
|
+
_publishedId?: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface FontSpriteAlias {
|
|
57
|
+
fontId: string;
|
|
58
|
+
textureId: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface FontResourceExtras extends ExtrasMap {
|
|
62
|
+
_fontSpriteAlias?: FontSpriteAlias;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function resolveFontFileName(fontName: string): string {
|
|
66
|
+
return /\.fnt$/i.test(fontName) ? fontName : `${fontName}.fnt`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Trim transparent edges from an image using the host raster backend.
|
|
71
|
+
* Returns the trimmed buffer, dimensions, and offsets.
|
|
72
|
+
* Falls back to the original image if trim fails (e.g. no alpha channel, no transparent edges).
|
|
73
|
+
*/
|
|
74
|
+
async function _trimImage(
|
|
75
|
+
encoder: AtlasRasterBackend,
|
|
76
|
+
input: AtlasRasterInput,
|
|
77
|
+
originalWidth: number,
|
|
78
|
+
originalHeight: number,
|
|
79
|
+
): Promise<TrimInfo> {
|
|
80
|
+
try {
|
|
81
|
+
const trimResult = await encoder(input).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
|
|
82
|
+
if (!isResolvedBuffer(trimResult)) {
|
|
83
|
+
throw new Error('atlas: encoder raw alpha trim did not return resolved metadata.');
|
|
84
|
+
}
|
|
85
|
+
const { data, info } = trimResult;
|
|
86
|
+
const width = info.width;
|
|
87
|
+
const height = info.height;
|
|
88
|
+
const channels = info.channels || 4;
|
|
89
|
+
let minX = width;
|
|
90
|
+
let minY = height;
|
|
91
|
+
let maxX = -1;
|
|
92
|
+
let maxY = -1;
|
|
93
|
+
|
|
94
|
+
for (let y = 0; y < height; y += 1) {
|
|
95
|
+
for (let x = 0; x < width; x += 1) {
|
|
96
|
+
const alphaIndex = (y * width + x) * channels + 3;
|
|
97
|
+
if ((data[alphaIndex] ?? 0) === 0) continue;
|
|
98
|
+
if (x < minX) minX = x;
|
|
99
|
+
if (y < minY) minY = y;
|
|
100
|
+
if (x > maxX) maxX = x;
|
|
101
|
+
if (y > maxY) maxY = y;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (maxX < minX || maxY < minY) {
|
|
106
|
+
return {
|
|
107
|
+
buffer: new Uint8Array(0),
|
|
108
|
+
width: 0,
|
|
109
|
+
height: 0,
|
|
110
|
+
offsetX: 0,
|
|
111
|
+
offsetY: 0,
|
|
112
|
+
originalWidth,
|
|
113
|
+
originalHeight,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const trimmedWidth = maxX - minX + 1;
|
|
118
|
+
const trimmedHeight = maxY - minY + 1;
|
|
119
|
+
const buffer = await encoder(input)
|
|
120
|
+
.extract({
|
|
121
|
+
left: minX,
|
|
122
|
+
top: minY,
|
|
123
|
+
width: trimmedWidth,
|
|
124
|
+
height: trimmedHeight,
|
|
125
|
+
})
|
|
126
|
+
.toBuffer();
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
buffer,
|
|
130
|
+
width: trimmedWidth,
|
|
131
|
+
height: trimmedHeight,
|
|
132
|
+
offsetX: minX,
|
|
133
|
+
offsetY: minY,
|
|
134
|
+
originalWidth,
|
|
135
|
+
originalHeight,
|
|
136
|
+
};
|
|
137
|
+
} catch {
|
|
138
|
+
// Trim failed (e.g. JPEG without alpha, nothing to trim) — return original
|
|
139
|
+
const buf = await encoder(input).png().toBuffer();
|
|
140
|
+
return {
|
|
141
|
+
buffer: buf,
|
|
142
|
+
width: originalWidth,
|
|
143
|
+
height: originalHeight,
|
|
144
|
+
offsetX: 0,
|
|
145
|
+
offsetY: 0,
|
|
146
|
+
originalWidth,
|
|
147
|
+
originalHeight,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Resolve an ImageResource to its actual file path on disk.
|
|
154
|
+
*/
|
|
155
|
+
export type InputItem = {
|
|
156
|
+
id: string;
|
|
157
|
+
width: number;
|
|
158
|
+
height: number;
|
|
159
|
+
originalWidth: number;
|
|
160
|
+
originalHeight: number;
|
|
161
|
+
offsetX: number;
|
|
162
|
+
offsetY: number;
|
|
163
|
+
resource: PackInputResource;
|
|
164
|
+
trimBuffer?: Uint8Array;
|
|
165
|
+
rasterizedBuffer?: Uint8Array;
|
|
166
|
+
sourceKind: 'image' | 'movieclip-frame';
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
export interface StandaloneAtlasGroup {
|
|
170
|
+
resource: PackInputResource;
|
|
171
|
+
branchName: string;
|
|
172
|
+
branchOrdinal: number;
|
|
173
|
+
sizeMode: 'default' | 'npot' | 'multipleOf4';
|
|
174
|
+
inputs: InputItem[];
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export interface PagedAtlasGroup {
|
|
178
|
+
pageIndex: number;
|
|
179
|
+
branchName: string;
|
|
180
|
+
branchOrdinal: number;
|
|
181
|
+
inputs: InputItem[];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function resolveMovieClipSourcePath(resource: MovieClipResource, pkg: Package, basePath: string): string {
|
|
185
|
+
const fileName = `${resource.getName()}.jta`;
|
|
186
|
+
const resourcePath = resource.getPath() ?? '/';
|
|
187
|
+
return `${basePath}/${pkg.getName()}${resourcePath}${fileName}`;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export async function prepareMovieClipResource(
|
|
191
|
+
resource: MovieClipResource,
|
|
192
|
+
pkg: Package,
|
|
193
|
+
encoder: AtlasRasterBackend | undefined,
|
|
194
|
+
basePath: string,
|
|
195
|
+
readFileRaw: (path: string) => Promise<Uint8Array>,
|
|
196
|
+
): Promise<PreparedJtaData> {
|
|
197
|
+
const filePath = resolveMovieClipSourcePath(resource, pkg, basePath);
|
|
198
|
+
let raw: Uint8Array;
|
|
199
|
+
try {
|
|
200
|
+
raw = await readFileRaw(filePath);
|
|
201
|
+
} catch {
|
|
202
|
+
throw new Error(`atlas: Could not read MovieClip "${filePath}".`);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
try {
|
|
206
|
+
return await prepareJtaForPublish(raw, encoder, filePath);
|
|
207
|
+
} catch (error) {
|
|
208
|
+
if (error instanceof Error && error.message.startsWith('atlas:')) throw error;
|
|
209
|
+
const detail = error instanceof Error ? ` ${error.message}` : '';
|
|
210
|
+
throw new Error(`atlas: Could not parse MovieClip "${filePath}".${detail}`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Collect a single ImageResource into the inputs array. */
|
|
215
|
+
export async function collectImage(
|
|
216
|
+
resource: ImageResource,
|
|
217
|
+
pkg: Package,
|
|
218
|
+
inputs: InputItem[],
|
|
219
|
+
encoder: AtlasRasterBackend | undefined,
|
|
220
|
+
options: AtlasOptions,
|
|
221
|
+
doTrim: boolean,
|
|
222
|
+
logger: ILogger,
|
|
223
|
+
): Promise<void> {
|
|
224
|
+
let origW = resource.getWidth() ?? 0;
|
|
225
|
+
let origH = resource.getHeight() ?? 0;
|
|
226
|
+
const declaredWidth = origW;
|
|
227
|
+
const declaredHeight = origH;
|
|
228
|
+
let sourceHasAlpha = false;
|
|
229
|
+
let rasterizedBuffer: Uint8Array | undefined;
|
|
230
|
+
|
|
231
|
+
if (encoder && options.basePath) {
|
|
232
|
+
const filePath = resolveImagePath(resource, pkg, options.basePath);
|
|
233
|
+
try {
|
|
234
|
+
const metadata = await encoder(filePath).metadata();
|
|
235
|
+
if (origW === 0 || origH === 0) {
|
|
236
|
+
origW = metadata.width ?? 0;
|
|
237
|
+
origH = metadata.height ?? 0;
|
|
238
|
+
resource.setWidth(origW);
|
|
239
|
+
resource.setHeight(origH);
|
|
240
|
+
}
|
|
241
|
+
sourceHasAlpha = metadata.hasAlpha === true || metadata.channels === 4;
|
|
242
|
+
if (/\.svg$/i.test(resolveImageFileName(resource)) && declaredWidth > 0 && declaredHeight > 0) {
|
|
243
|
+
rasterizedBuffer = await encoder(filePath)
|
|
244
|
+
.resize({ width: declaredWidth, height: declaredHeight, fit: 'fill' })
|
|
245
|
+
.png()
|
|
246
|
+
.toBuffer();
|
|
247
|
+
sourceHasAlpha = true;
|
|
248
|
+
}
|
|
249
|
+
} catch (error) {
|
|
250
|
+
if (options.strictOutput) {
|
|
251
|
+
const detail = error instanceof Error && error.message.startsWith('publishBrowser:')
|
|
252
|
+
? ` ${error.message}`
|
|
253
|
+
: '';
|
|
254
|
+
throw new Error(`atlas: Could not read image "${filePath}".${detail}`);
|
|
255
|
+
}
|
|
256
|
+
if (origW === 0 || origH === 0) {
|
|
257
|
+
logger.warn(`atlas: Could not read image "${filePath}", skipping.`);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (origW <= 0 || origH <= 0) return;
|
|
264
|
+
|
|
265
|
+
let packW = origW,
|
|
266
|
+
packH = origH,
|
|
267
|
+
offX = 0,
|
|
268
|
+
offY = 0;
|
|
269
|
+
let trimBuf: Uint8Array | undefined;
|
|
270
|
+
|
|
271
|
+
if (doTrim && sourceHasAlpha && options.basePath && encoder) {
|
|
272
|
+
const filePath = resolveImagePath(resource, pkg, options.basePath);
|
|
273
|
+
try {
|
|
274
|
+
const trimResult = await _trimImage(encoder, rasterizedBuffer ?? filePath, origW, origH);
|
|
275
|
+
packW = trimResult.width;
|
|
276
|
+
packH = trimResult.height;
|
|
277
|
+
offX = trimResult.offsetX;
|
|
278
|
+
offY = trimResult.offsetY;
|
|
279
|
+
trimBuf = trimResult.buffer;
|
|
280
|
+
} catch {
|
|
281
|
+
logger.warn(`atlas: Could not trim "${filePath}", using original.`);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
inputs.push({
|
|
286
|
+
id: getPublishedItemId(resource),
|
|
287
|
+
width: packW,
|
|
288
|
+
height: packH,
|
|
289
|
+
originalWidth: origW,
|
|
290
|
+
originalHeight: origH,
|
|
291
|
+
offsetX: offX,
|
|
292
|
+
offsetY: offY,
|
|
293
|
+
resource,
|
|
294
|
+
trimBuffer: trimBuf,
|
|
295
|
+
rasterizedBuffer,
|
|
296
|
+
sourceKind: 'image',
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** Collect MovieClip frame textures from a .jta file into the inputs array. */
|
|
301
|
+
export async function collectMovieClipFrames(
|
|
302
|
+
doc: Document,
|
|
303
|
+
resource: MovieClipResource,
|
|
304
|
+
pkg: Package,
|
|
305
|
+
inputs: InputItem[],
|
|
306
|
+
encoder: AtlasRasterBackend | undefined,
|
|
307
|
+
options: AtlasOptions,
|
|
308
|
+
logger: ILogger,
|
|
309
|
+
): Promise<void> {
|
|
310
|
+
if (!options.basePath || !options.readFileRaw) {
|
|
311
|
+
if (options.strictOutput) {
|
|
312
|
+
throw new Error(`atlas: MovieClip "${resource.getId()}" requires basePath and readFileRaw for complete raster output.`);
|
|
313
|
+
}
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
if (!encoder && options.strictOutput) {
|
|
317
|
+
throw new Error(`atlas: MovieClip "${resource.getId()}" requires an encoder for complete raster output.`);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const mcId = resource.getId();
|
|
321
|
+
const filePath = resolveMovieClipSourcePath(resource, pkg, options.basePath);
|
|
322
|
+
|
|
323
|
+
try {
|
|
324
|
+
const jta =
|
|
325
|
+
options.preparedMovieClips?.get(resource) ??
|
|
326
|
+
(await prepareMovieClipResource(resource, pkg, encoder, options.basePath, options.readFileRaw));
|
|
327
|
+
for (const frame of resource.listFrames()) {
|
|
328
|
+
resource.removeFrame(frame);
|
|
329
|
+
}
|
|
330
|
+
resource
|
|
331
|
+
.setInterval(jta.meta.interval)
|
|
332
|
+
.setSwing(jta.meta.swing)
|
|
333
|
+
.setRepeatDelay(jta.meta.repeatDelay);
|
|
334
|
+
const spriteIdByTextureIndex = new Map<number, string>();
|
|
335
|
+
for (const texture of jta.referencedTextures) {
|
|
336
|
+
if (texture.width <= 0 || texture.height <= 0) continue;
|
|
337
|
+
const itemId = `${mcId}_${texture.firstFrameIndex}`;
|
|
338
|
+
inputs.push({
|
|
339
|
+
id: itemId,
|
|
340
|
+
width: texture.width,
|
|
341
|
+
height: texture.height,
|
|
342
|
+
originalWidth: texture.width,
|
|
343
|
+
originalHeight: texture.height,
|
|
344
|
+
offsetX: 0,
|
|
345
|
+
offsetY: 0,
|
|
346
|
+
resource,
|
|
347
|
+
trimBuffer: texture.buffer,
|
|
348
|
+
sourceKind: 'movieclip-frame',
|
|
349
|
+
});
|
|
350
|
+
spriteIdByTextureIndex.set(texture.textureIndex, itemId);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
for (let frameIndex = 0; frameIndex < jta.meta.frames.length; frameIndex += 1) {
|
|
354
|
+
const meta = jta.meta.frames[frameIndex]!;
|
|
355
|
+
const frame = doc.createMovieFrame(`${mcId}_${frameIndex}`);
|
|
356
|
+
frame
|
|
357
|
+
.setRectX(meta.offsetX)
|
|
358
|
+
.setRectY(meta.offsetY)
|
|
359
|
+
.setRectWidth(meta.width)
|
|
360
|
+
.setRectHeight(meta.height)
|
|
361
|
+
.setAddDelay(meta.addDelay)
|
|
362
|
+
.setSpriteId(meta.textureIndex === -1 ? '' : (spriteIdByTextureIndex.get(meta.textureIndex) ?? ''));
|
|
363
|
+
resource.addFrame(frame);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
if (jta.meta.width > 0 && jta.meta.height > 0) {
|
|
367
|
+
resource.setWidth(jta.meta.width);
|
|
368
|
+
resource.setHeight(jta.meta.height);
|
|
369
|
+
}
|
|
370
|
+
} catch (error) {
|
|
371
|
+
const message = error instanceof Error ? error.message : `atlas: Could not parse MovieClip "${filePath}".`;
|
|
372
|
+
if (options.strictOutput) throw error;
|
|
373
|
+
logger.warn(`${message} Skipping frames.`);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** Collect a Bitmap Font's texture image, packed under the font's ID. */
|
|
378
|
+
export async function collectFontTexture(
|
|
379
|
+
doc: Document,
|
|
380
|
+
fontRes: FontResource,
|
|
381
|
+
pkg: Package,
|
|
382
|
+
options: AtlasOptions,
|
|
383
|
+
): Promise<void> {
|
|
384
|
+
const textureId = fontRes.getTextureId?.() ?? '';
|
|
385
|
+
|
|
386
|
+
if (textureId) {
|
|
387
|
+
// Record font→texture mapping so we can add a duplicate sprite entry
|
|
388
|
+
// after atlas packing. The editor stores both the image ID (jb800) and
|
|
389
|
+
// the font ID (wa8u2r) as separate sprites at the same atlas position.
|
|
390
|
+
const fontId = fontRes.getId();
|
|
391
|
+
fontRes.setExtras({ ...fontRes.getExtras(), _fontSpriteAlias: { fontId, textureId } });
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// Parse .fnt file for glyph data (needed for binary encoding)
|
|
395
|
+
// This applies to ALL fonts, not just those with a textureId
|
|
396
|
+
if (options.readFileRaw && options.basePath) {
|
|
397
|
+
const fontName = resolveFontFileName(fontRes.getName());
|
|
398
|
+
const fontPath = fontRes.getPath() ?? '/';
|
|
399
|
+
const pkgName = pkg.getName();
|
|
400
|
+
const fntFile = `${options.basePath}/${pkgName}${fontPath}${fontName}`;
|
|
401
|
+
try {
|
|
402
|
+
const fntData = await options.readFileRaw(fntFile);
|
|
403
|
+
const fntText = new TextDecoder().decode(fntData);
|
|
404
|
+
const fntParsed = parseFnt(fntText);
|
|
405
|
+
for (const glyph of fontRes.listGlyphs()) {
|
|
406
|
+
fontRes.removeGlyph(glyph);
|
|
407
|
+
}
|
|
408
|
+
fontRes
|
|
409
|
+
.setTtf(fntParsed.hasFace)
|
|
410
|
+
.setTint(fntParsed.colored)
|
|
411
|
+
.setAutoScale(fntParsed.resizable)
|
|
412
|
+
.setHasChannel(fntParsed.hasChannel)
|
|
413
|
+
.setFontSize(fntParsed.fontSize)
|
|
414
|
+
.setXAdvance(fntParsed.xadvance)
|
|
415
|
+
.setLineHeight(fntParsed.lineHeight);
|
|
416
|
+
for (const item of fntParsed.glyphs) {
|
|
417
|
+
const glyph = doc.createFontGlyph(`${fontRes.getId()}_${item.charId}`);
|
|
418
|
+
glyph
|
|
419
|
+
.setCharId(item.charId)
|
|
420
|
+
.setChar(item.charId > 0 ? String.fromCodePoint(item.charId) : '')
|
|
421
|
+
.setImg(item.img ?? '')
|
|
422
|
+
.setX(item.x)
|
|
423
|
+
.setY(item.y)
|
|
424
|
+
.setXOffset(item.xoffset)
|
|
425
|
+
.setYOffset(item.yoffset)
|
|
426
|
+
.setWidth(item.width)
|
|
427
|
+
.setHeight(item.height)
|
|
428
|
+
.setAdvance(item.xadvance)
|
|
429
|
+
.setLineHeight(fntParsed.lineHeight)
|
|
430
|
+
.setChannel(item.channel);
|
|
431
|
+
fontRes.addGlyph(glyph);
|
|
432
|
+
}
|
|
433
|
+
} catch {
|
|
434
|
+
/* .fnt not found */
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
export function isPackableResource(resource: PackageResource): resource is PackableResource {
|
|
440
|
+
return isImageResource(resource) || isMovieClipResource(resource) || isFontResource(resource);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function isResolvedBuffer(value: Uint8Array | AtlasRasterResolvedBuffer): value is AtlasRasterResolvedBuffer {
|
|
444
|
+
return typeof value === 'object' && value !== null && 'data' in value && 'info' in value;
|
|
445
|
+
}
|
package/src/atlas/jta.ts
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import {
|
|
2
|
+
deriveMovieClipModel,
|
|
3
|
+
parseJta,
|
|
4
|
+
probeRasterImage,
|
|
5
|
+
type RasterImageFormat,
|
|
6
|
+
} from '@openfairygui/core';
|
|
7
|
+
import type { AtlasRasterBackend } from '../publish/contracts.js';
|
|
8
|
+
|
|
9
|
+
export interface JtaFrameMeta {
|
|
10
|
+
addDelay: number;
|
|
11
|
+
offsetX: number;
|
|
12
|
+
offsetY: number;
|
|
13
|
+
width: number;
|
|
14
|
+
height: number;
|
|
15
|
+
textureIndex: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface JtaMeta {
|
|
19
|
+
interval: number;
|
|
20
|
+
repeatDelay: number;
|
|
21
|
+
swing: boolean;
|
|
22
|
+
width: number;
|
|
23
|
+
height: number;
|
|
24
|
+
frames: JtaFrameMeta[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface ExtractedJtaData {
|
|
28
|
+
frames: Uint8Array[];
|
|
29
|
+
meta: JtaMeta;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface PreparedJtaTexture {
|
|
33
|
+
textureIndex: number;
|
|
34
|
+
firstFrameIndex: number;
|
|
35
|
+
/** PNG bytes validated during preflight and reused by atlas compositing. */
|
|
36
|
+
buffer: Uint8Array;
|
|
37
|
+
width: number;
|
|
38
|
+
height: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface PreparedJtaData extends ExtractedJtaData {
|
|
42
|
+
referencedTextures: PreparedJtaTexture[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function extractJtaFrames(data: Uint8Array): ExtractedJtaData {
|
|
46
|
+
const parsed = parseJta(data);
|
|
47
|
+
const derived = deriveMovieClipModel(parsed);
|
|
48
|
+
return {
|
|
49
|
+
frames: parsed.textures.map((texture) => texture.raw),
|
|
50
|
+
meta: {
|
|
51
|
+
interval: derived.interval,
|
|
52
|
+
repeatDelay: derived.repeatDelay,
|
|
53
|
+
swing: derived.swing,
|
|
54
|
+
width: derived.dimensions.width,
|
|
55
|
+
height: derived.dimensions.height,
|
|
56
|
+
frames: derived.frames.map((frame) => ({
|
|
57
|
+
addDelay: frame.addDelay,
|
|
58
|
+
offsetX: frame.rectX,
|
|
59
|
+
offsetY: frame.rectY,
|
|
60
|
+
width: frame.rectWidth,
|
|
61
|
+
height: frame.rectHeight,
|
|
62
|
+
textureIndex: frame.textureIndex,
|
|
63
|
+
})),
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function detectSupportedRasterFormat(data: Uint8Array): RasterImageFormat | null {
|
|
69
|
+
if (
|
|
70
|
+
data.length >= 8 &&
|
|
71
|
+
data[0] === 0x89 &&
|
|
72
|
+
data[1] === 0x50 &&
|
|
73
|
+
data[2] === 0x4e &&
|
|
74
|
+
data[3] === 0x47 &&
|
|
75
|
+
data[4] === 0x0d &&
|
|
76
|
+
data[5] === 0x0a &&
|
|
77
|
+
data[6] === 0x1a &&
|
|
78
|
+
data[7] === 0x0a
|
|
79
|
+
) {
|
|
80
|
+
return 'png';
|
|
81
|
+
}
|
|
82
|
+
if (data.length >= 2 && data[0] === 0xff && data[1] === 0xd8) return 'jpeg';
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function couldNotDecode(filePath: string, frameIndex: number, textureIndex: number): Error {
|
|
87
|
+
return new Error(
|
|
88
|
+
`atlas: Could not decode MovieClip "${filePath}" frame ${frameIndex} (texture ${textureIndex}).`,
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export async function prepareJtaForPublish(
|
|
93
|
+
data: Uint8Array,
|
|
94
|
+
encoder: AtlasRasterBackend | undefined,
|
|
95
|
+
filePath: string,
|
|
96
|
+
): Promise<PreparedJtaData> {
|
|
97
|
+
const extracted = extractJtaFrames(data);
|
|
98
|
+
const firstFrameIndexByTextureIndex = new Map<number, number>();
|
|
99
|
+
|
|
100
|
+
for (let frameIndex = 0; frameIndex < extracted.meta.frames.length; frameIndex += 1) {
|
|
101
|
+
const textureIndex = extracted.meta.frames[frameIndex]!.textureIndex;
|
|
102
|
+
if (textureIndex >= 0 && !firstFrameIndexByTextureIndex.has(textureIndex)) {
|
|
103
|
+
firstFrameIndexByTextureIndex.set(textureIndex, frameIndex);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const referencedTextures: PreparedJtaTexture[] = [];
|
|
108
|
+
for (let textureIndex = 0; textureIndex < extracted.frames.length; textureIndex += 1) {
|
|
109
|
+
const firstFrameIndex = firstFrameIndexByTextureIndex.get(textureIndex);
|
|
110
|
+
if (firstFrameIndex === undefined) continue;
|
|
111
|
+
const raw = extracted.frames[textureIndex]!;
|
|
112
|
+
if (raw.byteLength === 0) {
|
|
113
|
+
throw new Error(
|
|
114
|
+
`atlas: MovieClip "${filePath}" frame ${firstFrameIndex} references empty texture ${textureIndex}.`,
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
const detectedFormat = detectSupportedRasterFormat(raw);
|
|
118
|
+
if (!detectedFormat) {
|
|
119
|
+
throw new Error(
|
|
120
|
+
`atlas: MovieClip "${filePath}" frame ${firstFrameIndex} (texture ${textureIndex}) uses an unsupported ` +
|
|
121
|
+
'raster format; only PNG and JPEG are supported.',
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
const imageInfo = probeRasterImage(raw);
|
|
125
|
+
if (!imageInfo || imageInfo.format !== detectedFormat) {
|
|
126
|
+
throw couldNotDecode(filePath, firstFrameIndex, textureIndex);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
let buffer = raw;
|
|
130
|
+
if (encoder) {
|
|
131
|
+
try {
|
|
132
|
+
buffer = await encoder(raw).png().toBuffer();
|
|
133
|
+
} catch {
|
|
134
|
+
throw couldNotDecode(filePath, firstFrameIndex, textureIndex);
|
|
135
|
+
}
|
|
136
|
+
const normalizedInfo = probeRasterImage(buffer);
|
|
137
|
+
if (
|
|
138
|
+
!normalizedInfo ||
|
|
139
|
+
normalizedInfo.format !== 'png' ||
|
|
140
|
+
normalizedInfo.width !== imageInfo.width ||
|
|
141
|
+
normalizedInfo.height !== imageInfo.height
|
|
142
|
+
) {
|
|
143
|
+
throw couldNotDecode(filePath, firstFrameIndex, textureIndex);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
referencedTextures.push({
|
|
148
|
+
textureIndex,
|
|
149
|
+
firstFrameIndex,
|
|
150
|
+
buffer,
|
|
151
|
+
width: imageInfo.width,
|
|
152
|
+
height: imageInfo.height,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return { ...extracted, referencedTextures };
|
|
157
|
+
}
|