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 "InsetMathBox.h"
19 #include "InsetMathCases.h"
20 #include "InsetMathColor.h"
21 #include "InsetMathDecoration.h"
22 #include "InsetMathDots.h"
23 #include "InsetMathEnsureMath.h"
24 #include "InsetMathFont.h"
25 #include "InsetMathFontOld.h"
26 #include "InsetMathFrac.h"
27 #include "InsetMathKern.h"
28 #include "InsetMathLefteqn.h"
29 #include "InsetMathOverset.h"
30 #include "InsetMathPhantom.h"
31 #include "InsetMathRef.h"
32 #include "InsetMathRoot.h"
33 #include "InsetMathSize.h"
34 #include "InsetMathSpace.h"
35 #include "InsetMathSpecialChar.h"
36 #include "InsetMathSplit.h"
37 #include "InsetMathSqrt.h"
38 #include "InsetMathStackrel.h"
39 #include "InsetMathSubstack.h"
40 #include "InsetMathSymbol.h"
41 #include "InsetMathTabular.h"
42 #include "InsetMathUnderset.h"
43 #include "InsetMathUnknown.h"
44 #include "InsetMathXArrow.h"
45 #include "InsetMathXYMatrix.h"
46 #include "MacroTable.h"
47 #include "MathMacro.h"
48 #include "MathMacroArgument.h"
49 #include "MathParser.h"
50 #include "MathStream.h"
51 #include "MathSupport.h"
53 #include "insets/InsetCommand.h"
55 #include "support/debug.h"
56 #include "support/docstream.h"
57 #include "support/FileName.h"
58 #include "support/filetools.h" // LibFileSearch
59 #include "support/lstrings.h"
61 #include "frontends/FontLoader.h"
63 #include "LyX.h" // use_gui
66 using namespace lyx::support;
75 MathWordList theWordList;
78 bool isMathFontAvailable(docstring & name)
86 // Do we have the font proper?
87 if (theFontLoader().available(f))
91 if (name == "eufrak") {
92 name = from_ascii("lyxfakefrak");
97 "font " << to_utf8(name) << " not available and I can't fake it");
104 FileName const filename = libFileSearch(string(), "symbols");
105 LYXERR(Debug::MATHED, "read symbols from " << filename);
106 if (filename.empty()) {
107 lyxerr << "Could not find symbols file" << endl;
111 ifstream fs(filename.toFilesystemEncoding().c_str());
114 while (getline(fs, line)) {
117 if (line.empty() || line[0] == '#')
120 // special case of iffont/else/endif
121 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
122 istringstream is(line);
126 docstring t = from_utf8(tmp);
127 skip = !isMathFontAvailable(t);
129 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
131 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
137 // special case of pre-defined macros
138 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
139 //lyxerr << "macro definition: '" << line << '\'' << endl;
140 istringstream is(line);
143 is >> macro >> requires;
144 MacroTable::globalMacros().insert(from_utf8(macro), requires);
148 idocstringstream is(from_utf8(line));
150 is >> tmp.name >> tmp.inset;
151 if (isFontName(tmp.inset))
152 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
155 // requires is optional
159 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
160 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' '
161 << to_utf8(tmp.extra));
165 if (isFontName(tmp.inset)) {
166 // tmp.inset _is_ the fontname here.
167 // create fallbacks if necessary
169 // store requirements as long as we can
170 if (tmp.requires.empty()) {
171 if (tmp.inset == "msa" || tmp.inset == "msb")
172 tmp.requires = from_ascii("amssymb");
173 else if (tmp.inset == "wasy")
174 tmp.requires = from_ascii("wasysym");
177 // symbol font is not available sometimes
178 docstring symbol_font = from_ascii("lyxsymbol");
180 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
181 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
183 } else if (isMathFontAvailable(tmp.inset)) {
184 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
185 tmp.draw.push_back(char_type(charid));
186 } else if (fallbackid && isMathFontAvailable(symbol_font)) {
187 if (tmp.inset == "cmex")
188 tmp.inset = from_ascii("lyxsymbol");
190 tmp.inset = from_ascii("lyxboldsymbol");
191 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
192 tmp.draw.push_back(char_type(fallbackid));
194 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
196 tmp.inset = from_ascii("lyxtex");
199 // it's a proper inset
200 LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
201 << " used for " << to_utf8(tmp.name));
204 if (theWordList.find(tmp.name) != theWordList.end())
205 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
206 << " already exists.");
208 theWordList[tmp.name] = tmp;
210 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
211 << " inset: " << to_utf8(tmp.inset)
212 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
213 << " extra: " << to_utf8(tmp.extra)
214 << " requires: " << to_utf8(tmp.requires) << '\'');
216 docstring tmp = from_ascii("cmm");
217 docstring tmp2 = from_ascii("cmsy");
218 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
222 bool isSpecialChar(docstring const & name)
224 if (name.size() != 1)
225 return name == "textasciicircum" || name == "mathcircumflex" ||
226 name == "textasciitilde" || name == "textbackslash";
228 char_type const c = name.at(0);
229 return c == '{' || c == '}' || c == '&' || c == '$' ||
230 c == '#' || c == '%' || c == '_' || c == ' ';
236 MathWordList const & mathedWordList()
244 static bool initialized = false;
253 bool ensureMath(WriteStream & os, bool needs_math_mode, bool macro)
255 bool brace = os.pendingBrace();
256 os.pendingBrace(false);
259 if (os.textMode() && needs_math_mode) {
260 os << "\\ensuremath{";
263 } else if (macro && brace && !needs_math_mode) {
264 // This is a user defined macro, but not a MathMacro, so we
265 // cannot be sure what mode is needed. As it was entered in
266 // a text box, we restore the text mode.
275 bool ensureMode(WriteStream & os, InsetMath::mode_type mode)
277 bool textmode = mode == InsetMath::TEXT_MODE;
278 if (os.latex() && textmode && os.pendingBrace()) {
280 os.pendingBrace(false);
281 os.pendingSpace(false);
284 bool oldmode = os.textMode();
285 os.textMode(textmode);
290 latexkeys const * in_word_set(docstring const & str)
292 MathWordList::iterator it = theWordList.find(str);
293 return it != theWordList.end() ? &(it->second) : 0;
297 MathAtom createInsetMath(char const * const s)
299 return createInsetMath(from_utf8(s));
303 MathAtom createInsetMath(docstring const & s)
305 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
306 latexkeys const * l = in_word_set(s);
308 docstring const & inset = l->inset;
309 //lyxerr << " found inset: '" << inset << '\'' << endl;
311 return MathAtom(new InsetMathRef(l->name));
312 if (inset == "overset")
313 return MathAtom(new InsetMathOverset);
314 if (inset == "underset")
315 return MathAtom(new InsetMathUnderset);
316 if (inset == "decoration")
317 return MathAtom(new InsetMathDecoration(l));
318 if (inset == "space")
319 return MathAtom(new InsetMathSpace(l->name));
321 return MathAtom(new InsetMathDots(l));
323 // return MathAtom(new InsetMathMBox);
324 // InsetMathMBox is proposed to replace InsetMathBox,
325 // but is not ready yet (it needs a BufferView for
327 return MathAtom(new InsetMathBox(l->name));
328 // if (inset == "fbox")
329 // return MathAtom(new InsetMathFBox(l));
330 if (inset == "style")
331 return MathAtom(new InsetMathSize(l));
333 return MathAtom(new InsetMathFont(l));
334 if (inset == "oldfont")
335 return MathAtom(new InsetMathFontOld(l));
336 if (inset == "matrix")
337 return MathAtom(new InsetMathAMSArray(s));
338 if (inset == "split")
339 return MathAtom(new InsetMathSplit(s));
341 // we can't create a InsetMathBig, since the argument
343 return MathAtom(new InsetMathUnknown(s));
344 return MathAtom(new InsetMathSymbol(l));
347 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
348 return MathAtom(new MathMacroArgument(s[1] - '0'));
349 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
350 && s[2] >= '1' && s[2] <= '9')
351 return MathAtom(new MathMacroArgument(s[2] - '0'));
353 return MathAtom(new InsetMathBoxed());
355 return MathAtom(new InsetMathFBox());
357 return MathAtom(new InsetMathMakebox(true));
359 return MathAtom(new InsetMathMakebox(false));
361 return MathAtom(new InsetMathKern);
362 if (s.substr(0, 8) == "xymatrix") {
363 char spacing_code = '\0';
365 size_t const len = s.length();
367 if (i < len && s[i] == '@') {
377 spacing_code = static_cast<char>(s[i]);
382 if (i < len && s[i] == '=') {
384 spacing = Length(to_ascii(s.substr(i)));
387 return MathAtom(new InsetMathXYMatrix(spacing, spacing_code));
389 if (s == "xrightarrow" || s == "xleftarrow")
390 return MathAtom(new InsetMathXArrow(s));
391 if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
392 return MathAtom(new InsetMathSplit(s));
394 return MathAtom(new InsetMathCases);
396 return MathAtom(new InsetMathSubstack);
397 if (s == "subarray" || s == "array")
398 return MathAtom(new InsetMathArray(s, 1, 1));
400 return MathAtom(new InsetMathSqrt);
402 return MathAtom(new InsetMathRoot);
404 return MathAtom(new InsetMathTabular(s, 1, 1));
406 return MathAtom(new InsetMathStackrel);
408 return MathAtom(new InsetMathBinom(InsetMathBinom::BINOM));
410 return MathAtom(new InsetMathBinom(InsetMathBinom::CHOOSE));
412 return MathAtom(new InsetMathBinom(InsetMathBinom::BRACE));
414 return MathAtom(new InsetMathBinom(InsetMathBinom::BRACK));
416 return MathAtom(new InsetMathFrac);
418 return MathAtom(new InsetMathFrac(InsetMathFrac::OVER));
420 return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC));
422 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC));
423 // This string value is only for math toolbar use. Not a LaTeX name
424 if (s == "unitfracthree")
425 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3));
427 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1));
429 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT));
431 // return MathAtom(new MathInferInset);
433 return MathAtom(new InsetMathFrac(InsetMathFrac::ATOP));
435 return MathAtom(new InsetMathLefteqn);
436 if (s == "boldsymbol")
437 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::AMS_BOLD));
439 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::BM_BOLD));
440 if (s == "heavysymbol" || s == "hm")
441 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::BM_HEAVY));
442 if (s == "color" || s == "normalcolor")
443 return MathAtom(new InsetMathColor(true));
444 if (s == "textcolor")
445 return MathAtom(new InsetMathColor(false));
447 return MathAtom(new InsetMathDFrac);
449 return MathAtom(new InsetMathTFrac);
451 return MathAtom(new InsetMathDBinom);
453 return MathAtom(new InsetMathTBinom);
455 return MathAtom(new InsetMathPhantom(InsetMathPhantom::hphantom));
457 return MathAtom(new InsetMathPhantom(InsetMathPhantom::phantom));
459 return MathAtom(new InsetMathPhantom(InsetMathPhantom::vphantom));
460 if (s == "ensuremath")
461 return MathAtom(new InsetMathEnsureMath);
462 if (isSpecialChar(s))
463 return MathAtom(new InsetMathSpecialChar(s));
465 return MathAtom(new MathMacro(s));
469 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
472 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
474 docstring body = split(str, name, ' ');
479 InsetCommandParams icp(REF_CODE);
481 InsetCommand::string2params("ref", to_utf8(str), icp);
482 mathed_parse_cell(ar, icp.getCommand());
486 return ar[0].nucleus();