Skip to content

The VS Code of video editing, built for agents.

TypeScript
2.4 KB
Raw Blame
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Regenerates src/fonts/catalog.ts from the Google Fonts metadata, the same
// listing fonts.google.com browses: `node scripts/google-fonts.mjs`. The
// catalog carries names, categories and weights only; the stylesheet and font
// files are asked of the Google Fonts CSS API when a family is used.

import { writeFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';

const METADATA = 'https://fonts.google.com/metadata/fonts';
const OUT = fileURLToPath(new URL('../src/fonts/catalog.ts', import.meta.url));
const CATEGORIES = ['Sans Serif', 'Serif', 'Display', 'Handwriting', 'Monospace'];

const response = await fetch(METADATA);
if (!response.ok) throw new Error(`${METADATA}: ${response.status}`);
// The endpoint guards against JSON hijacking with a )]}' line.
const { familyMetadataList } = JSON.parse((await response.text()).replace(/^\)\]\}'\s*/, ''));

const rows = [];
for (const font of familyMetadataList) {
	const category = CATEGORIES.indexOf(font.category);
	if (category < 0) throw new Error(`${font.family}: unknown category ${font.category}`);

	const roman = [];
	const italic = [];
	for (const key of Object.keys(font.fonts)) {
		const match = /^(\d+)(i?)$/.exec(key);
		if (!match) throw new Error(`${font.family}: unknown variant ${key}`);
		(match[2] ? italic : roman).push(Number(match[1]));
	}
	roman.sort((a, b) => a - b);
	italic.sort((a, b) => a - b);

	const row = [font.family, category, roman, italic];
	const wght = font.axes.find((axis) => axis.tag === 'wght');
	if (wght) row.push([wght.min, wght.max]);
	rows.push(row);
}

rows.sort((a, b) => a[0].localeCompare(b[0]));

writeFileSync(OUT, `/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Generated by scripts/google-fonts.mjs from ${METADATA}; do not edit.
// One row per family: [family, category, weights, italic weights, wght axis?].

import type { GoogleFontRow } from './google';

export const GOOGLE_FONT_ROWS: GoogleFontRow[] = [
${rows.map((row) => `\t${JSON.stringify(row)},`).join('\n')}
];
`);

console.log(`${rows.length} families written to ${OUT}`);