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