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