Ebrahim Byagowi | 8d19907 | 2020-02-19 14:56:55 +0330 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Behdad Esfahbod | 3eb936f | 2010-10-05 18:36:58 -0400 | [diff] [blame] | 2 | |
Ebrahim Byagowi | 80395f1 | 2018-03-29 22:00:41 +0430 | [diff] [blame] | 3 | import io, os.path, sys |
Behdad Esfahbod | 3eb936f | 2010-10-05 18:36:58 -0400 | [diff] [blame] | 4 | |
Behdad Esfahbod | 3f5327a | 2014-06-20 16:17:42 -0400 | [diff] [blame] | 5 | if len (sys.argv) != 4: |
Ebrahim Byagowi | 6a390df | 2020-02-10 17:19:23 +0330 | [diff] [blame] | 6 | print ("""usage: ./gen-arabic-table.py ArabicShaping.txt UnicodeData.txt Blocks.txt |
| 7 | |
David Corbett | fd748fa | 2020-03-15 15:59:31 -0400 | [diff] [blame^] | 8 | Input files: |
Ebrahim Byagowi | 6a390df | 2020-02-10 17:19:23 +0330 | [diff] [blame] | 9 | * https://unicode.org/Public/UCD/latest/ucd/ArabicShaping.txt |
| 10 | * https://unicode.org/Public/UCD/latest/ucd/UnicodeData.txt |
| 11 | * https://unicode.org/Public/UCD/latest/ucd/Blocks.txt |
| 12 | """, file=sys.stderr) |
Behdad Esfahbod | 697a65c | 2011-06-01 20:52:00 -0400 | [diff] [blame] | 13 | sys.exit (1) |
| 14 | |
Ebrahim Byagowi | 80395f1 | 2018-03-29 22:00:41 +0430 | [diff] [blame] | 15 | files = [io.open (x, encoding='utf-8') for x in sys.argv[1:]] |
Behdad Esfahbod | 697a65c | 2011-06-01 20:52:00 -0400 | [diff] [blame] | 16 | |
Behdad Esfahbod | 3f5327a | 2014-06-20 16:17:42 -0400 | [diff] [blame] | 17 | headers = [[files[0].readline (), files[0].readline ()], [files[2].readline (), files[2].readline ()]] |
Behdad Esfahbod | ae4a2b9 | 2012-04-10 16:25:08 -0400 | [diff] [blame] | 18 | headers.append (["UnicodeData.txt does not have a header."]) |
| 19 | while files[0].readline ().find ('##################') < 0: |
Behdad Esfahbod | 88e7f37 | 2010-12-21 14:18:24 -0500 | [diff] [blame] | 20 | pass |
Behdad Esfahbod | 3eb936f | 2010-10-05 18:36:58 -0400 | [diff] [blame] | 21 | |
Behdad Esfahbod | 3f5327a | 2014-06-20 16:17:42 -0400 | [diff] [blame] | 22 | blocks = {} |
| 23 | def read_blocks(f): |
| 24 | global blocks |
| 25 | for line in f: |
| 26 | |
| 27 | j = line.find ('#') |
| 28 | if j >= 0: |
| 29 | line = line[:j] |
| 30 | |
| 31 | fields = [x.strip () for x in line.split (';')] |
| 32 | if len (fields) == 1: |
| 33 | continue |
| 34 | |
| 35 | uu = fields[0].split ('..') |
| 36 | start = int (uu[0], 16) |
| 37 | if len (uu) == 1: |
| 38 | end = start |
| 39 | else: |
| 40 | end = int (uu[1], 16) |
| 41 | |
| 42 | t = fields[1] |
| 43 | |
| 44 | for u in range (start, end + 1): |
| 45 | blocks[u] = t |
Behdad Esfahbod | 14d7841 | 2010-11-17 16:52:58 -0500 | [diff] [blame] | 46 | |
Behdad Esfahbod | ae4a2b9 | 2012-04-10 16:25:08 -0400 | [diff] [blame] | 47 | def print_joining_table(f): |
| 48 | |
Behdad Esfahbod | 3f5327a | 2014-06-20 16:17:42 -0400 | [diff] [blame] | 49 | values = {} |
Behdad Esfahbod | ae4a2b9 | 2012-04-10 16:25:08 -0400 | [diff] [blame] | 50 | for line in f: |
| 51 | |
| 52 | if line[0] == '#': |
Behdad Esfahbod | ae4a2b9 | 2012-04-10 16:25:08 -0400 | [diff] [blame] | 53 | continue |
| 54 | |
| 55 | fields = [x.strip () for x in line.split (';')] |
| 56 | if len (fields) == 1: |
| 57 | continue |
| 58 | |
| 59 | u = int (fields[0], 16) |
Behdad Esfahbod | ae4a2b9 | 2012-04-10 16:25:08 -0400 | [diff] [blame] | 60 | |
| 61 | if fields[3] in ["ALAPH", "DALATH RISH"]: |
| 62 | value = "JOINING_GROUP_" + fields[3].replace(' ', '_') |
| 63 | else: |
| 64 | value = "JOINING_TYPE_" + fields[2] |
Behdad Esfahbod | f886707 | 2014-06-20 16:30:10 -0400 | [diff] [blame] | 65 | values[u] = value |
Behdad Esfahbod | ae4a2b9 | 2012-04-10 16:25:08 -0400 | [diff] [blame] | 66 | |
Behdad Esfahbod | 200dfe3 | 2014-06-20 16:20:59 -0400 | [diff] [blame] | 67 | short_value = {} |
David Corbett | fd748fa | 2020-03-15 15:59:31 -0400 | [diff] [blame^] | 68 | for value in sorted (set ([v for v in values.values ()] + ['JOINING_TYPE_X'])): |
Behdad Esfahbod | 200dfe3 | 2014-06-20 16:20:59 -0400 | [diff] [blame] | 69 | short = ''.join(x[0] for x in value.split('_')[2:]) |
| 70 | assert short not in short_value.values() |
| 71 | short_value[value] = short |
| 72 | |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 73 | print () |
Behdad Esfahbod | 200dfe3 | 2014-06-20 16:20:59 -0400 | [diff] [blame] | 74 | for value,short in short_value.items(): |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 75 | print ("#define %s %s" % (short, value)) |
Behdad Esfahbod | 200dfe3 | 2014-06-20 16:20:59 -0400 | [diff] [blame] | 76 | |
Behdad Esfahbod | b900fa2 | 2014-06-20 17:59:43 -0400 | [diff] [blame] | 77 | uu = sorted(values.keys()) |
Behdad Esfahbod | 3f5327a | 2014-06-20 16:17:42 -0400 | [diff] [blame] | 78 | num = len(values) |
Behdad Esfahbod | 2390d9b | 2014-06-21 14:07:00 -0600 | [diff] [blame] | 79 | all_blocks = set([blocks[u] for u in uu]) |
Behdad Esfahbod | b900fa2 | 2014-06-20 17:59:43 -0400 | [diff] [blame] | 80 | |
Behdad Esfahbod | dcee838 | 2014-06-22 11:29:59 -0600 | [diff] [blame] | 81 | last = -100000 |
Behdad Esfahbod | b900fa2 | 2014-06-20 17:59:43 -0400 | [diff] [blame] | 82 | ranges = [] |
| 83 | for u in uu: |
Behdad Esfahbod | 2390d9b | 2014-06-21 14:07:00 -0600 | [diff] [blame] | 84 | if u - last <= 1+16*5: |
Behdad Esfahbod | b900fa2 | 2014-06-20 17:59:43 -0400 | [diff] [blame] | 85 | ranges[-1][-1] = u |
| 86 | else: |
| 87 | ranges.append([u,u]) |
| 88 | last = u |
| 89 | |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 90 | print () |
| 91 | print ("static const uint8_t joining_table[] =") |
| 92 | print ("{") |
Behdad Esfahbod | 3f5327a | 2014-06-20 16:17:42 -0400 | [diff] [blame] | 93 | last_block = None |
Behdad Esfahbod | b900fa2 | 2014-06-20 17:59:43 -0400 | [diff] [blame] | 94 | offset = 0 |
| 95 | for start,end in ranges: |
Behdad Esfahbod | f886707 | 2014-06-20 16:30:10 -0400 | [diff] [blame] | 96 | |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 97 | print () |
| 98 | print ("#define joining_offset_0x%04xu %d" % (start, offset)) |
Behdad Esfahbod | f886707 | 2014-06-20 16:30:10 -0400 | [diff] [blame] | 99 | |
Behdad Esfahbod | b900fa2 | 2014-06-20 17:59:43 -0400 | [diff] [blame] | 100 | for u in range(start, end+1): |
| 101 | |
| 102 | block = blocks.get(u, last_block) |
| 103 | value = values.get(u, "JOINING_TYPE_X") |
| 104 | |
| 105 | if block != last_block or u == start: |
| 106 | if u != start: |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 107 | print () |
Behdad Esfahbod | 2390d9b | 2014-06-21 14:07:00 -0600 | [diff] [blame] | 108 | if block in all_blocks: |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 109 | print ("\n /* %s */" % block) |
Behdad Esfahbod | 2390d9b | 2014-06-21 14:07:00 -0600 | [diff] [blame] | 110 | else: |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 111 | print ("\n /* FILLER */") |
Behdad Esfahbod | b900fa2 | 2014-06-20 17:59:43 -0400 | [diff] [blame] | 112 | last_block = block |
| 113 | if u % 32 != 0: |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 114 | print () |
| 115 | print (" /* %04X */" % (u//32*32), " " * (u % 32), end="") |
Behdad Esfahbod | b900fa2 | 2014-06-20 17:59:43 -0400 | [diff] [blame] | 116 | |
| 117 | if u % 32 == 0: |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 118 | print () |
| 119 | print (" /* %04X */ " % u, end="") |
| 120 | print ("%s," % short_value[value], end="") |
| 121 | print () |
Behdad Esfahbod | 3f5327a | 2014-06-20 16:17:42 -0400 | [diff] [blame] | 122 | |
Behdad Esfahbod | b900fa2 | 2014-06-20 17:59:43 -0400 | [diff] [blame] | 123 | offset += end - start + 1 |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 124 | print () |
Behdad Esfahbod | b900fa2 | 2014-06-20 17:59:43 -0400 | [diff] [blame] | 125 | occupancy = num * 100. / offset |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 126 | print ("}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy)) |
| 127 | print () |
Behdad Esfahbod | ae4a2b9 | 2012-04-10 16:25:08 -0400 | [diff] [blame] | 128 | |
Behdad Esfahbod | 2390d9b | 2014-06-21 14:07:00 -0600 | [diff] [blame] | 129 | page_bits = 12; |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 130 | print () |
| 131 | print ("static unsigned int") |
| 132 | print ("joining_type (hb_codepoint_t u)") |
| 133 | print ("{") |
| 134 | print (" switch (u >> %d)" % page_bits) |
| 135 | print (" {") |
Behdad Esfahbod | b900fa2 | 2014-06-20 17:59:43 -0400 | [diff] [blame] | 136 | pages = set([u>>page_bits for u in [s for s,e in ranges]+[e for s,e in ranges]]) |
| 137 | for p in sorted(pages): |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 138 | print (" case 0x%0Xu:" % p) |
Behdad Esfahbod | b900fa2 | 2014-06-20 17:59:43 -0400 | [diff] [blame] | 139 | for (start,end) in ranges: |
| 140 | if p not in [start>>page_bits, end>>page_bits]: continue |
Behdad Esfahbod | c09a607 | 2014-07-11 15:05:36 -0400 | [diff] [blame] | 141 | offset = "joining_offset_0x%04xu" % start |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 142 | print (" if (hb_in_range<hb_codepoint_t> (u, 0x%04Xu, 0x%04Xu)) return joining_table[u - 0x%04Xu + %s];" % (start, end, start, offset)) |
| 143 | print (" break;") |
| 144 | print ("") |
| 145 | print (" default:") |
| 146 | print (" break;") |
| 147 | print (" }") |
| 148 | print (" return X;") |
| 149 | print ("}") |
| 150 | print () |
Behdad Esfahbod | 200dfe3 | 2014-06-20 16:20:59 -0400 | [diff] [blame] | 151 | for value,short in short_value.items(): |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 152 | print ("#undef %s" % (short)) |
| 153 | print () |
Behdad Esfahbod | 200dfe3 | 2014-06-20 16:20:59 -0400 | [diff] [blame] | 154 | |
Behdad Esfahbod | ae4a2b9 | 2012-04-10 16:25:08 -0400 | [diff] [blame] | 155 | def print_shaping_table(f): |
| 156 | |
| 157 | shapes = {} |
| 158 | ligatures = {} |
| 159 | names = {} |
| 160 | for line in f: |
| 161 | |
| 162 | fields = [x.strip () for x in line.split (';')] |
| 163 | if fields[5][0:1] != '<': |
| 164 | continue |
| 165 | |
| 166 | items = fields[5].split (' ') |
| 167 | shape, items = items[0][1:-1], tuple (int (x, 16) for x in items[1:]) |
| 168 | |
| 169 | if not shape in ['initial', 'medial', 'isolated', 'final']: |
| 170 | continue |
| 171 | |
| 172 | c = int (fields[0], 16) |
| 173 | if len (items) != 1: |
| 174 | # We only care about lam-alef ligatures |
| 175 | if len (items) != 2 or items[0] != 0x0644 or items[1] not in [0x0622, 0x0623, 0x0625, 0x0627]: |
| 176 | continue |
| 177 | |
| 178 | # Save ligature |
| 179 | names[c] = fields[1] |
| 180 | if items not in ligatures: |
| 181 | ligatures[items] = {} |
| 182 | ligatures[items][shape] = c |
| 183 | pass |
| 184 | else: |
| 185 | # Save shape |
| 186 | if items[0] not in names: |
| 187 | names[items[0]] = fields[1] |
| 188 | else: |
| 189 | names[items[0]] = os.path.commonprefix ([names[items[0]], fields[1]]).strip () |
| 190 | if items[0] not in shapes: |
| 191 | shapes[items[0]] = {} |
| 192 | shapes[items[0]][shape] = c |
| 193 | |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 194 | print () |
| 195 | print ("static const uint16_t shaping_table[][4] =") |
| 196 | print ("{") |
Behdad Esfahbod | ae4a2b9 | 2012-04-10 16:25:08 -0400 | [diff] [blame] | 197 | |
| 198 | keys = shapes.keys () |
| 199 | min_u, max_u = min (keys), max (keys) |
| 200 | for u in range (min_u, max_u + 1): |
Behdad Esfahbod | 07cfbe2 | 2012-09-06 01:16:39 -0400 | [diff] [blame] | 201 | s = [shapes[u][shape] if u in shapes and shape in shapes[u] else 0 |
Behdad Esfahbod | ae4a2b9 | 2012-04-10 16:25:08 -0400 | [diff] [blame] | 202 | for shape in ['initial', 'medial', 'final', 'isolated']] |
Behdad Esfahbod | c09a607 | 2014-07-11 15:05:36 -0400 | [diff] [blame] | 203 | value = ', '.join ("0x%04Xu" % c for c in s) |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 204 | print (" {%s}, /* U+%04X %s */" % (value, u, names[u] if u in names else "")) |
Behdad Esfahbod | ae4a2b9 | 2012-04-10 16:25:08 -0400 | [diff] [blame] | 205 | |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 206 | print ("};") |
| 207 | print () |
| 208 | print ("#define SHAPING_TABLE_FIRST 0x%04Xu" % min_u) |
| 209 | print ("#define SHAPING_TABLE_LAST 0x%04Xu" % max_u) |
| 210 | print () |
Behdad Esfahbod | ae4a2b9 | 2012-04-10 16:25:08 -0400 | [diff] [blame] | 211 | |
Behdad Esfahbod | 939c010 | 2012-04-10 17:20:05 -0400 | [diff] [blame] | 212 | ligas = {} |
Behdad Esfahbod | ae4a2b9 | 2012-04-10 16:25:08 -0400 | [diff] [blame] | 213 | for pair in ligatures.keys (): |
| 214 | for shape in ligatures[pair]: |
| 215 | c = ligatures[pair][shape] |
| 216 | if shape == 'isolated': |
| 217 | liga = (shapes[pair[0]]['initial'], shapes[pair[1]]['final']) |
| 218 | elif shape == 'final': |
| 219 | liga = (shapes[pair[0]]['medial'], shapes[pair[1]]['final']) |
| 220 | else: |
| 221 | raise Exception ("Unexpected shape", shape) |
Behdad Esfahbod | 939c010 | 2012-04-10 17:20:05 -0400 | [diff] [blame] | 222 | if liga[0] not in ligas: |
| 223 | ligas[liga[0]] = [] |
| 224 | ligas[liga[0]].append ((liga[1], c)) |
| 225 | max_i = max (len (ligas[l]) for l in ligas) |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 226 | print () |
| 227 | print ("static const struct ligature_set_t {") |
| 228 | print (" uint16_t first;") |
| 229 | print (" struct ligature_pairs_t {") |
| 230 | print (" uint16_t second;") |
| 231 | print (" uint16_t ligature;") |
| 232 | print (" } ligatures[%d];" % max_i) |
| 233 | print ("} ligature_table[] =") |
| 234 | print ("{") |
Ebrahim Byagowi | 26e0cbd | 2018-03-29 21:22:47 +0430 | [diff] [blame] | 235 | for first in sorted (ligas.keys ()): |
Behdad Esfahbod | 939c010 | 2012-04-10 17:20:05 -0400 | [diff] [blame] | 236 | |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 237 | print (" { 0x%04Xu, {" % (first)) |
Behdad Esfahbod | 939c010 | 2012-04-10 17:20:05 -0400 | [diff] [blame] | 238 | for liga in ligas[first]: |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 239 | print (" { 0x%04Xu, 0x%04Xu }, /* %s */" % (liga[0], liga[1], names[liga[1]])) |
| 240 | print (" }},") |
Behdad Esfahbod | ae4a2b9 | 2012-04-10 16:25:08 -0400 | [diff] [blame] | 241 | |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 242 | print ("};") |
| 243 | print () |
Behdad Esfahbod | ae4a2b9 | 2012-04-10 16:25:08 -0400 | [diff] [blame] | 244 | |
| 245 | |
| 246 | |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 247 | print ("/* == Start of generated table == */") |
| 248 | print ("/*") |
| 249 | print (" * The following table is generated by running:") |
| 250 | print (" *") |
| 251 | print (" * ./gen-arabic-table.py ArabicShaping.txt UnicodeData.txt Blocks.txt") |
| 252 | print (" *") |
| 253 | print (" * on files with these headers:") |
| 254 | print (" *") |
Behdad Esfahbod | ae4a2b9 | 2012-04-10 16:25:08 -0400 | [diff] [blame] | 255 | for h in headers: |
| 256 | for l in h: |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 257 | print (" * %s" % (l.strip())) |
| 258 | print (" */") |
| 259 | print () |
| 260 | print ("#ifndef HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH") |
| 261 | print ("#define HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH") |
| 262 | print () |
Behdad Esfahbod | 14d7841 | 2010-11-17 16:52:58 -0500 | [diff] [blame] | 263 | |
Behdad Esfahbod | 3f5327a | 2014-06-20 16:17:42 -0400 | [diff] [blame] | 264 | read_blocks (files[2]) |
Behdad Esfahbod | ae4a2b9 | 2012-04-10 16:25:08 -0400 | [diff] [blame] | 265 | print_joining_table (files[0]) |
| 266 | print_shaping_table (files[1]) |
Behdad Esfahbod | d606daa | 2011-09-20 14:34:06 -0400 | [diff] [blame] | 267 | |
Ebrahim Byagowi | cab2c2c | 2018-03-29 12:48:47 +0430 | [diff] [blame] | 268 | print () |
| 269 | print ("#endif /* HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH */") |
| 270 | print () |
| 271 | print ("/* == End of generated table == */") |