|
| 1 | +// TEMPORARY, DO NOT MERGE: renders real Docusaurus pages with the font stacks of |
| 2 | +// meta/docusaurus#13148 (before) and of the stacked cleanup PR (after) |
| 3 | +// usage: node probe.mjs <label> <browsers, comma-separated> [locale] |
| 4 | +import fs from 'node:fs'; |
| 5 | +import {chromium, firefox, webkit} from 'playwright-core'; |
| 6 | + |
| 7 | +const [label, browserList, locale = 'en-US'] = process.argv.slice(2); |
| 8 | + |
| 9 | +const EMOJI = `'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'`; |
| 10 | +const MONO = `SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace`; |
| 11 | +const BEFORE_BASE = `system-ui, -apple-system, 'Segoe UI', Roboto, Ubuntu, Cantarell, 'Noto Sans', sans-serif, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, ${EMOJI}`; |
| 12 | +const BEFORE_MONO = MONO; |
| 13 | +const AFTER_BASE = `system-ui, -apple-system, 'Segoe UI', Roboto, Ubuntu, Cantarell, 'Noto Sans', sans-serif, ${EMOJI}`; |
| 14 | +const AFTER_MONO = MONO; |
| 15 | + |
| 16 | +const css = (base, mono) => |
| 17 | + `:root { --ifm-font-family-base: ${base} !important; --ifm-font-family-monospace: ${mono} !important; }`; |
| 18 | +const VARIANTS = { |
| 19 | + before: css(BEFORE_BASE, BEFORE_MONO), |
| 20 | + after: css(AFTER_BASE, AFTER_MONO), |
| 21 | + keepArial: css(`system-ui, -apple-system, 'Segoe UI', Roboto, Ubuntu, Cantarell, 'Noto Sans', sans-serif, Arial, ${EMOJI}`, AFTER_MONO), |
| 22 | +}; |
| 23 | + |
| 24 | +const PAGES = [ |
| 25 | + { |
| 26 | + id: 'scripts', |
| 27 | + url: 'https://docusaurus.io/docs/installation', |
| 28 | + inject: '<div id="probe-scripts"><p id="s-ar" lang="ar">العربية: دوكوسورس يساعدك على بناء مواقع التوثيق بسرعة</p><p id="s-fa" lang="fa">فارسی: داکوسوروس به شما کمک میکند</p><p id="s-he" lang="he">עברית: דוקוסאורוס עוזר לך לבנות אתרי תיעוד</p><p id="s-ru" lang="ru">Русский: Docusaurus помогает быстро создавать сайты</p><p id="s-el" lang="el">Ελληνικά: Το Docusaurus σας βοηθά να φτιάξετε ιστότοπους</p><p id="s-vi" lang="vi">Tiếng Việt: Docusaurus giúp bạn xây dựng trang tài liệu</p><p id="s-th" lang="th">ไทย: Docusaurus ช่วยให้คุณสร้างเว็บไซต์เอกสาร</p><p id="s-hi" lang="hi">हिन्दी: डॉक्यूसॉरस आपको दस्तावेज़ साइटें बनाने में मदद करता है</p><p id="s-hy" lang="hy">Հայերեն: Docusaurus-ը օգնում է</p><p id="s-ka" lang="ka">ქართული: Docusaurus გეხმარებათ</p><p id="s-und" lang="und">Symbols: ← → ↑ ✓ ✔ ★ ☆ ♥ © ® ™ € £ ¥ § ¶ † ‡ • … ½ ∞ ≠ ≤ ≥ ∑ √ ⌘ ⌥ ⇧ ⏎ ₹ ₽ ₺ ₩ ﷼</p></div>', |
| 29 | + selectors: ['#s-ar', '#s-fa', '#s-he', '#s-ru', '#s-el', '#s-vi', '#s-th', '#s-hi', '#s-hy', '#s-ka', '#s-und'], |
| 30 | + clip: '#probe-scripts', |
| 31 | + }, |
| 32 | +]; |
| 33 | + |
| 34 | +const LATIN_CANDIDATES = [ |
| 35 | + 'system-ui', '-apple-system', 'BlinkMacSystemFont', 'sans-serif', 'monospace', 'ui-monospace', |
| 36 | + "'Segoe UI'", "'Yu Gothic'", 'Meiryo', "'Microsoft YaHei'", 'Arial', 'Helvetica', "'Helvetica Neue'", |
| 37 | + "'Liberation Sans'", "'DejaVu Sans'", "'Ubuntu Sans'", 'Ubuntu', "'Adwaita Sans'", 'Cantarell', "'Noto Sans'", |
| 38 | + 'Roboto', 'SFMono-Regular', "'SF Mono'", 'Menlo', 'Monaco', 'Consolas', "'Liberation Mono'", "'DejaVu Sans Mono'", |
| 39 | + "'Courier New'", |
| 40 | +]; |
| 41 | + |
| 42 | +const headless = process.platform !== 'linux'; |
| 43 | +const BROWSERS = { |
| 44 | + chrome: () => chromium.launch({channel: 'chrome', headless, args: [`--lang=${locale}`]}), |
| 45 | + msedge: () => chromium.launch({channel: 'msedge', headless, args: [`--lang=${locale}`]}), |
| 46 | + firefox: () => firefox.launch({headless}), |
| 47 | + webkit: () => webkit.launch({headless}), |
| 48 | +}; |
| 49 | + |
| 50 | +// Non-Chromium browsers have no API for the rendered font: compare the width of a Latin sample |
| 51 | +// with the width it gets in each installed candidate font |
| 52 | +async function guessLatinFonts(page, selector) { |
| 53 | + return page.evaluate( |
| 54 | + ({selector, candidates}) => { |
| 55 | + const el = document.querySelector(selector); |
| 56 | + if (!el) return null; |
| 57 | + const sample = 'The quick brown fox jumps over the lazy dog 0123456789 {}[]();'; |
| 58 | + const measure = (parent, family) => { |
| 59 | + const span = document.createElement('span'); |
| 60 | + span.textContent = sample; |
| 61 | + span.style.cssText = `white-space:nowrap;position:absolute;visibility:hidden;${family ? `font-family:${family}` : ''}`; |
| 62 | + parent.appendChild(span); |
| 63 | + const width = span.getBoundingClientRect().width; |
| 64 | + span.remove(); |
| 65 | + return width; |
| 66 | + }; |
| 67 | + const target = measure(el); |
| 68 | + const fallback = measure(el, 'fantasy'); |
| 69 | + return candidates.filter((family) => { |
| 70 | + const width = measure(el, `${family}, fantasy`); |
| 71 | + const installed = width !== fallback; |
| 72 | + return installed && Math.abs(width - target) < 0.5; |
| 73 | + }); |
| 74 | + }, |
| 75 | + {selector, candidates: LATIN_CANDIDATES}, |
| 76 | + ); |
| 77 | +} |
| 78 | + |
| 79 | +async function chromiumFonts(cdp, selector) { |
| 80 | + const {root} = await cdp.send('DOM.getDocument', {depth: -1}); |
| 81 | + const {nodeIds} = await cdp.send('DOM.querySelectorAll', {nodeId: root.nodeId, selector}); |
| 82 | + const results = []; |
| 83 | + for (const nodeId of nodeIds.slice(0, 3)) { |
| 84 | + const {fonts} = await cdp.send('CSS.getPlatformFontsForNode', {nodeId}); |
| 85 | + results.push(fonts.map((f) => `${f.familyName}(${f.glyphCount})`).join(' + ') || '-'); |
| 86 | + } |
| 87 | + return results; |
| 88 | +} |
| 89 | + |
| 90 | +const report = []; |
| 91 | +for (const name of browserList.split(',')) { |
| 92 | + let browser; |
| 93 | + try { |
| 94 | + browser = await BROWSERS[name](); |
| 95 | + } catch (err) { |
| 96 | + console.log(`${name}: cannot launch (${err.message.split('\n')[0]})`); |
| 97 | + continue; |
| 98 | + } |
| 99 | + const context = await browser.newContext({locale, viewport: {width: 1000, height: 900}, reducedMotion: 'reduce'}); |
| 100 | + const page = await context.newPage(); |
| 101 | + console.log(`\n## ${label} / ${name} ${browser.version()} / locale ${locale}`); |
| 102 | + for (const {id, url, htmlLang, selectors, fullPage, scrollTo, inject, clip} of PAGES) { |
| 103 | + try { |
| 104 | + await page.goto(url, {waitUntil: 'networkidle', timeout: 60_000}); |
| 105 | + } catch (err) { |
| 106 | + console.log(`${id}: cannot load (${err.message.split('\n')[0]})`); |
| 107 | + continue; |
| 108 | + } |
| 109 | + if (htmlLang) await page.evaluate((lang) => (document.documentElement.lang = lang), htmlLang); |
| 110 | + if (inject) await page.evaluate((html) => document.querySelector('article').insertAdjacentHTML('afterbegin', html), inject); |
| 111 | + for (const [variant, variantCss] of Object.entries(VARIANTS)) { |
| 112 | + await page.evaluate((variantCss) => { |
| 113 | + document.getElementById('probe-style')?.remove(); |
| 114 | + const style = document.createElement('style'); |
| 115 | + style.id = 'probe-style'; |
| 116 | + style.textContent = variantCss; |
| 117 | + document.head.appendChild(style); |
| 118 | + }, variantCss); |
| 119 | + await page.evaluate(() => document.fonts.ready); |
| 120 | + if (scrollTo) await page.evaluate((s) => document.querySelector(s)?.scrollIntoView({block: 'start'}), scrollTo); |
| 121 | + await page.waitForTimeout(300); |
| 122 | + const file = `${label}-${name}-${id}-${variant}.png`; |
| 123 | + if (clip) await page.locator(clip).screenshot({path: file}); |
| 124 | + else await page.screenshot({path: file, fullPage}); |
| 125 | + console.log(`\n### ${id} / ${variant} (${file})`); |
| 126 | + const cdp = !['firefox', 'webkit'].includes(name) ? await context.newCDPSession(page) : null; |
| 127 | + if (cdp) { |
| 128 | + await cdp.send('DOM.enable'); |
| 129 | + await cdp.send('CSS.enable'); |
| 130 | + } |
| 131 | + for (const selector of selectors) { |
| 132 | + const fonts = cdp ? await chromiumFonts(cdp, selector) : [await guessLatinFonts(page, selector)]; |
| 133 | + console.log(`${selector.padEnd(30)} ${fonts.map((f) => (Array.isArray(f) ? `latin≈[${f.join(', ')}]` : f)).join(' | ')}`); |
| 134 | + report.push({label, browser: name, page: id, variant, selector, fonts}); |
| 135 | + } |
| 136 | + await cdp?.detach(); |
| 137 | + } |
| 138 | + } |
| 139 | + await browser.close(); |
| 140 | +} |
| 141 | +fs.appendFileSync('report.json', report.map((r) => JSON.stringify(r)).join('\n') + '\n'); |
0 commit comments