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