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