1#!/usr/bin/env python3
2
3"""usage: ./gen-emoji-table.py emoji-data.txt
4
5Input file:
6* https://www.unicode.org/Public/UCD/latest/ucd/emoji/emoji-data.txt
7"""
8
9import sys
10from collections import OrderedDict
11import packTab
12
13if len (sys.argv) != 2:
14	sys.exit (__doc__)
15
16f = open(sys.argv[1])
17header = [f.readline () for _ in range(10)]
18
19ranges = OrderedDict()
20for line in f.readlines():
21	line = line.strip()
22	if not line or line[0] == '#':
23		continue
24	rang, typ = [s.strip() for s in line.split('#')[0].split(';')[:2]]
25
26	rang = [int(s, 16) for s in rang.split('..')]
27	if len(rang) > 1:
28		start, end = rang
29	else:
30		start = end = rang[0]
31
32	if typ not in ranges:
33		ranges[typ] = []
34	if ranges[typ] and ranges[typ][-1][1] == start - 1:
35		ranges[typ][-1] = (ranges[typ][-1][0], end)
36	else:
37		ranges[typ].append((start, end))
38
39
40
41print ("/* == Start of generated table == */")
42print ("/*")
43print (" * The following tables are generated by running:")
44print (" *")
45print (" *   ./gen-emoji-table.py emoji-data.txt")
46print (" *")
47print (" * on file with this header:")
48print (" *")
49for l in header:
50	print (" * %s" % (l.strip()))
51print (" */")
52print ()
53print ("#ifndef HB_UNICODE_EMOJI_TABLE_HH")
54print ("#define HB_UNICODE_EMOJI_TABLE_HH")
55print ()
56print ('#include "hb-unicode.hh"')
57print ()
58
59for typ, s in ranges.items():
60	if typ != "Extended_Pictographic": continue
61
62	arr = dict()
63	for start,end in s:
64		for i in range(start,end):
65			arr[i] = 1
66
67	sol = packTab.pack_table(arr, 0, compression=3)
68	code = packTab.Code('_hb_emoji')
69	sol.genCode(code, 'is_'+typ)
70	code.print_c(linkage='static inline')
71	print()
72
73print ()
74print ("#endif /* HB_UNICODE_EMOJI_TABLE_HH */")
75print ()
76print ("/* == End of generated table == */")
77