Skip to content

Commit 091c7ea

Browse files
slorberclaude
andcommitted
DO NOT MERGE: temporary font screenshots for #13133
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent caa3a86 commit 091c7ea

2 files changed

Lines changed: 256 additions & 0 deletions

File tree

‎.github/tmp-font-probe/probe.mjs‎

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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 VARIANTS = {
14+
before: `:root { --ifm-font-family-base: ${BEFORE} !important; }`,
15+
after: `:root { --ifm-font-family-base: ${AFTER} !important; }`,
16+
keepLinux:
17+
`:root { --ifm-font-family-base: ${KEEP_LINUX} !important; }\n` +
18+
`:root:where(:lang(zh), :lang(ja), :lang(ko)) { --ifm-font-family-base: ${AFTER} !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+
{
29+
id: 'ja-docs',
30+
url: 'https://docusaurus-i18n-staging.netlify.app/ja/docs/',
31+
selectors: ['.navbar__title', '.navbar__item', '.menu__link', 'article h1', 'article p'],
32+
fullPage: false,
33+
},
34+
];
35+
36+
const LATIN_CANDIDATES = [
37+
'system-ui', '-apple-system', 'BlinkMacSystemFont', 'sans-serif',
38+
"'Segoe UI'", "'Yu Gothic UI'", "'Yu Gothic'", 'Meiryo', "'Microsoft YaHei UI'", "'Microsoft YaHei'",
39+
"'Microsoft JhengHei UI'", "'Malgun Gothic'", 'Arial', 'Helvetica', "'Helvetica Neue'",
40+
"'Liberation Sans'", "'DejaVu Sans'", "'Ubuntu Sans'", 'Ubuntu', 'Cantarell', "'Noto Sans'",
41+
"'Comic Neue'", 'Roboto', "'Times New Roman'",
42+
];
43+
44+
// Linux needs a headed browser (under Xvfb) to read the GTK desktop font
45+
const headless = process.platform !== 'linux';
46+
const BROWSERS = {
47+
chrome: () => chromium.launch({channel: 'chrome', headless, args: [`--lang=${locale}`]}),
48+
msedge: () => chromium.launch({channel: 'msedge', headless, args: [`--lang=${locale}`]}),
49+
chromium: () => chromium.launch({headless, args: [`--lang=${locale}`]}),
50+
firefox: () => firefox.launch({headless}),
51+
webkit: () => webkit.launch({headless}),
52+
};
53+
54+
// Non-Chromium browsers have no API for the rendered font: compare the width of a Latin sample
55+
// with the width it gets in each installed candidate font
56+
async function guessLatinFonts(page, selector) {
57+
return page.evaluate(
58+
({selector, candidates}) => {
59+
const el = document.querySelector(selector);
60+
if (!el) return null;
61+
const sample = 'The quick brown fox jumps over the lazy dog 0123456789';
62+
const measure = (parent, family) => {
63+
const span = document.createElement('span');
64+
span.textContent = sample;
65+
span.style.cssText = `white-space:nowrap;position:absolute;visibility:hidden;${family ? `font-family:${family}` : ''}`;
66+
parent.appendChild(span);
67+
const width = span.getBoundingClientRect().width;
68+
span.remove();
69+
return width;
70+
};
71+
const target = measure(el);
72+
const mono = measure(el, 'monospace');
73+
return candidates.filter((family) => {
74+
const width = measure(el, `${family}, monospace`);
75+
const installed = width !== mono || family === 'monospace';
76+
return installed && Math.abs(width - target) < 0.5;
77+
});
78+
},
79+
{selector, candidates: LATIN_CANDIDATES},
80+
);
81+
}
82+
83+
async function chromiumFonts(cdp, selector) {
84+
const {root} = await cdp.send('DOM.getDocument', {depth: -1});
85+
const {nodeIds} = await cdp.send('DOM.querySelectorAll', {nodeId: root.nodeId, selector});
86+
const results = [];
87+
for (const nodeId of nodeIds.slice(0, 3)) {
88+
const {fonts} = await cdp.send('CSS.getPlatformFontsForNode', {nodeId});
89+
results.push(fonts.map((f) => `${f.familyName}(${f.glyphCount})`).join(' + ') || '-');
90+
}
91+
return results;
92+
}
93+
94+
const report = [];
95+
for (const name of browserList.split(',')) {
96+
let browser;
97+
try {
98+
browser = await BROWSERS[name]();
99+
} catch (err) {
100+
console.log(`${name}: cannot launch (${err.message.split('\n')[0]})`);
101+
continue;
102+
}
103+
const context = await browser.newContext({locale, viewport: {width: 1000, height: 900}});
104+
const page = await context.newPage();
105+
console.log(`\n## ${label} / ${name} ${browser.version()} / locale ${locale}`);
106+
for (const {id, url, selectors, fullPage} of PAGES) {
107+
try {
108+
await page.goto(url, {waitUntil: 'networkidle', timeout: 60_000});
109+
} catch (err) {
110+
console.log(`${id}: cannot load (${err.message.split('\n')[0]})`);
111+
continue;
112+
}
113+
for (const [variant, css] of Object.entries(VARIANTS)) {
114+
await page.evaluate((css) => {
115+
document.getElementById('probe-style')?.remove();
116+
const style = document.createElement('style');
117+
style.id = 'probe-style';
118+
style.textContent = css;
119+
document.head.appendChild(style);
120+
}, css);
121+
await page.evaluate(() => document.fonts.ready);
122+
await page.waitForTimeout(300);
123+
const file = `${label}-${name}-${id}-${variant}.png`;
124+
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+
// What system-ui alone resolves to, to check the OS setting applies
137+
if (variant === 'before') {
138+
await page.evaluate(() => {
139+
const p = document.createElement('p');
140+
p.id = 'probe-system-ui';
141+
p.style.fontFamily = 'system-ui';
142+
p.textContent = 'system-ui control 今令直骨角次';
143+
document.body.appendChild(p);
144+
});
145+
const fonts = cdp ? await chromiumFonts(cdp, '#probe-system-ui') : [await guessLatinFonts(page, '#probe-system-ui')];
146+
console.log(`${'system-ui alone'.padEnd(30)} ${fonts.map((f) => (Array.isArray(f) ? `latin≈[${f.join(', ')}]` : f)).join(' | ')}`);
147+
await page.evaluate(() => document.getElementById('probe-system-ui').remove());
148+
}
149+
await cdp?.detach();
150+
}
151+
}
152+
await browser.close();
153+
}
154+
fs.appendFileSync('report.json', report.map((r) => JSON.stringify(r)).join('\n') + '\n');
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# TEMPORARY, DO NOT MERGE: screenshots of the font stacks of meta/docusaurus#13133
2+
name: TMP font screenshots
3+
4+
on:
5+
push:
6+
branches: [claude/tmp-font-probe]
7+
8+
permissions:
9+
contents: read
10+
11+
jobs:
12+
probe:
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
os: [ubuntu-24.04, windows-latest, macos-latest]
17+
runs-on: ${{ matrix.os }}
18+
steps:
19+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
20+
with:
21+
persist-credentials: false
22+
23+
- name: Set up Node.js
24+
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
25+
with:
26+
node-version: lts/*
27+
28+
- name: Install probe
29+
shell: bash
30+
run: |
31+
mkdir -p "$RUNNER_TEMP/probe" && cp .github/tmp-font-probe/probe.mjs "$RUNNER_TEMP/probe/"
32+
cd "$RUNNER_TEMP/probe"
33+
npm init -y > /dev/null
34+
npm install --no-audit --no-fund playwright-core@1.63.0 > /dev/null
35+
36+
- name: Linux
37+
if: runner.os == 'Linux'
38+
run: |
39+
cd "$RUNNER_TEMP/probe"
40+
sudo apt-get update > /dev/null
41+
sudo apt-get install -y xvfb fonts-ubuntu fonts-noto-core fonts-noto-cjk fonts-cantarell fonts-comic-neue > /dev/null
42+
npx playwright-core install --with-deps chromium firefox > /dev/null
43+
fc-list : family | grep -iE 'ubuntu|cantarell|noto sans($|,| cjk)|comic|dejavu sans$|liberation sans' | sort -u
44+
google-chrome --version || true
45+
xvfb-run -a node probe.mjs linux-gtkdefault chrome,firefox
46+
# Chrome and Firefox read system-ui from the GTK font of the desktop
47+
for font in 'Ubuntu Sans' 'Noto Sans' 'Comic Neue'; do
48+
for gtk in gtk-3.0 gtk-4.0; do
49+
mkdir -p ~/.config/$gtk
50+
printf '[Settings]\ngtk-font-name=%s 11\n' "$font" > ~/.config/$gtk/settings.ini
51+
done
52+
echo "::group::GTK font $font"
53+
xvfb-run -a node probe.mjs "linux-gtk-${font// /}" chrome,firefox
54+
echo "::endgroup::"
55+
done
56+
57+
- name: macOS
58+
if: runner.os == 'macOS'
59+
run: |
60+
cd "$RUNNER_TEMP/probe"
61+
npx playwright-core install firefox webkit > /dev/null
62+
node probe.mjs macos chrome,msedge,firefox,webkit
63+
64+
- name: Windows
65+
if: runner.os == 'Windows'
66+
shell: pwsh
67+
run: |
68+
cd "$env:RUNNER_TEMP/probe"
69+
npx playwright-core install firefox | Out-Null
70+
Add-Type -TypeDefinition @"
71+
using System;
72+
using System.Runtime.InteropServices;
73+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
74+
public struct LOGFONT { public int lfHeight; public int lfWidth; public int lfEscapement; public int lfOrientation; public int lfWeight; public byte lfItalic; public byte lfUnderline; public byte lfStrikeOut; public byte lfCharSet; public byte lfOutPrecision; public byte lfClipPrecision; public byte lfQuality; public byte lfPitchAndFamily; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string lfFaceName; }
75+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
76+
public struct NONCLIENTMETRICS { public int cbSize; public int iBorderWidth; public int iScrollWidth; public int iScrollHeight; public int iCaptionWidth; public int iCaptionHeight; public LOGFONT lfCaptionFont; public int iSmCaptionWidth; public int iSmCaptionHeight; public LOGFONT lfSmCaptionFont; public int iMenuWidth; public int iMenuHeight; public LOGFONT lfMenuFont; public LOGFONT lfStatusFont; public LOGFONT lfMessageFont; public int iPaddedBorderWidth; }
77+
public static class Ncm {
78+
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
79+
static extern bool SystemParametersInfo(uint action, uint param, ref NONCLIENTMETRICS ncm, uint winIni);
80+
static NONCLIENTMETRICS Read() { var n = new NONCLIENTMETRICS(); n.cbSize = Marshal.SizeOf(n); SystemParametersInfo(0x29, (uint)n.cbSize, ref n, 0); return n; }
81+
public static string Get() { var n = Read(); return n.lfMenuFont.lfFaceName + " / " + n.lfMessageFont.lfFaceName; }
82+
public static bool Set(string face) { var n = Read(); n.lfMenuFont.lfFaceName = face; n.lfMessageFont.lfFaceName = face; n.lfStatusFont.lfFaceName = face; n.lfCaptionFont.lfFaceName = face; n.lfSmCaptionFont.lfFaceName = face; return SystemParametersInfo(0x2A, (uint)n.cbSize, ref n, 3); }
83+
}
84+
"@
85+
Write-Output "Menu / message fonts: $([Ncm]::Get())"
86+
node probe.mjs windows-english chrome,msedge,firefox en-US
87+
# Simulates the display languages: Chrome, Edge and Firefox read system-ui from the menu font
88+
foreach ($setup in @(@('Yu Gothic UI', 'ja-JP'), @('Microsoft YaHei UI', 'zh-CN'))) {
89+
$ok = [Ncm]::Set($setup[0])
90+
Write-Output "`n::group::UI font $($setup[0]): $ok, now '$([Ncm]::Get())'"
91+
node probe.mjs "windows-$($setup[1])" chrome,msedge,firefox $setup[1]
92+
Write-Output "::endgroup::"
93+
}
94+
95+
- name: Upload screenshots
96+
if: always()
97+
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
98+
with:
99+
name: font-screenshots-${{ runner.os }}
100+
path: |
101+
${{ runner.temp }}/probe/*.png
102+
${{ runner.temp }}/probe/report.json

0 commit comments

Comments
 (0)