Skip to content

🎥 Make videos programmatically with React

feat: Add `fillTextBox` method in packages/layout-utils

lw47d1877e8380
diff --git a/packages/docs/components/TableOfContents/LayoutUtils.tsx b/packages/docs/components/TableOfContents/LayoutUtils.tsx
index b58ea06952..efb5b05686 100644
--- a/packages/docs/components/TableOfContents/LayoutUtils.tsx
+++ b/packages/docs/components/TableOfContents/LayoutUtils.tsx
@@ -10,6 +10,10 @@ export const TableOfContents: React.FC = () => {
<strong>measureText()</strong>
<div>Get dimensions of text</div>
</TOCItem>
+ <TOCItem link="/docs/layout-utils/fill-text-box">
+ <strong>fillTextBox()</strong>
+ <div>Calculate text is fill the box</div>
+ </TOCItem>
</Grid>
</div>
);
diff --git a/packages/docs/docs/layout-utils/fill-text-box.md b/packages/docs/docs/layout-utils/fill-text-box.md
new file mode 100644
index 0000000000..cc487ed0db
--- /dev/null
+++ b/packages/docs/docs/layout-utils/fill-text-box.md
@@ -0,0 +1,86 @@
+---
+image: /generated/articles-docs-layout-utils-fill-text-box.png
+title: fillTextBox()
+crumb: "Make some"
+---
+
+_Part of the [`@remotion/layout-utils`](/docs/layout-utils) package._
+
+Calculate whether the text exceeds the box and wraps within the container. Only works in the browser, not in Node.js or Bun.
+
+## API
+
+The function takes the following options:
+
+### `maxBoxWidth`
+
+_Required_ _number_
+
+The max box width, unit `px`.
+
+### `maxLines`
+
+_Required_ _number_
+
+The max lines of the box.
+
+## Return value
+
+An object with an `add` method, which can be used to add words to the text box.
+
+### `add`
+
+The function takes the following options:
+
+#### `text`
+
+_Required_ _string_
+
+Any string.
+
+#### `fontFamily`
+
+_Required_ _string_
+
+Same as CSS style `font-family`.
+
+#### `fontSize`
+
+_Required_ _number_
+
+Same as CSS style `font-size`. Only _numbers_ allowed, unit `px`.
+
+#### `fontWeight`
+
+_string_
+
+Same as CSS style `font-weight`.
+
+### Return value
+
+The add method returns an object with two properties:
+
++ `exceedsBox`:
+ Boolean value, indicating whether adding the word would cause the text to exceed the width of the box (`maxBoxWidth`).
++ `newLine`:
+ Boolean value, indicating whether adding the word would require starting a new line in the text box.
+
+## Example
+
+```tsx twoslash
+import { fillTextBox } from "@remotion/layout-utils";
+
+const box = fillTextBox({maxLines: 4, maxBoxWidth: 100})
+box.add({text: 'Hello'}) // {exceedsBox: false, newLine: false}
+box.add({text: 'World!'}) // {exceedsBox: false, newLine: false}
+// Doesn't fit on the previous line anymore
+box.add({text: 'How'}) // {exceedsBox: false, newLine: true}
+// ...
+// Doesn't fix in the box anymore
+box.add({text: 'the end'}) // {exceedsBox: true, newLine: false}
+```
+
+## See also
+
+- [measureText()](/docs/layout-utils/measure-text)
+- [`@remotion/layout-utils`](/docs/layout-utils)
diff --git a/packages/layout-utils/src/index.ts b/packages/layout-utils/src/index.ts
index d2304f59ab..27e37b3f28 100644
--- a/packages/layout-utils/src/index.ts
+++ b/packages/layout-utils/src/index.ts
@@ -1 +1,2 @@
-export {measureText} from './layouts/measure-text';
+export { measureText } from "./layouts/measure-text";
+export { fillTextBox } from "./layouts/fill-text-box";
diff --git a/packages/layout-utils/src/layouts/fill-text-box.ts b/packages/layout-utils/src/layouts/fill-text-box.ts
new file mode 100644
index 0000000000..33344a556f
--- /dev/null
+++ b/packages/layout-utils/src/layouts/fill-text-box.ts
@@ -0,0 +1,69 @@
+import { measureText } from "./measure-text";
+
+type Word = {
+ text: string;
+ fontFamily: string;
+ fontWeight: string | number;
+ fontSize: number;
+};
+
+export const fillTextBox = ({
+ maxBoxWidth,
+ maxLines
+}: {
+ maxBoxWidth: number;
+ maxLines: number;
+}) => {
+ const lines: Word[][] = new Array(maxLines).fill(0).map(() => [] as Word[]);
+
+ return {
+ add: ({
+ text,
+ fontFamily,
+ fontWeight,
+ fontSize
+ }: {
+ text: string;
+ fontFamily: string;
+ fontWeight: string | number;
+ fontSize: number;
+ }): {
+ exceedsBox: boolean;
+ newLine: boolean;
+ } => {
+ const lastLineIndex = lines.findLastIndex(l => l.length > 0);
+ const currentlyAt = lastLineIndex === -1 ? 0 : lastLineIndex;
+ const lineToUse = lines[currentlyAt];
+
+ const lineWithWord: Word[] = [
+ ...lineToUse,
+ { text, fontFamily, fontWeight, fontSize }
+ ];
+
+ const widths = lineWithWord.map(w => {
+ return measureText(w).width;
+ });
+ const lineWidthWithWordAdded = widths.reduce((a, b) => a + b, 0);
+
+ if (lineWidthWithWordAdded <= maxBoxWidth) {
+ lines[currentlyAt].push({
+ text: lines[currentlyAt].length === 0 ? text.trimStart() : text,
+ fontFamily,
+ fontWeight,
+ fontSize
+ });
+
+ return { exceedsBox: false, newLine: false };
+ }
+
+ if (currentlyAt === maxLines - 1) {
+ return { exceedsBox: true, newLine: false };
+ }
+
+ lines[currentlyAt + 1] = [
+ { text: text.trimStart(), fontFamily, fontWeight, fontSize }
+ ];
+ return { exceedsBox: false, newLine: true };
+ }
+ };
+};