]> git.lyx.org Git - lyx.git/blob - src/mathed/MathFactory.C
Fix comment according to Enricos explanation
[lyx.git] / src / mathed / MathFactory.C
1 /**
2  * \file MathFactory.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "MathFactory.h"
14
15 #include "InsetMathAMSArray.h"
16 #include "InsetMathArray.h"
17 #include "InsetMathBinom.h"
18 #include "InsetMathBoldSymbol.h"
19 #include "InsetMathBoxed.h"
20 #include "InsetMathBox.h"
21 #include "InsetMathCases.h"
22 #include "InsetMathColor.h"
23 #include "InsetMathDecoration.h"
24 #include "InsetMathDFrac.h"
25 #include "InsetMathDots.h"
26 #include "InsetMathFBox.h"
27 #include "InsetMathFont.h"
28 #include "InsetMathFontOld.h"
29 #include "InsetMathFrac.h"
30 #include "InsetMathFrameBox.h"
31 #include "InsetMathKern.h"
32 #include "InsetMathLefteqn.h"
33 #include "InsetMathMacro.h"
34 #include "InsetMathMakebox.h"
35 #include "InsetMathOverset.h"
36 #include "InsetMathPhantom.h"
37 #include "InsetMathRef.h"
38 #include "InsetMathRoot.h"
39 #include "InsetMathSize.h"
40 #include "InsetMathSpace.h"
41 #include "InsetMathSplit.h"
42 #include "InsetMathSqrt.h"
43 #include "InsetMathStackrel.h"
44 #include "InsetMathSubstack.h"
45 #include "InsetMathSymbol.h"
46 #include "InsetMathTabular.h"
47 #include "InsetMathTFrac.h"
48 #include "InsetMathUnderset.h"
49 #include "InsetMathUnknown.h"
50 #include "InsetMathXArrow.h"
51 #include "InsetMathXYMatrix.h"
52 #include "MathMacroArgument.h"
53 #include "MathMacroTable.h"
54 #include "MathMacroTemplate.h"
55 #include "MathParser.h"
56 #include "MathSupport.h"
57
58 #include "debug.h"
59
60 #include "support/filetools.h" // LibFileSearch
61 #include "support/lstrings.h"
62
63 #include "frontends/FontLoader.h"
64
65 #include <fstream>
66 #include <sstream>
67
68
69 namespace lyx {
70
71 using support::libFileSearch;
72 using support::split;
73
74 using std::string;
75 using std::endl;
76 using std::istringstream;
77 using std::vector;
78
79 bool has_math_fonts;
80
81
82 namespace {
83
84 // file scope
85 typedef std::map<docstring, latexkeys> WordList;
86
87 WordList theWordList;
88
89
90 bool math_font_available(docstring & name)
91 {
92         LyXFont f;
93         augmentFont(f, name);
94
95         // Do we have the font proper?
96         if (theFontLoader().available(f))
97                 return true;
98
99         // can we fake it?
100         if (name == "eufrak") {
101                 name = from_ascii("lyxfakefrak");
102                 return true;
103         }
104
105         lyxerr[Debug::MATHED]
106                 << "font " << to_utf8(name) << " not available and I can't fake it"
107                 << endl;
108         return false;
109 }
110
111
112 void initSymbols()
113 {
114         string const filename = libFileSearch(string(), "symbols");
115         lyxerr[Debug::MATHED] << "read symbols from " << filename << endl;
116         if (filename.empty()) {
117                 lyxerr << "Could not find symbols file" << endl;
118                 return;
119         }
120
121         std::ifstream fs(filename.c_str());
122         string line;
123         bool skip = false;
124         while (getline(fs, line)) {
125                 int charid     = 0;
126                 int fallbackid = 0;
127                 if (!line.empty() && line[0] == '#')
128                         continue;
129
130                 // special case of iffont/else/endif
131                 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
132                         istringstream is(line);
133                         string tmp;
134                         is >> tmp;
135                         is >> tmp;
136                         docstring t = from_utf8(tmp);
137                         skip = !math_font_available(t);
138                         continue;
139                 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
140                         skip = !skip;
141                 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
142                         skip = false;
143                         continue;
144                 } else if (skip)
145                         continue;
146
147                 // special case of pre-defined macros
148                 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
149                         //lyxerr << "macro definition: '" << line << '\'' << endl;
150                         MacroTable::globalMacros().insert(from_utf8(line));
151                         continue;
152                 }
153
154                 idocstringstream is(from_utf8(line));
155                 latexkeys tmp;
156                 is >> tmp.name >>       tmp.inset;
157                 if (isFontName(tmp.inset))
158                         is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
159                 else
160                         is >> tmp.extra;
161                 if (!is) {
162                         lyxerr[Debug::MATHED] << "skipping line '" << line << '\'' << endl;
163                         lyxerr[Debug::MATHED]
164                                 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' ' << to_utf8(tmp.extra) << endl;
165                         continue;
166                 }
167
168                 if (isFontName(tmp.inset)) {
169                         // tmp.inset _is_ the fontname here.
170                         // create fallbacks if necessary
171
172                         // store requirements as long as we can
173                         if (tmp.inset == "msa" || tmp.inset == "msb")
174                                 tmp.requires = from_ascii("amssymb");
175                         // See http://bugzilla.lyx.org/show_bug.cgi?id=1942
176                         // else if (tmp.inset == "wasy")
177                         //      tmp.requires = "wasysym";
178
179                         // symbol font is not available sometimes
180                         docstring symbol_font = from_ascii("lyxsymbol");
181
182                         if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
183                                 lyxerr[Debug::MATHED] << "symbol abuse for " << to_utf8(tmp.name) << endl;
184                                 tmp.draw = tmp.name;
185                         } else if (math_font_available(tmp.inset)) {
186                                 lyxerr[Debug::MATHED] << "symbol available for " << to_utf8(tmp.name) << endl;
187                                 tmp.draw.push_back(char_type(charid));
188                         } else if (fallbackid && math_font_available(symbol_font)) {
189                                 if (tmp.inset == "cmex")
190                                         tmp.inset = from_ascii("lyxsymbol");
191                                 else
192                                         tmp.inset = from_ascii("lyxboldsymbol");
193                                 lyxerr[Debug::MATHED] << "symbol fallback for " << to_utf8(tmp.name) << endl;
194                                 tmp.draw.push_back(char_type(fallbackid));
195                         } else {
196                                 lyxerr[Debug::MATHED] << "faking " << to_utf8(tmp.name) << endl;
197                                 tmp.draw = tmp.name;
198                                 tmp.inset = from_ascii("lyxtex");
199                         }
200                 } else {
201                         // it's a proper inset
202                         lyxerr[Debug::MATHED] << "inset " << to_utf8(tmp.inset)
203                                               << " used for " << to_utf8(tmp.name)
204                                               << endl;
205                 }
206
207                 if (theWordList.find(tmp.name) != theWordList.end())
208                         lyxerr[Debug::MATHED]
209                                 << "readSymbols: inset " << to_utf8(tmp.name)
210                                 << " already exists." << endl;
211                 else
212                         theWordList[tmp.name] = tmp;
213
214                 lyxerr[Debug::MATHED]
215                         << "read symbol '" << to_utf8(tmp.name)
216                         << "  inset: " << to_utf8(tmp.inset)
217                         << "  draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
218                         << "  extra: " << to_utf8(tmp.extra)
219                         << '\'' << endl;
220         }
221         docstring tmp = from_ascii("cmm");
222         docstring tmp2 = from_ascii("cmsy");
223         has_math_fonts = math_font_available(tmp) && math_font_available(tmp2);
224 }
225
226
227 } // namespace anon
228
229
230 void initMath()
231 {
232         static bool initialized = false;
233         if (!initialized) {
234                 initialized = true;
235                 initParser();
236                 initSymbols();
237         }
238 }
239
240
241 latexkeys const * in_word_set(docstring const & str)
242 {
243         WordList::iterator it = theWordList.find(str);
244         return it != theWordList.end() ? &(it->second) : 0;
245 }
246
247
248 MathAtom createInsetMath(char const * const s)
249 {
250         return createInsetMath(from_utf8(s));
251 }
252
253
254 MathAtom createInsetMath(docstring const & s)
255 {
256         //lyxerr << "creating inset with name: '" << s << '\'' << endl;
257         latexkeys const * l = in_word_set(s);
258         if (l) {
259                 docstring const & inset = l->inset;
260                 //lyxerr << " found inset: '" << inset << '\'' << endl;
261                 if (inset == "ref")
262                         return MathAtom(new RefInset(l->name));
263                 if (inset == "overset")
264                         return MathAtom(new InsetMathOverset);
265                 if (inset == "underset")
266                         return MathAtom(new InsetMathUnderset);
267                 if (inset == "decoration")
268                         return MathAtom(new InsetMathDecoration(l));
269                 if (inset == "space")
270                         return MathAtom(new InsetMathSpace(l->name));
271                 if (inset == "dots")
272                         return MathAtom(new InsetMathDots(l));
273                 if (inset == "mbox")
274                         // return MathAtom(new InsetMathMBox);
275                         // InsetMathMBox is proposed to replace InsetMathBox,
276                         // but is not ready yet (it needs a BufferView for
277                         // construction)
278                         return MathAtom(new InsetMathBox(l->name));
279 //              if (inset == "fbox")
280 //                      return MathAtom(new InsetMathFBox(l));
281                 if (inset == "style")
282                         return MathAtom(new InsetMathSize(l));
283                 if (inset == "font")
284                         return MathAtom(new InsetMathFont(l));
285                 if (inset == "oldfont")
286                         return MathAtom(new InsetMathFontOld(l));
287                 if (inset == "matrix")
288                         return MathAtom(new InsetMathAMSArray(s));
289                 if (inset == "split")
290                         return MathAtom(new InsetMathSplit(s));
291                 if (inset == "big")
292                         // we can't create a InsetMathBig, since the argument
293                         // is missing.
294                         return MathAtom(new InsetMathUnknown(s));
295                 return MathAtom(new InsetMathSymbol(l));
296         }
297
298         if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
299                 return MathAtom(new MathMacroArgument(s[1] - '0'));
300         if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
301                         && s[2] >= '1' && s[2] <= '9')
302                 return MathAtom(new MathMacroArgument(s[2] - '0'));
303         if (s == "boxed")
304                 return MathAtom(new InsetMathBoxed());
305         if (s == "fbox")
306                 return MathAtom(new InsetMathFBox());
307         if (s == "framebox")
308                 return MathAtom(new InsetMathFrameBox);
309         if (s == "makebox")
310                 return MathAtom(new InsetMathMakebox);
311         if (s == "kern")
312                 return MathAtom(new InsetMathKern);
313         if (s.substr(0, 8) == "xymatrix") {
314                 char spacing_code = '\0';
315                 LyXLength spacing;
316                 size_t const len = s.length();
317                 size_t i = 8;
318                 if (i < len && s[i] == '@') {
319                         ++i;
320                         if (i < len) {
321                                 switch (s[i]) {
322                                 case 'R':
323                                 case 'C':
324                                 case 'M':
325                                 case 'W':
326                                 case 'H':
327                                 case 'L':
328                                         spacing_code = s[i];
329                                         ++i;
330                                         break;
331                                 }
332                         }
333                         if (i < len && s[i] == '=') {
334                                 ++i;
335                                 spacing = LyXLength(to_ascii(s.substr(i)));
336                         }
337                 }
338                 return MathAtom(new InsetMathXYMatrix(spacing, spacing_code));
339         }
340         if (s == "xrightarrow" || s == "xleftarrow")
341                 return MathAtom(new InsetMathXArrow(s));
342         if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
343                 return MathAtom(new InsetMathSplit(s));
344         if (s == "cases")
345                 return MathAtom(new InsetMathCases);
346         if (s == "substack")
347                 return MathAtom(new InsetMathSubstack);
348         if (s == "subarray" || s == "array")
349                 return MathAtom(new InsetMathArray(s, 1, 1));
350         if (s == "sqrt")
351                 return MathAtom(new InsetMathSqrt);
352         if (s == "root")
353                 return MathAtom(new InsetMathRoot);
354         if (s == "tabular")
355                 return MathAtom(new InsetMathTabular(s, 1, 1));
356         if (s == "stackrel")
357                 return MathAtom(new InsetMathStackrel);
358         if (s == "binom" || s == "choose")
359                 return MathAtom(new InsetMathBinom(s == "choose"));
360         if (s == "frac")
361                 return MathAtom(new InsetMathFrac);
362         if (s == "over")
363                 return MathAtom(new InsetMathFrac(InsetMathFrac::OVER));
364         if (s == "nicefrac")
365                 return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC));
366         //if (s == "infer")
367         //      return MathAtom(new MathInferInset);
368         if (s == "atop")
369                 return MathAtom(new InsetMathFrac(InsetMathFrac::ATOP));
370         if (s == "lefteqn")
371                 return MathAtom(new InsetMathLefteqn);
372         if (s == "boldsymbol")
373                 return MathAtom(new InsetMathBoldSymbol);
374         if (s == "color" || s == "normalcolor")
375                 return MathAtom(new InsetMathColor(true));
376         if (s == "textcolor")
377                 return MathAtom(new InsetMathColor(false));
378         if (s == "dfrac")
379                 return MathAtom(new InsetMathDFrac);
380         if (s == "tfrac")
381                 return MathAtom(new InsetMathTFrac);
382         if (s == "hphantom")
383                 return MathAtom(new InsetMathPhantom(InsetMathPhantom::hphantom));
384         if (s == "phantom")
385                 return MathAtom(new InsetMathPhantom(InsetMathPhantom::phantom));
386         if (s == "vphantom")
387                 return MathAtom(new InsetMathPhantom(InsetMathPhantom::vphantom));
388
389         if (MacroTable::globalMacros().has(s))
390                 return MathAtom(new MathMacro(s,
391                         MacroTable::globalMacros().get(s).numargs()));
392         //if (MacroTable::localMacros().has(s))
393         //      return MathAtom(new MathMacro(s,
394         //              MacroTable::localMacros().get(s).numargs()));
395
396         //lyxerr << "creating unknown inset '" << s << "'" << endl;
397         return MathAtom(new InsetMathUnknown(s));
398 }
399
400
401 bool createInsetMath_fromDialogStr(docstring const & str, MathArray & ar)
402 {
403         // An example str:
404         // "ref LatexCommand \\ref{sec:Title}\n\\end_inset\n\n";
405         docstring name;
406         docstring body = split(str, name, ' ');
407
408         if (name != "ref" )
409                 return false;
410
411         // body comes with a head "LatexCommand " and a
412         // tail "\nend_inset\n\n". Strip them off.
413         docstring trimmed;
414         body = split(body, trimmed, ' ');
415         split(body, trimmed, '\n');
416
417         mathed_parse_cell(ar, trimmed);
418         if (ar.size() != 1)
419                 return false;
420
421         return ar[0].nucleus();
422 }
423
424
425 } // namespace lyx