@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.
Files changed (55) hide show
  1. package/README.md +52 -6
  2. package/dist/atlas-C6tbl7nn.d.ts +193 -0
  3. package/dist/atlas-CHsu2Y8i.d.cts +193 -0
  4. package/dist/index.cjs +17 -3603
  5. package/dist/index.d.cts +5 -294
  6. package/dist/index.d.ts +5 -294
  7. package/dist/index.js +4 -3595
  8. package/dist/node.cjs +256 -0
  9. package/dist/node.d.cts +36 -0
  10. package/dist/node.d.ts +36 -0
  11. package/dist/node.js +254 -0
  12. package/dist/publish-CykUJfVa.cjs +3269 -0
  13. package/dist/publish-DXoaC1Nl.js +3174 -0
  14. package/dist/restore-BQp01WY3.js +914 -0
  15. package/dist/restore-BeWaJNjR.d.cts +288 -0
  16. package/dist/restore-CEywQUHz.cjs +919 -0
  17. package/dist/restore-Dh0-Nvms.d.ts +288 -0
  18. package/dist/uam-transaction.cjs +29 -14
  19. package/dist/uam-transaction.d.cts +2 -1
  20. package/dist/uam-transaction.d.ts +2 -1
  21. package/dist/uam-transaction.js +30 -16
  22. package/dist/web.cjs +440 -0
  23. package/dist/web.d.cts +44 -0
  24. package/dist/web.d.ts +44 -0
  25. package/dist/web.js +439 -0
  26. package/package.json +29 -4
  27. package/src/adapters/node/plugins.ts +82 -0
  28. package/src/adapters/node/publish.ts +130 -0
  29. package/src/adapters/node/restore.ts +187 -0
  30. package/src/adapters/web/publish.ts +196 -0
  31. package/src/adapters/web/raster.ts +421 -0
  32. package/src/atlas/font.ts +95 -0
  33. package/src/atlas/inputs.ts +445 -0
  34. package/src/atlas/jta.ts +157 -0
  35. package/src/atlas/packing.ts +762 -0
  36. package/src/atlas.ts +129 -1221
  37. package/src/codegen.ts +108 -82
  38. package/src/index.ts +43 -3
  39. package/src/node.ts +8 -0
  40. package/src/path-utils.ts +40 -0
  41. package/src/plugins/types.ts +56 -0
  42. package/src/publish/contracts.ts +80 -0
  43. package/src/publish/external-resources.ts +117 -0
  44. package/src/publish/options.ts +180 -0
  45. package/src/publish/package-context.ts +608 -0
  46. package/src/publish/resource-references.ts +210 -0
  47. package/src/publish.ts +327 -975
  48. package/src/restore-internals/font.ts +100 -0
  49. package/src/restore-internals/movie-clip.ts +104 -0
  50. package/src/restore-internals/output-transaction.ts +124 -0
  51. package/src/restore.ts +122 -311
  52. package/src/shared-types.ts +4 -8
  53. package/src/uam-transaction.ts +34 -17
  54. package/src/utils.ts +28 -0
  55. package/src/web.ts +11 -0
