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