Fix decomposed accents with standard fonts - #1798
Open
lbesecker195 wants to merge 1 commit into
Open
Conversation
Standard fonts encode each character as its WinAnsi code in hex. A combining mark such as U+0308 has no WinAnsi code, so its character code went into the hex string as three digits, and every character after it was read from the wrong byte. Text in NFD, e.g. "u" followed by a combining diaeresis, came out as unrelated characters. Compose the text (NFC) before encoding and measuring it, so those sequences use the precomposed letters that WinAnsi has. Fixes foliojs#1661 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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 kind of change does this PR introduce?
Bug fix for #1661.
With a standard font, text with decomposed accents (NFD) prints the wrong characters from the first accent onward.
'Text für'.normalize('NFD')isufollowed by U+0308 COMBINING DIAERESIS, while the NFC form has a singleü.AFMFont.encodeTextwrites each character's WinAnsi code withtoString(16), and the codes are joined into one hex string.üisfc, but U+0308 has no WinAnsi code, so it goes in as308, an odd number of digits. Every byte after it is read from the wrong position. The issue's sample produces this on master:StandardFont.encode()andStandardFont.widthOfString()now compose the text withnormalize('NFC')first. WinAnsiEncoding has the precomposed Latin-1 letters but no combining marks, so this is what the encoding can actually represent. Measuring uses the same text, so wrapping and alignment agree with what is drawn. Before, the width ofnaïvein NFD came out narrower, because the combining mark measured as.notdefnext to the narroweri.Embedded fonts are unchanged. fontkit lays out combining marks itself.
Checklist:
Tests
Two tests in
tests/unit/text.spec.js:Text für Été naïvein NFD produces the same content stream as in NFC, withfc,c9,e9andefbytes.widthOfString('naïve')is the same for NFD and NFC.yarn teststandard.jschangeyarn lintandprettier --checkon the changed files pass.Not changed here: a character with no WinAnsi code and no precomposed form, for example a combining mark on a letter that has no accented form in Latin-1, still produces a longer hex code and shifts the rest of the string. That needs a decision on what to emit for unencodable characters, so I kept this PR to the case in the issue.
🤖 Generated with Claude Code