blob: b1a34d478f3bac2b1346d0264a9b49f776db123b [file] [log] [blame]
Ebrahim Byagowi8d199072020-02-19 14:56:55 +03301#!/usr/bin/env python3
Garret Riegerddc4f2b2018-02-26 15:59:32 -08002
Ebrahim Byagowi08f1d952020-05-28 15:01:15 +04303"""Generates the code for a sorted unicode range array as used in hb-ot-os2-unicode-ranges.hh
luz paze2e30502022-01-16 07:00:53 -05004Input is a tab separated list of unicode ranges from the otspec
Ebrahim Byagowi08f1d952020-05-28 15:01:15 +04305(https://docs.microsoft.com/en-us/typography/opentype/spec/os2#ur).
6"""
Garret Riegerddc4f2b2018-02-26 15:59:32 -08007
Garret Riegerddc4f2b2018-02-26 15:59:32 -08008import re
9import sys
10
Garret Riegerddc4f2b2018-02-26 15:59:32 -080011
Behdad Esfahbod1dc601b2018-10-03 17:27:46 +020012print ("""static OS2Range _hb_os2_unicode_ranges[] =
Garret Riegerf1c8fc32018-02-26 17:48:51 -080013{""")
Garret Riegerddc4f2b2018-02-26 15:59:32 -080014
15args = sys.argv[1:]
16input_file = args[0]
17
Ebrahim Byagowiad871552020-05-29 00:11:19 +043018with open (input_file, mode="r", encoding="utf-8") as f:
Garret Riegerddc4f2b2018-02-26 15:59:32 -080019
Ebrahim Byagowi08f1d952020-05-28 15:01:15 +043020 all_ranges = []
Garret Riegerddc4f2b2018-02-26 15:59:32 -080021 current_bit = 0
22 while True:
23 line = f.readline().strip()
24 if not line:
25 break
26 fields = re.split(r'\t+', line)
27 if len(fields) == 3:
28 current_bit = fields[0]
29 fields = fields[1:]
30 elif len(fields) > 3:
cclaussf4da28b2018-12-30 12:58:34 +010031 raise Exception("bad input :(.")
Garret Riegerddc4f2b2018-02-26 15:59:32 -080032
33 name = fields[0]
34 ranges = re.split("-", fields[1])
35 if len(ranges) != 2:
cclaussf4da28b2018-12-30 12:58:34 +010036 raise Exception("bad input :(.")
Garret Riegerddc4f2b2018-02-26 15:59:32 -080037
38 v = tuple((int(ranges[0], 16), int(ranges[1], 16), int(current_bit), name))
39 all_ranges.append(v)
40
41all_ranges = sorted(all_ranges, key=lambda t: t[0])
42
43for ranges in all_ranges:
44 start = ("0x%X" % ranges[0]).rjust(8)
45 end = ("0x%X" % ranges[1]).rjust(8)
46 bit = ("%s" % ranges[2]).rjust(3)
47
Ebrahim Byagowicab2c2c2018-03-29 12:48:47 +043048 print (" {%s, %s, %s}, // %s" % (start, end, bit, ranges[3]))
Garret Riegerddc4f2b2018-02-26 15:59:32 -080049
Ebrahim Byagowicab2c2c2018-03-29 12:48:47 +043050print ("""};""")