]> git.lyx.org Git - lyx.git/blob - src/mathed/MathFactory.cpp
6cb6e702bafcbee43c6118db5ecd80515f052425
[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 "InsetMathMacro.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 "MathMacroTable.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         LyXFont 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                             (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                         // symbol font is not available sometimes
185                         docstring symbol_font = from_ascii("lyxsymbol");
186
187                         if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
188                                 LYXERR(Debug::MATHED) << "symbol abuse for " << to_utf8(tmp.name) << endl;
189                                 tmp.draw = tmp.name;
190                         } else if (math_font_available(tmp.inset)) {
191                                 LYXERR(Debug::MATHED) << "symbol available for " << to_utf8(tmp.name) << endl;
192                                 tmp.draw.push_back(char_type(charid));
193                         } else if (fallbackid && math_font_available(symbol_font)) {
194                                 if (tmp.inset == "cmex")
195                                         tmp.inset = from_ascii("lyxsymbol");
196                                 else
197                                         tmp.inset = from_ascii("lyxboldsymbol");
198                                 LYXERR(Debug::MATHED) << "symbol fallback for " << to_utf8(tmp.name) << endl;
199                                 tmp.draw.push_back(char_type(fallbackid));
200                         } else {
201                                 LYXERR(Debug::MATHED) << "faking " << to_utf8(tmp.name) << endl;
202                                 tmp.draw = tmp.name;
203                                 tmp.inset = from_ascii("lyxtex");
204                         }
205                 } else {
206                         // it's a proper inset
207                         LYXERR(Debug::MATHED) << "inset " << to_utf8(tmp.inset)
208                                               << " used for " << to_utf8(tmp.name)
209                                               << endl;
210                 }
211
212                 if (theWordList.find(tmp.name) != theWordList.end())
213                         LYXERR(Debug::MATHED)
214                                 << "readSymbols: inset " << to_utf8(tmp.name)
215                                 << " already exists." << endl;
216                 else
217                         theWordList[tmp.name] = tmp;
218
219                 LYXERR(Debug::MATHED)
220                         << "read symbol '" << to_utf8(tmp.name)
221                         << "  inset: " << to_utf8(tmp.inset)
222                         << "  draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
223                         << "  extra: " << to_utf8(tmp.extra)
224                         << "  requires: " << to_utf8(tmp.requires)
225                         << '\'' << endl;
226         }
227         docstring tmp = from_ascii("cmm");
228         docstring tmp2 = from_ascii("cmsy");
229         has_math_fonts = math_font_available(tmp) && math_font_available(tmp2);
230 }
231
232
233 } // namespace anon
234
235
236 void initMath()
237 {
238         static bool initialized = false;
239         if (!initialized) {
240                 initialized = true;
241                 initParser();
242                 initSymbols();
243         }
244 }
245
246
247 latexkeys const * in_word_set(docstring const & str)
248 {
249         WordList::iterator it = theWordList.find(str);
250         return it != theWordList.end() ? &(it->second) : 0;
251 }
252
253
254 MathAtom createInsetMath(char const * const s)
255 {
256         return createInsetMath(from_utf8(s));
257 }
258
259
260 MathAtom createInsetMath(docstring const & s)
261 {
262         //lyxerr << "creating inset with name: '" << s << '\'' << endl;
263         latexkeys const * l = in_word_set(s);
264         if (l) {
265                 docstring const & inset = l->inset;
266                 //lyxerr << " found inset: '" << inset << '\'' << endl;
267                 if (inset == "ref")
268                         return MathAtom(new RefInset(l->name));
269                 if (inset == "overset")
270                         return MathAtom(new InsetMathOverset);
271                 if (inset == "underset")
272                         return MathAtom(new InsetMathUnderset);
273                 if (inset == "decoration")
274                         return MathAtom(new InsetMathDecoration(l));
275                 if (inset == "space")
276                         return MathAtom(new InsetMathSpace(l->name));
277                 if (inset == "dots")
278                         return MathAtom(new InsetMathDots(l));
279                 if (inset == "mbox")
280                         // return MathAtom(new InsetMathMBox);
281                         // InsetMathMBox is proposed to replace InsetMathBox,
282                         // but is not ready yet (it needs a BufferView for
283                         // construction)
284                         return MathAtom(new InsetMathBox(l->name));
285 //              if (inset == "fbox")
286 //                      return MathAtom(new InsetMathFBox(l));
287                 if (inset == "style")
288                         return MathAtom(new InsetMathSize(l));
289                 if (inset == "font")
290                         return MathAtom(new InsetMathFont(l));
291                 if (inset == "oldfont")
292                         return MathAtom(new InsetMathFontOld(l));
293                 if (inset == "matrix")
294                         return MathAtom(new InsetMathAMSArray(s));
295                 if (inset == "split")
296                         return MathAtom(new InsetMathSplit(s));
297                 if (inset == "big")
298                         // we can't create a InsetMathBig, since the argument
299                         // is missing.
300                         return MathAtom(new InsetMathUnknown(s));
301                 return MathAtom(new InsetMathSymbol(l));
302         }
303
304         if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
305                 return MathAtom(new MathMacroArgument(s[1] - '0'));
306         if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
307                         && s[2] >= '1' && s[2] <= '9')
308                 return MathAtom(new MathMacroArgument(s[2] - '0'));
309         if (s == "boxed")
310                 return MathAtom(new InsetMathBoxed());
311         if (s == "fbox")
312                 return MathAtom(new InsetMathFBox());
313         if (s == "framebox")
314                 return MathAtom(new InsetMathFrameBox);
315         if (s == "makebox")
316                 return MathAtom(new InsetMathMakebox);
317         if (s == "kern")
318                 return MathAtom(new InsetMathKern);
319         if (s.substr(0, 8) == "xymatrix") {
320                 char spacing_code = '\0';
321                 LyXLength spacing;
322                 size_t const len = s.length();
323                 size_t i = 8;
324                 if (i < len && s[i] == '@') {
325                         ++i;
326                         if (i < len) {
327                                 switch (s[i]) {
328                                 case 'R':
329                                 case 'C':
330                                 case 'M':
331                                 case 'W':
332                                 case 'H':
333                                 case 'L':
334                                         spacing_code = static_cast<char>(s[i]);
335                                         ++i;
336                                         break;
337                                 }
338                         }
339                         if (i < len && s[i] == '=') {
340                                 ++i;
341                                 spacing = LyXLength(to_ascii(s.substr(i)));
342                         }
343                 }
344                 return MathAtom(new InsetMathXYMatrix(spacing, spacing_code));
345         }
346         if (s == "xrightarrow" || s == "xleftarrow")
347                 return MathAtom(new InsetMathXArrow(s));
348         if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
349                 return MathAtom(new InsetMathSplit(s));
350         if (s == "cases")
351                 return MathAtom(new InsetMathCases);
352         if (s == "substack")
353                 return MathAtom(new InsetMathSubstack);
354         if (s == "subarray" || s == "array")
355                 return MathAtom(new InsetMathArray(s, 1, 1));
356         if (s == "sqrt")
357                 return MathAtom(new InsetMathSqrt);
358         if (s == "root")
359                 return MathAtom(new InsetMathRoot);
360         if (s == "tabular")
361                 return MathAtom(new InsetMathTabular(s, 1, 1));
362         if (s == "stackrel")
363                 return MathAtom(new InsetMathStackrel);
364         if (s == "binom" || s == "choose")
365                 return MathAtom(new InsetMathBinom(s == "choose"));
366         if (s == "frac")
367                 return MathAtom(new InsetMathFrac);
368         if (s == "over")
369                 return MathAtom(new InsetMathFrac(InsetMathFrac::OVER));
370         if (s == "nicefrac")
371                 return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC));
372         //if (s == "infer")
373         //      return MathAtom(new MathInferInset);
374         if (s == "atop")
375                 return MathAtom(new InsetMathFrac(InsetMathFrac::ATOP));
376         if (s == "lefteqn")
377                 return MathAtom(new InsetMathLefteqn);
378         if (s == "boldsymbol")
379                 return MathAtom(new InsetMathBoldSymbol);
380         if (s == "color" || s == "normalcolor")
381                 return MathAtom(new InsetMathColor(true));
382         if (s == "textcolor")
383                 return MathAtom(new InsetMathColor(false));
384         if (s == "dfrac")
385                 return MathAtom(new InsetMathDFrac);
386         if (s == "tfrac")
387                 return MathAtom(new InsetMathTFrac);
388         if (s == "hphantom")
389                 return MathAtom(new InsetMathPhantom(InsetMathPhantom::hphantom));
390         if (s == "phantom")
391                 return MathAtom(new InsetMathPhantom(InsetMathPhantom::phantom));
392         if (s == "vphantom")
393                 return MathAtom(new InsetMathPhantom(InsetMathPhantom::vphantom));
394
395         if (MacroTable::globalMacros().has(s))
396                 return MathAtom(new MathMacro(s,
397                         MacroTable::globalMacros().get(s).numargs()));
398         //if (MacroTable::localMacros().has(s))
399         //      return MathAtom(new MathMacro(s,
400         //              MacroTable::localMacros().get(s).numargs()));
401
402         //lyxerr << "creating unknown inset '" << s << "'" << endl;
403         return MathAtom(new InsetMathUnknown(s));
404 }
405
406
407 bool createInsetMath_fromDialogStr(docstring const & str, MathArray & ar)
408 {
409         // An example str:
410         // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
411         docstring name;
412         docstring body = split(str, name, ' ');
413
414         if (name != "ref" )
415                 return false;
416
417         InsetCommandParams icp("ref");
418         // FIXME UNICODE
419         InsetCommandMailer::string2params("ref", to_utf8(str), icp);
420         mathed_parse_cell(ar, icp.getCommand());
421         if (ar.size() != 1)
422                 return false;
423
424         return ar[0].nucleus();
425 }
426
427
428 } // namespace lyx