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 for u in [0x17CD, 0x17CE, 0x17CF, 0x17D0, 0x17D3]:
83 if data[u][0] == 'Other':
84 data[u][0] = "Vowel_Dependent"
86 # Move the outliers NO-BREAK SPACE and DOTTED CIRCLE out
88 for u in ALLOWED_SINGLES:
92 print ("/* == Start of generated table == */")
94 print (" * The following table is generated by running:")
96 print (" * ./gen-indic-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt Blocks.txt")
98 print (" * on files with these headers:")
102 print (" * %s" % (l.strip()))
105 print ('#include "hb-ot-shape-complex-indic.hh"')
111 "Cantillation_Mark": 'Ca',
113 "Non_Joiner": 'ZWNJ',
117 "Vowel_Dependent": 'M',
118 "Consonant_Prefixed": 'CPrf',
121 "Not_Applicable": 'x',
125 # Add some of the values, to make them more readable, and to avoid duplicates
129 for v,s in short[i].items ():
132 what = ["INDIC_SYLLABIC_CATEGORY", "INDIC_MATRA_CATEGORY"]
133 what_short = ["ISC", "IMC"]
134 print ('#pragma GCC diagnostic push')
135 print ('#pragma GCC diagnostic ignored "-Wunused-macros"')
138 vv = sorted (values[i].keys ())
140 v_no_and = v.replace ('_And_', '_')
144 s = ''.join ([c for c in v_no_and if ord ('A') <= ord (c) <= ord ('Z')])
145 if s in all_shorts[i]:
146 raise Exception ("Duplicate short value alias", v, all_shorts[i][s])
149 print ("#define %s_%s %s_%s %s/* %3d chars; %s */" %
150 (what_short[i], s, what[i], v.upper (),
151 ' '* ((48-1 - len (what[i]) - 1 - len (v)) // 8),
153 print ('#pragma GCC diagnostic pop')
155 print ("#define _(S,M) INDIC_COMBINE_CATEGORIES (ISC_##S, IMC_##M)")
162 def print_block (block, start, end, data):
163 global total, used, last_block
164 if block and block != last_block:
167 print (" /* %s */" % block)
169 assert start % 8 == 0
170 assert (end+1) % 8 == 0
171 for u in range (start, end+1):
174 print (" /* %04X */" % u, end="")
177 d = data.get (u, defaults)
178 print ("%9s" % ("_(%s,%s)," % (short[0][d[0]], short[1][d[1]])), end="")
180 total += end - start + 1
185 uu = sorted (data.keys ())
192 print ("static const INDIC_TABLE_ELEMENT_TYPE indic_table[] = {")
200 while end in uu and block == data[end][2]:
202 end = (end-1)//8*8 + 7
204 if start != last + 1:
205 if start - last <= 1+16*3:
206 print_block (None, last+1, start-1, data)
210 ends.append (last + 1)
211 offset += ends[-1] - starts[-1]
214 print ("#define indic_offset_0x%04xu %d" % (start, offset))
215 starts.append (start)
217 print_block (block, start, end, data)
219 ends.append (last + 1)
220 offset += ends[-1] - starts[-1]
223 occupancy = used * 100. / total
225 print ("}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy))
227 print ("INDIC_TABLE_ELEMENT_TYPE")
228 print ("hb_indic_get_categories (hb_codepoint_t u)")
230 print (" switch (u >> %d)" % page_bits)
232 pages = set ([u>>page_bits for u in starts+ends+list (singles.keys ())])
233 for p in sorted(pages):
234 print (" case 0x%0Xu:" % p)
235 for u,d in singles.items ():
236 if p != u>>page_bits: continue
237 print (" if (unlikely (u == 0x%04Xu)) return _(%s,%s);" % (u, short[0][d[0]], short[1][d[1]]))
238 for (start,end) in zip (starts, ends):
239 if p not in [start>>page_bits, end>>page_bits]: continue
240 offset = "indic_offset_0x%04xu" % start
241 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))
247 print (" return _(x,x);")
253 vv = sorted (values[i].keys ())
255 print ("#undef %s_%s" %
256 (what_short[i], short[i][v]))
258 print ("/* == End of generated table == */")
260 # Maintain at least 30% occupancy in the table */
262 raise Exception ("Table too sparse, please investigate: ", occupancy)