michael@0: #!/usr/bin/python michael@0: michael@0: import sys michael@0: michael@0: if len (sys.argv) != 4: michael@0: print >>sys.stderr, "usage: ./gen-indic-table.py IndicSyllabicCategory.txt IndicMatraCategory.txt Blocks.txt" michael@0: sys.exit (1) michael@0: michael@0: files = [file (x) for x in sys.argv[1:]] michael@0: michael@0: headers = [[f.readline () for i in range (2)] for f in files] michael@0: michael@0: blocks = {} michael@0: data = [{} for f in files] michael@0: values = [{} for f in files] michael@0: for i, f in enumerate (files): michael@0: for line in f: michael@0: michael@0: j = line.find ('#') michael@0: if j >= 0: michael@0: line = line[:j] michael@0: michael@0: fields = [x.strip () for x in line.split (';')] michael@0: if len (fields) == 1: michael@0: continue michael@0: michael@0: uu = fields[0].split ('..') michael@0: start = int (uu[0], 16) michael@0: if len (uu) == 1: michael@0: end = start michael@0: else: michael@0: end = int (uu[1], 16) michael@0: michael@0: t = fields[1] michael@0: michael@0: for u in range (start, end + 1): michael@0: data[i][u] = t michael@0: values[i][t] = values[i].get (t, 0) + 1 michael@0: michael@0: if i == 2: michael@0: blocks[t] = (start, end) michael@0: michael@0: # Merge data into one dict: michael@0: defaults = ('Other', 'Not_Applicable', 'No_Block') michael@0: for i,v in enumerate (defaults): michael@0: values[i][v] = values[i].get (v, 0) + 1 michael@0: combined = {} michael@0: for i,d in enumerate (data): michael@0: for u,v in d.items (): michael@0: if i == 2 and not u in combined: michael@0: continue michael@0: if not u in combined: michael@0: combined[u] = list (defaults) michael@0: combined[u][i] = v michael@0: data = combined michael@0: del combined michael@0: num = len (data) michael@0: michael@0: # Move the outliers NO-BREAK SPACE and DOTTED CIRCLE out michael@0: singles = {} michael@0: for u in [0x00A0, 0x25CC]: michael@0: singles[u] = data[u] michael@0: del data[u] michael@0: michael@0: print "/* == Start of generated table == */" michael@0: print "/*" michael@0: print " * The following table is generated by running:" michael@0: print " *" michael@0: print " * ./gen-indic-table.py IndicSyllabicCategory.txt IndicMatraCategory.txt Blocks.txt" michael@0: print " *" michael@0: print " * on files with these headers:" michael@0: print " *" michael@0: for h in headers: michael@0: for l in h: michael@0: print " * %s" % (l.strip()) michael@0: print " */" michael@0: print michael@0: print '#include "hb-ot-shape-complex-indic-private.hh"' michael@0: print michael@0: michael@0: # Shorten values michael@0: short = [{ michael@0: "Bindu": 'Bi', michael@0: "Visarga": 'Vs', michael@0: "Vowel": 'Vo', michael@0: "Vowel_Dependent": 'M', michael@0: "Other": 'x', michael@0: },{ michael@0: "Not_Applicable": 'x', michael@0: }] michael@0: all_shorts = [[],[]] michael@0: michael@0: # Add some of the values, to make them more readable, and to avoid duplicates michael@0: michael@0: michael@0: for i in range (2): michael@0: for v,s in short[i].items (): michael@0: all_shorts[i].append (s) michael@0: michael@0: what = ["INDIC_SYLLABIC_CATEGORY", "INDIC_MATRA_CATEGORY"] michael@0: what_short = ["ISC", "IMC"] michael@0: for i in range (2): michael@0: print michael@0: vv = values[i].keys () michael@0: vv.sort () michael@0: for v in vv: michael@0: v_no_and = v.replace ('_And_', '_') michael@0: if v in short[i]: michael@0: s = short[i][v] michael@0: else: michael@0: s = ''.join ([c for c in v_no_and if ord ('A') <= ord (c) <= ord ('Z')]) michael@0: if s in all_shorts[i]: michael@0: raise Exception ("Duplicate short value alias", v, s) michael@0: all_shorts[i].append (s) michael@0: short[i][v] = s michael@0: print "#define %s_%s %s_%s %s/* %3d chars; %s */" % \ michael@0: (what_short[i], s, what[i], v.upper (), \ michael@0: ' '* ((48-1 - len (what[i]) - 1 - len (v)) / 8), \ michael@0: values[i][v], v) michael@0: print michael@0: print "#define _(S,M) INDIC_COMBINE_CATEGORIES (ISC_##S, IMC_##M)" michael@0: print michael@0: print michael@0: michael@0: total = 0 michael@0: used = 0 michael@0: def print_block (block, start, end, data): michael@0: print michael@0: print michael@0: print " /* %s (%04X..%04X) */" % (block, start, end) michael@0: num = 0 michael@0: for u in range (start, end+1): michael@0: if u % 8 == 0: michael@0: print michael@0: print " /* %04X */" % u, michael@0: if u in data: michael@0: num += 1 michael@0: d = data.get (u, defaults) michael@0: sys.stdout.write ("%9s" % ("_(%s,%s)," % (short[0][d[0]], short[1][d[1]]))) michael@0: michael@0: global total, used michael@0: total += end - start + 1 michael@0: used += num michael@0: michael@0: uu = data.keys () michael@0: uu.sort () michael@0: michael@0: last = -1 michael@0: num = 0 michael@0: offset = 0 michael@0: starts = [] michael@0: ends = [] michael@0: print "static const INDIC_TABLE_ELEMENT_TYPE indic_table[] = {" michael@0: for u in uu: michael@0: if u <= last: michael@0: continue michael@0: block = data[u][2] michael@0: (start, end) = blocks[block] michael@0: michael@0: if start != last + 1: michael@0: if start - last <= 33: michael@0: print_block ("FILLER", last+1, start-1, data) michael@0: last = start-1 michael@0: else: michael@0: if last >= 0: michael@0: ends.append (last + 1) michael@0: offset += ends[-1] - starts[-1] michael@0: print michael@0: print michael@0: print "#define indic_offset_0x%04x %d" % (start, offset) michael@0: starts.append (start) michael@0: michael@0: print_block (block, start, end, data) michael@0: last = end michael@0: ends.append (last + 1) michael@0: offset += ends[-1] - starts[-1] michael@0: print michael@0: print michael@0: print "#define indic_offset_total %d" % offset michael@0: print michael@0: occupancy = used * 100. / total michael@0: print "}; /* Table occupancy: %d%% */" % occupancy michael@0: print michael@0: print "INDIC_TABLE_ELEMENT_TYPE" michael@0: print "hb_indic_get_categories (hb_codepoint_t u)" michael@0: print "{" michael@0: for (start,end) in zip (starts, ends): michael@0: offset = "indic_offset_0x%04x" % start michael@0: print " if (0x%04X <= u && u <= 0x%04X) return indic_table[u - 0x%04X + %s];" % (start, end, start, offset) michael@0: for u,d in singles.items (): michael@0: print " if (unlikely (u == 0x%04X)) return _(%s,%s);" % (u, short[0][d[0]], short[1][d[1]]) michael@0: print " return _(x,x);" michael@0: print "}" michael@0: print michael@0: print "#undef _" michael@0: for i in range (2): michael@0: print michael@0: vv = values[i].keys () michael@0: vv.sort () michael@0: for v in vv: michael@0: print "#undef %s_%s" % \ michael@0: (what_short[i], short[i][v]) michael@0: print michael@0: print "/* == End of generated table == */" michael@0: michael@0: # Maintain at least 30% occupancy in the table */ michael@0: if occupancy < 30: michael@0: raise Exception ("Table too sparse, please investigate: ", occupancy)