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 "InsetMathTextsize.h"
47 #include "InsetMathUnderset.h"
48 #include "InsetMathUnknown.h"
49 #include "InsetMathHull.h"
50 #include "InsetMathXArrow.h"
51 #include "InsetMathXYMatrix.h"
52 #include "InsetMathDiagram.h"
53 #include "MacroTable.h"
54 #include "InsetMathMacro.h"
55 #include "InsetMathMacroArgument.h"
56 #include "MathParser.h"
57 #include "MathStream.h"
58 #include "MathSupport.h"
60 #include "insets/InsetCommand.h"
61 #include "insets/InsetSpace.h"
63 #include "support/debug.h"
64 #include "support/docstream.h"
65 #include "support/FileName.h"
66 #include "support/filetools.h" // LibFileSearch
67 #include "support/lstrings.h"
68 #include "support/textutils.h"
70 #include "frontends/FontLoader.h"
73 #include "BufferParams.h"
76 #include "LyX.h" // use_gui
81 using namespace lyx::support;
90 MathWordList theMathWordList;
93 bool isMathFontAvailable(string & name)
101 // Do we have the font proper?
102 if (theFontLoader().available(f))
106 if (name == "eufrak") {
107 name = "lyxfakefrak";
111 LYXERR(Debug::MATHED,
112 "font " << name << " not available and I can't fake it");
117 bool canBeDisplayed(char_type c)
121 return theFontLoader().canBeDisplayed(c);
125 bool isUnicodeSymbolAvailable(docstring const & name, char_type & c)
127 docstring cmd(from_ascii("\\") + name);
130 c = Encodings::fromLaTeXCommand(cmd, Encodings::MATH_CMD,
131 is_combining, termination);
132 if (c == 0 && name == "varOmega") {
133 // fallback for bug 7954, unicodesymbols does not list
134 // \\varOmega because of requirements, but this might change
135 cmd = from_ascii("\\mathit{\\Omega}");
136 c = Encodings::fromLaTeXCommand(cmd, Encodings::MATH_CMD,
137 is_combining, termination);
139 return c != 0 && !is_combining;
145 FileName const filename = libFileSearch(string(), "symbols");
146 LYXERR(Debug::MATHED, "read symbols from " << filename);
147 if (filename.empty()) {
148 lyxerr << "Could not find symbols file" << endl;
152 ifstream fs(filename.toFilesystemEncoding().c_str());
153 // limit the size of strings we read to avoid memory problems
157 while (getline(fs, line)) {
161 if (line.empty() || line[0] == '#')
164 // special case of iffont/else/endif
165 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
166 istringstream is(line);
167 // limit the size of strings we read to avoid memory problems
172 skip = !isMathFontAvailable(tmp);
174 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
177 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
183 // special case of pre-defined macros
184 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
185 //lyxerr << "macro definition: '" << line << '\'' << endl;
187 // \def\macroname{definition}
189 // \def\macroname{definition} requires
191 // \def\macroname{definition} extra xmlname requires
192 istringstream is(line);
198 is >> setw(65536) >> macro >> required;
199 if ((is >> xmlname)) {
201 if (!(is >> required))
205 MacroTable::iterator it = MacroTable::globalMacros().insert(
206 nullptr, from_utf8(macro));
207 if (!extra.empty() || !xmlname.empty() || !required.empty()) {
208 MathWordList::iterator wit = theMathWordList.find(it->first);
209 if (wit != theMathWordList.end())
210 LYXERR(Debug::MATHED, "readSymbols: inset "
211 << to_utf8(it->first) << " already exists.");
215 tmp.name = it->first;
216 tmp.extra = from_utf8(extra);
217 tmp.xmlname = from_utf8(xmlname);
218 if (required == "hiddensymbol") {
220 tmp.hidden = hidden = true;
222 tmp.required = required;
223 theMathWordList[it->first] = tmp;
224 wit = theMathWordList.find(it->first);
225 it->second.setSymbol(&(wit->second));
228 // If you change the following output, please adjust
229 // development/tools/generate_symbols_images.py.
230 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(it->first)
233 << " extra: " << extra
234 << " xml: " << xmlname
235 << " requires: " << required
236 << " hidden: " << hidden << '\'');
240 idocstringstream is(from_utf8(line));
243 is >> tmp.name >> help;
244 tmp.inset = to_ascii(help);
245 if (isFontName(tmp.inset)) {
246 is >> help >> fallbackid >> tmp.extra >> tmp.xmlname;
247 docstring cid, dsp_cid;
248 idocstringstream is2(subst(help, '|', ' '));
249 is2 >> charid >> dsp_charid;
252 // requires is optional
255 // backward compatibility
256 if (help == "esintoramsmath")
257 tmp.required = "esint|amsmath";
259 tmp.required = to_ascii(help);
262 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
263 << to_utf8(tmp.name) << ' ' << tmp.inset << ' '
264 << to_utf8(tmp.extra));
268 if (isFontName(tmp.inset)) {
269 // tmp.inset _is_ the fontname here.
270 // create fallbacks if necessary
272 // store requirements as long as we can
273 if (tmp.required.empty()) {
274 if (tmp.inset == "msa" || tmp.inset == "msb")
275 tmp.required = "amssymb";
276 else if (tmp.inset == "wasy")
277 tmp.required = "wasysym";
278 else if (tmp.inset == "mathscr")
279 tmp.required = "mathrsfs";
280 else if (tmp.inset == "mathds")
281 tmp.required = "dsfont";
284 // symbol font is not available sometimes
285 string symbol_font = "lyxsymbol";
286 char_type unicodesymbol = 0;
288 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
289 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
291 } else if (isMathFontAvailable(tmp.inset) && canBeDisplayed(charid)) {
292 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
293 tmp.draw.push_back(char_type(charid));
294 if (dsp_charid && canBeDisplayed(dsp_charid)) {
295 LYXERR(Debug::MATHED, "large symbol available for " << to_utf8(tmp.name));
296 tmp.dsp_draw.push_back(char_type(dsp_charid));
298 } else if (fallbackid && isMathFontAvailable(symbol_font) &&
299 canBeDisplayed(fallbackid)) {
300 if (tmp.inset == "cmex")
301 tmp.inset = "lyxsymbol";
303 tmp.inset = "lyxboldsymbol";
304 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
305 tmp.draw.push_back(char_type(fallbackid));
306 } else if (isUnicodeSymbolAvailable(tmp.name, unicodesymbol)) {
307 LYXERR(Debug::MATHED, "unicode fallback for " << to_utf8(tmp.name));
308 tmp.inset = "mathnormal";
309 tmp.draw.push_back(unicodesymbol);
311 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
313 tmp.inset = "lyxtex";
316 // it's a proper inset
317 LYXERR(Debug::MATHED, "inset " << tmp.inset
318 << " used for " << to_utf8(tmp.name));
321 if (tmp.required == "hiddensymbol")
327 if (theMathWordList.find(tmp.name) != theMathWordList.end())
328 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
329 << " already exists.");
331 theMathWordList[tmp.name] = tmp;
333 // If you change the following output, please adjust
334 // development/tools/generate_symbols_images.py.
335 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
336 << " inset: " << tmp.inset
337 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
338 << " extra: " << to_utf8(tmp.extra)
339 << " xml: " << to_utf8(tmp.xmlname)
340 << " requires: " << tmp.required
341 << " hidden: " << tmp.hidden << '\'');
344 string tmp2 = "cmsy";
345 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
349 bool isSpecialChar(docstring const & name)
351 if (name.size() != 1)
352 return name == "textasciicircum" || name == "mathcircumflex" ||
353 name == "textasciitilde" || name == "textbackslash";
355 char_type const c = name.at(0);
356 return c == '{' || c == '}' || c == '&' || c == '$' ||
357 c == '#' || c == '%' || c == '_';
363 MathWordList const & mathedWordList()
365 return theMathWordList;
371 static bool initialized = false;
380 bool ensureMath(TeXMathStream & os, bool needs_mathmode, bool macro,
383 bool brace = os.pendingBrace();
384 os.pendingBrace(false);
387 if (os.textMode() && needs_mathmode) {
393 os << "\\ensuremath{";
397 } else if (macro && textmode_macro && !os.textMode()) {
403 os << "\\lyxmathsym{";
407 } else if (macro && brace && !needs_mathmode && !textmode_macro) {
408 // This is a user defined macro, not a InsetMathMacro, so we
409 // cannot be sure what mode is needed. We leave it in the
410 // same environment it was entered by closing either \lyxmathsym
411 // or \ensuremath, whichever was opened.
414 os.textMode(!os.textMode());
420 int ensureMode(TeXMathStream & os, InsetMath::mode_type mode,
421 bool locked, bool ascii)
423 bool textmode = mode == InsetMath::TEXT_MODE;
424 if (os.latex() && textmode && os.pendingBrace()) {
426 os.pendingBrace(false);
427 os.pendingSpace(false);
430 int oldmodes = os.textMode() ? 0x01 : 0;
431 os.textMode(textmode);
432 oldmodes |= os.lockedMode() ? 0x02 : 0;
433 os.lockedMode(locked);
434 oldmodes |= os.asciiOnly() ? 0x04 : 0;
440 latexkeys const * in_word_set(docstring const & str)
442 MathWordList::iterator it = theMathWordList.find(str);
443 if (it == theMathWordList.end())
445 if (it->second.inset == "macro")
447 return &(it->second);
451 MathAtom createInsetMath(char const * const s, Buffer * buf)
453 return createInsetMath(from_utf8(s), buf);
457 MathAtom createInsetMath(docstring const & s, Buffer * buf)
459 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
460 if ((s == "ce" || s == "cf") && buf
461 && buf->params().use_package("mhchem") == BufferParams::package_off)
462 return MathAtom(new InsetMathMacro(buf, s));
464 latexkeys const * l = in_word_set(s);
466 string const & inset = l->inset;
467 //lyxerr << " found inset: '" << inset << '\'' << endl;
469 return MathAtom(new InsetMathRef(buf, l->name));
470 if (inset == "overset")
471 return MathAtom(new InsetMathOverset(buf));
472 if (inset == "underset")
473 return MathAtom(new InsetMathUnderset(buf));
474 if (inset == "decoration")
475 return MathAtom(new InsetMathDecoration(buf, l));
476 if (inset == "space")
477 return MathAtom(new InsetMathSpace(buf, to_ascii(l->name), ""));
478 if (inset == "class")
479 return MathAtom(new InsetMathClass(buf, string_to_class(s)));
481 return MathAtom(new InsetMathDots(buf, l));
483 return MathAtom(new InsetMathBox(buf, l->name));
484 // if (inset == "fbox")
485 // return MathAtom(new InsetMathFBox(l));
486 if (inset == "style")
487 return MathAtom(new InsetMathSize(buf, l));
489 return MathAtom(new InsetMathFont(buf, l));
490 if (inset == "oldfont")
491 return MathAtom(new InsetMathFontOld(buf, l));
492 if (inset == "textsize")
493 return MathAtom(new InsetMathTextsize(buf, l));
494 if (inset == "matrix")
495 return MathAtom(new InsetMathAMSArray(buf, s));
496 if (inset == "split")
497 return MathAtom(new InsetMathSplit(buf, s));
499 // we can't create a InsetMathBig, since the argument
501 return MathAtom(new InsetMathUnknown(buf, s));
502 return MathAtom(new InsetMathSymbol(buf, l));
505 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
506 return MathAtom(new InsetMathMacroArgument(buf, s[1] - '0'));
507 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
508 && s[2] >= '1' && s[2] <= '9')
509 return MathAtom(new InsetMathMacroArgument(buf, s[2] - '0'));
511 return MathAtom(new InsetMathBoxed(buf));
513 return MathAtom(new InsetMathFBox(buf));
515 return MathAtom(new InsetMathMakebox(buf, true));
517 return MathAtom(new InsetMathMakebox(buf, false));
518 if (s.substr(0, 8) == "xymatrix") {
519 char spacing_code = '\0';
521 bool equal_spacing = false;
522 size_t const len = s.length();
524 if (i < len && s[i] == '@') {
526 if (i < len && s[i] == '!') {
527 equal_spacing = true;
534 spacing_code = static_cast<char>(s[i]);
537 } else if (i < len) {
545 spacing_code = static_cast<char>(s[i]);
549 if (i < len && s[i] == '=') {
551 spacing = Length(to_ascii(s.substr(i)));
555 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
560 return MathAtom(new InsetMathDiagram(buf));
561 if (s == "xrightarrow" || s == "xleftarrow" ||
562 s == "xhookrightarrow" || s == "xhookleftarrow" ||
563 s == "xRightarrow" || s == "xLeftarrow" ||
564 s == "xleftrightarrow" || s == "xLeftrightarrow" ||
565 s == "xrightharpoondown" || s == "xrightharpoonup" ||
566 s == "xleftharpoondown" || s == "xleftharpoonup" ||
567 s == "xleftrightharpoons" || s == "xrightleftharpoons" ||
569 return MathAtom(new InsetMathXArrow(buf, s));
570 if (s == "split" || s == "alignedat")
571 return MathAtom(new InsetMathSplit(buf, s));
573 return MathAtom(new InsetMathCases(buf));
575 return MathAtom(new InsetMathSubstack(buf));
576 if (s == "subarray" || s == "array")
577 return MathAtom(new InsetMathArray(buf, s, 1, 1));
579 return MathAtom(new InsetMathSqrt(buf));
581 return MathAtom(new InsetMathRoot(buf));
583 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
585 return MathAtom(new InsetMathStackrel(buf, false));
586 // This string value is only for math toolbar use, no LaTeX name
587 if (s == "stackrelthree")
588 return MathAtom(new InsetMathStackrel(buf, true));
590 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
592 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
594 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
596 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
598 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
600 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
602 return MathAtom(new InsetMathFrac(buf));
604 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
606 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
608 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
610 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
612 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
614 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
615 // These string values are only for math toolbar use, no LaTeX names
616 if (s == "unitfracthree")
617 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
619 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
621 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
622 if (s == "cfracleft")
623 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
624 if (s == "cfracright")
625 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
626 if (s == "case") // TODO: only if class is aastex(6|62)
627 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::AASTEX_CASE));
629 // return MathAtom(new MathInferInset);
631 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
633 return MathAtom(new InsetMathLefteqn(buf));
634 if (s == "boldsymbol")
635 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
637 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
638 if (s == "heavysymbol" || s == "hm")
639 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
640 if (s == "color" || s == "normalcolor")
641 return MathAtom(new InsetMathColor(buf, true));
642 if (s == "textcolor")
643 return MathAtom(new InsetMathColor(buf, false));
645 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
647 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
649 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
651 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::cancel));
653 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::bcancel));
655 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::xcancel));
657 return MathAtom(new InsetMathCancelto(buf));
659 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smash));
660 // The following 2 string values are only for math toolbar use, no LaTeX names
662 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smashb));
664 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smasht));
666 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathclap));
668 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathllap));
670 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathrlap));
671 if (s == "ensuremath")
672 return MathAtom(new InsetMathEnsureMath(buf));
674 return MathAtom(new InsetMathSideset(buf, true, true));
675 // The following 3 string values are only for math toolbar use, no LaTeX names
677 return MathAtom(new InsetMathSideset(buf, false, true));
679 return MathAtom(new InsetMathSideset(buf, true, false));
681 return MathAtom(new InsetMathSideset(buf, false, false));
682 if (isSpecialChar(s))
683 return MathAtom(new InsetMathSpecialChar(buf, s));
685 return MathAtom(new InsetMathSpace(buf, " ", ""));
687 return MathAtom(new InsetMathHull(buf, hullRegexp));
689 return MathAtom(new InsetMathMacro(buf, s));
693 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
696 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
698 docstring body = split(str, name, ' ');
701 InsetCommandParams icp(REF_CODE);
703 InsetCommand::string2params(to_utf8(str), icp);
704 Encoding const * const utf8 = encodings.fromLyXName("utf8");
705 OutputParams op(utf8);
706 mathed_parse_cell(ar, icp.getCommand(op, false, true));
707 } else if (name == "mathspace") {
708 InsetSpaceParams isp(true);
709 InsetSpace::string2params(to_utf8(str), isp);
711 odocstringstream ods;
713 Encoding const * const ascii = encodings.fromLyXName("ascii");
714 OutputParams op(ascii);
716 mathed_parse_cell(ar, ods.str());
717 if (ar.size() == 2) {
719 if (ar[1].nucleus()->asBraceInset())
728 return ar[0].nucleus();