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