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