]> git.lyx.org Git - lyx.git/blob - src/mathed/math_factory.C
c865d52e0aad230100bff7ad86729594a0512693
[lyx.git] / src / mathed / math_factory.C
1 /**
2  * \file math_factory.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 "math_factory.h"
14 #include "math_parser.h"
15 #include "math_arrayinset.h"
16 #include "math_amsarrayinset.h"
17 #include "math_binominset.h"
18 #include "math_boxinset.h"
19 #include "math_boxedinset.h"
20 #include "math_boldsymbolinset.h"
21 #include "math_casesinset.h"
22 #include "math_colorinset.h"
23 #include "math_decorationinset.h"
24 #include "math_dfracinset.h"
25 #include "math_dotsinset.h"
26 #include "math_fboxinset.h"
27 #include "math_frameboxinset.h"
28 #include "math_fontinset.h"
29 #include "math_fontoldinset.h"
30 #include "math_fracinset.h"
31 #include "math_kerninset.h"
32 #include "math_lefteqninset.h"
33 #include "math_macro.h"
34 #include "math_macroarg.h"
35 #include "math_macrotable.h"
36 #include "math_macrotemplate.h"
37 #include "math_makeboxinset.h"
38 #include "math_oversetinset.h"
39 #include "math_parser.h"
40 #include "math_rootinset.h"
41 #include "math_sizeinset.h"
42 #include "math_spaceinset.h"
43 #include "math_splitinset.h"
44 #include "math_sqrtinset.h"
45 #include "math_stackrelinset.h"
46 #include "math_substackinset.h"
47 #include "math_symbolinset.h"
48 #include "math_tabularinset.h"
49 #include "math_tfracinset.h"
50 #include "math_undersetinset.h"
51 #include "math_unknowninset.h"
52 #include "math_xarrowinset.h"
53
54 //#include "insets/insetref.h"
55 #include "ref_inset.h"
56
57 #include "debug.h"
58 #include "math_support.h"
59
60 #include "support/filetools.h" // LibFileSearch
61 #include "support/lstrings.h"
62
63 #include "frontends/lyx_gui.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 (lyx_gui::font_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                         else if (tmp.inset == "wasy")
170                                 tmp.requires = "wasysym";
171
172                         // symbol font is not available sometimes
173                         string symbol_font = "lyxsymbol";
174
175                         if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
176                                 lyxerr[Debug::MATHED] << "symbol abuse for " << tmp.name << endl;
177                                 tmp.draw = tmp.name;
178                         } else if (math_font_available(tmp.inset)) {
179                                 lyxerr[Debug::MATHED] << "symbol available for " << tmp.name << endl;
180                                 tmp.draw += char(charid);
181                         } else if (fallbackid && math_font_available(symbol_font)) {
182                                 if (tmp.inset == "cmex")
183                                         tmp.inset  = "lyxsymbol";
184                                 else
185                                         tmp.inset  = "lyxboldsymbol";
186                                 lyxerr[Debug::MATHED] << "symbol fallback for " << tmp.name << endl;
187                                 tmp.draw += char(fallbackid);
188                         } else {
189                                 lyxerr[Debug::MATHED] << "faking " << tmp.name << endl;
190                                 tmp.draw = tmp.name;
191                                 tmp.inset = "lyxtex";
192                         }
193                 } else {
194                         // it's a proper inset
195                         lyxerr[Debug::MATHED] << "inset " << tmp.inset
196                                               << " used for " << tmp.name
197                                               << endl;
198                 }
199
200                 if (theWordList.find(tmp.name) != theWordList.end())
201                         lyxerr[Debug::MATHED]
202                                 << "readSymbols: inset " << tmp.name
203                                 << " already exists." << endl;
204                 else
205                         theWordList[tmp.name] = tmp;
206
207                 lyxerr[Debug::MATHED]
208                         << "read symbol '" << tmp.name
209                         << "  inset: " << tmp.inset
210                         << "  draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
211                         << "  extra: " << tmp.extra
212                         << '\'' << endl;
213         }
214         string tmp = "cmm";
215         string tmp2 = "cmsy";
216         has_math_fonts = math_font_available(tmp) && math_font_available(tmp2);
217 }
218
219
220 } // namespace anon
221
222
223 void initMath()
224 {
225         static bool initialized = false;
226         if (!initialized) {
227                 initParser();
228                 initSymbols();
229                 initialized = true;
230         }
231 }
232
233
234 latexkeys const * in_word_set(string const & str)
235 {
236         WordList::iterator it = theWordList.find(str);
237         return it != theWordList.end() ? &(it->second) : 0;
238 }
239
240
241 MathAtom createMathInset(string const & s)
242 {
243         //lyxerr << "creating inset with name: '" << s << '\'' << endl;
244         latexkeys const * l = in_word_set(s);
245         if (l) {
246                 string const & inset = l->inset;
247                 //lyxerr << " found inset: '" << inset << '\'' << endl;
248                 if (inset == "ref")
249                         return MathAtom(new RefInset(l->name));
250                 if (inset == "overset")
251                         return MathAtom(new MathOversetInset);
252                 if (inset == "underset")
253                         return MathAtom(new MathUndersetInset);
254                 if (inset == "decoration")
255                         return MathAtom(new MathDecorationInset(l));
256                 if (inset == "space")
257                         return MathAtom(new MathSpaceInset(l->name));
258                 if (inset == "dots")
259                         return MathAtom(new MathDotsInset(l));
260                 if (inset == "mbox")
261                         // return MathAtom(new MathMBoxInset);
262                         // MathMBoxInset is proposed to replace MathBoxInset,
263                         // but is not ready yet (it needs a BufferView for
264                         // construction)
265                         return MathAtom(new MathBoxInset(l->name));
266 //              if (inset == "fbox")
267 //                      return MathAtom(new MathFboxInset(l));
268                 if (inset == "style")
269                         return MathAtom(new MathSizeInset(l));
270                 if (inset == "font")
271                         return MathAtom(new MathFontInset(l));
272                 if (inset == "oldfont")
273                         return MathAtom(new MathFontOldInset(l));
274                 if (inset == "matrix")
275                         return MathAtom(new MathAMSArrayInset(s));
276                 return MathAtom(new MathSymbolInset(l));
277         }
278
279         if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
280                 return MathAtom(new MathMacroArgument(s[1] - '0'));
281         if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
282                         && s[2] >= '1' && s[2] <= '9')
283                 return MathAtom(new MathMacroArgument(s[2] - '0'));
284         if (s == "boxed")
285                 return MathAtom(new MathBoxedInset());
286         if (s == "fbox")
287                 return MathAtom(new MathFboxInset());
288         if (s == "framebox")
289                 return MathAtom(new MathFrameboxInset);
290         if (s == "makebox")
291                 return MathAtom(new MathMakeboxInset);
292         if (s == "kern")
293                 return MathAtom(new MathKernInset);
294         if (s == "xrightarrow" || s == "xleftarrow")
295                 return MathAtom(new MathXArrowInset(s));
296         if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
297                 return MathAtom(new MathSplitInset(s));
298         if (s == "cases")
299                 return MathAtom(new MathCasesInset);
300         if (s == "substack")
301                 return MathAtom(new MathSubstackInset);
302         if (s == "subarray" || s == "array")
303                 return MathAtom(new MathArrayInset(s, 1, 1));
304         if (s == "sqrt")
305                 return MathAtom(new MathSqrtInset);
306         if (s == "root")
307                 return MathAtom(new MathRootInset);
308         if (s == "tabular")
309                 return MathAtom(new MathTabularInset(s, 1, 1));
310         if (s == "stackrel")
311                 return MathAtom(new MathStackrelInset);
312         if (s == "binom" || s == "choose")
313                 return MathAtom(new MathBinomInset(s == "choose"));
314         if (s == "over" || s == "frac")
315                 return MathAtom(new MathFracInset);
316         //if (s == "infer")
317         //      return MathAtom(new MathInferInset);
318         if (s == "atop")
319                 return MathAtom(new MathFracInset(true));
320         if (s == "lefteqn")
321                 return MathAtom(new MathLefteqnInset);
322         if (s == "boldsymbol")
323                 return MathAtom(new MathBoldsymbolInset);
324         if (s == "color" || s == "normalcolor")
325                 return MathAtom(new MathColorInset(true));
326         if (s == "textcolor")
327                 return MathAtom(new MathColorInset(false));
328         if (s == "dfrac")
329                 return MathAtom(new MathDfracInset);
330         if (s == "tfrac")
331                 return MathAtom(new MathTfracInset);
332
333         if (MacroTable::globalMacros().has(s))
334                 return MathAtom(new MathMacro(s,
335                         MacroTable::globalMacros().get(s).numargs()));
336         //if (MacroTable::localMacros().has(s))
337         //      return MathAtom(new MathMacro(s,
338         //              MacroTable::localMacros().get(s).numargs()));
339
340         //lyxerr << "creating unknown inset '" << s << "'" << endl;
341         return MathAtom(new MathUnknownInset(s));
342 }
343
344
345 bool createMathInset_fromDialogStr(string const & str, MathArray & ar)
346 {
347         // An example str:
348         // "ref LatexCommand \\ref{sec:Title}\n\\end_inset\n\n";
349         string name;
350         string body = split(str, name, ' ');
351
352         if (name != "ref" )
353                 return false;
354
355         // body comes with a head "LatexCommand " and a
356         // tail "\nend_inset\n\n". Strip them off.
357         string trimmed;
358         body = split(body, trimmed, ' ');
359         split(body, trimmed, '\n');
360
361         mathed_parse_cell(ar, trimmed);
362         if (ar.size() != 1)
363                 return false;
364
365         return ar[0].nucleus();
366 }
367