// fontflash.browser.mjs: look at the chrome text, frame after frame, from // navigation until long after every font and stylesheet has arrived, in // each engine. Driven by fonts_browser_test.go (go test -tags browser // ./theme), which serves the pages, slows the responses and makes every // assertion; this file only drives the browsers and reports what it saw. // // ESTILO_PLAYWRIGHT= \ // node fontflash.browser.mjs // // Job: {engines, clip, scenarios: [{name, url, watchMs}], refs: {name: url}}. // For each engine and scenario, in a fresh context (empty cache, page // scripts off, as on the door): a screenshot of `clip` on about:blank // (the "not painted yet" picture), then screenshots of the same clip as // fast as the engine allows from the moment navigation commits until // watchMs, then one "final" screenshot. Then each reference page, // settled. Every picture is reported as a hash of its PNG bytes: the // same pixels encode to the same bytes, so a hash IS a visual state. // // Pictures are taken with Playwright's wait for document.fonts.ready // turned off (PW_TEST_SCREENSHOT_NO_FONTS_READY): with it on, a // screenshot waits out exactly the invisible-text window this looks for. // // When ESTILO_FONTFLASH_OUT is set, every distinct picture is written // there as --.png, to look at. process.env.PW_TEST_SCREENSHOT_NO_FONTS_READY = '1' import { createHash } from 'node:crypto' import { createRequire } from 'node:module' import { writeFileSync, mkdirSync } from 'node:fs' import { inflateSync } from 'node:zlib' const require = createRequire(import.meta.url) const pw = require(process.env.ESTILO_PLAYWRIGHT || 'playwright') const job = JSON.parse(process.argv[2]) const out = process.env.ESTILO_FONTFLASH_OUT if (out) mkdirSync(out, { recursive: true }) const hash = (b) => createHash('sha256').update(b).digest('hex').slice(0, 12) // unpainted reports whether every pixel of an 8-bit RGB or RGBA PNG is // white or fully transparent: an engine's "nothing painted yet" is // white (about:blank's) or transparent (WebKit, before a new page's // first frame), encoded differently from one // capture to the next, so it is recognised by its pixels rather than // its bytes. The probe page is tinted, so the page painted with its text // invisible is a different, non-white picture: never "unpainted". function unpainted(png) { let pos = 8, w = 0, h = 0, bpp = 0 const idat = [] while (pos < png.length) { const len = png.readUInt32BE(pos), type = png.toString('ascii', pos + 4, pos + 8) const data = png.subarray(pos + 8, pos + 8 + len) if (type === 'IHDR') { w = data.readUInt32BE(0); h = data.readUInt32BE(4) if (data[8] !== 8 || (data[9] !== 2 && data[9] !== 6) || data[12] !== 0) throw new Error('unsupported PNG') bpp = data[9] === 6 ? 4 : 3 } else if (type === 'IDAT') idat.push(data) pos += 12 + len } const raw = inflateSync(Buffer.concat(idat)), stride = w * bpp let prev = Buffer.alloc(stride) for (let y = 0; y < h; y++) { const f = raw[y * (stride + 1)], line = Buffer.from(raw.subarray(y * (stride + 1) + 1, (y + 1) * (stride + 1))) for (let i = 0; i < stride; i++) { const a = i >= bpp ? line[i - bpp] : 0, b = prev[i], c = i >= bpp ? prev[i - bpp] : 0 const p = a + b - c, pa = Math.abs(p - a), pb = Math.abs(p - b), pc = Math.abs(p - c) line[i] = (line[i] + [0, a, b, (a + b) >> 1, pa <= pb && pa <= pc ? a : pb <= pc ? b : c][f]) & 255 } for (let x = 0; x < stride; x += bpp) { const px = line.subarray(x, x + bpp) const clear = bpp === 4 && px[3] === 0 if (!clear && (px[0] !== 255 || px[1] !== 255 || px[2] !== 255)) return false } prev = line } return true } const results = [] for (const engine of job.engines) { let browser try { browser = await pw[engine].launch({ headless: true }) } catch (e) { results.push({ engine, error: String(e.message || e).split('\n')[0] }) continue } const version = browser.version() const shoot = async (page, keep) => { const png = await page.screenshot({ clip: job.clip, timeout: 15000 }) const h = unpainted(png) ? 'blank' : hash(png) if (out && keep) writeFileSync(`${out}/${engine}-${keep}-${h}.png`, png) return h } const scenarios = [] for (const sc of job.scenarios) { const context = await browser.newContext({ viewport: job.viewport, deviceScaleFactor: 1, javaScriptEnabled: false }) const page = await context.newPage() await page.goto('about:blank') await shoot(page, sc.name) const blank = 'blank' const samples = [] const started = Date.now() await page.goto(sc.url, { waitUntil: 'commit' }) while (Date.now() - started < sc.watchMs) { const t = Date.now() - started // Mid-navigation, before the new page has a frame, an engine may // refuse to capture at all: reported as "none", nothing painted. samples.push({ t, h: await shoot(page, sc.name).catch(() => 'none') }) } const final = await shoot(page, sc.name) const refs = {} for (const [name, url] of Object.entries(job.refs)) { await page.goto(url, { waitUntil: 'load' }) await page.waitForTimeout(500) refs[name] = await shoot(page, 'ref-' + name) } await context.close() scenarios.push({ name: sc.name, blank, samples, final, refs }) } await browser.close() results.push({ engine, version, scenarios }) } process.stdout.write(JSON.stringify(results))