Skip to content

🎥 Make videos programmatically with React

`@remotion/google-fonts`: Allow to await until font is loaded

Jonny Burger3e01b5ef47e5
diff --git a/packages/docs/docs/google-fonts/load-font.md b/packages/docs/docs/google-fonts/load-font.md
index d3bd9a688f..06b85928ee 100644
--- a/packages/docs/docs/google-fonts/load-font.md
+++ b/packages/docs/docs/google-fonts/load-font.md
@@ -129,6 +129,23 @@ Unicode range information about the font.
}
```
+### `waitUntilDone`<AvailableFrom v="4.0.83"/>
+
+A function that returns a promise that resolves when the font is loaded.
+
+```tsx title="Load a font in the background"
+import { loadFont } from "@remotion/google-fonts/Lobster";
+
+loadFont("normal", {
+ weights: ["400"],
+ subsets: ["latin"],
+})
+ .waitUntilDone()
+ .then(() => {
+ console.log("Done loading");
+ });
+```
+
## See also
- [Fonts](/docs/fonts)
diff --git a/packages/google-fonts/src/base.ts b/packages/google-fonts/src/base.ts
index eb6897f7cb..497a507f37 100644
--- a/packages/google-fonts/src/base.ts
+++ b/packages/google-fonts/src/base.ts
@@ -31,7 +31,9 @@ export const loadFonts = (
fontFamily: FontInfo["fontFamily"];
fonts: FontInfo["fonts"];
unicodeRanges: FontInfo["unicodeRanges"];
+ waitUntilDone: () => Promise<undefined>;
} => {
+ const promises: Promise<void>[] = [];
const styles = style ? [style] : Object.keys(meta.fonts);
for (const style of styles) {
// Don't load fonts on server
@@ -93,7 +95,7 @@ export const loadFonts = (
);
// Load font-face
- fontFace
+ const promise = fontFace
.load()
.then(() => {
(options?.document ?? document).fonts.add(fontFace);
@@ -104,6 +106,8 @@ export const loadFonts = (
loadedFonts[fontKey] = false;
throw err;
});
+
+ promises.push(promise);
}
}
}
@@ -112,5 +116,6 @@ export const loadFonts = (
fontFamily: meta.fontFamily,
fonts: meta.fonts,
unicodeRanges: meta.unicodeRanges,
+ waitUntilDone: () => Promise.all<void>(promises).then(() => undefined),
};
};