Repository navigation
fix(text): keep surrogate pairs whole when truncating and wrapping - #1179
Open
yu2971512385-ui wants to merge 1 commit into
Open
yu2971512385-ui wants to merge 1 commit into
yu2971512385-ui wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
truncateTextandparsePlainTextboth 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
truncateTextand wraps throughparsePlainText.Truncation
truncateSingleLineslices withtextLine.substr(0, subLength), andsubLengthis a code unit count — fromestimateLength, which walks withtext.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
parsePlainTextreads characters withtext.charAt(i), one code unit. Every wrapped line can therefore begin or end with half a character. Six emoji at width 30, before:after:
Scale
Over a grid of astral inputs (emoji, CJK Extension B, regional indicators, mathematical alphanumerics, mixed ASCII) at three widths:
One consequence worth flagging
Treating a pair as one character means an astral character is now measured once rather than twice.
measureCharWidthreturnsstWideCharWidthfor 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 deterministicmeasureTextso nothing depends on a font. Two of the five fail on master:and all five pass with the change. Full suite: 13 suites, 73 tests passing (68 before).
eslint src/graphic/helper/parseText.tsclean.🤖 Generated with Claude Code