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