5 if len (sys.argv) != 4:
6 print >>sys.stderr, "usage: ./gen-indic-table.py IndicSyllabicCategory.txt IndicMatraCategory.txt Blocks.txt"
9 BLACKLISTED_BLOCKS = ["Thai", "Lao", "Tibetan"]
11 files = [file (x) for x in sys.argv[1:]]
13 headers = [[f.readline () for i in range (2)] for f in files]
15 data = [{} for f in files]
16 values = [{} for f in files]
17 for i, f in enumerate (files):
24 fields = [x.strip () for x in line.split (';')]
28 uu = fields[0].split ('..')
29 start = int (uu[0], 16)
37 for u in range (start, end + 1):
39 values[i][t] = values[i].get (t, 0) + end - start + 1
41 # Merge data into one dict:
42 defaults = ('Other', 'Not_Applicable', 'No_Block')
43 for i,v in enumerate (defaults):
44 values[i][v] = values[i].get (v, 0) + 1
46 for i,d in enumerate (data):
47 for u,v in d.items ():
48 if i == 2 and not u in combined:
51 combined[u] = list (defaults)
53 combined = {k:v for k,v in combined.items() if v[2] not in BLACKLISTED_BLOCKS}
58 for u in [0x17CD, 0x17CE, 0x17CF, 0x17D0, 0x17D3]:
59 if data[u][0] == 'Other':
60 data[u][0] = "Vowel_Dependent"
62 # Move the outliers NO-BREAK SPACE and DOTTED CIRCLE out
64 for u in [0x00A0, 0x25CC]:
68 print "/* == Start of generated table == */"
70 print " * The following table is generated by running:"
72 print " * ./gen-indic-table.py IndicSyllabicCategory.txt IndicMatraCategory.txt Blocks.txt"
74 print " * on files with these headers:"
78 print " * %s" % (l.strip())
81 print '#include "hb-ot-shape-complex-indic-private.hh"'
87 "Cantillation_Mark": 'Ca',
93 "Vowel_Dependent": 'M',
96 "Not_Applicable": 'x',
100 # Add some of the values, to make them more readable, and to avoid duplicates
104 for v,s in short[i].items ():
107 what = ["INDIC_SYLLABIC_CATEGORY", "INDIC_MATRA_CATEGORY"]
108 what_short = ["ISC", "IMC"]
111 vv = values[i].keys ()
114 v_no_and = v.replace ('_And_', '_')
118 s = ''.join ([c for c in v_no_and if ord ('A') <= ord (c) <= ord ('Z')])
119 if s in all_shorts[i]:
120 raise Exception ("Duplicate short value alias", v, all_shorts[i][s])
123 print "#define %s_%s %s_%s %s/* %3d chars; %s */" % \
124 (what_short[i], s, what[i], v.upper (), \
125 ' '* ((48-1 - len (what[i]) - 1 - len (v)) / 8), \
128 print "#define _(S,M) INDIC_COMBINE_CATEGORIES (ISC_##S, IMC_##M)"
135 def print_block (block, start, end, data):
136 global total, used, last_block
137 if block and block != last_block:
140 print " /* %s */" % block
142 assert start % 8 == 0
143 assert (end+1) % 8 == 0
144 for u in range (start, end+1):
147 print " /* %04X */" % u,
150 d = data.get (u, defaults)
151 sys.stdout.write ("%9s" % ("_(%s,%s)," % (short[0][d[0]], short[1][d[1]])))
153 total += end - start + 1
166 print "static const INDIC_TABLE_ELEMENT_TYPE indic_table[] = {"
174 while end in uu and block == data[end][2]:
176 end = (end-1)//8*8 + 7
178 if start != last + 1:
179 if start - last <= 1+16*3:
180 print_block (None, last+1, start-1, data)
184 ends.append (last + 1)
185 offset += ends[-1] - starts[-1]
188 print "#define indic_offset_0x%04xu %d" % (start, offset)
189 starts.append (start)
191 print_block (block, start, end, data)
193 ends.append (last + 1)
194 offset += ends[-1] - starts[-1]
197 occupancy = used * 100. / total
199 print "}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy)
201 print "INDIC_TABLE_ELEMENT_TYPE"
202 print "hb_indic_get_categories (hb_codepoint_t u)"
204 print " switch (u >> %d)" % page_bits
206 pages = set([u>>page_bits for u in starts+ends+singles.keys()])
207 for p in sorted(pages):
208 print " case 0x%0Xu:" % p
209 for (start,end) in zip (starts, ends):
210 if p not in [start>>page_bits, end>>page_bits]: continue
211 offset = "indic_offset_0x%04xu" % start
212 print " if (hb_in_range (u, 0x%04Xu, 0x%04Xu)) return indic_table[u - 0x%04Xu + %s];" % (start, end, start, offset)
213 for u,d in singles.items ():
214 if p != u>>page_bits: continue
215 print " if (unlikely (u == 0x%04Xu)) return _(%s,%s);" % (u, short[0][d[0]], short[1][d[1]])
221 print " return _(x,x);"
227 vv = values[i].keys ()
230 print "#undef %s_%s" % \
231 (what_short[i], short[i][v])
233 print "/* == End of generated table == */"
235 # Maintain at least 30% occupancy in the table */
237 raise Exception ("Table too sparse, please investigate: ", occupancy)