initial import for sdlhaa & sdlhim
[sdlhildon] / sdlhim / src / unidata / builder.py
1 #!/usr/bin/python
2 import re
3
4 # Composite codes we are interested in (HIM cannot use any other)
5 interested = [ 0x0300, 0x0301, 0x0302, 0x0303, 0x0304, 0x032e, 0x0307, 0x0308,
6         0x030a, 0x030b, 0x030c, 0x0327, 0x0328, 0x0323, 0x0309, 0x031b]
7 interested.sort()
8
9 # Auxiliar function for parsing the decomposition segment in UnicodeData
10 hex_p = re.compile(r"\s+")
11 def parse_decomp(str):
12         m = hex_p.split(str)
13         f = filter(lambda x: x[0] != '<', m)
14         return [int(x, 16) for x in f]
15
16 # First read all composition exclusions
17 p = re.compile(r"(?P<value>[0-9A-F]+)")
18 f = open('CompositionExclusions.txt', 'r')
19 exclusions = set()
20
21 for line in f:
22         m = p.match(line)
23         if m is None:
24                 continue
25         
26         value = int(m.group('value'), 16)
27         
28         exclusions.add(value)
29
30 f.close()
31
32 # Now read decomposition data
33 p = re.compile(r"(?P<value>[0-9A-F]+);(?P<name>[^;]*);[^;]*;(?P<canonical_class>\d+);[^;]*;(?P<decomp>[^;]*);")
34 f = open('UnicodeData.txt', 'r')
35 composite = {}
36
37 for line in f:
38         m = p.match(line)
39         if m is None:
40                 print "#warning invalid line:", line
41                 continue
42         
43         value = int(m.group('value'), 16)
44         name = m.group('name')
45         segment = m.group('decomp')
46         if len(segment) > 0:    
47                 compat = segment[0] == '<'
48                 if not compat and value not in exclusions:
49                         decomp = parse_decomp(segment)
50                         first = 0
51                         second = decomp[0]
52                         if len(decomp) > 1:
53                                 first = second
54                                 second = decomp[1]
55                         
56                         if second not in composite:
57                                 composite[second] = {first: value}
58                         else:
59                                 composite[second][first] = value
60
61 # Nothing left to be collected.  Let there be light!
62 print """
63 /* This file is autogenerated by builder.py from UnicodeData.txt */
64 /* Do not edit; instead edit builder.py and regenerate. */
65
66 struct item {
67         unsigned short first;
68         unsigned short result;
69 };
70
71 struct table {
72         unsigned short second;
73         unsigned short data_size;
74         const struct item * data;
75 };
76
77 """
78
79 for second in interested:
80         if second not in composite:
81                 print "#warning data for composite 0x%04x not found" % second
82                 continue
83         
84         print "static const struct item values_for_%04x[] = {" % second
85         
86         items = composite[second].items()
87         items.sort()
88         for first, value in items:
89                 print "\t{0x%x, 0x%x}," % (first, value)
90         
91         print "};"
92         print ""
93
94 print ""
95
96 print "static const struct table composite_table[] = {"
97 for second in interested:
98         if second not in composite:
99                 continue
100         
101         data_size = len(composite[second])
102         print "\t{0x%x, %u, values_for_%04x}, " % (second, data_size, second)
103 print "};"
104
105 f.close()