michael@0: #!/usr/bin/python michael@0: michael@0: import sys michael@0: import os.path michael@0: michael@0: if len (sys.argv) != 3: michael@0: print >>sys.stderr, "usage: ./gen-arabic-table.py ArabicShaping.txt UnicodeData.txt" michael@0: sys.exit (1) michael@0: michael@0: files = [file (x) for x in sys.argv[1:]] michael@0: michael@0: headers = [[files[0].readline (), files[0].readline ()]] michael@0: headers.append (["UnicodeData.txt does not have a header."]) michael@0: while files[0].readline ().find ('##################') < 0: michael@0: pass michael@0: michael@0: michael@0: def print_joining_table(f): michael@0: michael@0: print michael@0: print "static const uint8_t joining_table[] =" michael@0: print "{" michael@0: michael@0: min_u = 0x110000 michael@0: max_u = 0 michael@0: num = 0 michael@0: last = -1 michael@0: block = '' michael@0: for line in f: michael@0: michael@0: if line[0] == '#': michael@0: if line.find (" characters"): michael@0: block = line[2:].strip () michael@0: continue 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: u = int (fields[0], 16) michael@0: if u == 0x200C or u == 0x200D: michael@0: continue michael@0: if u < last: michael@0: raise Exception ("Input data character not sorted", u) michael@0: min_u = min (min_u, u) michael@0: max_u = max (max_u, u) michael@0: num += 1 michael@0: michael@0: if block: michael@0: print "\n /* %s */\n" % block michael@0: block = '' michael@0: michael@0: if last != -1: michael@0: last += 1 michael@0: while last < u: michael@0: print " JOINING_TYPE_X, /* %04X */" % last michael@0: last += 1 michael@0: else: michael@0: last = u michael@0: michael@0: if fields[3] in ["ALAPH", "DALATH RISH"]: michael@0: value = "JOINING_GROUP_" + fields[3].replace(' ', '_') michael@0: else: michael@0: value = "JOINING_TYPE_" + fields[2] michael@0: print " %s, /* %s */" % (value, '; '.join(fields)) michael@0: michael@0: print michael@0: print "};" michael@0: print michael@0: print "#define JOINING_TABLE_FIRST 0x%04X" % min_u michael@0: print "#define JOINING_TABLE_LAST 0x%04X" % max_u michael@0: print michael@0: michael@0: occupancy = num * 100 / (max_u - min_u + 1) michael@0: # Maintain at least 40% occupancy in the table */ michael@0: if occupancy < 40: michael@0: raise Exception ("Table too sparse, please investigate: ", occupancy) michael@0: michael@0: def print_shaping_table(f): michael@0: michael@0: shapes = {} michael@0: ligatures = {} michael@0: names = {} michael@0: for line in f: michael@0: michael@0: fields = [x.strip () for x in line.split (';')] michael@0: if fields[5][0:1] != '<': michael@0: continue michael@0: michael@0: items = fields[5].split (' ') michael@0: shape, items = items[0][1:-1], tuple (int (x, 16) for x in items[1:]) michael@0: michael@0: if not shape in ['initial', 'medial', 'isolated', 'final']: michael@0: continue michael@0: michael@0: c = int (fields[0], 16) michael@0: if len (items) != 1: michael@0: # We only care about lam-alef ligatures michael@0: if len (items) != 2 or items[0] != 0x0644 or items[1] not in [0x0622, 0x0623, 0x0625, 0x0627]: michael@0: continue michael@0: michael@0: # Save ligature michael@0: names[c] = fields[1] michael@0: if items not in ligatures: michael@0: ligatures[items] = {} michael@0: ligatures[items][shape] = c michael@0: pass michael@0: else: michael@0: # Save shape michael@0: if items[0] not in names: michael@0: names[items[0]] = fields[1] michael@0: else: michael@0: names[items[0]] = os.path.commonprefix ([names[items[0]], fields[1]]).strip () michael@0: if items[0] not in shapes: michael@0: shapes[items[0]] = {} michael@0: shapes[items[0]][shape] = c michael@0: michael@0: print michael@0: print "static const uint16_t shaping_table[][4] =" michael@0: print "{" michael@0: michael@0: keys = shapes.keys () michael@0: min_u, max_u = min (keys), max (keys) michael@0: for u in range (min_u, max_u + 1): michael@0: s = [shapes[u][shape] if u in shapes and shape in shapes[u] else 0 michael@0: for shape in ['initial', 'medial', 'final', 'isolated']] michael@0: value = ', '.join ("0x%04X" % c for c in s) michael@0: print " {%s}, /* U+%04X %s */" % (value, u, names[u] if u in names else "") michael@0: michael@0: print "};" michael@0: print michael@0: print "#define SHAPING_TABLE_FIRST 0x%04X" % min_u michael@0: print "#define SHAPING_TABLE_LAST 0x%04X" % max_u michael@0: print michael@0: michael@0: ligas = {} michael@0: for pair in ligatures.keys (): michael@0: for shape in ligatures[pair]: michael@0: c = ligatures[pair][shape] michael@0: if shape == 'isolated': michael@0: liga = (shapes[pair[0]]['initial'], shapes[pair[1]]['final']) michael@0: elif shape == 'final': michael@0: liga = (shapes[pair[0]]['medial'], shapes[pair[1]]['final']) michael@0: else: michael@0: raise Exception ("Unexpected shape", shape) michael@0: if liga[0] not in ligas: michael@0: ligas[liga[0]] = [] michael@0: ligas[liga[0]].append ((liga[1], c)) michael@0: max_i = max (len (ligas[l]) for l in ligas) michael@0: print michael@0: print "static const struct ligature_set_t {" michael@0: print " uint16_t first;" michael@0: print " struct ligature_pairs_t {" michael@0: print " uint16_t second;" michael@0: print " uint16_t ligature;" michael@0: print " } ligatures[%d];" % max_i michael@0: print "} ligature_table[] =" michael@0: print "{" michael@0: keys = ligas.keys () michael@0: keys.sort () michael@0: for first in keys: michael@0: michael@0: print " { 0x%04X, {" % (first) michael@0: for liga in ligas[first]: michael@0: print " { 0x%04X, 0x%04X }, /* %s */" % (liga[0], liga[1], names[liga[1]]) michael@0: print " }}," michael@0: michael@0: print "};" michael@0: print michael@0: michael@0: 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-arabic-table.py ArabicShaping.txt UnicodeData.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 "#ifndef HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH" michael@0: print "#define HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH" michael@0: print michael@0: michael@0: print_joining_table (files[0]) michael@0: print_shaping_table (files[1]) michael@0: michael@0: print michael@0: print "#endif /* HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH */" michael@0: print michael@0: print "/* == End of generated table == */" michael@0: