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