3 from __future__ import print_function, division, absolute_import
5 import io, os.path, sys
7 if len (sys.argv) != 4:
8 print ("usage: ./gen-arabic-table.py ArabicShaping.txt UnicodeData.txt Blocks.txt", file=sys.stderr)
11 files = [io.open (x, encoding='utf-8') for x in sys.argv[1:]]
13 headers = [[files[0].readline (), files[0].readline ()], [files[2].readline (), files[2].readline ()]]
14 headers.append (["UnicodeData.txt does not have a header."])
15 while files[0].readline ().find ('##################') < 0:
27 fields = [x.strip () for x in line.split (';')]
31 uu = fields[0].split ('..')
32 start = int (uu[0], 16)
40 for u in range (start, end + 1):
43 def print_joining_table(f):
51 fields = [x.strip () for x in line.split (';')]
55 u = int (fields[0], 16)
57 if fields[3] in ["ALAPH", "DALATH RISH"]:
58 value = "JOINING_GROUP_" + fields[3].replace(' ', '_')
60 value = "JOINING_TYPE_" + fields[2]
64 for value in set([v for v in values.values()] + ['JOINING_TYPE_X']):
65 short = ''.join(x[0] for x in value.split('_')[2:])
66 assert short not in short_value.values()
67 short_value[value] = short
70 for value,short in short_value.items():
71 print ("#define %s %s" % (short, value))
73 uu = sorted(values.keys())
75 all_blocks = set([blocks[u] for u in uu])
80 if u - last <= 1+16*5:
87 print ("static const uint8_t joining_table[] =")
91 for start,end in ranges:
94 print ("#define joining_offset_0x%04xu %d" % (start, offset))
96 for u in range(start, end+1):
98 block = blocks.get(u, last_block)
99 value = values.get(u, "JOINING_TYPE_X")
101 if block != last_block or u == start:
104 if block in all_blocks:
105 print ("\n /* %s */" % block)
107 print ("\n /* FILLER */")
111 print (" /* %04X */" % (u//32*32), " " * (u % 32), end="")
115 print (" /* %04X */ " % u, end="")
116 print ("%s," % short_value[value], end="")
119 offset += end - start + 1
121 occupancy = num * 100. / offset
122 print ("}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy))
127 print ("static unsigned int")
128 print ("joining_type (hb_codepoint_t u)")
130 print (" switch (u >> %d)" % page_bits)
132 pages = set([u>>page_bits for u in [s for s,e in ranges]+[e for s,e in ranges]])
133 for p in sorted(pages):
134 print (" case 0x%0Xu:" % p)
135 for (start,end) in ranges:
136 if p not in [start>>page_bits, end>>page_bits]: continue
137 offset = "joining_offset_0x%04xu" % start
138 print (" if (hb_in_range<hb_codepoint_t> (u, 0x%04Xu, 0x%04Xu)) return joining_table[u - 0x%04Xu + %s];" % (start, end, start, offset))
147 for value,short in short_value.items():
148 print ("#undef %s" % (short))
151 def print_shaping_table(f):
158 fields = [x.strip () for x in line.split (';')]
159 if fields[5][0:1] != '<':
162 items = fields[5].split (' ')
163 shape, items = items[0][1:-1], tuple (int (x, 16) for x in items[1:])
165 if not shape in ['initial', 'medial', 'isolated', 'final']:
168 c = int (fields[0], 16)
170 # We only care about lam-alef ligatures
171 if len (items) != 2 or items[0] != 0x0644 or items[1] not in [0x0622, 0x0623, 0x0625, 0x0627]:
176 if items not in ligatures:
177 ligatures[items] = {}
178 ligatures[items][shape] = c
182 if items[0] not in names:
183 names[items[0]] = fields[1]
185 names[items[0]] = os.path.commonprefix ([names[items[0]], fields[1]]).strip ()
186 if items[0] not in shapes:
187 shapes[items[0]] = {}
188 shapes[items[0]][shape] = c
191 print ("static const uint16_t shaping_table[][4] =")
194 keys = shapes.keys ()
195 min_u, max_u = min (keys), max (keys)
196 for u in range (min_u, max_u + 1):
197 s = [shapes[u][shape] if u in shapes and shape in shapes[u] else 0
198 for shape in ['initial', 'medial', 'final', 'isolated']]
199 value = ', '.join ("0x%04Xu" % c for c in s)
200 print (" {%s}, /* U+%04X %s */" % (value, u, names[u] if u in names else ""))
204 print ("#define SHAPING_TABLE_FIRST 0x%04Xu" % min_u)
205 print ("#define SHAPING_TABLE_LAST 0x%04Xu" % max_u)
209 for pair in ligatures.keys ():
210 for shape in ligatures[pair]:
211 c = ligatures[pair][shape]
212 if shape == 'isolated':
213 liga = (shapes[pair[0]]['initial'], shapes[pair[1]]['final'])
214 elif shape == 'final':
215 liga = (shapes[pair[0]]['medial'], shapes[pair[1]]['final'])
217 raise Exception ("Unexpected shape", shape)
218 if liga[0] not in ligas:
220 ligas[liga[0]].append ((liga[1], c))
221 max_i = max (len (ligas[l]) for l in ligas)
223 print ("static const struct ligature_set_t {")
224 print (" uint16_t first;")
225 print (" struct ligature_pairs_t {")
226 print (" uint16_t second;")
227 print (" uint16_t ligature;")
228 print (" } ligatures[%d];" % max_i)
229 print ("} ligature_table[] =")
231 for first in sorted (ligas.keys ()):
233 print (" { 0x%04Xu, {" % (first))
234 for liga in ligas[first]:
235 print (" { 0x%04Xu, 0x%04Xu }, /* %s */" % (liga[0], liga[1], names[liga[1]]))
243 print ("/* == Start of generated table == */")
245 print (" * The following table is generated by running:")
247 print (" * ./gen-arabic-table.py ArabicShaping.txt UnicodeData.txt Blocks.txt")
249 print (" * on files with these headers:")
253 print (" * %s" % (l.strip()))
256 print ("#ifndef HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH")
257 print ("#define HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH")
260 read_blocks (files[2])
261 print_joining_table (files[0])
262 print_shaping_table (files[1])
265 print ("#endif /* HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH */")
267 print ("/* == End of generated table == */")