]> git.lyx.org Git - lyx.git/blob - development/tools/generate_symbols_list.py
Check that we have bash before running updatelfuns.sh
[lyx.git] / development / tools / generate_symbols_list.py
1 #!/usr/bin/python
2 import sys,string,re,os,os.path
3
4 def get_code(code, font):
5     if font != "dontknowwhichfontusesthisstrangeencoding":
6         return code
7     if code < 10:
8         return code+161
9     elif code < 32:
10         return code+163
11     else:
12         return code
13
14 font_names = {}
15 symbols = {}
16 xsymbols = {}
17
18 ignore_list = ["not", "braceld", "bracerd", "bracelu", "braceru",
19                "lmoustache", "rmoustache", "lgroup", "rgroup", "bracevert"]
20
21 def process(file):
22     fh = open(file)
23     lines = fh.readlines()
24     fh.close()
25     package, ext = os.path.splitext(os.path.basename(file))
26     if ext != ".sty":
27         package = ''
28     mdsymbolcode = 0
29
30     n = len(lines)
31     for i in xrange(n):
32         line = lines[i]
33         mo =  re.match(r'\s*%.*', line)
34         if mo != None:
35             continue
36         next_line = ""
37         if i+1 < n:
38             next_line = lines[i+1]
39
40         # some entries are spread over two lines so we join the next line
41         # to the current one, (if current line contains a comment, we remove it)
42         line = string.split(line,'%')[0]+next_line
43
44         mo =  re.match(r'.*\\DeclareSymbolFont\s*\{(.*?)\}\s*\{(.*?)\}\s*\{(.*?)\}.*', line)
45         if mo != None:
46             font_names[mo.group(1)] = mo.group(3)
47
48         mo =  re.match(r'^\s*\\mdsy\@DeclareSymbolFont\s*\{(.*?)\}\s*\{(.*?)\}\s*\{(.*?)\}.*', line)
49         if mo != None:
50             font_names[mo.group(1)] = mo.group(3)
51
52         # \mdsy@setslot resets the counter for \mdsy@DeclareSymbol
53         mo =  re.match(r'^\s*\\mdsy\@setslot\s*\{(.*?)\}.*', line)
54         if mo != None:
55             mdsymbolcode = int(mo.group(1))
56
57         # \mdsy@nextslot increments the counter for \mdsy@DeclareSymbol
58         mo =  re.match(r'^\s*\\mdsy\@nextslot.*', line)
59         if mo != None:
60             mdsymbolcode = mdsymbolcode + 1
61
62         mo =  re.match(r'.*\\(\\mdsy\@)?DeclareMath(Symbol|Delimiter)\s*\{?\\(\w*?)\}?\s*\{?\\(.*?)\}?\s*\{(.*?)\}\s*\{([\'"]?)(.*?)\}.*', line)
63         code = -1
64         try:
65             if mo != None:
66                 symbol = mo.group(3)
67                 type = mo.group(4)
68                 font = mo.group(5)
69                 if mo.group(6) == '':
70                     code = int(mo.group(7))
71                 elif mo.group(6) == '"':
72                     code = int(mo.group(7), 16)
73                 else:
74                     code = int(mo.group(7), 8)
75             else:
76                 mo = re.match(r'.*\\edef\\(\w*?)\{.*?\{\\hexnumber@\\sym(.*?)\}(.*?)\}', line)
77                 if mo != None:
78                     symbol = mo.group(1)
79                     type = "mathord"
80                     font = mo.group(2)
81                     code = int(mo.group(3), 16)
82         except ValueError:
83                 code = -1
84
85         if mo == None:
86             mo =  re.match(r'^\s*\\mdsy\@DeclareSymbol\s*\{(.*?)\}\s*\{(.*?)\}\s*\{\\(.*?)\}.*', line)
87             if mo != None:
88                 symbol = mo.group(1)
89                 type = mo.group(3)
90                 font = mo.group(2)
91                 code = mdsymbolcode
92                 mdsymbolcode = mdsymbolcode + 1
93
94         if mo == None:
95             mo =  re.match(r'^\s*\\mdsy\@DeclareAlias\s*\{(.*?)\}\s*\{(.*?)\}\s*\{\\(.*?)\}.*', line)
96             if mo != None:
97                 symbol = mo.group(1)
98                 type = mo.group(3)
99                 font = mo.group(2)
100                 code = mdsymbolcode - 1
101
102         if mo != None and symbol not in ignore_list:
103             mo2 = re.match(r'\s*\\def\\(.*?)\{', next_line)
104             if mo2 != None and symbol == mo2.group(1)+"op":
105                 sys.stderr.write("%s -> %s\n" % (symbol, mo2.group(1)))
106                 symbol = mo2.group(1)
107
108             if font_names.has_key(font):
109                 font = font_names[font]
110
111             code = get_code(code, font)
112             if code < 0:
113                 continue
114
115             xcode = 0
116             if xsymbols.has_key(symbol):
117                 xcode = xsymbols[symbol]
118                 del xsymbols[symbol]
119
120             if symbols.has_key(symbol):
121                 sys.stderr.write(symbol+ " exists\n")
122                 if code != symbols[symbol]:
123                     sys.stderr.write("code is not equal!!!\n")
124             else:
125                 symbols[symbol] = code
126                 if package == '':
127                     print "%-18s %-4s %3d %3d %-6s" % (symbol,font,code,xcode,type)
128                 else:
129                     print "%-18s %-4s %3d %3d %-9s x  %s" % (symbol,font,code,xcode,type,package)
130
131
132 path = os.path.split(sys.argv[0])[0]
133 fh = open(os.path.join(path, "x-font"))
134 lines = fh.readlines()
135 fh.close()
136 for line in lines:
137     x = string.split(line)
138     symbol = x[0]
139     code = string.atoi(x[1],16)
140     xsymbols[symbol] = code
141
142 for file in sys.argv[1:]:
143     print "# Generated from " + os.path.basename(file) + "\n"
144     process(file)
145     print
146
147 exceptions = [
148     ("neq", "x", 0, 185, "mathrel"),
149     ("textdegree", "x", 0, 176, "mathord"),
150     ("cong", "x", 0, 64, "mathrel"),
151     ("surd", "x", 0, 214, "mathord")
152 ]
153
154 if xsymbols.has_key("leq"):
155     sys.exit(0)
156
157 for x in exceptions:
158     print "%-18s %-4s %3d %3d %-6s" % x
159     if xsymbols.has_key(x[0]):
160         del xsymbols[x[0]]
161
162 print """
163 lyxbar             cmsy 161   0 mathord
164 lyxeq              cmr   61   0 mathord
165 lyxdabar           msa   57   0 mathord
166 lyxright           msa   75   0 mathord
167 lyxleft            msa   76   0 mathord
168 """
169
170 for symbol in xsymbols.keys():
171     sys.stderr.write(symbol+"\n")