6 print >>sys.stderr, "usage: ./gen-indic-table.py IndicSyllabicCategory.txt IndicMatraCategory.txt Blocks.txt"
9 files = [file (sys.argv[i+1]) for i in range (3)]
11 headers = [[f.readline () for i in range (2)] for f in files]
14 data = [{} for f in files]
15 values = [{} for f in files]
16 for i, f in enumerate (files):
23 fields = [x.strip () for x in line.split (';')]
27 uu = fields[0].split ('..')
28 start = int (uu[0], 16)
36 for u in range (start, end + 1):
38 values[i][t] = values[i].get (t, 0) + 1
41 blocks[t] = (start, end)
43 # Merge data into one dict:
44 defaults = ('Other', 'Not_Applicable', 'No_Block')
45 for i,v in enumerate (defaults):
46 values[i][v] = values[i].get (v, 0) + 1
48 for i,d in enumerate (data):
49 for u,v in d.items ():
50 if i == 2 and not u in combined:
53 combined[u] = list (defaults)
59 # Move the outliers NO-BREAK SPACE and DOTTED CIRCLE out
61 for u in [0x00A0, 0x25CC]:
65 print "/* == Start of generated table == */"
67 print " * The following table is generated by running:"
69 print " * ./gen-indic-table.py IndicSyllabicCategory.txt IndicMatraCategory.txt Blocks.txt"
71 print " * on files with these headers:"
75 print " * %s" % (l.strip())
84 "Vowel_Dependent": 'M',
87 "Not_Applicable": 'x',
91 # Add some of the values, to make them more readable, and to avoid duplicates
95 for v,s in short[i].items ():
96 all_shorts[i].append (s)
98 what = ["INDIC_SYLLABIC_CATEGORY", "INDIC_MATRA_CATEGORY"]
99 what_short = ["ISC", "IMC"]
102 vv = values[i].keys ()
105 v_no_and = v.replace ('_And_', '_')
109 s = ''.join ([c for c in v_no_and if ord ('A') <= ord (c) <= ord ('Z')])
110 if s in all_shorts[i]:
111 raise Exception ("Duplicate short value alias", v, s)
112 all_shorts[i].append (s)
114 print "#define %s_%s %s_%s %s/* %3d chars; %s */" % \
115 (what_short[i], s, what[i], v.upper (), \
116 ' '* ((48-1 - len (what[i]) - 1 - len (v)) / 8), \
119 print "#define _(S,M) INDIC_COMBINE_CATEGORIES (ISC_##S, IMC_##M)"
123 def print_block (block, start, end, data):
126 print " /* %s (%04X..%04X) */" % (block, start, end)
128 for u in range (start, end+1):
131 print " /* %04X */" % u,
134 d = data.get (u, defaults)
135 sys.stdout.write ("%9s" % ("_(%s,%s)," % (short[0][d[0]], short[1][d[1]])))
138 # Filler block, don't check occupancy
140 total = end - start + 1
141 occupancy = num * 100. / total
142 # Maintain at least 30% occupancy in the table */
144 raise Exception ("Table too sparse, please investigate: ", occupancy, block)
154 print "static const INDIC_TABLE_ELEMENT_TYPE indic_table[] = {"
159 (start, end) = blocks[block]
161 if start != last + 1:
162 if start - last <= 33:
163 print_block ("FILLER", last+1, start-1, data)
167 ends.append (last + 1)
168 offset += ends[-1] - starts[-1]
171 print "#define indic_offset_0x%04x %d" % (start, offset)
172 starts.append (start)
174 print_block (block, start, end, data)
176 ends.append (last + 1)
177 offset += ends[-1] - starts[-1]
180 print "#define indic_offset_total %d" % offset
185 print "static INDIC_TABLE_ELEMENT_TYPE"
186 print "get_indic_categories (hb_codepoint_t u)"
188 for (start,end) in zip (starts, ends):
189 offset = "indic_offset_0x%04x" % start
190 print " if (0x%04X <= u && u <= 0x%04X) return indic_table[u - 0x%04X + %s];" % (start, end, start, offset)
191 for u,d in singles.items ():
192 print " if (unlikely (u == 0x%04X)) return _(%s,%s);" % (u, short[0][d[0]], short[1][d[1]])
193 print " return _(x,x);"
200 vv = values[i].keys ()
203 print "#undef %s_%s" % \
204 (what_short[i], short[i][v])
208 print "/* == End of generated table == */"