1#!/usr/bin/env python3
2
3"""Generates the code for a sorted unicode range array as used in hb-ot-os2-unicode-ranges.hh
4Input is a tab seperated list of unicode ranges from the otspec
5(https://docs.microsoft.com/en-us/typography/opentype/spec/os2#ur).
6"""
7
8import re
9import sys
10
11
12print ("""static OS2Range _hb_os2_unicode_ranges[] =
13{""")
14
15args = sys.argv[1:]
16input_file = args[0]
17
18with open (input_file, mode="r", encoding="utf-8") as f:
19
20  all_ranges = []
21  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:
31      raise Exception("bad input :(.")
32
33    name = fields[0]
34    ranges = re.split("-", fields[1])
35    if len(ranges) != 2:
36      raise Exception("bad input :(.")
37
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
48  print ("  {%s, %s, %s}, // %s" % (start, end, bit, ranges[3]))
49
50print ("""};""")
51