remotion icon indicating copy to clipboard operation
remotion copied to clipboard

@remotion/web-renderer: a Chinese, Japanese or Korean word that wraps is drawn on the first line and overlaps the text before it

Open DanZavy opened this issue 2 weeks ago • 1 comments

Bug

@remotion/web-renderer draws a wrapped Chinese, Japanese or Korean word over the text before it. The Player shows the same frame correctly.

Seen in 4.0.537 and 4.0.543, the latest release when this was written.

Cause

findWords (src/drawing/text/find-line-breaks.text.ts) splits each text node with new Intl.Segmenter("en", { granularity: "word" }), wraps each segment in a span and draws it at that span's getBoundingClientRect().

In Chinese and Japanese a word segment is often several characters long, and Korean breaks lines between syllables, so the browser may break a line inside a segment. getBoundingClientRect() of a span split across two lines is the union of both fragments: its left is the second line's left (the box's left edge) and its top is the first line's top. The renderer then draws the whole segment, unbroken, from there on the first line, over whatever precedes it.

A probe at 44 px and box widths from 200 to 900 px counted the segments split across lines: Chinese 35, Japanese 60, Korean 68. Thai, Lao, Khmer, Burmese and English gave 0: Chromium breaks those lines at the same word boundaries the segmenter finds.

Reproduction

<div style={{ width: 260, fontSize: 48, lineHeight: 1.2 }} lang="zh">
  今天我们一起去公园散步吧
</div>

Render one frame with renderStillOnWeb and compare it with the Player. In the export, the words that wrap onto line 2 are drawn on line 1, starting at the box's left.

Workaround we ship

We render each grapheme of Han, kana, Hangul, Bopomofo and fullwidth forms as its own text node, so no segment can span a line break. Each text node's advance rounds to 1/64 px in Chromium, so the split line is up to n/64 px wider: we measure and size the box with that allowance.

A related case: a segment whose advance is not positive

processNode returns before drawing an element whose width <= 0. With a negative letter-spacing, a short word can have a zero or negative advance, and the export drops it while the Player draws it. This is not a request to change the skip, which is right for empty boxes. It is a note that "width ≤ 0" does not mean "draws nothing" for text.

Suggested fix

Split a segment's rect per line with range.getClientRects() and draw each fragment's characters on its own line. Alternatively, segment with the element's lang and fall back to graphemes when a segment has more than one client rect.

DanZavy avatar Nov 15 '26 15:11 DanZavy

Hi! I'd like to take this. As you described, findWords in find-line-breaks.text.ts splits on spaces, so a CJK run is treated as one "word" and the wrap position gets out of sync with what the Player shows. My plan is to split CJK text into per-character (grapheme) break opportunities, ideally using Intl.Segmenter with a fallback, so line breaks match the browser layout. I'll add a test that renders a wrapped CJK line and compare it with the Player output. Could you assign it to me?

Mittalnew avatar Nov 16 '26 05:11 Mittalnew