diff --git a/pkgs/id/web/src/world-surface.test.ts b/pkgs/id/web/src/world-surface.test.ts index d3fc85c353..9a5737eb58 100644 --- a/pkgs/id/web/src/world-surface.test.ts +++ b/pkgs/id/web/src/world-surface.test.ts @@ -85,6 +85,31 @@ describe("world screen presenter", () => { expect(renderWorldSurface(root, makeScreen([{ symbol: "", fg: 0, bg: 0, bold: false, columns: 1 }]))).toBe(false); }); + it("measures emoji-modifier and tag sequences as their base, like Rust unicode-width (#1431)", () => { + const root = document.createElement("div"); + // Each of these is exactly one grapheme whose Rust unicode-width 0.2 width is 2: + // ๐Ÿ‘๐Ÿฝ U+1F44D U+1F3FD (emoji modifier), ๐Ÿด๓ง๓ข๓ฅ๓ฎ๓ง๓ฟ England flag (U+1F3F4 + tag sequence). + const sequenceGlyphs = [ + "\u{1F44D}\u{1F3FD}", + "\u{1F3F4}\u{E0067}\u{E0062}\u{E0065}\u{E006E}\u{E0067}\u{E007F}", + ]; + for (const symbol of sequenceGlyphs) { + const screen: ScreenSurface = { + kind: "screen", + version: 1, + width: 2, + height: 1, + cells: [ + { symbol, fg: 0, bg: 0, bold: false, columns: 2 }, + { symbol: "", fg: 0, bg: 0, bold: false, columns: 0 }, + ], + }; + expect(renderWorldSurface(root, screen)).toBe(true); + // โ€ฆand the same glyph at a mismatched columns value must be refused. + expect(renderWorldSurface(root, makeScreen([{ symbol, fg: 0, bg: 0, bold: false, columns: 1 }]))).toBe(false); + } + }); + it("measures astral-plane, ZWJ and variation-selector glyphs as width 2, like Rust unicode-width (#761)", () => { const root = document.createElement("div"); // Each of these is exactly one grapheme whose Rust unicode-width 0.2 width is 2: diff --git a/pkgs/id/web/src/world-surface.ts b/pkgs/id/web/src/world-surface.ts index a613caa4c0..08a6090ade 100644 --- a/pkgs/id/web/src/world-surface.ts +++ b/pkgs/id/web/src/world-surface.ts @@ -135,6 +135,9 @@ const COMBINING_MARK = /^\p{M}$/u; /** Width of one code point under unicode-width 0.2: wide = 2, combining/zero-width = 0, else 1. */ function codePointWidth(character: string, codePoint: number): number { if (codePoint === 0x200d || codePoint === 0x200c || (codePoint >= 0xfe00 && codePoint <= 0xfe0f)) return 0; + // Tag characters (U+E0020..U+E007F) contribute 0: a tag sequence (e.g. the + // England flag) measures as its base alone under unicode-width 0.2 (#1431). + if (codePoint >= 0xe0020 && codePoint <= 0xe007f) return 0; if (isWideCodePoint(codePoint)) return 2; if (COMBINING_MARK.test(character)) return 0; return 1; @@ -145,6 +148,9 @@ function codePointWidth(character: string, codePoint: number): number { * a ZWJ sequence containing a wide/emoji code point measures 2 in total * (not per component), and U+FE0F (emoji presentation) forces width 2 โ€” * e.g. ๐Ÿ˜€, ๐ ฎท, ๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘ง and โš ๏ธ are all width 2, matching the Rust validator. + * An emoji modifier (U+1F3FB..U+1F3FF) contributes 0, so a modified emoji + * measures as its base alone, and tag characters contribute 0 via + * codePointWidth, so a tag sequence measures as its base (#1431). */ function graphemeWidth(symbol: string): number { const characters = Array.from(symbol); @@ -153,6 +159,12 @@ function graphemeWidth(symbol: string): number { return codePoints.some((codePoint) => isWideCodePoint(codePoint)) ? 2 : 1; } if (codePoints.includes(0xfe0f)) return 2; + const isModifier = (codePoint: number) => codePoint >= 0x1f3fb && codePoint <= 0x1f3ff; + if (codePoints.some(isModifier)) { + const base = characters.filter((_, i) => !isModifier(codePoints[i])); + // A cluster of only modifiers falls through to the plain sum. + if (base.length > 0) return graphemeWidth(base.join("")); + } return characters.reduce((total, character, i) => total + codePointWidth(character, codePoints[i]), 0); }