2 * \file MathFactory.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "MathFactory.h"
15 #include "InsetMathAMSArray.h"
16 #include "InsetMathArray.h"
17 #include "InsetMathBinom.h"
18 #include "InsetMathBoldSymbol.h"
19 #include "InsetMathBoxed.h"
20 #include "InsetMathBox.h"
21 #include "InsetMathCases.h"
22 #include "InsetMathColor.h"
23 #include "InsetMathDecoration.h"
24 #include "InsetMathDFrac.h"
25 #include "InsetMathDots.h"
26 #include "InsetMathFBox.h"
27 #include "InsetMathFont.h"
28 #include "InsetMathFontOld.h"
29 #include "InsetMathFrac.h"
30 #include "InsetMathFrameBox.h"
31 #include "InsetMathKern.h"
32 #include "InsetMathLefteqn.h"
33 #include "MathMacro.h"
34 #include "InsetMathMakebox.h"
35 #include "InsetMathOverset.h"
36 #include "InsetMathPhantom.h"
37 #include "InsetMathRef.h"
38 #include "InsetMathRoot.h"
39 #include "InsetMathSize.h"
40 #include "InsetMathSpace.h"
41 #include "InsetMathSplit.h"
42 #include "InsetMathSqrt.h"
43 #include "InsetMathStackrel.h"
44 #include "InsetMathSubstack.h"
45 #include "InsetMathSymbol.h"
46 #include "InsetMathTabular.h"
47 #include "InsetMathTFrac.h"
48 #include "InsetMathUnderset.h"
49 #include "InsetMathUnknown.h"
50 #include "InsetMathXArrow.h"
51 #include "InsetMathXYMatrix.h"
52 #include "MathMacroArgument.h"
53 #include "MacroTable.h"
54 #include "MathParser.h"
55 #include "MathSupport.h"
59 #include "insets/InsetCommand.h"
61 #include "support/filetools.h" // LibFileSearch
62 #include "support/lstrings.h"
64 #include "frontends/FontLoader.h"
69 using support::libFileSearch;
74 using std::istringstream;
83 typedef std::map<docstring, latexkeys> WordList;
88 bool math_font_available(docstring & name)
93 // Do we have the font proper?
94 if (theFontLoader().available(f))
98 if (name == "eufrak") {
99 name = from_ascii("lyxfakefrak");
103 LYXERR(Debug::MATHED)
104 << "font " << to_utf8(name) << " not available and I can't fake it"
112 support::FileName const filename = libFileSearch(string(), "symbols");
113 LYXERR(Debug::MATHED) << "read symbols from " << filename << endl;
114 if (filename.empty()) {
115 lyxerr << "Could not find symbols file" << endl;
119 std::ifstream fs(filename.toFilesystemEncoding().c_str());
122 while (getline(fs, line)) {
125 if (line.empty() || line[0] == '#')
128 // special case of iffont/else/endif
129 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
130 istringstream is(line);
134 docstring t = from_utf8(tmp);
135 skip = !math_font_available(t);
137 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
139 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
145 // special case of pre-defined macros
146 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
147 //lyxerr << "macro definition: '" << line << '\'' << endl;
148 istringstream is(line);
151 is >> macro >> requires;
152 MacroTable::globalMacros().insert(from_utf8(macro), requires);
156 idocstringstream is(from_utf8(line));
158 is >> tmp.name >> tmp.inset;
159 if (isFontName(tmp.inset))
160 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
163 // requires is optional
167 LYXERR(Debug::MATHED) << "skipping line '" << line << '\'' << endl;
168 LYXERR(Debug::MATHED)
169 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' ' << to_utf8(tmp.extra) << endl;
173 if (isFontName(tmp.inset)) {
174 // tmp.inset _is_ the fontname here.
175 // create fallbacks if necessary
177 // store requirements as long as we can
178 if (tmp.requires.empty()) {
179 if (tmp.inset == "msa" || tmp.inset == "msb")
180 tmp.requires = from_ascii("amssymb");
181 else if (tmp.inset == "wasy")
182 tmp.requires = from_ascii("wasysym");
185 // symbol font is not available sometimes
186 docstring symbol_font = from_ascii("lyxsymbol");
188 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
189 LYXERR(Debug::MATHED) << "symbol abuse for " << to_utf8(tmp.name) << endl;
191 } else if (math_font_available(tmp.inset)) {
192 LYXERR(Debug::MATHED) << "symbol available for " << to_utf8(tmp.name) << endl;
193 tmp.draw.push_back(char_type(charid));
194 } else if (fallbackid && math_font_available(symbol_font)) {
195 if (tmp.inset == "cmex")
196 tmp.inset = from_ascii("lyxsymbol");
198 tmp.inset = from_ascii("lyxboldsymbol");
199 LYXERR(Debug::MATHED) << "symbol fallback for " << to_utf8(tmp.name) << endl;
200 tmp.draw.push_back(char_type(fallbackid));
202 LYXERR(Debug::MATHED) << "faking " << to_utf8(tmp.name) << endl;
204 tmp.inset = from_ascii("lyxtex");
207 // it's a proper inset
208 LYXERR(Debug::MATHED) << "inset " << to_utf8(tmp.inset)
209 << " used for " << to_utf8(tmp.name)
213 if (theWordList.find(tmp.name) != theWordList.end())
214 LYXERR(Debug::MATHED)
215 << "readSymbols: inset " << to_utf8(tmp.name)
216 << " already exists." << endl;
218 theWordList[tmp.name] = tmp;
220 LYXERR(Debug::MATHED)
221 << "read symbol '" << to_utf8(tmp.name)
222 << " inset: " << to_utf8(tmp.inset)
223 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
224 << " extra: " << to_utf8(tmp.extra)
225 << " requires: " << to_utf8(tmp.requires)
228 docstring tmp = from_ascii("cmm");
229 docstring tmp2 = from_ascii("cmsy");
230 has_math_fonts = math_font_available(tmp) && math_font_available(tmp2);
239 static bool initialized = false;
248 latexkeys const * in_word_set(docstring const & str)
250 WordList::iterator it = theWordList.find(str);
251 return it != theWordList.end() ? &(it->second) : 0;
255 MathAtom createInsetMath(char const * const s)
257 return createInsetMath(from_utf8(s));
261 MathAtom createInsetMath(docstring const & s)
263 //lyxerr << "creating inset with name: '" << s << '\'' << endl;
264 latexkeys const * l = in_word_set(s);
266 docstring const & inset = l->inset;
267 //lyxerr << " found inset: '" << inset << '\'' << endl;
269 return MathAtom(new InsetMathRef(l->name));
270 if (inset == "overset")
271 return MathAtom(new InsetMathOverset);
272 if (inset == "underset")
273 return MathAtom(new InsetMathUnderset);
274 if (inset == "decoration")
275 return MathAtom(new InsetMathDecoration(l));
276 if (inset == "space")
277 return MathAtom(new InsetMathSpace(l->name));
279 return MathAtom(new InsetMathDots(l));
281 // return MathAtom(new InsetMathMBox);
282 // InsetMathMBox is proposed to replace InsetMathBox,
283 // but is not ready yet (it needs a BufferView for
285 return MathAtom(new InsetMathBox(l->name));
286 // if (inset == "fbox")
287 // return MathAtom(new InsetMathFBox(l));
288 if (inset == "style")
289 return MathAtom(new InsetMathSize(l));
291 return MathAtom(new InsetMathFont(l));
292 if (inset == "oldfont")
293 return MathAtom(new InsetMathFontOld(l));
294 if (inset == "matrix")
295 return MathAtom(new InsetMathAMSArray(s));
296 if (inset == "split")
297 return MathAtom(new InsetMathSplit(s));
299 // we can't create a InsetMathBig, since the argument
301 return MathAtom(new InsetMathUnknown(s));
302 return MathAtom(new InsetMathSymbol(l));
305 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
306 return MathAtom(new MathMacroArgument(s[1] - '0'));
307 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
308 && s[2] >= '1' && s[2] <= '9')
309 return MathAtom(new MathMacroArgument(s[2] - '0'));
311 return MathAtom(new InsetMathBoxed());
313 return MathAtom(new InsetMathFBox());
315 return MathAtom(new InsetMathFrameBox);
317 return MathAtom(new InsetMathMakebox);
319 return MathAtom(new InsetMathKern);
320 if (s.substr(0, 8) == "xymatrix") {
321 char spacing_code = '\0';
323 size_t const len = s.length();
325 if (i < len && s[i] == '@') {
335 spacing_code = static_cast<char>(s[i]);
340 if (i < len && s[i] == '=') {
342 spacing = Length(to_ascii(s.substr(i)));
345 return MathAtom(new InsetMathXYMatrix(spacing, spacing_code));
347 if (s == "xrightarrow" || s == "xleftarrow")
348 return MathAtom(new InsetMathXArrow(s));
349 if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
350 return MathAtom(new InsetMathSplit(s));
352 return MathAtom(new InsetMathCases);
354 return MathAtom(new InsetMathSubstack);
355 if (s == "subarray" || s == "array")
356 return MathAtom(new InsetMathArray(s, 1, 1));
358 return MathAtom(new InsetMathSqrt);
360 return MathAtom(new InsetMathRoot);
362 return MathAtom(new InsetMathTabular(s, 1, 1));
364 return MathAtom(new InsetMathStackrel);
365 if (s == "binom" || s == "choose")
366 return MathAtom(new InsetMathBinom(s == "choose"));
368 return MathAtom(new InsetMathFrac);
370 return MathAtom(new InsetMathFrac(InsetMathFrac::OVER));
372 return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC));
374 // return MathAtom(new MathInferInset);
376 return MathAtom(new InsetMathFrac(InsetMathFrac::ATOP));
378 return MathAtom(new InsetMathLefteqn);
379 if (s == "boldsymbol")
380 return MathAtom(new InsetMathBoldSymbol);
381 if (s == "color" || s == "normalcolor")
382 return MathAtom(new InsetMathColor(true));
383 if (s == "textcolor")
384 return MathAtom(new InsetMathColor(false));
386 return MathAtom(new InsetMathDFrac);
388 return MathAtom(new InsetMathTFrac);
390 return MathAtom(new InsetMathPhantom(InsetMathPhantom::hphantom));
392 return MathAtom(new InsetMathPhantom(InsetMathPhantom::phantom));
394 return MathAtom(new InsetMathPhantom(InsetMathPhantom::vphantom));
396 if (MacroTable::globalMacros().has(s))
397 return MathAtom(new MathMacro(s,
398 MacroTable::globalMacros().get(s).numargs()));
399 //if (MacroTable::localMacros().has(s))
400 // return MathAtom(new MathMacro(s,
401 // MacroTable::localMacros().get(s).numargs()));
403 //lyxerr << "creating unknown inset '" << s << "'" << endl;
404 return MathAtom(new InsetMathUnknown(s));
408 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
411 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
413 docstring body = split(str, name, ' ');
418 InsetCommandParams icp("ref");
420 InsetCommandMailer::string2params("ref", to_utf8(str), icp);
421 mathed_parse_cell(ar, icp.getCommand());
425 return ar[0].nucleus();