blob: 515f4ca1492712f7e30bf7f7befd9d54cdee1bba [file] [log] [blame]
Behdad Esfahboddf3f36f2019-05-12 20:56:36 -07001#!/usr/bin/python
2
Garret Riegerddc4f2b2018-02-26 15:59:32 -08003# -*- coding: utf-8 -*-
4
5# Generates the code for a sorted unicode range array as used in hb-ot-os2-unicode-ranges.hh
Garret Riegerf1c8fc32018-02-26 17:48:51 -08006# Input is a tab seperated list of unicode ranges from the otspec
David Corbettfe4a0ac2019-04-30 13:35:50 -04007# (https://docs.microsoft.com/en-us/typography/opentype/spec/os2#ur).
Garret Riegerddc4f2b2018-02-26 15:59:32 -08008
Ebrahim Byagowicab2c2c2018-03-29 12:48:47 +04309from __future__ import print_function, division, absolute_import
10
Garret Riegerddc4f2b2018-02-26 15:59:32 -080011import io
12import re
13import sys
14
cclaussf4da28b2018-12-30 12:58:34 +010015try:
16 reload(sys)
17 sys.setdefaultencoding('utf-8')
18except NameError:
19 pass # Python 3
Garret Riegerddc4f2b2018-02-26 15:59:32 -080020
Behdad Esfahbod1dc601b2018-10-03 17:27:46 +020021print ("""static OS2Range _hb_os2_unicode_ranges[] =
Garret Riegerf1c8fc32018-02-26 17:48:51 -080022{""")
Garret Riegerddc4f2b2018-02-26 15:59:32 -080023
24args = sys.argv[1:]
25input_file = args[0]
26
27with io.open(input_file, mode="r", encoding="utf-8") as f:
28
29 all_ranges = [];
30 current_bit = 0
31 while True:
32 line = f.readline().strip()
33 if not line:
34 break
35 fields = re.split(r'\t+', line)
36 if len(fields) == 3:
37 current_bit = fields[0]
38 fields = fields[1:]
39 elif len(fields) > 3:
cclaussf4da28b2018-12-30 12:58:34 +010040 raise Exception("bad input :(.")
Garret Riegerddc4f2b2018-02-26 15:59:32 -080041
42 name = fields[0]
43 ranges = re.split("-", fields[1])
44 if len(ranges) != 2:
cclaussf4da28b2018-12-30 12:58:34 +010045 raise Exception("bad input :(.")
Garret Riegerddc4f2b2018-02-26 15:59:32 -080046
47 v = tuple((int(ranges[0], 16), int(ranges[1], 16), int(current_bit), name))
48 all_ranges.append(v)
49
50all_ranges = sorted(all_ranges, key=lambda t: t[0])
51
52for ranges in all_ranges:
53 start = ("0x%X" % ranges[0]).rjust(8)
54 end = ("0x%X" % ranges[1]).rjust(8)
55 bit = ("%s" % ranges[2]).rjust(3)
56
Ebrahim Byagowicab2c2c2018-03-29 12:48:47 +043057 print (" {%s, %s, %s}, // %s" % (start, end, bit, ranges[3]))
Garret Riegerddc4f2b2018-02-26 15:59:32 -080058
Ebrahim Byagowicab2c2c2018-03-29 12:48:47 +043059print ("""};""")