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 "Consonant_Prefixed": 'CPrf', 95 "Other": 'x', 96},{ 97 "Not_Applicable": 'x', 98}] 99all_shorts = [{},{}] 100 101# Add some of the values, to make them more readable, and to avoid duplicates 102 103 104for i in range (2): 105 for v,s in short[i].items (): 106 all_shorts[i][s] = v 107 108what = ["INDIC_SYLLABIC_CATEGORY", "INDIC_MATRA_CATEGORY"] 109what_short = ["ISC", "IMC"] 110for 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) 128print 129print "#define _(S,M) INDIC_COMBINE_CATEGORIES (ISC_##S, IMC_##M)" 130print 131print 132 133total = 0 134used = 0 135last_block = None 136def 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 159uu = data.keys () 160uu.sort () 161 162last = -100000 163num = 0 164offset = 0 165starts = [] 166ends = [] 167print "static const INDIC_TABLE_ELEMENT_TYPE indic_table[] = {" 168for 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 194ends.append (last + 1) 195offset += ends[-1] - starts[-1] 196print 197print 198occupancy = used * 100. / total 199page_bits = 12 200print "}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy) 201print 202print "INDIC_TABLE_ELEMENT_TYPE" 203print "hb_indic_get_categories (hb_codepoint_t u)" 204print "{" 205print " switch (u >> %d)" % page_bits 206print " {" 207pages = set([u>>page_bits for u in starts+ends+singles.keys()]) 208for 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 "" 219print " default:" 220print " break;" 221print " }" 222print " return _(x,x);" 223print "}" 224print 225print "#undef _" 226for 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]) 233print 234print "/* == End of generated table == */" 235 236# Maintain at least 30% occupancy in the table */ 237if occupancy < 30: 238 raise Exception ("Table too sparse, please investigate: ", occupancy) 239