]> git.lyx.org Git - lyx.git/blob - src/mathed/math_factory.C
Fix stupid parser bug
[lyx.git] / src / mathed / math_factory.C
1 #include <config.h>
2
3 #include "math_parser.h"
4 #include "math_arrayinset.h"
5 #include "math_amsarrayinset.h"
6 #include "math_binominset.h"
7 #include "math_boxinset.h"
8 #include "math_casesinset.h"
9 #include "math_decorationinset.h"
10 #include "math_dotsinset.h"
11 #include "math_fboxinset.h"
12 #include "math_fontinset.h"
13 #include "math_fracinset.h"
14 #include "math_kerninset.h"
15 #include "math_lefteqninset.h"
16 #include "math_macro.h"
17 #include "math_macrotable.h"
18 #include "math_macroarg.h"
19 #include "math_notinset.h"
20 #include "math_rootinset.h"
21 #include "math_sizeinset.h"
22 #include "math_spaceinset.h"
23 #include "math_splitinset.h"
24 #include "math_sqrtinset.h"
25 #include "math_stackrelinset.h"
26 #include "math_substackinset.h"
27 #include "math_symbolinset.h"
28 #include "math_undersetinset.h"
29 #include "math_unknowninset.h"
30 #include "math_xarrowinset.h"
31 #include "math_xymatrixinset.h"
32 #include "math_xyarrowinset.h"
33
34 #include "ref_inset.h"
35
36 #include "math_metricsinfo.h"
37 #include "debug.h"
38 #include "math_support.h"
39 #include "Lsstream.h"
40 #include "support/filetools.h" // LibFileSearch
41 #include "frontends/font_loader.h"
42
43 #include <map>
44 #include <fstream>
45
46
47 namespace {
48
49 // file scope
50 typedef std::map<string, latexkeys> WordList;
51 WordList theWordList;
52
53
54 struct key_type {
55         ///
56         string name;
57         ///
58         string inset;
59         ///
60         string extra;
61 };
62
63
64 key_type wordlist_array[] =
65 {
66         {"!",  "space", ""},
67         {",",  "space", ""},
68         {":",  "space", ""},
69         {";",  "space", ""},
70         {"Vmatrix",  "matrix", ""},
71         {"acute",  "decoration", ""},
72         {"bar",  "decoration", ""},
73         {"begin",  "begin", ""},
74         {"bf",  "oldfont", ""},
75         {"bmatrix",  "matrix", ""},
76         {"acute",  "decoration", ""},
77         {"breve",  "decoration", ""},
78         {"cal",  "oldfont", ""},
79         {"cdots",  "dots", ""},
80         {"check",  "decoration", ""},
81         {"ddot",  "decoration", ""},
82         {"dddot",  "decoration", ""},
83         {"ddots",  "dots", ""},
84         {"displaystyle",  "style", ""},
85         {"dot",  "decoration", ""},
86         {"dotsb",  "dots", ""},
87         {"dotsc",  "dots", ""},
88         {"dotsi",  "dots", ""},
89         {"dotsm",  "dots", ""},
90         {"dotso",  "dots", ""},
91         {"end",  "end", ""},
92         {"fbox",  "fbox", ""},
93         {"frak",  "font", ""},
94         {"grave",  "decoration", ""},
95         {"hat",  "decoration", ""},
96         {"it",  "oldfont", ""},
97         {"label",  "label", ""},
98         {"ldots",  "dots", ""},
99         {"left",  "left", ""},
100         {"limits",  "limit", ""},
101         {"lyxbox",  "box", ""},
102         {"lyxnegspace",  "space", ""},
103         {"lyxposspace",  "space", ""},
104         {"mathbb",  "font", ""},
105         {"mathbf",  "font", ""},
106         {"mathcal",  "font", ""},
107         {"mathfrak",  "font", ""},
108         {"mathit",  "font", ""},
109         {"mathnormal",  "font", ""},
110         {"mathring",  "decoration", ""},
111         {"mathrm",  "font", ""},
112         {"mathsf",  "font", ""},
113         {"mathtt",  "font", ""},
114         {"matrix",  "matrix", ""},
115         {"mbox",  "box", ""},
116         {"newcommand",  "newcommand", ""},
117         {"nolimits",  "limit", ""},
118         {"nonumber",  "nonum", ""},
119         {"overbrace",  "decoration", ""},
120         {"overleftarrow",  "decoration", ""},
121         {"overline",  "decoration", ""},
122         {"overrightarrow",  "decoration", ""},
123         {"overleftrightarrow", "decoration", ""},
124         {"pmatrix",  "matrix", ""},
125         {"protect",  "protect", ""},
126         {"qquad",  "space", ""},
127         {"quad",  "space", ""},
128         {"right",  "right", ""},
129         {"rm",  "oldfont", ""},
130         {"scriptscriptstyle",  "style", ""},
131         {"scriptstyle",  "style", ""},
132         {"textbf",  "font", "mathtext"},
133         {"textit",  "font", "mathtext"},
134         {"textmd",  "font", "mathtext"},
135         {"textrm",  "font", "mathtext"},
136         {"textsl",  "font", "mathtext"},
137         {"textup",  "font", "mathtext"},
138         {"textstyle",  "style", ""},
139         {"tilde",  "decoration", ""},
140         {"tt",  "oldfont", ""},
141         {"underbar",  "decoration", ""},
142         {"underbrace",  "decoration", ""},
143         {"underleftarrow", "decoration", ""},
144         {"underline",  "decoration", ""},
145         {"underrightarrow", "decoration", ""},
146         {"underleftrightarrow", "decoration", ""},
147         {"underset",  "underset", ""},
148         {"vdots",  "dots", ""},
149         {"vec",  "decoration", ""},
150         {"vmatrix",  "matrix", ""},
151         {"widehat",  "decoration", ""},
152         {"widetilde",  "decoration", ""}
153 };
154
155
156 bool math_font_available(string & name)
157 {
158         LyXFont f;
159         augmentFont(f, name);
160
161         // Do we have the font proper?
162         if (fontloader.available(f))
163                 return true;
164
165         // can we fake it?
166         if (name == "eufrak") {
167                 name = "lyxfakefrak";
168                 return true;
169         }
170
171         lyxerr[Debug::MATHED] << "font " << name << " not available and I can't fake it\n";
172         return false;
173 }
174
175
176 void readSymbols(string const & filename)
177 {
178         lyxerr[Debug::MATHED] << "read symbols from " << filename << "\n";
179         std::ifstream fs(filename.c_str());
180         while (fs) {
181                 int charid     = 0;
182                 int fallbackid = 0;
183                 latexkeys tmp;
184                 string line;
185                 getline(fs, line);
186                 istringstream is(line);
187                 is      >> tmp.name
188                                 >> tmp.inset
189                                 >> charid
190                                 >> fallbackid
191                                 >> tmp.extra
192                                 >> tmp.xmlname;
193                 if (!is)
194                         continue;
195
196                 // tmp.inset _is_ the fontname here.
197                 // create fallbacks if necessary
198                 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra=="special") {
199                         lyxerr[Debug::MATHED] << "symbol abuse for " << tmp.name << "\n";
200                         tmp.draw = tmp.name;
201                 } else if (math_font_available(tmp.inset)) {
202                         lyxerr[Debug::MATHED] << "symbol available for " << tmp.name << "\n";
203                         tmp.draw += char(charid);
204                 } else if (fallbackid) {
205                         if (tmp.inset == "cmex")
206                                 tmp.inset  = "lyxsymbol";
207                         else
208                                 tmp.inset  = "lyxboldsymbol";
209                         lyxerr[Debug::MATHED] << "symbol fallback for " << tmp.name << "\n";
210                         tmp.draw += char(fallbackid); 
211                 } else {
212                         lyxerr[Debug::MATHED] << "faking " << tmp.name << "\n";
213                         tmp.draw = tmp.name;
214                         tmp.inset = "lyxtex";
215                 }
216
217                 if (theWordList.find(tmp.name) != theWordList.end())
218                         lyxerr[Debug::MATHED] << "readSymbols: inset " << tmp.name
219                                << " already exists.\n";
220                 else
221                         theWordList[tmp.name] = tmp;
222                 lyxerr[Debug::MATHED] << "read symbol '" << tmp.name
223                                         <<  "  inset: " << tmp.inset
224                                         <<  "  draw: " << int(tmp.draw[0])
225                                         <<  "  extra: " << tmp.extra
226                                         << "'\n";
227         }
228 }
229
230
231 void initSymbols()
232 {
233         unsigned const n = sizeof(wordlist_array) / sizeof(wordlist_array[0]);
234         for (key_type * p = wordlist_array; p != wordlist_array + n; ++p) {
235                 latexkeys tmp;
236                 tmp.name  = p->name;
237                 tmp.inset = p->inset;
238                 tmp.draw  = p->name;
239                 theWordList[p->name] = tmp;
240         }
241
242         lyxerr[Debug::MATHED] << "reading symbols file\n";
243         string const file = LibFileSearch(string(), "symbols");
244         if (file.empty())
245                 lyxerr << "Could not find symbols file\n";
246         else
247                 readSymbols(file);
248 }
249
250
251 } // namespace anon
252
253
254 latexkeys const * in_word_set(string const & str)
255 {
256         static bool initialized = false;
257
258         if (!initialized) {
259                 initSymbols();
260                 initialized = true;
261         }
262
263         WordList::iterator it = theWordList.find(str);
264         //lyxerr << "looking up '" << str << "' found: "
265         // << (it != theWordList.end()) << "\n";
266         return (it != theWordList.end()) ? &(it->second) : 0;
267 }
268
269
270 MathAtom createMathInset(string const & s)
271 {
272         lyxerr[Debug::MATHED] << "creating inset with name: '" << s << "'\n";
273         if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
274                 return MathAtom(new MathMacroArgument(s[1] - '0'));
275
276         if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
277                         && s[2] >= '1' && s[2] <= '9')
278                 return MathAtom(new MathMacroArgument(s[2] - '0'));
279         if (s == "kern")
280                 return MathAtom(new MathKernInset);
281         if (s == "xymatrix")
282                 return MathAtom(new MathXYMatrixInset);
283         if (s == "xrightarrow" || s == "xleftarrow")
284                 return MathAtom(new MathXArrowInset(s));
285         if (s == "split" || s == "gathered" || s == "aligned")
286                 return MathAtom(new MathSplitInset(s));
287         if (s == "cases")
288                 return MathAtom(new MathCasesInset);
289         if (s == "substack")
290                 return MathAtom(new MathSubstackInset);
291         if (s == "subarray" || s == "array")
292                 return MathAtom(new MathArrayInset(s, 1, 1));
293         if (s == "sqrt")
294                 return MathAtom(new MathSqrtInset);
295         if (s == "root")
296                 return MathAtom(new MathRootInset);
297         if (s == "stack")
298                 return MathAtom(new MathStackrelInset);
299         if (s == "binom" || s == "choose")
300                 return MathAtom(new MathBinomInset);
301         if (s == "over" || s == "frac")
302                 return MathAtom(new MathFracInset);
303         if (s == "atop")
304                 return MathAtom(new MathFracInset(true));
305         if (s == "not")
306                 return MathAtom(new MathNotInset);
307         if (s == "lefteqn")
308                 return MathAtom(new MathLefteqnInset);
309         if (s == "ref")
310                 return MathAtom(new RefInset);
311
312
313         latexkeys const * l = in_word_set(s);
314         if (l) {
315                 string const & inset = l->inset;
316                 lyxerr[Debug::MATHED] << " found inset: '" << inset << "'\n";
317                 if (inset == "underset")
318                         return MathAtom(new MathUndersetInset);
319                 if (inset == "decoration")
320                         return MathAtom(new MathDecorationInset(l->name));
321                 if (inset == "space")
322                         return MathAtom(new MathSpaceInset(l->name));
323                 if (inset == "dots")
324                         return MathAtom(new MathDotsInset(l->name));
325                 if (inset == "box")
326                         return MathAtom(new MathBoxInset(l->name));
327                 if (inset == "fbox")
328                         return MathAtom(new MathFboxInset);
329                 if (inset == "style")
330                         return MathAtom(new MathSizeInset(l));
331                 if (inset == "font")
332                         return MathAtom(new MathFontInset(l->name));
333                 if (inset == "oldfont")
334                         return MathAtom(new MathFontInset(l->name));
335                 if (inset == "matrix")
336                         return MathAtom(new MathAMSArrayInset(s));
337                 return MathAtom(new MathSymbolInset(l));
338         }
339
340         if (MathMacroTable::has(s))
341                 return MathAtom(new MathMacro(s));
342
343         //lyxerr[Debug::MATHED] << "creating inset 2 with name: '" << s << "'\n";
344         return MathAtom(new MathUnknownInset(s));
345 }