[Indic] Start an Indic shaper
[apps/core/preloaded/video-player.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 IndicMatraCategory.txt Blocks.txt"
7         sys.exit (1)
8
9 files = [file (sys.argv[i+1]) for i in range (3)]
10
11 headers = [[f.readline () for i in range (2)] for f in files]
12
13 blocks = {}
14 data = [{} for f in files]
15 values = [{} for f in files]
16 for i, f in enumerate (files):
17         for line in f:
18
19                 j = line.find ('#')
20                 if j >= 0:
21                         line = line[:j]
22                 
23                 fields = [x.strip () for x in line.split (';')]
24                 if len (fields) == 1:
25                         continue
26
27                 uu = fields[0].split ('..')
28                 start = int (uu[0], 16)
29                 if len (uu) == 1:
30                         end = start
31                 else:
32                         end = int (uu[1], 16)
33
34                 t = fields[1]
35
36                 for u in range (start, end + 1):
37                         data[i][u] = t
38                 values[i][t] = values[i].get (t, 0) + 1
39
40                 if i == 2:
41                         blocks[t] = (start, end)
42
43 # Merge data into one dict:
44 defaults = ('Other', 'Not_Applicable', 'No_Block')
45 for i,v in enumerate (defaults):
46         values[i][v] = values[i].get (v, 0) + 1
47 combined = {}
48 for i,d in enumerate (data):
49         for u,v in d.items ():
50                 if i == 2 and not u in combined:
51                         continue
52                 if not u in combined:
53                         combined[u] = list (defaults)
54                 combined[u][i] = v
55 data = combined
56 del combined
57 num = len (data)
58
59 # Move the outliers NO-BREAK SPACE and DOTTED CIRCLE out
60 singles = {}
61 for u in [0x00A0, 0x25CC]:
62         singles[u] = data[u]
63         del data[u]
64
65 print "/* == Start of generated table == */"
66 print "/*"
67 print " * The following table is generated by running:"
68 print " *"
69 print " *   ./gen-indic-table.py IndicSyllabicCategory.txt IndicMatraCategory.txt Blocks.txt"
70 print " *"
71 print " * on files with these headers:"
72 print " *"
73 for h in headers:
74         for l in h:
75                 print " * %s" % (l.strip())
76 print " */"
77
78 # Shorten values
79 print
80 short = [{
81         "Bindu":                'Bi',
82         "Visarga":              'Vs',
83         "Vowel":                'Vo',
84         "Other":                'X',
85 },{
86         "Not_Applicable":       'X',
87 }]
88 all_shorts = [[],[]]
89
90 # Add some of the values, to make them more readable, and to avoid duplicates
91
92
93 for i in range (2):
94         for v,s in short[i].items ():
95                 all_shorts[i].append (s)
96
97 what = ["INDIC_SYLLABIC_CATEGORY", "INDIC_MATRA_CATEGORY"]
98 what_short = ["ISC", "IMC"]
99 for i in range (2):
100         print
101         vv = values[i].keys ()
102         vv.sort ()
103         for v in vv:
104                 v_no_and = v.replace ('_And_', '_')
105                 if v in short[i]:
106                         s = short[i][v]
107                 else:
108                         s = ''.join ([c for c in v_no_and if ord ('A') <= ord (c) <= ord ('Z')])
109                         if s in all_shorts[i]:
110                                 raise Exception ("Duplicate short value alias", v, s)
111                         all_shorts[i].append (s)
112                         short[i][v] = s
113                 print "#define %s_%s    %s_%s   %s/* %3d chars; %s */" % \
114                         (what_short[i], s, what[i], v.upper (), \
115                         '       '* ((48-1 - len (what[i]) - 1 - len (v)) / 8), \
116                         values[i][v], v)
117 print
118 print "#define _(S,M) INDIC_COMBINE_CATEGORIES (ISC_##S, IMC_##M)"
119 print
120 print
121
122 def print_block (block, start, end, data):
123         print
124         print
125         print "  /* %s  (%04X..%04X) */" % (block, start, end)
126         num = 0
127         for u in range (start, end+1):
128                 if u % 8 == 0:
129                         print
130                         print "  /* %04X */" % u,
131                 if u in data:
132                         num += 1
133                 d = data.get (u, defaults)
134                 print "%10s" % ("_(%s,%s)," % (short[0][d[0]], short[1][d[1]])),
135
136         if num == 0:
137                 # Filler block, don't check occupancy
138                 return
139         total = end - start + 1
140         occupancy = num * 100. / total
141         # Maintain at least 30% occupancy in the table */
142         if occupancy < 30:
143                 raise Exception ("Table too sparse, please investigate: ", occupancy, block)
144
145 uu = data.keys ()
146 uu.sort ()
147
148 last = -1
149 num = 0
150 total = 0
151 tables = []
152 for u in uu:
153         if u <= last:
154                 continue
155         block = data[u][2]
156         (start, end) = blocks[block]
157
158         if start != last + 1:
159                 if start - last <= 33:
160                         print_block ("FILLER", last+1, start-1, data)
161                         last = start-1
162                 else:
163                         if last >= 0:
164                                 print
165                                 print "};"
166                                 print
167                         print "static const INDIC_TABLE_ELEMENT_TYPE indic_table_0x%04x[] =" % start
168                         print "{",
169                         tables.append (start)
170
171         print_block (block, start, end, data)
172         last = end
173 print
174 print "};"
175 print
176
177 print
178 print "static INDIC_TABLE_ELEMENT_TYPE"
179 print "get_indic_categories (hb_codepoint_t u)"
180 print "{"
181 for u in tables:
182         t = "indic_table_0x%04x" % u
183         print "  if (0x%04X <= u && u <= 0x%04X + ARRAY_LENGTH (%s)) return %s[u - 0x%04X];" % (u, u, t, t, u)
184 for u,d in singles.items ():
185         print "  if (unlikely (u == 0x%04X)) return _(%s,%s);" % (u, short[0][d[0]], short[1][d[1]])
186 print "  return _(X,X);"
187 print "}"
188
189 print
190 print "#undef _"
191 for i in range (2):
192         print
193         vv = values[i].keys ()
194         vv.sort ()
195         for v in vv:
196                 print "#undef %s_%s" % \
197                         (what_short[i], short[i][v])
198
199 print
200 print
201 print "/* == End of generated table == */"