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 7from __future__ import print_function, division, absolute_import 8 9import io 10import re 11import sys 12 13reload(sys) 14sys.setdefaultencoding('utf-8') 15 16print ("""static OS2Range _hb_os2_unicode_ranges[] = 17{""") 18 19args = sys.argv[1:] 20input_file = args[0] 21 22with io.open(input_file, mode="r", encoding="utf-8") as f: 23 24 all_ranges = []; 25 current_bit = 0 26 while True: 27 line = f.readline().strip() 28 if not line: 29 break 30 fields = re.split(r'\t+', line) 31 if len(fields) == 3: 32 current_bit = fields[0] 33 fields = fields[1:] 34 elif len(fields) > 3: 35 raise Error("bad input :(.") 36 37 name = fields[0] 38 ranges = re.split("-", fields[1]) 39 if len(ranges) != 2: 40 raise Error("bad input :(.") 41 42 v = tuple((int(ranges[0], 16), int(ranges[1], 16), int(current_bit), name)) 43 all_ranges.append(v) 44 45all_ranges = sorted(all_ranges, key=lambda t: t[0]) 46 47for ranges in all_ranges: 48 start = ("0x%X" % ranges[0]).rjust(8) 49 end = ("0x%X" % ranges[1]).rjust(8) 50 bit = ("%s" % ranges[2]).rjust(3) 51 52 print (" {%s, %s, %s}, // %s" % (start, end, bit, ranges[3])) 53 54print ("""};""") 55