]> git.lyx.org Git - lyx.git/blob - src/mathed/math_factory.C
Get rid of lyxstring, remove usage of STRCONV.
[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_decorationinset.h"
22 #include "math_dotsinset.h"
23 #include "math_ertinset.h"
24 #include "math_fboxinset.h"
25 #include "math_frameboxinset.h"
26 #include "math_fontinset.h"
27 #include "math_fontoldinset.h"
28 #include "math_fracinset.h"
29 #include "math_kerninset.h"
30 #include "math_lefteqninset.h"
31 #include "math_macro.h"
32 #include "math_macroarg.h"
33 #include "math_macrotable.h"
34 #include "math_macrotemplate.h"
35 #include "math_makeboxinset.h"
36 #include "math_oversetinset.h"
37 #include "math_parboxinset.h"
38 #include "math_rootinset.h"
39 #include "math_sizeinset.h"
40 #include "math_spaceinset.h"
41 #include "math_splitinset.h"
42 #include "math_sqrtinset.h"
43 #include "math_stackrelinset.h"
44 #include "math_substackinset.h"
45 #include "math_symbolinset.h"
46 #include "math_tabularinset.h"
47 #include "math_undersetinset.h"
48 #include "math_unknowninset.h"
49 #include "math_xarrowinset.h"
50
51 //#include "insets/insetref.h"
52 #include "ref_inset.h"
53
54 #include "debug.h"
55 #include "math_support.h"
56 #include "support/std_sstream.h"
57 #include "support/filetools.h" // LibFileSearch
58 #include "support/lstrings.h"
59 #include "frontends/lyx_gui.h"
60
61 #include <fstream>
62
63 using lyx::support::LibFileSearch;
64 using lyx::support::split;
65
66 using std::endl;
67 using std::istringstream;
68
69 bool has_math_fonts;
70
71
72 namespace {
73
74 // file scope
75 typedef std::map<string, latexkeys> WordList;
76 WordList theWordList;
77
78
79 bool math_font_available(string & name)
80 {
81         LyXFont f;
82         augmentFont(f, name);
83
84         // Do we have the font proper?
85         if (lyx_gui::font_available(f))
86                 return true;
87
88         // can we fake it?
89         if (name == "eufrak") {
90                 name = "lyxfakefrak";
91                 return true;
92         }
93
94         lyxerr[Debug::MATHED]
95                 << "font " << name << " not available and I can't fake it"
96                 << endl;
97         return false;
98 }
99
100
101 void initSymbols()
102 {
103         string const filename = LibFileSearch(string(), "symbols");
104         lyxerr[Debug::MATHED] << "read symbols from " << filename << endl;
105         if (filename.empty()) {
106                 lyxerr << "Could not find symbols file" << endl;
107                 return;
108         }
109
110         std::ifstream fs(filename.c_str());
111         string line;
112         bool skip = false;
113         while (getline(fs, line)) {
114                 int charid     = 0;
115                 int fallbackid = 0;
116                 if (!line.empty() && line[0] == '#')
117                         continue;
118
119                 // special case of iffont/else/endif
120                 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
121                         istringstream is(line);
122                         string tmp;
123                         is >> tmp;
124                         is >> tmp;
125                         skip = !math_font_available(tmp);
126                         continue;
127                 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
128                         skip = !skip;
129                 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
130                         skip = false;
131                         continue;
132                 } else if (skip)
133                         continue;
134
135                 // special case of pre-defined macros
136                 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
137                         //lyxerr << "defining: '" << line << '\'' << endl;
138                         istringstream is(line);
139                         MathMacroTable::create(MathAtom(new MathMacroTemplate(is)));
140                         continue;
141                 }
142
143                 istringstream is(line);
144                 latexkeys tmp;
145                 is >> tmp.name >> tmp.inset;
146                 if (isFontName(tmp.inset))
147                         is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
148                 else
149                         is >> tmp.extra;
150                 if (!is) {
151                         lyxerr[Debug::MATHED] << "skipping line '" << line << '\'' << endl;
152                         lyxerr[Debug::MATHED]
153                                 << tmp.name << ' ' << tmp.inset << ' ' << tmp.extra << endl;
154                         continue;
155                 }
156
157                 if (isFontName(tmp.inset)) {
158                         // tmp.inset _is_ the fontname here.
159                         // create fallbacks if necessary
160
161                         // symbol font is not available sometimes
162                         string symbol_font = "lyxsymbol";
163
164                         if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
165                                 lyxerr[Debug::MATHED] << "symbol abuse for " << tmp.name << endl;
166                                 tmp.draw = tmp.name;
167                         } else if (math_font_available(tmp.inset)) {
168                                 lyxerr[Debug::MATHED] << "symbol available for " << tmp.name << endl;
169                                 tmp.draw += char(charid);
170                         } else if (fallbackid && math_font_available(symbol_font)) {
171                                 if (tmp.inset == "cmex")
172                                         tmp.inset  = "lyxsymbol";
173                                 else
174                                         tmp.inset  = "lyxboldsymbol";
175                                 lyxerr[Debug::MATHED] << "symbol fallback for " << tmp.name << endl;
176                                 tmp.draw += char(fallbackid);
177                         } else {
178                                 lyxerr[Debug::MATHED] << "faking " << tmp.name << endl;
179                                 tmp.draw = tmp.name;
180                                 tmp.inset = "lyxtex";
181                         }
182                 } else {
183                         // it's a proper inset
184                         lyxerr[Debug::MATHED] << "inset " << tmp.inset
185                                               << " used for " << tmp.name
186                                               << endl;
187                 }
188
189                 if (theWordList.find(tmp.name) != theWordList.end())
190                         lyxerr[Debug::MATHED]
191                                 << "readSymbols: inset " << tmp.name
192                                 << " already exists." << endl;
193                 else
194                         theWordList[tmp.name] = tmp;
195
196                 lyxerr[Debug::MATHED]
197                         << "read symbol '" << tmp.name
198                         << "  inset: " << tmp.inset
199                         << "  draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
200                         << "  extra: " << tmp.extra
201                         << '\'' << endl;
202         }
203         string tmp = "cmm";
204         string tmp2 = "cmsy";
205         has_math_fonts = math_font_available(tmp) && math_font_available(tmp2);
206 }
207
208
209 } // namespace anon
210
211
212 void initMath()
213 {
214         static bool initialized = false;
215         if (!initialized) {
216                 initSymbols();
217                 initialized = true;
218         }
219 }
220
221
222 latexkeys const * in_word_set(string const & str)
223 {
224         WordList::iterator it = theWordList.find(str);
225         //lyxerr << "looking up '" << str << "' found: "
226         // << (it != theWordList.end()) << endl;
227         return (it != theWordList.end()) ? &(it->second) : 0;
228 }
229
230
231 MathAtom createMathInset(string const & s)
232 {
233         lyxerr[Debug::MATHED] << "creating inset with name: '"
234                               << s << '\'' << endl;;
235         latexkeys const * l = in_word_set(s);
236         if (l) {
237                 string const & inset = l->inset;
238                 lyxerr[Debug::MATHED] << " found inset: '" <<
239                         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
313         if (MathMacroTable::has(s))
314                 return MathAtom(new MathMacro(s));
315
316         //lyxerr[Debug::MATHED] << "creating inset 2 with name: '" << s << '\'' << endl;
317         return MathAtom(new MathUnknownInset(s));
318 }
319
320
321 bool createMathInset_fromDialogStr(string const & str, MathArray & ar)
322 {
323         // An example str:
324         // "ref LatexCommand \\ref{sec:Title}\n\\end_inset\n\n";
325         string name;
326         string body = split(str, name, ' ');
327
328         if (name != "ref" )
329                 return false;
330
331         // body comes with a head "LatexCommand " and a
332         // tail "\nend_inset\n\n". Strip them off.
333         string trimmed;
334         body = split(body, trimmed, ' ');
335         split(body, trimmed, '\n');
336
337         mathed_parse_cell(ar, trimmed);
338         if (ar.size() != 1)
339                 return false;
340
341         return ar[0].nucleus();
342 }