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