6 if len (sys.argv) != 4:
7 print >>sys.stderr, "usage: ./gen-arabic-table.py ArabicShaping.txt UnicodeData.txt Blocks.txt"
10 files = [file (x) for x in sys.argv[1:]]
12 headers = [[files[0].readline (), files[0].readline ()], [files[2].readline (), files[2].readline ()]]
13 headers.append (["UnicodeData.txt does not have a header."])
14 while files[0].readline ().find ('##################') < 0:
26 fields = [x.strip () for x in line.split (';')]
30 uu = fields[0].split ('..')
31 start = int (uu[0], 16)
39 for u in range (start, end + 1):
42 def print_joining_table(f):
50 fields = [x.strip () for x in line.split (';')]
54 u = int (fields[0], 16)
56 if fields[3] in ["ALAPH", "DALATH RISH"]:
57 value = "JOINING_GROUP_" + fields[3].replace(' ', '_')
59 value = "JOINING_TYPE_" + fields[2]
63 for value in set([v for v in values.values()] + ['JOINING_TYPE_X']):
64 short = ''.join(x[0] for x in value.split('_')[2:])
65 assert short not in short_value.values()
66 short_value[value] = short
69 for value,short in short_value.items():
70 print "#define %s %s" % (short, value)
72 uu = sorted(values.keys())
74 all_blocks = set([blocks[u] for u in uu])
79 if u - last <= 1+16*5:
86 print "static const uint8_t joining_table[] ="
90 for start,end in ranges:
93 print "#define joining_offset_0x%04xu %d" % (start, offset)
95 for u in range(start, end+1):
97 block = blocks.get(u, last_block)
98 value = values.get(u, "JOINING_TYPE_X")
100 if block != last_block or u == start:
103 if block in all_blocks:
104 print "\n /* %s */" % block
106 print "\n /* FILLER */"
110 print " /* %04X */" % (u//32*32), " " * (u % 32),
114 print " /* %04X */ " % u,
115 sys.stdout.write("%s," % short_value[value])
118 offset += end - start + 1
120 occupancy = num * 100. / offset
121 print "}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy)
126 print "static unsigned int"
127 print "joining_type (hb_codepoint_t u)"
129 print " switch (u >> %d)" % page_bits
131 pages = set([u>>page_bits for u in [s for s,e in ranges]+[e for s,e in ranges]])
132 for p in sorted(pages):
133 print " case 0x%0Xu:" % p
134 for (start,end) in ranges:
135 if p not in [start>>page_bits, end>>page_bits]: continue
136 offset = "joining_offset_0x%04xu" % start
137 print " if (hb_in_range (u, 0x%04Xu, 0x%04Xu)) return joining_table[u - 0x%04Xu + %s];" % (start, end, start, offset)
146 for value,short in short_value.items():
147 print "#undef %s" % (short)
150 def print_shaping_table(f):
157 fields = [x.strip () for x in line.split (';')]
158 if fields[5][0:1] != '<':
161 items = fields[5].split (' ')
162 shape, items = items[0][1:-1], tuple (int (x, 16) for x in items[1:])
164 if not shape in ['initial', 'medial', 'isolated', 'final']:
167 c = int (fields[0], 16)
169 # We only care about lam-alef ligatures
170 if len (items) != 2 or items[0] != 0x0644 or items[1] not in [0x0622, 0x0623, 0x0625, 0x0627]:
175 if items not in ligatures:
176 ligatures[items] = {}
177 ligatures[items][shape] = c
181 if items[0] not in names:
182 names[items[0]] = fields[1]
184 names[items[0]] = os.path.commonprefix ([names[items[0]], fields[1]]).strip ()
185 if items[0] not in shapes:
186 shapes[items[0]] = {}
187 shapes[items[0]][shape] = c
190 print "static const uint16_t shaping_table[][4] ="
193 keys = shapes.keys ()
194 min_u, max_u = min (keys), max (keys)
195 for u in range (min_u, max_u + 1):
196 s = [shapes[u][shape] if u in shapes and shape in shapes[u] else 0
197 for shape in ['initial', 'medial', 'final', 'isolated']]
198 value = ', '.join ("0x%04Xu" % c for c in s)
199 print " {%s}, /* U+%04X %s */" % (value, u, names[u] if u in names else "")
203 print "#define SHAPING_TABLE_FIRST 0x%04Xu" % min_u
204 print "#define SHAPING_TABLE_LAST 0x%04Xu" % max_u
208 for pair in ligatures.keys ():
209 for shape in ligatures[pair]:
210 c = ligatures[pair][shape]
211 if shape == 'isolated':
212 liga = (shapes[pair[0]]['initial'], shapes[pair[1]]['final'])
213 elif shape == 'final':
214 liga = (shapes[pair[0]]['medial'], shapes[pair[1]]['final'])
216 raise Exception ("Unexpected shape", shape)
217 if liga[0] not in ligas:
219 ligas[liga[0]].append ((liga[1], c))
220 max_i = max (len (ligas[l]) for l in ligas)
222 print "static const struct ligature_set_t {"
223 print " uint16_t first;"
224 print " struct ligature_pairs_t {"
225 print " uint16_t second;"
226 print " uint16_t ligature;"
227 print " } ligatures[%d];" % max_i
228 print "} ligature_table[] ="
234 print " { 0x%04Xu, {" % (first)
235 for liga in ligas[first]:
236 print " { 0x%04Xu, 0x%04Xu }, /* %s */" % (liga[0], liga[1], names[liga[1]])
244 print "/* == Start of generated table == */"
246 print " * The following table is generated by running:"
248 print " * ./gen-arabic-table.py ArabicShaping.txt UnicodeData.txt Blocks.txt"
250 print " * on files with these headers:"
254 print " * %s" % (l.strip())
257 print "#ifndef HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH"
258 print "#define HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH"
261 read_blocks (files[2])
262 print_joining_table (files[0])
263 print_shaping_table (files[1])
266 print "#endif /* HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH */"
268 print "/* == End of generated table == */"