Behdad Esfahbod | 3eb936f | 2010-10-05 18:36:58 -0400 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import sys |
| 4 | |
Behdad Esfahbod | 88e7f37 | 2010-12-21 14:18:24 -0500 | [diff] [blame] | 5 | header = sys.stdin.readline (), sys.stdin.readline () |
| 6 | while sys.stdin.readline ().find ('##################') < 0: |
| 7 | pass |
Behdad Esfahbod | 3eb936f | 2010-10-05 18:36:58 -0400 | [diff] [blame] | 8 | |
Behdad Esfahbod | 14d7841 | 2010-11-17 16:52:58 -0500 | [diff] [blame] | 9 | |
| 10 | print "/* == Start of generated table == */" |
| 11 | print "/*" |
| 12 | print " * The following table is generated by running:" |
| 13 | print " *" |
| 14 | print " * ./gen-arabic-joining-table.py < ArabicShaping.txt" |
| 15 | print " *" |
| 16 | print " * on the ArabicShaping.txt file with the header:" |
| 17 | print " *" |
Behdad Esfahbod | 3eb936f | 2010-10-05 18:36:58 -0400 | [diff] [blame] | 18 | for line in header: |
Behdad Esfahbod | 14d7841 | 2010-11-17 16:52:58 -0500 | [diff] [blame] | 19 | print " * %s" % (line.strip()) |
| 20 | print " */" |
| 21 | |
Behdad Esfahbod | 88e7f37 | 2010-12-21 14:18:24 -0500 | [diff] [blame] | 22 | print "static const uint8_t joining_table[] =" |
Behdad Esfahbod | 14d7841 | 2010-11-17 16:52:58 -0500 | [diff] [blame] | 23 | print "{" |
| 24 | |
Behdad Esfahbod | 88e7f37 | 2010-12-21 14:18:24 -0500 | [diff] [blame] | 25 | |
| 26 | min_u = 0x110000 |
| 27 | max_u = 0 |
| 28 | num = 0 |
| 29 | last = -1 |
| 30 | block = '' |
| 31 | for line in sys.stdin: |
| 32 | |
| 33 | if line[0] == '#': |
| 34 | if line.find (" characters"): |
| 35 | block = line[2:].strip () |
| 36 | continue |
| 37 | |
| 38 | fields = [x.strip () for x in line.split (';')] |
| 39 | if len (fields) == 1: |
| 40 | continue |
| 41 | |
| 42 | u = int (fields[0], 16) |
| 43 | if u == 0x200C or u == 0x200D: |
| 44 | continue |
| 45 | if u < last: |
| 46 | raise Exception ("Input data character not sorted", u) |
| 47 | min_u = min (min_u, u) |
| 48 | max_u = max (max_u, u) |
| 49 | num += 1 |
| 50 | |
| 51 | if block: |
| 52 | print "\n /* %s */\n" % block |
| 53 | block = '' |
| 54 | |
| 55 | if last != -1: |
| 56 | last += 1 |
| 57 | while last < u: |
| 58 | print " JOINING_TYPE_X, /* %04X */" % last |
| 59 | last += 1 |
Behdad Esfahbod | 3eb936f | 2010-10-05 18:36:58 -0400 | [diff] [blame] | 60 | else: |
Behdad Esfahbod | 88e7f37 | 2010-12-21 14:18:24 -0500 | [diff] [blame] | 61 | last = u |
| 62 | |
| 63 | if fields[3] in ["ALAPH", "DALATH RISH"]: |
| 64 | value = "JOINING_GROUP_" + fields[3].replace(' ', '_') |
| 65 | else: |
| 66 | value = "JOINING_TYPE_" + fields[2] |
| 67 | print " %s, /* %s */" % (value, '; '.join(fields)) |
| 68 | |
| 69 | print |
Behdad Esfahbod | 14d7841 | 2010-11-17 16:52:58 -0500 | [diff] [blame] | 70 | print " JOINING_TYPE_X /* dummy */" |
| 71 | print "};" |
Behdad Esfahbod | 88e7f37 | 2010-12-21 14:18:24 -0500 | [diff] [blame] | 72 | print |
| 73 | |
| 74 | print "#define JOINING_TABLE_FIRST 0x%04x" % min_u |
| 75 | print "#define JOINING_TABLE_LAST 0x%04x" % max_u |
| 76 | print |
| 77 | |
Behdad Esfahbod | 14d7841 | 2010-11-17 16:52:58 -0500 | [diff] [blame] | 78 | print "/* == End of generated table == */" |
Behdad Esfahbod | 88e7f37 | 2010-12-21 14:18:24 -0500 | [diff] [blame] | 79 | |
| 80 | occupancy = num * 100 / (max_u - min_u + 1) |
| 81 | # Maintain at least 40% occupancy in the table */ |
| 82 | if occupancy < 40: |
| 83 | raise Exception ("Table too sparse, please investigate: ", occupancy) |