Fix UTF-16 encoding of supplementary plane characters#171
Open
iliaal wants to merge 1 commit into
Open
Conversation
write_string() used a 0x100000 threshold instead of 0x10000 when deciding between a single UTF-16 code unit and a surrogate pair, and the surrogate path did not subtract the 0x10000 bias. Characters at U+10000 and above (emoji, CJK extensions) were emitted as raw 5-digit code points or with the wrong high surrogate, producing malformed UTF-16 in content strings drawn with unicode=true.
Owner
|
I will be incorporating a subset of your fix but ATM it is a somewhat academic exercise since the current Unicode font support only maps plane 0... |
michaelrsweet
added a commit
that referenced
this pull request
Jun 17, 2026
Owner
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.
write_string()mis-encodes any character at U+10000 or above when drawing text withunicode=true. Two bugs in the same branch: the threshold that picks a surrogate pair is0x100000instead of0x10000, so supplementary plane characters take the single-code-unit branch and print as a raw 5-hex-digit value (U+1F600 becomes1F600); and the surrogate formula never subtracts the0x10000bias, so even a character that reached the pair branch got the wrong high surrogate (D87Drather thanD83D). Both produce malformed UTF-16 in the content stream for emoji, CJK extension blocks, and other non-BMP text.The fix corrects the threshold and subtracts the bias before splitting into surrogates. BMP characters are unchanged.
Added a unit test in
do_unicode_tests(): it writesA, U+00E9, U+20AC, and U+1F600 withunicode=true, reads the content stream back, and checks for<004100E920ACD83DDE00>.