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