]> git.lyx.org Git - lyx.git/blob - src/mathed/math_factory.C
minimal effort implementation of:
[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_phantominset.h"
41 #include "math_rootinset.h"
42 #include "math_sizeinset.h"
43 #include "math_spaceinset.h"
44 #include "math_splitinset.h"
45 #include "math_sqrtinset.h"
46 #include "math_stackrelinset.h"
47 #include "math_substackinset.h"
48 #include "math_symbolinset.h"
49 #include "math_tabularinset.h"
50 #include "math_tfracinset.h"
51 #include "math_undersetinset.h"
52 #include "math_unknowninset.h"
53 #include "math_xarrowinset.h"
54 #include "math_xymatrixinset.h"
55
56 //#include "insets/insetref.h"
57 #include "ref_inset.h"
58
59 #include "debug.h"
60 #include "math_support.h"
61
62 #include "support/filetools.h" // LibFileSearch
63 #include "support/lstrings.h"
64
65 #include "frontends/lyx_gui.h"
66
67 #include <fstream>
68 #include <sstream>
69
70 using lyx::support::libFileSearch;
71 using lyx::support::split;
72
73 using std::string;
74 using std::endl;
75 using std::istringstream;
76
77 bool has_math_fonts;
78
79
80 namespace {
81
82 // file scope
83 typedef std::map<string, latexkeys> WordList;
84 WordList theWordList;
85
86
87 bool math_font_available(string & name)
88 {
89         LyXFont f;
90         augmentFont(f, name);
91
92         // Do we have the font proper?
93         if (lyx_gui::font_available(f))
94                 return true;
95
96         // can we fake it?
97         if (name == "eufrak") {
98                 name = "lyxfakefrak";
99                 return true;
100         }
101
102         lyxerr[Debug::MATHED]
103                 << "font " << name << " not available and I can't fake it"
104                 << endl;
105         return false;
106 }
107
108
109 void initSymbols()
110 {
111         string const filename = libFileSearch(string(), "symbols");
112         lyxerr[Debug::MATHED] << "read symbols from " << filename << endl;
113         if (filename.empty()) {
114                 lyxerr << "Could not find symbols file" << endl;
115                 return;
116         }
117
118         std::ifstream fs(filename.c_str());
119         string line;
120         bool skip = false;
121         while (getline(fs, line)) {
122                 int charid     = 0;
123                 int fallbackid = 0;
124                 if (!line.empty() && line[0] == '#')
125                         continue;
126
127                 // special case of iffont/else/endif
128                 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
129                         istringstream is(line);
130                         string tmp;
131                         is >> tmp;
132                         is >> tmp;
133                         skip = !math_font_available(tmp);
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                         MacroTable::globalMacros().insert(line);
147                         continue;
148                 }
149
150                 istringstream is(line);
151                 latexkeys tmp;
152                 is >> tmp.name >> tmp.inset;
153                 if (isFontName(tmp.inset))
154                         is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
155                 else
156                         is >> tmp.extra;
157                 if (!is) {
158                         lyxerr[Debug::MATHED] << "skipping line '" << line << '\'' << endl;
159                         lyxerr[Debug::MATHED]
160                                 << tmp.name << ' ' << tmp.inset << ' ' << tmp.extra << endl;
161                         continue;
162                 }
163
164                 if (isFontName(tmp.inset)) {
165                         // tmp.inset _is_ the fontname here.
166                         // create fallbacks if necessary
167
168                         // store requirements as long as we can
169                         if (tmp.inset == "msa" || tmp.inset == "msb")
170                                 tmp.requires = "amssymb";
171                         // See http://bugzilla.lyx.org/show_bug.cgi?id=1942
172                         // else if (tmp.inset == "wasy")
173                         //      tmp.requires = "wasysym";
174
175                         // symbol font is not available sometimes
176                         string symbol_font = "lyxsymbol";
177
178                         if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
179                                 lyxerr[Debug::MATHED] << "symbol abuse for " << tmp.name << endl;
180                                 tmp.draw = tmp.name;
181                         } else if (math_font_available(tmp.inset)) {
182                                 lyxerr[Debug::MATHED] << "symbol available for " << tmp.name << endl;
183                                 tmp.draw += char(charid);
184                         } else if (fallbackid && math_font_available(symbol_font)) {
185                                 if (tmp.inset == "cmex")
186                                         tmp.inset  = "lyxsymbol";
187                                 else
188                                         tmp.inset  = "lyxboldsymbol";
189                                 lyxerr[Debug::MATHED] << "symbol fallback for " << tmp.name << endl;
190                                 tmp.draw += char(fallbackid);
191                         } else {
192                                 lyxerr[Debug::MATHED] << "faking " << tmp.name << endl;
193                                 tmp.draw = tmp.name;
194                                 tmp.inset = "lyxtex";
195                         }
196                 } else {
197                         // it's a proper inset
198                         lyxerr[Debug::MATHED] << "inset " << tmp.inset
199                                               << " used for " << tmp.name
200                                               << endl;
201                 }
202
203                 if (theWordList.find(tmp.name) != theWordList.end())
204                         lyxerr[Debug::MATHED]
205                                 << "readSymbols: inset " << tmp.name
206                                 << " already exists." << endl;
207                 else
208                         theWordList[tmp.name] = tmp;
209
210                 lyxerr[Debug::MATHED]
211                         << "read symbol '" << tmp.name
212                         << "  inset: " << tmp.inset
213                         << "  draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
214                         << "  extra: " << tmp.extra
215                         << '\'' << endl;
216         }
217         string tmp = "cmm";
218         string tmp2 = "cmsy";
219         has_math_fonts = math_font_available(tmp) && math_font_available(tmp2);
220 }
221
222
223 } // namespace anon
224
225
226 void initMath()
227 {
228         static bool initialized = false;
229         if (!initialized) {
230                 initialized = true;
231                 initParser();
232                 initSymbols();
233         }
234 }
235
236
237 latexkeys const * in_word_set(string const & str)
238 {
239         WordList::iterator it = theWordList.find(str);
240         return it != theWordList.end() ? &(it->second) : 0;
241 }
242
243
244 MathAtom createMathInset(string const & s)
245 {
246         //lyxerr << "creating inset with name: '" << s << '\'' << endl;
247         latexkeys const * l = in_word_set(s);
248         if (l) {
249                 string const & inset = l->inset;
250                 //lyxerr << " found inset: '" << inset << '\'' << endl;
251                 if (inset == "ref")
252                         return MathAtom(new RefInset(l->name));
253                 if (inset == "overset")
254                         return MathAtom(new MathOversetInset);
255                 if (inset == "underset")
256                         return MathAtom(new MathUndersetInset);
257                 if (inset == "decoration")
258                         return MathAtom(new MathDecorationInset(l));
259                 if (inset == "space")
260                         return MathAtom(new MathSpaceInset(l->name));
261                 if (inset == "dots")
262                         return MathAtom(new MathDotsInset(l));
263                 if (inset == "mbox")
264                         // return MathAtom(new MathMBoxInset);
265                         // MathMBoxInset is proposed to replace MathBoxInset,
266                         // but is not ready yet (it needs a BufferView for
267                         // construction)
268                         return MathAtom(new MathBoxInset(l->name));
269 //              if (inset == "fbox")
270 //                      return MathAtom(new MathFboxInset(l));
271                 if (inset == "style")
272                         return MathAtom(new MathSizeInset(l));
273                 if (inset == "font")
274                         return MathAtom(new MathFontInset(l));
275                 if (inset == "oldfont")
276                         return MathAtom(new MathFontOldInset(l));
277                 if (inset == "matrix")
278                         return MathAtom(new MathAMSArrayInset(s));
279                 if (inset == "big")
280                         // we can't create a MathBigInset, since the argument
281                         // is missing.
282                         return MathAtom(new MathUnknownInset(s));
283                 return MathAtom(new MathSymbolInset(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 MathBoxedInset());
293         if (s == "fbox")
294                 return MathAtom(new MathFboxInset());
295         if (s == "framebox")
296                 return MathAtom(new MathFrameboxInset);
297         if (s == "makebox")
298                 return MathAtom(new MathMakeboxInset);
299         if (s == "kern")
300                 return MathAtom(new MathKernInset);
301         if (s == "xymatrix")
302                 return MathAtom(new MathXYMatrixInset);
303         if (s == "xrightarrow" || s == "xleftarrow")
304                 return MathAtom(new MathXArrowInset(s));
305         if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
306                 return MathAtom(new MathSplitInset(s));
307         if (s == "cases")
308                 return MathAtom(new MathCasesInset);
309         if (s == "substack")
310                 return MathAtom(new MathSubstackInset);
311         if (s == "subarray" || s == "array")
312                 return MathAtom(new MathArrayInset(s, 1, 1));
313         if (s == "sqrt")
314                 return MathAtom(new MathSqrtInset);
315         if (s == "root")
316                 return MathAtom(new MathRootInset);
317         if (s == "tabular")
318                 return MathAtom(new MathTabularInset(s, 1, 1));
319         if (s == "stackrel")
320                 return MathAtom(new MathStackrelInset);
321         if (s == "binom" || s == "choose")
322                 return MathAtom(new MathBinomInset(s == "choose"));
323         if (s == "frac")
324                 return MathAtom(new MathFracInset);
325         if (s == "over")
326                 return MathAtom(new MathFracInset(MathFracInset::OVER));
327         if (s == "nicefrac")
328                 return MathAtom(new MathFracInset(MathFracInset::NICEFRAC));
329         //if (s == "infer")
330         //      return MathAtom(new MathInferInset);
331         if (s == "atop")
332                 return MathAtom(new MathFracInset(MathFracInset::ATOP));
333         if (s == "lefteqn")
334                 return MathAtom(new MathLefteqnInset);
335         if (s == "boldsymbol")
336                 return MathAtom(new MathBoldsymbolInset);
337         if (s == "color" || s == "normalcolor")
338                 return MathAtom(new MathColorInset(true));
339         if (s == "textcolor")
340                 return MathAtom(new MathColorInset(false));
341         if (s == "dfrac")
342                 return MathAtom(new MathDfracInset);
343         if (s == "tfrac")
344                 return MathAtom(new MathTfracInset);
345         if (s == "hphantom")
346                 return MathAtom(new MathPhantomInset(MathPhantomInset::hphantom));
347         if (s == "phantom")
348                 return MathAtom(new MathPhantomInset(MathPhantomInset::phantom));
349         if (s == "vphantom")
350                 return MathAtom(new MathPhantomInset(MathPhantomInset::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 MathUnknownInset(s));
361 }
362
363
364 bool createMathInset_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 }