f4fdb8379b3213ff9aa30a0a6c7c7b79303d72d9
[framework/uifw/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 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         "Vowel_Dependent":      'M',
85         "Other":                'x',
86 },{
87         "Not_Applicable":       'x',
88 }]
89 all_shorts = [[],[]]
90
91 # Add some of the values, to make them more readable, and to avoid duplicates
92
93
94 for i in range (2):
95         for v,s in short[i].items ():
96                 all_shorts[i].append (s)
97
98 what = ["INDIC_SYLLABIC_CATEGORY", "INDIC_MATRA_CATEGORY"]
99 what_short = ["ISC", "IMC"]
100 for i in range (2):
101         print
102         vv = values[i].keys ()
103         vv.sort ()
104         for v in vv:
105                 v_no_and = v.replace ('_And_', '_')
106                 if v in short[i]:
107                         s = short[i][v]
108                 else:
109                         s = ''.join ([c for c in v_no_and if ord ('A') <= ord (c) <= ord ('Z')])
110                         if s in all_shorts[i]:
111                                 raise Exception ("Duplicate short value alias", v, s)
112                         all_shorts[i].append (s)
113                         short[i][v] = s
114                 print "#define %s_%s    %s_%s   %s/* %3d chars; %s */" % \
115                         (what_short[i], s, what[i], v.upper (), \
116                         '       '* ((48-1 - len (what[i]) - 1 - len (v)) / 8), \
117                         values[i][v], v)
118 print
119 print "#define _(S,M) INDIC_COMBINE_CATEGORIES (ISC_##S, IMC_##M)"
120 print
121 print
122
123 def print_block (block, start, end, data):
124         print
125         print
126         print "  /* %s  (%04X..%04X) */" % (block, start, end)
127         num = 0
128         for u in range (start, end+1):
129                 if u % 8 == 0:
130                         print
131                         print "  /* %04X */" % u,
132                 if u in data:
133                         num += 1
134                 d = data.get (u, defaults)
135                 sys.stdout.write ("%9s" % ("_(%s,%s)," % (short[0][d[0]], short[1][d[1]])))
136
137         if num == 0:
138                 # Filler block, don't check occupancy
139                 return
140         total = end - start + 1
141         occupancy = num * 100. / total
142         # Maintain at least 30% occupancy in the table */
143         if occupancy < 30:
144                 raise Exception ("Table too sparse, please investigate: ", occupancy, block)
145
146 uu = data.keys ()
147 uu.sort ()
148
149 last = -1
150 num = 0
151 offset = 0
152 starts = []
153 ends = []
154 print "static const INDIC_TABLE_ELEMENT_TYPE indic_table[] = {"
155 for u in uu:
156         if u <= last:
157                 continue
158         block = data[u][2]
159         (start, end) = blocks[block]
160
161         if start != last + 1:
162                 if start - last <= 33:
163                         print_block ("FILLER", last+1, start-1, data)
164                         last = start-1
165                 else:
166                         if last >= 0:
167                                 ends.append (last + 1)
168                                 offset += ends[-1] - starts[-1]
169                         print
170                         print
171                         print "#define indic_offset_0x%04x %d" % (start, offset)
172                         starts.append (start)
173
174         print_block (block, start, end, data)
175         last = end
176 ends.append (last + 1)
177 offset += ends[-1] - starts[-1]
178 print
179 print
180 print "#define indic_offset_total %d" % offset
181 print
182 print "};"
183
184 print
185 print "static INDIC_TABLE_ELEMENT_TYPE"
186 print "get_indic_categories (hb_codepoint_t u)"
187 print "{"
188 for (start,end) in zip (starts, ends):
189         offset = "indic_offset_0x%04x" % start
190         print "  if (0x%04X <= u && u <= 0x%04X) return indic_table[u - 0x%04X + %s];" % (start, end, start, offset)
191 for u,d in singles.items ():
192         print "  if (unlikely (u == 0x%04X)) return _(%s,%s);" % (u, short[0][d[0]], short[1][d[1]])
193 print "  return _(x,x);"
194 print "}"
195
196 print
197 print "#undef _"
198 for i in range (2):
199         print
200         vv = values[i].keys ()
201         vv.sort ()
202         for v in vv:
203                 print "#undef %s_%s" % \
204                         (what_short[i], short[i][v])
205
206 print
207 print
208 print "/* == End of generated table == */"