Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions pkgs/id/web/src/world-surface.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions pkgs/id/web/src/world-surface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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(""));
Comment on lines +164 to +166

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve two-column width for narrow emoji modifier bases

For modifier sequences such as โ˜๐Ÿฝ (U+261D U+1F3FD) and โœŒ๐Ÿฝ, stripping the modifier leaves a base outside WIDE_RANGES, so this branch returns 1. Consequently, renderWorldSurface now accepts these two-column glyphs with columns: 1 and no continuation, allowing a glyph to extend beyond its declared grid cell; previously those malformed surfaces were refused. unicode-width 0.2.2 specifies that emoji modifier sequences have width 2. Recognize valid modifier sequences as width 2 rather than unconditionally measuring their stripped base, and add a narrow-base regression case.

Useful? React with ๐Ÿ‘ย / ๐Ÿ‘Ž.

}
return characters.reduce((total, character, i) => total + codePointWidth(character, codePoints[i]), 0);
}

Expand Down
Loading