// THROWAWAY: pixel proof of composed views, old (958228a) vs new, three engines; metrics in page. import { createRequire } from 'node:module' import { readFileSync, readdirSync } from 'node:fs' const require = createRequire(process.argv[2] + '/apps/app/package.json') const pw = require('@playwright/test') const DIR = process.argv[3] const engines = process.argv[4].split(',') const variants = [...new Set(readdirSync(DIR + '/new').map((f) => f.split('.')[0]))].sort() const read = (side, v, form, part) => readFileSync(`${DIR}/${side}/${v}.${form}.${part}.svg`, 'utf8') const LIB = ` window.store = {} window.keep = async (key, img) => { const c = document.createElement('canvas'); c.width = img.width; c.height = img.height; const x = c.getContext('2d'); x.fillStyle='#fff'; x.fillRect(0,0,c.width,c.height); x.drawImage(img, 0, 0, c.width, c.height); window.store[key] = x.getImageData(0, 0, c.width, c.height).data } window.pdf = async (key, svg) => { const root = new DOMParser().parseFromString(svg, 'image/svg+xml').documentElement; const vb = root.getAttribute('viewBox').split(/[\\s,]+/).map(Number); const w = 1200, h = Math.round(1200 * vb[3] / vb[2]); root.setAttribute('width', String(w)); root.setAttribute('height', String(h)); const url = URL.createObjectURL(new Blob([new XMLSerializer().serializeToString(root)], { type: 'image/svg+xml;charset=utf-8' })); const img = new Image(); img.width = w; img.height = h; img.src = url; await img.decode(); await window.keep(key, img) } window.png = async (key, b64) => { const img = new Image(); img.src = 'data:image/png;base64,' + b64; await img.decode(); await window.keep(key, img) } window.diff = (a, b) => { a = window.store[a]; b = window.store[b]; let n = 0; for (let i = 0; i < a.length; i += 4) if (a[i] !== b[i] || a[i+1] !== b[i+1] || a[i+2] !== b[i+2]) n++; return n } window.ink = (a) => { a = window.store[a]; let n = 0; for (let i = 0; i < a.length; i += 4) if (a[i] < 250 || a[i+1] < 250 || a[i+2] < 250) n++; return n } window.hl = (a) => { a = window.store[a]; let n = 0; for (let i = 0; i < a.length; i += 4) if (Math.abs(a[i]-0xb8) <= 24 && Math.abs(a[i+1]-0x5a) <= 24 && Math.abs(a[i+2]-0x3f) <= 24) n++; return n } ` for (const name of engines) { const browser = await pw[name].launch() const work = await browser.newPage() const shot = await browser.newPage({ viewport: { width: 1100, height: 900 } }) await work.setContent(''); await work.addScriptTag({ content: LIB }) console.log(`== ${name} ${browser.version()}`) const load = async (key, form, svg) => { if (form === 'pdf') return work.evaluate(([k, s]) => window.pdf(k, s), [key, svg]) await shot.setContent(`
${svg.replace(/^`) const b64 = (await shot.locator('#h').screenshot()).toString('base64') return work.evaluate(([k, b]) => window.png(k, b), [key, b64]) } for (const form of ['pdf', 'canvas']) { await load('base', form, read('new', 'plain', form, 'layer')) for (const v of variants) { for (const side of ['old', 'new']) for (const part of ['full', 'layer']) await load(`${side}.${part}`, form, read(side, v, form, part)) const row = await work.evaluate(() => ({ full_old_vs_new: window.diff('old.full', 'new.full'), markers_old_vs_plain: window.diff('old.layer', 'base'), markers_new_vs_plain: window.diff('new.layer', 'base'), marker_ink_old: window.ink('old.layer'), marker_ink_new: window.ink('new.layer'), highlight_old: window.hl('old.full'), highlight_new: window.hl('new.full'), })) console.log(form.padEnd(6), v.padEnd(15), JSON.stringify(row)) } } await browser.close() }