Skip to content

🎥 Make videos programmatically with React

Add loadVariableFontFromInfo()

JonnyBurger00078489aa17
diff --git a/packages/docs/docs/google-fonts/TableOfContents.tsx b/packages/docs/docs/google-fonts/TableOfContents.tsx
index 06222fd92a..dd5c94f1d1 100644
--- a/packages/docs/docs/google-fonts/TableOfContents.tsx
+++ b/packages/docs/docs/google-fonts/TableOfContents.tsx
@@ -10,6 +10,10 @@ export const TableOfContents: React.FC = () => {
<strong>loadFont()</strong>
<div>Load a Google Font</div>
</TOCItem>
+ <TOCItem link="/docs/google-fonts/load-variable-font">
+ <strong>loadVariableFont()</strong>
+ <div>Load a variable Google Font</div>
+ </TOCItem>
<TOCItem link="/docs/google-fonts/get-available-fonts">
<strong>getAvailableFonts()</strong>
<div>Static list of available fonts</div>
@@ -22,6 +26,10 @@ export const TableOfContents: React.FC = () => {
<strong>loadFontFromInfo()</strong>
<div>Load a Google Font based on metadata</div>
</TOCItem>
+ <TOCItem link="/docs/google-fonts/load-variable-font-from-info">
+ <strong>loadVariableFontFromInfo()</strong>
+ <div>Load a variable Google Font based on metadata</div>
+ </TOCItem>
</Grid>
</div>
);
diff --git a/packages/docs/docs/google-fonts/get-info.mdx b/packages/docs/docs/google-fonts/get-info.mdx
index e82ad9c76e..5e386d782e 100644
--- a/packages/docs/docs/google-fonts/get-info.mdx
+++ b/packages/docs/docs/google-fonts/get-info.mdx
@@ -41,7 +41,37 @@ console.log(getInfo());
}
```
+## Variable font information
+
+### variable?<AvailableFrom v="4.0.524" />
+
+For fonts that support variable axes, this property contains the variable font metadata. It is omitted for non-variable fonts.
+
+```tsx twoslash title="Read variable font axes"
+import {getInfo} from '@remotion/google-fonts/NotoSans';
+
+const variable = getInfo().variable;
+
+if (variable) {
+ console.log(variable.axes.wght);
+ // {min: 100, max: 900}
+}
+```
+
+#### axes
+
+An object containing the minimum and maximum value of each variation axis.
+
+#### fontFaces
+
+The style, weight range, stretch range, character subset, Unicode range and source URL of each variable font face.
+
+#### url
+
+The Google Fonts CSS URL used to retrieve the variable font faces.
+
## See also
- [Fonts](/docs/fonts)
- [`@remotion/google-fonts`](/docs/google-fonts)
+- [`loadVariableFont()`](/docs/google-fonts/load-variable-font)
diff --git a/packages/docs/docs/google-fonts/load-variable-font-from-info.mdx b/packages/docs/docs/google-fonts/load-variable-font-from-info.mdx
new file mode 100644
index 0000000000..3b471abc81
--- /dev/null
+++ b/packages/docs/docs/google-fonts/load-variable-font-from-info.mdx
@@ -0,0 +1,69 @@
+---
+image: /generated/articles-docs-google-fonts-load-variable-font-from-info.png
+title: loadVariableFontFromInfo()
+crumb: '@remotion/google-fonts'
+---
+
+# loadVariableFontFromInfo()<AvailableFrom v="4.0.524" />
+
+Loads a variable Google Font from metadata obtained using [`getInfo()`](/docs/google-fonts/get-info).
+
+Because the metadata is pure JSON, it can be loaded from a server without including the generated font module in the client bundle.
+
+## Usage
+
+```tsx twoslash title="Load a variable font from metadata"
+import {getInfo} from '@remotion/google-fonts/NotoSans';
+import {loadVariableFontFromInfo} from '@remotion/google-fonts/from-info';
+
+const info = getInfo();
+
+const {axes, fontFamily, waitUntilDone} = loadVariableFontFromInfo(
+ info,
+ 'normal',
+ {
+ subsets: ['latin'],
+ },
+);
+
+console.log(fontFamily, axes.wght);
+waitUntilDone();
+```
+
+## Arguments
+
+### info
+
+Font metadata returned by [`getInfo()`](/docs/google-fonts/get-info). The font must include variable font information.
+
+### style
+
+The variable font style to load, such as `normal` or `italic`.
+
+### options
+
+#### subsets
+
+The character subsets to load. At least one subset is required.
+
+#### document?
+
+The document into which the font should be added. By default, the global `window.document` is used.
+
+#### ignoreTooManyRequestsWarning?
+
+Set this to `true` to disable the warning emitted when loading more than 20 subset files.
+
+## Return value
+
+The return value is the same as [`loadVariableFont()`](/docs/google-fonts/load-variable-font).
+
+## Compatibility
+
+<CompatibilityTable chrome firefox safari nodejs="" bun="" serverlessFunctions="" clientSideRendering={false} serverSideRendering player studio hideServers />
+
+## See also
+
+- [`loadVariableFont()`](/docs/google-fonts/load-variable-font)
+- [`loadFontFromInfo()`](/docs/google-fonts/load-font-from-info)
+- [`getInfo()`](/docs/google-fonts/get-info)
diff --git a/packages/docs/docs/google-fonts/load-variable-font.mdx b/packages/docs/docs/google-fonts/load-variable-font.mdx
index 7e55b44e8a..48c2b684b9 100644
--- a/packages/docs/docs/google-fonts/load-variable-font.mdx
+++ b/packages/docs/docs/google-fonts/load-variable-font.mdx
@@ -83,5 +83,6 @@ Returns a promise that resolves once the font files have loaded.
## See also
+- [`loadVariableFontFromInfo()`](/docs/google-fonts/load-variable-font-from-info)
- [`loadFont()`](/docs/google-fonts/load-font)
- [`@remotion/google-fonts`](/docs/google-fonts)
diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.ts
index 976a94016e..926f6d55f0 100644
--- a/packages/docs/sidebars.ts
+++ b/packages/docs/sidebars.ts
@@ -397,6 +397,7 @@ const sidebars: SidebarsConfig = {
'google-fonts/get-available-fonts',
'google-fonts/get-info',
'google-fonts/load-font-from-info',
+ 'google-fonts/load-variable-font-from-info',
],
},
{
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index 6293bbc658..cf441d5e14 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -2834,6 +2834,15 @@ export const articles = [
noAi: false,
slug: 'google-fonts/load-font',
},
+ {
+ id: 'google-fonts/load-variable-font-from-info',
+ title: 'loadVariableFontFromInfo()',
+ relativePath: 'docs/google-fonts/load-variable-font-from-info.mdx',
+ compId: 'articles-docs-google-fonts-load-variable-font-from-info',
+ crumb: '@remotion/google-fonts',
+ noAi: false,
+ slug: 'google-fonts/load-variable-font-from-info',
+ },
{
id: 'google-fonts/load-variable-font',
title: 'loadVariableFont()',
diff --git a/packages/docs/static/generated/articles-docs-google-fonts-load-variable-font-from-info.png b/packages/docs/static/generated/articles-docs-google-fonts-load-variable-font-from-info.png
new file mode 100644
index 0000000000..71dc2fd274
Binary files /dev/null and b/packages/docs/static/generated/articles-docs-google-fonts-load-variable-font-from-info.png differ
diff --git a/packages/google-fonts/src/from-info.ts b/packages/google-fonts/src/from-info.ts
index 4a4c4d89ca..9cef9b65ea 100644
--- a/packages/google-fonts/src/from-info.ts
+++ b/packages/google-fonts/src/from-info.ts
@@ -1,3 +1,4 @@
-import {loadFonts} from './base';
+import {loadFonts, loadVariableFonts} from './base';
export const loadFontFromInfo = loadFonts;
+export const loadVariableFontFromInfo = loadVariableFonts;
diff --git a/packages/google-fonts/test/variable-font.test.ts b/packages/google-fonts/test/variable-font.test.ts
index 2cde91ec62..94fcbeae19 100644
--- a/packages/google-fonts/test/variable-font.test.ts
+++ b/packages/google-fonts/test/variable-font.test.ts
@@ -10,11 +10,12 @@ mock.module('remotion/no-react', () => ({
},
}));
-test('loads a variable font face with its weight range', async () => {
- const {loadVariableFont} = await import('../src/NotoSans');
+test('loads variable font faces from a generated module and font info', async () => {
+ const {getInfo, loadVariableFont} = await import('../src/NotoSans');
+ const {loadVariableFontFromInfo} = await import('../src/from-info');
const originalFontFace = globalThis.FontFace;
- let descriptors: FontFaceDescriptors | null = null;
- let addedFontFace: FontFace | null = null;
+ const descriptors: FontFaceDescriptors[] = [];
+ const addedFontFaces: FontFace[] = [];
class MockFontFace {
status: FontFaceLoadStatus = 'unloaded';
@@ -24,7 +25,9 @@ test('loads a variable font face with its weight range', async () => {
_source: string,
fontDescriptors?: FontFaceDescriptors,
) {
- descriptors = fontDescriptors ?? null;
+ if (fontDescriptors) {
+ descriptors.push(fontDescriptors);
+ }
}
load() {
@@ -39,7 +42,7 @@ test('loads a variable font face with its weight range', async () => {
const document = {
fonts: {
add: (fontFace: FontFace) => {
- addedFontFace = fontFace;
+ addedFontFaces.push(fontFace);
},
},
} as unknown as Document;
@@ -52,14 +55,31 @@ test('loads a variable font face with its weight range', async () => {
expect(loaded.axes.wdth).toEqual({min: 62.5, max: 100});
expect(loaded.axes.wght).toEqual({min: 100, max: 900});
await loaded.waitUntilDone();
- expect(descriptors).toEqual({
- stretch: '62.5% 100%',
- style: 'normal',
- unicodeRange:
- 'U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD',
- weight: '100 900',
+ const loadedFromInfo = loadVariableFontFromInfo(getInfo(), 'italic', {
+ document,
+ subsets: ['latin'],
});
- expect(addedFontFace).toBeInstanceOf(MockFontFace);
+ await loadedFromInfo.waitUntilDone();
+
+ expect(descriptors).toEqual([
+ {
+ stretch: '62.5% 100%',
+ style: 'normal',
+ unicodeRange:
+ 'U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD',
+ weight: '100 900',
+ },
+ {
+ stretch: '62.5% 100%',
+ style: 'italic',
+ unicodeRange:
+ 'U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD',
+ weight: '100 900',
+ },
+ ]);
+ expect(addedFontFaces).toHaveLength(2);
+ expect(addedFontFaces[0]).toBeInstanceOf(MockFontFace);
+ expect(addedFontFaces[1]).toBeInstanceOf(MockFontFace);
} finally {
globalThis.FontFace = originalFontFace;
}