]> git.lyx.org Git - lyx.git/blob - src/mathed/MathFactory.cpp
b1e73b2a3a3ed38bb1232f2159c909310a7d8aed
[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 "InsetMathBM.h"
18 #include "InsetMathBoldSymbol.h"
19 #include "InsetMathBox.h"
20 #include "InsetMathCases.h"
21 #include "InsetMathColor.h"
22 #include "InsetMathDecoration.h"
23 #include "InsetMathDots.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 "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 latexkeys const * in_word_set(docstring const & str)
240 {
241         MathWordList::iterator it = theWordList.find(str);
242         return it != theWordList.end() ? &(it->second) : 0;
243 }
244
245
246 MathAtom createInsetMath(char const * const s)
247 {
248         return createInsetMath(from_utf8(s));
249 }
250
251
252 MathAtom createInsetMath(docstring const & s)
253 {
254         //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
255         latexkeys const * l = in_word_set(s);
256         if (l) {
257                 docstring const & inset = l->inset;
258                 //lyxerr << " found inset: '" << inset << '\'' << endl;
259                 if (inset == "ref")
260                         return MathAtom(new InsetMathRef(l->name));
261                 if (inset == "overset")
262                         return MathAtom(new InsetMathOverset);
263                 if (inset == "underset")
264                         return MathAtom(new InsetMathUnderset);
265                 if (inset == "decoration")
266                         return MathAtom(new InsetMathDecoration(l));
267                 if (inset == "space")
268                         return MathAtom(new InsetMathSpace(l->name));
269                 if (inset == "dots")
270                         return MathAtom(new InsetMathDots(l));
271                 if (inset == "mbox")
272                         // return MathAtom(new InsetMathMBox);
273                         // InsetMathMBox is proposed to replace InsetMathBox,
274                         // but is not ready yet (it needs a BufferView for
275                         // construction)
276                         return MathAtom(new InsetMathBox(l->name));
277 //              if (inset == "fbox")
278 //                      return MathAtom(new InsetMathFBox(l));
279                 if (inset == "style")
280                         return MathAtom(new InsetMathSize(l));
281                 if (inset == "font")
282                         return MathAtom(new InsetMathFont(l));
283                 if (inset == "oldfont")
284                         return MathAtom(new InsetMathFontOld(l));
285                 if (inset == "matrix")
286                         return MathAtom(new InsetMathAMSArray(s));
287                 if (inset == "split")
288                         return MathAtom(new InsetMathSplit(s));
289                 if (inset == "big")
290                         // we can't create a InsetMathBig, since the argument
291                         // is missing.
292                         return MathAtom(new InsetMathUnknown(s));
293                 return MathAtom(new InsetMathSymbol(l));
294         }
295
296         if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
297                 return MathAtom(new MathMacroArgument(s[1] - '0'));
298         if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
299                         && s[2] >= '1' && s[2] <= '9')
300                 return MathAtom(new MathMacroArgument(s[2] - '0'));
301         if (s == "boxed")
302                 return MathAtom(new InsetMathBoxed());
303         if (s == "fbox")
304                 return MathAtom(new InsetMathFBox());
305         if (s == "framebox")
306                 return MathAtom(new InsetMathMakebox(true));
307         if (s == "makebox")
308                 return MathAtom(new InsetMathMakebox(false));
309         if (s == "kern")
310                 return MathAtom(new InsetMathKern);
311         if (s.substr(0, 8) == "xymatrix") {
312                 char spacing_code = '\0';
313                 Length spacing;
314                 size_t const len = s.length();
315                 size_t i = 8;
316                 if (i < len && s[i] == '@') {
317                         ++i;
318                         if (i < len) {
319                                 switch (s[i]) {
320                                 case 'R':
321                                 case 'C':
322                                 case 'M':
323                                 case 'W':
324                                 case 'H':
325                                 case 'L':
326                                         spacing_code = static_cast<char>(s[i]);
327                                         ++i;
328                                         break;
329                                 }
330                         }
331                         if (i < len && s[i] == '=') {
332                                 ++i;
333                                 spacing = Length(to_ascii(s.substr(i)));
334                         }
335                 }
336                 return MathAtom(new InsetMathXYMatrix(spacing, spacing_code));
337         }
338         if (s == "xrightarrow" || s == "xleftarrow")
339                 return MathAtom(new InsetMathXArrow(s));
340         if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
341                 return MathAtom(new InsetMathSplit(s));
342         if (s == "cases")
343                 return MathAtom(new InsetMathCases);
344         if (s == "substack")
345                 return MathAtom(new InsetMathSubstack);
346         if (s == "subarray" || s == "array")
347                 return MathAtom(new InsetMathArray(s, 1, 1));
348         if (s == "sqrt")
349                 return MathAtom(new InsetMathSqrt);
350         if (s == "root")
351                 return MathAtom(new InsetMathRoot);
352         if (s == "tabular")
353                 return MathAtom(new InsetMathTabular(s, 1, 1));
354         if (s == "stackrel")
355                 return MathAtom(new InsetMathStackrel);
356         if (s == "binom" || s == "choose")
357                 return MathAtom(new InsetMathBinom(s == "choose"));
358         if (s == "frac")
359                 return MathAtom(new InsetMathFrac);
360         if (s == "over")
361                 return MathAtom(new InsetMathFrac(InsetMathFrac::OVER));
362         if (s == "nicefrac")
363                 return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC));
364         if (s == "unitfrac")
365                 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC));
366         // This string value is only for math toolbar use. Not a LaTeX name
367         if (s == "unitfracthree")
368                 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3));
369         if (s == "unitone")
370                 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1));
371         if (s == "unittwo")
372                 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT));
373         //if (s == "infer")
374         //      return MathAtom(new MathInferInset);
375         if (s == "atop")
376                 return MathAtom(new InsetMathFrac(InsetMathFrac::ATOP));
377         if (s == "lefteqn")
378                 return MathAtom(new InsetMathLefteqn);
379         if (s == "boldsymbol")
380                 return MathAtom(new InsetMathBoldSymbol);
381         if (s == "bm")
382                 return MathAtom(new InsetMathBM);
383         if (s == "color" || s == "normalcolor")
384                 return MathAtom(new InsetMathColor(true));
385         if (s == "textcolor")
386                 return MathAtom(new InsetMathColor(false));
387         if (s == "dfrac")
388                 return MathAtom(new InsetMathDFrac);
389         if (s == "tfrac")
390                 return MathAtom(new InsetMathTFrac);
391         if (s == "dbinom")
392                 return MathAtom(new InsetMathDBinom);
393         if (s == "tbinom")
394                 return MathAtom(new InsetMathTBinom);
395         if (s == "hphantom")
396                 return MathAtom(new InsetMathPhantom(InsetMathPhantom::hphantom));
397         if (s == "phantom")
398                 return MathAtom(new InsetMathPhantom(InsetMathPhantom::phantom));
399         if (s == "vphantom")
400                 return MathAtom(new InsetMathPhantom(InsetMathPhantom::vphantom));
401
402         return MathAtom(new MathMacro(s));
403 }
404
405
406 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
407 {
408         // An example str:
409         // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
410         docstring name;
411         docstring body = split(str, name, ' ');
412
413         if (name != "ref" )
414                 return false;
415
416         InsetCommandParams icp(REF_CODE);
417         // FIXME UNICODE
418         InsetCommandMailer::string2params("ref", to_utf8(str), icp);
419         mathed_parse_cell(ar, icp.getCommand());
420         if (ar.size() != 1)
421                 return false;
422
423         return ar[0].nucleus();
424 }
425
426
427 } // namespace lyx