🎥 Make videos programmatically with React
new algo
Jonny Burgerf3885bd1719b
diff --git a/packages/web-renderer/src/drawing/text/draw-text.ts b/packages/web-renderer/src/drawing/text/draw-text.tsindex 78d9c57f84..3ee4cf0d9c 100644--- a/packages/web-renderer/src/drawing/text/draw-text.ts+++ b/packages/web-renderer/src/drawing/text/draw-text.ts@@ -3,7 +3,6 @@ import {Internals} from 'remotion';import type {DrawFn} from '../drawn-fn';import {applyTextTransform} from './apply-text-transform';import {findLineBreaks} from './find-line-breaks.text';-import {getCollapsedText} from './get-collapsed-text';export const drawText = ({span,@@ -57,8 +56,7 @@ export const drawText = ({contextToDraw.textBaseline = 'alphabetic';const originalText = span.textContent;- const {collapsedText} = getCollapsedText(span);- const transformedText = applyTextTransform(collapsedText, textTransform);+ const transformedText = applyTextTransform(originalText, textTransform);span.textContent = transformedText;// For RTL text, fill from the right edge instead of leftdiff --git a/packages/web-renderer/src/drawing/text/find-line-breaks.text.ts b/packages/web-renderer/src/drawing/text/find-line-breaks.text.tsindex 17963b964e..28cf082eef 100644--- a/packages/web-renderer/src/drawing/text/find-line-breaks.text.ts+++ b/packages/web-renderer/src/drawing/text/find-line-breaks.text.ts@@ -39,6 +39,7 @@ export function findLineBreaks(}> {const textNode = span.childNodes[0] as Text;const originalText = textNode.textContent;+ textNode.textContent = '';const originalRect = span.getBoundingClientRect();const segmenter = new Intl.Segmenter('en', {granularity: 'word'});@@ -46,7 +47,6 @@ export function findLineBreaks(const words = Array.from(segments).map((s) => s.segment);- // If the text would be centered in a flexbox containerconst lines: Array<{text: string;height: number;@@ -57,37 +57,41 @@ export function findLineBreaks(let testText = '';let previousRect: DOMRect = originalRect;- textNode.textContent = '';-for (let i = 0; i < words.length; i += 1) {const word = words[i];- testText += word;let wordsToAdd = word;- while (typeof words[i + 1] !== 'undefined' && words[i + 1].trim() === '') {- testText += words[i + 1];+ while (typeof words[i + 1] !== 'undefined' && wordsToAdd.trim() === '') {wordsToAdd += words[i + 1];i++;}previousRect = span.getBoundingClientRect();+ testText += wordsToAdd;+ const previousText = textNode.textContent;textNode.textContent = testText;- const {collapsedText, leading, trailing} = getCollapsedText(span);- textNode.textContent = collapsedText;const rect = span.getBoundingClientRect();const currentHeight = rect.height;- // If height changed significantly, we had a line break- if (+ const isLineBreak =previousRect &&previousRect.height !== 0 &&- Math.abs(currentHeight - previousRect.height) > 2- ) {+ Math.abs(currentHeight - previousRect.height) > 2;++ const {collapsedText} = getCollapsedText({+ span,+ previousText,+ wordsToAdd,+ canCollapseStart: isLineBreak || lines.length === 0,+ });++ // If height changed significantly, we had a line break+ if (isLineBreak) {const offsetHorizontal = rtl? previousRect.right! - originalRect.right: previousRect.left! - originalRect.left;let textForPreviousLine = currentLine;- let textForNewLine = wordsToAdd;+ let textForNewLine = collapsedText;// If the segment that triggered the break can't start a line (e.g., punctuation),// the browser would have moved the preceding word to the new line as well@@ -111,28 +115,19 @@ export function findLineBreaks(.slice(0, lastWordIndex).join('');textForNewLine =- currentLineSegments.slice(lastWordIndex).join('') + wordsToAdd;+ currentLineSegments.slice(lastWordIndex).join('') + collapsedText;}}- let effectiveText = textForPreviousLine;- if (leading) {- effectiveText = effectiveText.trimStart();- }-- if (trailing) {- effectiveText = effectiveText.trimEnd();- }-lines.push({- text: effectiveText,+ text: textForPreviousLine,height: currentHeight - previousRect.height,- offsetHorizontal,+ offsetHorizontal: 0,});currentLine = textForNewLine;} else {- currentLine += wordsToAdd;+ currentLine += collapsedText;}}@@ -152,12 +147,13 @@ export function findLineBreaks(lines.push({text: currentLine,height: rect.height - lines.reduce((acc, curr) => acc + curr.height, 0),- offsetHorizontal,+ offsetHorizontal: 0,});}// Reset to original texttextNode.textContent = originalText;+ console.log({lines});return lines;}diff --git a/packages/web-renderer/src/drawing/text/get-collapsed-text.ts b/packages/web-renderer/src/drawing/text/get-collapsed-text.tsindex 7009d3ca9c..c9942efc12 100644--- a/packages/web-renderer/src/drawing/text/get-collapsed-text.ts+++ b/packages/web-renderer/src/drawing/text/get-collapsed-text.ts@@ -1,10 +1,16 @@-export const getCollapsedText = (- span: HTMLSpanElement,-): {- collapsedText: string;- leading: boolean;- trailing: boolean;-} => {+const ZERO_WIDTH_SPACE = '\u200B';++export const getCollapsedText = ({+ span,+ previousText,+ wordsToAdd,+ canCollapseStart,+}: {+ wordsToAdd: string;+ span: HTMLSpanElement;+ previousText: string;+ canCollapseStart: boolean;+}) => {const textNode = span.firstChild;if (!textNode || textNode.nodeType !== Node.TEXT_NODE) {@@ -12,58 +18,51 @@ export const getCollapsedText = (}const originalText = (textNode as Text).textContent || '';- let collapsedText = originalText;- // Helper to measure width- const measureWidth = (text: string): number => {- (textNode as Text).textContent = text;- return span.getBoundingClientRect().width;+ const measureBox = (textToAddCandidate: string) => {+ (textNode as Text).textContent = previousText + textToAddCandidate;+ return span.getBoundingClientRect();};- const originalWidth = measureWidth(originalText);-- let leading = false;- let trailing = false;+ const measureWidth = (textToAddCandidate: string): number => {+ return measureBox(textToAddCandidate).width;+ };// Test leading whitespace- if (/^\s/.test(collapsedText)) {- const trimmedLeading = collapsedText.replace(/^\s+/, '');- const newWidth = measureWidth(trimmedLeading);+ if (/^\s/.test(wordsToAdd) && canCollapseStart) {+ const currentWidth = measureWidth(wordsToAdd);+ const newWidth = measureWidth(ZERO_WIDTH_SPACE + wordsToAdd.trimStart());- if (newWidth === originalWidth) {+ if (newWidth === currentWidth) {// Whitespace was collapsed by the browser- collapsedText = trimmedLeading;- leading = true;+ wordsToAdd = wordsToAdd.trimStart();}}// Test trailing whitespace (on current collapsed text)- if (/\s$/.test(collapsedText)) {- const currentWidth = measureWidth(collapsedText);- const trimmedTrailing = collapsedText.replace(/\s+$/, '');- const newWidth = measureWidth(trimmedTrailing);+ if (/\s$/.test(wordsToAdd)) {+ const currentWidth = measureWidth(wordsToAdd);+ const newWidth = measureWidth(wordsToAdd.trimEnd() + ZERO_WIDTH_SPACE);if (newWidth === currentWidth) {// Whitespace was collapsed by the browser- collapsedText = trimmedTrailing;- trailing = true;+ wordsToAdd = wordsToAdd.trimEnd();}}// Test internal duplicate whitespace (on current collapsed text)- if (/\s\s/.test(collapsedText)) {- const currentWidth = measureWidth(collapsedText);- const collapsedInternal = collapsedText.replace(/\s\s+/g, ' ');- const newWidth = measureWidth(collapsedInternal);+ if (/\s\s/.test(wordsToAdd)) {+ const currentWidth = measureWidth(wordsToAdd);+ const newWidth = measureWidth(wordsToAdd.replace(/\s\s+/g, ' '));if (newWidth === currentWidth) {// Whitespace was collapsed by the browser- collapsedText = collapsedInternal;+ wordsToAdd = wordsToAdd.replace(/\s\s+/g, ' ');}}// Restore original text(textNode as Text).textContent = originalText;- return {collapsedText, leading, trailing};+ return {collapsedText: wordsToAdd};};diff --git a/packages/web-renderer/src/test/fixtures/whitespace-collapsing-2.tsx b/packages/web-renderer/src/test/fixtures/whitespace-collapsing-2.tsxindex b7e3392413..2330c91d5b 100644--- a/packages/web-renderer/src/test/fixtures/whitespace-collapsing-2.tsx+++ b/packages/web-renderer/src/test/fixtures/whitespace-collapsing-2.tsx@@ -5,100 +5,26 @@ const Component: React.FC = () => {<divstyle={{position: 'absolute',- inset: 0,- width: '100%',- height: '100%',- display: 'flex',+ inset: '30px 0px 0px 0',+ width: '613.625px',+ height: '430.143px',flexDirection: 'column',+ fontSize: 56,+ lineHeight: '1.2',+ fontWeight: 600,+ fontFamily: 'Helvetica',+ display: 'inline-block',+ paddingLeft: 30,+ paddingRight: 30,}}>- <div- style={{- position: 'absolute',- inset: 0,- width: '100%',- height: '100%',- display: 'flex',- flexDirection: 'column',- background: 'rgb(250, 250, 250)',- }}- >- <div- style={{- position: 'absolute',- inset: 0,- width: '100%',- height: '100%',- display: 'flex',- }}- >- <div- style={{- position: 'absolute',- inset: 0,- width: '100%',- height: '100%',- display: 'flex',- flexDirection: 'column',- }}- >- <div- style={{- position: 'absolute',- inset: '30px 0px 0px 436.375px',- width: '613.625px',- height: '430.143px',- display: 'flex',- flexDirection: 'column',- fontSize: 56,- lineHeight: 2,- borderWidth: 0,- borderStyle: 'solid',- borderColor: 'rgb(235, 235, 235)',- backgroundColor: 'rgb(255, 255, 255)',- justifyContent: 'center',- borderRadius: 20,- opacity: 1,- }}- >- <div>- <div>- <span- style={{- lineHeight: '1.2',- display: 'inline-block',- paddingLeft: 30,- paddingRight: 30,- width: '100%',- fontWeight: 600,- fontFamily: 'Helvetica',- }}- >- <span> need</span>- <span> FFmpeg</span>- <span> to</span>- <span> encode</span>- <span> the</span>- <span> frames</span>- <span> and</span>- <span> mix</span>- <span> the</span>- <span> audio</span>- <span> all together</span>- <span> into</span>- <span> an</span>- <span> MP4</span>- <span> file.</span>- <span> And</span>- <span> if</span>- <span> you</span>- </span>- </div>- </div>- </div>- </div>- </div>- </div>+ <span> and</span>+ <span> mix</span>+ <span> the</span>+ <span> audio</span>+ <span> all together</span>+ <span> into</span>+ <span> an</span></div>);};@@ -106,8 +32,8 @@ const Component: React.FC = () => {export const whiteSpaceCollapsing2 = {component: Component,id: 'whitespace-collapsing-2',- width: 1080,- height: 1080,+ width: 600,+ height: 200,fps: 30,durationInFrames: 1,} as const;diff --git a/packages/web-renderer/src/test/whitespace-collapsing.test.tsx b/packages/web-renderer/src/test/whitespace-collapsing.test.tsxindex c5240cf630..ed89de2ae5 100644--- a/packages/web-renderer/src/test/whitespace-collapsing.test.tsx+++ b/packages/web-renderer/src/test/whitespace-collapsing.test.tsx@@ -5,7 +5,7 @@ import {whiteSpaceCollapsing} from './fixtures/whitespace-collapsing';import {whiteSpaceCollapsing2} from './fixtures/whitespace-collapsing-2';import {testImage} from './utils';-test('should render box-decoration-break', async () => {+test('small test for whitespace collapsing', async () => {const {blob} = await renderStillOnWeb({licenseKey: 'free-license',composition: whiteSpaceCollapsing,@@ -17,7 +17,7 @@ test('should render box-decoration-break', async () => {await testImage({blob, testId: 'whitespace-collapsing', threshold: 0.02});});-test('should render box-decoration-break', async () => {+test('3 lines of text for whitespace collapsing', async () => {const {blob} = await renderStillOnWeb({licenseKey: 'free-license',composition: whiteSpaceCollapsing2,