5 if len (sys.argv) != 4:
6 print >>sys.stderr, "usage: ./gen-indic-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt Blocks.txt"
9 ALLOWED_SINGLES = [0x00A0, 0x25CC]
26 'General Punctuation',
27 'Superscripts and Subscripts',
28 'Devanagari Extended',
33 files = [file (x) for x in sys.argv[1:]]
35 headers = [[f.readline () for i in range (2)] for f in files]
37 data = [{} for f in files]
38 values = [{} for f in files]
39 for i, f in enumerate (files):
46 fields = [x.strip () for x in line.split (';')]
50 uu = fields[0].split ('..')
51 start = int (uu[0], 16)
59 for u in range (start, end + 1):
61 values[i][t] = values[i].get (t, 0) + end - start + 1
63 # Merge data into one dict:
64 defaults = ('Other', 'Not_Applicable', 'No_Block')
65 for i,v in enumerate (defaults):
66 values[i][v] = values[i].get (v, 0) + 1
68 for i,d in enumerate (data):
69 for u,v in d.items ():
70 if i == 2 and not u in combined:
73 combined[u] = list (defaults)
75 combined = {k:v for k,v in combined.items() if k in ALLOWED_SINGLES or v[2] in ALLOWED_BLOCKS}
80 for u in [0x17CD, 0x17CE, 0x17CF, 0x17D0, 0x17D3]:
81 if data[u][0] == 'Other':
82 data[u][0] = "Vowel_Dependent"
84 # Move the outliers NO-BREAK SPACE and DOTTED CIRCLE out
86 for u in ALLOWED_SINGLES:
90 print "/* == Start of generated table == */"
92 print " * The following table is generated by running:"
94 print " * ./gen-indic-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt Blocks.txt"
96 print " * on files with these headers:"
100 print " * %s" % (l.strip())
103 print '#include "hb-ot-shape-complex-indic-private.hh"'
109 "Cantillation_Mark": 'Ca',
111 "Non_Joiner": 'ZWNJ',
115 "Vowel_Dependent": 'M',
116 "Consonant_Prefixed": 'CPrf',
119 "Not_Applicable": 'x',
123 # Add some of the values, to make them more readable, and to avoid duplicates
127 for v,s in short[i].items ():
130 what = ["INDIC_SYLLABIC_CATEGORY", "INDIC_MATRA_CATEGORY"]
131 what_short = ["ISC", "IMC"]
134 vv = values[i].keys ()
137 v_no_and = v.replace ('_And_', '_')
141 s = ''.join ([c for c in v_no_and if ord ('A') <= ord (c) <= ord ('Z')])
142 if s in all_shorts[i]:
143 raise Exception ("Duplicate short value alias", v, all_shorts[i][s])
146 print "#define %s_%s %s_%s %s/* %3d chars; %s */" % \
147 (what_short[i], s, what[i], v.upper (), \
148 ' '* ((48-1 - len (what[i]) - 1 - len (v)) / 8), \
151 print "#define _(S,M) INDIC_COMBINE_CATEGORIES (ISC_##S, IMC_##M)"
158 def print_block (block, start, end, data):
159 global total, used, last_block
160 if block and block != last_block:
163 print " /* %s */" % block
165 assert start % 8 == 0
166 assert (end+1) % 8 == 0
167 for u in range (start, end+1):
170 print " /* %04X */" % u,
173 d = data.get (u, defaults)
174 sys.stdout.write ("%9s" % ("_(%s,%s)," % (short[0][d[0]], short[1][d[1]])))
176 total += end - start + 1
189 print "static const INDIC_TABLE_ELEMENT_TYPE indic_table[] = {"
197 while end in uu and block == data[end][2]:
199 end = (end-1)//8*8 + 7
201 if start != last + 1:
202 if start - last <= 1+16*3:
203 print_block (None, last+1, start-1, data)
207 ends.append (last + 1)
208 offset += ends[-1] - starts[-1]
211 print "#define indic_offset_0x%04xu %d" % (start, offset)
212 starts.append (start)
214 print_block (block, start, end, data)
216 ends.append (last + 1)
217 offset += ends[-1] - starts[-1]
220 occupancy = used * 100. / total
222 print "}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy)
224 print "INDIC_TABLE_ELEMENT_TYPE"
225 print "hb_indic_get_categories (hb_codepoint_t u)"
227 print " switch (u >> %d)" % page_bits
229 pages = set([u>>page_bits for u in starts+ends+singles.keys()])
230 for p in sorted(pages):
231 print " case 0x%0Xu:" % p
232 for (start,end) in zip (starts, ends):
233 if p not in [start>>page_bits, end>>page_bits]: continue
234 offset = "indic_offset_0x%04xu" % start
235 print " if (hb_in_range (u, 0x%04Xu, 0x%04Xu)) return indic_table[u - 0x%04Xu + %s];" % (start, end-1, start, offset)
236 for u,d in singles.items ():
237 if p != u>>page_bits: continue
238 print " if (unlikely (u == 0x%04Xu)) return _(%s,%s);" % (u, short[0][d[0]], short[1][d[1]])
244 print " return _(x,x);"
250 vv = values[i].keys ()
253 print "#undef %s_%s" % \
254 (what_short[i], short[i][v])
256 print "/* == End of generated table == */"
258 # Maintain at least 30% occupancy in the table */
260 raise Exception ("Table too sparse, please investigate: ", occupancy)