Record each sprite's size in its set's manifest

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Christopher Monsanto
2026-11-08 22:07:13 -05:00
parent 8c2bfc91b9
commit f207627b49
7 changed files with 62 additions and 9 deletions

View File

@@ -4,7 +4,7 @@ import * as nodePath from 'node:path';
import * as spritedata from '@smogon/sprite-data/index.ts';
import type {Artifact} from '../tools/build/artifact.ts';
import type {DeployCtx, SrcFile} from '../tools/deploy/api.ts';
import type {DeployCtx, Size, SrcFile} from '../tools/deploy/api.ts';
// Anything with a sprite-id nominal name and an extension: a built artifact
// or a raw source file.
@@ -21,16 +21,17 @@ export type Tree = {
};
// A record of one copy this manifest made: where under the tree root the copy
// landed, and the un-stamped filename it answers to.
// landed, the un-stamped filename it answers to, and the picture's size.
type Entry = {
dir: string,
stamped: string,
filename: string,
size: Size,
};
// Holds the unhashed name -> served url mapping published beside a stamped set.
// Each entry's url is the tree's url plus the copy's place in it, so no
// consumer needs a prefix of its own.
// Holds the unhashed name -> served url and size mapping published beside a
// stamped set. Each entry's url is the tree's url plus the copy's place in it,
// so no consumer needs a prefix of its own.
export class Manifest {
#ctx: DeployCtx;
#tree: Tree;
@@ -54,7 +55,7 @@ export class Manifest {
throw new Error(`duplicate sprite name ${name}`);
}
let stamped = `${name}-${h}.${e}`;
this.#entries.set(name, {dir, stamped, filename: `${name}.${e}`});
this.#entries.set(name, {dir, stamped, filename: `${name}.${e}`, size: await this.#ctx.size(f)});
this.#ctx.copy(f, nodePath.posix.join(this.#tree.root, dir, stamped));
}
@@ -69,9 +70,9 @@ export class Manifest {
}
write(dst: string): void {
let sorted: Record<string, string> = {};
for (let [k] of this.#sorted()) {
sorted[k] = this.url(k);
let sorted: Record<string, {url: string} & Size> = {};
for (let [k, e] of this.#sorted()) {
sorted[k] = {url: this.url(k), ...e.size};
}
this.#ctx.write(dst, JSON.stringify(sorted, null, 4) + '\n');
}

View File

@@ -36,8 +36,37 @@ export type DeployCtx = {
// of its bytes, byte-compatible with artifact hashes; for several, a digest
// of the sorted per-source digests (order-insensitive).
hash(...srcs: CopySource[]): Promise<string>,
size(src: CopySource): Promise<Size>,
};
export type Size = {width: number, height: number};
// Reads the size a browser lays a PNG, GIF or WebP out at from its header: a
// GIF's is its logical screen, not its first frame's.
export function imageSize(head: Buffer, what: string): Size {
let ascii = (start: number, end: number) => head.toString('latin1', start, end);
if (ascii(1, 4) === 'PNG') {
return {width: head.readUInt32BE(16), height: head.readUInt32BE(20)};
}
if (ascii(0, 3) === 'GIF') {
return {width: head.readUInt16LE(6), height: head.readUInt16LE(8)};
}
if (ascii(0, 4) === 'RIFF' && ascii(8, 12) === 'WEBP') {
switch (ascii(12, 16)) {
case 'VP8 ':
return {width: head.readUInt16LE(26) & 0x3fff, height: head.readUInt16LE(28) & 0x3fff};
case 'VP8L': {
let bits = head.readUInt32LE(21);
return {width: (bits & 0x3fff) + 1, height: ((bits >>> 14) & 0x3fff) + 1};
}
case 'VP8X':
return {width: head.readUIntLE(24, 3) + 1, height: head.readUIntLE(27, 3) + 1};
}
}
throw new Error(`${what} is not a PNG, GIF or WebP`);
}
export type DeployFn = (ctx: DeployCtx) => void | Promise<void>;
// Like rule(): a buildFile registers free-floating deploy blocks next to the
@@ -114,5 +143,11 @@ export function makeCtx(casDir: string, queue: ActionQueue): DeployCtx {
}
return shortHash(h.digest());
},
async size(src: CopySource): Promise<Size> {
let path = srcPath(src);
await using f = await fs.open(path);
let {buffer} = await f.read({buffer: Buffer.alloc(30), position: 0});
return imageSize(buffer, path);
},
};
}

View File

@@ -250,3 +250,20 @@ test('publish refuses links in the tree, links to what it does not carry, and __
reserved.write('a', '__meta/__files');
await assert.rejects(() => reserved.publish(tree, meta, 'd'), /publish's own/);
});
test('ctx.size reads each format the sets publish', async () => {
let ctx = makeCtx(pathlib.join(tmpdir(), 'cas'), new ActionQueue());
let src = pathlib.join(import.meta.dirname, 'src');
let sizes = await Promise.all(['32.png', '7x5.gif', '9x4-lossy.webp', '9x4-lossless.webp', '300x200-animated.webp']
.map(name => ctx.size(pathlib.join(src, name))));
assert.deepEqual(sizes, [
{width: 20, height: 26},
{width: 7, height: 5},
{width: 9, height: 4},
{width: 9, height: 4},
{width: 300, height: 200},
]);
let other = pathlib.join(tmpdir(), 'x.png');
fs.writeFileSync(other, '<svg xmlns="http://www.w3.org/2000/svg"/>');
await assert.rejects(() => ctx.size(other), /not a PNG, GIF or WebP/);
});

Binary file not shown.

After

Width:  |  Height:  |  Size: 156 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 B