fix: detect VS16 and skin-tone modifiers anywhere in grapheme cluster#23
Open
gammons wants to merge 2 commits intoclipperhouse:mainfrom
Open
fix: detect VS16 and skin-tone modifiers anywhere in grapheme cluster#23gammons wants to merge 2 commits intoclipperhouse:mainfrom
gammons wants to merge 2 commits intoclipperhouse:mainfrom
Conversation
The VS16 check previously only examined the 3 bytes immediately after the first rune (s[sz:sz+3]). In complex emoji sequences like ⛹🏻♂️ (person bouncing ball + skin tone + ZWJ + gender + VS16), VS16 appears much later in the cluster and was missed, causing graphemeWidth to return 1 instead of 2. Similarly, skin-tone modifiers (U+1F3FB–U+1F3FF) on text-default Extended_Pictographic bases like 🕵🏻 (detective + skin tone) were not detected, also returning 1 instead of 2. The fix scans the full grapheme cluster for both VS16 and skin-tone modifiers, promoting to width 2 when either is found. This matches terminal rendering behavior and UTS#51 ED-13 (emoji modifier sequences are always rendered in emoji presentation).
Some terminals (notably kitty) don't follow Unicode TR51's VS16 emoji presentation rules: "❤️" is rendered as 1 cell despite displaywidth's spec-compliant view of 2. Conversely, kitty renders "👍🏼" as 4 cells where displaywidth says 2. Applications that probe the terminal directly (e.g. via CSI 6n cursor position reports) can now install a map of grapheme-cluster → width that overrides the trie + heuristics. Lookup precedence: external map first, then existing logic. ASCII fast paths bypass the override. This lets downstream libraries (charmbracelet/x/ansi, charm.land/lipgloss) inherit the probed widths automatically, eliminating the mismatch between layout calculations and actual terminal rendering.
Owner
|
Thanks @gammons! My first look, the bugs seem legit, I will make sure I understand them. Looks like your fix is straightforward. The external overrides, I can imagine the use case — I’ve seen different terminals do different things. It might not be the terminal! But I don’t love injecting into this library, I’d rather someone wrap this library instead. |
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
graphemeWidth()currently checks for VS16 (U+FE0F) only at the bytes immediately after the first rune (s[sz:sz+3]). This misses VS16 in complex emoji sequences where it appears later in the grapheme cluster. It also has no detection for skin-tone emoji modifiers (U+1F3FB–U+1F3FF) on text-defaultExtended_Pictographicbases.This PR scans the full grapheme cluster for both VS16 and skin-tone modifiers, promoting to width 2 when either is found.
Bugs Fixed
Bug A: Non-adjacent VS16 (~30 emoji)
In ZWJ + skin-tone + gender sequences, VS16 appears many bytes after the first rune:
graphemeWidthreturned 1 for all of these. Terminals render them as 2.Bug B: Skin-tone modifier on text-default base (~60 emoji)
When a skin-tone modifier follows a text-default
Extended_Pictographicbase (like🕵🏻detective + skin tone), the sequence forms anemoji_modifier_sequenceper UTS#51 ED-13 and is always rendered in emoji presentation. But there was no skin-tone modifier detection.Examples:
🕵🏻,☝🏽,✌🏾,✍🏿,🖐🏻,⛹🏼,🏌🏽,🏋🏾Fix
Replaced the positional VS16 check with a scan of the remaining bytes in the grapheme cluster, looking for either:
0xEF 0xB8 0x8F(U+FE0F in UTF-8)0xF0 0x9F 0x8F 0xBB–0xF0 0x9F 0x8F 0xBF(U+1F3FB–U+1F3FF)The scan only runs when the base character is not already
_Wide, so there is no performance impact on the common case (most emoji haveEmoji_Presentation=Yesand are already wide).Tests
Added 14 new test cases covering both bug categories. All existing tests pass unchanged — zero regressions.