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