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 "InsetMathCancel.h"
20 #include "InsetMathCancelto.h"
21 #include "InsetMathCases.h"
22 #include "InsetMathColor.h"
23 #include "InsetMathDecoration.h"
24 #include "InsetMathDots.h"
25 #include "InsetMathEnsureMath.h"
26 #include "InsetMathFont.h"
27 #include "InsetMathFontOld.h"
28 #include "InsetMathFrac.h"
29 #include "InsetMathKern.h"
30 #include "InsetMathLefteqn.h"
31 #include "InsetMathOverset.h"
32 #include "InsetMathPhantom.h"
33 #include "InsetMathRef.h"
34 #include "InsetMathRoot.h"
35 #include "InsetMathSideset.h"
36 #include "InsetMathSize.h"
37 #include "InsetMathSpace.h"
38 #include "InsetMathSpecialChar.h"
39 #include "InsetMathSplit.h"
40 #include "InsetMathSqrt.h"
41 #include "InsetMathStackrel.h"
42 #include "InsetMathSubstack.h"
43 #include "InsetMathSymbol.h"
44 #include "InsetMathTabular.h"
45 #include "InsetMathUnderset.h"
46 #include "InsetMathUnknown.h"
47 #include "InsetMathHull.h"
48 #include "InsetMathXArrow.h"
49 #include "InsetMathXYMatrix.h"
50 #include "InsetMathDiagram.h"
51 #include "MacroTable.h"
52 #include "MathMacro.h"
53 #include "MathMacroArgument.h"
54 #include "MathParser.h"
55 #include "MathStream.h"
56 #include "MathSupport.h"
58 #include "insets/InsetCommand.h"
59 #include "insets/InsetSpace.h"
61 #include "support/debug.h"
62 #include "support/docstream.h"
63 #include "support/FileName.h"
64 #include "support/filetools.h" // LibFileSearch
65 #include "support/lstrings.h"
66 #include "support/textutils.h"
68 #include "frontends/FontLoader.h"
71 #include "BufferParams.h"
73 #include "LyX.h" // use_gui
74 #include "OutputParams.h"
77 using namespace lyx::support;
86 MathWordList theMathWordList;
89 bool isMathFontAvailable(docstring & name)
97 // Do we have the font proper?
98 if (theFontLoader().available(f))
102 if (name == "eufrak") {
103 name = from_ascii("lyxfakefrak");
107 LYXERR(Debug::MATHED,
108 "font " << to_utf8(name) << " not available and I can't fake it");
113 bool canBeDisplayed(char_type c)
117 return theFontLoader().canBeDisplayed(c);
121 bool isUnicodeSymbolAvailable(docstring const & name, char_type & c)
123 docstring cmd(from_ascii("\\") + name);
126 c = Encodings::fromLaTeXCommand(cmd, Encodings::MATH_CMD,
127 is_combining, termination);
128 if (c == 0 && name == "varOmega") {
129 // fallback for bug 7954, unicodesymbols does not list
130 // \\varOmega because of requirements, but this might change
131 cmd = from_ascii("\\mathit{\\Omega}");
132 c = Encodings::fromLaTeXCommand(cmd, Encodings::MATH_CMD,
133 is_combining, termination);
135 return c != 0 && !is_combining;
141 FileName const filename = libFileSearch(string(), "symbols");
142 LYXERR(Debug::MATHED, "read symbols from " << filename);
143 if (filename.empty()) {
144 lyxerr << "Could not find symbols file" << endl;
148 ifstream fs(filename.toFilesystemEncoding().c_str());
151 while (getline(fs, line)) {
154 if (line.empty() || line[0] == '#')
157 // special case of iffont/else/endif
158 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
159 istringstream is(line);
163 docstring t = from_utf8(tmp);
164 skip = !isMathFontAvailable(t);
166 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
169 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
175 // special case of pre-defined macros
176 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
177 //lyxerr << "macro definition: '" << line << '\'' << endl;
179 // \def\macroname{definition}
181 // \def\macroname{definition} requires
183 // \def\macroname{definition} extra xmlname requires
184 istringstream is(line);
189 is >> macro >> requires;
190 if ((is >> xmlname)) {
192 if (!(is >> requires))
196 MacroTable::iterator it = MacroTable::globalMacros().insert(
197 0, from_utf8(macro));
198 if (!extra.empty() || !xmlname.empty() || !requires.empty()) {
199 MathWordList::iterator wit = theMathWordList.find(it->first);
200 if (wit != theMathWordList.end())
201 LYXERR(Debug::MATHED, "readSymbols: inset "
202 << to_utf8(it->first) << " already exists.");
205 tmp.inset = from_ascii("macro");
206 tmp.name = it->first;
207 tmp.extra = from_utf8(extra);
208 tmp.xmlname = from_utf8(xmlname);
209 tmp.requires = from_utf8(requires);
210 theMathWordList[it->first] = tmp;
211 wit = theMathWordList.find(it->first);
212 it->second.setSymbol(&(wit->second));
215 // If you change the following output, please adjust
216 // development/tools/generate_symbols_images.py.
217 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(it->first)
220 << " extra: " << extra
221 << " xml: " << xmlname
222 << " requires: " << requires << '\'');
226 idocstringstream is(from_utf8(line));
228 is >> tmp.name >> tmp.inset;
229 if (isFontName(tmp.inset))
230 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
233 // requires is optional
236 // backward compatibility
237 if (tmp.requires == "esintoramsmath")
238 tmp.requires = from_ascii("esint|amsmath");
240 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
241 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' '
242 << to_utf8(tmp.extra));
246 if (isFontName(tmp.inset)) {
247 // tmp.inset _is_ the fontname here.
248 // create fallbacks if necessary
250 // store requirements as long as we can
251 if (tmp.requires.empty()) {
252 if (tmp.inset == "msa" || tmp.inset == "msb")
253 tmp.requires = from_ascii("amssymb");
254 else if (tmp.inset == "wasy")
255 tmp.requires = from_ascii("wasysym");
256 else if (tmp.inset == "mathscr")
257 tmp.requires = from_ascii("mathrsfs");
260 // symbol font is not available sometimes
261 docstring symbol_font = from_ascii("lyxsymbol");
262 char_type unicodesymbol = 0;
264 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
265 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
267 } else if (isMathFontAvailable(tmp.inset) && canBeDisplayed(charid)) {
268 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
269 tmp.draw.push_back(char_type(charid));
270 } else if (fallbackid && isMathFontAvailable(symbol_font) &&
271 canBeDisplayed(fallbackid)) {
272 if (tmp.inset == "cmex")
273 tmp.inset = from_ascii("lyxsymbol");
275 tmp.inset = from_ascii("lyxboldsymbol");
276 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
277 tmp.draw.push_back(char_type(fallbackid));
278 } else if (isUnicodeSymbolAvailable(tmp.name, unicodesymbol)) {
279 LYXERR(Debug::MATHED, "unicode fallback for " << to_utf8(tmp.name));
280 tmp.inset = from_ascii("mathnormal");
281 tmp.draw.push_back(unicodesymbol);
283 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
285 tmp.inset = from_ascii("lyxtex");
288 // it's a proper inset
289 LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
290 << " used for " << to_utf8(tmp.name));
293 if (theMathWordList.find(tmp.name) != theMathWordList.end())
294 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
295 << " already exists.");
297 theMathWordList[tmp.name] = tmp;
299 // If you change the following output, please adjust
300 // development/tools/generate_symbols_images.py.
301 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
302 << " inset: " << to_utf8(tmp.inset)
303 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
304 << " extra: " << to_utf8(tmp.extra)
305 << " xml: " << to_utf8(tmp.xmlname)
306 << " requires: " << to_utf8(tmp.requires) << '\'');
308 docstring tmp = from_ascii("cmm");
309 docstring tmp2 = from_ascii("cmsy");
310 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
314 bool isSpecialChar(docstring const & name)
316 if (name.size() != 1)
317 return name == "textasciicircum" || name == "mathcircumflex" ||
318 name == "textasciitilde" || name == "textbackslash";
320 char_type const c = name.at(0);
321 return c == '{' || c == '}' || c == '&' || c == '$' ||
322 c == '#' || c == '%' || c == '_';
328 MathWordList const & mathedWordList()
330 return theMathWordList;
336 static bool initialized = false;
345 bool ensureMath(WriteStream & os, bool needs_math_mode, bool macro)
347 bool brace = os.pendingBrace();
348 os.pendingBrace(false);
351 if (os.textMode() && needs_math_mode) {
352 os << "\\ensuremath{";
355 } else if (macro && brace && !needs_math_mode) {
356 // This is a user defined macro, but not a MathMacro, so we
357 // cannot be sure what mode is needed. As it was entered in
358 // a text box, we restore the text mode.
367 int ensureMode(WriteStream & os, InsetMath::mode_type mode,
368 bool locked, bool ascii)
370 bool textmode = mode == InsetMath::TEXT_MODE;
371 if (os.latex() && textmode && os.pendingBrace()) {
373 os.pendingBrace(false);
374 os.pendingSpace(false);
377 int oldmodes = os.textMode() ? 0x01 : 0;
378 os.textMode(textmode);
379 oldmodes |= os.lockedMode() ? 0x02 : 0;
380 os.lockedMode(locked);
381 oldmodes |= os.asciiOnly() ? 0x04 : 0;
387 latexkeys const * in_word_set(docstring const & str)
389 MathWordList::iterator it = theMathWordList.find(str);
390 if (it == theMathWordList.end())
392 if (it->second.inset == "macro")
394 return &(it->second);
398 MathAtom createInsetMath(char const * const s, Buffer * buf)
400 return createInsetMath(from_utf8(s), buf);
404 MathAtom createInsetMath(docstring const & s, Buffer * buf)
406 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
407 if ((s == "ce" || s == "cf") && buf
408 && buf->params().use_package("mhchem") == BufferParams::package_off)
409 return MathAtom(new MathMacro(buf, s));
411 latexkeys const * l = in_word_set(s);
413 docstring const & inset = l->inset;
414 //lyxerr << " found inset: '" << inset << '\'' << endl;
416 return MathAtom(new InsetMathRef(buf, l->name));
417 if (inset == "overset")
418 return MathAtom(new InsetMathOverset(buf));
419 if (inset == "underset")
420 return MathAtom(new InsetMathUnderset(buf));
421 if (inset == "decoration")
422 return MathAtom(new InsetMathDecoration(buf, l));
423 if (inset == "space")
424 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
426 return MathAtom(new InsetMathDots(l));
428 return MathAtom(new InsetMathBox(buf, l->name));
429 // if (inset == "fbox")
430 // return MathAtom(new InsetMathFBox(l));
431 if (inset == "style")
432 return MathAtom(new InsetMathSize(buf, l));
434 return MathAtom(new InsetMathFont(buf, l));
435 if (inset == "oldfont")
436 return MathAtom(new InsetMathFontOld(buf, l));
437 if (inset == "matrix")
438 return MathAtom(new InsetMathAMSArray(buf, s));
439 if (inset == "split")
440 return MathAtom(new InsetMathSplit(buf, s));
442 // we can't create a InsetMathBig, since the argument
444 return MathAtom(new InsetMathUnknown(s));
445 return MathAtom(new InsetMathSymbol(l));
448 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
449 return MathAtom(new MathMacroArgument(s[1] - '0'));
450 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
451 && s[2] >= '1' && s[2] <= '9')
452 return MathAtom(new MathMacroArgument(s[2] - '0'));
454 return MathAtom(new InsetMathBoxed(buf));
456 return MathAtom(new InsetMathFBox(buf));
458 return MathAtom(new InsetMathMakebox(buf, true));
460 return MathAtom(new InsetMathMakebox(buf, false));
462 return MathAtom(new InsetMathKern);
463 if (s.substr(0, 8) == "xymatrix") {
464 char spacing_code = '\0';
466 bool equal_spacing = false;
467 size_t const len = s.length();
469 if (i < len && s[i] == '@') {
471 if (i < len && s[i] == '!') {
472 equal_spacing = true;
479 spacing_code = static_cast<char>(s[i]);
482 } else if (i < len) {
490 spacing_code = static_cast<char>(s[i]);
494 if (i < len && s[i] == '=') {
496 spacing = Length(to_ascii(s.substr(i)));
500 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
505 return MathAtom(new InsetMathDiagram(buf));
506 if (s == "xrightarrow" || s == "xleftarrow" ||
507 s == "xhookrightarrow" || s == "xhookleftarrow" ||
508 s == "xRightarrow" || s == "xLeftarrow" ||
509 s == "xleftrightarrow" || s == "xLeftrightarrow" ||
510 s == "xrightharpoondown" || s == "xrightharpoonup" ||
511 s == "xleftharpoondown" || s == "xleftharpoonup" ||
512 s == "xleftrightharpoons" || s == "xrightleftharpoons" ||
514 return MathAtom(new InsetMathXArrow(buf, s));
515 if (s == "split" || s == "alignedat")
516 return MathAtom(new InsetMathSplit(buf, s));
518 return MathAtom(new InsetMathCases(buf));
520 return MathAtom(new InsetMathSubstack(buf));
521 if (s == "subarray" || s == "array")
522 return MathAtom(new InsetMathArray(buf, s, 1, 1));
524 return MathAtom(new InsetMathSqrt(buf));
526 return MathAtom(new InsetMathRoot(buf));
528 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
530 return MathAtom(new InsetMathStackrel(buf, false));
531 // This string value is only for math toolbar use, no LaTeX name
532 if (s == "stackrelthree")
533 return MathAtom(new InsetMathStackrel(buf, true));
535 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
537 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
539 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
541 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
543 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
545 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
547 return MathAtom(new InsetMathFrac(buf));
549 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
551 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
553 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
555 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
557 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
559 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
560 // These string values are only for math toolbar use, no LaTeX names
561 if (s == "unitfracthree")
562 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
564 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
566 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
567 if (s == "cfracleft")
568 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
569 if (s == "cfracright")
570 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
572 // return MathAtom(new MathInferInset);
574 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
576 return MathAtom(new InsetMathLefteqn(buf));
577 if (s == "boldsymbol")
578 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
580 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
581 if (s == "heavysymbol" || s == "hm")
582 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
583 if (s == "color" || s == "normalcolor")
584 return MathAtom(new InsetMathColor(buf, true));
585 if (s == "textcolor")
586 return MathAtom(new InsetMathColor(buf, false));
588 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
590 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
592 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
594 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::cancel));
596 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::bcancel));
598 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::xcancel));
600 return MathAtom(new InsetMathCancelto(buf));
602 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smash));
603 // The following 2 string values are only for math toolbar use, no LaTeX names
605 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smashb));
607 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smasht));
609 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathclap));
611 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathllap));
613 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathrlap));
614 if (s == "ensuremath")
615 return MathAtom(new InsetMathEnsureMath(buf));
617 return MathAtom(new InsetMathSideset(buf, true, true));
618 // The following 3 string values are only for math toolbar use, no LaTeX names
620 return MathAtom(new InsetMathSideset(buf, false, true));
622 return MathAtom(new InsetMathSideset(buf, true, false));
624 return MathAtom(new InsetMathSideset(buf, false, false));
625 if (isSpecialChar(s))
626 return MathAtom(new InsetMathSpecialChar(s));
628 return MathAtom(new InsetMathSpace(" ", ""));
631 return MathAtom(new InsetMathHull(buf, hullRegexp));
633 return MathAtom(new MathMacro(buf, s));
637 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
640 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
642 docstring body = split(str, name, ' ');
645 InsetCommandParams icp(REF_CODE);
647 InsetCommand::string2params(to_utf8(str), icp);
648 Encoding const * const utf8 = encodings.fromLyXName("utf8");
649 OutputParams op(utf8);
650 mathed_parse_cell(ar, icp.getCommand(op));
651 } else if (name == "mathspace") {
652 InsetSpaceParams isp(true);
653 InsetSpace::string2params(to_utf8(str), isp);
656 odocstringstream ods;
657 otexstream os(ods, texrow);
658 Encoding const * const ascii = encodings.fromLyXName("ascii");
659 OutputParams op(ascii);
661 mathed_parse_cell(ar, ods.str());
662 if (ar.size() == 2) {
664 if (ar[1].nucleus()->asBraceInset())
673 return ar[0].nucleus();
677 bool isAsciiOrMathAlpha(char_type c)
679 return isASCII(c) || Encodings::isMathAlpha(c);