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"
74 #include "LyX.h" // use_gui
79 using namespace lyx::support;
88 MathWordList theMathWordList;
91 bool isMathFontAvailable(string & name)
99 // Do we have the font proper?
100 if (theFontLoader().available(f))
104 if (name == "eufrak") {
105 name = "lyxfakefrak";
109 LYXERR(Debug::MATHED,
110 "font " << name << " not available and I can't fake it");
115 bool canBeDisplayed(char_type c)
119 return theFontLoader().canBeDisplayed(c);
123 bool isUnicodeSymbolAvailable(docstring const & name, char_type & c)
125 docstring cmd(from_ascii("\\") + name);
128 c = Encodings::fromLaTeXCommand(cmd, Encodings::MATH_CMD,
129 is_combining, termination);
130 if (c == 0 && name == "varOmega") {
131 // fallback for bug 7954, unicodesymbols does not list
132 // \\varOmega because of requirements, but this might change
133 cmd = from_ascii("\\mathit{\\Omega}");
134 c = Encodings::fromLaTeXCommand(cmd, Encodings::MATH_CMD,
135 is_combining, termination);
137 return c != 0 && !is_combining;
143 FileName const filename = libFileSearch(string(), "symbols");
144 LYXERR(Debug::MATHED, "read symbols from " << filename);
145 if (filename.empty()) {
146 lyxerr << "Could not find symbols file" << endl;
150 ifstream fs(filename.toFilesystemEncoding().c_str());
151 // limit the size of strings we read to avoid memory problems
155 while (getline(fs, line)) {
159 if (line.empty() || line[0] == '#')
162 // special case of iffont/else/endif
163 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
164 istringstream is(line);
165 // limit the size of strings we read to avoid memory problems
170 skip = !isMathFontAvailable(tmp);
172 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
175 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
181 // special case of pre-defined macros
182 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
183 //lyxerr << "macro definition: '" << line << '\'' << endl;
185 // \def\macroname{definition}
187 // \def\macroname{definition} requires
189 // \def\macroname{definition} extra htmlname xmlname requires
190 istringstream is(line);
197 is >> setw(65536) >> macro >> required;
198 if ((is >> htmlname >> xmlname)) {
200 if (!(is >> required))
203 htmlname = xmlname = "";
204 MacroTable::iterator it = MacroTable::globalMacros().insert(
205 nullptr, from_utf8(macro));
206 if (!extra.empty() || !htmlname.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.htmlname = from_utf8(htmlname);
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 << " html: " << htmlname
235 << " xml: " << xmlname
236 << " requires: " << required
237 << " hidden: " << hidden << '\'');
241 idocstringstream is(from_utf8(line));
244 is >> tmp.name >> help;
245 tmp.inset = to_ascii(help);
246 if (isFontName(tmp.inset)) {
247 is >> help >> fallbackid >> tmp.extra >> tmp.htmlname >> tmp.xmlname;
248 docstring cid, dsp_cid;
249 idocstringstream is2(subst(help, '|', ' '));
250 is2 >> charid >> dsp_charid;
253 // requires is optional
256 // backward compatibility
257 if (help == "esintoramsmath")
258 tmp.required = "esint|amsmath";
260 tmp.required = to_ascii(help);
263 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
264 << to_utf8(tmp.name) << ' ' << tmp.inset << ' '
265 << to_utf8(tmp.extra));
269 if (isFontName(tmp.inset)) {
270 // tmp.inset _is_ the fontname here.
271 // create fallbacks if necessary
273 // store requirements as long as we can
274 if (tmp.required.empty()) {
275 if (tmp.inset == "msa" || tmp.inset == "msb")
276 tmp.required = "amssymb";
277 else if (tmp.inset == "wasy")
278 tmp.required = "wasysym";
279 else if (tmp.inset == "mathscr")
280 tmp.required = "mathrsfs";
281 else if (tmp.inset == "mathds")
282 tmp.required = "dsfont";
285 // symbol font is not available sometimes
286 string symbol_font = "lyxsymbol";
287 char_type unicodesymbol = 0;
289 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
290 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
292 } else if (isMathFontAvailable(tmp.inset) && canBeDisplayed(charid)) {
293 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
294 tmp.draw.push_back(char_type(charid));
295 if (dsp_charid && canBeDisplayed(dsp_charid)) {
296 LYXERR(Debug::MATHED, "large symbol available for " << to_utf8(tmp.name));
297 tmp.dsp_draw.push_back(char_type(dsp_charid));
299 } else if (fallbackid && isMathFontAvailable(symbol_font) &&
300 canBeDisplayed(fallbackid)) {
301 if (tmp.inset == "cmex")
302 tmp.inset = "lyxsymbol";
304 tmp.inset = "lyxboldsymbol";
305 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
306 tmp.draw.push_back(char_type(fallbackid));
307 } else if (isUnicodeSymbolAvailable(tmp.name, unicodesymbol)) {
308 LYXERR(Debug::MATHED, "unicode fallback for " << to_utf8(tmp.name));
309 tmp.inset = "mathnormal";
310 tmp.draw.push_back(unicodesymbol);
312 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
314 tmp.inset = "lyxtex";
317 // it's a proper inset
318 LYXERR(Debug::MATHED, "inset " << tmp.inset
319 << " used for " << to_utf8(tmp.name));
322 if (tmp.required == "hiddensymbol")
328 if (theMathWordList.find(tmp.name) != theMathWordList.end())
329 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
330 << " already exists.");
332 theMathWordList[tmp.name] = tmp;
334 // If you change the following output, please adjust
335 // development/tools/generate_symbols_images.py.
336 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
337 << " inset: " << tmp.inset
338 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
339 << " extra: " << to_utf8(tmp.extra)
340 << " html: " << to_utf8(tmp.htmlname)
341 << " xml: " << to_utf8(tmp.xmlname)
342 << " requires: " << tmp.required
343 << " hidden: " << tmp.hidden << '\'');
346 string tmp2 = "cmsy";
347 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
351 bool isSpecialChar(docstring const & name)
353 if (name.size() != 1)
354 return name == "textasciicircum" || name == "mathcircumflex" ||
355 name == "textasciitilde" || name == "textbackslash";
357 char_type const c = name.at(0);
358 return c == '{' || c == '}' || c == '&' || c == '$' ||
359 c == '#' || c == '%' || c == '_';
365 MathWordList const & mathedWordList()
367 return theMathWordList;
373 static bool initialized = false;
382 bool ensureMath(WriteStream & os, bool needs_mathmode, bool macro,
385 bool brace = os.pendingBrace();
386 os.pendingBrace(false);
389 if (os.textMode() && needs_mathmode) {
395 os << "\\ensuremath{";
399 } else if (macro && textmode_macro && !os.textMode()) {
405 os << "\\lyxmathsym{";
409 } else if (macro && brace && !needs_mathmode && !textmode_macro) {
410 // This is a user defined macro, not a InsetMathMacro, so we
411 // cannot be sure what mode is needed. We leave it in the
412 // same environment it was entered by closing either \lyxmathsym
413 // or \ensuremath, whichever was opened.
416 os.textMode(!os.textMode());
422 int ensureMode(WriteStream & os, InsetMath::mode_type mode,
423 bool locked, bool ascii)
425 bool textmode = mode == InsetMath::TEXT_MODE;
426 if (os.latex() && textmode && os.pendingBrace()) {
428 os.pendingBrace(false);
429 os.pendingSpace(false);
432 int oldmodes = os.textMode() ? 0x01 : 0;
433 os.textMode(textmode);
434 oldmodes |= os.lockedMode() ? 0x02 : 0;
435 os.lockedMode(locked);
436 oldmodes |= os.asciiOnly() ? 0x04 : 0;
442 latexkeys const * in_word_set(docstring const & str)
444 MathWordList::iterator it = theMathWordList.find(str);
445 if (it == theMathWordList.end())
447 if (it->second.inset == "macro")
449 return &(it->second);
453 MathAtom createInsetMath(char const * const s, Buffer * buf)
455 return createInsetMath(from_utf8(s), buf);
459 MathAtom createInsetMath(docstring const & s, Buffer * buf)
461 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
462 if ((s == "ce" || s == "cf") && buf
463 && buf->params().use_package("mhchem") == BufferParams::package_off)
464 return MathAtom(new InsetMathMacro(buf, s));
466 latexkeys const * l = in_word_set(s);
468 string const & inset = l->inset;
469 //lyxerr << " found inset: '" << inset << '\'' << endl;
471 return MathAtom(new InsetMathRef(buf, l->name));
472 if (inset == "overset")
473 return MathAtom(new InsetMathOverset(buf));
474 if (inset == "underset")
475 return MathAtom(new InsetMathUnderset(buf));
476 if (inset == "decoration")
477 return MathAtom(new InsetMathDecoration(buf, l));
478 if (inset == "space")
479 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
480 if (inset == "class")
481 return MathAtom(new InsetMathClass(buf, string_to_class(s)));
483 return MathAtom(new InsetMathDots(l));
485 return MathAtom(new InsetMathBox(buf, l->name));
486 // if (inset == "fbox")
487 // return MathAtom(new InsetMathFBox(l));
488 if (inset == "style")
489 return MathAtom(new InsetMathSize(buf, l));
491 return MathAtom(new InsetMathFont(buf, l));
492 if (inset == "oldfont")
493 return MathAtom(new InsetMathFontOld(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(s));
502 return MathAtom(new InsetMathSymbol(l));
505 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
506 return MathAtom(new InsetMathMacroArgument(s[1] - '0'));
507 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
508 && s[2] >= '1' && s[2] <= '9')
509 return MathAtom(new InsetMathMacroArgument(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));
627 // return MathAtom(new MathInferInset);
629 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
631 return MathAtom(new InsetMathLefteqn(buf));
632 if (s == "boldsymbol")
633 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
635 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
636 if (s == "heavysymbol" || s == "hm")
637 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
638 if (s == "color" || s == "normalcolor")
639 return MathAtom(new InsetMathColor(buf, true));
640 if (s == "textcolor")
641 return MathAtom(new InsetMathColor(buf, false));
643 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
645 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
647 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
649 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::cancel));
651 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::bcancel));
653 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::xcancel));
655 return MathAtom(new InsetMathCancelto(buf));
657 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smash));
658 // The following 2 string values are only for math toolbar use, no LaTeX names
660 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smashb));
662 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smasht));
664 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathclap));
666 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathllap));
668 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathrlap));
669 if (s == "ensuremath")
670 return MathAtom(new InsetMathEnsureMath(buf));
672 return MathAtom(new InsetMathSideset(buf, true, true));
673 // The following 3 string values are only for math toolbar use, no LaTeX names
675 return MathAtom(new InsetMathSideset(buf, false, true));
677 return MathAtom(new InsetMathSideset(buf, true, false));
679 return MathAtom(new InsetMathSideset(buf, false, false));
680 if (isSpecialChar(s))
681 return MathAtom(new InsetMathSpecialChar(s));
683 return MathAtom(new InsetMathSpace(" ", ""));
685 return MathAtom(new InsetMathHull(buf, hullRegexp));
687 return MathAtom(new InsetMathMacro(buf, s));
691 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
694 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
696 docstring body = split(str, name, ' ');
699 InsetCommandParams icp(REF_CODE);
701 InsetCommand::string2params(to_utf8(str), icp);
702 Encoding const * const utf8 = encodings.fromLyXName("utf8");
703 OutputParams op(utf8);
704 mathed_parse_cell(ar, icp.getCommand(op));
705 } else if (name == "mathspace") {
706 InsetSpaceParams isp(true);
707 InsetSpace::string2params(to_utf8(str), isp);
709 odocstringstream ods;
711 Encoding const * const ascii = encodings.fromLyXName("ascii");
712 OutputParams op(ascii);
714 mathed_parse_cell(ar, ods.str());
715 if (ar.size() == 2) {
717 if (ar[1].nucleus()->asBraceInset())
726 return ar[0].nucleus();