3 """usage: ./gen-indic-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt Blocks.txt
6 * https://unicode.org/Public/UCD/latest/ucd/IndicSyllabicCategory.txt
7 * https://unicode.org/Public/UCD/latest/ucd/IndicPositionalCategory.txt
8 * https://unicode.org/Public/UCD/latest/ucd/Blocks.txt
13 if len (sys.argv) != 4:
16 ALLOWED_SINGLES = [0x00A0, 0x25CC]
33 'General Punctuation',
34 'Superscripts and Subscripts',
35 'Devanagari Extended',
40 files = [open (x, encoding='utf-8') for x in sys.argv[1:]]
42 headers = [[f.readline () for i in range (2)] for f in files]
44 data = [{} for f in files]
45 values = [{} for f in files]
46 for i, f in enumerate (files):
53 fields = [x.strip () for x in line.split (';')]
57 uu = fields[0].split ('..')
58 start = int (uu[0], 16)
66 for u in range (start, end + 1):
68 values[i][t] = values[i].get (t, 0) + end - start + 1
70 # Merge data into one dict:
71 defaults = ('Other', 'Not_Applicable', 'No_Block')
72 for i,v in enumerate (defaults):
73 values[i][v] = values[i].get (v, 0) + 1
75 for i,d in enumerate (data):
76 for u,v in d.items ():
77 if i == 2 and not u in combined:
80 combined[u] = list (defaults)
82 combined = {k:v for k,v in combined.items() if k in ALLOWED_SINGLES or v[2] in ALLOWED_BLOCKS}
87 # Move the outliers NO-BREAK SPACE and DOTTED CIRCLE out
89 for u in ALLOWED_SINGLES:
93 print ("/* == Start of generated table == */")
95 print (" * The following table is generated by running:")
97 print (" * ./gen-indic-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt Blocks.txt")
99 print (" * on files with these headers:")
103 print (" * %s" % (l.strip()))
106 print ('#include "hb.hh"')
108 print ('#ifndef HB_NO_OT_SHAPE')
110 print ('#include "hb-ot-shape-complex-indic.hh"')
116 "Cantillation_Mark": 'Ca',
118 "Non_Joiner": 'ZWNJ',
122 "Vowel_Dependent": 'M',
123 "Consonant_Prefixed": 'CPrf',
126 "Not_Applicable": 'x',
130 # Add some of the values, to make them more readable, and to avoid duplicates
134 for v,s in short[i].items ():
137 what = ["INDIC_SYLLABIC_CATEGORY", "INDIC_MATRA_CATEGORY"]
138 what_short = ["ISC", "IMC"]
139 print ('#pragma GCC diagnostic push')
140 print ('#pragma GCC diagnostic ignored "-Wunused-macros"')
143 vv = sorted (values[i].keys ())
145 v_no_and = v.replace ('_And_', '_')
149 s = ''.join ([c for c in v_no_and if ord ('A') <= ord (c) <= ord ('Z')])
150 if s in all_shorts[i]:
151 raise Exception ("Duplicate short value alias", v, all_shorts[i][s])
154 cat_defs.append ((what_short[i] + '_' + s, what[i] + '_' + v.upper (), str (values[i][v]), v))
156 maxlen_s = max ([len (c[0]) for c in cat_defs])
157 maxlen_l = max ([len (c[1]) for c in cat_defs])
158 maxlen_n = max ([len (c[2]) for c in cat_defs])
161 for c in [c for c in cat_defs if s in c[0]]:
162 print ("#define %s %s /* %s chars; %s */" %
163 (c[0].ljust (maxlen_s), c[1].ljust (maxlen_l), c[2].rjust (maxlen_n), c[3]))
165 print ('#pragma GCC diagnostic pop')
167 print ("#define _(S,M) INDIC_COMBINE_CATEGORIES (ISC_##S, IMC_##M)")
174 def print_block (block, start, end, data):
175 global total, used, last_block
176 if block and block != last_block:
179 print (" /* %s */" % block)
181 assert start % 8 == 0
182 assert (end+1) % 8 == 0
183 for u in range (start, end+1):
186 print (" /* %04X */" % u, end="")
189 d = data.get (u, defaults)
190 print ("%9s" % ("_(%s,%s)," % (short[0][d[0]], short[1][d[1]])), end="")
192 total += end - start + 1
197 uu = sorted (data.keys ())
204 print ("static const INDIC_TABLE_ELEMENT_TYPE indic_table[] = {")
212 while end in uu and block == data[end][2]:
214 end = (end-1)//8*8 + 7
216 if start != last + 1:
217 if start - last <= 1+16*3:
218 print_block (None, last+1, start-1, data)
222 ends.append (last + 1)
223 offset += ends[-1] - starts[-1]
226 print ("#define indic_offset_0x%04xu %d" % (start, offset))
227 starts.append (start)
229 print_block (block, start, end, data)
231 ends.append (last + 1)
232 offset += ends[-1] - starts[-1]
235 occupancy = used * 100. / total
237 print ("}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy))
239 print ("INDIC_TABLE_ELEMENT_TYPE")
240 print ("hb_indic_get_categories (hb_codepoint_t u)")
242 print (" switch (u >> %d)" % page_bits)
244 pages = set ([u>>page_bits for u in starts+ends+list (singles.keys ())])
245 for p in sorted(pages):
246 print (" case 0x%0Xu:" % p)
247 for u,d in singles.items ():
248 if p != u>>page_bits: continue
249 print (" if (unlikely (u == 0x%04Xu)) return _(%s,%s);" % (u, short[0][d[0]], short[1][d[1]]))
250 for (start,end) in zip (starts, ends):
251 if p not in [start>>page_bits, end>>page_bits]: continue
252 offset = "indic_offset_0x%04xu" % start
253 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))
259 print (" return _(x,x);")
265 vv = sorted (values[i].keys ())
267 print ("#undef %s_%s" %
268 (what_short[i], short[i][v]))
272 print ("/* == End of generated table == */")
274 # Maintain at least 30% occupancy in the table */
276 raise Exception ("Table too sparse, please investigate: ", occupancy)