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