|
| 1 | +import {readFileSync} from 'node:fs' |
| 2 | +import {webkit, chromium} from 'playwright' |
| 3 | +const fonts = JSON.parse(readFileSync(new URL('./fonts.json', import.meta.url), 'utf8')) |
| 4 | +const faces = Object.entries(fonts).map(([k, b]) => `@font-face{font-family:'S-${k}';src:url(data:font/woff2;base64,${b}) format('woff2');font-weight:400 700}`).join('\n') |
| 5 | +const label = `--pk-label-line: round(up, 1lh, 1px);--pk-label-room: calc(var(--pk-label-line) - round(down, 1cap, 0.015625px) - 1px);--pk-label-above: round(down, calc(var(--pk-label-room) / 2), 0.015625px);text-box: trim-both cap alphabetic;box-sizing: border-box;min-height: var(--pk-label-line);padding-block: var(--pk-label-above) calc(var(--pk-label-room) - var(--pk-label-above));` |
| 6 | +const variants = { |
| 7 | + plain: '', |
| 8 | + label, |
| 9 | + lbcBlock: '-webkit-line-box-contain: block;', |
| 10 | + labelLbcBlock: label + '-webkit-line-box-contain: block;', |
| 11 | + labelLbcBlockInline: label + '-webkit-line-box-contain: block inline;', |
| 12 | + labelLbcBlockGlyphs: label + '-webkit-line-box-contain: block glyphs;', |
| 13 | + labelLbcBlockReplaced: label + '-webkit-line-box-contain: block replaced;', |
| 14 | + labelSpanLh0: label + '|span', |
| 15 | +} |
| 16 | +const text = 'Eb cd ef gh ij kl mn op qr st uv' |
| 17 | +const page = `<!doctype html><html><head><style>${faces}\nbody{margin:0}div.t{display:block;width:90px;line-height:1.5}span.in{line-height:0}</style></head><body><div id=root></div> |
| 18 | +<script> |
| 19 | +const variants=${JSON.stringify(variants)}, fams=${JSON.stringify(Object.keys(fonts))}; |
| 20 | +(async()=>{for(const f of fams){await document.fonts.load('12px "S-'+f+'"')} |
| 21 | +const out=[]; |
| 22 | +for(const f of fams)for(const size of [12,13,14,16])for(const [name,css] of Object.entries(variants)){ |
| 23 | + const el=document.createElement('div');el.className='t';const [c,mode]=css.split('|');el.style.cssText=c+';font-family:"S-'+f+'";font-size:'+size+'px'; |
| 24 | + if(mode==='span'){const s=document.createElement('span');s.className='in';s.textContent=${JSON.stringify(text)};el.append(s)}else el.textContent=${JSON.stringify(text)}; |
| 25 | + document.getElementById('root').append(el); |
| 26 | + const r=document.createRange();r.selectNodeContents(el);const lines=new Set([...r.getClientRects()].map(x=>Math.round(x.top*64)/64)).size; |
| 27 | + out.push({f,size,name,lh:getComputedStyle(el).lineHeight,lines,h:el.getBoundingClientRect().height});el.remove()} |
| 28 | +document.title='done';window.__out=out})() |
| 29 | +</script></body></html>` |
| 30 | +for (const [name, type] of [['webkit', webkit], ['chromium', chromium]]) { |
| 31 | + const browser = await type.launch() |
| 32 | + const p = await browser.newPage() |
| 33 | + await p.setContent(page) |
| 34 | + await p.waitForFunction(() => document.title === 'done') |
| 35 | + const out = await p.evaluate(() => window.__out) |
| 36 | + console.log(`== ${name} ${process.platform} supports lbc: ${await p.evaluate(() => CSS.supports('-webkit-line-box-contain', 'block'))}`) |
| 37 | + for (const o of out) console.log(`${o.f} ${o.size}px lh=${o.lh} ${o.name.padEnd(22)} lines=${o.lines} h=${o.h.toFixed(5)}`) |
| 38 | + await browser.close() |
| 39 | +} |
0 commit comments