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