🎥 Make videos programmatically with React
Add fill-text-box in `package/layout-utils`
willliud574ae77e621
diff --git a/packages/docs/docs/layout-utils/fill-text-box.md b/packages/docs/docs/layout-utils/fill-text-box.mdindex cc487ed0db..0096587f31 100644--- a/packages/docs/docs/layout-utils/fill-text-box.md+++ b/packages/docs/docs/layout-utils/fill-text-box.md@@ -4,6 +4,8 @@ title: fillTextBox()crumb: "Make some"---+_Available from v4.0.54_+_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.@@ -30,7 +32,7 @@ An object with an `add` method, which can be used to add words to the text box.### `add`-The function takes the following options:+Arguments:#### `text`@@ -61,23 +63,26 @@ Same as CSS style `font-weight`.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`).+ _Boolean_, whether adding the word would cause the text to exceed the width of the box.+ `newLine`:- Boolean value, indicating whether adding the word would require starting a new line in the text box.+ _Boolean_, whether adding the word would require starting a new line in the text box.## Example```tsx twoslashimport { fillTextBox } from "@remotion/layout-utils";+const fontFamily = "Arial";+const fontSize = 12;+const box = fillTextBox({maxLines: 4, maxBoxWidth: 100})-box.add({text: 'Hello'}) // {exceedsBox: false, newLine: false}-box.add({text: 'World!'}) // {exceedsBox: false, newLine: false}+box.add({text: 'Hello', fontFamily, fontSize}) // {exceedsBox: false, newLine: false}+box.add({text: 'World!', fontFamily, fontSize}) // {exceedsBox: false, newLine: false}// Doesn't fit on the previous line anymore-box.add({text: 'How'}) // {exceedsBox: false, newLine: true}+box.add({text: 'How', fontFamily, fontSize}) // {exceedsBox: false, newLine: true}// ...// Doesn't fix in the box anymore-box.add({text: 'the end'}) // {exceedsBox: true, newLine: false}+box.add({text: 'the end', fontFamily, fontSize}) // {exceedsBox: true, newLine: false}```## See alsodiff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.jsindex 75a5574d99..087dab3360 100644--- a/packages/docs/sidebars.js+++ b/packages/docs/sidebars.js@@ -445,7 +445,10 @@ module.exports = {type: "doc",id: "layout-utils/index",},- items: ["layout-utils/measure-text"],+ items: [+ "layout-utils/measure-text",+ "layout-utils/fill-text-box",+ ],},],mainSidebar: [diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.tsindex e862b12c04..08cee2838d 100644--- a/packages/docs/src/data/articles.ts+++ b/packages/docs/src/data/articles.ts@@ -1378,6 +1378,13 @@ export const articles = [compId: "articles-docs-layout-utils-measure-text",crumb: "Make some",},+ {+ id: "layout-utils/fill-text-box",+ title: "fillTextBox()",+ relativePath: "docs/layout-utils/fill-text-box.md",+ compId: "articles-docs-layout-utils-fill-text-box",+ crumb: "Make some",+ },{id: "legacy-babel",title: "Using legacy Babel transpilation",diff --git a/packages/example/src/Title/index.tsx b/packages/example/src/Title/index.tsxindex 5657d3aa9a..ba3246ce33 100644--- a/packages/example/src/Title/index.tsx+++ b/packages/example/src/Title/index.tsx@@ -1,4 +1,4 @@-import {measureText} from '@remotion/layout-utils';+import {fillTextBox, measureText} from '@remotion/layout-utils';import React from 'react';import {spring, SpringConfig, useCurrentFrame, useVideoConfig} from 'remotion';@@ -24,7 +24,14 @@ export const Title: React.FC<{});const secondWord = spring({config: springConfig,- frame: Math.max(0, frame - 5),+ frame: Math.max(0, frame - 4),+ from: 0,+ to: 1,+ fps,+ });+ const backGroundWord = spring({+ config: springConfig,+ frame: Math.max(0, frame - 8),from: 0,to: 1,fps,@@ -35,10 +42,16 @@ export const Title: React.FC<{fontFamily: 'SF Pro Text',};const {width, height} = measureText({- text: line1,+ text: `${line1} ${line2}`,fontFamily: fontStyle.fontFamily,fontSize: fontStyle.fontSize,});+ const box = fillTextBox({maxBoxWidth: 270, maxLines: 1});+ const fillTextResult = box.add({+ text: `${line1} ${line2}`,+ ...fontStyle,+ });+return (<divstyle={{@@ -50,23 +63,49 @@ export const Title: React.FC<{textAlign: 'center',}}>- <div style={fontStyle}>- <span+ <div style={{...fontStyle, position: 'relative'}}>+ <div style={{zIndex: 1, position: 'relative'}}>+ <span+ style={{+ display: 'inline-block',+ transform: `scale(${firstWord})`,+ }}+ >+ {line1}+ </span>+ <span+ style={{transform: `scale(${secondWord})`, display: 'inline-block'}}+ >+ {' '}+ {line2}+ </span>+ </div>+ <divstyle={{- display: 'inline-block',- transform: `scale(${firstWord})`,- width,- height,+ transform: `scale(${backGroundWord})`,+ display: fillTextResult.newLine ? 'none' : 'block',}}>- {line1}- </span>- <span- style={{transform: `scale(${secondWord})`, display: 'inline-block'}}- >- {' '}- {line2}- </span>+ <span+ style={{+ display: 'inline-block',+ color: '#eee',+ opacity: 0.7,+ fontSize: 100,+ fontStyle: 'oblique',+ position: 'absolute',+ transform: 'translateX(2px)',+ top: -100,+ left: 0,+ right: 0,+ width,+ height,+ zIndex: -1,+ }}+ >+ {line1} {line2}+ </span>+ </div></div></div>);diff --git a/packages/layout-utils/src/layouts/fill-text-box.ts b/packages/layout-utils/src/layouts/fill-text-box.tsindex eec223254a..77bf74bea3 100644--- a/packages/layout-utils/src/layouts/fill-text-box.ts+++ b/packages/layout-utils/src/layouts/fill-text-box.ts@@ -1,69 +1,73 @@-import { measureText } from "./measure-text";+import {measureText} from './measure-text';type Word = {- text: string;- fontFamily: string;- fontWeight: string | number;- fontSize: number;+ text: string;+ fontFamily: string;+ fontSize: number;+ fontWeight?: string | number;};export const fillTextBox = ({- maxBoxWidth,- maxLines+ maxBoxWidth,+ maxLines,}: {- maxBoxWidth: number;- maxLines: number;+ maxBoxWidth: number;+ maxLines: number;}) => {- const lines: Word[][] = new Array(maxLines).fill(0).map(() => [] as Word[]);+ 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.reverse().findIndex(l => l.length > 0);- const currentlyAt = lastLineIndex === -1 ? 0 : lastLineIndex;- const lineToUse = lines[currentlyAt];+ return {+ add: ({+ text,+ fontFamily,+ fontWeight,+ fontSize,+ }: {+ text: string;+ fontFamily: string;+ fontWeight: string | number;+ fontSize: number;+ }): {+ exceedsBox: boolean;+ newLine: boolean;+ } => {+ const lastLineIndex = lines.reduceRight((acc, curr, index) => {+ if (acc === -1 && curr.length > 0) {+ return index;+ }- const lineWithWord: Word[] = [- ...lineToUse,- { text, fontFamily, fontWeight, fontSize }- ];+ return acc;+ }, -1);+ const currentlyAt = lastLineIndex === -1 ? 0 : lastLineIndex;+ const lineToUse = lines[currentlyAt];- const widths = lineWithWord.map(w => {- return measureText(w).width;- });- const lineWidthWithWordAdded = widths.reduce((a, b) => a + b, 0);+ const lineWithWord: Word[] = [+ ...lineToUse,+ {text, fontFamily, fontWeight, fontSize},+ ];- if (lineWidthWithWordAdded <= maxBoxWidth) {- lines[currentlyAt].push({- text: lines[currentlyAt].length === 0 ? text.trimStart() : text,- fontFamily,- fontWeight,- fontSize- });+ const widths = lineWithWord.map((w) => measureText(w).width);+ const lineWidthWithWordAdded = widths.reduce((a, b) => a + b, 0);- return { exceedsBox: false, newLine: false };- }+ if (lineWidthWithWordAdded <= maxBoxWidth) {+ lines[currentlyAt].push({+ text: lines[currentlyAt].length === 0 ? text.trimStart() : text,+ fontFamily,+ fontWeight,+ fontSize,+ });- if (currentlyAt === maxLines - 1) {- return { exceedsBox: true, newLine: false };- }+ return {exceedsBox: false, newLine: false};+ }- lines[currentlyAt + 1] = [- { text: text.trimStart(), fontFamily, fontWeight, fontSize }- ];- return { exceedsBox: false, newLine: true };- }- };+ if (currentlyAt === maxLines - 1) {+ return {exceedsBox: true, newLine: false};+ }++ lines[currentlyAt + 1] = [+ {text: text.trimStart(), fontFamily, fontWeight, fontSize},+ ];+ return {exceedsBox: false, newLine: true};+ },+ };};