13eb936f1539475098f39be78654b9c39b86f0799Behdad Esfahbod#!/usr/bin/python
23eb936f1539475098f39be78654b9c39b86f0799Behdad Esfahbod
33eb936f1539475098f39be78654b9c39b86f0799Behdad Esfahbodimport sys
4ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbodimport os.path
53eb936f1539475098f39be78654b9c39b86f0799Behdad Esfahbod
63f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbodif len (sys.argv) != 4:
73f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod	print >>sys.stderr, "usage: ./gen-arabic-table.py ArabicShaping.txt UnicodeData.txt Blocks.txt"
8697a65c5f5cda53bc68720886a253a019e8212a8Behdad Esfahbod	sys.exit (1)
9697a65c5f5cda53bc68720886a253a019e8212a8Behdad Esfahbod
10ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbodfiles = [file (x) for x in sys.argv[1:]]
11697a65c5f5cda53bc68720886a253a019e8212a8Behdad Esfahbod
123f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbodheaders = [[files[0].readline (), files[0].readline ()], [files[2].readline (), files[2].readline ()]]
13ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbodheaders.append (["UnicodeData.txt does not have a header."])
14ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbodwhile files[0].readline ().find ('##################') < 0:
1588e7f37488e4e8590619d815b975232a0c9d2ea0Behdad Esfahbod	pass
163eb936f1539475098f39be78654b9c39b86f0799Behdad Esfahbod
173f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbodblocks = {}
183f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahboddef read_blocks(f):
193f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod	global blocks
203f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod	for line in f:
2114d784116b08685425e4ddcb1c1f813dbe2b2986Behdad Esfahbod
223f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod		j = line.find ('#')
233f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod		if j >= 0:
243f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod			line = line[:j]
25ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
263f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod		fields = [x.strip () for x in line.split (';')]
273f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod		if len (fields) == 1:
283f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod			continue
293f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod
303f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod		uu = fields[0].split ('..')
313f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod		start = int (uu[0], 16)
323f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod		if len (uu) == 1:
333f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod			end = start
343f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod		else:
353f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod			end = int (uu[1], 16)
36ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
373f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod		t = fields[1]
383f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod
393f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod		for u in range (start, end + 1):
403f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod			blocks[u] = t
413f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod
423f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahboddef print_joining_table(f):
433f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod
443f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod	values = {}
45ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod	for line in f:
46ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
47ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod		if line[0] == '#':
48ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod			continue
49ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
50ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod		fields = [x.strip () for x in line.split (';')]
51ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod		if len (fields) == 1:
52ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod			continue
53ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
54ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod		u = int (fields[0], 16)
55ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
56ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod		if fields[3] in ["ALAPH", "DALATH RISH"]:
57ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod			value = "JOINING_GROUP_" + fields[3].replace(' ', '_')
58ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod		else:
59ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod			value = "JOINING_TYPE_" + fields[2]
60f88670749006991282bcc4e8b6218487295ca670Behdad Esfahbod		values[u] = value
61ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
62200dfe3eb10feda2ad25940338b08011f4757ca4Behdad Esfahbod	short_value = {}
63f88670749006991282bcc4e8b6218487295ca670Behdad Esfahbod	for value in set([v for v in values.values()] + ['JOINING_TYPE_X']):
64200dfe3eb10feda2ad25940338b08011f4757ca4Behdad Esfahbod		short = ''.join(x[0] for x in value.split('_')[2:])
65200dfe3eb10feda2ad25940338b08011f4757ca4Behdad Esfahbod		assert short not in short_value.values()
66200dfe3eb10feda2ad25940338b08011f4757ca4Behdad Esfahbod		short_value[value] = short
67200dfe3eb10feda2ad25940338b08011f4757ca4Behdad Esfahbod
68200dfe3eb10feda2ad25940338b08011f4757ca4Behdad Esfahbod	print
69200dfe3eb10feda2ad25940338b08011f4757ca4Behdad Esfahbod	for value,short in short_value.items():
70200dfe3eb10feda2ad25940338b08011f4757ca4Behdad Esfahbod		print "#define %s	%s" % (short, value)
71200dfe3eb10feda2ad25940338b08011f4757ca4Behdad Esfahbod
72b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod	uu = sorted(values.keys())
733f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod	num = len(values)
742390d9b67e1dfeccd3f0850d1304c52b54817ca0Behdad Esfahbod	all_blocks = set([blocks[u] for u in uu])
75b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod
76dcee838e89c2863c2fda4e8f098e720637e02335Behdad Esfahbod	last = -100000
77b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod	ranges = []
78b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod	for u in uu:
792390d9b67e1dfeccd3f0850d1304c52b54817ca0Behdad Esfahbod		if u - last <= 1+16*5:
80b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod			ranges[-1][-1] = u
81b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod		else:
82b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod			ranges.append([u,u])
83b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod		last = u
84b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod
853f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod	print
863f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod	print "static const uint8_t joining_table[] ="
873f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod	print "{"
883f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbod	last_block = None
89b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod	offset = 0
90b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod	for start,end in ranges:
91b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod
92b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod		print
93c09a607a842fdd7b33e2c57e8af96d21ff091acdBehdad Esfahbod		print "#define joining_offset_0x%04xu %d" % (start, offset)
94b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod
95b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod		for u in range(start, end+1):
96b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod
97b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod			block = blocks.get(u, last_block)
98b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod			value = values.get(u, "JOINING_TYPE_X")
99f88670749006991282bcc4e8b6218487295ca670Behdad Esfahbod
100b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod			if block != last_block or u == start:
101b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod				if u != start:
102b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod					print
1032390d9b67e1dfeccd3f0850d1304c52b54817ca0Behdad Esfahbod				if block in all_blocks:
1042390d9b67e1dfeccd3f0850d1304c52b54817ca0Behdad Esfahbod					print "\n  /* %s */" % block
1052390d9b67e1dfeccd3f0850d1304c52b54817ca0Behdad Esfahbod				else:
1062390d9b67e1dfeccd3f0850d1304c52b54817ca0Behdad Esfahbod					print "\n  /* FILLER */"
107b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod				last_block = block
108b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod				if u % 32 != 0:
109b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod					print
110b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod					print "  /* %04X */" % (u//32*32), "  " * (u % 32),
111f88670749006991282bcc4e8b6218487295ca670Behdad Esfahbod
112b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod			if u % 32 == 0:
113f88670749006991282bcc4e8b6218487295ca670Behdad Esfahbod				print
114b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod				print "  /* %04X */ " % u,
115b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod			sys.stdout.write("%s," % short_value[value])
116b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod		print
117f88670749006991282bcc4e8b6218487295ca670Behdad Esfahbod
118b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod		offset += end - start + 1
119ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod	print
120b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod	occupancy = num * 100. / offset
121b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod	print "}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy)
122ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod	print
123ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
1242390d9b67e1dfeccd3f0850d1304c52b54817ca0Behdad Esfahbod	page_bits = 12;
125b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod	print
126b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod	print "static unsigned int"
127b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod	print "joining_type (hb_codepoint_t u)"
128b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod	print "{"
129b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod	print "  switch (u >> %d)" % page_bits
130b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod	print "  {"
131b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod	pages = set([u>>page_bits for u in [s for s,e in ranges]+[e for s,e in ranges]])
132b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod	for p in sorted(pages):
133c09a607a842fdd7b33e2c57e8af96d21ff091acdBehdad Esfahbod		print "    case 0x%0Xu:" % p
134b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod		for (start,end) in ranges:
135b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod			if p not in [start>>page_bits, end>>page_bits]: continue
136c09a607a842fdd7b33e2c57e8af96d21ff091acdBehdad Esfahbod			offset = "joining_offset_0x%04xu" % start
137c09a607a842fdd7b33e2c57e8af96d21ff091acdBehdad Esfahbod			print "      if (hb_in_range (u, 0x%04Xu, 0x%04Xu)) return joining_table[u - 0x%04Xu + %s];" % (start, end, start, offset)
138b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod		print "      break;"
139b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod		print ""
140b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod	print "    default:"
141b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod	print "      break;"
142b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod	print "  }"
143b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod	print "  return X;"
144b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod	print "}"
145b900fa2c8cc088dbcbdbf90bfdf8764f9ee1c96aBehdad Esfahbod	print
146200dfe3eb10feda2ad25940338b08011f4757ca4Behdad Esfahbod	for value,short in short_value.items():
147200dfe3eb10feda2ad25940338b08011f4757ca4Behdad Esfahbod		print "#undef %s" % (short)
148200dfe3eb10feda2ad25940338b08011f4757ca4Behdad Esfahbod	print
149200dfe3eb10feda2ad25940338b08011f4757ca4Behdad Esfahbod
150ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahboddef print_shaping_table(f):
151ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
152ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod	shapes = {}
153ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod	ligatures = {}
154ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod	names = {}
155ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod	for line in f:
156ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
157ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod		fields = [x.strip () for x in line.split (';')]
158ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod		if fields[5][0:1] != '<':
159ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod			continue
160ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
161ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod		items = fields[5].split (' ')
162ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod		shape, items = items[0][1:-1], tuple (int (x, 16) for x in items[1:])
163ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
164ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod		if not shape in ['initial', 'medial', 'isolated', 'final']:
165ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod			continue
166ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
167ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod		c = int (fields[0], 16)
168ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod		if len (items) != 1:
169ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod			# We only care about lam-alef ligatures
170ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod			if len (items) != 2 or items[0] != 0x0644 or items[1] not in [0x0622, 0x0623, 0x0625, 0x0627]:
171ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod				continue
172ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
173ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod			# Save ligature
174ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod			names[c] = fields[1]
175ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod			if items not in ligatures:
176ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod				ligatures[items] = {}
177ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod			ligatures[items][shape] = c
178ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod			pass
179ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod		else:
180ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod			# Save shape
181ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod			if items[0] not in names:
182ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod				names[items[0]] = fields[1]
183ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod			else:
184ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod				names[items[0]] = os.path.commonprefix ([names[items[0]], fields[1]]).strip ()
185ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod			if items[0] not in shapes:
186ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod				shapes[items[0]] = {}
187ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod			shapes[items[0]][shape] = c
188ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
189ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod	print
190ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod	print "static const uint16_t shaping_table[][4] ="
191ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod	print "{"
192ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
193ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod	keys = shapes.keys ()
194ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod	min_u, max_u = min (keys), max (keys)
195ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod	for u in range (min_u, max_u + 1):
19607cfbe21b511056d5c3c75cbfe84e4e982c3e8fbBehdad Esfahbod		s = [shapes[u][shape] if u in shapes and shape in shapes[u] else 0
197ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod		     for shape in  ['initial', 'medial', 'final', 'isolated']]
198c09a607a842fdd7b33e2c57e8af96d21ff091acdBehdad Esfahbod		value = ', '.join ("0x%04Xu" % c for c in s)
199ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod		print "  {%s}, /* U+%04X %s */" % (value, u, names[u] if u in names else "")
200ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
201ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod	print "};"
202ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod	print
203c09a607a842fdd7b33e2c57e8af96d21ff091acdBehdad Esfahbod	print "#define SHAPING_TABLE_FIRST	0x%04Xu" % min_u
204c09a607a842fdd7b33e2c57e8af96d21ff091acdBehdad Esfahbod	print "#define SHAPING_TABLE_LAST	0x%04Xu" % max_u
205ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod	print
206ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
207939c010211b063f78874a3b72b032c1ed9a13b87Behdad Esfahbod	ligas = {}
208ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod	for pair in ligatures.keys ():
209ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod		for shape in ligatures[pair]:
210ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod			c = ligatures[pair][shape]
211ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod			if shape == 'isolated':
212ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod				liga = (shapes[pair[0]]['initial'], shapes[pair[1]]['final'])
213ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod			elif shape == 'final':
214ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod				liga = (shapes[pair[0]]['medial'], shapes[pair[1]]['final'])
215ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod			else:
216ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod				raise Exception ("Unexpected shape", shape)
217939c010211b063f78874a3b72b032c1ed9a13b87Behdad Esfahbod			if liga[0] not in ligas:
218939c010211b063f78874a3b72b032c1ed9a13b87Behdad Esfahbod				ligas[liga[0]] = []
219939c010211b063f78874a3b72b032c1ed9a13b87Behdad Esfahbod			ligas[liga[0]].append ((liga[1], c))
220939c010211b063f78874a3b72b032c1ed9a13b87Behdad Esfahbod	max_i = max (len (ligas[l]) for l in ligas)
221939c010211b063f78874a3b72b032c1ed9a13b87Behdad Esfahbod	print
22207cfbe21b511056d5c3c75cbfe84e4e982c3e8fbBehdad Esfahbod	print "static const struct ligature_set_t {"
223939c010211b063f78874a3b72b032c1ed9a13b87Behdad Esfahbod	print " uint16_t first;"
22407cfbe21b511056d5c3c75cbfe84e4e982c3e8fbBehdad Esfahbod	print " struct ligature_pairs_t {"
225939c010211b063f78874a3b72b032c1ed9a13b87Behdad Esfahbod	print "   uint16_t second;"
226939c010211b063f78874a3b72b032c1ed9a13b87Behdad Esfahbod	print "   uint16_t ligature;"
227939c010211b063f78874a3b72b032c1ed9a13b87Behdad Esfahbod	print " } ligatures[%d];" % max_i
228939c010211b063f78874a3b72b032c1ed9a13b87Behdad Esfahbod	print "} ligature_table[] ="
229939c010211b063f78874a3b72b032c1ed9a13b87Behdad Esfahbod	print "{"
230939c010211b063f78874a3b72b032c1ed9a13b87Behdad Esfahbod	keys = ligas.keys ()
231939c010211b063f78874a3b72b032c1ed9a13b87Behdad Esfahbod	keys.sort ()
232939c010211b063f78874a3b72b032c1ed9a13b87Behdad Esfahbod	for first in keys:
233939c010211b063f78874a3b72b032c1ed9a13b87Behdad Esfahbod
234c09a607a842fdd7b33e2c57e8af96d21ff091acdBehdad Esfahbod		print "  { 0x%04Xu, {" % (first)
235939c010211b063f78874a3b72b032c1ed9a13b87Behdad Esfahbod		for liga in ligas[first]:
236c09a607a842fdd7b33e2c57e8af96d21ff091acdBehdad Esfahbod			print "    { 0x%04Xu, 0x%04Xu }, /* %s */" % (liga[0], liga[1], names[liga[1]])
237939c010211b063f78874a3b72b032c1ed9a13b87Behdad Esfahbod		print "  }},"
238ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
239ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod	print "};"
240ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod	print
241ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
242ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
243ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
24414d784116b08685425e4ddcb1c1f813dbe2b2986Behdad Esfahbodprint "/* == Start of generated table == */"
24514d784116b08685425e4ddcb1c1f813dbe2b2986Behdad Esfahbodprint "/*"
24614d784116b08685425e4ddcb1c1f813dbe2b2986Behdad Esfahbodprint " * The following table is generated by running:"
24714d784116b08685425e4ddcb1c1f813dbe2b2986Behdad Esfahbodprint " *"
2483f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbodprint " *   ./gen-arabic-table.py ArabicShaping.txt UnicodeData.txt Blocks.txt"
24914d784116b08685425e4ddcb1c1f813dbe2b2986Behdad Esfahbodprint " *"
250697a65c5f5cda53bc68720886a253a019e8212a8Behdad Esfahbodprint " * on files with these headers:"
25114d784116b08685425e4ddcb1c1f813dbe2b2986Behdad Esfahbodprint " *"
252ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbodfor h in headers:
253ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod	for l in h:
254ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod		print " * %s" % (l.strip())
25514d784116b08685425e4ddcb1c1f813dbe2b2986Behdad Esfahbodprint " */"
2566d4016f1ba48d409800dc3281b93e5cd58c99d9fBehdad Esfahbodprint
2576d4016f1ba48d409800dc3281b93e5cd58c99d9fBehdad Esfahbodprint "#ifndef HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH"
2586d4016f1ba48d409800dc3281b93e5cd58c99d9fBehdad Esfahbodprint "#define HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH"
2596d4016f1ba48d409800dc3281b93e5cd58c99d9fBehdad Esfahbodprint
260ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
2613f5327a41efcf50f64d9498ef3dfd6875ef6a5abBehdad Esfahbodread_blocks (files[2])
262ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbodprint_joining_table (files[0])
263ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbodprint_shaping_table (files[1])
264ae4a2b9365051c23c9a299cf76f3ab7e661999b1Behdad Esfahbod
26588e7f37488e4e8590619d815b975232a0c9d2ea0Behdad Esfahbodprint
2666d4016f1ba48d409800dc3281b93e5cd58c99d9fBehdad Esfahbodprint "#endif /* HB_OT_SHAPE_COMPLEX_ARABIC_TABLE_HH */"
2676d4016f1ba48d409800dc3281b93e5cd58c99d9fBehdad Esfahbodprint
26814d784116b08685425e4ddcb1c1f813dbe2b2986Behdad Esfahbodprint "/* == End of generated table == */"
26988e7f37488e4e8590619d815b975232a0c9d2ea0Behdad Esfahbod
270