From df6e0cf15b040b6e250877d4ee87b38ee3e5fe6e Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Fri, 30 Oct 2026 23:47:26 -0500 Subject: [PATCH 1/4] id(web): grapheme-width parity remainders for the world presenter (#1581) - WIDE_RANGES gains U+2B1B..U+2B1C and U+2B55 (wide in unicode-width 0.2.2, previously measured 1). - An emoji-modifier sequence measures 2 as a whole, even on a narrow base (U+261D + U+1F3FB), instead of as its base alone. - Hangul jamo vowels/trailing jamo (U+1160..U+11FF) contribute 0, so composed LV/LVT clusters measure 2 like the precomposed syllable. - Only Mn/Me marks measure 0; spacing marks (Mc, e.g. lone U+0903) measure 1. - The TS-only 16-unit symbol cap is removed: the Rust validator has no symbol-length limit, and the cap refused Rust-valid cells. All widths re-verified against a Rust probe on the pinned unicode-width 0.2.2. Parity tests cover each class at its correct and wrong column counts. Full web suite: 489 passed, 1 pre-existing search-panel failure (also failing on clean main), tsc clean. --- pkgs/id/web/src/world-surface.test.ts | 54 +++++++++++++++++++++++++++ pkgs/id/web/src/world-surface.ts | 28 ++++++++++---- 2 files changed, 75 insertions(+), 7 deletions(-) diff --git a/pkgs/id/web/src/world-surface.test.ts b/pkgs/id/web/src/world-surface.test.ts index 89a61db7ba..fd58f6ddbb 100644 --- a/pkgs/id/web/src/world-surface.test.ts +++ b/pkgs/id/web/src/world-surface.test.ts @@ -107,6 +107,60 @@ describe("world screen presenter", () => { } }); + it("measures the #1581 remainder classes like Rust unicode-width 0.2.2", () => { + const root = document.createElement("div"); + const expectWide = (symbol: string) => { + 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); + expect(renderWorldSurface(root, makeScreen([{ symbol, fg: 0, bg: 0, bold: false, columns: 1 }]))).toBe(false); + }; + // âŦ› U+2B1B, âŦœ U+2B1C, ⭕ U+2B55: wide in unicode-width, previously + // missing from WIDE_RANGES and measured 1. + for (const symbol of ["\u{2B1B}", "\u{2B1C}", "\u{2B55}"]) expectWide(symbol); + // ☝đŸģ U+261D U+1F3FB: a modifier sequence measures 2 even on a + // narrow (width-1) base. + expectWide("\u{261D}\u{1F3FB}"); + // Composed Hangul jamo: LV U+1100 U+1161 and LVT + U+11A8 measure + // as the precomposed syllable — 2, not the per-component sum. + expectWide("\u{1100}\u{1161}"); + expectWide("\u{1100}\u{1161}\u{11A8}"); + + // A lone spacing mark (U+0903, category Mc) measures 1, not 0: + // renders in 1 column and is refused in 2. + expect(renderWorldSurface(root, makeScreen([{ symbol: "\u{0903}", fg: 0, bg: 0, bold: false, columns: 1 }]))).toBe( + true, + ); + const markInWide: ScreenSurface = { + kind: "screen", + version: 1, + width: 2, + height: 1, + cells: [ + { symbol: "\u{0903}", fg: 0, bg: 0, bold: false, columns: 2 }, + { symbol: "", fg: 0, bg: 0, bold: false, columns: 0 }, + ], + }; + expect(renderWorldSurface(root, markInWide)).toBe(false); + + // One grapheme of 18 UTF-16 units (e + 17 combining acutes, width + // 1) renders: there is no symbol-length cap, matching the Rust + // validator. + const longGrapheme = `e${"\u0301".repeat(17)}`; + expect(longGrapheme.length).toBe(18); + expect(renderWorldSurface(root, makeScreen([{ symbol: longGrapheme, fg: 0, bg: 0, bold: false, columns: 1 }]))).toBe( + true, + ); + }); + 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 08a6090ade..e29a6479cc 100644 --- a/pkgs/id/web/src/world-surface.ts +++ b/pkgs/id/web/src/world-surface.ts @@ -60,6 +60,8 @@ function xtermColor(index: number): string | null { const WIDE_RANGES: ReadonlyArray = [ [0x1100, 0x115f], [0x2329, 0x232a], + [0x2b1b, 0x2b1c], + [0x2b55, 0x2b55], [0x2e80, 0x303e], [0x3040, 0x33ff], [0x3400, 0x4dbf], @@ -130,7 +132,10 @@ function isWideCodePoint(codePoint: number): boolean { return WIDE_RANGES.some(([low, high]) => codePoint >= low && codePoint <= high); } -const COMBINING_MARK = /^\p{M}$/u; +// Non-spacing and enclosing marks (Mn, Me) measure 0 under unicode-width +// 0.2, but spacing marks (Mc, e.g. U+0903) measure 1, like a base character +// — a blanket \p{M} rule diverged on lone spacing marks (#1581). +const NON_SPACING_MARK = /^(?:\p{Mn}|\p{Me})$/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 { @@ -138,8 +143,12 @@ function codePointWidth(character: string, codePoint: number): number { // 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; + // Hangul jamo vowels and trailing jamo (U+1160..U+11FF) contribute 0: + // the lead jamo carries the composed syllable's width 2, so an LV/LVT + // cluster measures 2 in total, not per component (#1581). + if (codePoint >= 0x1160 && codePoint <= 0x11ff) return 0; if (isWideCodePoint(codePoint)) return 2; - if (COMBINING_MARK.test(character)) return 0; + if (NON_SPACING_MARK.test(character)) return 0; return 1; } @@ -148,9 +157,11 @@ 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). + * An emoji-modifier sequence (base + U+1F3FB..U+1F3FF) measures 2 as a + * whole under unicode-width 0.2 — even when the base alone is narrow + * (☝ U+261D is width 1, ☝đŸģ is width 2) — and tag characters contribute + * 0 via codePointWidth, so a tag sequence measures as its base (#1431, + * #1581). */ function graphemeWidth(symbol: string): number { const characters = Array.from(symbol); @@ -163,7 +174,7 @@ function graphemeWidth(symbol: string): number { 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("")); + if (base.length > 0) return 2; } return characters.reduce((total, character, i) => total + codePointWidth(character, codePoints[i]), 0); } @@ -205,10 +216,13 @@ export function renderWorldSurface(root: HTMLElement, surface: ScreenSurface): b for (let index = 0; index < surface.cells.length; index++) { const cell = surface.cells[index]; + // No symbol-length cap: the Rust validator imposes none — only + // exactly-one-grapheme and width 1–2 (checked below) — and a TS-only + // cap refused Rust-valid cells such as a base letter carrying many + // combining marks (#1581). The two validators must not disagree. if ( !cell || typeof cell.symbol !== "string" || - cell.symbol.length > 16 || Array.from(cell.symbol).some((character) => { const codePoint = character.codePointAt(0) ?? 0; return codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f); From 5be1c505ceb1a28ea1023d40fd6442fe6824961c Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sat, 31 Oct 2026 00:22:09 -0500 Subject: [PATCH 2/4] id(web): Grapheme_Extend spacing marks measure zero in the presenter (#1581) Codex P2 on df6e0cf15: the Mc-to-width-1 change regressed Bengali U+0995 U+09BE and Oriya U+0B15 U+0B3E, whose spacing marks also carry Grapheme_Extend, which unicode-width 0.2 treats as zero-width. codePointWidth now returns 0 for Grapheme_Extend code points (after the wide check, so emoji modifiers are unaffected); U+0903 (Mc without Grapheme_Extend) still measures 1. Regression coverage for both clusters added beside the U+0903 case. Web tests: world-surface 7/7 pass, tsc clean; the one full-suite failure (search-panel) is pre-existing on the branch base, proven by stash-and-rerun. --- pkgs/id/web/src/world-surface.test.ts | 19 +++++++++++++++++++ pkgs/id/web/src/world-surface.ts | 9 ++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/pkgs/id/web/src/world-surface.test.ts b/pkgs/id/web/src/world-surface.test.ts index fd58f6ddbb..5379c45b41 100644 --- a/pkgs/id/web/src/world-surface.test.ts +++ b/pkgs/id/web/src/world-surface.test.ts @@ -151,6 +151,25 @@ describe("world screen presenter", () => { }; expect(renderWorldSurface(root, markInWide)).toBe(false); + // Spacing marks with Grapheme_Extend measure 0 in a cluster, like + // unicode-width 0.2: Bengali āĻ•āĻž (U+0995 U+09BE) and Oriya āŦ•āŦž + // (U+0B15 U+0B3E) are single graphemes measuring 1 column in total + // — they render in 1 column and are refused in 2. + for (const symbol of ["\u{0995}\u{09BE}", "\u{0B15}\u{0B3E}"]) { + expect(renderWorldSurface(root, makeScreen([{ symbol, fg: 0, bg: 0, bold: false, columns: 1 }]))).toBe(true); + const clusterInWide: 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, clusterInWide)).toBe(false); + } + // One grapheme of 18 UTF-16 units (e + 17 combining acutes, width // 1) renders: there is no symbol-length cap, matching the Rust // validator. diff --git a/pkgs/id/web/src/world-surface.ts b/pkgs/id/web/src/world-surface.ts index e29a6479cc..4313ec35e3 100644 --- a/pkgs/id/web/src/world-surface.ts +++ b/pkgs/id/web/src/world-surface.ts @@ -137,6 +137,13 @@ function isWideCodePoint(codePoint: number): boolean { // — a blanket \p{M} rule diverged on lone spacing marks (#1581). const NON_SPACING_MARK = /^(?:\p{Mn}|\p{Me})$/u; +// Spacing marks that also carry Grapheme_Extend are the exception: +// unicode-width 0.2 treats Grapheme_Extend as zero-width, so Bengali +// U+09BE and Oriya U+0B3E (both Mc) measure 0 in a cluster — āĻ•āĻž is +// one column, not two — while U+0903 (Mc without Grapheme_Extend) +// keeps measuring 1 above (#1581, Codex review on df6e0cf15). +const GRAPHEME_EXTEND = /^\p{Grapheme_Extend}$/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; @@ -148,7 +155,7 @@ function codePointWidth(character: string, codePoint: number): number { // cluster measures 2 in total, not per component (#1581). if (codePoint >= 0x1160 && codePoint <= 0x11ff) return 0; if (isWideCodePoint(codePoint)) return 2; - if (NON_SPACING_MARK.test(character)) return 0; + if (NON_SPACING_MARK.test(character) || GRAPHEME_EXTEND.test(character)) return 0; return 1; } From 91c786767e288c1e9734f865cbfda6ed72ede700 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sat, 31 Oct 2026 00:55:23 -0500 Subject: [PATCH 3/4] id(web): measure U+FE0E text-presentation sequences as width 1 (#1581) unicode-width 0.2.2 narrows a default-emoji-presentation code point from 2 to 1 when U+FE0E (VS15) immediately follows it, but graphemeWidth summed the wide base and the zero-width selector to 2, refusing valid one-column surfaces (Codex/CodeRabbit review on the #1581 fix). Add TEXT_PRESENTATION_RANGES (the 150 code points verified exhaustively against the pinned crate), subtract the extra column in the plain sum when U+FE0E immediately follows one, and add the 57 BMP bases that were also missing from WIDE_RANGES (they measured 1 alone, Rust 2). Differential over U+0020..U+2FFFF, alone and +U+FE0E: 57 alone and 93 text-presentation divergences fixed, zero new. Tests: world-surface 8/8, tsc clean; full web suite has the same single pre-existing search-panel aria-label failure as clean HEAD. --- pkgs/id/web/src/world-surface.test.ts | 80 ++++++++++++++ pkgs/id/web/src/world-surface.ts | 146 +++++++++++++++++++++++++- 2 files changed, 224 insertions(+), 2 deletions(-) diff --git a/pkgs/id/web/src/world-surface.test.ts b/pkgs/id/web/src/world-surface.test.ts index 5379c45b41..1964bdbe6a 100644 --- a/pkgs/id/web/src/world-surface.test.ts +++ b/pkgs/id/web/src/world-surface.test.ts @@ -180,6 +180,86 @@ describe("world screen presenter", () => { ); }); + it("measures U+FE0E text-presentation sequences as width 1, like Rust unicode-width 0.2.2 (#1581)", () => { + const root = document.createElement("div"); + const expectNarrow = (symbol: string) => { + expect(renderWorldSurface(root, makeScreen([{ symbol, fg: 0, bg: 0, bold: false, columns: 1 }]))).toBe(true); + const wideScreen: 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, wideScreen)).toBe(false); + }; + // Default-emoji-presentation bases narrow to 1 with U+FE0E (VS15): + // âŦ›ī¸Ž U+2B1B, âŦœī¸Ž U+2B1C, â­•ī¸Ž U+2B55 (the classes the wide fix added), + // âŒšī¸Ž U+231A, â­ī¸Ž U+2B50, and astral đŸ•ī¸Ž U+1F550. + for (const symbol of [ + "\u{2B1B}\u{FE0E}", + "\u{2B1C}\u{FE0E}", + "\u{2B55}\u{FE0E}", + "\u{231A}\u{FE0E}", + "\u{2B50}\u{FE0E}", + "\u{1F550}\u{FE0E}", + ]) { + expectNarrow(symbol); + } + // The same bases alone (no U+FE0E) are wide: ⌚ U+231A and + // ⭐ U+2B50 render in 2 columns and are refused in 1. + for (const symbol of ["\u{231A}", "\u{2B50}"]) { + 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); + expect(renderWorldSurface(root, makeScreen([{ symbol, fg: 0, bg: 0, bold: false, columns: 1 }]))).toBe(false); + } + // A combining mark between the base and U+FE0E breaks the narrowing: + // U+2B1B U+0301 U+FE0E still measures 2 under unicode-width 0.2.2. + const interrupted = "\u{2B1B}\u{0301}\u{FE0E}"; + const interruptedScreen: ScreenSurface = { + kind: "screen", + version: 1, + width: 2, + height: 1, + cells: [ + { symbol: interrupted, fg: 0, bg: 0, bold: false, columns: 2 }, + { symbol: "", fg: 0, bg: 0, bold: false, columns: 0 }, + ], + }; + expect(renderWorldSurface(root, interruptedScreen)).toBe(true); + expect(renderWorldSurface(root, makeScreen([{ symbol: interrupted, fg: 0, bg: 0, bold: false, columns: 1 }]))).toBe( + false, + ); + // Wide code points outside the text-presentation set keep width 2 + // with U+FE0E: đŸ˜€ī¸Ž U+1F600 U+FE0E and æŧĸī¸Ž U+6F22 U+FE0E. + for (const symbol of ["\u{1F600}\u{FE0E}", "\u{6F22}\u{FE0E}"]) { + 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); + 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 4313ec35e3..ea295aeb10 100644 --- a/pkgs/id/web/src/world-surface.ts +++ b/pkgs/id/web/src/world-surface.ts @@ -59,8 +59,42 @@ function xtermColor(index: number): string | null { // (U+20000–U+2FFFD, U+30000–U+3FFFD). const WIDE_RANGES: ReadonlyArray = [ [0x1100, 0x115f], + // BMP default-emoji-presentation ranges (the BMP part of + // TEXT_PRESENTATION_RANGES below): wide alone under unicode-width + // 0.2.2, narrowing to 1 with U+FE0E (#1581). + [0x231a, 0x231b], [0x2329, 0x232a], + [0x23e9, 0x23ec], + [0x23f0, 0x23f0], + [0x23f3, 0x23f3], + [0x25fd, 0x25fe], + [0x2614, 0x2615], + [0x2648, 0x2653], + [0x267f, 0x267f], + [0x2693, 0x2693], + [0x26a1, 0x26a1], + [0x26aa, 0x26ab], + [0x26bd, 0x26be], + [0x26c4, 0x26c5], + [0x26ce, 0x26ce], + [0x26d4, 0x26d4], + [0x26ea, 0x26ea], + [0x26f2, 0x26f3], + [0x26f5, 0x26f5], + [0x26fa, 0x26fa], + [0x26fd, 0x26fd], + [0x2705, 0x2705], + [0x270a, 0x270b], + [0x2728, 0x2728], + [0x274c, 0x274c], + [0x274e, 0x274e], + [0x2753, 0x2755], + [0x2757, 0x2757], + [0x2795, 0x2797], + [0x27b0, 0x27b0], + [0x27bf, 0x27bf], [0x2b1b, 0x2b1c], + [0x2b50, 0x2b50], [0x2b55, 0x2b55], [0x2e80, 0x303e], [0x3040, 0x33ff], @@ -132,6 +166,102 @@ function isWideCodePoint(codePoint: number): boolean { return WIDE_RANGES.some(([low, high]) => codePoint >= low && codePoint <= high); } +// Code points that measure 2 alone but narrow to 1 when immediately +// followed by U+FE0E (variation selector 15, text presentation) under +// unicode-width 0.2 — the crate's default-emoji-presentation set (e.g. +// âŦ› U+2B1B alone is 2 columns, âŦ›ī¸Ž U+2B1B U+FE0E is 1). Most wide code +// points are NOT in this set: U+1F600 and CJK keep width 2 with U+FE0E. +// Verified exhaustively against unicode-width 0.2.2 (#1581). +const TEXT_PRESENTATION_RANGES: ReadonlyArray = [ + [0x231a, 0x231b], + [0x23e9, 0x23ec], + [0x23f0, 0x23f0], + [0x23f3, 0x23f3], + [0x25fd, 0x25fe], + [0x2614, 0x2615], + [0x2648, 0x2653], + [0x267f, 0x267f], + [0x2693, 0x2693], + [0x26a1, 0x26a1], + [0x26aa, 0x26ab], + [0x26bd, 0x26be], + [0x26c4, 0x26c5], + [0x26ce, 0x26ce], + [0x26d4, 0x26d4], + [0x26ea, 0x26ea], + [0x26f2, 0x26f3], + [0x26f5, 0x26f5], + [0x26fa, 0x26fa], + [0x26fd, 0x26fd], + [0x2705, 0x2705], + [0x270a, 0x270b], + [0x2728, 0x2728], + [0x274c, 0x274c], + [0x274e, 0x274e], + [0x2753, 0x2755], + [0x2757, 0x2757], + [0x2795, 0x2797], + [0x27b0, 0x27b0], + [0x27bf, 0x27bf], + [0x2b1b, 0x2b1c], + [0x2b50, 0x2b50], + [0x2b55, 0x2b55], + [0x1f004, 0x1f004], + [0x1f30d, 0x1f30f], + [0x1f315, 0x1f315], + [0x1f31c, 0x1f31c], + [0x1f378, 0x1f378], + [0x1f393, 0x1f393], + [0x1f3a7, 0x1f3a7], + [0x1f3ac, 0x1f3ae], + [0x1f3c2, 0x1f3c2], + [0x1f3c4, 0x1f3c4], + [0x1f3c6, 0x1f3c6], + [0x1f3ca, 0x1f3ca], + [0x1f3e0, 0x1f3e0], + [0x1f3ed, 0x1f3ed], + [0x1f408, 0x1f408], + [0x1f415, 0x1f415], + [0x1f41f, 0x1f41f], + [0x1f426, 0x1f426], + [0x1f442, 0x1f442], + [0x1f446, 0x1f449], + [0x1f44d, 0x1f44e], + [0x1f453, 0x1f453], + [0x1f46a, 0x1f46a], + [0x1f47d, 0x1f47d], + [0x1f4a3, 0x1f4a3], + [0x1f4b0, 0x1f4b0], + [0x1f4b3, 0x1f4b3], + [0x1f4bb, 0x1f4bb], + [0x1f4bf, 0x1f4bf], + [0x1f4cb, 0x1f4cb], + [0x1f4da, 0x1f4da], + [0x1f4df, 0x1f4df], + [0x1f4e4, 0x1f4e6], + [0x1f4ea, 0x1f4ed], + [0x1f4f7, 0x1f4f7], + [0x1f4f9, 0x1f4fb], + [0x1f508, 0x1f508], + [0x1f50d, 0x1f50d], + [0x1f512, 0x1f513], + [0x1f550, 0x1f567], + [0x1f610, 0x1f610], + [0x1f687, 0x1f687], + [0x1f68d, 0x1f68d], + [0x1f691, 0x1f691], + [0x1f694, 0x1f694], + [0x1f698, 0x1f698], + [0x1f6ad, 0x1f6ad], + [0x1f6b2, 0x1f6b2], + [0x1f6b9, 0x1f6ba], + [0x1f6bc, 0x1f6bc], +]; + +function isTextPresentationCodePoint(codePoint: number): boolean { + return TEXT_PRESENTATION_RANGES.some(([low, high]) => codePoint >= low && codePoint <= high); +} + // Non-spacing and enclosing marks (Mn, Me) measure 0 under unicode-width // 0.2, but spacing marks (Mc, e.g. U+0903) measure 1, like a base character // — a blanket \p{M} rule diverged on lone spacing marks (#1581). @@ -168,7 +298,10 @@ function codePointWidth(character: string, codePoint: number): number { * whole under unicode-width 0.2 — even when the base alone is narrow * (☝ U+261D is width 1, ☝đŸģ is width 2) — and tag characters contribute * 0 via codePointWidth, so a tag sequence measures as its base (#1431, - * #1581). + * #1581). U+FE0E (text presentation) immediately after a + * default-emoji-presentation code point narrows it from 2 to 1 in the + * plain sum below (see TEXT_PRESENTATION_RANGES); after any other code + * point it changes nothing. */ function graphemeWidth(symbol: string): number { const characters = Array.from(symbol); @@ -183,7 +316,16 @@ function graphemeWidth(symbol: string): number { // A cluster of only modifiers falls through to the plain sum. if (base.length > 0) return 2; } - return characters.reduce((total, character, i) => total + codePointWidth(character, codePoints[i]), 0); + return characters.reduce((total, character, i) => { + const width = codePointWidth(character, codePoints[i]); + // U+FE0E itself measures 0 (handled in codePointWidth); when it + // immediately follows a text-presentation code point, that point + // counts 1 instead of 2, so subtract the extra column (#1581). + if (codePoints[i] === 0xfe0e && i > 0 && isTextPresentationCodePoint(codePoints[i - 1])) { + return total + width - 1; + } + return total + width; + }, 0); } const segmenter = From 81797ebfe3f1d60b7169c93627cad37aa0468b2c Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sat, 31 Oct 2026 01:40:36 -0500 Subject: [PATCH 4/4] id(web): apply U+FE0E narrowing before the ZWJ width shortcut (#1581) --- pkgs/id/web/src/world-surface.test.ts | 5 +++++ pkgs/id/web/src/world-surface.ts | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pkgs/id/web/src/world-surface.test.ts b/pkgs/id/web/src/world-surface.test.ts index 1964bdbe6a..a2a3a5314e 100644 --- a/pkgs/id/web/src/world-surface.test.ts +++ b/pkgs/id/web/src/world-surface.test.ts @@ -206,6 +206,11 @@ describe("world screen presenter", () => { "\u{231A}\u{FE0E}", "\u{2B50}\u{FE0E}", "\u{1F550}\u{FE0E}", + // A trailing ZWJ does not undo the narrowing: the ZWJ shortcut + // must apply text-presentation narrowing first, so + // U+2B1B U+FE0E U+200D still measures 1 (Codex review on #2945). + "\u{2B1B}\u{FE0E}\u{200D}", + "\u{1F550}\u{FE0E}\u{200D}", ]) { expectNarrow(symbol); } diff --git a/pkgs/id/web/src/world-surface.ts b/pkgs/id/web/src/world-surface.ts index ea295aeb10..29de837fc3 100644 --- a/pkgs/id/web/src/world-surface.ts +++ b/pkgs/id/web/src/world-surface.ts @@ -307,7 +307,16 @@ function graphemeWidth(symbol: string): number { const characters = Array.from(symbol); const codePoints = characters.map((character) => character.codePointAt(0) ?? 0); if (codePoints.includes(0x200d)) { - return codePoints.some((codePoint) => isWideCodePoint(codePoint)) ? 2 : 1; + // U+FE0E narrowing applies before the ZWJ shortcut: a + // text-presentation code point immediately followed by U+FE0E + // counts 1, so a ZWJ sequence whose only wide point is narrowed + // (e.g. U+2B1B U+FE0E U+200D) measures 1 under unicode-width + // 0.2.2, not 2 (#1581, Codex review on PR #2945). + const narrowed = new Set(); + codePoints.forEach((codePoint, i) => { + if (codePoint === 0xfe0e && i > 0 && isTextPresentationCodePoint(codePoints[i - 1])) narrowed.add(i - 1); + }); + return codePoints.some((codePoint, i) => !narrowed.has(i) && isWideCodePoint(codePoint)) ? 2 : 1; } if (codePoints.includes(0xfe0f)) return 2; const isModifier = (codePoint: number) => codePoint >= 0x1f3fb && codePoint <= 0x1f3ff;