// The loading screen, drawn locally. No video model and no image model ever draws it, so the label is // spelled exactly and the bar is the same on every run. // // It writes small transparent PNGs (a dark panel with an empty bar track, the label in three dot // states and a strip for the bar's fill) with a built-in 5 x 7 pixel font and Node's zlib. The bar's fill is animated by ffmpeg // (assemble-reveal.mjs). No npm packages, no system fonts, no drawtext filter needed. // // Used by assemble-reveal.mjs. Node 18 or newer. import { writeFileSync } from 'node:fs' import { join } from 'node:path' import { deflateSync } from 'node:zlib' // 5 columns x 7 rows, one string per row. Lower case is drawn as upper case. const FONT = { A: '01110 10001 10001 11111 10001 10001 10001', B: '11110 10001 10001 11110 10001 10001 11110', C: '01110 10001 10000 10000 10000 10001 01110', D: '11110 10001 10001 10001 10001 10001 11110', E: '11111 10000 10000 11110 10000 10000 11111', F: '11111 10000 10000 11110 10000 10000 10000', G: '01110 10001 10000 10111 10001 10001 01111', H: '10001 10001 10001 11111 10001 10001 10001', I: '01110 00100 00100 00100 00100 00100 01110', J: '00111 00010 00010 00010 00010 10010 01100', K: '10001 10010 10100 11000 10100 10010 10001', L: '10000 10000 10000 10000 10000 10000 11111', M: '10001 11011 10101 10101 10001 10001 10001', N: '10001 11001 10101 10011 10001 10001 10001', O: '01110 10001 10001 10001 10001 10001 01110', P: '11110 10001 10001 11110 10000 10000 10000', Q: '01110 10001 10001 10001 10101 10010 01101', R: '11110 10001 10001 11110 10100 10010 10001', S: '01111 10000 10000 01110 00001 00001 11110', T: '11111 00100 00100 00100 00100 00100 00100', U: '10001 10001 10001 10001 10001 10001 01110', V: '10001 10001 10001 10001 10001 01010 00100', W: '10001 10001 10001 10101 10101 10101 01010', X: '10001 10001 01010 00100 01010 10001 10001', Y: '10001 10001 01010 00100 00100 00100 00100', Z: '11111 00001 00010 00100 01000 10000 11111', 0: '01110 10001 10011 10101 11001 10001 01110', 1: '00100 01100 00100 00100 00100 00100 01110', 2: '01110 10001 00001 00010 00100 01000 11111', 3: '11110 00001 00001 01110 00001 00001 11110', 4: '00010 00110 01010 10010 11111 00010 00010', 5: '11111 10000 11110 00001 00001 10001 01110', 6: '00110 01000 10000 11110 10001 10001 01110', 7: '11111 00001 00010 00100 01000 01000 01000', 8: '01110 10001 10001 01110 10001 10001 01110', 9: '01110 10001 10001 01111 00001 00010 01100', '.': '00000 00000 00000 00000 00000 01100 01100', ',': '00000 00000 00000 00000 01100 00100 01000', '!': '00100 00100 00100 00100 00100 00000 00100', '?': '01110 10001 00001 00010 00100 00000 00100', '-': '00000 00000 00000 11111 00000 00000 00000', ':': '00000 01100 01100 00000 01100 01100 00000', '%': '11001 11010 00010 00100 01000 01011 10011', "'": '00100 00100 01000 00000 00000 00000 00000', ' ': '00000 00000 00000 00000 00000 00000 00000', } export const LABEL_CHARS = 'letters, numbers, space and . , ! ? - : % \'' const crcTable = new Int32Array(256).map((_, n) => { let c = n for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1 return c }) const crc32 = (buf) => { let c = -1 for (const b of buf) c = crcTable[(c ^ b) & 0xff] ^ (c >>> 8) return (c ^ -1) >>> 0 } const chunk = (type, data) => { const out = Buffer.alloc(12 + data.length) out.writeUInt32BE(data.length, 0) out.write(type, 4, 'ascii') data.copy(out, 8) out.writeUInt32BE(crc32(out.subarray(4, 8 + data.length)), 8 + data.length) return out } /** A transparent RGBA canvas that can be written as a PNG. */ class Canvas { constructor(width, height) { this.width = width this.height = height this.px = Buffer.alloc(width * height * 4) } /** Source-over blend of one pixel; alpha is 0 to 1. */ blend(x, y, [r, g, b], alpha) { if (x < 0 || y < 0 || x >= this.width || y >= this.height || alpha <= 0) return const i = (y * this.width + x) * 4 const a0 = this.px[i + 3] / 255 const a = alpha + a0 * (1 - alpha) for (const [k, v] of [r, g, b].entries()) this.px[i + k] = Math.round((v * alpha + this.px[i + k] * a0 * (1 - alpha)) / a) this.px[i + 3] = Math.round(a * 255) } /** A rounded rectangle with a 1 px soft edge. */ roundRect(x, y, w, h, radius, colour, alpha) { for (let j = 0; j < h; j++) { for (let i = 0; i < w; i++) { const dx = Math.max(radius - i - 0.5, i + 0.5 - (w - radius), 0) const dy = Math.max(radius - j - 0.5, j + 0.5 - (h - radius), 0) const cover = Math.min(1, Math.max(0, radius - Math.hypot(dx, dy) + 0.5)) this.blend(x + i, y + j, colour, alpha * cover) } } } rect(x, y, w, h, colour, alpha) { for (let j = 0; j < h; j++) for (let i = 0; i < w; i++) this.blend(x + i, y + j, colour, alpha) } png() { const raw = Buffer.alloc((this.width * 4 + 1) * this.height) for (let y = 0; y < this.height; y++) this.px.copy(raw, y * (this.width * 4 + 1) + 1, y * this.width * 4, (y + 1) * this.width * 4) const head = Buffer.alloc(13) head.writeUInt32BE(this.width, 0) head.writeUInt32BE(this.height, 4) head.set([8, 6, 0, 0, 0], 8) return Buffer.concat([Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), chunk('IHDR', head), chunk('IDAT', deflateSync(raw, { level: 9 })), chunk('IEND', Buffer.alloc(0))]) } } /** Checks the label and returns it in upper case. Throws a plain message the CLI can print. */ export function cleanLabel(text) { const label = String(text ?? '').trim().toUpperCase() if (!label) throw new Error('The label is empty.') if (label.length > 16) throw new Error(`The label "${text}" is ${label.length} characters; keep it to 16 or fewer.`) for (const ch of label) if (!FONT[ch]) throw new Error(`The label cannot hold "${ch}". The pixel font has ${LABEL_CHARS}.`) return label } const textWidth = (text, scale) => (text.length * 6 - 1) * scale function drawText(canvas, text, x, y, scale, colour, alpha = 1) { let cx = x for (const ch of text) { FONT[ch].split(' ').forEach((row, r) => { for (let c = 0; c < 5; c++) if (row[c] === '1') canvas.rect(cx + c * scale, y + r * scale, scale, scale, colour, alpha) }) cx += 6 * scale } } const hex = (value) => { const m = /^#?([0-9a-f]{6})$/i.exec(value ?? '') if (!m) throw new Error(`The colour "${value}" must be a hex code like #ffffff.`) return [0, 2, 4].map((i) => parseInt(m[1].slice(i, i + 2), 16)) } /** * Writes the loader's PNGs into dir and returns where everything sits, in pixels of the 720 x 1280 frame. * panel.png: dark rounded panel with the empty bar track. label-0.png to label-2.png: the label with * one, two and three dots, on a canvas of the same size so the letters never shift. */ export function writeLoader(dir, { width, height, label = 'loading', dots = true, centreY = 0.66, accent = '#ffffff' }) { const text = cleanLabel(label) const ink = hex(accent) const white = [255, 255, 255] const scale = Math.max(3, Math.round(width / 100)) // 7 px at 720 wide const full = dots ? `${text}...` : text const panelW = Math.min(width - 80, Math.max(Math.round(width * 0.78), textWidth(full, scale) + 8 * scale)) const panelH = Math.round(scale * 24) const panelX = Math.round((width - panelW) / 2) const panelY = Math.round(height * centreY - panelH / 2) const pad = Math.round(scale * 4.5) const panel = new Canvas(panelW, panelH) panel.roundRect(0, 0, panelW, panelH, scale * 3, [12, 12, 16], 0.62) const trackW = panelW - pad * 2 const trackH = scale * 4 const trackX = pad const trackY = panelH - pad - trackH const border = Math.max(2, Math.round(scale * 0.5)) panel.roundRect(trackX, trackY, trackW, trackH, scale, white, 0.9) panel.roundRect(trackX + border, trackY + border, trackW - border * 2, trackH - border * 2, Math.max(1, scale - border), [12, 12, 16], 1) const labelH = 7 * scale const labelW = textWidth(full, scale) const labelX = Math.round((panelW - labelW) / 2) const labelY = Math.round(trackY / 2 - labelH / 2 + pad / 4) const files = { panel: join(dir, 'panel.png'), labels: [] } writeFileSync(files.panel, panel.png()) for (const n of dots ? [1, 2, 3] : [0]) { const c = new Canvas(panelW, panelH) drawText(c, `${text}${'.'.repeat(n)}`, labelX, labelY, scale, white) const file = join(dir, `label-${n}.png`) writeFileSync(file, c.png()) files.labels.push(file) } // The fill is a strip twice as wide as the bar: the left half solid, the right half clear. ffmpeg slides a // window along it (crop with a time expression), so the visible fill grows from the left. const inner = { x: panelX + trackX + border * 2, y: panelY + trackY + border * 2, w: trackW - border * 4, h: trackH - border * 4 } const strip = new Canvas(inner.w * 2, inner.h) strip.roundRect(0, 0, inner.w, inner.h, Math.max(1, Math.floor(inner.h / 2)), ink, 1) files.strip = join(dir, 'fill-strip.png') writeFileSync(files.strip, strip.png()) return { ...files, panel_xy: [panelX, panelY], panel_size: [panelW, panelH], fill: inner, text: full, bottom: panelY + panelH } }