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 htmlname xmlname requires
191 istringstream is(line);
198 is >> setw(65536) >> macro >> required;
199 if ((is >> htmlname >> xmlname)) {
201 if (!(is >> required))
204 htmlname = xmlname = "";
205 MacroTable::iterator it = MacroTable::globalMacros().insert(
206 nullptr, from_utf8(macro));
207 if (!extra.empty() || !htmlname.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.htmlname = from_utf8(htmlname);
218 tmp.xmlname = from_utf8(xmlname);
219 if (required == "hiddensymbol") {
221 tmp.hidden = hidden = true;
223 tmp.required = required;
224 theMathWordList[it->first] = tmp;
225 wit = theMathWordList.find(it->first);
226 it->second.setSymbol(&(wit->second));
229 // If you change the following output, please adjust
230 // development/tools/generate_symbols_images.py.
231 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(it->first)
234 << " extra: " << extra
235 << " html: " << htmlname
236 << " xml: " << xmlname
237 << " requires: " << required
238 << " hidden: " << hidden << '\'');
242 idocstringstream is(from_utf8(line));
245 is >> tmp.name >> help;
246 tmp.inset = to_ascii(help);
247 if (isFontName(tmp.inset)) {
248 is >> help >> fallbackid >> tmp.extra >> tmp.htmlname >> tmp.xmlname;
249 docstring cid, dsp_cid;
250 idocstringstream is2(subst(help, '|', ' '));
251 is2 >> charid >> dsp_charid;
254 // requires is optional
257 // backward compatibility
258 if (help == "esintoramsmath")
259 tmp.required = "esint|amsmath";
261 tmp.required = to_ascii(help);
264 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
265 << to_utf8(tmp.name) << ' ' << tmp.inset << ' '
266 << to_utf8(tmp.extra));
270 if (isFontName(tmp.inset)) {
271 // tmp.inset _is_ the fontname here.
272 // create fallbacks if necessary
274 // store requirements as long as we can
275 if (tmp.required.empty()) {
276 if (tmp.inset == "msa" || tmp.inset == "msb")
277 tmp.required = "amssymb";
278 else if (tmp.inset == "wasy")
279 tmp.required = "wasysym";
280 else if (tmp.inset == "mathscr")
281 tmp.required = "mathrsfs";
282 else if (tmp.inset == "mathds")
283 tmp.required = "dsfont";
286 // symbol font is not available sometimes
287 string symbol_font = "lyxsymbol";
288 char_type unicodesymbol = 0;
290 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
291 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
293 } else if (isMathFontAvailable(tmp.inset) && canBeDisplayed(charid)) {
294 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
295 tmp.draw.push_back(char_type(charid));
296 if (dsp_charid && canBeDisplayed(dsp_charid)) {
297 LYXERR(Debug::MATHED, "large symbol available for " << to_utf8(tmp.name));
298 tmp.dsp_draw.push_back(char_type(dsp_charid));
300 } else if (fallbackid && isMathFontAvailable(symbol_font) &&
301 canBeDisplayed(fallbackid)) {
302 if (tmp.inset == "cmex")
303 tmp.inset = "lyxsymbol";
305 tmp.inset = "lyxboldsymbol";
306 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
307 tmp.draw.push_back(char_type(fallbackid));
308 } else if (isUnicodeSymbolAvailable(tmp.name, unicodesymbol)) {
309 LYXERR(Debug::MATHED, "unicode fallback for " << to_utf8(tmp.name));
310 tmp.inset = "mathnormal";
311 tmp.draw.push_back(unicodesymbol);
313 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
315 tmp.inset = "lyxtex";
318 // it's a proper inset
319 LYXERR(Debug::MATHED, "inset " << tmp.inset
320 << " used for " << to_utf8(tmp.name));
323 if (tmp.required == "hiddensymbol")
329 if (theMathWordList.find(tmp.name) != theMathWordList.end())
330 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
331 << " already exists.");
333 theMathWordList[tmp.name] = tmp;
335 // If you change the following output, please adjust
336 // development/tools/generate_symbols_images.py.
337 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
338 << " inset: " << tmp.inset
339 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
340 << " extra: " << to_utf8(tmp.extra)
341 << " html: " << to_utf8(tmp.htmlname)
342 << " xml: " << to_utf8(tmp.xmlname)
343 << " requires: " << tmp.required
344 << " hidden: " << tmp.hidden << '\'');
347 string tmp2 = "cmsy";
348 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
352 bool isSpecialChar(docstring const & name)
354 if (name.size() != 1)
355 return name == "textasciicircum" || name == "mathcircumflex" ||
356 name == "textasciitilde" || name == "textbackslash";
358 char_type const c = name.at(0);
359 return c == '{' || c == '}' || c == '&' || c == '$' ||
360 c == '#' || c == '%' || c == '_';
366 MathWordList const & mathedWordList()
368 return theMathWordList;
374 static bool initialized = false;
383 bool ensureMath(TeXMathStream & os, bool needs_mathmode, bool macro,
386 bool brace = os.pendingBrace();
387 os.pendingBrace(false);
390 if (os.textMode() && needs_mathmode) {
396 os << "\\ensuremath{";
400 } else if (macro && textmode_macro && !os.textMode()) {
406 os << "\\lyxmathsym{";
410 } else if (macro && brace && !needs_mathmode && !textmode_macro) {
411 // This is a user defined macro, not a InsetMathMacro, so we
412 // cannot be sure what mode is needed. We leave it in the
413 // same environment it was entered by closing either \lyxmathsym
414 // or \ensuremath, whichever was opened.
417 os.textMode(!os.textMode());
423 int ensureMode(TeXMathStream & os, InsetMath::mode_type mode,
424 bool locked, bool ascii)
426 bool textmode = mode == InsetMath::TEXT_MODE;
427 if (os.latex() && textmode && os.pendingBrace()) {
429 os.pendingBrace(false);
430 os.pendingSpace(false);
433 int oldmodes = os.textMode() ? 0x01 : 0;
434 os.textMode(textmode);
435 oldmodes |= os.lockedMode() ? 0x02 : 0;
436 os.lockedMode(locked);
437 oldmodes |= os.asciiOnly() ? 0x04 : 0;
443 latexkeys const * in_word_set(docstring const & str)
445 MathWordList::iterator it = theMathWordList.find(str);
446 if (it == theMathWordList.end())
448 if (it->second.inset == "macro")
450 return &(it->second);
454 MathAtom createInsetMath(char const * const s, Buffer * buf)
456 return createInsetMath(from_utf8(s), buf);
460 MathAtom createInsetMath(docstring const & s, Buffer * buf)
462 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
463 if ((s == "ce" || s == "cf") && buf
464 && buf->params().use_package("mhchem") == BufferParams::package_off)
465 return MathAtom(new InsetMathMacro(buf, s));
467 latexkeys const * l = in_word_set(s);
469 string const & inset = l->inset;
470 //lyxerr << " found inset: '" << inset << '\'' << endl;
472 return MathAtom(new InsetMathRef(buf, l->name));
473 if (inset == "overset")
474 return MathAtom(new InsetMathOverset(buf));
475 if (inset == "underset")
476 return MathAtom(new InsetMathUnderset(buf));
477 if (inset == "decoration")
478 return MathAtom(new InsetMathDecoration(buf, l));
479 if (inset == "space")
480 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
481 if (inset == "class")
482 return MathAtom(new InsetMathClass(buf, string_to_class(s)));
484 return MathAtom(new InsetMathDots(l));
486 return MathAtom(new InsetMathBox(buf, l->name));
487 // if (inset == "fbox")
488 // return MathAtom(new InsetMathFBox(l));
489 if (inset == "style")
490 return MathAtom(new InsetMathSize(buf, l));
492 return MathAtom(new InsetMathFont(buf, l));
493 if (inset == "oldfont")
494 return MathAtom(new InsetMathFontOld(buf, l));
495 if (inset == "matrix")
496 return MathAtom(new InsetMathAMSArray(buf, s));
497 if (inset == "split")
498 return MathAtom(new InsetMathSplit(buf, s));
500 // we can't create a InsetMathBig, since the argument
502 return MathAtom(new InsetMathUnknown(s));
503 return MathAtom(new InsetMathSymbol(l));
506 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
507 return MathAtom(new InsetMathMacroArgument(s[1] - '0'));
508 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
509 && s[2] >= '1' && s[2] <= '9')
510 return MathAtom(new InsetMathMacroArgument(s[2] - '0'));
512 return MathAtom(new InsetMathBoxed(buf));
514 return MathAtom(new InsetMathFBox(buf));
516 return MathAtom(new InsetMathMakebox(buf, true));
518 return MathAtom(new InsetMathMakebox(buf, false));
519 if (s.substr(0, 8) == "xymatrix") {
520 char spacing_code = '\0';
522 bool equal_spacing = false;
523 size_t const len = s.length();
525 if (i < len && s[i] == '@') {
527 if (i < len && s[i] == '!') {
528 equal_spacing = true;
535 spacing_code = static_cast<char>(s[i]);
538 } else if (i < len) {
546 spacing_code = static_cast<char>(s[i]);
550 if (i < len && s[i] == '=') {
552 spacing = Length(to_ascii(s.substr(i)));
556 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
561 return MathAtom(new InsetMathDiagram(buf));
562 if (s == "xrightarrow" || s == "xleftarrow" ||
563 s == "xhookrightarrow" || s == "xhookleftarrow" ||
564 s == "xRightarrow" || s == "xLeftarrow" ||
565 s == "xleftrightarrow" || s == "xLeftrightarrow" ||
566 s == "xrightharpoondown" || s == "xrightharpoonup" ||
567 s == "xleftharpoondown" || s == "xleftharpoonup" ||
568 s == "xleftrightharpoons" || s == "xrightleftharpoons" ||
570 return MathAtom(new InsetMathXArrow(buf, s));
571 if (s == "split" || s == "alignedat")
572 return MathAtom(new InsetMathSplit(buf, s));
574 return MathAtom(new InsetMathCases(buf));
576 return MathAtom(new InsetMathSubstack(buf));
577 if (s == "subarray" || s == "array")
578 return MathAtom(new InsetMathArray(buf, s, 1, 1));
580 return MathAtom(new InsetMathSqrt(buf));
582 return MathAtom(new InsetMathRoot(buf));
584 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
586 return MathAtom(new InsetMathStackrel(buf, false));
587 // This string value is only for math toolbar use, no LaTeX name
588 if (s == "stackrelthree")
589 return MathAtom(new InsetMathStackrel(buf, true));
591 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
593 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
595 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
597 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
599 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
601 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
603 return MathAtom(new InsetMathFrac(buf));
605 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
607 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
609 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
611 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
613 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
615 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
616 // These string values are only for math toolbar use, no LaTeX names
617 if (s == "unitfracthree")
618 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
620 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
622 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
623 if (s == "cfracleft")
624 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
625 if (s == "cfracright")
626 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
627 if (s == "case") // TODO: only if class is aastex(6|62)
628 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::AASTEX_CASE));
630 // return MathAtom(new MathInferInset);
632 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
634 return MathAtom(new InsetMathLefteqn(buf));
635 if (s == "boldsymbol")
636 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
638 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
639 if (s == "heavysymbol" || s == "hm")
640 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
641 if (s == "color" || s == "normalcolor")
642 return MathAtom(new InsetMathColor(buf, true));
643 if (s == "textcolor")
644 return MathAtom(new InsetMathColor(buf, false));
646 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
648 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
650 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
652 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::cancel));
654 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::bcancel));
656 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::xcancel));
658 return MathAtom(new InsetMathCancelto(buf));
660 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smash));
661 // The following 2 string values are only for math toolbar use, no LaTeX names
663 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smashb));
665 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smasht));
667 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathclap));
669 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathllap));
671 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathrlap));
672 if (s == "ensuremath")
673 return MathAtom(new InsetMathEnsureMath(buf));
675 return MathAtom(new InsetMathSideset(buf, true, true));
676 // The following 3 string values are only for math toolbar use, no LaTeX names
678 return MathAtom(new InsetMathSideset(buf, false, true));
680 return MathAtom(new InsetMathSideset(buf, true, false));
682 return MathAtom(new InsetMathSideset(buf, false, false));
683 if (isSpecialChar(s))
684 return MathAtom(new InsetMathSpecialChar(s));
686 return MathAtom(new InsetMathSpace(" ", ""));
688 return MathAtom(new InsetMathHull(buf, hullRegexp));
690 return MathAtom(new InsetMathMacro(buf, s));
694 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
697 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
699 docstring body = split(str, name, ' ');
702 InsetCommandParams icp(REF_CODE);
704 InsetCommand::string2params(to_utf8(str), icp);
705 Encoding const * const utf8 = encodings.fromLyXName("utf8");
706 OutputParams op(utf8);
707 mathed_parse_cell(ar, icp.getCommand(op));
708 } else if (name == "mathspace") {
709 InsetSpaceParams isp(true);
710 InsetSpace::string2params(to_utf8(str), isp);
712 odocstringstream ods;
714 Encoding const * const ascii = encodings.fromLyXName("ascii");
715 OutputParams op(ascii);
717 mathed_parse_cell(ar, ods.str());
718 if (ar.size() == 2) {
720 if (ar[1].nucleus()->asBraceInset())
729 return ar[0].nucleus();