Skip to content

🎥 Make videos programmatically with React

handle font weight and whitespace collapsing!

JonnyBurger444c0b10f126
diff --git a/packages/web-renderer/src/drawing/text/get-collapsed-text.ts b/packages/web-renderer/src/drawing/text/get-collapsed-text.ts
new file mode 100644
index 0000000000..ba08fa61f1
--- /dev/null
+++ b/packages/web-renderer/src/drawing/text/get-collapsed-text.ts
@@ -0,0 +1,58 @@
+export const getCollapsedText = (span: HTMLSpanElement): string => {
+ const textNode = span.firstChild;
+
+ if (!textNode || textNode.nodeType !== Node.TEXT_NODE) {
+ throw new Error('Span must contain a single text node');
+ }
+
+ 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 originalWidth = measureWidth(originalText);
+
+ // Test leading whitespace
+ if (/^\s/.test(collapsedText)) {
+ const trimmedLeading = collapsedText.replace(/^\s+/, '');
+ const newWidth = measureWidth(trimmedLeading);
+
+ if (newWidth === originalWidth) {
+ // Whitespace was collapsed by the browser
+ collapsedText = trimmedLeading;
+ }
+ }
+
+ // 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 (newWidth === currentWidth) {
+ // Whitespace was collapsed by the browser
+ collapsedText = trimmedTrailing;
+ }
+ }
+
+ // 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 (newWidth === currentWidth) {
+ // Whitespace was collapsed by the browser
+ collapsedText = collapsedInternal;
+ }
+ }
+
+ // Restore original text
+ (textNode as Text).textContent = originalText;
+
+ return collapsedText;
+};
diff --git a/packages/web-renderer/src/drawing/text/handle-text-node.ts b/packages/web-renderer/src/drawing/text/handle-text-node.ts
index 7b577ef57a..92487bcd4a 100644
--- a/packages/web-renderer/src/drawing/text/handle-text-node.ts
+++ b/packages/web-renderer/src/drawing/text/handle-text-node.ts
@@ -1,3 +1,5 @@
+import {getCollapsedText} from './get-collapsed-text';
+
export const handleTextNode = (
node: Text,
context: OffscreenCanvasRenderingContext2D,
@@ -16,11 +18,25 @@ export const handleTextNode = (
span.appendChild(node);
const rect = span.getBoundingClientRect();
const style = getComputedStyle(span);
- const {fontFamily, fontSize, color} = style;
+ const {fontFamily, fontSize, fontWeight, color, lineHeight} = style;
- context.font = `${fontSize} ${fontFamily}`;
+ context.font = `${fontWeight} ${fontSize} ${fontFamily}`;
context.fillStyle = color;
- context.fillText(node.textContent, rect.left, rect.top);
+ context.textBaseline = 'top';
+
+ // Calculate the baseline position considering line height
+ const fontSizePx = parseFloat(fontSize);
+ // TODO: This is not necessarily correct, need to create text and measur to know for sure
+ const lineHeightPx =
+ lineHeight === 'normal' ? 1.2 * fontSizePx : parseFloat(lineHeight);
+
+ const baselineOffset = (lineHeightPx - fontSizePx) / 2;
+
+ context.fillText(
+ getCollapsedText(span),
+ rect.left,
+ rect.top + baselineOffset,
+ );
// Undo the layout manipulation
parent.insertBefore(node, span);
diff --git a/packages/web-renderer/src/test/fixtures/text.tsx b/packages/web-renderer/src/test/fixtures/text.tsx
index 4f44d7d117..51d438f9d9 100644
--- a/packages/web-renderer/src/test/fixtures/text.tsx
+++ b/packages/web-renderer/src/test/fixtures/text.tsx
@@ -11,7 +11,10 @@ const Component: React.FC = () => {
>
<div style={{width: 550}}>
Hello from{' '}
- <a href="https://github.com/WICG/html-in-canvas">html-in-canvas</a>!
+ <a href="https://github.com/WICG/html-in-canvas">
+ remotion-web-renderer
+ </a>
+ !
<br />I{"'"}m multi-line, <b>formatted</b>, rotated text with emoji
(&#128512;), RTL text
<span dir="rtl">من فارسی صحبت میکنم</span>