@@ -0,0 +1,421 @@
1
+ import type {
2
+ AtlasRasterBackend,
3
+ AtlasRasterInput,
4
+ AtlasRasterPipeline,
5
+ AtlasRasterResolvedBuffer,
6
+ PublishFileSystem,
7
+ PublishSourceFileSystem,
8
+ } from '../../publish/contracts.js';
9
+ import { XMLParser, XMLValidator } from 'fast-xml-parser';
10
+
11
+ type BrowserCanvas = OffscreenCanvas | HTMLCanvasElement;
12
+
13
+ interface BrowserContext {
14
+ clearRect(x: number, y: number, width: number, height: number): void;
15
+ drawImage(image: CanvasImageSource, dx: number, dy: number): void;
16
+ drawImage(image: CanvasImageSource, dx: number, dy: number, dw: number, dh: number): void;
17
+ drawImage(
18
+ image: CanvasImageSource,
19
+ sx: number,
20
+ sy: number,
21
+ sw: number,
22
+ sh: number,
23
+ dx: number,
24
+ dy: number,
25
+ dw: number,
26
+ dh: number,
27
+ ): void;
28
+ fillRect(x: number, y: number, width: number, height: number): void;
29
+ getImageData(sx: number, sy: number, sw: number, sh: number): ImageData;
30
+ rotate(angle: number): void;
31
+ restore(): void;
32
+ save(): void;
33
+ translate(x: number, y: number): void;
34
+ fillStyle: string | CanvasGradient | CanvasPattern;
35
+ }
36
+
37
+ interface BrowserRaster {
38
+ canvas: BrowserCanvas;
39
+ width: number;
40
+ height: number;
41
+ }
42
+
43
+ const MAX_SVG_SOURCE_BYTES = 8 * 1024 * 1024;
44
+ const MAX_SVG_DIMENSION = 16_384;
45
+ const MAX_SVG_PIXELS = 64 * 1024 * 1024;
46
+ const MAX_SVG_NODES = 50_000;
47
+ const UNSAFE_SVG_ELEMENTS = new Set([
48
+ 'a',
49
+ 'animate',
50
+ 'animatecolor',
51
+ 'animatemotion',
52
+ 'animatetransform',
53
+ 'audio',
54
+ 'canvas',
55
+ 'discard',
56
+ 'embed',
57
+ 'feimage',
58
+ 'foreignobject',
59
+ 'iframe',
60
+ 'image',
61
+ 'object',
62
+ 'script',
63
+ 'set',
64
+ 'style',
65
+ 'video',
66
+ ]);
67
+
68
+ type ParsedSvgEntry = Record<string, unknown> & { ':@'?: Record<string, unknown> };
69
+
70
+ function unsafeSvg(message: string): never {
71
+ throw new Error(`publishBrowser: unsafe SVG input (${message}).`);
72
+ }
73
+
74
+ function svgLocalName(name: string): string {
75
+ return name.split(':').at(-1)!.toLowerCase();
76
+ }
77
+
78
+ function parseSvgLength(value: unknown, name: string): number | undefined {
79
+ if (value === undefined) return undefined;
80
+ const match = String(value).match(/^\s*(?:\d+(?:\.\d+)?|\.\d+)(?:px)?\s*$/iu);
81
+ if (!match) unsafeSvg(`${name} must use a finite pixel value`);
82
+ const parsed = Number.parseFloat(match[0]);
83
+ if (!Number.isFinite(parsed) || parsed <= 0 || parsed > MAX_SVG_DIMENSION) {
84
+ unsafeSvg(`${name} exceeds the supported dimensions`);
85
+ }
86
+ return parsed;
87
+ }
88
+
89
+ function validateSvgAttribute(name: string, value: unknown): void {
90
+ const normalizedName = name.toLowerCase();
91
+ if (normalizedName === 'xmlns' || normalizedName.startsWith('xmlns:')) return;
92
+ const localName = svgLocalName(name);
93
+ const text = String(value);
94
+ if (localName.startsWith('on')) unsafeSvg(`event attribute "${name}" is not allowed`);
95
+ if (localName === 'style' || localName === 'src') unsafeSvg(`attribute "${name}" is not allowed`);
96
+ if (localName === 'href' && !/^#[A-Za-z_][\w:.-]*$/u.test(text)) {
97
+ unsafeSvg(`external reference in "${name}" is not allowed`);
98
+ }
99
+ if (/(?:^|[\s("'=])(?:https?:|file:|javascript:|data:|\/\/)/iu.test(text)) {
100
+ unsafeSvg(`external URL in "${name}" is not allowed`);
101
+ }
102
+ for (const match of text.matchAll(/url\s*\(([^)]*)\)/giu)) {
103
+ const reference = (match[1] ?? '').trim().replace(/^(['"])(.*)\1$/u, '$2');
104
+ if (!/^#[A-Za-z_][\w:.-]*$/u.test(reference)) unsafeSvg(`external url() in "${name}" is not allowed`);
105
+ }
106
+ }
107
+
108
+ function visitSvgEntry(entry: ParsedSvgEntry): void {
109
+ const pending = [entry];
110
+ let nodeCount = 0;
111
+ while (pending.length > 0) {
112
+ const current = pending.pop()!;
113
+ for (const [name, value] of Object.entries(current)) {
114
+ if (name === ':@' || name.startsWith('#') || name.startsWith('?')) continue;
115
+ if (++nodeCount > MAX_SVG_NODES) unsafeSvg('node count exceeds the supported limit');
116
+ const localName = svgLocalName(name);
117
+ if (UNSAFE_SVG_ELEMENTS.has(localName)) unsafeSvg(`element <${name}> is not allowed`);
118
+ for (const [attributeName, attributeValue] of Object.entries(current[':@'] ?? {})) {
119
+ validateSvgAttribute(attributeName, attributeValue);
120
+ }
121
+ if (Array.isArray(value)) {
122
+ for (const child of value) {
123
+ if (child && typeof child === 'object' && !Array.isArray(child)) pending.push(child as ParsedSvgEntry);
124
+ }
125
+ }
126
+ }
127
+ }
128
+ }
129
+
130
+ function validateSvg(bytes: Uint8Array): void {
131
+ if (bytes.byteLength === 0 || bytes.byteLength > MAX_SVG_SOURCE_BYTES) unsafeSvg('source size is unsupported');
132
+ let source: string;
133
+ try {
134
+ source = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
135
+ } catch {
136
+ unsafeSvg('source is not valid UTF-8');
137
+ }
138
+ if (/<!\s*(?:doctype|entity)\b|<\?xml-stylesheet\b/iu.test(source)) unsafeSvg('DTD, entities, and stylesheets are not allowed');
139
+ if (XMLValidator.validate(source, { allowBooleanAttributes: false }) !== true) unsafeSvg('source is not well-formed XML');
140
+ const parsed = new XMLParser({
141
+ preserveOrder: true,
142
+ ignoreAttributes: false,
143
+ attributeNamePrefix: '',
144
+ parseAttributeValue: false,
145
+ parseTagValue: false,
146
+ processEntities: false,
147
+ trimValues: false,
148
+ }).parse(source) as ParsedSvgEntry[];
149
+ const roots = parsed.flatMap((entry) => Object.keys(entry)
150
+ .filter((name) => name !== ':@' && !name.startsWith('#') && !name.startsWith('?'))
151
+ .map((name) => ({ entry, name })));
152
+ if (roots.length !== 1 || svgLocalName(roots[0]!.name) !== 'svg') unsafeSvg('a single <svg> root is required');
153
+ const root = roots[0]!.entry;
154
+ visitSvgEntry(root);
155
+ const attributes = root[':@'] ?? {};
156
+ const width = parseSvgLength(attributes.width, 'width');
157
+ const height = parseSvgLength(attributes.height, 'height');
158
+ let viewBoxWidth: number | undefined;
159
+ let viewBoxHeight: number | undefined;
160
+ if (attributes.viewBox !== undefined) {
161
+ const viewBox = String(attributes.viewBox).trim().split(/[\s,]+/u).map(Number);
162
+ if (viewBox.length !== 4 || viewBox.some((value) => !Number.isFinite(value)) || viewBox[2]! <= 0 || viewBox[3]! <= 0) {
163
+ unsafeSvg('viewBox must contain four finite values with positive dimensions');
164
+ }
165
+ viewBoxWidth = viewBox[2];
166
+ viewBoxHeight = viewBox[3];
167
+ if (viewBoxWidth > MAX_SVG_DIMENSION || viewBoxHeight > MAX_SVG_DIMENSION) unsafeSvg('viewBox exceeds the supported dimensions');
168
+ }
169
+ const rasterWidth = width ?? viewBoxWidth ?? 300;
170
+ const rasterHeight = height ?? viewBoxHeight ?? 150;
171
+ if (rasterWidth * rasterHeight > MAX_SVG_PIXELS) unsafeSvg('pixel count exceeds the supported limit');
172
+ }
173
+
174
+ function getBrowserContext(canvas: BrowserCanvas): BrowserContext {
175
+ const context = canvas.getContext('2d');
176
+ if (!context) throw new Error('publishBrowser: a 2D canvas context is unavailable.');
177
+ return context as unknown as BrowserContext;
178
+ }
179
+
180
+ function createBrowserCanvas(width: number, height: number): BrowserCanvas {
181
+ if (typeof OffscreenCanvas !== 'undefined') return new OffscreenCanvas(width, height);
182
+ if (typeof globalThis.document === 'undefined') {
183
+ throw new Error('publishBrowser: OffscreenCanvas or a DOM canvas is required for atlas PNG generation.');
184
+ }
185
+ const canvas = globalThis.document.createElement('canvas');
186
+ canvas.width = width;
187
+ canvas.height = height;
188
+ return canvas;
189
+ }
190
+
191
+ export function assertBrowserImageSupport(): void {
192
+ if (typeof createImageBitmap !== 'function') {
193
+ throw new Error('publishBrowser: createImageBitmap is required for atlas PNG generation.');
194
+ }
195
+ if (typeof OffscreenCanvas === 'undefined' && typeof globalThis.document === 'undefined') {
196
+ throw new Error('publishBrowser: OffscreenCanvas or a DOM canvas is required for atlas PNG generation.');
197
+ }
198
+ }
199
+
200
+ function createRaster(
201
+ width: number,
202
+ height: number,
203
+ background?: { r: number; g: number; b: number; alpha: number },
204
+ ): BrowserRaster {
205
+ const canvas = createBrowserCanvas(width, height);
206
+ const context = getBrowserContext(canvas);
207
+ context.clearRect(0, 0, width, height);
208
+ if (background && background.alpha > 0) {
209
+ context.fillStyle = `rgba(${background.r}, ${background.g}, ${background.b}, ${background.alpha})`;
210
+ context.fillRect(0, 0, width, height);
211
+ }
212
+ return { canvas, width, height };
213
+ }
214
+
215
+ function imageMimeType(path: string): string {
216
+ if (/\.svg$/iu.test(path)) return 'image/svg+xml';
217
+ if (/\.jpe?g$/iu.test(path)) return 'image/jpeg';
218
+ if (/\.webp$/iu.test(path)) return 'image/webp';
219
+ if (/\.gif$/iu.test(path)) return 'image/gif';
220
+ return 'image/png';
221
+ }
222
+
223
+ function imageMimeTypeFromBytes(bytes: Uint8Array): string {
224
+ if (bytes[0] === 0xff && bytes[1] === 0xd8) return 'image/jpeg';
225
+ if (bytes[0] === 0x47 && bytes[1] === 0x49 && bytes[2] === 0x46) return 'image/gif';
226
+ if (bytes[0] === 0x52 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x46) return 'image/webp';
227
+ return 'image/png';
228
+ }
229
+
230
+ async function canvasToPng(canvas: BrowserCanvas): Promise<Uint8Array> {
231
+ let blob: Blob;
232
+ if ('convertToBlob' in canvas && typeof canvas.convertToBlob === 'function') {
233
+ blob = await canvas.convertToBlob({ type: 'image/png' });
234
+ } else {
235
+ blob = await new Promise<Blob>((resolve, reject) => {
236
+ (canvas as HTMLCanvasElement).toBlob((value) => {
237
+ if (value) resolve(value);
238
+ else reject(new Error('publishBrowser: canvas PNG encoding failed.'));
239
+ }, 'image/png');
240
+ });
241
+ }
242
+ return new Uint8Array(await blob.arrayBuffer());
243
+ }
244
+
245
+ async function decodeRaster(bytes: Uint8Array, mimeType: string): Promise<BrowserRaster> {
246
+ if (typeof createImageBitmap !== 'function') {
247
+ throw new Error('publishBrowser: createImageBitmap is required for atlas PNG generation.');
248
+ }
249
+ const copy = bytes.slice();
250
+ if (mimeType === 'image/svg+xml') validateSvg(copy);
251
+ const blob = new Blob([copy.buffer as ArrayBuffer], { type: mimeType });
252
+ let bitmap: ImageBitmap;
253
+ try {
254
+ bitmap = await createImageBitmap(blob);
255
+ } catch (error) {
256
+ if (mimeType !== 'image/svg+xml') throw error;
257
+ return decodeSvgWithDom(blob);
258
+ }
259
+ try {
260
+ const raster = createRaster(bitmap.width, bitmap.height);
261
+ getBrowserContext(raster.canvas).drawImage(bitmap, 0, 0);
262
+ return raster;
263
+ } finally {
264
+ bitmap.close();
265
+ }
266
+ }
267
+
268
+ async function decodeSvgWithDom(blob: Blob): Promise<BrowserRaster> {
269
+ if (typeof globalThis.Image !== 'function'
270
+ || typeof globalThis.URL?.createObjectURL !== 'function'
271
+ || typeof globalThis.URL?.revokeObjectURL !== 'function'
272
+ ) {
273
+ throw new Error('publishBrowser: createImageBitmap rejected SVG and DOM image decoding is unavailable.');
274
+ }
275
+ const url = globalThis.URL.createObjectURL(blob);
276
+ try {
277
+ const image = new globalThis.Image();
278
+ await new Promise<void>((resolve, reject) => {
279
+ image.onload = () => resolve();
280
+ image.onerror = () => reject(new Error('publishBrowser: DOM image decoding failed for SVG.'));
281
+ image.src = url;
282
+ });
283
+ const width = image.naturalWidth || image.width;
284
+ const height = image.naturalHeight || image.height;
285
+ if (!Number.isFinite(width) || !Number.isFinite(height)
286
+ || width <= 0 || height <= 0
287
+ || width > MAX_SVG_DIMENSION || height > MAX_SVG_DIMENSION
288
+ || width * height > MAX_SVG_PIXELS
289
+ ) unsafeSvg('decoded dimensions exceed the supported limit');
290
+ const raster = createRaster(width, height);
291
+ getBrowserContext(raster.canvas).drawImage(image, 0, 0);
292
+ return raster;
293
+ } finally {
294
+ globalThis.URL.revokeObjectURL(url);
295
+ }
296
+ }
297
+
298
+ class BrowserImagePipeline implements AtlasRasterPipeline {
299
+ private rawOutput = false;
300
+
301
+ constructor(
302
+ private raster: Promise<BrowserRaster>,
303
+ private readonly decode: (bytes: Uint8Array) => Promise<BrowserRaster>,
304
+ private readonly write: (path: string, data: Uint8Array) => Promise<void>,
305
+ ) {}
306
+
307
+ ensureAlpha(): this {
308
+ return this;
309
+ }
310
+
311
+ resize(options: { width: number; height: number; fit?: 'fill' }): this {
312
+ this.raster = this.raster.then((source) => {
313
+ const target = createRaster(options.width, options.height);
314
+ getBrowserContext(target.canvas).drawImage(source.canvas, 0, 0, options.width, options.height);
315
+ return target;
316
+ });
317
+ return this;
318
+ }
319
+
320
+ raw(): this {
321
+ this.rawOutput = true;
322
+ return this;
323
+ }
324
+
325
+ extract(options: { left: number; top: number; width: number; height: number }): this {
326
+ this.raster = this.raster.then((source) => {
327
+ const target = createRaster(options.width, options.height);
328
+ getBrowserContext(target.canvas).drawImage(
329
+ source.canvas,
330
+ options.left,
331
+ options.top,
332
+ options.width,
333
+ options.height,
334
+ 0,
335
+ 0,
336
+ options.width,
337
+ options.height,
338
+ );
339
+ return target;
340
+ });
341
+ return this;
342
+ }
343
+
344
+ png(): this {
345
+ this.rawOutput = false;
346
+ return this;
347
+ }
348
+
349
+ rotate(angle: number): this {
350
+ this.raster = this.raster.then((source) => {
351
+ if (angle % 180 === 0) return source;
352
+ const target = createRaster(source.height, source.width);
353
+ const context = getBrowserContext(target.canvas);
354
+ context.save();
355
+ if (angle === 270 || angle === -90) {
356
+ context.translate(0, source.width);
357
+ context.rotate(-Math.PI / 2);
358
+ } else {
359
+ context.translate(source.height, 0);
360
+ context.rotate(Math.PI / 2);
361
+ }
362
+ context.drawImage(source.canvas, 0, 0);
363
+ context.restore();
364
+ return target;
365
+ });
366
+ return this;
367
+ }
368
+
369
+ composite(inputs: Array<{ input: Uint8Array; left: number; top: number }>): this {
370
+ this.raster = this.raster.then(async (target) => {
371
+ const context = getBrowserContext(target.canvas);
372
+ for (const input of inputs) {
373
+ const source = await this.decode(input.input);
374
+ context.drawImage(source.canvas, input.left, input.top);
375
+ }
376
+ return target;
377
+ });
378
+ return this;
379
+ }
380
+
381
+ async metadata(): Promise<{ width: number; height: number; channels: number; hasAlpha: boolean }> {
382
+ const raster = await this.raster;
383
+ return { width: raster.width, height: raster.height, channels: 4, hasAlpha: true };
384
+ }
385
+
386
+ async toBuffer(options: { resolveWithObject: true }): Promise<AtlasRasterResolvedBuffer>;
387
+ async toBuffer(options?: { resolveWithObject?: false }): Promise<Uint8Array>;
388
+ async toBuffer(options?: { resolveWithObject?: boolean }): Promise<Uint8Array | AtlasRasterResolvedBuffer> {
389
+ const raster = await this.raster;
390
+ if (options?.resolveWithObject) {
391
+ const data = getBrowserContext(raster.canvas).getImageData(0, 0, raster.width, raster.height).data;
392
+ return { data: new Uint8Array(data), info: { width: raster.width, height: raster.height, channels: 4 } };
393
+ }
394
+ if (this.rawOutput)
395
+ return new Uint8Array(
396
+ getBrowserContext(raster.canvas).getImageData(0, 0, raster.width, raster.height).data,
397
+ );
398
+ return canvasToPng(raster.canvas);
399
+ }
400
+
401
+ async toFile(path: string): Promise<void> {
402
+ const raster = await this.raster;
403
+ await this.write(path, await canvasToPng(raster.canvas));
404
+ }
405
+ }
406
+
407
+ export function createBrowserImageEncoder(
408
+ sourceFileSystem: PublishSourceFileSystem,
409
+ outputFileSystem: PublishFileSystem,
410
+ ): AtlasRasterBackend {
411
+ const decode = (bytes: Uint8Array) => decodeRaster(bytes, imageMimeTypeFromBytes(bytes));
412
+ return (input: AtlasRasterInput): BrowserImagePipeline => {
413
+ const raster =
414
+ typeof input === 'string'
415
+ ? sourceFileSystem.readFileRaw(input).then((bytes) => decodeRaster(bytes, imageMimeType(input)))
416
+ : input instanceof Uint8Array
417
+ ? decode(input)
418
+ : Promise.resolve(createRaster(input.create.width, input.create.height, input.create.background));
419
+ return new BrowserImagePipeline(raster, decode, outputFileSystem.writeFileRaw);
420
+ };
421
+ }
@@ -0,0 +1,95 @@
1
+ export interface ParsedFontGlyph {
2
+ charId: number;
3
+ img: string | null;
4
+ x: number;
5
+ y: number;
6
+ xoffset: number;
7
+ yoffset: number;
8
+ width: number;
9
+ height: number;
10
+ xadvance: number;
11
+ channel: number;
12
+ }
13
+
14
+ export interface ParsedFont {
15
+ hasFace: boolean;
16
+ colored: boolean;
17
+ resizable: boolean;
18
+ hasChannel: boolean;
19
+ fontSize: number;
20
+ xadvance: number;
21
+ lineHeight: number;
22
+ glyphs: ParsedFontGlyph[];
23
+ }
24
+
25
+ /** Parse a BMFont .fnt text file into structured data for binary encoding. */
26
+ export function parseFnt(text: string): ParsedFont {
27
+ const lines = text.split(/\r?\n/);
28
+ let hasFace = false;
29
+ let colored = false;
30
+ let resizable = false;
31
+ let hasChannel = false;
32
+ let fontSize = 0;
33
+ let globalXadvance = 0;
34
+ let lineHeight = 0;
35
+ const glyphs: ParsedFontGlyph[] = [];
36
+
37
+ for (const line of lines) {
38
+ const trimmed = line.trim();
39
+ if (!trimmed) continue;
40
+ const parts = trimmed.split(/\s+/);
41
+ const attrs: Record<string, string> = {};
42
+ for (let index = 1; index < parts.length; index += 1) {
43
+ const entry = parts[index]?.split('=') ?? [];
44
+ if (entry.length === 2 && entry[0]) attrs[entry[0]] = entry[1] ?? '';
45
+ }
46
+
47
+ switch (parts[0]) {
48
+ case 'info':
49
+ hasFace = attrs.face != null;
50
+ colored = hasFace;
51
+ if (attrs.colored !== undefined) colored = attrs.colored === 'true';
52
+ fontSize = parseInt(attrs.size ?? '', 10) || 0;
53
+ resizable = attrs.resizable === 'true';
54
+ break;
55
+ case 'common':
56
+ lineHeight = parseInt(attrs.lineHeight ?? '', 10) || 0;
57
+ globalXadvance = parseInt(attrs.xadvance ?? '', 10) || 0;
58
+ if (fontSize === 0) fontSize = lineHeight;
59
+ else if (lineHeight === 0) lineHeight = fontSize;
60
+ break;
61
+ case 'char': {
62
+ const charId = parseInt(attrs.id ?? '', 10) || 0;
63
+ if (charId === 0) continue;
64
+ const img = attrs.img || null;
65
+ if (!hasFace && !img) continue;
66
+ const channel = parseInt(attrs.chnl ?? '', 10) || 0;
67
+ if (channel !== 0 && channel !== 15) hasChannel = true;
68
+ glyphs.push({
69
+ charId,
70
+ img,
71
+ x: parseInt(attrs.x ?? '', 10) || 0,
72
+ y: parseInt(attrs.y ?? '', 10) || 0,
73
+ xoffset: parseInt(attrs.xoffset ?? '', 10) || 0,
74
+ yoffset: parseInt(attrs.yoffset ?? '', 10) || 0,
75
+ width: parseInt(attrs.width ?? '', 10) || 0,
76
+ height: parseInt(attrs.height ?? '', 10) || 0,
77
+ xadvance: parseInt(attrs.xadvance ?? '', 10) || 0,
78
+ channel,
79
+ });
80
+ break;
81
+ }
82
+ }
83
+ }
84
+
85
+ return {
86
+ hasFace,
87
+ colored,
88
+ resizable: fontSize > 0 ? resizable : false,
89
+ hasChannel,
90
+ fontSize,
91
+ xadvance: globalXadvance,
92
+ lineHeight,
93
+ glyphs,
94
+ };
95
+ }