oficina / estilo Public

Clone
git clone https://amadan.net/oficina/estilo

Plain git — no account needed to clone.

Download

Download this file

1// fontflash.browser.mjs: look at the chrome text, frame after frame, from
2// navigation until long after every font and stylesheet has arrived, in
3// each engine. Driven by fonts_browser_test.go (go test -tags browser
4// ./theme), which serves the pages, slows the responses and makes every
5// assertion; this file only drives the browsers and reports what it saw.
6//
7// ESTILO_PLAYWRIGHT=<path to the playwright package> \
8// node fontflash.browser.mjs <job JSON>
9//
10// Job: {engines, clip, scenarios: [{name, url, watchMs}], refs: {name: url}}.
11// For each engine and scenario, in a fresh context (empty cache, page
12// scripts off, as on the door): a screenshot of `clip` on about:blank
13// (the "not painted yet" picture), then screenshots of the same clip as
14// fast as the engine allows from the moment navigation commits until
15// watchMs, then one "final" screenshot. Then each reference page,
16// settled. Every picture is reported as a hash of its PNG bytes: the
17// same pixels encode to the same bytes, so a hash IS a visual state.
18//
19// Pictures are taken with Playwright's wait for document.fonts.ready
20// turned off (PW_TEST_SCREENSHOT_NO_FONTS_READY): with it on, a
21// screenshot waits out exactly the invisible-text window this looks for.
22//
23// When ESTILO_FONTFLASH_OUT is set, every distinct picture is written
24// there as <engine>-<scenario>-<hash>.png, to look at.
25
26process.env.PW_TEST_SCREENSHOT_NO_FONTS_READY = '1'
27import { createHash } from 'node:crypto'
28import { createRequire } from 'node:module'
29import { writeFileSync, mkdirSync } from 'node:fs'
30import { inflateSync } from 'node:zlib'
31
32const require = createRequire(import.meta.url)
33const pw = require(process.env.ESTILO_PLAYWRIGHT || 'playwright')
34const job = JSON.parse(process.argv[2])
35const out = process.env.ESTILO_FONTFLASH_OUT
36if (out) mkdirSync(out, { recursive: true })
37
38const hash = (b) => createHash('sha256').update(b).digest('hex').slice(0, 12)
39
40// unpainted reports whether every pixel of an 8-bit RGB or RGBA PNG is
41// white or fully transparent: an engine's "nothing painted yet" is
42// white (about:blank's) or transparent (WebKit, before a new page's
43// first frame), encoded differently from one
44// capture to the next, so it is recognised by its pixels rather than
45// its bytes. The probe page is tinted, so the page painted with its text
46// invisible is a different, non-white picture: never "unpainted".
47function unpainted(png) {
48 let pos = 8, w = 0, h = 0, bpp = 0
49 const idat = []
50 while (pos < png.length) {
51 const len = png.readUInt32BE(pos), type = png.toString('ascii', pos + 4, pos + 8)
52 const data = png.subarray(pos + 8, pos + 8 + len)
53 if (type === 'IHDR') {
54 w = data.readUInt32BE(0); h = data.readUInt32BE(4)
55 if (data[8] !== 8 || (data[9] !== 2 && data[9] !== 6) || data[12] !== 0) throw new Error('unsupported PNG')
56 bpp = data[9] === 6 ? 4 : 3
57 } else if (type === 'IDAT') idat.push(data)
58 pos += 12 + len
59 }
60 const raw = inflateSync(Buffer.concat(idat)), stride = w * bpp
61 let prev = Buffer.alloc(stride)
62 for (let y = 0; y < h; y++) {
63 const f = raw[y * (stride + 1)], line = Buffer.from(raw.subarray(y * (stride + 1) + 1, (y + 1) * (stride + 1)))
64 for (let i = 0; i < stride; i++) {
65 const a = i >= bpp ? line[i - bpp] : 0, b = prev[i], c = i >= bpp ? prev[i - bpp] : 0
66 const p = a + b - c, pa = Math.abs(p - a), pb = Math.abs(p - b), pc = Math.abs(p - c)
67 line[i] = (line[i] + [0, a, b, (a + b) >> 1, pa <= pb && pa <= pc ? a : pb <= pc ? b : c][f]) & 255
68 }
69 for (let x = 0; x < stride; x += bpp) {
70 const px = line.subarray(x, x + bpp)
71 const clear = bpp === 4 && px[3] === 0
72 if (!clear && (px[0] !== 255 || px[1] !== 255 || px[2] !== 255)) return false
73 }
74 prev = line
75 }
76 return true
77}
78
79const results = []
80for (const engine of job.engines) {
81 let browser
82 try {
83 browser = await pw[engine].launch({ headless: true })
84 } catch (e) {
85 results.push({ engine, error: String(e.message || e).split('\n')[0] })
86 continue
87 }
88 const version = browser.version()
89 const shoot = async (page, keep) => {
90 const png = await page.screenshot({ clip: job.clip, timeout: 15000 })
91 const h = unpainted(png) ? 'blank' : hash(png)
92 if (out && keep) writeFileSync(`${out}/${engine}-${keep}-${h}.png`, png)
93 return h
94 }
95 const scenarios = []
96 for (const sc of job.scenarios) {
97 const context = await browser.newContext({ viewport: job.viewport, deviceScaleFactor: 1, javaScriptEnabled: false })
98 const page = await context.newPage()
99 await page.goto('about:blank')
100 await shoot(page, sc.name)
101 const blank = 'blank'
102 const samples = []
103 const started = Date.now()
104 await page.goto(sc.url, { waitUntil: 'commit' })
105 while (Date.now() - started < sc.watchMs) {
106 const t = Date.now() - started
107 // Mid-navigation, before the new page has a frame, an engine may
108 // refuse to capture at all: reported as "none", nothing painted.
109 samples.push({ t, h: await shoot(page, sc.name).catch(() => 'none') })
110 }
111 const final = await shoot(page, sc.name)
112 const refs = {}
113 for (const [name, url] of Object.entries(job.refs)) {
114 await page.goto(url, { waitUntil: 'load' })
115 await page.waitForTimeout(500)
116 refs[name] = await shoot(page, 'ref-' + name)
117 }
118 await context.close()
119 scenarios.push({ name: sc.name, blank, samples, final, refs })
120 }
121 await browser.close()
122 results.push({ engine, version, scenarios })
123}
124process.stdout.write(JSON.stringify(results))
125