]> git.lyx.org Git - lyx.git/blob - src/mathed/math_factory.C
bug + spped fixes + small stuff
[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_boldsymbolinset.h"
20 #include "math_casesinset.h"
21 #include "math_colorinset.h"
22 #include "math_decorationinset.h"
23 #include "math_dfracinset.h"
24 #include "math_dotsinset.h"
25 #include "math_ertinset.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_parboxinset.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_undersetinset.h"
50 #include "math_unknowninset.h"
51 #include "math_xarrowinset.h"
52
53 //#include "insets/insetref.h"
54 #include "ref_inset.h"
55
56 #include "debug.h"
57 #include "math_support.h"
58 #include "support/std_sstream.h"
59 #include "support/filetools.h" // LibFileSearch
60 #include "support/lstrings.h"
61 #include "frontends/lyx_gui.h"
62
63 #include <fstream>
64
65 using lyx::support::LibFileSearch;
66 using lyx::support::split;
67
68 using std::string;
69 using std::endl;
70 using std::istringstream;
71
72 bool has_math_fonts;
73
74
75 namespace {
76
77 // file scope
78 typedef std::map<string, latexkeys> WordList;
79 WordList theWordList;
80
81
82 bool math_font_available(string & name)
83 {
84         LyXFont f;
85         augmentFont(f, name);
86
87         // Do we have the font proper?
88         if (lyx_gui::font_available(f))
89                 return true;
90
91         // can we fake it?
92         if (name == "eufrak") {
93                 name = "lyxfakefrak";
94                 return true;
95         }
96
97         lyxerr[Debug::MATHED]
98                 << "font " << name << " not available and I can't fake it"
99                 << endl;
100         return false;
101 }
102
103
104 void initSymbols()
105 {
106         string const filename = LibFileSearch(string(), "symbols");
107         lyxerr[Debug::MATHED] << "read symbols from " << filename << endl;
108         if (filename.empty()) {
109                 lyxerr << "Could not find symbols file" << endl;
110                 return;
111         }
112
113         std::ifstream fs(filename.c_str());
114         string line;
115         bool skip = false;
116         while (getline(fs, line)) {
117                 int charid     = 0;
118                 int fallbackid = 0;
119                 if (!line.empty() && line[0] == '#')
120                         continue;
121
122                 // special case of iffont/else/endif
123                 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
124                         istringstream is(line);
125                         string tmp;
126                         is >> tmp;
127                         is >> tmp;
128                         skip = !math_font_available(tmp);
129                         continue;
130                 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
131                         skip = !skip;
132                 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
133                         skip = false;
134                         continue;
135                 } else if (skip)
136                         continue;
137
138                 // special case of pre-defined macros
139                 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
140                         //lyxerr << "defining: '" << line << '\'' << endl;
141                         istringstream is(line);
142                         MathMacroTable::create(MathAtom(new MathMacroTemplate(is)));
143                         continue;
144                 }
145
146                 istringstream is(line);
147                 latexkeys tmp;
148                 is >> tmp.name >> tmp.inset;
149                 if (isFontName(tmp.inset))
150                         is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
151                 else
152                         is >> tmp.extra;
153                 if (!is) {
154                         lyxerr[Debug::MATHED] << "skipping line '" << line << '\'' << endl;
155                         lyxerr[Debug::MATHED]
156                                 << tmp.name << ' ' << tmp.inset << ' ' << tmp.extra << endl;
157                         continue;
158                 }
159
160                 if (isFontName(tmp.inset)) {
161                         // tmp.inset _is_ the fontname here.
162                         // create fallbacks if necessary
163
164                         // symbol font is not available sometimes
165                         string symbol_font = "lyxsymbol";
166
167                         if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
168                                 lyxerr[Debug::MATHED] << "symbol abuse for " << tmp.name << endl;
169                                 tmp.draw = tmp.name;
170                         } else if (math_font_available(tmp.inset)) {
171                                 lyxerr[Debug::MATHED] << "symbol available for " << tmp.name << endl;
172                                 tmp.draw += char(charid);
173                         } else if (fallbackid && math_font_available(symbol_font)) {
174                                 if (tmp.inset == "cmex")
175                                         tmp.inset  = "lyxsymbol";
176                                 else
177                                         tmp.inset  = "lyxboldsymbol";
178                                 lyxerr[Debug::MATHED] << "symbol fallback for " << tmp.name << endl;
179                                 tmp.draw += char(fallbackid);
180                         } else {
181                                 lyxerr[Debug::MATHED] << "faking " << tmp.name << endl;
182                                 tmp.draw = tmp.name;
183                                 tmp.inset = "lyxtex";
184                         }
185                 } else {
186                         // it's a proper inset
187                         lyxerr[Debug::MATHED] << "inset " << tmp.inset
188                                               << " used for " << tmp.name
189                                               << endl;
190                 }
191
192                 if (theWordList.find(tmp.name) != theWordList.end())
193                         lyxerr[Debug::MATHED]
194                                 << "readSymbols: inset " << tmp.name
195                                 << " already exists." << endl;
196                 else
197                         theWordList[tmp.name] = tmp;
198
199                 lyxerr[Debug::MATHED]
200                         << "read symbol '" << tmp.name
201                         << "  inset: " << tmp.inset
202                         << "  draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
203                         << "  extra: " << tmp.extra
204                         << '\'' << endl;
205         }
206         string tmp = "cmm";
207         string tmp2 = "cmsy";
208         has_math_fonts = math_font_available(tmp) && math_font_available(tmp2);
209 }
210
211
212 } // namespace anon
213
214
215 void initMath()
216 {
217         static bool initialized = false;
218         if (!initialized) {
219                 initSymbols();
220                 initialized = true;
221         }
222 }
223
224
225 latexkeys const * in_word_set(string const & str)
226 {
227         WordList::iterator it = theWordList.find(str);
228         //lyxerr << "looking up '" << str << "' found: "
229         // << (it != theWordList.end()) << endl;
230         return (it != theWordList.end()) ? &(it->second) : 0;
231 }
232
233
234 MathAtom createMathInset(string const & s)
235 {
236         //lyxerr << "creating inset with name: '" << s << '\'' << endl;;
237         latexkeys const * l = in_word_set(s);
238         if (l) {
239                 string const & inset = l->inset;
240                 //lyxerr << " found inset: '" << inset << '\'' << endl;
241                 if (inset == "ref")
242                         return MathAtom(new RefInset(l->name));
243                 if (inset == "overset")
244                         return MathAtom(new MathOversetInset);
245                 if (inset == "underset")
246                         return MathAtom(new MathUndersetInset);
247                 if (inset == "decoration")
248                         return MathAtom(new MathDecorationInset(l));
249                 if (inset == "space")
250                         return MathAtom(new MathSpaceInset(l->name));
251                 if (inset == "dots")
252                         return MathAtom(new MathDotsInset(l));
253                 if (inset == "mbox")
254                         return MathAtom(new MathBoxInset(l->name));
255                 if (inset == "parbox")
256                         return MathAtom(new MathParboxInset);
257                 if (inset == "fbox")
258                         return MathAtom(new MathFboxInset(l));
259                 if (inset == "style")
260                         return MathAtom(new MathSizeInset(l));
261                 if (inset == "font")
262                         return MathAtom(new MathFontInset(l));
263                 if (inset == "oldfont")
264                         return MathAtom(new MathFontOldInset(l));
265                 if (inset == "matrix")
266                         return MathAtom(new MathAMSArrayInset(s));
267                 return MathAtom(new MathSymbolInset(l));
268         }
269
270         if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
271                 return MathAtom(new MathMacroArgument(s[1] - '0'));
272         if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
273                         && s[2] >= '1' && s[2] <= '9')
274                 return MathAtom(new MathMacroArgument(s[2] - '0'));
275         if (s == "framebox")
276                 return MathAtom(new MathFrameboxInset);
277         if (s == "makebox")
278                 return MathAtom(new MathMakeboxInset);
279         if (s == "kern")
280                 return MathAtom(new MathKernInset);
281         if (s == "xrightarrow" || s == "xleftarrow")
282                 return MathAtom(new MathXArrowInset(s));
283         if (s == "split" || s == "gathered" || s == "aligned")
284                 return MathAtom(new MathSplitInset(s));
285         if (s == "cases")
286                 return MathAtom(new MathCasesInset);
287         if (s == "substack")
288                 return MathAtom(new MathSubstackInset);
289         if (s == "subarray" || s == "array")
290                 return MathAtom(new MathArrayInset(s, 1, 1));
291         if (s == "sqrt")
292                 return MathAtom(new MathSqrtInset);
293         if (s == "root")
294                 return MathAtom(new MathRootInset);
295         if (s == "tabular")
296                 return MathAtom(new MathTabularInset(s, 1, 1));
297         if (s == "stackrel")
298                 return MathAtom(new MathStackrelInset);
299         if (s == "binom" || s == "choose")
300                 return MathAtom(new MathBinomInset(s == "choose"));
301         if (s == "over" || s == "frac")
302                 return MathAtom(new MathFracInset);
303         //if (s == "infer")
304         //      return MathAtom(new MathInferInset);
305         if (s == "atop")
306                 return MathAtom(new MathFracInset(true));
307         if (s == "lefteqn")
308                 return MathAtom(new MathLefteqnInset);
309         if (s == "lyxert")
310                 return MathAtom(new MathErtInset);
311         if (s == "boldsymbol")
312                 return MathAtom(new MathBoldsymbolInset);
313         if (s == "color")
314                 return MathAtom(new MathColorInset(true));
315         if (s == "textcolor")
316                 return MathAtom(new MathColorInset(false));
317         if (s == "dfrac")
318                 return MathAtom(new MathDfracInset);
319
320         if (MathMacroTable::has(s))
321                 return MathAtom(new MathMacro(s));
322
323         //lyxerr << "creating inset 2 with name: '" << s << '\'' << endl;
324         return MathAtom(new MathUnknownInset(s));
325 }
326
327
328 bool createMathInset_fromDialogStr(string const & str, MathArray & ar)
329 {
330         // An example str:
331         // "ref LatexCommand \\ref{sec:Title}\n\\end_inset\n\n";
332         string name;
333         string body = split(str, name, ' ');
334
335         if (name != "ref" )
336                 return false;
337
338         // body comes with a head "LatexCommand " and a
339         // tail "\nend_inset\n\n". Strip them off.
340         string trimmed;
341         body = split(body, trimmed, ' ');
342         split(body, trimmed, '\n');
343
344         mathed_parse_cell(ar, trimmed);
345         if (ar.size() != 1)
346                 return false;
347
348         return ar[0].nucleus();
349 }