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