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