|
| 1 | +// TEMPORARY, DO NOT MERGE: renders real Docusaurus pages with each font stack |
| 2 | +// usage: node probe.mjs <label> <browsers, comma-separated> [locale] |
| 3 | +import fs from 'node:fs'; |
| 4 | +import {chromium, firefox, webkit} from 'playwright-core'; |
| 5 | + |
| 6 | +const [label, browserList, locale = 'en-US'] = process.argv.slice(2); |
| 7 | + |
| 8 | +const EMOJI = `'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'`; |
| 9 | +const BEFORE = `system-ui, -apple-system, 'Segoe UI', Roboto, Ubuntu, Cantarell, 'Noto Sans', sans-serif, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, ${EMOJI}`; |
| 10 | +const AFTER = `-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Ubuntu, Cantarell, 'Noto Sans', sans-serif, 'Segoe UI', Helvetica, Arial, sans-serif, ${EMOJI}`; |
| 11 | +const KEEP_LINUX = `-apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, Roboto, Ubuntu, Cantarell, 'Noto Sans', sans-serif, 'Segoe UI', Helvetica, Arial, sans-serif, ${EMOJI}`; |
| 12 | + |
| 13 | +const LINUX_SAME = `-apple-system, BlinkMacSystemFont, 'Segoe UI', var(--ifm-font-family-cjk), system-ui, Roboto, Ubuntu, Cantarell, 'Noto Sans', sans-serif, 'Segoe UI', Helvetica, Arial, sans-serif, ${EMOJI}`; |
| 14 | + |
| 15 | +const DROP = `-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Ubuntu Sans', Ubuntu, 'Adwaita Sans', Cantarell, 'Noto Sans', sans-serif, 'Segoe UI', Helvetica, Arial, sans-serif, ${EMOJI}`; |
| 16 | +const VARIANTS = { |
| 17 | + before: `:root { --ifm-font-family-base: ${BEFORE} !important; }`, |
| 18 | + drop: `:root { --ifm-font-family-base: ${DROP} !important; }`, |
| 19 | +}; |
| 20 | + |
| 21 | +const PAGES = [ |
| 22 | + { |
| 23 | + id: 'cjk', |
| 24 | + url: 'https://deploy-preview-13132--docusaurus-2.netlify.app/tests/pages/cjk-font-tests', |
| 25 | + selectors: ['article h1', 'article > div > p', 'article div[lang] p', 'article div[lang] p strong'], |
| 26 | + fullPage: true, |
| 27 | + }, |
| 28 | + ...['ja', 'zh-CN', 'zh-TW', 'ko'].map((htmlLang) => ({ |
| 29 | + id: `site-${htmlLang}`, |
| 30 | + url: 'https://docusaurus-i18n-staging.netlify.app/ja/docs/', |
| 31 | + htmlLang, |
| 32 | + selectors: ['.navbar__title', '.navbar__item', '.menu__link', 'article h1', 'article p'], |
| 33 | + fullPage: false, |
| 34 | + })), |
| 35 | +]; |
| 36 | + |
| 37 | +const LATIN_CANDIDATES = [ |
| 38 | + 'system-ui', '-apple-system', 'BlinkMacSystemFont', 'sans-serif', |
| 39 | + "'Segoe UI'", "'Yu Gothic UI'", "'Yu Gothic'", 'Meiryo', "'Microsoft YaHei UI'", "'Microsoft YaHei'", |
| 40 | + "'Microsoft JhengHei UI'", "'Malgun Gothic'", 'Arial', 'Helvetica', "'Helvetica Neue'", |
| 41 | + "'Liberation Sans'", "'DejaVu Sans'", "'Ubuntu Sans'", 'Ubuntu', 'Cantarell', "'Noto Sans'", |
| 42 | + "'Comic Neue'", 'Roboto', "'Times New Roman'", |
| 43 | +]; |
| 44 | + |
| 45 | +// Linux needs a headed browser (under Xvfb) to read the GTK desktop font |
| 46 | +const headless = process.platform !== 'linux'; |
| 47 | +const BROWSERS = { |
| 48 | + chrome: () => chromium.launch({channel: 'chrome', headless, args: [`--lang=${locale}`]}), |
| 49 | + msedge: () => chromium.launch({channel: 'msedge', headless, args: [`--lang=${locale}`]}), |
| 50 | + chromium: () => chromium.launch({headless, args: [`--lang=${locale}`]}), |
| 51 | + firefox: () => firefox.launch({headless}), |
| 52 | + webkit: () => webkit.launch({headless}), |
| 53 | +}; |
| 54 | + |
| 55 | +// Non-Chromium browsers have no API for the rendered font: compare the width of a Latin sample |
| 56 | +// with the width it gets in each installed candidate font |
| 57 | +async function guessLatinFonts(page, selector) { |
| 58 | + return page.evaluate( |
| 59 | + ({selector, candidates}) => { |
| 60 | + const el = document.querySelector(selector); |
| 61 | + if (!el) return null; |
| 62 | + const sample = 'The quick brown fox jumps over the lazy dog 0123456789'; |
| 63 | + const measure = (parent, family) => { |
| 64 | + const span = document.createElement('span'); |
| 65 | + span.textContent = sample; |
| 66 | + span.style.cssText = `white-space:nowrap;position:absolute;visibility:hidden;${family ? `font-family:${family}` : ''}`; |
| 67 | + parent.appendChild(span); |
| 68 | + const width = span.getBoundingClientRect().width; |
| 69 | + span.remove(); |
| 70 | + return width; |
| 71 | + }; |
| 72 | + const target = measure(el); |
| 73 | + const mono = measure(el, 'monospace'); |
| 74 | + return candidates.filter((family) => { |
| 75 | + const width = measure(el, `${family}, monospace`); |
| 76 | + const installed = width !== mono || family === 'monospace'; |
| 77 | + return installed && Math.abs(width - target) < 0.5; |
| 78 | + }); |
| 79 | + }, |
| 80 | + {selector, candidates: LATIN_CANDIDATES}, |
| 81 | + ); |
| 82 | +} |
| 83 | + |
| 84 | +async function chromiumFonts(cdp, selector) { |
| 85 | + const {root} = await cdp.send('DOM.getDocument', {depth: -1}); |
| 86 | + const {nodeIds} = await cdp.send('DOM.querySelectorAll', {nodeId: root.nodeId, selector}); |
| 87 | + const results = []; |
| 88 | + for (const nodeId of nodeIds.slice(0, 3)) { |
| 89 | + const {fonts} = await cdp.send('CSS.getPlatformFontsForNode', {nodeId}); |
| 90 | + results.push(fonts.map((f) => `${f.familyName}(${f.glyphCount})`).join(' + ') || '-'); |
| 91 | + } |
| 92 | + return results; |
| 93 | +} |
| 94 | + |
| 95 | +const report = []; |
| 96 | +for (const name of browserList.split(',')) { |
| 97 | + let browser; |
| 98 | + try { |
| 99 | + browser = await BROWSERS[name](); |
| 100 | + } catch (err) { |
| 101 | + console.log(`${name}: cannot launch (${err.message.split('\n')[0]})`); |
| 102 | + continue; |
| 103 | + } |
| 104 | + const context = await browser.newContext({locale, viewport: {width: 1000, height: 900}}); |
| 105 | + const page = await context.newPage(); |
| 106 | + console.log(`\n## ${label} / ${name} ${browser.version()} / locale ${locale}`); |
| 107 | + for (const {id, url, htmlLang, selectors, fullPage} of PAGES) { |
| 108 | + try { |
| 109 | + await page.goto(url, {waitUntil: 'networkidle', timeout: 60_000}); |
| 110 | + } catch (err) { |
| 111 | + console.log(`${id}: cannot load (${err.message.split('\n')[0]})`); |
| 112 | + continue; |
| 113 | + } |
| 114 | + // Simulates a site in that locale: the staging site only deploys Japanese |
| 115 | + if (htmlLang) await page.evaluate((lang) => (document.documentElement.lang = lang), htmlLang); |
| 116 | + for (const [variant, css] of Object.entries(VARIANTS)) { |
| 117 | + await page.evaluate((css) => { |
| 118 | + document.getElementById('probe-style')?.remove(); |
| 119 | + const style = document.createElement('style'); |
| 120 | + style.id = 'probe-style'; |
| 121 | + style.textContent = css; |
| 122 | + document.head.appendChild(style); |
| 123 | + }, css); |
| 124 | + await page.evaluate(() => document.fonts.ready); |
| 125 | + await page.waitForTimeout(300); |
| 126 | + const file = `${label}-${name}-${id}-${variant}.png`; |
| 127 | + await page.screenshot({path: file, fullPage}); |
| 128 | + console.log(`\n### ${id} / ${variant} (${file})`); |
| 129 | + const cdp = !['firefox', 'webkit'].includes(name) ? await context.newCDPSession(page) : null; |
| 130 | + if (cdp) { |
| 131 | + await cdp.send('DOM.enable'); |
| 132 | + await cdp.send('CSS.enable'); |
| 133 | + } |
| 134 | + for (const selector of selectors) { |
| 135 | + const fonts = cdp ? await chromiumFonts(cdp, selector) : [await guessLatinFonts(page, selector)]; |
| 136 | + console.log(`${selector.padEnd(30)} ${fonts.map((f) => (Array.isArray(f) ? `latin≈[${f.join(', ')}]` : f)).join(' | ')}`); |
| 137 | + report.push({label, browser: name, page: id, variant, selector, fonts}); |
| 138 | + } |
| 139 | + // What system-ui alone resolves to, to check the OS setting applies |
| 140 | + if (variant === 'before') { |
| 141 | + await page.evaluate(() => { |
| 142 | + const p = document.createElement('p'); |
| 143 | + p.id = 'probe-system-ui'; |
| 144 | + p.style.fontFamily = 'system-ui'; |
| 145 | + p.textContent = 'system-ui control 今令直骨角次'; |
| 146 | + document.body.appendChild(p); |
| 147 | + }); |
| 148 | + const fonts = cdp ? await chromiumFonts(cdp, '#probe-system-ui') : [await guessLatinFonts(page, '#probe-system-ui')]; |
| 149 | + console.log(`${'system-ui alone'.padEnd(30)} ${fonts.map((f) => (Array.isArray(f) ? `latin≈[${f.join(', ')}]` : f)).join(' | ')}`); |
| 150 | + await page.evaluate(() => document.getElementById('probe-system-ui').remove()); |
| 151 | + } |
| 152 | + await cdp?.detach(); |
| 153 | + } |
| 154 | + } |
| 155 | + await browser.close(); |
| 156 | +} |
| 157 | +fs.appendFileSync('report.json', report.map((r) => JSON.stringify(r)).join('\n') + '\n'); |
0 commit comments