Skip to content

🎥 Make videos programmatically with React

implement RTL text

JonnyBurger0fc5af9f2b1a
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 92487bcd4a..9a845c1db4 100644
--- a/packages/web-renderer/src/drawing/text/handle-text-node.ts
+++ b/packages/web-renderer/src/drawing/text/handle-text-node.ts
@@ -18,12 +18,17 @@ export const handleTextNode = (
span.appendChild(node);
const rect = span.getBoundingClientRect();
const style = getComputedStyle(span);
- const {fontFamily, fontSize, fontWeight, color, lineHeight} = style;
+ const {fontFamily, fontSize, fontWeight, color, lineHeight, direction} =
+ style;
context.font = `${fontWeight} ${fontSize} ${fontFamily}`;
context.fillStyle = color;
context.textBaseline = 'top';
+ // Handle RTL text rendering
+ const isRTL = direction === 'rtl';
+ context.textAlign = isRTL ? 'right' : 'left';
+
// 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
@@ -32,9 +37,12 @@ export const handleTextNode = (
const baselineOffset = (lineHeightPx - fontSizePx) / 2;
+ // For RTL text, fill from the right edge instead of left
+ const xPosition = isRTL ? rect.right : rect.left;
+
context.fillText(
getCollapsedText(span),
- rect.left,
+ xPosition,
rect.top + baselineOffset,
);