1#!/usr/bin/python
2
3import sys
4
5if len (sys.argv) != 4:
6	print >>sys.stderr, "usage: ./gen-indic-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt Blocks.txt"
7	sys.exit (1)
8
9ALLOWED_SINGLES = [0x00A0, 0x25CC]
10ALLOWED_BLOCKS = [
11	'Basic Latin',
12	'Latin-1 Supplement',
13	'Devanagari',
14	'Bengali',
15	'Gurmukhi',
16	'Gujarati',
17	'Oriya',
18	'Tamil',
19	'Telugu',
20	'Kannada',
21	'Malayalam',
22	'Sinhala',
23	'Myanmar',
24	'Khmer',
25	'Vedic Extensions',
26	'General Punctuation',
27	'Superscripts and Subscripts',
28	'Devanagari Extended',
29	'Myanmar Extended-B',
30	'Myanmar Extended-A',
31]
32
33files = [file (x) for x in sys.argv[1:]]
34
35headers = [[f.readline () for i in range (2)] for f in files]
36
37data = [{} for f in files]
38values = [{} for f in files]
39for i, f in enumerate (files):
40	for line in f:
41
42		j = line.find ('#')
43		if j >= 0:
44			line = line[:j]
45
46		fields = [x.strip () for x in line.split (';')]
47		if len (fields) == 1:
48			continue
49
50		uu = fields[0].split ('..')
51		start = int (uu[0], 16)
52		if len (uu) == 1:
53			end = start
54		else:
55			end = int (uu[1], 16)
56
57		t = fields[1]
58
59		for u in range (start, end + 1):
60			data[i][u] = t
61		values[i][t] = values[i].get (t, 0) + end - start + 1
62
63# Merge data into one dict:
64defaults = ('Other', 'Not_Applicable', 'No_Block')
65for i,v in enumerate (defaults):
66	values[i][v] = values[i].get (v, 0) + 1
67combined = {}
68for i,d in enumerate (data):
69	for u,v in d.items ():
70		if i == 2 and not u in combined:
71			continue
72		if not u in combined:
73			combined[u] = list (defaults)
74		combined[u][i] = v
75combined = {k:v for k,v in combined.items() if k in ALLOWED_SINGLES or v[2] in ALLOWED_BLOCKS}
76data = combined
77del combined
78num = len (data)
79
80for u in [0x17CD, 0x17CE, 0x17CF, 0x17D0, 0x17D3]:
81	if data[u][0] == 'Other':
82		data[u][0] = "Vowel_Dependent"
83
84# Move the outliers NO-BREAK SPACE and DOTTED CIRCLE out
85singles = {}
86for u in ALLOWED_SINGLES:
87	singles[u] = data[u]
88	del data[u]
89
90print "/* == Start of generated table == */"
91print "/*"
92print " * The following table is generated by running:"
93print " *"
94print " *   ./gen-indic-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt Blocks.txt"
95print " *"
96print " * on files with these headers:"
97print " *"
98for h in headers:
99	for l in h:
100		print " * %s" % (l.strip())
101print " */"
102print
103print '#include "hb-ot-shape-complex-indic-private.hh"'
104print
105
106# Shorten values
107short = [{
108	"Bindu":		'Bi',
109	"Cantillation_Mark":	'Ca',
110	"Joiner":		'ZWJ',
111	"Non_Joiner":		'ZWNJ',
112	"Number":		'Nd',
113	"Visarga":		'Vs',
114	"Vowel":		'Vo',
115	"Vowel_Dependent":	'M',
116	"Consonant_Prefixed":	'CPrf',
117	"Other":		'x',
118},{
119	"Not_Applicable":	'x',
120}]
121all_shorts = [{},{}]
122
123# Add some of the values, to make them more readable, and to avoid duplicates
124
125
126for i in range (2):
127	for v,s in short[i].items ():
128		all_shorts[i][s] = v
129
130what = ["INDIC_SYLLABIC_CATEGORY", "INDIC_MATRA_CATEGORY"]
131what_short = ["ISC", "IMC"]
132for i in range (2):
133	print
134	vv = values[i].keys ()
135	vv.sort ()
136	for v in vv:
137		v_no_and = v.replace ('_And_', '_')
138		if v in short[i]:
139			s = short[i][v]
140		else:
141			s = ''.join ([c for c in v_no_and if ord ('A') <= ord (c) <= ord ('Z')])
142			if s in all_shorts[i]:
143				raise Exception ("Duplicate short value alias", v, all_shorts[i][s])
144			all_shorts[i][s] = v
145			short[i][v] = s
146		print "#define %s_%s	%s_%s	%s/* %3d chars; %s */" % \
147			(what_short[i], s, what[i], v.upper (), \
148			'	'* ((48-1 - len (what[i]) - 1 - len (v)) / 8), \
149			values[i][v], v)
150print
151print "#define _(S,M) INDIC_COMBINE_CATEGORIES (ISC_##S, IMC_##M)"
152print
153print
154
155total = 0
156used = 0
157last_block = None
158def print_block (block, start, end, data):
159	global total, used, last_block
160	if block and block != last_block:
161		print
162		print
163		print "  /* %s */" % block
164	num = 0
165	assert start % 8 == 0
166	assert (end+1) % 8 == 0
167	for u in range (start, end+1):
168		if u % 8 == 0:
169			print
170			print "  /* %04X */" % u,
171		if u in data:
172			num += 1
173		d = data.get (u, defaults)
174		sys.stdout.write ("%9s" % ("_(%s,%s)," % (short[0][d[0]], short[1][d[1]])))
175
176	total += end - start + 1
177	used += num
178	if block:
179		last_block = block
180
181uu = data.keys ()
182uu.sort ()
183
184last = -100000
185num = 0
186offset = 0
187starts = []
188ends = []
189print "static const INDIC_TABLE_ELEMENT_TYPE indic_table[] = {"
190for u in uu:
191	if u <= last:
192		continue
193	block = data[u][2]
194
195	start = u//8*8
196	end = start+1
197	while end in uu and block == data[end][2]:
198		end += 1
199	end = (end-1)//8*8 + 7
200
201	if start != last + 1:
202		if start - last <= 1+16*3:
203			print_block (None, last+1, start-1, data)
204			last = start-1
205		else:
206			if last >= 0:
207				ends.append (last + 1)
208				offset += ends[-1] - starts[-1]
209			print
210			print
211			print "#define indic_offset_0x%04xu %d" % (start, offset)
212			starts.append (start)
213
214	print_block (block, start, end, data)
215	last = end
216ends.append (last + 1)
217offset += ends[-1] - starts[-1]
218print
219print
220occupancy = used * 100. / total
221page_bits = 12
222print "}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy)
223print
224print "INDIC_TABLE_ELEMENT_TYPE"
225print "hb_indic_get_categories (hb_codepoint_t u)"
226print "{"
227print "  switch (u >> %d)" % page_bits
228print "  {"
229pages = set([u>>page_bits for u in starts+ends+singles.keys()])
230for p in sorted(pages):
231	print "    case 0x%0Xu:" % p
232	for (start,end) in zip (starts, ends):
233		if p not in [start>>page_bits, end>>page_bits]: continue
234		offset = "indic_offset_0x%04xu" % start
235		print "      if (hb_in_range (u, 0x%04Xu, 0x%04Xu)) return indic_table[u - 0x%04Xu + %s];" % (start, end-1, start, offset)
236	for u,d in singles.items ():
237		if p != u>>page_bits: continue
238		print "      if (unlikely (u == 0x%04Xu)) return _(%s,%s);" % (u, short[0][d[0]], short[1][d[1]])
239	print "      break;"
240	print ""
241print "    default:"
242print "      break;"
243print "  }"
244print "  return _(x,x);"
245print "}"
246print
247print "#undef _"
248for i in range (2):
249	print
250	vv = values[i].keys ()
251	vv.sort ()
252	for v in vv:
253		print "#undef %s_%s" % \
254			(what_short[i], short[i][v])
255print
256print "/* == End of generated table == */"
257
258# Maintain at least 30% occupancy in the table */
259if occupancy < 30:
260	raise Exception ("Table too sparse, please investigate: ", occupancy)
261