#!/usr/bin/env python3 """Generate compositor/emoji/data.mbt: the emoji picker's emoji, names and search keywords. Sources (Unicode License v3, see third_party/unicode-emoji/LICENSE): - Unicode emoji-test.txt (Emoji 18.0): every emoji, in CLDR order, with its group and name. - CLDR annotations (en.xml and annotationsDerived/en.xml, release 49): search keywords. scripts/gen-emoji-data.py # downloads the pinned sources scripts/gen-emoji-data.py DIR # reads emoji-test.txt, en.xml, en-derived.xml from DIR One line per emoji the picker lists (skin tone variants are not listed: they hang off their base): GROUP \t EMOJI \t NAME \t KEYWORDS \t TONES GROUP is the picker tab (0 Smileys .. 8 Flags), KEYWORDS are space-separated lowercase words, TONES is empty or the five skin tone variants (light .. dark) separated by spaces. """ import os import sys import urllib.request import xml.etree.ElementTree as ET EMOJI_TEST = "https://www.unicode.org/Public/18.0.0/emoji/emoji-test.txt" CLDR = "https://raw.githubusercontent.com/unicode-org/cldr/release-49/common/" SOURCES = { "emoji-test.txt": EMOJI_TEST, "en.xml": CLDR + "annotations/en.xml", "en-derived.xml": CLDR + "annotationsDerived/en.xml", } # emoji-test.txt group -> picker tab. "Component" (bare skin tones, hair styles) is not listed. GROUPS = { "Smileys & Emotion": 0, "People & Body": 1, "Animals & Nature": 2, "Food & Drink": 3, "Activities": 4, "Travel & Places": 5, "Objects": 6, "Symbols": 7, "Flags": 8, } TONES = [0x1F3FB, 0x1F3FC, 0x1F3FD, 0x1F3FE, 0x1F3FF] def read_sources(src_dir): out = {} for name, url in SOURCES.items(): if src_dir: path = os.path.join(src_dir, name) if not os.path.exists(path): sys.exit(f"gen-emoji-data: {path} is missing (expected {url})") with open(path, encoding="utf-8") as f: out[name] = f.read() else: with urllib.request.urlopen(url) as r: out[name] = r.read().decode("utf-8") return out def annotations(xml_text): """CLDR annotation keywords and names, keyed by the emoji without FE0F.""" words, names = {}, {} root = ET.fromstring(xml_text) for a in root.iter("annotation"): cp = a.get("cp") if a.get("type") == "tts": names[cp] = a.text.strip() else: words[cp] = [w.strip() for w in a.text.split("|") if w.strip()] return words, names def keyword_list(seq_key, words, name): seen, out = set(), [] for phrase in words.get(seq_key, []): for w in phrase.lower().replace(":", " ").replace(",", " ").split(): if w not in seen and w not in name.split(): seen.add(w) out.append(w) return out def main(): src = read_sources(sys.argv[1] if len(sys.argv) > 1 else None) words, _ = annotations(src["en.xml"]) dwords, _ = annotations(src["en-derived.xml"]) words = {**dwords, **words} entries = [] # [tab, emoji, name, keywords, tones] by_key = {} # base without FE0F and tones -> entry group = None for line in src["emoji-test.txt"].splitlines(): if line.startswith("# group:"): group = line.split(":", 1)[1].strip() continue if not line or line.startswith("#") or "; fully-qualified" not in line: continue if group not in GROUPS: if group != "Component": sys.exit(f"gen-emoji-data: unknown group {group!r}") continue cps = [int(x, 16) for x in line.split(";")[0].split()] name = line.split("#", 1)[1].strip().split(" ", 2)[2] # "😀 E1.0 grinning face" seq = "".join(chr(c) for c in cps) key = tuple(c for c in cps if c != 0xFE0F and c not in TONES) tones = {c for c in cps if c in TONES} if not tones: e = [GROUPS[group], seq, name, keyword_list(seq.replace("️", ""), words, name), [""] * 5] entries.append(e) by_key[key] = e elif len(tones) == 1 and key in by_key: # One tone for every person in it: the variant the picker's skin tone choice picks. by_key[key][4][TONES.index(tones.pop())] = seq for e in entries: if any(e[4]) and not all(e[4]): sys.exit(f"gen-emoji-data: {e[2]} has only some skin tones: {e[4]}") entries.sort(key=lambda e: e[0]) # stable: CLDR order within each tab lines = [] for tab, seq, name, kws, tones in entries: for field in (seq, name, *kws, *tones): if "\t" in field or "\n" in field or '"' in field or "\\" in field: sys.exit(f"gen-emoji-data: unexpected character in {field!r}") lines.append("\t".join([str(tab), seq, name, " ".join(kws), " ".join(tones) if any(tones) else ""])) out = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "compositor", "emoji", "data.mbt") with open(out, "w", encoding="utf-8") as f: f.write("// Generated by scripts/gen-emoji-data.py from Unicode emoji-test.txt (Emoji 18.0) and CLDR\n") f.write("// annotations (release 49); Unicode License v3, see third_party/unicode-emoji/LICENSE. Don't edit.\n\n") f.write("///| One line per emoji: tab, emoji, name, keywords, skin tone variants (tab-separated).\n") f.write("let emoji_data : String =\n") for ln in lines: f.write(" #|" + ln + "\n") toned = sum(1 for e in entries if any(e[4])) print(f"gen-emoji-data: {len(entries)} emoji ({toned} with skin tones) -> {os.path.normpath(out)}") if __name__ == "__main__": main()