fix(textkit): skip hyphen insertion for CJK characters in line breaking#3315
Open
matangot wants to merge 1 commit intodiegomura:masterfrom
Open
fix(textkit): skip hyphen insertion for CJK characters in line breaking#3315matangot wants to merge 1 commit intodiegomura:masterfrom
matangot wants to merge 1 commit intodiegomura:masterfrom
Conversation
CJK (Chinese/Japanese) text gets incorrectly hyphenated with `-` dashes at line breaks. breakLines unconditionally inserts a hyphen glyph when breaking at a penalty node, which is correct for Latin scripts but wrong for CJK text where characters wrap naturally without hyphens. Check whether the character at the break point is in the CJK Unicode range and skip hyphen insertion if so.
|
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.
Summary
CJK (Chinese/Japanese) text gets incorrectly hyphenated with
-dashes at line breaks. This happens becausebreakLinesunconditionally inserts a hyphen glyph when breaking at a penalty node, which is correct for Latin scripts but wrong for CJK text where characters wrap naturally without hyphens.Before
After
The Problem
CJK text has no spaces between characters, so the entire text block becomes a single "word" in the layout engine. When a user's
hyphenationCallbacksplits this into individual characters (the correct approach for CJK),breakLinesinserts a-hyphen at every line break point — because it assumes all penalty-node breaks are Latin-style hyphenation.The Fix
In
breakLines, check whether the character at the break point is in the CJK Unicode range. If so, skip the hyphen glyph insertion.Where
CJK_RANGE = /[\u3000-\u9fff\uf900-\ufaff\uff00-\uffef]/This covers:
Korean (Hangul) is excluded as it uses spaces between words and doesn't hit this code path in practice.
Notes
A complete CJK line breaking solution would also include kinsoku (禁則処理) rules in the layout engine — preventing punctuation like
。,、from starting a new line. That's currently left to userland viahyphenationCallbackbut could be a follow-up enhancement.