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