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 "InsetMathBoldSymbol.h"
18 #include "InsetMathBoxed.h"
19 #include "InsetMathBox.h"
20 #include "InsetMathCases.h"
21 #include "InsetMathColor.h"
22 #include "InsetMathDecoration.h"
23 #include "InsetMathDots.h"
24 #include "InsetMathFBox.h"
25 #include "InsetMathFont.h"
26 #include "InsetMathFontOld.h"
27 #include "InsetMathFrac.h"
28 #include "InsetMathFrameBox.h"
29 #include "InsetMathKern.h"
30 #include "InsetMathLefteqn.h"
31 #include "InsetMathMakebox.h"
32 #include "InsetMathOverset.h"
33 #include "InsetMathPhantom.h"
34 #include "InsetMathRef.h"
35 #include "InsetMathRoot.h"
36 #include "InsetMathSize.h"
37 #include "InsetMathSpace.h"
38 #include "InsetMathSplit.h"
39 #include "InsetMathSqrt.h"
40 #include "InsetMathStackrel.h"
41 #include "InsetMathSubstack.h"
42 #include "InsetMathSymbol.h"
43 #include "InsetMathTabular.h"
44 #include "InsetMathUnderset.h"
45 #include "InsetMathUnknown.h"
46 #include "InsetMathXArrow.h"
47 #include "InsetMathXYMatrix.h"
48 #include "MacroTable.h"
49 #include "MathMacro.h"
50 #include "MathMacroArgument.h"
51 #include "MathParser.h"
52 #include "MathSupport.h"
56 #include "insets/InsetCommand.h"
58 #include "support/filetools.h" // LibFileSearch
59 #include "support/lstrings.h"
61 #include "frontends/FontLoader.h"
66 using support::libFileSearch;
71 using std::istringstream;
80 typedef std::map<docstring, latexkeys> WordList;
85 bool math_font_available(docstring & name)
90 // Do we have the font proper?
91 if (theFontLoader().available(f))
95 if (name == "eufrak") {
96 name = from_ascii("lyxfakefrak");
100 LYXERR(Debug::MATHED)
101 << "font " << to_utf8(name) << " not available and I can't fake it"
109 support::FileName const filename = libFileSearch(string(), "symbols");
110 LYXERR(Debug::MATHED) << "read symbols from " << filename << endl;
111 if (filename.empty()) {
112 lyxerr << "Could not find symbols file" << endl;
116 std::ifstream fs(filename.toFilesystemEncoding().c_str());
119 while (getline(fs, line)) {
122 if (line.empty() || line[0] == '#')
125 // special case of iffont/else/endif
126 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
127 istringstream is(line);
131 docstring t = from_utf8(tmp);
132 skip = !math_font_available(t);
134 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
136 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
142 // special case of pre-defined macros
143 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
144 //lyxerr << "macro definition: '" << line << '\'' << endl;
145 istringstream is(line);
148 is >> macro >> requires;
149 MacroTable::globalMacros().insert(from_utf8(macro), requires);
153 idocstringstream is(from_utf8(line));
155 is >> tmp.name >> tmp.inset;
156 if (isFontName(tmp.inset))
157 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
160 // requires is optional
164 LYXERR(Debug::MATHED) << "skipping line '" << line << '\'' << endl;
165 LYXERR(Debug::MATHED)
166 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' ' << to_utf8(tmp.extra) << endl;
170 if (isFontName(tmp.inset)) {
171 // tmp.inset _is_ the fontname here.
172 // create fallbacks if necessary
174 // store requirements as long as we can
175 if (tmp.requires.empty()) {
176 if (tmp.inset == "msa" || tmp.inset == "msb")
177 tmp.requires = from_ascii("amssymb");
178 else if (tmp.inset == "wasy")
179 tmp.requires = from_ascii("wasysym");
182 // symbol font is not available sometimes
183 docstring symbol_font = from_ascii("lyxsymbol");
185 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
186 LYXERR(Debug::MATHED) << "symbol abuse for " << to_utf8(tmp.name) << endl;
188 } else if (math_font_available(tmp.inset)) {
189 LYXERR(Debug::MATHED) << "symbol available for " << to_utf8(tmp.name) << endl;
190 tmp.draw.push_back(char_type(charid));
191 } else if (fallbackid && math_font_available(symbol_font)) {
192 if (tmp.inset == "cmex")
193 tmp.inset = from_ascii("lyxsymbol");
195 tmp.inset = from_ascii("lyxboldsymbol");
196 LYXERR(Debug::MATHED) << "symbol fallback for " << to_utf8(tmp.name) << endl;
197 tmp.draw.push_back(char_type(fallbackid));
199 LYXERR(Debug::MATHED) << "faking " << to_utf8(tmp.name) << endl;
201 tmp.inset = from_ascii("lyxtex");
204 // it's a proper inset
205 LYXERR(Debug::MATHED) << "inset " << to_utf8(tmp.inset)
206 << " used for " << to_utf8(tmp.name)
210 if (theWordList.find(tmp.name) != theWordList.end())
211 LYXERR(Debug::MATHED)
212 << "readSymbols: inset " << to_utf8(tmp.name)
213 << " already exists." << endl;
215 theWordList[tmp.name] = tmp;
217 LYXERR(Debug::MATHED)
218 << "read symbol '" << to_utf8(tmp.name)
219 << " inset: " << to_utf8(tmp.inset)
220 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
221 << " extra: " << to_utf8(tmp.extra)
222 << " requires: " << to_utf8(tmp.requires)
225 docstring tmp = from_ascii("cmm");
226 docstring tmp2 = from_ascii("cmsy");
227 has_math_fonts = math_font_available(tmp) && math_font_available(tmp2);
236 static bool initialized = false;
245 latexkeys const * in_word_set(docstring const & str)
247 WordList::iterator it = theWordList.find(str);
248 return it != theWordList.end() ? &(it->second) : 0;
252 MathAtom createInsetMath(char const * const s)
254 return createInsetMath(from_utf8(s));
258 MathAtom createInsetMath(docstring const & s)
260 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
261 latexkeys const * l = in_word_set(s);
263 docstring const & inset = l->inset;
264 //lyxerr << " found inset: '" << inset << '\'' << endl;
266 return MathAtom(new InsetMathRef(l->name));
267 if (inset == "overset")
268 return MathAtom(new InsetMathOverset);
269 if (inset == "underset")
270 return MathAtom(new InsetMathUnderset);
271 if (inset == "decoration")
272 return MathAtom(new InsetMathDecoration(l));
273 if (inset == "space")
274 return MathAtom(new InsetMathSpace(l->name));
276 return MathAtom(new InsetMathDots(l));
278 // return MathAtom(new InsetMathMBox);
279 // InsetMathMBox is proposed to replace InsetMathBox,
280 // but is not ready yet (it needs a BufferView for
282 return MathAtom(new InsetMathBox(l->name));
283 // if (inset == "fbox")
284 // return MathAtom(new InsetMathFBox(l));
285 if (inset == "style")
286 return MathAtom(new InsetMathSize(l));
288 return MathAtom(new InsetMathFont(l));
289 if (inset == "oldfont")
290 return MathAtom(new InsetMathFontOld(l));
291 if (inset == "matrix")
292 return MathAtom(new InsetMathAMSArray(s));
293 if (inset == "split")
294 return MathAtom(new InsetMathSplit(s));
296 // we can't create a InsetMathBig, since the argument
298 return MathAtom(new InsetMathUnknown(s));
299 return MathAtom(new InsetMathSymbol(l));
302 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
303 return MathAtom(new MathMacroArgument(s[1] - '0'));
304 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
305 && s[2] >= '1' && s[2] <= '9')
306 return MathAtom(new MathMacroArgument(s[2] - '0'));
308 return MathAtom(new InsetMathBoxed());
310 return MathAtom(new InsetMathFBox());
312 return MathAtom(new InsetMathFrameBox);
314 return MathAtom(new InsetMathMakebox);
316 return MathAtom(new InsetMathKern);
317 if (s.substr(0, 8) == "xymatrix") {
318 char spacing_code = '\0';
320 size_t const len = s.length();
322 if (i < len && s[i] == '@') {
332 spacing_code = static_cast<char>(s[i]);
337 if (i < len && s[i] == '=') {
339 spacing = Length(to_ascii(s.substr(i)));
342 return MathAtom(new InsetMathXYMatrix(spacing, spacing_code));
344 if (s == "xrightarrow" || s == "xleftarrow")
345 return MathAtom(new InsetMathXArrow(s));
346 if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
347 return MathAtom(new InsetMathSplit(s));
349 return MathAtom(new InsetMathCases);
351 return MathAtom(new InsetMathSubstack);
352 if (s == "subarray" || s == "array")
353 return MathAtom(new InsetMathArray(s, 1, 1));
355 return MathAtom(new InsetMathSqrt);
357 return MathAtom(new InsetMathRoot);
359 return MathAtom(new InsetMathTabular(s, 1, 1));
361 return MathAtom(new InsetMathStackrel);
362 if (s == "binom" || s == "choose")
363 return MathAtom(new InsetMathBinom(s == "choose"));
365 return MathAtom(new InsetMathFrac);
367 return MathAtom(new InsetMathFrac(InsetMathFrac::OVER));
369 return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC));
371 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC));
372 // This string value is only for math toolbar use. Not a LaTeX name
373 if (s == "unitfracthree")
374 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3));
376 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1));
378 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT));
380 // return MathAtom(new MathInferInset);
382 return MathAtom(new InsetMathFrac(InsetMathFrac::ATOP));
384 return MathAtom(new InsetMathLefteqn);
385 if (s == "boldsymbol")
386 return MathAtom(new InsetMathBoldSymbol);
387 if (s == "color" || s == "normalcolor")
388 return MathAtom(new InsetMathColor(true));
389 if (s == "textcolor")
390 return MathAtom(new InsetMathColor(false));
392 return MathAtom(new InsetMathDFrac);
394 return MathAtom(new InsetMathTFrac);
396 return MathAtom(new InsetMathDBinom);
398 return MathAtom(new InsetMathTBinom);
400 return MathAtom(new InsetMathPhantom(InsetMathPhantom::hphantom));
402 return MathAtom(new InsetMathPhantom(InsetMathPhantom::phantom));
404 return MathAtom(new InsetMathPhantom(InsetMathPhantom::vphantom));
406 return MathAtom(new MathMacro(s));
410 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
413 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
415 docstring body = split(str, name, ' ');
420 InsetCommandParams icp(REF_CODE);
422 InsetCommandMailer::string2params("ref", to_utf8(str), icp);
423 mathed_parse_cell(ar, icp.getCommand());
427 return ar[0].nucleus();