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