diff --git a/pkgs/id/web/scripts/generate-emoji-presentation-fixture.py b/pkgs/id/web/scripts/generate-emoji-presentation-fixture.py new file mode 100644 index 000000000..ce5ddea3b --- /dev/null +++ b/pkgs/id/web/scripts/generate-emoji-presentation-fixture.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 +"""Regenerate the EMOJI_PRESENTATION_SEQ_RANGES parity fixture. + +Usage: generate-emoji-presentation-fixture.py [output.json] + +Decodes the EMOJI_PRESENTATION_LEAVES bit table from unicode-width 0.2.2's +src/tables.rs exactly the way the crate's starts_emoji_presentation_seq +lookup does (top bits select the leaf row, bits 3..9 index the byte, the +low 3 bits select the bit), sweeps every code point, and writes the +resulting membership ranges as the fixture consumed by +src/world-surface.test.ts (#10819). The committed fixture was generated +from the unicode-width 0.2.2 registry source; re-run against a newer +crate only together with a deliberate table update in world-surface.ts. +""" +import json +import re +import sys + +TOP_MAP = {0x0: 0, 0x8: 1, 0x9: 2, 0xA: 3, 0xC: 4, 0x7C: 5, 0x7D: 6} + + +def main() -> None: + tables_rs = sys.argv[1] + out_path = sys.argv[2] if len(sys.argv) > 2 else "src/test-fixtures/emoji-presentation-seq-ranges.json" + src = open(tables_rs).read() + i = src.index("static EMOJI_PRESENTATION_LEAVES") + block=src[i:src.index("]);",i)] + nums = [int(x, 16) for x in re.findall(r"0x[0-9A-Fa-f]+", block)] + assert len(nums) == 7 * 128, f"expected 896 leaf bytes, parsed {len(nums)}" + leaves = [nums[k * 128:(k + 1) * 128] for k in range(7)] + + def member(cp: int) -> bool: + leaf = TOP_MAP.get(cp >> 10) + if leaf is None: + return False + return ((leaves[leaf][(cp >> 3) & 0x7F] >> (cp & 7)) & 1) == 1 + + ranges = [] + start = None + for cp in range(0x110000): + cur = False if 0xD800 <= cp <= 0xDFFF else member(cp) + if cur and start is None: + start = cp + if not cur and start is not None: + ranges.append([start, cp - 1]) + start = None + if start is not None: + ranges.append([start, 0x10FFFF]) + + fixture = { + "description": "Code point ranges that can start an emoji presentation sequence (be followed by U+FE0F to form a width-2 cluster), decoded from unicode-width 0.2.2's EMOJI_PRESENTATION_LEAVES bit table. Pins the hand transcription in world-surface.ts (EMOJI_PRESENTATION_SEQ_RANGES) against the crate (#10819).", + "source": "unicode-width 0.2.2, src/tables.rs, starts_emoji_presentation_seq / EMOJI_PRESENTATION_LEAVES", + "generated_by": "scripts/generate-emoji-presentation-fixture.py", + "range_count": len(ranges), + "code_point_count": sum(h - l + 1 for l, h in ranges), + "ranges": ranges, + } + with open(out_path, "w") as fh: + json.dump(fixture, fh, indent=2) + fh.write("\n") + print(f"wrote {out_path}: {fixture['range_count']} ranges, {fixture['code_point_count']} code points") + + +if __name__ == "__main__": + main() diff --git a/pkgs/id/web/src/test-fixtures/emoji-presentation-seq-ranges.json b/pkgs/id/web/src/test-fixtures/emoji-presentation-seq-ranges.json new file mode 100644 index 000000000..894b1274f --- /dev/null +++ b/pkgs/id/web/src/test-fixtures/emoji-presentation-seq-ranges.json @@ -0,0 +1,741 @@ +{ + "description": "Code point ranges that can start an emoji presentation sequence (be followed by U+FE0F to form a width-2 cluster), decoded from unicode-width 0.2.2's EMOJI_PRESENTATION_LEAVES bit table. Pins the hand transcription in world-surface.ts (EMOJI_PRESENTATION_SEQ_RANGES) against the crate (#10819).", + "source": "unicode-width 0.2.2, src/tables.rs, starts_emoji_presentation_seq / EMOJI_PRESENTATION_LEAVES", + "generated_by": "scripts/generate-emoji-presentation-fixture.py", + "range_count": 183, + "code_point_count": 371, + "ranges": [ + [ + 35, + 35 + ], + [ + 42, + 42 + ], + [ + 48, + 57 + ], + [ + 169, + 169 + ], + [ + 174, + 174 + ], + [ + 8252, + 8252 + ], + [ + 8265, + 8265 + ], + [ + 8482, + 8482 + ], + [ + 8505, + 8505 + ], + [ + 8596, + 8601 + ], + [ + 8617, + 8618 + ], + [ + 8986, + 8987 + ], + [ + 9000, + 9000 + ], + [ + 9167, + 9167 + ], + [ + 9193, + 9203 + ], + [ + 9208, + 9210 + ], + [ + 9410, + 9410 + ], + [ + 9642, + 9643 + ], + [ + 9654, + 9654 + ], + [ + 9664, + 9664 + ], + [ + 9723, + 9726 + ], + [ + 9728, + 9732 + ], + [ + 9742, + 9742 + ], + [ + 9745, + 9745 + ], + [ + 9748, + 9749 + ], + [ + 9752, + 9752 + ], + [ + 9757, + 9757 + ], + [ + 9760, + 9760 + ], + [ + 9762, + 9763 + ], + [ + 9766, + 9766 + ], + [ + 9770, + 9770 + ], + [ + 9774, + 9775 + ], + [ + 9784, + 9786 + ], + [ + 9792, + 9792 + ], + [ + 9794, + 9794 + ], + [ + 9800, + 9811 + ], + [ + 9823, + 9824 + ], + [ + 9827, + 9827 + ], + [ + 9829, + 9830 + ], + [ + 9832, + 9832 + ], + [ + 9851, + 9851 + ], + [ + 9854, + 9855 + ], + [ + 9874, + 9879 + ], + [ + 9881, + 9881 + ], + [ + 9883, + 9884 + ], + [ + 9888, + 9889 + ], + [ + 9895, + 9895 + ], + [ + 9898, + 9899 + ], + [ + 9904, + 9905 + ], + [ + 9917, + 9918 + ], + [ + 9924, + 9925 + ], + [ + 9928, + 9928 + ], + [ + 9934, + 9935 + ], + [ + 9937, + 9937 + ], + [ + 9939, + 9940 + ], + [ + 9961, + 9962 + ], + [ + 9968, + 9973 + ], + [ + 9975, + 9978 + ], + [ + 9981, + 9981 + ], + [ + 9986, + 9986 + ], + [ + 9989, + 9989 + ], + [ + 9992, + 9997 + ], + [ + 9999, + 9999 + ], + [ + 10002, + 10002 + ], + [ + 10004, + 10004 + ], + [ + 10006, + 10006 + ], + [ + 10013, + 10013 + ], + [ + 10017, + 10017 + ], + [ + 10024, + 10024 + ], + [ + 10035, + 10036 + ], + [ + 10052, + 10052 + ], + [ + 10055, + 10055 + ], + [ + 10060, + 10060 + ], + [ + 10062, + 10062 + ], + [ + 10067, + 10069 + ], + [ + 10071, + 10071 + ], + [ + 10083, + 10084 + ], + [ + 10133, + 10135 + ], + [ + 10145, + 10145 + ], + [ + 10160, + 10160 + ], + [ + 10175, + 10175 + ], + [ + 10548, + 10549 + ], + [ + 11013, + 11015 + ], + [ + 11035, + 11036 + ], + [ + 11088, + 11088 + ], + [ + 11093, + 11093 + ], + [ + 12336, + 12336 + ], + [ + 12349, + 12349 + ], + [ + 12951, + 12951 + ], + [ + 12953, + 12953 + ], + [ + 126980, + 126980 + ], + [ + 127344, + 127345 + ], + [ + 127358, + 127359 + ], + [ + 127490, + 127490 + ], + [ + 127514, + 127514 + ], + [ + 127535, + 127535 + ], + [ + 127543, + 127543 + ], + [ + 127757, + 127759 + ], + [ + 127765, + 127765 + ], + [ + 127772, + 127772 + ], + [ + 127777, + 127777 + ], + [ + 127780, + 127788 + ], + [ + 127798, + 127798 + ], + [ + 127864, + 127864 + ], + [ + 127869, + 127869 + ], + [ + 127891, + 127891 + ], + [ + 127894, + 127895 + ], + [ + 127897, + 127899 + ], + [ + 127902, + 127903 + ], + [ + 127911, + 127911 + ], + [ + 127916, + 127918 + ], + [ + 127938, + 127938 + ], + [ + 127940, + 127940 + ], + [ + 127942, + 127942 + ], + [ + 127946, + 127950 + ], + [ + 127956, + 127968 + ], + [ + 127981, + 127981 + ], + [ + 127987, + 127987 + ], + [ + 127989, + 127989 + ], + [ + 127991, + 127991 + ], + [ + 128008, + 128008 + ], + [ + 128021, + 128021 + ], + [ + 128031, + 128031 + ], + [ + 128038, + 128038 + ], + [ + 128063, + 128063 + ], + [ + 128065, + 128066 + ], + [ + 128070, + 128073 + ], + [ + 128077, + 128078 + ], + [ + 128083, + 128083 + ], + [ + 128106, + 128106 + ], + [ + 128125, + 128125 + ], + [ + 128163, + 128163 + ], + [ + 128176, + 128176 + ], + [ + 128179, + 128179 + ], + [ + 128187, + 128187 + ], + [ + 128191, + 128191 + ], + [ + 128203, + 128203 + ], + [ + 128218, + 128218 + ], + [ + 128223, + 128223 + ], + [ + 128228, + 128230 + ], + [ + 128234, + 128237 + ], + [ + 128247, + 128247 + ], + [ + 128249, + 128251 + ], + [ + 128253, + 128253 + ], + [ + 128264, + 128264 + ], + [ + 128269, + 128269 + ], + [ + 128274, + 128275 + ], + [ + 128329, + 128330 + ], + [ + 128336, + 128359 + ], + [ + 128367, + 128368 + ], + [ + 128371, + 128377 + ], + [ + 128391, + 128391 + ], + [ + 128394, + 128397 + ], + [ + 128400, + 128400 + ], + [ + 128421, + 128421 + ], + [ + 128424, + 128424 + ], + [ + 128433, + 128434 + ], + [ + 128444, + 128444 + ], + [ + 128450, + 128452 + ], + [ + 128465, + 128467 + ], + [ + 128476, + 128478 + ], + [ + 128481, + 128481 + ], + [ + 128483, + 128483 + ], + [ + 128488, + 128488 + ], + [ + 128495, + 128495 + ], + [ + 128499, + 128499 + ], + [ + 128506, + 128506 + ], + [ + 128528, + 128528 + ], + [ + 128647, + 128647 + ], + [ + 128653, + 128653 + ], + [ + 128657, + 128657 + ], + [ + 128660, + 128660 + ], + [ + 128664, + 128664 + ], + [ + 128685, + 128685 + ], + [ + 128690, + 128690 + ], + [ + 128697, + 128698 + ], + [ + 128700, + 128700 + ], + [ + 128715, + 128715 + ], + [ + 128717, + 128719 + ], + [ + 128736, + 128741 + ], + [ + 128745, + 128745 + ], + [ + 128752, + 128752 + ], + [ + 128755, + 128755 + ] + ] +} diff --git a/pkgs/id/web/src/world-surface.test.ts b/pkgs/id/web/src/world-surface.test.ts index 3cace264e..288892c14 100644 --- a/pkgs/id/web/src/world-surface.test.ts +++ b/pkgs/id/web/src/world-surface.test.ts @@ -1,6 +1,7 @@ import { describe, expect, it } from "vitest"; -import { applyScreenPatch, renderWorldSurface, type ScreenSurface } from "./world-surface"; +import { applyScreenPatch, isEmojiPresentationSeqBase, renderWorldSurface, type ScreenSurface } from "./world-surface"; import screenCellParityCorpus from "./test-fixtures/screen-cell-parity.json"; +import emojiPresentationFixture from "./test-fixtures/emoji-presentation-seq-ranges.json"; function makeScreen(cells: ScreenSurface["cells"]): ScreenSurface { return { kind: "screen", version: 1, width: cells.length, height: 1, cells }; @@ -522,3 +523,47 @@ describe("world screen presenter", () => { } }); }); + +describe("EMOJI_PRESENTATION_SEQ_RANGES parity with unicode-width 0.2.2 (#10819)", () => { + // The fixture is decoded from the crate's EMOJI_PRESENTATION_LEAVES bit + // table by scripts/generate-emoji-presentation-fixture.py; the ranges in + // world-surface.ts are a hand transcription of the same table (#4355). + // Any future edit to a range edge must fail here, not in production. + const fixtureRanges = emojiPresentationFixture.ranges as Array<[number, number]>; + const hex = (cp: number): string => `U+${cp.toString(16).toUpperCase()}`; + + it("fixture carries the full crate table (183 ranges, 371 code points)", () => { + expect(emojiPresentationFixture.range_count).toBe(183); + expect(emojiPresentationFixture.code_point_count).toBe(371); + expect(fixtureRanges.length).toBe(183); + expect(fixtureRanges.reduce((total, [low, high]) => total + high - low + 1, 0)).toBe(371); + }); + + it("matches the crate at every range boundary (low-1, low, high, high+1)", () => { + for (const [low, high] of fixtureRanges) { + expect(isEmojiPresentationSeqBase(low), `${hex(low)} (range low) must start a sequence`).toBe(true); + expect(isEmojiPresentationSeqBase(high), `${hex(high)} (range high) must start a sequence`).toBe(true); + if (low > 0) { + expect(isEmojiPresentationSeqBase(low - 1), `${hex(low - 1)} (low-1) must not start a sequence`).toBe(false); + } + if (high < 0x10ffff) { + expect(isEmojiPresentationSeqBase(high + 1), `${hex(high + 1)} (high+1) must not start a sequence`).toBe(false); + } + } + }); + + it("reproduces the fixture ranges exactly over the whole codespace", () => { + const derived: Array<[number, number]> = []; + let start: number | null = null; + for (let cp = 0; cp <= 0x10ffff; cp += 1) { + const member = isEmojiPresentationSeqBase(cp); + if (member && start === null) start = cp; + if (!member && start !== null) { + derived.push([start, cp - 1]); + start = null; + } + } + if (start !== null) derived.push([start, 0x10ffff]); + expect(derived).toEqual(fixtureRanges); + }, 60_000); +}); diff --git a/pkgs/id/web/src/world-surface.ts b/pkgs/id/web/src/world-surface.ts index bdb97d118..5db76389a 100644 --- a/pkgs/id/web/src/world-surface.ts +++ b/pkgs/id/web/src/world-surface.ts @@ -537,7 +537,8 @@ const EMOJI_PRESENTATION_SEQ_RANGES: ReadonlyArray = [0x1f6f3, 0x1f6f3], ]; -function isEmojiPresentationSeqBase(codePoint: number): boolean { +// Exported for the boundary/parity test against the unicode-width 0.2.2 fixture (#10819). +export function isEmojiPresentationSeqBase(codePoint: number): boolean { return EMOJI_PRESENTATION_SEQ_RANGES.some(([low, high]) => codePoint >= low && codePoint <= high); }