Debian Codegraph @

scripts/generate-emoji-variation-sequences.py

raw
1 #!/usr/bin/env python3
2
3 import argparse
4
5
6 class Codepoint:
7 def __init__(self, start: int, end: None | int = None):
8 self.start = start
9 self.end = start if end is None else end
10 self.vs15 = False
11 self.vs16 = False
12
13 def __repr__(self) -> str:
14 return f'{self.start:x}-{self.end:x}, vs15={self.vs15}, vs16={self.vs16}'
15
16
17 def main() -> None:
18 parser = argparse.ArgumentParser()
19 parser.add_argument('input', type=argparse.FileType('r'))
20 parser.add_argument('output', type=argparse.FileType('w'))
21 opts = parser.parse_args()
22
23 codepoints: dict[int, Codepoint] = {}
24
25 for line in opts.input:
26 line = line.rstrip()
27 if not line:
28 continue
29 if line[0] == '#':
30 continue
31
32 # Example: "0023 FE0E ; text style; # (1.1) NUMBER SIGN"
33 cps, _ = line.split(';', maxsplit=1) # cps = "0023 FE0F "
34 cps = cps.strip().split(' ') # cps = ["0023", "FE0F"]
35
36 if len(cps) != 2:
37 raise NotImplementedError(f'emoji variation sequences with more than one base codepoint: {cps}')
38
39 cp, vs = cps # cp = "0023", vs = "FE0F"
40 cp = int(cp, 16) # cp = 0x23
41 vs = int(vs, 16) # vs = 0xfe0f
42
43 assert vs in [0xfe0e, 0xfe0f]
44
45 if cp not in codepoints:
46 codepoints[cp] = Codepoint(cp)
47
48 assert codepoints[cp].start == cp
49
50 if vs == 0xfe0e:
51 codepoints[cp].vs15 = True
52 else:
53 codepoints[cp].vs16 = True
54
55 sorted_list = sorted(codepoints.values(), key=lambda cp: cp.start)
56
57 compacted: list[Codepoint] = []
58 for i, cp in enumerate(sorted_list):
59 assert cp.end == cp.start
60
61 if i == 0:
62 compacted.append(cp)
63 continue
64
65 last_cp = compacted[-1]
66 if last_cp.end == cp.start - 1 and last_cp.vs15 == cp.vs15 and last_cp.vs16 == cp.vs16:
67 compacted[-1].end = cp.start
68 else:
69 compacted.append(cp)
70
71 opts.output.write('#pragma once\n')
72 opts.output.write('#include <stdint.h>\n')
73 opts.output.write('#include <stdbool.h>\n')
74 opts.output.write('\n')
75 opts.output.write('struct emoji_vs {\n')
76 opts.output.write(' uint32_t start:21;\n')
77 opts.output.write(' uint32_t end:21;\n')
78 opts.output.write(' bool vs15:1;\n')
79 opts.output.write(' bool vs16:1;\n')
80 opts.output.write('} __attribute__((packed));\n')
81 opts.output.write('_Static_assert(sizeof(struct emoji_vs) == 6, "unexpected struct size");\n')
82 opts.output.write('\n')
83 opts.output.write('#if defined(FOOT_GRAPHEME_CLUSTERING)\n')
84 opts.output.write('\n')
85
86 opts.output.write(f'static const struct emoji_vs emoji_vs[{len(compacted)}] = {{\n')
87
88 for cp in compacted:
89 opts.output.write(' {\n')
90 opts.output.write(f' .start = 0x{cp.start:X},\n')
91 opts.output.write(f' .end = 0x{cp.end:x},\n')
92 opts.output.write(f' .vs15 = {"true" if cp.vs15 else "false"},\n')
93 opts.output.write(f' .vs16 = {"true" if cp.vs16 else "false"},\n')
94 opts.output.write(' },\n')
95
96 opts.output.write('};\n')
97 opts.output.write('\n')
98 opts.output.write('#endif /* FOOT_GRAPHEME_CLUSTERING */\n')
99
100
101 if __name__ == '__main__':
102 main()