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