Skip to content

🎥 Make videos programmatically with React

New fitText() API

Jonny Burger7a22daf7616b
diff --git a/packages/docs/components/TableOfContents/LayoutUtils.tsx b/packages/docs/components/TableOfContents/LayoutUtils.tsx
index 063f87bade..daa0bc0912 100644
--- a/packages/docs/components/TableOfContents/LayoutUtils.tsx
+++ b/packages/docs/components/TableOfContents/LayoutUtils.tsx
@@ -14,6 +14,10 @@ export const TableOfContents: React.FC = () => {
<strong>fillTextBox()</strong>
<div>Find line breaks and overflows in a text box</div>
</TOCItem>
+ <TOCItem link="/docs/layout-utils/fit-text">
+ <strong>fitText()</strong>
+ <div>Get font size to fit text in a box</div>
+ </TOCItem>
</Grid>
</div>
);
diff --git a/packages/docs/docs/layout-utils/fit-text.md b/packages/docs/docs/layout-utils/fit-text.md
new file mode 100644
index 0000000000..f233e32ec0
--- /dev/null
+++ b/packages/docs/docs/layout-utils/fit-text.md
@@ -0,0 +1,145 @@
+---
+image: /generated/articles-docs-layout-utils-fit-text.png
+title: fitText()
+crumb: "@remotion/layout-utils"
+---
+
+_Part of the [`@remotion/layout-utils`](/docs/layout-utils) package. Available from v4.0.88_
+
+Calculates the `font-size` needed to fit a text into a given width.
+
+```tsx twoslash title="FitText.tsx"
+import { fitText } from "@remotion/layout-utils";
+
+const text = "Hello World";
+const width = 100;
+const fontFamily = "Arial";
+const fontWeight = "bold";
+
+const { fontSize } = fitText({
+ text,
+ withinWidth: width,
+ fontFamily: fontFamily,
+ fontWeight: fontWeight,
+});
+
+// Example markup:
+<div style={{ fontSize, width, fontFamily, fontWeight }}>{text}</div>;
+```
+
+## API
+
+Accepts an object with the following properties:
+
+### `text`
+
+_string_
+
+The text to fit.
+
+### `withinWidth`
+
+_number_
+
+The width to fit the text into.
+
+:::info
+In the default Remotion stylesheet, borders shrink the container due to `box-sizing: border-box`.
+Subtract any borders, or use `outline` instead of `border`.
+:::
+
+### `fontFamily`
+
+_string_
+
+The `font-family` CSS property you are going to assign to the text.
+
+:::info
+The font needs to be loaded before this API is being called.
+If you use [`@remotion/google-fonts`](/docs/google-fonts), wait until [`waitUntilDone()`](/docs/google-fonts/load-font#waituntildone) resolves first.
+:::
+
+### `fontWeight`
+
+_string | number, optional_
+
+Pass this option if you are going to assign a `font-weight` CSS property to the text.
+
+### `letterSpacing`
+
+_string, optional_
+
+Pass this option if you are going to assign a `letter-spacing` CSS property to the text.
+
+### `fontVariantNumeric`
+
+_string, optional_
+
+Pass this option if you are going to assign a `font-variant-numeric` CSS property to the text.
+
+## Return value
+
+An object with `fontSize` in pixels. Assign this to the `style` prop of your text element.
+
+## Example
+
+```tsx twoslash
+import { fitText } from "@remotion/layout-utils";
+import React from "react";
+import { AbsoluteFill } from "remotion";
+
+const boxWidth = 600;
+// Must be loaded before calling fitText()
+const fontFamily = "Helvetica";
+const fontWeight = "bold";
+
+export const FitText: React.FC<{ text: string }> = ({ text }) => {
+ const fontSize = Math.min(
+ 80,
+ fitText({
+ fontFamily,
+ text,
+ withinWidth: boxWidth,
+ fontWeight,
+ }).fontSize,
+ );
+
+ return (
+ <AbsoluteFill
+ style={{
+ justifyContent: "center",
+ alignItems: "center",
+ backgroundColor: "white",
+ }}
+ >
+ <div
+ style={{
+ width: boxWidth,
+ outline: "1px dashed rgba(0, 0, 0, 0.5)",
+ height: 100,
+ fontSize,
+ fontWeight,
+ fontFamily,
+ display: "flex",
+ alignItems: "center",
+ }}
+ >
+ {text}
+ </div>
+ </AbsoluteFill>
+ );
+};
+```
+
+Notes:
+
+- A maximum font size of `80` was specified to prevent the text from becoming too large.
+- The `fontFamily` and `fontWeight` were passed to the `div` element to ensure that the text is rendered with the same font as the one used by `fitText()`.
+- The `outline` CSS property was used instead of `border`.
+ This is because in Remotion, the border is inside by default and shrinks the container, due to `box-sizing: border-box` being in the default stylesheet.
+
+## See also
+
+- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/layout-utils/src/layouts/fit-text.md)
+- [`@remotion/layout-utils`](/docs/layout-utils)
+- [`@remotion/google-fonts`](/docs/google-fonts)
diff --git a/packages/docs/docs/layout-utils/index.md b/packages/docs/docs/layout-utils/index.mdx
similarity index 93%
rename from packages/docs/docs/layout-utils/index.md
rename to packages/docs/docs/layout-utils/index.mdx
index d8a4403d07..374c8b4a41 100644
--- a/packages/docs/docs/layout-utils/index.md
+++ b/packages/docs/docs/layout-utils/index.mdx
@@ -3,10 +3,9 @@ image: /generated/articles-docs-layout-utils-index.png
title: "@remotion/layout-utils"
---
-
_Available from v4.0.50_
-Utility functions that create a remotion layout.
+Utility functions for creating dynamic layouts.
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 7ce2e3b4f6..179b28e404 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -461,7 +461,11 @@ module.exports = {
type: "doc",
id: "layout-utils/index",
},
- items: ["layout-utils/measure-text", "layout-utils/fill-text-box"],
+ items: [
+ "layout-utils/measure-text",
+ "layout-utils/fill-text-box",
+ "layout-utils/fit-text",
+ ],
},
],
mainSidebar: [
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index 00046fbadc..036999c0a6 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -1392,10 +1392,17 @@ export const articles = [
compId: "articles-docs-layout-utils-fill-text-box",
crumb: "@remotion/layout-utils",
},
+ {
+ id: "layout-utils/fit-text",
+ title: "fitText()",
+ relativePath: "docs/layout-utils/fit-text.md",
+ compId: "articles-docs-layout-utils-fit-text",
+ crumb: "@remotion/layout-utils",
+ },
{
id: "layout-utils/index",
title: "@remotion/layout-utils",
- relativePath: "docs/layout-utils/index.md",
+ relativePath: "docs/layout-utils/index.mdx",
compId: "articles-docs-layout-utils-index",
crumb: null,
},
diff --git a/packages/docs/static/generated/articles-docs-layout-utils-fit-text.png b/packages/docs/static/generated/articles-docs-layout-utils-fit-text.png
new file mode 100644
index 0000000000..10cd9d83be
Binary files /dev/null and b/packages/docs/static/generated/articles-docs-layout-utils-fit-text.png differ
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index c72485a77d..ed52d61870 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -62,6 +62,7 @@ import {TenFrameTester} from './TenFrameTester';
import ThreeBasic from './ThreeBasic';
import {VideoTextureDemo} from './ThreeScene/Scene';
import {Timeout} from './Timeout';
+import {FitText, fitTextSchema} from './Title/FitText';
import {AudioTransition} from './Transitions/AudioTransition';
import {BasicTransition} from './Transitions/BasicTransition';
import {CustomTransition} from './Transitions/CustomTransition';
@@ -438,6 +439,16 @@ export const Index: React.FC = () => {
line2: 'text',
}}
/>
+ <Still
+ id="FitText"
+ component={FitText}
+ width={800}
+ height={900}
+ schema={fitTextSchema}
+ defaultProps={{
+ line: 'Test',
+ }}
+ />
<Composition
id="beta-text"
component={BetaText}
diff --git a/packages/example/src/Title/FitText.tsx b/packages/example/src/Title/FitText.tsx
new file mode 100644
index 0000000000..9371a35065
--- /dev/null
+++ b/packages/example/src/Title/FitText.tsx
@@ -0,0 +1,53 @@
+import {fitText} from '@remotion/layout-utils';
+import React from 'react';
+import {AbsoluteFill} from 'remotion';
+import {z} from 'zod';
+
+const boxWidth = 600;
+// Must be loaded before calling fitText()
+const fontFamily = 'GT Planar';
+const fontWeight = 'bold';
+
+export const fitTextSchema = z.object({
+ line: z.string(),
+});
+
+export const FitText: React.FC<z.infer<typeof fitTextSchema>> = ({line}) => {
+ const fontSize = Math.min(
+ 80,
+ fitText({
+ fontFamily,
+ text: line,
+ withinWidth: boxWidth,
+ fontWeight,
+ }).fontSize,
+ );
+
+ return (
+ <AbsoluteFill
+ style={{
+ justifyContent: 'center',
+ alignItems: 'center',
+ backgroundColor: 'white',
+ }}
+ >
+ <div style={{backgroundColor: '#0B84F3', padding: 20}}>
+ <div
+ style={{
+ width: boxWidth,
+ outline: '1px dashed rgba(255, 255, 255, 0.5)',
+ height: 100,
+ fontSize,
+ fontWeight,
+ fontFamily,
+ display: 'flex',
+ alignItems: 'center',
+ color: 'white',
+ }}
+ >
+ {line}
+ </div>
+ </div>
+ </AbsoluteFill>
+ );
+};
diff --git a/packages/layout-utils/src/index.ts b/packages/layout-utils/src/index.ts
index 8acf7b8146..ce49ef8366 100644
--- a/packages/layout-utils/src/index.ts
+++ b/packages/layout-utils/src/index.ts
@@ -1,2 +1,3 @@
export {fillTextBox} from './layouts/fill-text-box';
+export {fitText} from './layouts/fit-text';
export {measureText} from './layouts/measure-text';
diff --git a/packages/layout-utils/src/layouts/fit-text.ts b/packages/layout-utils/src/layouts/fit-text.ts
new file mode 100644
index 0000000000..2967031456
--- /dev/null
+++ b/packages/layout-utils/src/layouts/fit-text.ts
@@ -0,0 +1,29 @@
+import {measureText} from '../layouts/measure-text';
+
+const sampleSize = 100;
+
+export const fitText = ({
+ text,
+ withinWidth,
+ fontFamily,
+ fontVariantNumeric,
+ fontWeight,
+ letterSpacing,
+}: {
+ text: string;
+ withinWidth: number;
+ fontFamily: string;
+ fontWeight?: number | string;
+ letterSpacing?: string;
+ fontVariantNumeric?: string;
+}) => {
+ const estimate = measureText({
+ text,
+ fontFamily,
+ fontSize: sampleSize,
+ fontWeight,
+ fontVariantNumeric,
+ letterSpacing,
+ });
+ return {fontSize: (withinWidth / estimate.width) * sampleSize};
+};