Skip to content

fix: keep decomposed (NFD) Hangul syllables whole and two columns wide - #248

Open
wonjangcloud9 wants to merge 1 commit into
gajus:masterfrom
wonjangcloud9:fix/nfd-hangul
Open

wonjangcloud9 wants to merge 1 commit into
gajus:masterfrom
wonjangcloud9:fix/nfd-hangul

Conversation

@wonjangcloud9

Copy link
Copy Markdown

Korean text in decomposed form (NFD) was split mid-syllable when wrapped, measured at twice its width, and truncated too early. macOS writes Korean file names this way, and they reach tables through fs.readdir, archives and file pickers. In NFD, each syllable is an initial consonant, a vowel and often a final consonant jamo, e.g. 홍 = U+1112 U+1169 U+11BC.

const nfd = '홍길동전입니다'.normalize('NFD');

table([[nfd]], {columns: [{width: 6}]});
// master: lines 홍ᄀ / ᅵᆯ동 / 전ᄋ / ᅵᆸ니 / 다 (syllables torn apart)
// branch: lines 홍길동 / 전입니 / 다
  • Widths: '홍길동'.normalize('NFD') measured 12 columns instead of 6, so its column was twice as wide as the NFC text next to it.
  • Truncation: truncate: 4 gave 홍… instead of 홍길동….

Cause

  • graphemeRegex attaches marks (\p{M}), emoji modifiers and tags to the code point before them. Hangul vowel and final consonant jamo (U+1160–U+11FF, U+D7B0–U+D7FF) are \p{Lo}, so each became a grapheme of its own. UAX Newline not allowed in column data #29 (GB6–GB8) keeps them in the syllable.
  • string-width 4 counts each of those jamo as one column, so a three-jamo syllable measured 4 columns. Newer string-width versions measure it as 2.

Change

  • graphemeRegex also attaches U+1160–U+11FF and U+D7B0–U+D7FF to the character before them.
  • A new measureWidth, used by measureGrapheme and stringWidth, measures a string that contains those jamo in its NFC form, where each syllable is one full-width character. Other strings are measured exactly as before, after one extra regex test. Grapheme measurements are cached as before, and single-column text still never reaches it.
  • The text itself is not normalized. Cells keep the code points they were given, and slice-ansi positions are counted per code point as before, so slicing stays aligned.

Tests

New cases, which all fail on master:

  • splitGraphemes: an NFD syllable is one grapheme of width 2.
  • wrapString: NFD 홍길동전입니다 wraps to 홍길동 / 전입니 / 다.
  • truncateTableData: NFD 홍길동전입니다 truncated to 4 gives 홍길동….

mocha runs the whole suite: 238 passing, and on master the 3 new cases fail. eslint on the changed files and tsc --noEmit are clean.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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