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