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