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