import { spawnSync } from 'node:child_process'
import { mkdirSync, readFileSync } from 'node:fs'
import path from 'node:path'
import { decodePng } from './bench/png'
const usage = 'bun tools/shot.ts <file.slint> <component> <out.png> [--size WxH] [--scale 1.25] [--data state.json]'
const [file, component, output, ...options] = process.argv.slice(2)
if (!file || !component || !output) throw new Error(usage)
if (path.extname(output).toLowerCase() !== '.png') throw new Error('Output must be a .png file')
let size: [number, number] | undefined
let scale = 1
let data: string | undefined
for (let i = 0; i < options.length; i += 2) {
const flag = options[i]
const value = options[i + 1]
if (!value) throw new Error(usage)
if (flag === '--size') {
const match = /^(\d+(?:\.\d+)?)x(\d+(?:\.\d+)?)$/i.exec(value)
if (!match) throw new Error('Size must be WIDTHxHEIGHT')
size = [Number(match[1]), Number(match[2])]
if (size.some(n => !Number.isFinite(n) || n <= 0)) throw new Error('Size must be positive and finite')
} else if (flag === '--scale') {
scale = Number(value)
if (!Number.isFinite(scale) || scale <= 0) throw new Error('Scale must be positive and finite')
} else if (flag === '--data') {
data = path.resolve(value)
JSON.parse(readFileSync(data, 'utf8'))
} else throw new Error(`Unknown option ${flag}\n${usage}`)
}
let expected = size?.map(n => Math.round(n * scale))
if (expected?.some(n => n < 1)) throw new Error('Scaled size must be at least one physical pixel')
const viewer = process.env.SLINT_VIEWER ?? ''
if (!viewer) throw new Error('Set SLINT_VIEWER to a software-renderer slint-viewer executable')
mkdirSync(path.dirname(path.resolve(output)), { recursive: true })
// Do not pass --backend: the viewer then installs its windowless platform.
// Supply a viewer built without Skia to use Slint software.
console.warn('Headless renderer depends on SLINT_VIEWER build features; see docs/development.md')
function capture(captureScale: number, physicalSize?: number[]) {
const args = ['--screenshot', path.resolve(output!), '--component', component!]
// Allocate the final partial physical pixel; never resize the rendered PNG.
if (physicalSize) args.push('--size', physicalSize.map(n => n / captureScale).join('x'))
if (data) args.push('--load-data', data)
args.push(path.resolve(file!))
const result = spawnSync(viewer, args, {
env: { ...process.env, SLINT_SCALE_FACTOR: String(captureScale) },
encoding: 'utf8',
timeout: 60000,
windowsHide: true,
})
if (result.error) throw result.error
if (result.status !== 0) throw new Error(`slint-viewer exited ${result.status}: ${result.stderr || result.stdout}`)
if (result.stderr) console.error(result.stderr.trim())
return decodePng(readFileSync(output!))
}
// Discover preferred logical dimensions at 1x before fractional-scale capture.
if (!size && scale !== 1) {
const preferred = capture(1)
expected = [preferred.width, preferred.height].map(n => Math.round(n * scale))
}
const image = capture(scale, expected)
if (expected && (image.width !== expected[0] || image.height !== expected[1])) {
throw new Error(`PNG is ${image.width}x${image.height}; expected ${expected.join('x')}`)
}
console.log(`${output}: ${image.width}x${image.height} (scale ${scale})`)