8cf59858802e0910c62b5a8ac4c5946ecf059e09
[platform/upstream/harfbuzz.git] / src / gen-os2-unicode-ranges.py
1 # -*- coding: utf-8 -*-
2
3 # Generates the code for a sorted unicode range array as used in hb-ot-os2-unicode-ranges.hh
4 # Input is a tab seperated list of unicode ranges from the otspec
5 # (https://docs.microsoft.com/en-us/typography/opentype/spec/os2#ulunicoderange1).
6
7 from __future__ import print_function, division, absolute_import
8
9 import io
10 import re
11 import sys
12
13 try:
14   reload(sys)
15   sys.setdefaultencoding('utf-8')
16 except NameError:
17   pass  # Python 3
18
19 print ("""static OS2Range _hb_os2_unicode_ranges[] =
20 {""")
21
22 args = sys.argv[1:]
23 input_file = args[0]
24
25 with io.open(input_file, mode="r", encoding="utf-8") as f:
26
27   all_ranges = [];
28   current_bit = 0
29   while True:
30     line = f.readline().strip()
31     if not line:
32       break
33     fields = re.split(r'\t+', line)
34     if len(fields) == 3:
35       current_bit = fields[0]
36       fields = fields[1:]
37     elif len(fields) > 3:
38       raise Exception("bad input :(.")
39
40     name = fields[0]
41     ranges = re.split("-", fields[1])
42     if len(ranges) != 2:
43       raise Exception("bad input :(.")
44
45     v = tuple((int(ranges[0], 16), int(ranges[1], 16), int(current_bit), name))
46     all_ranges.append(v)
47
48 all_ranges = sorted(all_ranges, key=lambda t: t[0])
49
50 for ranges in all_ranges:
51   start = ("0x%X" % ranges[0]).rjust(8)
52   end = ("0x%X" % ranges[1]).rjust(8)
53   bit = ("%s" % ranges[2]).rjust(3)
54
55   print ("  {%s, %s, %s}, // %s" % (start, end, bit, ranges[3]))
56
57 print ("""};""")