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