Skip to content

🎥 Make videos programmatically with React

massive change

Jonny Burgerfbd52cd6829b
diff --git a/packages/web-renderer/src/drawing/text/draw-text.ts b/packages/web-renderer/src/drawing/text/draw-text.ts
index 3f174a9390..24e020e97b 100644
--- a/packages/web-renderer/src/drawing/text/draw-text.ts
+++ b/packages/web-renderer/src/drawing/text/draw-text.ts
@@ -2,7 +2,7 @@ import type {LogLevel} from 'remotion';
import {Internals} from 'remotion';
import type {DrawFn} from '../drawn-fn';
import {applyTextTransform} from './apply-text-transform';
-import {findLineBreaks} from './find-line-breaks.text';
+import {findWords} from './find-line-breaks.text';
export const drawText = ({
span,
@@ -59,26 +59,22 @@ export const drawText = ({
const transformedText = applyTextTransform(originalText, textTransform);
span.textContent = transformedText;
- const {lines, xPosition} = findLineBreaks(span, isRTL);
+ const tokens = findWords(span);
- let offsetTop = 0;
+ for (const token of tokens) {
+ const measurements = contextToDraw.measureText(token.text);
+ const {fontBoundingBoxDescent, fontBoundingBoxAscent} = measurements;
- const measurements = contextToDraw.measureText(lines[0].text);
- const {fontBoundingBoxDescent, fontBoundingBoxAscent} = measurements;
-
- const fontHeight = fontBoundingBoxAscent + fontBoundingBoxDescent;
-
- for (const line of lines) {
+ const fontHeight = fontBoundingBoxAscent + fontBoundingBoxDescent;
// Calculate leading
- const leading = line.height - fontHeight;
+ const leading = rect.height - fontHeight;
const halfLeading = leading / 2;
contextToDraw.fillText(
- line.text,
- xPosition + line.offsetHorizontal,
- rect.top + halfLeading + fontBoundingBoxAscent + offsetTop,
+ token.text,
+ token.rect.left,
+ token.rect.top + fontBoundingBoxAscent,
);
- offsetTop += line.height;
}
span.textContent = originalText;
diff --git a/packages/web-renderer/src/drawing/text/find-line-breaks.text.ts b/packages/web-renderer/src/drawing/text/find-line-breaks.text.ts
index c4664d5acb..335445ecc5 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
@@ -1,179 +1,37 @@
-import {getCollapsedText} from './get-collapsed-text';
-
-// Punctuation that cannot start a line according to Unicode line breaking rules
-// When these would start a line, the browser moves the preceding word to the new line
-const cannotStartLine = (segment: string): boolean => {
- if (segment.length === 0) return false;
- const firstChar = segment[0];
- const forbiddenLineStarts = [
- '.',
- ',',
- ';',
- ':',
- '!',
- '?',
- ')',
- ']',
- '}',
- '"',
- "'",
- '"',
- `'`,
- '»',
- '…',
- '‥',
- '·',
- '%',
- '‰',
- ];
- return forbiddenLineStarts.includes(firstChar);
-};
-
-type RectMeasurement = {
- rectList: DOMRectList;
+type Token = {
+ text: string;
rect: DOMRect;
};
-const getRectMeasurement = (el: HTMLSpanElement) => {
- const rectList = el.getClientRects();
- const rect = el.getBoundingClientRect();
- return {rectList, rect};
-};
-
-export function findLineBreaks(
- span: HTMLSpanElement,
- rtl: boolean,
-): {
- lines: {
- text: string;
- height: number;
- offsetHorizontal: number;
- }[];
- xPosition: number;
-} {
- const textNode = span.childNodes[0] as Text;
- const originalText = textNode.textContent;
- textNode.textContent = '';
- const originalRect = span.getBoundingClientRect();
-
+export const findWords = (span: HTMLSpanElement) => {
+ const originalText = span.textContent;
const segmenter = new Intl.Segmenter('en', {granularity: 'word'});
- const segments = segmenter.segment(originalText);
-
+ const segments = segmenter.segment(span.textContent);
const words = Array.from(segments).map((s) => s.segment);
- const lines: Array<{
- text: string;
- height: number;
- offsetHorizontal: number;
- }> = [];
+ const tokens: Token[] = [];
- let currentLine = '';
- let testText = '';
- let previousRect: RectMeasurement;
-
- for (let i = 0; i < words.length; i += 1) {
+ for (let i = 0; i < words.length; i++) {
+ const wordsBefore = words.slice(0, i);
+ const wordsAfter = words.slice(i);
const word = words[i];
- let wordsToAdd = word;
- while (typeof words[i + 1] !== 'undefined' && wordsToAdd.trim() === '') {
- wordsToAdd += words[i + 1];
- i++;
- }
-
- previousRect = getRectMeasurement(span);
- testText += wordsToAdd;
- const previousText = textNode.textContent;
- textNode.textContent = testText;
- const rect = span.getBoundingClientRect();
- const currentHeight = rect.height;
-
- const isLineBreak =
- previousRect &&
- previousRect.rect.height !== 0 &&
- Math.abs(currentHeight - previousRect.rect.height) > 2;
-
- const {collapsedText} = getCollapsedText({
- span,
- previousText,
- wordsToAdd,
- canCollapseStart: isLineBreak || lines.length === 0,
- });
-
- // If height changed significantly, we had a line break
- if (isLineBreak) {
- let textForPreviousLine = currentLine;
- 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
- if (cannotStartLine(word)) {
- const currentLineSegments = Array.from(
- segmenter.segment(currentLine),
- ).map((s) => s.segment);
- // Find the last non-whitespace segment (the word to move)
- let lastWordIndex = currentLineSegments.length - 1;
- while (
- lastWordIndex >= 0 &&
- currentLineSegments[lastWordIndex].trim() === ''
- ) {
- lastWordIndex--;
- }
-
- if (lastWordIndex >= 0) {
- // Move the last word (and any trailing whitespace) to the new line
- textForPreviousLine = currentLineSegments
- .slice(0, lastWordIndex)
- .join('');
- textForNewLine =
- currentLineSegments.slice(lastWordIndex).join('') + collapsedText;
- }
- }
-
- const rects = getRectMeasurement(span);
- const lastRect = rects.rectList[rects.rectList.length - 1];
-
- let offsetHorizontal = rtl
- ? lastRect.right - originalRect.right
- : lastRect.left - originalRect.left;
-
- if (lines.length === 0) {
- offsetHorizontal = 0;
- }
-
- lines.push({
- text: textForPreviousLine,
- height: currentHeight - previousRect.rect.height,
- offsetHorizontal,
- });
-
- currentLine = textForNewLine;
- } else {
- currentLine += collapsedText;
- }
+ const wordsBeforeText = wordsBefore.join('');
+ const wordsAfterText = wordsAfter.join('');
+
+ const beforeNode = document.createTextNode(wordsBeforeText);
+ const afterNode = document.createTextNode(wordsAfterText);
+ const interstitialNode = document.createElement('span');
+ interstitialNode.textContent = word;
+ span.textContent = '';
+ span.appendChild(beforeNode);
+ span.appendChild(interstitialNode);
+ span.appendChild(afterNode);
+
+ const rect = interstitialNode.getBoundingClientRect();
+ span.textContent = originalText;
+ tokens.push({text: word, rect});
}
- // Add the last line
- if (currentLine) {
- textNode.textContent = testText;
-
- const rects = getRectMeasurement(span);
- const lastRect = rects.rectList[rects.rectList.length - 1];
-
- const offsetHorizontal = rtl
- ? lastRect.right - originalRect.right
- : lastRect.left - originalRect.left;
-
- lines.push({
- text: currentLine,
- height:
- rects.rect.height - lines.reduce((acc, curr) => acc + curr.height, 0),
- offsetHorizontal,
- });
- }
-
- // Reset to original text
- textNode.textContent = originalText;
-
- const originalXPosition = rtl ? originalRect.right : originalRect.left;
- return {lines, xPosition: originalXPosition};
-}
+ return tokens;
+};
diff --git a/packages/web-renderer/src/test/whitespace-collapsing.test.tsx b/packages/web-renderer/src/test/whitespace-collapsing.test.tsx
index ed89de2ae5..4fe342bd17 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('small test for whitespace collapsing', async () => {
+test.only('small test for whitespace collapsing', async () => {
const {blob} = await renderStillOnWeb({
licenseKey: 'free-license',
composition: whiteSpaceCollapsing,