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