]> git.lyx.org Git - lyx.git/blob - src/mathed/MathFactory.cpp
267432affd878c7440656ca73001efaa1a511b84
[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 int ensureMode(WriteStream & os, InsetMath::mode_type mode, bool locked)
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         int oldmodes = os.textMode() ? 1 : 0;
289         os.textMode(textmode);
290         oldmodes |= os.lockedMode() ? 2 : 0;
291         os.lockedMode(locked);
292         return oldmodes;
293 }
294
295
296 latexkeys const * in_word_set(docstring const & str)
297 {
298         MathWordList::iterator it = theWordList.find(str);
299         return it != theWordList.end() ? &(it->second) : 0;
300 }
301
302
303 MathAtom createInsetMath(char const * const s)
304 {
305         return createInsetMath(from_utf8(s));
306 }
307
308
309 MathAtom createInsetMath(docstring const & s)
310 {
311         //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
312         latexkeys const * l = in_word_set(s);
313         if (l) {
314                 docstring const & inset = l->inset;
315                 //lyxerr << " found inset: '" << inset << '\'' << endl;
316                 if (inset == "ref")
317                         return MathAtom(new InsetMathRef(l->name));
318                 if (inset == "overset")
319                         return MathAtom(new InsetMathOverset);
320                 if (inset == "underset")
321                         return MathAtom(new InsetMathUnderset);
322                 if (inset == "decoration")
323                         return MathAtom(new InsetMathDecoration(l));
324                 if (inset == "space")
325                         return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
326                 if (inset == "dots")
327                         return MathAtom(new InsetMathDots(l));
328                 if (inset == "mbox")
329                         // return MathAtom(new InsetMathMBox);
330                         // InsetMathMBox is proposed to replace InsetMathBox,
331                         // but is not ready yet (it needs a BufferView for
332                         // construction)
333                         return MathAtom(new InsetMathBox(l->name));
334 //              if (inset == "fbox")
335 //                      return MathAtom(new InsetMathFBox(l));
336                 if (inset == "style")
337                         return MathAtom(new InsetMathSize(l));
338                 if (inset == "font")
339                         return MathAtom(new InsetMathFont(l));
340                 if (inset == "oldfont")
341                         return MathAtom(new InsetMathFontOld(l));
342                 if (inset == "matrix")
343                         return MathAtom(new InsetMathAMSArray(s));
344                 if (inset == "split")
345                         return MathAtom(new InsetMathSplit(s));
346                 if (inset == "big")
347                         // we can't create a InsetMathBig, since the argument
348                         // is missing.
349                         return MathAtom(new InsetMathUnknown(s));
350                 return MathAtom(new InsetMathSymbol(l));
351         }
352
353         if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
354                 return MathAtom(new MathMacroArgument(s[1] - '0'));
355         if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
356                         && s[2] >= '1' && s[2] <= '9')
357                 return MathAtom(new MathMacroArgument(s[2] - '0'));
358         if (s == "boxed")
359                 return MathAtom(new InsetMathBoxed());
360         if (s == "fbox")
361                 return MathAtom(new InsetMathFBox());
362         if (s == "framebox")
363                 return MathAtom(new InsetMathMakebox(true));
364         if (s == "makebox")
365                 return MathAtom(new InsetMathMakebox(false));
366         if (s == "kern")
367                 return MathAtom(new InsetMathKern);
368         if (s.substr(0, 8) == "xymatrix") {
369                 char spacing_code = '\0';
370                 Length spacing;
371                 size_t const len = s.length();
372                 size_t i = 8;
373                 if (i < len && s[i] == '@') {
374                         ++i;
375                         if (i < len) {
376                                 switch (s[i]) {
377                                 case 'R':
378                                 case 'C':
379                                 case 'M':
380                                 case 'W':
381                                 case 'H':
382                                 case 'L':
383                                         spacing_code = static_cast<char>(s[i]);
384                                         ++i;
385                                         break;
386                                 }
387                         }
388                         if (i < len && s[i] == '=') {
389                                 ++i;
390                                 spacing = Length(to_ascii(s.substr(i)));
391                         }
392                 }
393                 return MathAtom(new InsetMathXYMatrix(spacing, spacing_code));
394         }
395         if (s == "xrightarrow" || s == "xleftarrow")
396                 return MathAtom(new InsetMathXArrow(s));
397         if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
398                 return MathAtom(new InsetMathSplit(s));
399         if (s == "cases")
400                 return MathAtom(new InsetMathCases);
401         if (s == "substack")
402                 return MathAtom(new InsetMathSubstack);
403         if (s == "subarray" || s == "array")
404                 return MathAtom(new InsetMathArray(s, 1, 1));
405         if (s == "sqrt")
406                 return MathAtom(new InsetMathSqrt);
407         if (s == "root")
408                 return MathAtom(new InsetMathRoot);
409         if (s == "tabular")
410                 return MathAtom(new InsetMathTabular(s, 1, 1));
411         if (s == "stackrel")
412                 return MathAtom(new InsetMathStackrel);
413         if (s == "binom")
414                 return MathAtom(new InsetMathBinom(InsetMathBinom::BINOM));
415         if (s == "dbinom")
416                 return MathAtom(new InsetMathBinom(InsetMathBinom::DBINOM));
417         if (s == "tbinom")
418                 return MathAtom(new InsetMathBinom(InsetMathBinom::TBINOM));
419         if (s == "choose")
420                 return MathAtom(new InsetMathBinom(InsetMathBinom::CHOOSE));
421         if (s == "brace")
422                 return MathAtom(new InsetMathBinom(InsetMathBinom::BRACE));
423         if (s == "brack")
424                 return MathAtom(new InsetMathBinom(InsetMathBinom::BRACK));
425         if (s == "frac")
426                 return MathAtom(new InsetMathFrac);
427         if (s == "cfrac")
428                 return MathAtom(new InsetMathFrac(InsetMathFrac::CFRAC));
429         if (s == "dfrac")
430                 return MathAtom(new InsetMathFrac(InsetMathFrac::DFRAC));
431         if (s == "tfrac")
432                 return MathAtom(new InsetMathFrac(InsetMathFrac::TFRAC));
433         if (s == "over")
434                 return MathAtom(new InsetMathFrac(InsetMathFrac::OVER));
435         if (s == "nicefrac")
436                 return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC));
437         if (s == "unitfrac")
438                 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC));
439         // These string values are only for math toolbar use, no LaTeX names
440         if (s == "unitfracthree")
441                 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3));
442         if (s == "unitone")
443                 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1));
444         if (s == "unittwo")
445                 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT));
446         if (s == "cfracleft")
447                 return MathAtom(new InsetMathFrac(InsetMathFrac::CFRACLEFT));
448         if (s == "cfracright")
449                 return MathAtom(new InsetMathFrac(InsetMathFrac::CFRACRIGHT));
450         //if (s == "infer")
451         //      return MathAtom(new MathInferInset);
452         if (s == "atop")
453                 return MathAtom(new InsetMathFrac(InsetMathFrac::ATOP));
454         if (s == "lefteqn")
455                 return MathAtom(new InsetMathLefteqn);
456         if (s == "boldsymbol")
457                 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::AMS_BOLD));
458         if (s == "bm")
459                 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::BM_BOLD));
460         if (s == "heavysymbol" || s == "hm")
461                 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::BM_HEAVY));
462         if (s == "color" || s == "normalcolor")
463                 return MathAtom(new InsetMathColor(true));
464         if (s == "textcolor")
465                 return MathAtom(new InsetMathColor(false));
466         if (s == "hphantom")
467                 return MathAtom(new InsetMathPhantom(InsetMathPhantom::hphantom));
468         if (s == "phantom")
469                 return MathAtom(new InsetMathPhantom(InsetMathPhantom::phantom));
470         if (s == "vphantom")
471                 return MathAtom(new InsetMathPhantom(InsetMathPhantom::vphantom));
472         if (s == "ensuremath")
473                 return MathAtom(new InsetMathEnsureMath);
474         if (isSpecialChar(s))
475                 return MathAtom(new InsetMathSpecialChar(s));
476
477         if (s == "regexp")
478                 return MathAtom(new InsetMathHull(hullRegexp));
479
480         return MathAtom(new MathMacro(s));
481 }
482
483
484 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
485 {
486         // An example str:
487         // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
488         docstring name;
489         docstring body = split(str, name, ' ');
490
491         if (name == "ref") {
492                 InsetCommandParams icp(REF_CODE);
493                 // FIXME UNICODE
494                 InsetCommand::string2params("ref", to_utf8(str), icp);
495                 mathed_parse_cell(ar, icp.getCommand());
496         } else if (name == "mathspace") {
497                 InsetSpaceParams isp(true);
498                 InsetSpace::string2params(to_utf8(str), isp);
499                 InsetSpace is(isp);
500                 odocstringstream os;
501                 Encoding const * const ascii = encodings.fromLyXName("ascii");
502                 OutputParams op(ascii);
503                 is.latex(os, op);
504                 mathed_parse_cell(ar, os.str());
505                 if (ar.size() == 2) {
506                         // remove "{}"
507                         if (ar[1].nucleus()->asBraceInset())
508                                 ar.pop_back();
509                 }
510         } else
511                 return false;
512
513         if (ar.size() != 1)
514                 return false;
515
516         return ar[0].nucleus();
517 }
518
519
520 bool isAsciiOrMathAlpha(char_type c)
521 {
522         return c < 0x80 || Encodings::isMathAlpha(c);
523 }
524
525
526 } // namespace lyx