|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off - build-time check run by build-libghostty-wasm.sh. |
| 2 | +/** |
| 3 | + * Diffs ghostty-sprite.wasm against Ghostty's own sprite golden images |
| 4 | + * (src/font/sprite/testdata at the pinned revision). Each PNG is a 16x16 grid |
| 5 | + * of padded cells for one 256-codepoint page at one metric set; the wasm must |
| 6 | + * reproduce every page pixel for pixel. |
| 7 | + * |
| 8 | + * Usage: node check-ghostty-sprite-goldens.ts <ghostty-sprite.wasm> <testdata-dir> |
| 9 | + */ |
| 10 | +import * as NodeFS from "node:fs"; |
| 11 | +import * as NodePath from "node:path"; |
| 12 | +import * as NodeZlib from "node:zlib"; |
| 13 | + |
| 14 | +interface SpriteExports { |
| 15 | + readonly memory: WebAssembly.Memory; |
| 16 | + readonly t3_sprite_has: (codepoint: number) => number; |
| 17 | + readonly t3_sprite_render: ( |
| 18 | + codepoint: number, |
| 19 | + cellWidth: number, |
| 20 | + cellHeight: number, |
| 21 | + thickness: number, |
| 22 | + span: number, |
| 23 | + ) => number; |
| 24 | +} |
| 25 | + |
| 26 | +// Metric sets from Ghostty's "sprite face render all sprites" test, keyed by |
| 27 | +// the golden file suffix: cell width, ascent, descent, box thickness. |
| 28 | +const METRIC_SETS: Record<string, readonly [number, number, number, number]> = { |
| 29 | + "18x36+4": [18, 30, 6, 4], |
| 30 | + "12x24+3": [12, 20, 4, 3], |
| 31 | + "11x21+2": [11, 19, 2, 2], |
| 32 | + "9x17+1": [9, 15, 2, 1], |
| 33 | +}; |
| 34 | + |
| 35 | +function paeth(a: number, b: number, c: number): number { |
| 36 | + const p = a + b - c; |
| 37 | + const pa = Math.abs(p - a); |
| 38 | + const pb = Math.abs(p - b); |
| 39 | + const pc = Math.abs(p - c); |
| 40 | + return pa <= pb && pa <= pc ? a : pb <= pc ? b : c; |
| 41 | +} |
| 42 | + |
| 43 | +// Decodes the only PNG shape z2d writes for alpha8 surfaces: 8-bit grayscale, |
| 44 | +// non-interlaced. |
| 45 | +function decodeGrayPng(buffer: Buffer): { width: number; height: number; pixels: Uint8Array } { |
| 46 | + const width = buffer.readUInt32BE(16); |
| 47 | + const height = buffer.readUInt32BE(20); |
| 48 | + if (buffer[24] !== 8 || buffer[25] !== 0 || buffer[28] !== 0) { |
| 49 | + throw new Error("expected an 8-bit grayscale, non-interlaced PNG"); |
| 50 | + } |
| 51 | + const chunks: Buffer[] = []; |
| 52 | + for (let offset = 8; offset < buffer.length;) { |
| 53 | + const length = buffer.readUInt32BE(offset); |
| 54 | + if (buffer.toString("ascii", offset + 4, offset + 8) === "IDAT") { |
| 55 | + chunks.push(buffer.subarray(offset + 8, offset + 8 + length)); |
| 56 | + } |
| 57 | + offset += 12 + length; |
| 58 | + } |
| 59 | + const raw = NodeZlib.inflateSync(Buffer.concat(chunks)); |
| 60 | + const pixels = new Uint8Array(width * height); |
| 61 | + for (let y = 0; y < height; y += 1) { |
| 62 | + const filter = raw[y * (width + 1)]; |
| 63 | + for (let x = 0; x < width; x += 1) { |
| 64 | + const a = x > 0 ? pixels[y * width + x - 1]! : 0; |
| 65 | + const b = y > 0 ? pixels[(y - 1) * width + x]! : 0; |
| 66 | + const c = x > 0 && y > 0 ? pixels[(y - 1) * width + x - 1]! : 0; |
| 67 | + const predictor = |
| 68 | + filter === 1 |
| 69 | + ? a |
| 70 | + : filter === 2 |
| 71 | + ? b |
| 72 | + : filter === 3 |
| 73 | + ? (a + b) >> 1 |
| 74 | + : filter === 4 |
| 75 | + ? paeth(a, b, c) |
| 76 | + : 0; |
| 77 | + pixels[y * width + x] = (raw[y * (width + 1) + 1 + x]! + predictor) & 0xff; |
| 78 | + } |
| 79 | + } |
| 80 | + return { width, height, pixels }; |
| 81 | +} |
| 82 | + |
| 83 | +const [wasmPath, testdataDir] = process.argv.slice(2); |
| 84 | +if (!wasmPath || !testdataDir) { |
| 85 | + console.error("usage: node check-ghostty-sprite-goldens.ts <ghostty-sprite.wasm> <testdata-dir>"); |
| 86 | + process.exit(2); |
| 87 | +} |
| 88 | + |
| 89 | +const { instance } = await WebAssembly.instantiate(NodeFS.readFileSync(wasmPath)); |
| 90 | +const sprite = instance.exports as unknown as SpriteExports; |
| 91 | + |
| 92 | +let failedPages = 0; |
| 93 | +let renderedGlyphs = 0; |
| 94 | +const pages = NodeFS.readdirSync(testdataDir) |
| 95 | + .filter((file) => file.endsWith(".png")) |
| 96 | + .toSorted(); |
| 97 | +for (const file of pages) { |
| 98 | + const match = /^U\+([0-9A-F]+)\.\.\.U\+[0-9A-F]+-(\d+x\d+\+\d+)\.png$/.exec(file); |
| 99 | + const metrics = match ? METRIC_SETS[match[2]!] : undefined; |
| 100 | + if (!match || !metrics) throw new Error(`unrecognized golden file: ${file}`); |
| 101 | + const [cellWidth, ascent, descent, thickness] = metrics; |
| 102 | + const cellHeight = ascent + descent; |
| 103 | + const golden = decodeGrayPng(NodeFS.readFileSync(NodePath.join(testdataDir, file))); |
| 104 | + const paddedWidth = cellWidth + 2 * Math.floor(cellWidth / 4); |
| 105 | + const paddedHeight = cellHeight + 2 * Math.floor(cellHeight / 4); |
| 106 | + const actual = new Uint8Array(golden.width * golden.height); |
| 107 | + const failedGlyphs: string[] = []; |
| 108 | + for (let index = 0; index < 256; index += 1) { |
| 109 | + const codepoint = Number.parseInt(match[1]!, 16) + index; |
| 110 | + if (!sprite.t3_sprite_has(codepoint)) continue; |
| 111 | + const pointer = sprite.t3_sprite_render(codepoint, cellWidth, cellHeight, thickness, 1); |
| 112 | + if (pointer === 0) { |
| 113 | + failedGlyphs.push(`${codepoint.toString(16)}(render failed)`); |
| 114 | + continue; |
| 115 | + } |
| 116 | + renderedGlyphs += 1; |
| 117 | + const cell = new Uint8Array(sprite.memory.buffer, pointer, paddedWidth * paddedHeight); |
| 118 | + const originX = (index % 16) * paddedWidth; |
| 119 | + const originY = Math.floor(index / 16) * paddedHeight; |
| 120 | + for (let y = 0; y < paddedHeight; y += 1) { |
| 121 | + actual.set( |
| 122 | + cell.subarray(y * paddedWidth, (y + 1) * paddedWidth), |
| 123 | + (originY + y) * golden.width + originX, |
| 124 | + ); |
| 125 | + } |
| 126 | + } |
| 127 | + // Compare whole pages so golden pixels in cells we left blank also fail. |
| 128 | + let differingPixels = 0; |
| 129 | + for (let index = 0; index < actual.length; index += 1) { |
| 130 | + if (actual[index] !== golden.pixels[index]) differingPixels += 1; |
| 131 | + } |
| 132 | + const ok = failedGlyphs.length === 0 && differingPixels === 0; |
| 133 | + if (!ok) failedPages += 1; |
| 134 | + console.log( |
| 135 | + `${ok ? "ok " : "FAIL"} ${file}${differingPixels ? ` differing-px=${differingPixels}` : ""}${failedGlyphs.length ? ` ${failedGlyphs.join(",")}` : ""}`, |
| 136 | + ); |
| 137 | +} |
| 138 | + |
| 139 | +console.log( |
| 140 | + `[sprite-goldens] pages=${pages.length} failed=${failedPages} glyphs=${renderedGlyphs}`, |
| 141 | +); |
| 142 | +if (pages.length === 0 || failedPages > 0) process.exit(1); |
0 commit comments