3 from __future__ import print_function, division, absolute_import
7 if len (sys.argv) != 4:
8 print ("usage: ./gen-indic-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt Blocks.txt", file=sys.stderr)
11 ALLOWED_SINGLES = [0x00A0, 0x25CC]
28 'General Punctuation',
29 'Superscripts and Subscripts',
30 'Devanagari Extended',
35 files = [io.open (x, encoding='utf-8') for x in sys.argv[1:]]
37 headers = [[f.readline () for i in range (2)] for f in files]
39 data = [{} for f in files]
40 values = [{} for f in files]
41 for i, f in enumerate (files):
48 fields = [x.strip () for x in line.split (';')]
52 uu = fields[0].split ('..')
53 start = int (uu[0], 16)
61 for u in range (start, end + 1):
63 values[i][t] = values[i].get (t, 0) + end - start + 1
65 # Merge data into one dict:
66 defaults = ('Other', 'Not_Applicable', 'No_Block')
67 for i,v in enumerate (defaults):
68 values[i][v] = values[i].get (v, 0) + 1
70 for i,d in enumerate (data):
71 for u,v in d.items ():
72 if i == 2 and not u in combined:
75 combined[u] = list (defaults)
77 combined = {k:v for k,v in combined.items() if k in ALLOWED_SINGLES or v[2] in ALLOWED_BLOCKS}
82 # Move the outliers NO-BREAK SPACE and DOTTED CIRCLE out
84 for u in ALLOWED_SINGLES:
88 print ("/* == Start of generated table == */")
90 print (" * The following table is generated by running:")
92 print (" * ./gen-indic-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt Blocks.txt")
94 print (" * on files with these headers:")
98 print (" * %s" % (l.strip()))
101 print ('#include "hb-ot-shape-complex-indic.hh"')
107 "Cantillation_Mark": 'Ca',
109 "Non_Joiner": 'ZWNJ',
113 "Vowel_Dependent": 'M',
114 "Consonant_Prefixed": 'CPrf',
117 "Not_Applicable": 'x',
121 # Add some of the values, to make them more readable, and to avoid duplicates
125 for v,s in short[i].items ():
128 what = ["INDIC_SYLLABIC_CATEGORY", "INDIC_MATRA_CATEGORY"]
129 what_short = ["ISC", "IMC"]
130 print ('#pragma GCC diagnostic push')
131 print ('#pragma GCC diagnostic ignored "-Wunused-macros"')
134 vv = sorted (values[i].keys ())
136 v_no_and = v.replace ('_And_', '_')
140 s = ''.join ([c for c in v_no_and if ord ('A') <= ord (c) <= ord ('Z')])
141 if s in all_shorts[i]:
142 raise Exception ("Duplicate short value alias", v, all_shorts[i][s])
145 print ("#define %s_%s %s_%s %s/* %3d chars; %s */" %
146 (what_short[i], s, what[i], v.upper (),
147 ' '* ((48-1 - len (what[i]) - 1 - len (v)) // 8),
149 print ('#pragma GCC diagnostic pop')
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, end="")
173 d = data.get (u, defaults)
174 print ("%9s" % ("_(%s,%s)," % (short[0][d[0]], short[1][d[1]])), end="")
176 total += end - start + 1
181 uu = sorted (data.keys ())
188 print ("static const INDIC_TABLE_ELEMENT_TYPE indic_table[] = {")
196 while end in uu and block == data[end][2]:
198 end = (end-1)//8*8 + 7
200 if start != last + 1:
201 if start - last <= 1+16*3:
202 print_block (None, last+1, start-1, data)
206 ends.append (last + 1)
207 offset += ends[-1] - starts[-1]
210 print ("#define indic_offset_0x%04xu %d" % (start, offset))
211 starts.append (start)
213 print_block (block, start, end, data)
215 ends.append (last + 1)
216 offset += ends[-1] - starts[-1]
219 occupancy = used * 100. / total
221 print ("}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy))
223 print ("INDIC_TABLE_ELEMENT_TYPE")
224 print ("hb_indic_get_categories (hb_codepoint_t u)")
226 print (" switch (u >> %d)" % page_bits)
228 pages = set ([u>>page_bits for u in starts+ends+list (singles.keys ())])
229 for p in sorted(pages):
230 print (" case 0x%0Xu:" % p)
231 for u,d in singles.items ():
232 if p != u>>page_bits: continue
233 print (" if (unlikely (u == 0x%04Xu)) return _(%s,%s);" % (u, short[0][d[0]], short[1][d[1]]))
234 for (start,end) in zip (starts, ends):
235 if p not in [start>>page_bits, end>>page_bits]: continue
236 offset = "indic_offset_0x%04xu" % start
237 print (" if (hb_in_range<hb_codepoint_t> (u, 0x%04Xu, 0x%04Xu)) return indic_table[u - 0x%04Xu + %s];" % (start, end-1, start, offset))
243 print (" return _(x,x);")
249 vv = sorted (values[i].keys ())
251 print ("#undef %s_%s" %
252 (what_short[i], short[i][v]))
254 print ("/* == End of generated table == */")
256 # Maintain at least 30% occupancy in the table */
258 raise Exception ("Table too sparse, please investigate: ", occupancy)