Skip to content

🎥 Make videos programmatically with React

`@web-renderer`: Account for "cannotStartLine" Unicode rules

JonnyBurgerb79622250712
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 42fe93f90e..1bd599fba4 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,5 +1,34 @@
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);
+};
+
export function findLineBreaks(
span: HTMLSpanElement,
rtl: boolean,
@@ -60,13 +89,42 @@ export function findLineBreaks(
const shouldCollapse =
!computedStyle.whiteSpaceCollapse.includes('preserve');
+ let textForPreviousLine = currentLine;
+ let textForNewLine = wordsToAdd;
+
+ // 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('') + wordsToAdd;
+ }
+ }
+
lines.push({
- text: shouldCollapse ? currentLine.trim() : currentLine,
+ text: shouldCollapse ? textForPreviousLine.trim() : textForPreviousLine,
height: currentHeight - previousRect.height,
offsetHorizontal,
});
- currentLine = wordsToAdd;
+ currentLine = textForNewLine;
} else {
currentLine += wordsToAdd;
}
diff --git a/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-chromium-darwin.png b/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-chromium-darwin.png
index d6d9828b15..7f42da95b3 100644
Binary files a/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-chromium-darwin.png and b/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-chromium-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-firefox-darwin.png b/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-firefox-darwin.png
index 6e59c15a95..515c0c910b 100644
Binary files a/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-firefox-darwin.png and b/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-firefox-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-webkit-darwin.png b/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-webkit-darwin.png
index f18c592b62..081a275e29 100644
Binary files a/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-webkit-darwin.png and b/packages/web-renderer/src/test/__screenshots__/background-clip.test.tsx/background-clip-text-webkit-darwin.png differ
diff --git a/packages/web-renderer/src/test/fixtures/text/background-clip-text.tsx b/packages/web-renderer/src/test/fixtures/text/background-clip-text.tsx
index a59c78224f..04bcfef924 100644
--- a/packages/web-renderer/src/test/fixtures/text/background-clip-text.tsx
+++ b/packages/web-renderer/src/test/fixtures/text/background-clip-text.tsx
@@ -22,22 +22,6 @@ const Component: React.FC = () => {
flexDirection: 'column',
}}
>
- <p
- style={{
- ...baseStyle,
- backgroundClip: 'border-box',
- }}
- >
- The background extends behind the border.
- </p>
- <p
- style={{
- ...baseStyle,
- backgroundClip: 'padding-box',
- }}
- >
- The background extends to the inside edge of the border.
- </p>
<p
style={{
...baseStyle,
@@ -46,16 +30,6 @@ const Component: React.FC = () => {
>
The background extends only to the edge of the content box.
</p>
- <p
- style={{
- ...baseStyle,
- backgroundClip: 'text',
- WebkitBackgroundClip: 'text',
- color: 'rgba(0, 0, 0, 0.2)',
- }}
- >
- The background is clipped to the foreground text.
- </p>
</AbsoluteFill>
);
};