]> git.lyx.org Git - lyx.git/blob - src/mathed/math_hash.C
whichFont down to 5.3%
[lyx.git] / src / mathed / math_hash.C
1 #include <config.h>
2
3 #include "math_parser.h"
4 #include "math_metricsinfo.h"
5 #include "lyxlex.h"
6 #include "debug.h"
7 #include "support/filetools.h" // LibFileSearch
8
9 #include <map>
10
11
12 namespace {
13
14 // global
15 std::map<string, latexkeys> theWordList;
16
17
18 struct key_type {
19         ///
20         char const * name;
21         ///
22         MathTokenEnum token;
23         ///
24         unsigned int id;
25 };
26
27
28 key_type wordlist_array[] = 
29 {
30         {"!",  LM_TK_SPACE, 0},
31         {"#",  LM_TK_SPECIAL, '#'},
32         {"$",  LM_TK_SPECIAL, '$'},
33         {"%",  LM_TK_SPECIAL, '%'},
34         {"&",  LM_TK_SPECIAL, '&'},
35         {"(",  LM_TK_BEGIN, LM_OT_SIMPLE},
36         {")",  LM_TK_END, LM_OT_SIMPLE},
37         {",",  LM_TK_SPACE, 1},
38         {".",  LM_TK_SPECIAL, '.'},
39         {":",  LM_TK_SPACE, 2},
40         {";",  LM_TK_SPACE, 3},
41         {"Pr",  LM_TK_FUNCLIM, 0},
42         {"[",  LM_TK_BEGIN, LM_OT_EQUATION},
43         {"]",  LM_TK_END, LM_OT_EQUATION},
44         {"_",  LM_TK_SPECIAL, '_'},
45         {"acute",  LM_TK_DECORATION, 0},
46         {"arccos",  LM_TK_FUNC, 0},
47         {"arcsin",  LM_TK_FUNC, 0},
48         {"arctan",  LM_TK_FUNC, 0},
49         {"arg",  LM_TK_FUNC, 0},
50         {"atop",  LM_TK_ATOP, 0},
51         {"bar",  LM_TK_DECORATION, 0},
52         {"begin",  LM_TK_BEGIN, 0},
53         {"bf",  LM_TK_OLDFONT, LM_TC_BF},
54         {"binom",  LM_TK_BINOM, 0},
55         {"bmod",  LM_TK_FUNC, 0},
56         {"breve",  LM_TK_DECORATION, 0},
57         {"cal",  LM_TK_OLDFONT, LM_TC_CAL},
58         {"cdots",  LM_TK_DOTS, 0},
59         {"check",  LM_TK_DECORATION, 0},
60         {"choose",  LM_TK_CHOOSE, 0},
61         {"cos",  LM_TK_FUNC, 0},
62         {"cosh",  LM_TK_FUNC, 0},
63         {"cot",  LM_TK_FUNC, 0},
64         {"coth",  LM_TK_FUNC, 0},
65         {"csc",  LM_TK_FUNC, 0},
66         {"ddot",  LM_TK_DECORATION, 0},
67         {"dddot",  LM_TK_DECORATION, 0},
68         {"ddots",  LM_TK_DOTS, 0},
69         {"deg",  LM_TK_FUNC, 0},
70         {"det",  LM_TK_FUNCLIM, 0},
71         {"dim",  LM_TK_FUNC, 0},
72         {"displaystyle",  LM_TK_STY, LM_ST_DISPLAY},
73         {"dot",  LM_TK_DECORATION, 0},
74         {"end",  LM_TK_END, 0},
75         {"exp",  LM_TK_FUNC, 0},
76         {"frac",  LM_TK_FRAC, 0},
77         {"frak",  LM_TK_FONT, LM_TC_EUFRAK},
78         {"gcd",  LM_TK_FUNCLIM, 0},
79         {"grave",  LM_TK_DECORATION, 0},
80         {"hat",  LM_TK_DECORATION, 0},
81         {"hom",  LM_TK_FUNC, 0},
82         {"inf",  LM_TK_FUNCLIM, 0},
83         {"it",  LM_TK_OLDFONT, LM_TC_IT},
84         {"ker",  LM_TK_FUNC, 0},
85         {"kern",  LM_TK_KERN, 0},
86         {"label",  LM_TK_LABEL, 0},
87         {"lefteqn",  LM_TK_LEFTEQN, 1},
88         {"ldots",  LM_TK_DOTS, 0},
89         {"left",  LM_TK_LEFT, 0},
90         {"lg",  LM_TK_FUNC, 0},
91         {"lim",  LM_TK_FUNCLIM, 0},
92         {"liminf",  LM_TK_FUNCLIM, 0},
93         {"limits",  LM_TK_LIMIT, 1 },
94         {"limsup",  LM_TK_FUNCLIM, 0},
95         {"ln",  LM_TK_FUNC, 0},
96         {"log",  LM_TK_FUNC, 0},
97         {"lyxbox",  LM_TK_BOX, 0},
98         {"mathbb",  LM_TK_FONT, LM_TC_BB},
99         {"mathbf",  LM_TK_FONT, LM_TC_BF},
100         {"mathcal",  LM_TK_FONT, LM_TC_CAL},
101         {"mathfrak",  LM_TK_FONT, LM_TC_EUFRAK},
102         {"mathit",  LM_TK_FONT, LM_TC_IT},
103         {"mathnormal",  LM_TK_FONT, LM_TC_VAR},
104         {"mathrm",  LM_TK_FONT, LM_TC_RM},
105         {"mathsf",  LM_TK_FONT, LM_TC_SF},
106         {"mathtt",  LM_TK_FONT, LM_TC_TT},
107         {"max",  LM_TK_FUNCLIM, 0},
108         //{"mbox",  LM_TK_BOX, 0},
109         {"min",  LM_TK_FUNCLIM, 0},
110         {"newcommand",  LM_TK_NEWCOMMAND, 0 },
111         {"nolimits",  LM_TK_LIMIT, static_cast<unsigned>(-1)},
112         {"nonumber",  LM_TK_NONUM, 0},
113         {"not",  LM_TK_NOT, 0},
114         {"over",  LM_TK_OVER, 0},
115         {"overbrace",  LM_TK_DECORATION, 0},
116         {"overleftarrow",  LM_TK_DECORATION, 0},
117         {"overline",  LM_TK_DECORATION, 0},
118         {"overrightarrow",  LM_TK_DECORATION, 0},
119         {"overleftrightarrow", LM_TK_DECORATION, 0},
120         {"protect",  LM_TK_PROTECT, 0},
121         {"qquad",  LM_TK_SPACE, 5},
122         {"quad",  LM_TK_SPACE, 4},
123         {"right",  LM_TK_RIGHT, 0},
124         {"rm",  LM_TK_OLDFONT, LM_TC_RM},
125         {"root",  LM_TK_ROOT, 0},
126         {"scriptscriptstyle",  LM_TK_STY, LM_ST_SCRIPTSCRIPT},
127         {"scriptstyle",  LM_TK_STY, LM_ST_SCRIPT},
128         {"sec",  LM_TK_FUNC, 0},
129         {"sin",  LM_TK_FUNC, 0},
130         {"sinh",  LM_TK_FUNC, 0},
131         {"sqrt",  LM_TK_SQRT, 0},
132         {"stackrel",  LM_TK_STACK, 0},
133         {"sup",  LM_TK_FUNCLIM, 0},
134         {"tan",  LM_TK_FUNC, 0},
135         {"tanh",  LM_TK_FUNC, 0},
136         {"textrm",  LM_TK_FONT, LM_TC_TEXTRM},
137         {"textstyle",  LM_TK_STY, LM_ST_TEXT},
138         {"tilde",  LM_TK_DECORATION, 0},
139         {"tt",  LM_TK_OLDFONT, LM_TC_TT},
140         {"underbar",  LM_TK_DECORATION, 0},
141         {"underbrace",  LM_TK_DECORATION, 0},
142         {"underleftarrow", LM_TK_DECORATION, 0},
143         {"underline",  LM_TK_DECORATION, 0},
144         {"underrightarrow", LM_TK_DECORATION, 0},
145         {"underleftrightarrow", LM_TK_DECORATION, 0},
146         {"underset",  LM_TK_UNDERSET, 0},
147         {"vdots",  LM_TK_DOTS, 0},
148         {"vec",  LM_TK_DECORATION, 0},
149         {"widehat",  LM_TK_DECORATION, 0},
150         {"widetilde",  LM_TK_DECORATION, 0},
151         {"{",  LM_TK_SPECIAL, '{'},
152         {"}",  LM_TK_SPECIAL, '}'}
153 };
154
155
156
157 MathTokenEnum tokenEnum(const string & font)
158 {
159         if (font == "cmr")
160                 return LM_TK_CMR;
161         if (font == "cmsy")
162                 return LM_TK_CMSY;
163         if (font == "cmm")
164                 return LM_TK_CMM;
165         if (font == "cmex")
166                 return LM_TK_CMEX;
167         if (font == "msa")
168                 return LM_TK_MSA;
169         if (font == "msb")
170                 return LM_TK_MSB;
171         return LM_TK_SYM;
172 }
173
174
175 void readSymbols(string const & filename)
176 {
177         LyXLex lex(0, 0);
178         lex.setFile(filename);
179         while (lex.isOK() && lex.next()) {
180                 latexkeys tmp;
181                 tmp.name = lex.getString();
182                 if (lex.next())
183                         tmp.token = tokenEnum(lex.getString());
184                 if (lex.next())
185                         tmp.latex_font_id = lex.getInteger();
186                 if (lex.next())
187                         tmp.id = lex.getInteger();
188                 if (lex.next())
189                         tmp.type = lex.getString();
190                 if (lex.next())
191                         tmp.xmlname = lex.getString();
192                 if (theWordList.find(tmp.name) != theWordList.end())
193                         lyxerr << "readSymbols: token " << tmp.name
194                                << " already exists.\n";
195                 else
196                         theWordList[tmp.name] = tmp;
197         }
198 }
199
200 void initSymbols()
201 {
202         unsigned const n = sizeof(wordlist_array) / sizeof(wordlist_array[0]);
203         for (key_type * p = wordlist_array; p != wordlist_array + n; ++p) {
204                 latexkeys tmp;
205                 tmp.name          = p->name;
206                 tmp.token         = p->token;
207                 tmp.id            = p->id;
208                 tmp.latex_font_id = 0;
209                 theWordList[p->name] = tmp;
210         }
211
212         lyxerr[Debug::MATHED] << "reading symbols file\n";
213         string const file = LibFileSearch(string(), "symbols");
214         if (file.empty())
215                 lyxerr << "Could not find symbols file\n";
216         else
217                 readSymbols(file);
218 }
219
220
221 } // namespace anon
222
223
224 latexkeys const * in_word_set(string const & str)
225 {
226         static bool initialized = false;
227
228         if (!initialized) {
229                 initSymbols();
230                 initialized = true;
231         }
232
233         std::map<string, latexkeys>::iterator it = theWordList.find(str);
234         return (it != theWordList.end()) ? &(it->second) : 0;
235 }