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