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 "InsetMathClass.h"
23 #include "InsetMathColor.h"
24 #include "InsetMathDecoration.h"
25 #include "InsetMathDots.h"
26 #include "InsetMathEnsureMath.h"
27 #include "InsetMathFont.h"
28 #include "InsetMathFontOld.h"
29 #include "InsetMathFrac.h"
30 #include "InsetMathKern.h"
31 #include "InsetMathLefteqn.h"
32 #include "InsetMathOverset.h"
33 #include "InsetMathPhantom.h"
34 #include "InsetMathRef.h"
35 #include "InsetMathRoot.h"
36 #include "InsetMathSideset.h"
37 #include "InsetMathSize.h"
38 #include "InsetMathSpace.h"
39 #include "InsetMathSpecialChar.h"
40 #include "InsetMathSplit.h"
41 #include "InsetMathSqrt.h"
42 #include "InsetMathStackrel.h"
43 #include "InsetMathSubstack.h"
44 #include "InsetMathSymbol.h"
45 #include "InsetMathTabular.h"
46 #include "InsetMathUnderset.h"
47 #include "InsetMathUnknown.h"
48 #include "InsetMathHull.h"
49 #include "InsetMathXArrow.h"
50 #include "InsetMathXYMatrix.h"
51 #include "InsetMathDiagram.h"
52 #include "MacroTable.h"
53 #include "InsetMathMacro.h"
54 #include "InsetMathMacroArgument.h"
55 #include "MathParser.h"
56 #include "MathStream.h"
57 #include "MathSupport.h"
59 #include "insets/InsetCommand.h"
60 #include "insets/InsetSpace.h"
62 #include "support/debug.h"
63 #include "support/docstream.h"
64 #include "support/FileName.h"
65 #include "support/filetools.h" // LibFileSearch
66 #include "support/lstrings.h"
67 #include "support/textutils.h"
69 #include "frontends/FontLoader.h"
72 #include "BufferParams.h"
75 #include "LyX.h" // use_gui
80 using namespace lyx::support;
89 MathWordList theMathWordList;
92 bool isMathFontAvailable(string & name)
100 // Do we have the font proper?
101 if (theFontLoader().available(f))
105 if (name == "eufrak") {
106 name = "lyxfakefrak";
110 LYXERR(Debug::MATHED,
111 "font " << name << " not available and I can't fake it");
116 bool canBeDisplayed(char_type c)
120 return theFontLoader().canBeDisplayed(c);
124 bool isUnicodeSymbolAvailable(docstring const & name, char_type & c)
126 docstring cmd(from_ascii("\\") + name);
129 c = Encodings::fromLaTeXCommand(cmd, Encodings::MATH_CMD,
130 is_combining, termination);
131 if (c == 0 && name == "varOmega") {
132 // fallback for bug 7954, unicodesymbols does not list
133 // \\varOmega because of requirements, but this might change
134 cmd = from_ascii("\\mathit{\\Omega}");
135 c = Encodings::fromLaTeXCommand(cmd, Encodings::MATH_CMD,
136 is_combining, termination);
138 return c != 0 && !is_combining;
144 FileName const filename = libFileSearch(string(), "symbols");
145 LYXERR(Debug::MATHED, "read symbols from " << filename);
146 if (filename.empty()) {
147 lyxerr << "Could not find symbols file" << endl;
151 ifstream fs(filename.toFilesystemEncoding().c_str());
152 // limit the size of strings we read to avoid memory problems
156 while (getline(fs, line)) {
160 if (line.empty() || line[0] == '#')
163 // special case of iffont/else/endif
164 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
165 istringstream is(line);
166 // limit the size of strings we read to avoid memory problems
171 skip = !isMathFontAvailable(tmp);
173 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
176 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
182 // special case of pre-defined macros
183 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
184 //lyxerr << "macro definition: '" << line << '\'' << endl;
186 // \def\macroname{definition}
188 // \def\macroname{definition} requires
190 // \def\macroname{definition} extra xmlname requires
191 istringstream is(line);
197 is >> setw(65536) >> macro >> required;
198 if ((is >> xmlname)) {
200 if (!(is >> required))
204 MacroTable::iterator it = MacroTable::globalMacros().insert(
205 nullptr, from_utf8(macro));
206 if (!extra.empty() || !xmlname.empty() || !required.empty()) {
207 MathWordList::iterator wit = theMathWordList.find(it->first);
208 if (wit != theMathWordList.end())
209 LYXERR(Debug::MATHED, "readSymbols: inset "
210 << to_utf8(it->first) << " already exists.");
214 tmp.name = it->first;
215 tmp.extra = from_utf8(extra);
216 tmp.xmlname = from_utf8(xmlname);
217 if (required == "hiddensymbol") {
219 tmp.hidden = hidden = true;
221 tmp.required = required;
222 theMathWordList[it->first] = tmp;
223 wit = theMathWordList.find(it->first);
224 it->second.setSymbol(&(wit->second));
227 // If you change the following output, please adjust
228 // development/tools/generate_symbols_images.py.
229 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(it->first)
232 << " extra: " << extra
233 << " xml: " << xmlname
234 << " requires: " << required
235 << " hidden: " << hidden << '\'');
239 idocstringstream is(from_utf8(line));
242 is >> tmp.name >> help;
243 tmp.inset = to_ascii(help);
244 if (isFontName(tmp.inset)) {
245 is >> help >> fallbackid >> tmp.extra >> tmp.xmlname;
246 docstring cid, dsp_cid;
247 idocstringstream is2(subst(help, '|', ' '));
248 is2 >> charid >> dsp_charid;
251 // requires is optional
254 // backward compatibility
255 if (help == "esintoramsmath")
256 tmp.required = "esint|amsmath";
258 tmp.required = to_ascii(help);
261 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
262 << to_utf8(tmp.name) << ' ' << tmp.inset << ' '
263 << to_utf8(tmp.extra));
267 if (isFontName(tmp.inset)) {
268 // tmp.inset _is_ the fontname here.
269 // create fallbacks if necessary
271 // store requirements as long as we can
272 if (tmp.required.empty()) {
273 if (tmp.inset == "msa" || tmp.inset == "msb")
274 tmp.required = "amssymb";
275 else if (tmp.inset == "wasy")
276 tmp.required = "wasysym";
277 else if (tmp.inset == "mathscr")
278 tmp.required = "mathrsfs";
279 else if (tmp.inset == "mathds")
280 tmp.required = "dsfont";
283 // symbol font is not available sometimes
284 string symbol_font = "lyxsymbol";
285 char_type unicodesymbol = 0;
287 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
288 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
290 } else if (isMathFontAvailable(tmp.inset) && canBeDisplayed(charid)) {
291 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
292 tmp.draw.push_back(char_type(charid));
293 if (dsp_charid && canBeDisplayed(dsp_charid)) {
294 LYXERR(Debug::MATHED, "large symbol available for " << to_utf8(tmp.name));
295 tmp.dsp_draw.push_back(char_type(dsp_charid));
297 } else if (fallbackid && isMathFontAvailable(symbol_font) &&
298 canBeDisplayed(fallbackid)) {
299 if (tmp.inset == "cmex")
300 tmp.inset = "lyxsymbol";
302 tmp.inset = "lyxboldsymbol";
303 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
304 tmp.draw.push_back(char_type(fallbackid));
305 } else if (isUnicodeSymbolAvailable(tmp.name, unicodesymbol)) {
306 LYXERR(Debug::MATHED, "unicode fallback for " << to_utf8(tmp.name));
307 tmp.inset = "mathnormal";
308 tmp.draw.push_back(unicodesymbol);
310 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
312 tmp.inset = "lyxtex";
315 // it's a proper inset
316 LYXERR(Debug::MATHED, "inset " << tmp.inset
317 << " used for " << to_utf8(tmp.name));
320 if (tmp.required == "hiddensymbol")
326 if (theMathWordList.find(tmp.name) != theMathWordList.end())
327 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
328 << " already exists.");
330 theMathWordList[tmp.name] = tmp;
332 // If you change the following output, please adjust
333 // development/tools/generate_symbols_images.py.
334 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
335 << " inset: " << tmp.inset
336 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
337 << " extra: " << to_utf8(tmp.extra)
338 << " xml: " << to_utf8(tmp.xmlname)
339 << " requires: " << tmp.required
340 << " hidden: " << tmp.hidden << '\'');
343 string tmp2 = "cmsy";
344 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
348 bool isSpecialChar(docstring const & name)
350 if (name.size() != 1)
351 return name == "textasciicircum" || name == "mathcircumflex" ||
352 name == "textasciitilde" || name == "textbackslash";
354 char_type const c = name.at(0);
355 return c == '{' || c == '}' || c == '&' || c == '$' ||
356 c == '#' || c == '%' || c == '_';
362 MathWordList const & mathedWordList()
364 return theMathWordList;
370 static bool initialized = false;
379 bool ensureMath(TeXMathStream & os, bool needs_mathmode, bool macro,
382 bool brace = os.pendingBrace();
383 os.pendingBrace(false);
386 if (os.textMode() && needs_mathmode) {
392 os << "\\ensuremath{";
396 } else if (macro && textmode_macro && !os.textMode()) {
402 os << "\\lyxmathsym{";
406 } else if (macro && brace && !needs_mathmode && !textmode_macro) {
407 // This is a user defined macro, not a InsetMathMacro, so we
408 // cannot be sure what mode is needed. We leave it in the
409 // same environment it was entered by closing either \lyxmathsym
410 // or \ensuremath, whichever was opened.
413 os.textMode(!os.textMode());
419 int ensureMode(TeXMathStream & os, InsetMath::mode_type mode,
420 bool locked, bool ascii)
422 bool textmode = mode == InsetMath::TEXT_MODE;
423 if (os.latex() && textmode && os.pendingBrace()) {
425 os.pendingBrace(false);
426 os.pendingSpace(false);
429 int oldmodes = os.textMode() ? 0x01 : 0;
430 os.textMode(textmode);
431 oldmodes |= os.lockedMode() ? 0x02 : 0;
432 os.lockedMode(locked);
433 oldmodes |= os.asciiOnly() ? 0x04 : 0;
439 latexkeys const * in_word_set(docstring const & str)
441 MathWordList::iterator it = theMathWordList.find(str);
442 if (it == theMathWordList.end())
444 if (it->second.inset == "macro")
446 return &(it->second);
450 MathAtom createInsetMath(char const * const s, Buffer * buf)
452 return createInsetMath(from_utf8(s), buf);
456 MathAtom createInsetMath(docstring const & s, Buffer * buf)
458 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
459 if ((s == "ce" || s == "cf") && buf
460 && buf->params().use_package("mhchem") == BufferParams::package_off)
461 return MathAtom(new InsetMathMacro(buf, s));
463 latexkeys const * l = in_word_set(s);
465 string const & inset = l->inset;
466 //lyxerr << " found inset: '" << inset << '\'' << endl;
468 return MathAtom(new InsetMathRef(buf, l->name));
469 if (inset == "overset")
470 return MathAtom(new InsetMathOverset(buf));
471 if (inset == "underset")
472 return MathAtom(new InsetMathUnderset(buf));
473 if (inset == "decoration")
474 return MathAtom(new InsetMathDecoration(buf, l));
475 if (inset == "space")
476 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
477 if (inset == "class")
478 return MathAtom(new InsetMathClass(buf, string_to_class(s)));
480 return MathAtom(new InsetMathDots(l));
482 return MathAtom(new InsetMathBox(buf, l->name));
483 // if (inset == "fbox")
484 // return MathAtom(new InsetMathFBox(l));
485 if (inset == "style")
486 return MathAtom(new InsetMathSize(buf, l));
488 return MathAtom(new InsetMathFont(buf, l));
489 if (inset == "oldfont")
490 return MathAtom(new InsetMathFontOld(buf, l));
491 if (inset == "matrix")
492 return MathAtom(new InsetMathAMSArray(buf, s));
493 if (inset == "split")
494 return MathAtom(new InsetMathSplit(buf, s));
496 // we can't create a InsetMathBig, since the argument
498 return MathAtom(new InsetMathUnknown(s));
499 return MathAtom(new InsetMathSymbol(l));
502 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
503 return MathAtom(new InsetMathMacroArgument(s[1] - '0'));
504 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
505 && s[2] >= '1' && s[2] <= '9')
506 return MathAtom(new InsetMathMacroArgument(s[2] - '0'));
508 return MathAtom(new InsetMathBoxed(buf));
510 return MathAtom(new InsetMathFBox(buf));
512 return MathAtom(new InsetMathMakebox(buf, true));
514 return MathAtom(new InsetMathMakebox(buf, false));
515 if (s.substr(0, 8) == "xymatrix") {
516 char spacing_code = '\0';
518 bool equal_spacing = false;
519 size_t const len = s.length();
521 if (i < len && s[i] == '@') {
523 if (i < len && s[i] == '!') {
524 equal_spacing = true;
531 spacing_code = static_cast<char>(s[i]);
534 } else if (i < len) {
542 spacing_code = static_cast<char>(s[i]);
546 if (i < len && s[i] == '=') {
548 spacing = Length(to_ascii(s.substr(i)));
552 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
557 return MathAtom(new InsetMathDiagram(buf));
558 if (s == "xrightarrow" || s == "xleftarrow" ||
559 s == "xhookrightarrow" || s == "xhookleftarrow" ||
560 s == "xRightarrow" || s == "xLeftarrow" ||
561 s == "xleftrightarrow" || s == "xLeftrightarrow" ||
562 s == "xrightharpoondown" || s == "xrightharpoonup" ||
563 s == "xleftharpoondown" || s == "xleftharpoonup" ||
564 s == "xleftrightharpoons" || s == "xrightleftharpoons" ||
566 return MathAtom(new InsetMathXArrow(buf, s));
567 if (s == "split" || s == "alignedat")
568 return MathAtom(new InsetMathSplit(buf, s));
570 return MathAtom(new InsetMathCases(buf));
572 return MathAtom(new InsetMathSubstack(buf));
573 if (s == "subarray" || s == "array")
574 return MathAtom(new InsetMathArray(buf, s, 1, 1));
576 return MathAtom(new InsetMathSqrt(buf));
578 return MathAtom(new InsetMathRoot(buf));
580 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
582 return MathAtom(new InsetMathStackrel(buf, false));
583 // This string value is only for math toolbar use, no LaTeX name
584 if (s == "stackrelthree")
585 return MathAtom(new InsetMathStackrel(buf, true));
587 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
589 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
591 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
593 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
595 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
597 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
599 return MathAtom(new InsetMathFrac(buf));
601 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
603 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
605 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
607 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
609 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
611 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
612 // These string values are only for math toolbar use, no LaTeX names
613 if (s == "unitfracthree")
614 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
616 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
618 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
619 if (s == "cfracleft")
620 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
621 if (s == "cfracright")
622 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
623 if (s == "case") // TODO: only if class is aastex(6|62)
624 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::AASTEX_CASE));
626 // return MathAtom(new MathInferInset);
628 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
630 return MathAtom(new InsetMathLefteqn(buf));
631 if (s == "boldsymbol")
632 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
634 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
635 if (s == "heavysymbol" || s == "hm")
636 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
637 if (s == "color" || s == "normalcolor")
638 return MathAtom(new InsetMathColor(buf, true));
639 if (s == "textcolor")
640 return MathAtom(new InsetMathColor(buf, false));
642 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
644 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
646 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
648 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::cancel));
650 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::bcancel));
652 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::xcancel));
654 return MathAtom(new InsetMathCancelto(buf));
656 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smash));
657 // The following 2 string values are only for math toolbar use, no LaTeX names
659 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smashb));
661 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smasht));
663 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathclap));
665 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathllap));
667 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathrlap));
668 if (s == "ensuremath")
669 return MathAtom(new InsetMathEnsureMath(buf));
671 return MathAtom(new InsetMathSideset(buf, true, true));
672 // The following 3 string values are only for math toolbar use, no LaTeX names
674 return MathAtom(new InsetMathSideset(buf, false, true));
676 return MathAtom(new InsetMathSideset(buf, true, false));
678 return MathAtom(new InsetMathSideset(buf, false, false));
679 if (isSpecialChar(s))
680 return MathAtom(new InsetMathSpecialChar(s));
682 return MathAtom(new InsetMathSpace(" ", ""));
684 return MathAtom(new InsetMathHull(buf, hullRegexp));
686 return MathAtom(new InsetMathMacro(buf, s));
690 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
693 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
695 docstring body = split(str, name, ' ');
698 InsetCommandParams icp(REF_CODE);
700 InsetCommand::string2params(to_utf8(str), icp);
701 Encoding const * const utf8 = encodings.fromLyXName("utf8");
702 OutputParams op(utf8);
703 mathed_parse_cell(ar, icp.getCommand(op));
704 } else if (name == "mathspace") {
705 InsetSpaceParams isp(true);
706 InsetSpace::string2params(to_utf8(str), isp);
708 odocstringstream ods;
710 Encoding const * const ascii = encodings.fromLyXName("ascii");
711 OutputParams op(ascii);
713 mathed_parse_cell(ar, ods.str());
714 if (ar.size() == 2) {
716 if (ar[1].nucleus()->asBraceInset())
725 return ar[0].nucleus();