Skip to content

Fix UTF-16 encoding of supplementary plane characters#171

Open
iliaal wants to merge 1 commit into
michaelrsweet:masterfrom
iliaal:fix-utf16-surrogate-pairs
Open

Fix UTF-16 encoding of supplementary plane characters#171
iliaal wants to merge 1 commit into
michaelrsweet:masterfrom
iliaal:fix-utf16-surrogate-pairs

Conversation

@iliaal

@iliaal iliaal commented Jun 13, 2026

Copy link
Copy Markdown

write_string() mis-encodes any character at U+10000 or above when drawing text with unicode=true. Two bugs in the same branch: the threshold that picks a surrogate pair is 0x100000 instead of 0x10000, so supplementary plane characters take the single-code-unit branch and print as a raw 5-hex-digit value (U+1F600 becomes 1F600); and the surrogate formula never subtracts the 0x10000 bias, so even a character that reached the pair branch got the wrong high surrogate (D87D rather than D83D). 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 writes A, U+00E9, U+20AC, and U+1F600 with unicode=true, reads the content stream back, and checks for <004100E920ACD83DDE00>.

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.
@michaelrsweet michaelrsweet self-assigned this Jun 13, 2026
@michaelrsweet michaelrsweet added bug Something isn't working investigating labels Jun 13, 2026
@michaelrsweet michaelrsweet added this to the Stable milestone Jun 13, 2026
@michaelrsweet

Copy link
Copy Markdown
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

Copy link
Copy Markdown
Owner

Here is the 1.6.x fix:

[v1.6.x 1061687] Fix UTF-16 encoding of Unicode strings in embedded content (Issue #171)

Still working on the changes for 1.7.x to embed a font with extended Unicode support...

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants