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