Something went wrong. Try again.
doll.systems community website
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546import { glob, mkdir, readFile, writeFile } from 'node:fs/promises';import subsetFont from 'subset-font';
export async function subsetFonts() { const identity = JSON.parse(await readFile('src/assets/identity.json', 'utf8')); const fonts = [ { family: 'LINE Seed JP', package: 'line-seed-jp', weights: [400, 700] }, { family: 'DotGothic16', package: 'dotgothic16', weights: [400] }, ]; const files = await Array.fromAsync(glob('src/**/*.{astro,glsl,json,md,mjs,scss,ts}')); const sources = await Promise.all(files.map((file) => readFile(file, 'utf8'))); const glyphs = [...new Set(sources.join(''))] .filter((character) => character.codePointAt(0) > 127) .sort((a, b) => a.codePointAt(0) - b.codePointAt(0)); const text = glyphs.join(''); const unicodeRange = glyphs .map((character) => `U+${character.codePointAt(0).toString(16).toUpperCase()}`) .join(','); const faces = []; await mkdir('.astro/generated/fonts', { recursive: true }); await mkdir('.astro/generated/styles', { recursive: true }); for (const { family, package: name, weights } of fonts) for (const weight of weights) { const file = `${name}-site-${weight}.woff2`; const source = await readFile( `node_modules/@fontsource/${name}/files/${name}-japanese-${weight}-normal.woff2`, ); await writeFile( `.astro/generated/fonts/${file}`, await subsetFont(source, text, { targetFormat: 'woff2' }), ); if (name === 'line-seed-jp' && weight === 400) await writeFile( '.astro/generated/fonts/line-seed-jp-title.woff2', await subsetFont(source, identity.flickerTitle, { targetFormat: 'woff2' }), ); faces.push( `@font-face{font-family:'${family}';src:url('../fonts/${file}') format('woff2');font-style:normal;font-weight:${weight};font-display:swap;unicode-range:${unicodeRange}}`, ); } await writeFile( '.astro/generated/styles/_japanese-fonts.scss', `/* Generated by scripts/subset-fonts.mjs. */\n${faces.join('\n')}\n`, );}