#!/usr/bin/env python3 """Generates lib/src/utils/unicode_tables.dart (cell widths and grapheme properties for the emulator). python3 -I tool/gen_unicode_tables.py > lib/src/utils/unicode_tables.dart Widths follow glibc's wcwidth (what tmux, ncurses and the shell measure with): 0 general categories Mn, Me and Cf (except U+00AD SOFT HYPHEN and the prepended concatenation marks, 1), the Hangul fillers, Hangul Jamo medial vowels and final consonants (U+1160-11FF, U+D7B0-D7FF) and U+200B; 2 East Asian Width W or F (which includes every Emoji_Presentation character); 1 everything else that prints. The Unicode version is the running Python's `unicodedata` (16.0 with Python 3.13+); `emoji-data.txt` must be the same version (https://www.unicode.org/Public//ucd/emoji/emoji-data.txt). """ import sys import unicodedata MAX = 0x110000 def ranges(predicate): out = [] start = None for cp in range(MAX): hit = predicate(cp) if hit and start is None: start = cp elif not hit and start is not None: out.append((start, cp - 1)) start = None if start is not None: out.append((start, MAX - 1)) return out # Where glibc 2.44 differs from the category rules below (checked by # comparing every assigned code point with its wcwidth in en_US.UTF-8). GLIBC_ONE = {0x00AD, *range(0x0600, 0x0606), 0x06DD, 0x070F, 0x0890, 0x0891, 0x08E2, 0xFFF9, 0xFFFA, 0xFFFB, 0x110BD, 0x110CD, *range(0x13430, 0x13440)} GLIBC_ZERO = {0x3164, 0xFFA0} GLIBC_TWO = set(range(0x3248, 0x3250)) def zero_width(cp): if cp in GLIBC_ONE: return False if cp in GLIBC_ZERO: return True if 0x1160 <= cp <= 0x11FF or 0xD7B0 <= cp <= 0xD7FF or cp == 0x200B: return True return unicodedata.category(chr(cp)) in ('Mn', 'Me', 'Cf') def wide(cp): if cp in GLIBC_TWO: return True return unicodedata.east_asian_width(chr(cp)) in ('W', 'F') def emoji_property(path, name): hits = set() with open(path, encoding='utf-8') as data: for line in data: line = line.split('#', 1)[0].strip() if not line: continue span, prop = [part.strip() for part in line.split(';')] if prop != name: continue if '..' in span: first, last = span.split('..') else: first = last = span hits.update(range(int(first, 16), int(last, 16) + 1)) return hits def emit(name, doc, spans): print(f'/// {doc}') print(f'const List {name} = [') for first, last in spans: print(f' 0x{first:X}, 0x{last:X},') print('];') print() def main(): emoji_data = sys.argv[1] emoji = emoji_property(emoji_data, 'Emoji') presentation = emoji_property(emoji_data, 'Emoji_Presentation') pictographic = emoji_property(emoji_data, 'Extended_Pictographic') modifier_base = emoji_property(emoji_data, 'Emoji_Modifier_Base') print('// GENERATED by tool/gen_unicode_tables.py from Python ' f'unicodedata {unicodedata.unidata_version} and emoji-data.txt.') print('// Do not edit by hand. Each list holds inclusive [first, last] pairs.') print() print(f"const String unicodeTablesVersion = '{unicodedata.unidata_version}';") print() emit('zeroWidthRanges', 'Code points glibc wcwidth measures as 0 columns (they join the ' 'previous cell).', ranges(zero_width)) emit('wideRanges', 'East Asian Width W or F: two columns.', ranges(wide)) emit('extendedPictographicRanges', 'Extended_Pictographic: what a ZWJ joins into one emoji.', ranges(lambda cp: cp in pictographic)) emit('emojiTextDefaultRanges', 'Emoji shown as text by default (Emoji and not Emoji_Presentation): ' 'U+FE0F after one asks for the two-column emoji form.', ranges(lambda cp: cp in emoji and cp not in presentation)) emit('emojiModifierBaseRanges', 'Emoji_Modifier_Base: a skin tone modifier after one joins it.', ranges(lambda cp: cp in modifier_base)) if __name__ == '__main__': main()