Skip to content

fix(text): keep surrogate pairs whole when truncating and wrapping - #1179

Open
yu2971512385-ui wants to merge 1 commit into
ecomfe:masterfrom
yu2971512385-ui:fix/surrogate-pairs-in-text-layout
Open

yu2971512385-ui wants to merge 1 commit into
ecomfe:masterfrom
yu2971512385-ui:fix/surrogate-pairs-in-text-layout

Conversation

@yu2971512385-ui

Copy link
Copy Markdown

What

truncateText and parsePlainText both walk text a UTF-16 code unit at a time, so a character outside the BMP can be cut in half. The orphaned surrogate renders as a replacement character.

This reaches users through ECharts, which truncates axis labels through truncateText and wraps through parsePlainText.

Truncation

truncateSingleLine slices with textLine.substr(0, subLength), and subLength is a code unit count — from estimateLength, which walks with text.charCodeAt(i), or from the proportional estimate on later iterations.

#1160 by @mahirhir found this first and proposed the same guard. They withdrew it before anyone reviewed it ("not really worth taking up your review time"), but the bug is still on master, so the fix is carried here with credit.

Wrapping, which #1160 did not cover

parsePlainText reads characters with text.charAt(i), one code unit. Every wrapped line can therefore begin or end with half a character. Six emoji at width 30, before:

"😀\ud83d" | "\ude00😀" | "😀\ud83d" | "\ude00😀"

after:

"😀😀😀" | "😀😀😀"

Scale

Over a grid of astral inputs (emoji, CJK Extension B, regional indicators, mathematical alphanumerics, mixed ASCII) at three widths:

truncateText:           7 of 18 broken before, 0 after
parsePlainText (wrap): 10 of 18 broken before, 0 after
ASCII and BMP output unchanged by the patch: true

One consequence worth flagging

Treating a pair as one character means an astral character is now measured once rather than twice. measureCharWidth returns stWideCharWidth for anything non-ASCII, so an emoji used to be charged two wide-character widths; it is now charged one, which matches its actual advance and means emoji text wraps later than it used to.

That is a behaviour change beyond keeping characters whole, and I think it is the right one — but if you would rather keep the old widths and only stop the splitting, say so and I will make the pair consume two widths instead. ASCII and BMP text is unaffected either way, which the tests pin.

Tests

test/ut/spec/graphic/surrogatePairs.test.ts, five cases covering both paths, with a deterministic measureText so nothing depends on a font. Two of the five fail on master:

✕ should not leave half a surrogate pair when cutting
✓ should still cut plain text exactly where it did before
✕ should not split a surrogate pair across a line break
✓ should keep every character, in order
✓ should wrap text inside the BMP as it did before

and all five pass with the change. Full suite: 13 suites, 73 tests passing (68 before). eslint src/graphic/helper/parseText.ts clean.

🤖 Generated with Claude Code

Both paths walk text a UTF-16 code unit at a time, so a character outside
the BMP can be cut in half and render as a replacement character.

truncateSingleLine slices with `textLine.substr(0, subLength)`, where
subLength comes from estimateLength or from the proportional estimate, both
counts of code units. ecomfe#1160 found this and proposed the same guard; the
author withdrew it before review, so it is carried here with credit.

parsePlainText has it too, and worse: `text.charAt(i)` takes one code unit,
so every wrapped line can begin or end with half a character. With six
emoji at width 30 the old code produced

  "😀\ud83d" | "\ude00😀" | ...

Over a small grid of astral inputs and widths, truncation broke 7 of 18
cases and wrapping 10 of 18; both are 0 after.

Treating a pair as one character also means an astral character is now
measured once rather than twice, so emoji are no longer charged double
width and wrap later than before. ASCII and BMP text is byte-identical,
which the tests pin down.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant