1#!/usr/bin/python 2 3import sys 4 5if len (sys.argv) != 4: 6 print >>sys.stderr, "usage: ./gen-indic-table.py IndicSyllabicCategory.txt IndicMatraCategory.txt Blocks.txt" 7 sys.exit (1) 8 9BLACKLISTED_BLOCKS = ["Thai", "Lao", "Tibetan"] 10 11files = [file (x) for x in sys.argv[1:]] 12 13headers = [[f.readline () for i in range (2)] for f in files] 14 15data = [{} for f in files] 16values = [{} for f in files] 17for i, f in enumerate (files): 18 for line in f: 19 20 j = line.find ('#') 21 if j >= 0: 22 line = line[:j] 23 24 fields = [x.strip () for x in line.split (';')] 25 if len (fields) == 1: 26 continue 27 28 uu = fields[0].split ('..') 29 start = int (uu[0], 16) 30 if len (uu) == 1: 31 end = start 32 else: 33 end = int (uu[1], 16) 34 35 t = fields[1] 36 37 for u in range (start, end + 1): 38 data[i][u] = t 39 values[i][t] = values[i].get (t, 0) + end - start + 1 40 41# Merge data into one dict: 42defaults = ('Other', 'Not_Applicable', 'No_Block') 43for i,v in enumerate (defaults): 44 values[i][v] = values[i].get (v, 0) + 1 45combined = {} 46for i,d in enumerate (data): 47 for u,v in d.items (): 48 if i == 2 and not u in combined: 49 continue 50 if not u in combined: 51 combined[u] = list (defaults) 52 combined[u][i] = v 53combined = {k:v for k,v in combined.items() if v[2] not in BLACKLISTED_BLOCKS} 54data = combined 55del combined 56num = len (data) 57 58for u in [0x17CD, 0x17CE, 0x17CF, 0x17D0, 0x17D3]: 59 if data[u][0] == 'Other': 60 data[u][0] = "Vowel_Dependent" 61 62# Move the outliers NO-BREAK SPACE and DOTTED CIRCLE out 63singles = {} 64for u in [0x00A0, 0x25CC]: 65 singles[u] = data[u] 66 del data[u] 67 68print "/* == Start of generated table == */" 69print "/*" 70print " * The following table is generated by running:" 71print " *" 72print " * ./gen-indic-table.py IndicSyllabicCategory.txt IndicMatraCategory.txt Blocks.txt" 73print " *" 74print " * on files with these headers:" 75print " *" 76for h in headers: 77 for l in h: 78 print " * %s" % (l.strip()) 79print " */" 80print 81print '#include "hb-ot-shape-complex-indic-private.hh"' 82print 83 84# Shorten values 85short = [{ 86 "Bindu": 'Bi', 87 "Cantillation_Mark": 'Ca', 88 "Joiner": 'ZWJ', 89 "Non_Joiner": 'ZWNJ', 90 "Number": 'Nd', 91 "Visarga": 'Vs', 92 "Vowel": 'Vo', 93 "Vowel_Dependent": 'M', 94 "Other": 'x', 95},{ 96 "Not_Applicable": 'x', 97}] 98all_shorts = [{},{}] 99 100# Add some of the values, to make them more readable, and to avoid duplicates 101 102 103for i in range (2): 104 for v,s in short[i].items (): 105 all_shorts[i][s] = v 106 107what = ["INDIC_SYLLABIC_CATEGORY", "INDIC_MATRA_CATEGORY"] 108what_short = ["ISC", "IMC"] 109for i in range (2): 110 print 111 vv = values[i].keys () 112 vv.sort () 113 for v in vv: 114 v_no_and = v.replace ('_And_', '_') 115 if v in short[i]: 116 s = short[i][v] 117 else: 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]) 121 all_shorts[i][s] = v 122 short[i][v] = 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), \ 126 values[i][v], v) 127print 128print "#define _(S,M) INDIC_COMBINE_CATEGORIES (ISC_##S, IMC_##M)" 129print 130print 131 132total = 0 133used = 0 134last_block = None 135def print_block (block, start, end, data): 136 global total, used, last_block 137 if block and block != last_block: 138 print 139 print 140 print " /* %s */" % block 141 num = 0 142 assert start % 8 == 0 143 assert (end+1) % 8 == 0 144 for u in range (start, end+1): 145 if u % 8 == 0: 146 print 147 print " /* %04X */" % u, 148 if u in data: 149 num += 1 150 d = data.get (u, defaults) 151 sys.stdout.write ("%9s" % ("_(%s,%s)," % (short[0][d[0]], short[1][d[1]]))) 152 153 total += end - start + 1 154 used += num 155 if block: 156 last_block = block 157 158uu = data.keys () 159uu.sort () 160 161last = -100000 162num = 0 163offset = 0 164starts = [] 165ends = [] 166print "static const INDIC_TABLE_ELEMENT_TYPE indic_table[] = {" 167for u in uu: 168 if u <= last: 169 continue 170 block = data[u][2] 171 172 start = u//8*8 173 end = start+1 174 while end in uu and block == data[end][2]: 175 end += 1 176 end = (end-1)//8*8 + 7 177 178 if start != last + 1: 179 if start - last <= 1+16*3: 180 print_block (None, last+1, start-1, data) 181 last = start-1 182 else: 183 if last >= 0: 184 ends.append (last + 1) 185 offset += ends[-1] - starts[-1] 186 print 187 print 188 print "#define indic_offset_0x%04xu %d" % (start, offset) 189 starts.append (start) 190 191 print_block (block, start, end, data) 192 last = end 193ends.append (last + 1) 194offset += ends[-1] - starts[-1] 195print 196print 197occupancy = used * 100. / total 198page_bits = 12 199print "}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy) 200print 201print "INDIC_TABLE_ELEMENT_TYPE" 202print "hb_indic_get_categories (hb_codepoint_t u)" 203print "{" 204print " switch (u >> %d)" % page_bits 205print " {" 206pages = set([u>>page_bits for u in starts+ends+singles.keys()]) 207for 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]]) 216 print " break;" 217 print "" 218print " default:" 219print " break;" 220print " }" 221print " return _(x,x);" 222print "}" 223print 224print "#undef _" 225for i in range (2): 226 print 227 vv = values[i].keys () 228 vv.sort () 229 for v in vv: 230 print "#undef %s_%s" % \ 231 (what_short[i], short[i][v]) 232print 233print "/* == End of generated table == */" 234 235# Maintain at least 30% occupancy in the table */ 236if occupancy < 30: 237 raise Exception ("Table too sparse, please investigate: ", occupancy) 238