Skip to content

Keep characters above U+FFFF whole when wrapping text - #1880

Open
ravitejas-tech wants to merge 2 commits into
foliojs:masterfrom
ravitejas-tech:fix/astral-plane-characters
Open

ravitejas-tech wants to merge 2 commits into
foliojs:masterfrom
ravitejas-tech:fix/astral-plane-characters

Conversation

@ravitejas-tech

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Bug fix. Related to #1565.

A character above U+FFFF, such as an emoji or U+24256, is two UTF-16 code units. LineWrapper moved one code unit at a time when it split a word wider than the line and when it shortened a line to make room for the ellipsis, so it could cut such a character in half. Both halves were then drawn as missing glyphs.

It shows when half a character measures narrower than a whole one: with characterSpacing, which counts code units, or with a font whose .notdef is narrower than its glyphs. With the font from #1565 (Chocolate Classical Sans):

doc
  .font('ChocolateClassicalSans-Regular.ttf')
  .text('\u{24256}'.repeat(20), {
    width: 110,
    characterSpacing: 2,
    lineBreak: 'whitespace',
  });

Before, some lines ended with half a character and the next line started with the other half, both drawn as missing glyphs. The wrapper now steps over a surrogate pair as one character. Grapheme clusters can still be split, as the existing TODO says.

Checklist:

  • Unit Tests
  • Documentation N/A
  • Update CHANGELOG.md
  • Ready to be merged

The two new LineWrapper tests wrap with Roboto at every width from 20 to 120 and fail without the change.

A character above U+FFFF, such as an emoji or U+24256, is two UTF-16
code units. The line wrapper moved one code unit at a time when it
split a word wider than the line and when it shortened a line for the
ellipsis, so it could cut such a character in half. Both halves were
then drawn as missing glyphs.

This happens when half a character measures narrower than a whole one:
with a font whose .notdef is narrower than its glyphs, or with
characterSpacing, which counts code units. The wrapper now steps over
a surrogate pair as one character.

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