Skip to content

🎥 Make videos programmatically with React

update

Jonny Burger5011a1dcdb6a
diff --git a/packages/docs/docs/using-fonts.md b/packages/docs/docs/using-fonts.mdx
similarity index 76%
rename from packages/docs/docs/using-fonts.md
rename to packages/docs/docs/using-fonts.mdx
index b9cd375894..df05a4cf35 100644
--- a/packages/docs/docs/using-fonts.md
+++ b/packages/docs/docs/using-fonts.mdx
@@ -48,29 +48,32 @@ const MyComp: React.FC = () => {
_available from v4.0.164_
-Put the font into your `public/` folder.
+Put the font into your `public/` folder.
Put this somewhere in your app where it gets executed:
```tsx twoslash title="load-fonts.ts"
-import {loadFont} from "@remotion/fonts";
+import { loadFont } from "@remotion/fonts";
+import { staticFile } from "remotion";
+
+const fontFamily = "Inter";
loadFont({
- family: 'Font family name',
- url: 'Local font url',
- weight: '500',
- // ... other options
-}).then(() => console.log('Font loaded!'))
+ family: fontFamily,
+ url: staticFile("Inter-Regular.woff2"),
+ weight: "500",
+}).then(() => {
+ console.log("Font loaded!");
+});
```
-:::note
-Check the available options [around the URL](/docs/fonts/load-font).
-The font format should be one of `woff2` for WOFF2 files, `woff` for WOFF, `opentype` for OTF, `truetype` for TTF.
-:::
-
The font is now available for use:
```tsx twoslash title="MyComp.tsx"
-<div style={{ fontFamily: "Bangers" }}>Some text</div>
+const fontFamily = "Inter";
+
+// ---cut---
+
+<div style={{ fontFamily: fontFamily }}>Some text</div>;
```
:::info
diff --git a/packages/fonts/src/get-font-format.ts b/packages/fonts/src/get-font-format.ts
index 9f8f62f86c..dd864845ba 100644
--- a/packages/fonts/src/get-font-format.ts
+++ b/packages/fonts/src/get-font-format.ts
@@ -1,4 +1,4 @@
-type FontFormat = 'woff2' | 'woff' | 'opentype' | 'truetype';
+export type FontFormat = 'woff2' | 'woff' | 'opentype' | 'truetype';
export const getFontFormat = (url: string): FontFormat => {
const ext = url.split('.').pop()?.toLowerCase();
@@ -12,6 +12,6 @@ export const getFontFormat = (url: string): FontFormat => {
case 'ttf':
return 'truetype';
default:
- throw new Error(`Unsupported font format: ${ext}`);
+ throw new Error(`Unsupported font extension: ${ext}`);
}
};
diff --git a/packages/fonts/src/load-font.ts b/packages/fonts/src/load-font.ts
index bcbe7b2423..b32b2e46f4 100644
--- a/packages/fonts/src/load-font.ts
+++ b/packages/fonts/src/load-font.ts
@@ -1,4 +1,5 @@
-import {cancelRender, continueRender, delayRender, staticFile} from 'remotion';
+import {cancelRender, continueRender, delayRender} from 'remotion';
+import type {FontFormat} from './get-font-format';
import {getFontFormat} from './get-font-format';
type LoadFontOptions = {
@@ -14,6 +15,7 @@ type LoadFontOptions = {
unicodeRange?: string;
variant?: string;
weight?: string;
+ format?: FontFormat;
};
export const loadFont = async ({
@@ -28,25 +30,24 @@ export const loadFont = async ({
style,
unicodeRange,
weight,
+ format,
+ variant,
}: LoadFontOptions): Promise<void> => {
const waitForFont = delayRender();
try {
- const fontFormat = getFontFormat(url);
- const font = new FontFace(
- family,
- `url('${staticFile(url)}') format('${fontFormat}')`,
- {
- ascentOverride,
- descentOverride,
- display,
- featureSettings,
- lineGapOverride,
- stretch,
- style,
- unicodeRange,
- weight,
- },
- );
+ const fontFormat = format ?? getFontFormat(url);
+ const font = new FontFace(family, `url('${url}') format('${fontFormat}')`, {
+ ascentOverride,
+ descentOverride,
+ display,
+ featureSettings,
+ lineGapOverride,
+ stretch,
+ style,
+ unicodeRange,
+ weight,
+ variant,
+ });
await font.load();
document.fonts.add(font);
continueRender(waitForFont);