Skip to content

fix: detect VS16 and skin-tone modifiers anywhere in grapheme cluster#23

Open
gammons wants to merge 2 commits intoclipperhouse:mainfrom
gammons:fix-emoji-modifier-width
Open

fix: detect VS16 and skin-tone modifiers anywhere in grapheme cluster#23
gammons wants to merge 2 commits intoclipperhouse:mainfrom
gammons:fix-emoji-modifier-width

Conversation

@gammons
Copy link
Copy Markdown

@gammons gammons commented Apr 27, 2026

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-default Extended_Pictographic bases.

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:

⛹🏻‍♂️ = U+26F9 U+1F3FB U+200D U+2642 U+FE0F
         ^^^^^^                        ^^^^^^
         1st rune (3 bytes)            VS16 at byte 13
         
         s[sz:sz+3] checks bytes 3-5 → skin tone bytes, NOT VS16

graphemeWidth returned 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_Pictographic base (like 🕵🏻 detective + skin tone), the sequence forms an emoji_modifier_sequence per 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:

  • VS16: 0xEF 0xB8 0x8F (U+FE0F in UTF-8)
  • Skin-tone modifier: 0xF0 0x9F 0x8F 0xBB0xF0 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 have Emoji_Presentation=Yes and are already wide).

Tests

Added 14 new test cases covering both bug categories. All existing tests pass unchanged — zero regressions.

gammons added 2 commits April 27, 2026 14:10
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.
@clipperhouse
Copy link
Copy Markdown
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.

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