• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2
3# -*- coding: utf-8 -*-
4
5# Generates the code for a sorted unicode range array as used in hb-ot-os2-unicode-ranges.hh
6# Input is a tab seperated list of unicode ranges from the otspec
7# (https://docs.microsoft.com/en-us/typography/opentype/spec/os2#ur).
8
9from __future__ import print_function, division, absolute_import
10
11import io
12import re
13import sys
14
15try:
16  reload(sys)
17  sys.setdefaultencoding('utf-8')
18except NameError:
19  pass  # Python 3
20
21print ("""static OS2Range _hb_os2_unicode_ranges[] =
22{""")
23
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:
40      raise Exception("bad input :(.")
41
42    name = fields[0]
43    ranges = re.split("-", fields[1])
44    if len(ranges) != 2:
45      raise Exception("bad input :(.")
46
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
57  print ("  {%s, %s, %s}, // %s" % (start, end, bit, ranges[3]))
58
59print ("""};""")
60