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