6 if len (sys.argv) != 3:
7 print >>sys.stderr, "usage: ./gen-arabic-table.py ArabicShaping.txt UnicodeData.txt"
10 files = [file (x) for x in sys.argv[1:]]
12 headers = [[files[0].readline (), files[0].readline ()]]
13 headers.append (["UnicodeData.txt does not have a header."])
14 while files[0].readline ().find ('##################') < 0:
18 def print_joining_table(f):
21 print "static const uint8_t joining_table[] ="
32 if line.find (" characters"):
33 block = line[2:].strip ()
36 fields = [x.strip () for x in line.split (';')]
40 u = int (fields[0], 16)
41 if u == 0x200C or u == 0x200D:
44 raise Exception ("Input data character not sorted", u)
45 min_u = min (min_u, u)
46 max_u = max (max_u, u)
50 print "\n /* %s */\n" % block
56 print " JOINING_TYPE_X, /* %04X */" % last
61 if fields[3] in ["ALAPH", "DALATH RISH"]:
62 value = "JOINING_GROUP_" + fields[3].replace(' ', '_')
64 value = "JOINING_TYPE_" + fields[2]
65 print " %s, /* %s */" % (value, '; '.join(fields))
70 print "#define JOINING_TABLE_FIRST 0x%04X" % min_u
71 print "#define JOINING_TABLE_LAST 0x%04X" % max_u
74 occupancy = num * 100 / (max_u - min_u + 1)
75 # Maintain at least 40% occupancy in the table */
77 raise Exception ("Table too sparse, please investigate: ", occupancy)
79 def print_shaping_table(f):
86 fields = [x.strip () for x in line.split (';')]
87 if fields[5][0:1] != '<':
90 items = fields[5].split (' ')
91 shape, items = items[0][1:-1], tuple (int (x, 16) for x in items[1:])
93 if not shape in ['initial', 'medial', 'isolated', 'final']:
96 c = int (fields[0], 16)
98 # We only care about lam-alef ligatures
99 if len (items) != 2 or items[0] != 0x0644 or items[1] not in [0x0622, 0x0623, 0x0625, 0x0627]:
104 if items not in ligatures:
105 ligatures[items] = {}
106 ligatures[items][shape] = c
110 if items[0] not in names:
111 names[items[0]] = fields[1]
113 names[items[0]] = os.path.commonprefix ([names[items[0]], fields[1]]).strip ()
114 if items[0] not in shapes:
115 shapes[items[0]] = {}
116 shapes[items[0]][shape] = c
119 print "static const uint16_t shaping_table[][4] ="
122 keys = shapes.keys ()
123 min_u, max_u = min (keys), max (keys)
124 for u in range (min_u, max_u + 1):
125 s = [shapes[u][shape] if u in shapes and shape in shapes[u] else u
126 for shape in ['initial', 'medial', 'final', 'isolated']]
127 value = ', '.join ("0x%04X" % c for c in s)
128 print " {%s}, /* U+%04X %s */" % (value, u, names[u] if u in names else "")
132 print "#define SHAPING_TABLE_FIRST 0x%04X" % min_u
133 print "#define SHAPING_TABLE_LAST 0x%04X" % max_u
137 for pair in ligatures.keys ():
138 for shape in ligatures[pair]:
139 c = ligatures[pair][shape]
140 if shape == 'isolated':
141 liga = (shapes[pair[0]]['initial'], shapes[pair[1]]['final'])
142 elif shape == 'final':
143 liga = (shapes[pair[0]]['medial'], shapes[pair[1]]['final'])
145 raise Exception ("Unexpected shape", shape)
146 if liga[0] not in ligas:
148 ligas[liga[0]].append ((liga[1], c))
149 max_i = max (len (ligas[l]) for l in ligas)
151 print "static const struct {"
152 print " uint16_t first;"
154 print " uint16_t second;"
155 print " uint16_t ligature;"
156 print " } ligatures[%d];" % max_i
157 print "} ligature_table[] ="
163 print " { 0x%04X, {" % (first)
164 for liga in ligas[first]:
165 print " { 0x%04X, 0x%04X }, /* %s */" % (liga[0], liga[1], names[liga[1]])
173 print "/* == Start of generated table == */"
175 print " * The following table is generated by running:"
177 print " * ./gen-arabic-table.py ArabicShaping.txt UnicodeData.txt"
179 print " * on files with these headers:"
183 print " * %s" % (l.strip())
186 print "#ifndef HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH"
187 print "#define HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH"
190 print_joining_table (files[0])
191 print_shaping_table (files[1])
194 print "#endif /* HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH */"
196 print "/* == End of generated table == */"