]> git.lyx.org Git - lyx.git/blob - development/tools/generate_symbols_list.py
ee9b3abbf027431b84994650761dcbd790f70f0b
[lyx.git] / development / tools / generate_symbols_list.py
1 #!/usr/bin/python
2 import sys,string,re,os
3
4 def is_prefix(a, b):
5     return a[:len(b)] == b
6
7 def get_code(code, font):
8     if code < 10:
9         return code+161
10     elif code < 32:
11         return code+163
12     else:
13         return code
14
15 font_names = {}
16 symbols = {}
17 xsymbols = {}
18
19 ignore_list = ["not", "braceld", "bracerd", "bracelu", "braceru",
20                "lmoustache", "rmoustache", "lgroup", "rgroup", "bracevert"]
21
22 def process(file):
23     fh = open(file)
24     lines = fh.readlines()
25     fh.close()
26
27     n = len(lines)
28     for i in xrange(n):
29         line = lines[i]
30         next_line = ""
31         if i+1 < n:
32             next_line = lines[i+1]
33
34         # some entries are spread over two lines so we join the next line 
35         # to the current one, (if current line contains a comment, we remove it)
36         line = string.split(line,'%')[0]+next_line
37
38         mo =  re.match(r'.*\\DeclareSymbolFont\s*\{(.*?)\}\s*\{(.*?)\}\s*\{(.*?)\}.*', line)
39         if mo != None:
40             font_names[mo.group(1)] = mo.group(3)
41
42         mo =  re.match(r'.*\\DeclareMath(Symbol|Delimiter)\s*\{?\\(\w*?)\}?\s*\{?\\(.*?)\}?\s*\{(.*?)\}\s*\{"(.*?)\}.*', line)
43         if mo != None:
44             symbol = mo.group(2)
45             type = mo.group(3)
46             font = mo.group(4)
47             code = mo.group(5)
48         else:
49             mo = re.match(r'.*\\edef\\(\w*?)\{.*?\{\\hexnumber@\\sym(.*?)\}(.*?)\}', line)
50             if mo != None:
51                 symbol = mo.group(1)
52                 type = "mathord"
53                 font = mo.group(2)
54                 code = mo.group(3)
55             
56         if mo != None and symbol not in ignore_list:
57             mo2 = re.match(r'\s*\\def\\(.*?)\{', next_line)
58             if mo2 != None and is_prefix(symbol,mo2.group(1)):
59                 sys.stderr.write("%s -> %s\n" % (symbol, mo2.group(1)))
60                 symbol = mo2.group(1)
61
62             if font_names.has_key(font):
63                 font = font_names[font]
64
65             code = get_code(string.atoi(code, 16), font)
66             if code == 0:
67                 continue
68
69             xcode = 0
70             if xsymbols.has_key(symbol):
71                 xcode = xsymbols[symbol]
72                 del xsymbols[symbol]
73
74             if symbols.has_key(symbol):
75                 sys.stderr.write(symbol+ " exists\n")
76                 if code != symbols[symbol]:
77                     sys.stderr.write("code is not equal!!!\n")
78             else:
79                 symbols[symbol] = code
80                 print "%-18s %-4s %3d %3d %-6s" % (symbol,font,code,xcode,type)
81
82
83 path = os.path.split(sys.argv[0])[0]
84 fh = open(os.path.join(path, "x-font"))
85 lines = fh.readlines()
86 fh.close()
87 for line in lines:
88     x = string.split(line)
89     symbol = x[0]
90     code = string.atoi(x[1],16)
91     xsymbols[symbol] = code
92
93 for file in sys.argv[1:]:
94     print "# Generated from " + os.path.basename(file) + "\n"
95     process(file)
96     print
97
98 exceptions = [
99     ("neq", "none", 0, 185, "mathrel"),
100     ("textdegree", "none", 0, 176, "mathord"),
101     ("cong", "none", 0, 64, "mathrel"),
102     ("surd", "note", 0, 214, "mathord")
103 ]
104
105 if xsymbols.has_key("leq"):
106     sys.exit(0)
107
108 for x in exceptions:
109     print "%-18s %-4s %3d %3d %-6s" % x
110     if xsymbols.has_key(x[0]):
111         del xsymbols[x[0]]
112
113 for symbol in xsymbols.keys():
114     sys.stderr.write(symbol+"\n")