Skip to content

🎥 Make videos programmatically with React

better type safety and make all parameters optional

Jonny Burgere9e7d77eb71f
diff --git a/packages/google-fonts/scripts/generate.ts b/packages/google-fonts/scripts/generate.ts
index 14ccb5c5d2..f7244d297b 100644
--- a/packages/google-fonts/scripts/generate.ts
+++ b/packages/google-fonts/scripts/generate.ts
@@ -7,6 +7,7 @@ import PQueue from "p-queue";
import { getCssLink, unqoute, quote, removeWhitespace } from "./utils";
import { Font, googleFonts } from "./google-fonts";
+import { Info } from "../src/base";
const OUTDIR = "./src";
const CSS_CACHE_DIR = "./.cache-css";
@@ -24,7 +25,7 @@ const generate = async (font: Font) => {
// Read css from cache, otherwise from url
let css: null | string = null;
- let fontFamily: null | string = null;
+ let fontFamily: null | string = unqoute(font.family);
let cssFile = path.resolve(CSS_CACHE_DIR, cssname);
if (!fs.existsSync(cssFile)) {
// Get from url with user agent that support woff2
@@ -116,7 +117,7 @@ const generate = async (font: Font) => {
console.log(`- Generating ${filename}`);
// Prepare info data
- const info = {
+ const info: Info = {
fontFamily,
importName,
version: font.version,
@@ -143,10 +144,10 @@ type Variants = {\n`;
output += `};
export const loadFont = <T extends keyof Variants>(
- style: T,
- options: {
- weights: Variants[T]['weights'][];
- subsets: Variants[T]['subsets'][];
+ style?: T,
+ options?: {
+ weights?: Variants[T]['weights'][];
+ subsets?: Variants[T]['subsets'][];
}
) => {
return loadFonts(info, style, options);
diff --git a/packages/google-fonts/src/base.ts b/packages/google-fonts/src/base.ts
index 5a58889313..913ac6ac44 100644
--- a/packages/google-fonts/src/base.ts
+++ b/packages/google-fonts/src/base.ts
@@ -2,71 +2,95 @@ import type {} from "css-font-loading-module";
import { continueRender, delayRender } from "remotion";
const loadedFonts: { [key: string]: boolean } = {};
+export type Info = {
+ fontFamily: string;
+ importName: string;
+ version: string;
+ url: string;
+ unicodeRanges: Record<string, string>;
+ fonts: Record<string, Record<string, Record<string, string>>>;
+};
export const loadFonts = (
- meta: any,
- style: string,
- options: {
- weights: string[];
- subsets: string[];
+ meta: Info,
+ style?: string,
+ options?: {
+ weights?: string[];
+ subsets?: string[];
}
): { fontFamily: string } => {
- const fontFamily = meta.family.replace(/^['"]/g, "").replace(/['"]$/g, "");
- for (const weight of options.weights) {
- for (const subset of options.subsets) {
- // Get font url from meta
- let font = meta.fonts[style]?.[weight]?.[subset];
-
- // Check is font available in meta
- if (!font) {
+ const styles = style ? [style] : Object.keys(meta.fonts);
+ for (const style of styles) {
+ if (!meta.fonts[style]) {
+ throw new Error(
+ `The font ${meta.fontFamily} does not have a style ${style}`
+ );
+ }
+ const weights = options?.weights ?? Object.keys(meta.fonts[style].weights);
+ for (const weight of weights) {
+ if (!meta.fonts[style][weight]) {
throw new Error(
- `weight: ${weight} subset: ${subset} is not available for '${meta.family}'`
+ `The font ${meta.fontFamily} does not have a weight ${weight} in style ${style}`
);
}
+ const subsets =
+ options?.subsets ?? Object.keys(meta.fonts[style][weight]);
+ for (const subset of subsets) {
+ // Get font url from meta
+ let font = meta.fonts[style]?.[weight]?.[subset];
- // Check is font already loaded
- let fontKey = `${meta.family}-${style}-${weight}-${subset}`;
- if (loadedFonts[fontKey]) {
- continue;
- }
+ // Check is font available in meta
+ if (!font) {
+ throw new Error(
+ `weight: ${weight} subset: ${subset} is not available for '${meta.fontFamily}'`
+ );
+ }
- // Mark font as loaded
- loadedFonts[fontKey] = true;
+ // Check is font already loaded
+ let fontKey = `${meta.fontFamily}-${style}-${weight}-${subset}`;
+ if (loadedFonts[fontKey]) {
+ continue;
+ }
- const handle = delayRender(
- `Fetching ${meta.family} font ${JSON.stringify({
- style,
- weight,
- subset,
- })}`
- );
+ // Mark font as loaded
+ loadedFonts[fontKey] = true;
- // Create font-face
- const fontFace = new FontFace(
- fontFamily,
- `url(${font}) format('woff2')`,
- {
- weight: weight,
- style: style,
- unicodeRange: meta.unicodeRanges[subset],
- }
- );
+ const handle = delayRender(
+ `Fetching ${meta.fontFamily} font ${JSON.stringify({
+ style,
+ weight,
+ subset,
+ })}`
+ );
- // Load font-face
- fontFace
- .load()
- .then(() => {
- document.fonts.add(fontFace);
- continueRender(handle);
- })
- .catch((err) => {
- // Mark font as not loaded
- loadedFonts[fontKey] = false;
- throw err;
- });
+ // Create font-face
+ const fontFace = new FontFace(
+ meta.fontFamily,
+ `url(${font}) format('woff2')`,
+ {
+ weight: weight,
+ style: style,
+ unicodeRange: meta.unicodeRanges[subset],
+ }
+ );
+
+ // Load font-face
+ fontFace
+ .load()
+ .then(() => {
+ document.fonts.add(fontFace);
+ continueRender(handle);
+ })
+ .catch((err) => {
+ // Mark font as not loaded
+ loadedFonts[fontKey] = false;
+ throw err;
+ });
+ }
}
}
+
return {
- fontFamily,
+ fontFamily: meta.fontFamily,
};
};