Skip to content

Commit 8b3fa87

Browse files
committed
try inlining fonts in svg
1 parent eabdcba commit 8b3fa87

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

packages/dev/s2-docs/scripts/generateOGImages.mjs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ const [adobeCleanRegular, adobeCleanBold] = await Promise.all([
8989
loadFont('https://use.typekit.net/af/eaf09c/000000000000000000017703/27/d?primer=7cdcb44be4a7db8877ffa5c0007b8dd865b3bbc383831fe2ea177f62257a9191&fvd=n7&v=3')
9090
]);
9191

92+
// Detect font MIME type and CSS format for inlining
93+
function detectFontFormatAndMime(bytes) {
94+
const buf = Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes);
95+
const header = buf.subarray(0, 4).toString('ascii');
96+
if (header === 'wOF2') {
97+
return {mime: 'font/woff2', format: 'woff2'};
98+
}
99+
if (header === 'wOFF') {
100+
return {mime: 'font/woff', format: 'woff'};
101+
}
102+
if (header === 'OTTO') {
103+
return {mime: 'font/otf', format: 'opentype'};
104+
}
105+
return {mime: 'font/woff2', format: 'woff2'};
106+
}
107+
92108
// Mappings for components that don't match their SVG file names
93109
const componentSvgExceptions = {
94110
'Accordion': 'DisclosureGroup.svg',
@@ -271,6 +287,14 @@ async function getComponentSvg(title) {
271287
svgContent = svgContent.replace(/background:\s*#f4f6fc/g, 'background: #f8f8f8');
272288
svgContent = svgContent.replace(/var\(--anatomy-font\)/g, 'adobe-clean');
273289

290+
// Inline fonts so text within illustration renders in CI
291+
const regMeta = detectFontFormatAndMime(adobeCleanRegular);
292+
const boldMeta = detectFontFormatAndMime(adobeCleanBold);
293+
const regB64 = Buffer.from(adobeCleanRegular).toString('base64');
294+
const boldB64 = Buffer.from(adobeCleanBold).toString('base64');
295+
const fontCss = `<style>@font-face{font-family:'adobe-clean';src:url(data:${regMeta.mime};base64,${regB64}) format('${regMeta.format}');font-weight:400;font-style:normal;font-display:block}@font-face{font-family:'adobe-clean';src:url(data:${boldMeta.mime};base64,${boldB64}) format('${boldMeta.format}');font-weight:700;font-style:normal;font-display:block}</style>`;
296+
svgContent = svgContent.replace(/<svg\b[^>]*>/i, (m) => m + fontCss);
297+
274298
// Convert SVG to data URI for use as image source
275299
const svgBase64 = Buffer.from(svgContent).toString('base64');
276300
return `data:image/svg+xml;base64,${svgBase64}`;

0 commit comments

Comments
 (0)