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