1 #!/usr/bin/python 2 3 import sys 4 5 if len (sys.argv) != 4: 6 print >>sys.stderr, "usage: ./gen-indic-table.py IndicSyllabicCategory.txt IndicMatraCategory.txt Blocks.txt" 7 sys.exit (1) 8 9 BLACKLISTED_BLOCKS = ["Thai", "Lao", "Tibetan"] 10 11 files = [file (x) for x in sys.argv[1:]] 12 13 headers = [[f.readline () for i in range (2)] for f in files] 14 15 data = [{} for f in files] 16 values = [{} for f in files] 17 for 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: 42 defaults = ('Other', 'Not_Applicable', 'No_Block') 43 for i,v in enumerate (defaults): 44 values[i][v] = values[i].get (v, 0) + 1 45 combined = {} 46 for 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 53 combined = {k:v for k,v in combined.items() if v[2] not in BLACKLISTED_BLOCKS} 54 data = combined 55 del combined 56 num = len (data) 57 58 for 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 63 singles = {} 64 for u in [0x00A0, 0x25CC]: 65 singles[u] = data[u] 66 del data[u] 67 68 print "/* == Start of generated table == */" 69 print "/*" 70 print " * The following table is generated by running:" 71 print " *" 72 print " * ./gen-indic-table.py IndicSyllabicCategory.txt IndicMatraCategory.txt Blocks.txt" 73 print " *" 74 print " * on files with these headers:" 75 print " *" 76 for h in headers: 77 for l in h: 78 print " * %s" % (l.strip()) 79 print " */" 80 print 81 print '#include "hb-ot-shape-complex-indic-private.hh"' 82 print 83 84 # Shorten values 85 short = [{ 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 "Consonant_Prefixed": 'CPrf', 95 "Other": 'x', 96 },{ 97 "Not_Applicable": 'x', 98 }] 99 all_shorts = [{},{}] 100 101 # Add some of the values, to make them more readable, and to avoid duplicates 102 103 104 for i in range (2): 105 for v,s in short[i].items (): 106 all_shorts[i][s] = v 107 108 what = ["INDIC_SYLLABIC_CATEGORY", "INDIC_MATRA_CATEGORY"] 109 what_short = ["ISC", "IMC"] 110 for i in range (2): 111 print 112 vv = values[i].keys () 113 vv.sort () 114 for v in vv: 115 v_no_and = v.replace ('_And_', '_') 116 if v in short[i]: 117 s = short[i][v] 118 else: 119 s = ''.join ([c for c in v_no_and if ord ('A') <= ord (c) <= ord ('Z')]) 120 if s in all_shorts[i]: 121 raise Exception ("Duplicate short value alias", v, all_shorts[i][s]) 122 all_shorts[i][s] = v 123 short[i][v] = s 124 print "#define %s_%s %s_%s %s/* %3d chars; %s */" % \ 125 (what_short[i], s, what[i], v.upper (), \ 126 ' '* ((48-1 - len (what[i]) - 1 - len (v)) / 8), \ 127 values[i][v], v) 128 print 129 print "#define _(S,M) INDIC_COMBINE_CATEGORIES (ISC_##S, IMC_##M)" 130 print 131 print 132 133 total = 0 134 used = 0 135 last_block = None 136 def print_block (block, start, end, data): 137 global total, used, last_block 138 if block and block != last_block: 139 print 140 print 141 print " /* %s */" % block 142 num = 0 143 assert start % 8 == 0 144 assert (end+1) % 8 == 0 145 for u in range (start, end+1): 146 if u % 8 == 0: 147 print 148 print " /* %04X */" % u, 149 if u in data: 150 num += 1 151 d = data.get (u, defaults) 152 sys.stdout.write ("%9s" % ("_(%s,%s)," % (short[0][d[0]], short[1][d[1]]))) 153 154 total += end - start + 1 155 used += num 156 if block: 157 last_block = block 158 159 uu = data.keys () 160 uu.sort () 161 162 last = -100000 163 num = 0 164 offset = 0 165 starts = [] 166 ends = [] 167 print "static const INDIC_TABLE_ELEMENT_TYPE indic_table[] = {" 168 for u in uu: 169 if u <= last: 170 continue 171 block = data[u][2] 172 173 start = u//8*8 174 end = start+1 175 while end in uu and block == data[end][2]: 176 end += 1 177 end = (end-1)//8*8 + 7 178 179 if start != last + 1: 180 if start - last <= 1+16*3: 181 print_block (None, last+1, start-1, data) 182 last = start-1 183 else: 184 if last >= 0: 185 ends.append (last + 1) 186 offset += ends[-1] - starts[-1] 187 print 188 print 189 print "#define indic_offset_0x%04xu %d" % (start, offset) 190 starts.append (start) 191 192 print_block (block, start, end, data) 193 last = end 194 ends.append (last + 1) 195 offset += ends[-1] - starts[-1] 196 print 197 print 198 occupancy = used * 100. / total 199 page_bits = 12 200 print "}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy) 201 print 202 print "INDIC_TABLE_ELEMENT_TYPE" 203 print "hb_indic_get_categories (hb_codepoint_t u)" 204 print "{" 205 print " switch (u >> %d)" % page_bits 206 print " {" 207 pages = set([u>>page_bits for u in starts+ends+singles.keys()]) 208 for p in sorted(pages): 209 print " case 0x%0Xu:" % p 210 for (start,end) in zip (starts, ends): 211 if p not in [start>>page_bits, end>>page_bits]: continue 212 offset = "indic_offset_0x%04xu" % start 213 print " if (hb_in_range (u, 0x%04Xu, 0x%04Xu)) return indic_table[u - 0x%04Xu + %s];" % (start, end-1, start, offset) 214 for u,d in singles.items (): 215 if p != u>>page_bits: continue 216 print " if (unlikely (u == 0x%04Xu)) return _(%s,%s);" % (u, short[0][d[0]], short[1][d[1]]) 217 print " break;" 218 print "" 219 print " default:" 220 print " break;" 221 print " }" 222 print " return _(x,x);" 223 print "}" 224 print 225 print "#undef _" 226 for i in range (2): 227 print 228 vv = values[i].keys () 229 vv.sort () 230 for v in vv: 231 print "#undef %s_%s" % \ 232 (what_short[i], short[i][v]) 233 print 234 print "/* == End of generated table == */" 235 236 # Maintain at least 30% occupancy in the table */ 237 if occupancy < 30: 238 raise Exception ("Table too sparse, please investigate: ", occupancy) 239