3016cd07b6808e50e5ba376fb36fae6109f945d8
[platform/upstream/harfbuzz.git] / src / gen-indic-table.py
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 IndicPositionalCategory.txt Blocks.txt"
7         sys.exit (1)
8
9 ALLOWED_SINGLES = [0x00A0, 0x25CC]
10 ALLOWED_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
33 files = [file (x) for x in sys.argv[1:]]
34
35 headers = [[f.readline () for i in range (2)] for f in files]
36
37 data = [{} for f in files]
38 values = [{} for f in files]
39 for 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:
64 defaults = ('Other', 'Not_Applicable', 'No_Block')
65 for i,v in enumerate (defaults):
66         values[i][v] = values[i].get (v, 0) + 1
67 combined = {}
68 for 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
75 combined = {k:v for k,v in combined.items() if k in ALLOWED_SINGLES or v[2] in ALLOWED_BLOCKS}
76 data = combined
77 del combined
78 num = len (data)
79
80 for 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
85 singles = {}
86 for u in ALLOWED_SINGLES:
87         singles[u] = data[u]
88         del data[u]
89
90 print "/* == Start of generated table == */"
91 print "/*"
92 print " * The following table is generated by running:"
93 print " *"
94 print " *   ./gen-indic-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt Blocks.txt"
95 print " *"
96 print " * on files with these headers:"
97 print " *"
98 for h in headers:
99         for l in h:
100                 print " * %s" % (l.strip())
101 print " */"
102 print
103 print '#include "hb-ot-shape-complex-indic-private.hh"'
104 print
105
106 # Shorten values
107 short = [{
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 }]
121 all_shorts = [{},{}]
122
123 # Add some of the values, to make them more readable, and to avoid duplicates
124
125
126 for i in range (2):
127         for v,s in short[i].items ():
128                 all_shorts[i][s] = v
129
130 what = ["INDIC_SYLLABIC_CATEGORY", "INDIC_MATRA_CATEGORY"]
131 what_short = ["ISC", "IMC"]
132 for 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)
150 print
151 print "#define _(S,M) INDIC_COMBINE_CATEGORIES (ISC_##S, IMC_##M)"
152 print
153 print
154
155 total = 0
156 used = 0
157 last_block = None
158 def 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
181 uu = data.keys ()
182 uu.sort ()
183
184 last = -100000
185 num = 0
186 offset = 0
187 starts = []
188 ends = []
189 print "static const INDIC_TABLE_ELEMENT_TYPE indic_table[] = {"
190 for 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
216 ends.append (last + 1)
217 offset += ends[-1] - starts[-1]
218 print
219 print
220 occupancy = used * 100. / total
221 page_bits = 12
222 print "}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy)
223 print
224 print "INDIC_TABLE_ELEMENT_TYPE"
225 print "hb_indic_get_categories (hb_codepoint_t u)"
226 print "{"
227 print "  switch (u >> %d)" % page_bits
228 print "  {"
229 pages = set([u>>page_bits for u in starts+ends+singles.keys()])
230 for 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 ""
241 print "    default:"
242 print "      break;"
243 print "  }"
244 print "  return _(x,x);"
245 print "}"
246 print
247 print "#undef _"
248 for 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])
255 print
256 print "/* == End of generated table == */"
257
258 # Maintain at least 30% occupancy in the table */
259 if occupancy < 30:
260         raise Exception ("Table too sparse, please investigate: ", occupancy)