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