]> git.lyx.org Git - features.git/blob - src/mathed/MathFactory.cpp
Fix bug 5393
[features.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 "InsetMathXArrow.h"
45 #include "InsetMathXYMatrix.h"
46 #include "MacroTable.h"
47 #include "MathMacro.h"
48 #include "MathMacroArgument.h"
49 #include "MathParser.h"
50 #include "MathStream.h"
51 #include "MathSupport.h"
52
53 #include "insets/InsetCommand.h"
54
55 #include "support/debug.h"
56 #include "support/docstream.h"
57 #include "support/FileName.h"
58 #include "support/filetools.h" // LibFileSearch
59 #include "support/lstrings.h"
60
61 #include "frontends/FontLoader.h"
62
63 #include "LyX.h" // use_gui
64
65 using namespace std;
66 using namespace lyx::support;
67
68 namespace lyx {
69
70 bool has_math_fonts;
71
72
73 namespace {
74
75 MathWordList theWordList;
76
77
78 bool isMathFontAvailable(docstring & name)
79 {
80         if (!use_gui)
81                 return false;
82
83         FontInfo f;
84         augmentFont(f, name);
85
86         // Do we have the font proper?
87         if (theFontLoader().available(f))
88                 return true;
89
90         // can we fake it?
91         if (name == "eufrak") {
92                 name = from_ascii("lyxfakefrak");
93                 return true;
94         }
95
96         LYXERR(Debug::MATHED,
97                 "font " << to_utf8(name) << " not available and I can't fake it");
98         return false;
99 }
100
101
102 void initSymbols()
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 = !isMathFontAvailable(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 (isMathFontAvailable(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 && isMathFontAvailable(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 = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
219 }
220
221
222 bool isSpecialChar(docstring name)
223 {
224         if (name.size() != 1) {
225                 string const s = to_ascii(name);
226                 return  s == "textasciicircum" || s == "mathcircumflex" ||
227                         s == "textasciitilde"  || s == "textbackslash";
228         } else {
229                 char_type const c = name.at(0);
230                 return  c == '{' || c == '}' || c == '&' || c == '$' ||
231                         c == '#' || c == '%' || c == '_';
232         }
233 }
234
235
236 } // namespace anon
237
238 MathWordList const & mathedWordList()
239 {
240         return theWordList;
241 }
242
243
244 void initMath()
245 {
246         static bool initialized = false;
247         if (!initialized) {
248                 initialized = true;
249                 initParser();
250                 initSymbols();
251         }
252 }
253
254
255 bool ensureMath(WriteStream & os, bool needs_math_mode, bool macro)
256 {
257         bool brace = os.pendingBrace();
258         os.pendingBrace(false);
259         if (!os.latex())
260                 return brace;
261         if (os.textMode() && needs_math_mode) {
262                 os << "\\ensuremath{";
263                 os.textMode(false);
264                 brace = true;
265         } else if (macro && brace && !needs_math_mode) {
266                 // This is a user defined macro, but not a MathMacro, so we
267                 // cannot be sure what mode is needed. As it was entered in
268                 // a text box, we restore the text mode.
269                 os << '}';
270                 os.textMode(true);
271                 brace = false;
272         }
273         return brace;
274 }
275
276
277 bool ensureMode(WriteStream & os, InsetMath::mode_type mode)
278 {
279         bool textmode = mode == InsetMath::TEXT_MODE;
280         if (os.latex() && textmode && os.pendingBrace()) {
281                 os.os() << '}';
282                 os.pendingBrace(false);
283                 os.pendingSpace(false);
284                 os.textMode(true);
285         }
286         bool oldmode = os.textMode();
287         os.textMode(textmode);
288         return oldmode;
289 }
290
291
292 latexkeys const * in_word_set(docstring const & str)
293 {
294         MathWordList::iterator it = theWordList.find(str);
295         return it != theWordList.end() ? &(it->second) : 0;
296 }
297
298
299 MathAtom createInsetMath(char const * const s)
300 {
301         return createInsetMath(from_utf8(s));
302 }
303
304
305 MathAtom createInsetMath(docstring const & s)
306 {
307         //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
308         latexkeys const * l = in_word_set(s);
309         if (l) {
310                 docstring const & inset = l->inset;
311                 //lyxerr << " found inset: '" << inset << '\'' << endl;
312                 if (inset == "ref")
313                         return MathAtom(new InsetMathRef(l->name));
314                 if (inset == "overset")
315                         return MathAtom(new InsetMathOverset);
316                 if (inset == "underset")
317                         return MathAtom(new InsetMathUnderset);
318                 if (inset == "decoration")
319                         return MathAtom(new InsetMathDecoration(l));
320                 if (inset == "space")
321                         return MathAtom(new InsetMathSpace(l->name));
322                 if (inset == "dots")
323                         return MathAtom(new InsetMathDots(l));
324                 if (inset == "mbox")
325                         // return MathAtom(new InsetMathMBox);
326                         // InsetMathMBox is proposed to replace InsetMathBox,
327                         // but is not ready yet (it needs a BufferView for
328                         // construction)
329                         return MathAtom(new InsetMathBox(l->name));
330 //              if (inset == "fbox")
331 //                      return MathAtom(new InsetMathFBox(l));
332                 if (inset == "style")
333                         return MathAtom(new InsetMathSize(l));
334                 if (inset == "font")
335                         return MathAtom(new InsetMathFont(l));
336                 if (inset == "oldfont")
337                         return MathAtom(new InsetMathFontOld(l));
338                 if (inset == "matrix")
339                         return MathAtom(new InsetMathAMSArray(s));
340                 if (inset == "split")
341                         return MathAtom(new InsetMathSplit(s));
342                 if (inset == "big")
343                         // we can't create a InsetMathBig, since the argument
344                         // is missing.
345                         return MathAtom(new InsetMathUnknown(s));
346                 return MathAtom(new InsetMathSymbol(l));
347         }
348
349         if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
350                 return MathAtom(new MathMacroArgument(s[1] - '0'));
351         if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
352                         && s[2] >= '1' && s[2] <= '9')
353                 return MathAtom(new MathMacroArgument(s[2] - '0'));
354         if (s == "boxed")
355                 return MathAtom(new InsetMathBoxed());
356         if (s == "fbox")
357                 return MathAtom(new InsetMathFBox());
358         if (s == "framebox")
359                 return MathAtom(new InsetMathMakebox(true));
360         if (s == "makebox")
361                 return MathAtom(new InsetMathMakebox(false));
362         if (s == "kern")
363                 return MathAtom(new InsetMathKern);
364         if (s.substr(0, 8) == "xymatrix") {
365                 char spacing_code = '\0';
366                 Length spacing;
367                 size_t const len = s.length();
368                 size_t i = 8;
369                 if (i < len && s[i] == '@') {
370                         ++i;
371                         if (i < len) {
372                                 switch (s[i]) {
373                                 case 'R':
374                                 case 'C':
375                                 case 'M':
376                                 case 'W':
377                                 case 'H':
378                                 case 'L':
379                                         spacing_code = static_cast<char>(s[i]);
380                                         ++i;
381                                         break;
382                                 }
383                         }
384                         if (i < len && s[i] == '=') {
385                                 ++i;
386                                 spacing = Length(to_ascii(s.substr(i)));
387                         }
388                 }
389                 return MathAtom(new InsetMathXYMatrix(spacing, spacing_code));
390         }
391         if (s == "xrightarrow" || s == "xleftarrow")
392                 return MathAtom(new InsetMathXArrow(s));
393         if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
394                 return MathAtom(new InsetMathSplit(s));
395         if (s == "cases")
396                 return MathAtom(new InsetMathCases);
397         if (s == "substack")
398                 return MathAtom(new InsetMathSubstack);
399         if (s == "subarray" || s == "array")
400                 return MathAtom(new InsetMathArray(s, 1, 1));
401         if (s == "sqrt")
402                 return MathAtom(new InsetMathSqrt);
403         if (s == "root")
404                 return MathAtom(new InsetMathRoot);
405         if (s == "tabular")
406                 return MathAtom(new InsetMathTabular(s, 1, 1));
407         if (s == "stackrel")
408                 return MathAtom(new InsetMathStackrel);
409         if (s == "binom")
410                 return MathAtom(new InsetMathBinom(InsetMathBinom::BINOM));
411         if (s == "choose")
412                 return MathAtom(new InsetMathBinom(InsetMathBinom::CHOOSE));
413         if (s == "brace")
414                 return MathAtom(new InsetMathBinom(InsetMathBinom::BRACE));
415         if (s == "brack")
416                 return MathAtom(new InsetMathBinom(InsetMathBinom::BRACK));
417         if (s == "frac")
418                 return MathAtom(new InsetMathFrac);
419         if (s == "over")
420                 return MathAtom(new InsetMathFrac(InsetMathFrac::OVER));
421         if (s == "nicefrac")
422                 return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC));
423         if (s == "unitfrac")
424                 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC));
425         // This string value is only for math toolbar use. Not a LaTeX name
426         if (s == "unitfracthree")
427                 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3));
428         if (s == "unitone")
429                 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1));
430         if (s == "unittwo")
431                 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT));
432         //if (s == "infer")
433         //      return MathAtom(new MathInferInset);
434         if (s == "atop")
435                 return MathAtom(new InsetMathFrac(InsetMathFrac::ATOP));
436         if (s == "lefteqn")
437                 return MathAtom(new InsetMathLefteqn);
438         if (s == "boldsymbol")
439                 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::AMS_BOLD));
440         if (s == "bm")
441                 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::BM_BOLD));
442         if (s == "heavysymbol" || s == "hm")
443                 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::BM_HEAVY));
444         if (s == "color" || s == "normalcolor")
445                 return MathAtom(new InsetMathColor(true));
446         if (s == "textcolor")
447                 return MathAtom(new InsetMathColor(false));
448         if (s == "dfrac")
449                 return MathAtom(new InsetMathDFrac);
450         if (s == "tfrac")
451                 return MathAtom(new InsetMathTFrac);
452         if (s == "dbinom")
453                 return MathAtom(new InsetMathDBinom);
454         if (s == "tbinom")
455                 return MathAtom(new InsetMathTBinom);
456         if (s == "hphantom")
457                 return MathAtom(new InsetMathPhantom(InsetMathPhantom::hphantom));
458         if (s == "phantom")
459                 return MathAtom(new InsetMathPhantom(InsetMathPhantom::phantom));
460         if (s == "vphantom")
461                 return MathAtom(new InsetMathPhantom(InsetMathPhantom::vphantom));
462         if (s == "ensuremath")
463                 return MathAtom(new InsetMathEnsureMath);
464         if (isSpecialChar(s))
465                 return MathAtom(new InsetMathSpecialChar(s));
466
467         return MathAtom(new MathMacro(s));
468 }
469
470
471 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
472 {
473         // An example str:
474         // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
475         docstring name;
476         docstring body = split(str, name, ' ');
477
478         if (name != "ref" )
479                 return false;
480
481         InsetCommandParams icp(REF_CODE);
482         // FIXME UNICODE
483         InsetCommand::string2params("ref", to_utf8(str), icp);
484         mathed_parse_cell(ar, icp.getCommand());
485         if (ar.size() != 1)
486                 return false;
487
488         return ar[0].nucleus();
489 }
490
491
492 } // namespace lyx