]> git.lyx.org Git - lyx.git/blob - src/mathed/MathFactory.cpp
- introduce option to suppress the LaTeX package mhchem, fileformat change
[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 "InsetMathEnsureMath.h"
24 #include "InsetMathFont.h"
25 #include "InsetMathFontOld.h"
26 #include "InsetMathFrac.h"
27 #include "InsetMathKern.h"
28 #include "InsetMathLefteqn.h"
29 #include "InsetMathOverset.h"
30 #include "InsetMathPhantom.h"
31 #include "InsetMathRef.h"
32 #include "InsetMathRoot.h"
33 #include "InsetMathSize.h"
34 #include "InsetMathSpace.h"
35 #include "InsetMathSpecialChar.h"
36 #include "InsetMathSplit.h"
37 #include "InsetMathSqrt.h"
38 #include "InsetMathStackrel.h"
39 #include "InsetMathSubstack.h"
40 #include "InsetMathSymbol.h"
41 #include "InsetMathTabular.h"
42 #include "InsetMathUnderset.h"
43 #include "InsetMathUnknown.h"
44 #include "InsetMathHull.h"
45 #include "InsetMathXArrow.h"
46 #include "InsetMathXYMatrix.h"
47 #include "MacroTable.h"
48 #include "MathMacro.h"
49 #include "MathMacroArgument.h"
50 #include "MathParser.h"
51 #include "MathStream.h"
52 #include "MathSupport.h"
53
54 #include "insets/InsetCommand.h"
55 #include "insets/InsetSpace.h"
56
57 #include "support/debug.h"
58 #include "support/docstream.h"
59 #include "support/FileName.h"
60 #include "support/filetools.h" // LibFileSearch
61 #include "support/lstrings.h"
62
63 #include "frontends/FontLoader.h"
64
65 #include "Buffer.h"
66 #include "BufferParams.h"
67 #include "Encoding.h"
68 #include "LyX.h" // use_gui
69 #include "OutputParams.h"
70
71 using namespace std;
72 using namespace lyx::support;
73
74 namespace lyx {
75
76 bool has_math_fonts;
77
78
79 namespace {
80
81 MathWordList theWordList;
82
83
84 bool isMathFontAvailable(docstring & name)
85 {
86         if (!use_gui)
87                 return false;
88
89         FontInfo f;
90         augmentFont(f, name);
91
92         // Do we have the font proper?
93         if (theFontLoader().available(f))
94                 return true;
95
96         // can we fake it?
97         if (name == "eufrak") {
98                 name = from_ascii("lyxfakefrak");
99                 return true;
100         }
101
102         LYXERR(Debug::MATHED,
103                 "font " << to_utf8(name) << " not available and I can't fake it");
104         return false;
105 }
106
107
108 void initSymbols()
109 {
110         FileName const filename = libFileSearch(string(), "symbols");
111         LYXERR(Debug::MATHED, "read symbols from " << filename);
112         if (filename.empty()) {
113                 lyxerr << "Could not find symbols file" << endl;
114                 return;
115         }
116
117         ifstream fs(filename.toFilesystemEncoding().c_str());
118         string line;
119         bool skip = false;
120         while (getline(fs, line)) {
121                 int charid     = 0;
122                 int fallbackid = 0;
123                 if (line.empty() || line[0] == '#')
124                         continue;
125
126                 // special case of iffont/else/endif
127                 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
128                         istringstream is(line);
129                         string tmp;
130                         is >> tmp;
131                         is >> tmp;
132                         docstring t = from_utf8(tmp);
133                         skip = !isMathFontAvailable(t);
134                         continue;
135                 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
136                         skip = !skip;
137                 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
138                         skip = false;
139                         continue;
140                 } else if (skip)
141                         continue;
142
143                 // special case of pre-defined macros
144                 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
145                         //lyxerr << "macro definition: '" << line << '\'' << endl;
146                         istringstream is(line);
147                         string macro;
148                         string requires;
149                         is >> macro >> requires;
150                         MacroTable::globalMacros().insert(0, from_utf8(macro), requires);
151                         continue;
152                 }
153
154                 idocstringstream is(from_utf8(line));
155                 latexkeys tmp;
156                 is >> tmp.name >> tmp.inset;
157                 if (isFontName(tmp.inset))
158                         is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
159                 else
160                         is >> tmp.extra;
161                 // requires is optional
162                 if (is)
163                         is >> tmp.requires;
164                 else {
165                         LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
166                                 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' '
167                                 << to_utf8(tmp.extra));
168                         continue;
169                 }
170
171                 if (isFontName(tmp.inset)) {
172                         // tmp.inset _is_ the fontname here.
173                         // create fallbacks if necessary
174
175                         // store requirements as long as we can
176                         if (tmp.requires.empty()) {
177                                 if (tmp.inset == "msa" || tmp.inset == "msb")
178                                         tmp.requires = from_ascii("amssymb");
179                                 else if (tmp.inset == "wasy")
180                                         tmp.requires = from_ascii("wasysym");
181                         }
182
183                         // symbol font is not available sometimes
184                         docstring symbol_font = from_ascii("lyxsymbol");
185
186                         if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
187                                 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
188                                 tmp.draw = tmp.name;
189                         } else if (isMathFontAvailable(tmp.inset)) {
190                                 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
191                                 tmp.draw.push_back(char_type(charid));
192                         } else if (fallbackid && isMathFontAvailable(symbol_font)) {
193                                 if (tmp.inset == "cmex")
194                                         tmp.inset = from_ascii("lyxsymbol");
195                                 else
196                                         tmp.inset = from_ascii("lyxboldsymbol");
197                                 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
198                                 tmp.draw.push_back(char_type(fallbackid));
199                         } else {
200                                 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
201                                 tmp.draw = tmp.name;
202                                 tmp.inset = from_ascii("lyxtex");
203                         }
204                 } else {
205                         // it's a proper inset
206                         LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
207                                               << " used for " << to_utf8(tmp.name));
208                 }
209
210                 if (theWordList.find(tmp.name) != theWordList.end())
211                         LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
212                                 << " already exists.");
213                 else
214                         theWordList[tmp.name] = tmp;
215
216                 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
217                         << "  inset: " << to_utf8(tmp.inset)
218                         << "  draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
219                         << "  extra: " << to_utf8(tmp.extra)
220                         << "  requires: " << to_utf8(tmp.requires) << '\'');
221         }
222         docstring tmp = from_ascii("cmm");
223         docstring tmp2 = from_ascii("cmsy");
224         has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
225 }
226
227
228 bool isSpecialChar(docstring const & name)
229 {
230         if (name.size() != 1)
231                 return  name == "textasciicircum" || name == "mathcircumflex" ||
232                         name == "textasciitilde"  || name == "textbackslash";
233
234         char_type const c = name.at(0);
235         return  c == '{' || c == '}' || c == '&' || c == '$' ||
236                 c == '#' || c == '%' || c == '_' || c == ' ';
237 }
238
239
240 } // namespace anon
241
242 MathWordList const & mathedWordList()
243 {
244         return theWordList;
245 }
246
247
248 void initMath()
249 {
250         static bool initialized = false;
251         if (!initialized) {
252                 initialized = true;
253                 initParser();
254                 initSymbols();
255         }
256 }
257
258
259 bool ensureMath(WriteStream & os, bool needs_math_mode, bool macro)
260 {
261         bool brace = os.pendingBrace();
262         os.pendingBrace(false);
263         if (!os.latex())
264                 return brace;
265         if (os.textMode() && needs_math_mode) {
266                 os << "\\ensuremath{";
267                 os.textMode(false);
268                 brace = true;
269         } else if (macro && brace && !needs_math_mode) {
270                 // This is a user defined macro, but not a MathMacro, so we
271                 // cannot be sure what mode is needed. As it was entered in
272                 // a text box, we restore the text mode.
273                 os << '}';
274                 os.textMode(true);
275                 brace = false;
276         }
277         return brace;
278 }
279
280
281 int ensureMode(WriteStream & os, InsetMath::mode_type mode, bool locked)
282 {
283         bool textmode = mode == InsetMath::TEXT_MODE;
284         if (os.latex() && textmode && os.pendingBrace()) {
285                 os.os() << '}';
286                 os.pendingBrace(false);
287                 os.pendingSpace(false);
288                 os.textMode(true);
289         }
290         int oldmodes = os.textMode() ? 1 : 0;
291         os.textMode(textmode);
292         oldmodes |= os.lockedMode() ? 2 : 0;
293         os.lockedMode(locked);
294         return oldmodes;
295 }
296
297
298 latexkeys const * in_word_set(docstring const & str)
299 {
300         MathWordList::iterator it = theWordList.find(str);
301         return it != theWordList.end() ? &(it->second) : 0;
302 }
303
304
305 MathAtom createInsetMath(char const * const s, Buffer * buf)
306 {
307         return createInsetMath(from_utf8(s), buf);
308 }
309
310
311 MathAtom createInsetMath(docstring const & s, Buffer * buf)
312 {
313         //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
314         if ((s == "ce" || s == "cf") && buf
315             && buf->params().use_mhchem == BufferParams::package_off)
316                 return MathAtom(new MathMacro(buf, s));
317
318         latexkeys const * l = in_word_set(s);
319         if (l) {
320                 docstring const & inset = l->inset;
321                 //lyxerr << " found inset: '" << inset << '\'' << endl;
322                 if (inset == "ref")
323                         return MathAtom(new InsetMathRef(buf, l->name));
324                 if (inset == "overset")
325                         return MathAtom(new InsetMathOverset(buf));
326                 if (inset == "underset")
327                         return MathAtom(new InsetMathUnderset(buf));
328                 if (inset == "decoration")
329                         return MathAtom(new InsetMathDecoration(buf, l));
330                 if (inset == "space")
331                         return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
332                 if (inset == "dots")
333                         return MathAtom(new InsetMathDots(l));
334                 if (inset == "mbox")
335                         // return MathAtom(new InsetMathMBox);
336                         // InsetMathMBox is proposed to replace InsetMathBox,
337                         // but is not ready yet (it needs a BufferView for
338                         // construction)
339                         return MathAtom(new InsetMathBox(buf, l->name));
340 //              if (inset == "fbox")
341 //                      return MathAtom(new InsetMathFBox(l));
342                 if (inset == "style")
343                         return MathAtom(new InsetMathSize(buf, l));
344                 if (inset == "font")
345                         return MathAtom(new InsetMathFont(buf, l));
346                 if (inset == "oldfont")
347                         return MathAtom(new InsetMathFontOld(buf, l));
348                 if (inset == "matrix")
349                         return MathAtom(new InsetMathAMSArray(buf, s));
350                 if (inset == "split")
351                         return MathAtom(new InsetMathSplit(buf, s));
352                 if (inset == "big")
353                         // we can't create a InsetMathBig, since the argument
354                         // is missing.
355                         return MathAtom(new InsetMathUnknown(s));
356                 return MathAtom(new InsetMathSymbol(l));
357         }
358
359         if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
360                 return MathAtom(new MathMacroArgument(s[1] - '0'));
361         if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
362                         && s[2] >= '1' && s[2] <= '9')
363                 return MathAtom(new MathMacroArgument(s[2] - '0'));
364         if (s == "boxed")
365                 return MathAtom(new InsetMathBoxed(buf));
366         if (s == "fbox")
367                 return MathAtom(new InsetMathFBox(buf));
368         if (s == "framebox")
369                 return MathAtom(new InsetMathMakebox(buf, true));
370         if (s == "makebox")
371                 return MathAtom(new InsetMathMakebox(buf, false));
372         if (s == "kern")
373                 return MathAtom(new InsetMathKern);
374         if (s.substr(0, 8) == "xymatrix") {
375                 char spacing_code = '\0';
376                 Length spacing;
377                 size_t const len = s.length();
378                 size_t i = 8;
379                 if (i < len && s[i] == '@') {
380                         ++i;
381                         if (i < len) {
382                                 switch (s[i]) {
383                                 case 'R':
384                                 case 'C':
385                                 case 'M':
386                                 case 'W':
387                                 case 'H':
388                                 case 'L':
389                                         spacing_code = static_cast<char>(s[i]);
390                                         ++i;
391                                         break;
392                                 }
393                         }
394                         if (i < len && s[i] == '=') {
395                                 ++i;
396                                 spacing = Length(to_ascii(s.substr(i)));
397                         }
398                 }
399                 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code));
400         }
401         if (s == "xrightarrow" || s == "xleftarrow")
402                 return MathAtom(new InsetMathXArrow(buf, s));
403         if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
404                 return MathAtom(new InsetMathSplit(buf, s));
405         if (s == "cases")
406                 return MathAtom(new InsetMathCases(buf));
407         if (s == "substack")
408                 return MathAtom(new InsetMathSubstack(buf));
409         if (s == "subarray" || s == "array")
410                 return MathAtom(new InsetMathArray(buf, s, 1, 1));
411         if (s == "sqrt")
412                 return MathAtom(new InsetMathSqrt(buf));
413         if (s == "root")
414                 return MathAtom(new InsetMathRoot(buf));
415         if (s == "tabular")
416                 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
417         if (s == "stackrel")
418                 return MathAtom(new InsetMathStackrel(buf));
419         if (s == "binom")
420                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
421         if (s == "dbinom")
422                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
423         if (s == "tbinom")
424                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
425         if (s == "choose")
426                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
427         if (s == "brace")
428                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
429         if (s == "brack")
430                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
431         if (s == "frac")
432                 return MathAtom(new InsetMathFrac(buf));
433         if (s == "cfrac")
434                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
435         if (s == "dfrac")
436                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
437         if (s == "tfrac")
438                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
439         if (s == "over")
440                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
441         if (s == "nicefrac")
442                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
443         if (s == "unitfrac")
444                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
445         // These string values are only for math toolbar use, no LaTeX names
446         if (s == "unitfracthree")
447                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
448         if (s == "unitone")
449                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
450         if (s == "unittwo")
451                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
452         if (s == "cfracleft")
453                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
454         if (s == "cfracright")
455                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
456         //if (s == "infer")
457         //      return MathAtom(new MathInferInset);
458         if (s == "atop")
459                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
460         if (s == "lefteqn")
461                 return MathAtom(new InsetMathLefteqn(buf));
462         if (s == "boldsymbol")
463                 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
464         if (s == "bm")
465                 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
466         if (s == "heavysymbol" || s == "hm")
467                 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
468         if (s == "color" || s == "normalcolor")
469                 return MathAtom(new InsetMathColor(buf, true));
470         if (s == "textcolor")
471                 return MathAtom(new InsetMathColor(buf, false));
472         if (s == "hphantom")
473                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
474         if (s == "phantom")
475                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
476         if (s == "vphantom")
477                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
478         if (s == "ensuremath")
479                 return MathAtom(new InsetMathEnsureMath(buf));
480         if (isSpecialChar(s))
481                 return MathAtom(new InsetMathSpecialChar(s));
482
483         if (s == "regexp")
484                 return MathAtom(new InsetMathHull(buf, hullRegexp));
485
486         return MathAtom(new MathMacro(buf, s));
487 }
488
489
490 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
491 {
492         // An example str:
493         // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
494         docstring name;
495         docstring body = split(str, name, ' ');
496
497         if (name == "ref") {
498                 InsetCommandParams icp(REF_CODE);
499                 // FIXME UNICODE
500                 InsetCommand::string2params("ref", to_utf8(str), icp);
501                 mathed_parse_cell(ar, icp.getCommand());
502         } else if (name == "mathspace") {
503                 InsetSpaceParams isp(true);
504                 InsetSpace::string2params(to_utf8(str), isp);
505                 InsetSpace is(isp);
506                 odocstringstream os;
507                 Encoding const * const ascii = encodings.fromLyXName("ascii");
508                 OutputParams op(ascii);
509                 is.latex(os, op);
510                 mathed_parse_cell(ar, os.str());
511                 if (ar.size() == 2) {
512                         // remove "{}"
513                         if (ar[1].nucleus()->asBraceInset())
514                                 ar.pop_back();
515                 }
516         } else
517                 return false;
518
519         if (ar.size() != 1)
520                 return false;
521
522         return ar[0].nucleus();
523 }
524
525
526 bool isAsciiOrMathAlpha(char_type c)
527 {
528         return c < 0x80 || Encodings::isMathAlpha(c);
529 }
530
531
532 } // namespace lyx