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