/**
* Slint widget bench (M0.1 S3): renders every widget state from bench/slint/fixtures/*.slint with tools/shot.ts
* (software-only viewer, scale 1), crops the 100 % game truth from bench/slint/refs-100.json, scores with
* tools/bench/diff.ts and writes out/bench-slint/{<id>.side6.png, report.md, contact.png}.
*
* bun tools/bench-slint.ts [id-or-prefix …] [--quiet]
*
* A crop entry in refs-100.json: { image, rect, masks?, bg, fixture?, component?, note?, eye? }.
* - masks: text ink boxes [x, y, w, h, bg?] (image-absolute). They are widened by MASK_PAD_X/Y before surface scoring
* and judged separately by ink box (±1 px), because Slint's glyph advances drift on long lines.
* - eye: id of a 125 % crop in bench/refs.json. The state has no 100 % truth, so it is rendered and shown next to
* that crop, unscored ("eye-judged").
*/
import { spawn } from 'node:child_process'
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import path from 'node:path'
import { compare, diffImage, sideBySide, textDelta, verdict, type Rect, type TextDelta, type Verdict } from './bench/diff'
import { crop, decodePng, encodePng, zoom, type Image } from './bench/png'
import { loadRefs, resolveImagePath, type Refs } from './bench/refs'
import { styleInstall } from './dump-styles'
const ROOT = path.resolve(import.meta.dirname, '..')
const OUT = path.join(ROOT, 'out/bench-slint')
const MASK_PAD_X = 8
const MASK_PAD_Y = 1
export interface Entry {
image?: string
rect: Rect
masks?: (number[])[]
/** Non-text content outside the widget contract, e.g. mod-specific artwork. */
surfaceMasks?: Rect[]
bg?: string
fixture?: string
component?: string
note?: string
eye?: string
/** Property values for the fixture component (shot.ts --data), e.g. crop-relative offsets. */
data?: Record<string, unknown>
/** Fixture draws the whole screenshot layout at image coordinates and takes `ox`/`oy` (crop origin) from data; this sets ox/oy to rect x/y when data omits them. */
scene?: boolean
group?: string
/** Narrow padding where an adjacent icon must not count as text ink. */
maskPadding?: [number, number]
}
export interface Result {
id: string
/** Crop-relative position of the largest unmasked difference. */
worst?: string
tier: Verdict | 'EYE' | 'MISSING'
maxDelta: number
pct: number
text: TextDelta[]
note: string
}
export function componentName(id: string): string {
return id.split(/[.\-]/).map(p => p.charAt(0).toUpperCase() + p.slice(1)).join('')
}
export function defaultFixture(id: string): string {
return `bench/slint/fixtures/${id.split('.')[0]}.slint`
}
export function paddedMasks(masks: Rect[], width: number, height: number, padX = MASK_PAD_X, padY = MASK_PAD_Y): Rect[] {
return masks.map(([x, y, w, h]) => {
const x0 = Math.max(0, x - padX), y0 = Math.max(0, y - padY)
const x1 = Math.min(width, x + w + padX), y1 = Math.min(height, y + h + padY)
return [x0, y0, x1 - x0, y1 - y0] as Rect
})
}
function run(cmd: string, args: string[]): Promise<{ code: number, out: string }> {
return new Promise(resolve => {
const child = spawn(cmd, args, { cwd: ROOT, windowsHide: true })
let out = ''
child.stdout.on('data', d => { out += d })
child.stderr.on('data', d => { out += d })
child.on('close', code => resolve({ code: code ?? 1, out }))
})
}
async function pool<T>(items: T[], size: number, fn: (item: T) => Promise<void>): Promise<void> {
let next = 0
await Promise.all(Array.from({ length: Math.min(size, items.length) }, async () => {
while (next < items.length) await fn(items[next++]!)
}))
}
const images = new Map<string, Image>()
function load(file: string): Image {
let img = images.get(file)
if (!img) { img = decodePng(readFileSync(file)); images.set(file, img) }
return img
}
/** Brute-force shift (dx, dy of ours relative to truth, ±6 px) with the smallest mean |Δ|; a placement aid, not a score. */
function bestShift(truth: Image, ours: Image): string {
let best = '', bestErr = Infinity
for (let dy = -6; dy <= 6; dy++) for (let dx = -6; dx <= 6; dx++) {
let err = 0, n = 0
for (let y = 8; y < truth.height - 8; y++) for (let x = 8; x < truth.width - 8; x += 2) {
const sx = x - dx, sy = y - dy
if (sx < 0 || sy < 0 || sx >= ours.width || sy >= ours.height) continue
const i = (y * truth.width + x) * 4, j = (sy * ours.width + sx) * 4
err += Math.abs(truth.data[i]! - ours.data[j]!) + Math.abs(truth.data[i + 1]! - ours.data[j + 1]!) + Math.abs(truth.data[i + 2]! - ours.data[j + 2]!)
n++
}
if (n && err / n < bestErr) { bestErr = err / n; best = `(${dx},${dy}) err ${(err / n).toFixed(2)}` }
}
return best
}
function label(text: string): string { return text.replace(/\|/g, '/') }
if (import.meta.main) {
const args = process.argv.slice(2).filter(a => !a.startsWith('--'))
const refs = JSON.parse(readFileSync(path.join(ROOT, 'bench/slint/refs-100.json'), 'utf8')) as { images: Record<string, string>, crops: Record<string, Entry> }
const refs125: Refs = loadRefs(path.join(ROOT, 'bench/refs.json'))
const ids = Object.keys(refs.crops).filter(id => !args.length || args.some(a => id === a || id.startsWith(a)))
mkdirSync(OUT, { recursive: true })
mkdirSync(path.join(OUT, 'shots'), { recursive: true })
const results: Result[] = []
const tiles: { id: string, truth: Image, ours: Image }[] = []
await pool(ids, 6, async id => {
const e = refs.crops[id]!
const [, , w, h] = e.rect
const file = e.fixture ?? defaultFixture(id)
const comp = e.component ?? componentName(id)
const shot = path.join(OUT, 'shots', `${id}.png`)
if (!existsSync(path.join(ROOT, file))) { results.push({ id, tier: 'MISSING', maxDelta: 0, pct: 0, text: [], note: `no fixture ${file}` }); return }
const dataArgs: string[] = []
if (e.data || e.scene) { const dp = path.join(OUT, 'shots', `${id}.json`); writeFileSync(dp, JSON.stringify(e.scene ? { ox: e.rect[0], oy: e.rect[1], ...e.data } : e.data)); dataArgs.push('--data', dp) }
const r = await run(process.execPath, ['tools/shot.ts', file, comp, shot, '--size', `${w}x${h}`, '--scale', '1', ...dataArgs])
if (r.code !== 0) { results.push({ id, tier: 'MISSING', maxDelta: 0, pct: 0, text: [], note: `render failed: ${r.out.split('\n').filter(Boolean).slice(-2).join(' ').slice(0, 160)}` }); return }
const ours = load(shot)
images.delete(shot)
if (e.eye !== undefined) {
const t = e.eye ? refs125.crops[e.eye] : undefined
const truth125 = t ? crop(load(resolveImagePath(refs125, t.image, ROOT, styleInstall())), ...t.rect) : undefined
writeFileSync(path.join(OUT, `${id}.side6.png`), encodePng(zoom(sideBySide(truth125 ? [truth125, ours] : [ours], 2), 6)))
results.push({ id, tier: 'EYE', maxDelta: 0, pct: 0, text: [], note: e.note ?? '' })
tiles.push({ id, truth: truth125 ?? ours, ours })
return
}
const truthImg = load(path.join(ROOT, refs.images[e.image!]!))
const truth = crop(truthImg, ...e.rect)
const masks = (e.masks ?? []).map(([mx, my, mw, mh]) => [mx! - e.rect[0], my! - e.rect[1], mw!, mh!] as Rect)
const wide = paddedMasks(masks, w, h, ...(e.maskPadding ?? [MASK_PAD_X, MASK_PAD_Y] as [number, number]))
const surfaceMasks = (e.surfaceMasks ?? []).map(([mx, my, mw, mh]) => [mx - e.rect[0], my - e.rect[1], mw, mh] as Rect)
const allMasks = [...wide, ...surfaceMasks]
const m = compare(truth, ours, allMasks)
const text: TextDelta[] = masks.map((mk, i) => textDelta(truth, ours, wide[i]!, (e.masks![i]![4] as unknown as string | undefined) ?? e.bg ?? '#000000'))
const tier = verdict(m.maxDelta, m.over2, m.pixels, text.every(t => t.ok))
let worst = '', worstD = -1
for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) {
if (allMasks.some(([mx, my, mw, mh]) => x >= mx && x < mx + mw && y >= my && y < my + mh)) continue
const i = (y * w + x) * 4
const d = Math.max(Math.abs(truth.data[i]! - ours.data[i]!), Math.abs(truth.data[i + 1]! - ours.data[i + 1]!), Math.abs(truth.data[i + 2]! - ours.data[i + 2]!), Math.abs(truth.data[i + 3]! - ours.data[i + 3]!))
if (d > worstD) { worstD = d; worst = `${x},${y}` }
}
const diff = diffImage(truth, ours, allMasks)
if (process.argv.includes('--align')) console.log(`${id} best shift ${bestShift(truth, ours)}`)
writeFileSync(path.join(OUT, `${id}.side6.png`), encodePng(zoom(sideBySide([truth, ours, diff], 2), 6)))
results.push({ id, tier, worst, maxDelta: m.maxDelta, pct: m.pixels ? 100 * m.over2 / m.pixels : 0, text, note: e.note ?? '' })
tiles.push({ id, truth, ours })
})
results.sort((a, b) => a.id.localeCompare(b.id, 'en', { numeric: true }))
const count = (t: string) => results.filter(r => r.tier === t).length
const lines = [
'# Slint widget bench (S3)',
'',
`PASS ${count('PASS')} · NEAR ${count('NEAR')} · FAIL ${count('FAIL')} · eye-judged ${count('EYE')} · missing ${count('MISSING')}`,
'',
'Tiers: PASS max Δ ≤ 2; NEAR max Δ ≤ 8 and ≤ 2 % of pixels over 2; FAIL otherwise or any text ink edge off by more than 1 px. Surface score excludes text masks (ink boxes widened by 8 px horizontally, 1 px vertically). Text ink Δ = ours − truth edges (left, top, right, bottom), px.',
'',
'| id | state | max Δ | % >2 | tier | text ink Δ (l,t,r,b) | what still differs and why |',
'|---|---|---|---|---|---|---|',
]
for (const r of results) {
const text = r.text.map(t => t.edges ? `(${t.edges.join(',')})` : t.ok ? '—' : 'no ink').join(' ') || '—'
const state = r.id.split('.').slice(1).join('.')
lines.push(`| ${r.id} | ${state} | ${r.tier === 'EYE' ? '—' : r.maxDelta} | ${r.tier === 'EYE' ? '—' : r.pct.toFixed(2)} | ${r.tier} | ${text} | ${label(r.note)} |`)
}
writeFileSync(path.join(OUT, 'report.md'), lines.join('\n') + '\n')
writeFileSync(path.join(OUT, 'results.json'), JSON.stringify(results, null, 2) + '\n')
// Contact sheet: truth over ours for every tile, 3x, wrapped at ~2600 px.
const Z = 3, PAD = 4, MAXW = 2600
const cells = tiles.sort((a, b) => a.id.localeCompare(b.id, 'en', { numeric: true })).map(t => {
const w = Math.max(t.truth.width, t.ours.width)
return { w: w * Z + PAD, img: sideBySide([zoom(t.truth, Z), zoom(t.ours, Z)], 2, '#00ffff') , id: t.id }
})
const rows: typeof cells[] = [[]]
let rowW = 0
for (const c of cells) {
if (rowW + c.img.width + PAD > MAXW && rows.at(-1)!.length) { rows.push([]); rowW = 0 }
rows.at(-1)!.push(c); rowW += c.img.width + PAD
}
const sheetW = Math.max(...rows.map(r => r.reduce((n, c) => n + c.img.width + PAD, 0)), 1)
const rowH = rows.map(r => Math.max(0, ...r.map(c => c.img.height)) + PAD)
const sheetH = rowH.reduce((a, b) => a + b, 0) || 1
const sheet: Image = { width: sheetW, height: sheetH, data: new Uint8Array(sheetW * sheetH * 4).fill(0) }
for (let i = 0; i < sheetW * sheetH; i++) { sheet.data[i * 4] = 40; sheet.data[i * 4 + 1] = 0; sheet.data[i * 4 + 2] = 40; sheet.data[i * 4 + 3] = 255 }
let oy = 0
rows.forEach((r, ri) => {
let ox = 0
for (const c of r) {
for (let y = 0; y < c.img.height; y++) sheet.data.set(c.img.data.subarray(y * c.img.width * 4, (y + 1) * c.img.width * 4), ((oy + y) * sheetW + ox) * 4)
ox += c.img.width + PAD
}
oy += rowH[ri]!
})
writeFileSync(path.join(OUT, 'contact.png'), encodePng(sheet))
for (const r of results) {
const text = r.text.map(t => t.edges ? `(${t.edges.join(',')})` : '').join(' ')
if (!process.argv.includes('--quiet') || r.tier === 'FAIL' || r.tier === 'MISSING') console.log(`${r.tier.padEnd(7)} ${r.id.padEnd(34)} Δ${String(r.maxDelta).padStart(3)} ${r.pct.toFixed(2).padStart(6)}% ${text} ${r.tier === 'MISSING' ? r.note : r.tier === 'PASS' ? '' : `worst@${r.worst}`}`)
}
console.log(`PASS ${count('PASS')} NEAR ${count('NEAR')} FAIL ${count('FAIL')} EYE ${count('EYE')} MISSING ${count('MISSING')} -> ${path.relative(ROOT, OUT)}/report.md`)
if (count('MISSING')) process.exitCode = 1
}