Skip to content

fix: word wrap breaks next to Chinese and Japanese characters - #2084

Merged
lavrton merged 3 commits into
konvajs:masterfrom
b84330808:fix-word-wrap-cjk
Nov 9, 2026
Merged

lavrton merged 3 commits into
konvajs:masterfrom
b84330808:fix-word-wrap-cjk

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

Follow-up to #1746, where you asked for a PR with a test.

wrap: 'word' only breaks at spaces and dashes, so mixed text is cut at the only space, far from the edge:

const text = new Konva.Text({ text: 'Konva 是畫布函式庫', fontSize: 20 });
text.width(text.measureSize('Konva 是畫').width + 1);
text.textArr.map((l) => l.text);
// before: ['Konva', '是畫布函', '式庫']
// after:  ['Konva 是畫', '布函式庫']

With no space on the line, a latin word next to them was split in the middle instead (畫布Ko / nva).

Chinese and Japanese have no spaces between words, so a line can now break next to any of their characters, except before closing punctuation and after opening punctuation. Korean uses spaces and keeps breaking at them. Text with no Chinese, Japanese or fullwidth characters takes the same breaks as before. The check only runs when a line needs wrapping and adds no measuring.

I ran the unit tests on node-canvas and skia, but not test:browser.

b84330808 and others added 3 commits November 6, 2026 14:19
Word wrap only broke at spaces and dashes, so text that mixes Chinese or
Japanese with a latin word was cut at the only space, far from the edge, and a
latin word next to them was split in the middle. These scripts have no spaces
between words: a line can now break next to any of their characters, except
before closing punctuation and after opening punctuation. Korean uses spaces
and keeps breaking at them.

Refs konvajs#1746
…aracter before

々 ゝ ゞ ヽ ヾ 〻 〃 repeat the character before them, so a line must not start
with one, and neither may halfwidth 。 、 ・ or a fullwidth %. They are not
closing punctuation, so the general-category classes missed them and 人々
wrapped as 人 / 々, which Chrome never does.

The rule that a line can end before a space now lives in isBreak with the
other break rules instead of in a separate guard of the wrap loop. Breaks are
unchanged.

The Konva日本語 test case passed before the fix as well, so it is replaced
by one that breaks between a latin word and a Chinese character. The latin
case duplicated existing tests and is dropped.
The tests assumed a CJK glyph is wider than a latin letter, so the latin
word that moves to the next line fit there. Where no CJK font is installed
the glyphs are narrow placeholder boxes, as on the CI runners for skia and
Chrome, and the word was cut. Each width now fits every line the assertion
expects, and the filled-line case asserts only the line it is about. Every
case passes with narrow and wide CJK glyphs and fails on the old wrap code.
@lavrton
lavrton merged commit b92df91 into konvajs:master Nov 9, 2026
3 checks passed
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.

2 participants