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
75 #include "OutputParams.h"
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)) {
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 0, 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 >> charid >> fallbackid >> tmp.extra >> tmp.htmlname >> tmp.xmlname;
250 // requires is optional
253 // backward compatibility
254 if (help == "esintoramsmath")
255 tmp.required = "esint|amsmath";
257 tmp.required = to_ascii(help);
260 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
261 << to_utf8(tmp.name) << ' ' << tmp.inset << ' '
262 << to_utf8(tmp.extra));
266 if (isFontName(tmp.inset)) {
267 // tmp.inset _is_ the fontname here.
268 // create fallbacks if necessary
270 // store requirements as long as we can
271 if (tmp.required.empty()) {
272 if (tmp.inset == "msa" || tmp.inset == "msb")
273 tmp.required = "amssymb";
274 else if (tmp.inset == "wasy")
275 tmp.required = "wasysym";
276 else if (tmp.inset == "mathscr")
277 tmp.required = "mathrsfs";
278 else if (tmp.inset == "mathds")
279 tmp.required = "dsfont";
282 // symbol font is not available sometimes
283 string symbol_font = "lyxsymbol";
284 char_type unicodesymbol = 0;
286 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
287 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
289 } else if (isMathFontAvailable(tmp.inset) && canBeDisplayed(charid)) {
290 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
291 tmp.draw.push_back(char_type(charid));
292 } else if (fallbackid && isMathFontAvailable(symbol_font) &&
293 canBeDisplayed(fallbackid)) {
294 if (tmp.inset == "cmex")
295 tmp.inset = "lyxsymbol";
297 tmp.inset = "lyxboldsymbol";
298 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
299 tmp.draw.push_back(char_type(fallbackid));
300 } else if (isUnicodeSymbolAvailable(tmp.name, unicodesymbol)) {
301 LYXERR(Debug::MATHED, "unicode fallback for " << to_utf8(tmp.name));
302 tmp.inset = "mathnormal";
303 tmp.draw.push_back(unicodesymbol);
305 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
307 tmp.inset = "lyxtex";
310 // it's a proper inset
311 LYXERR(Debug::MATHED, "inset " << tmp.inset
312 << " used for " << to_utf8(tmp.name));
315 if (tmp.required == "hiddensymbol")
321 if (theMathWordList.find(tmp.name) != theMathWordList.end())
322 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
323 << " already exists.");
325 theMathWordList[tmp.name] = tmp;
327 // If you change the following output, please adjust
328 // development/tools/generate_symbols_images.py.
329 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
330 << " inset: " << tmp.inset
331 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
332 << " extra: " << to_utf8(tmp.extra)
333 << " html: " << to_utf8(tmp.htmlname)
334 << " xml: " << to_utf8(tmp.xmlname)
335 << " requires: " << tmp.required
336 << " hidden: " << tmp.hidden << '\'');
339 string tmp2 = "cmsy";
340 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
344 bool isSpecialChar(docstring const & name)
346 if (name.size() != 1)
347 return name == "textasciicircum" || name == "mathcircumflex" ||
348 name == "textasciitilde" || name == "textbackslash";
350 char_type const c = name.at(0);
351 return c == '{' || c == '}' || c == '&' || c == '$' ||
352 c == '#' || c == '%' || c == '_';
358 MathWordList const & mathedWordList()
360 return theMathWordList;
366 static bool initialized = false;
375 bool ensureMath(WriteStream & os, bool needs_mathmode, bool macro,
378 bool brace = os.pendingBrace();
379 os.pendingBrace(false);
382 if (os.textMode() && needs_mathmode) {
388 os << "\\ensuremath{";
392 } else if (macro && textmode_macro && !os.textMode()) {
398 os << "\\lyxmathsym{";
402 } else if (macro && brace && !needs_mathmode && !textmode_macro) {
403 // This is a user defined macro, not a InsetMathMacro, so we
404 // cannot be sure what mode is needed. We leave it in the
405 // same environment it was entered by closing either \lyxmathsym
406 // or \ensuremath, whichever was opened.
409 os.textMode(!os.textMode());
415 int ensureMode(WriteStream & os, InsetMath::mode_type mode,
416 bool locked, bool ascii)
418 bool textmode = mode == InsetMath::TEXT_MODE;
419 if (os.latex() && textmode && os.pendingBrace()) {
421 os.pendingBrace(false);
422 os.pendingSpace(false);
425 int oldmodes = os.textMode() ? 0x01 : 0;
426 os.textMode(textmode);
427 oldmodes |= os.lockedMode() ? 0x02 : 0;
428 os.lockedMode(locked);
429 oldmodes |= os.asciiOnly() ? 0x04 : 0;
435 latexkeys const * in_word_set(docstring const & str)
437 MathWordList::iterator it = theMathWordList.find(str);
438 if (it == theMathWordList.end())
440 if (it->second.inset == "macro")
442 return &(it->second);
446 MathAtom createInsetMath(char const * const s, Buffer * buf)
448 return createInsetMath(from_utf8(s), buf);
452 MathAtom createInsetMath(docstring const & s, Buffer * buf)
454 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
455 if ((s == "ce" || s == "cf") && buf
456 && buf->params().use_package("mhchem") == BufferParams::package_off)
457 return MathAtom(new InsetMathMacro(buf, s));
459 latexkeys const * l = in_word_set(s);
461 string const & inset = l->inset;
462 //lyxerr << " found inset: '" << inset << '\'' << endl;
464 return MathAtom(new InsetMathRef(buf, l->name));
465 if (inset == "overset")
466 return MathAtom(new InsetMathOverset(buf));
467 if (inset == "underset")
468 return MathAtom(new InsetMathUnderset(buf));
469 if (inset == "decoration")
470 return MathAtom(new InsetMathDecoration(buf, l));
471 if (inset == "space")
472 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
473 if (inset == "class")
474 return MathAtom(new InsetMathClass(buf, string_to_class(s)));
476 return MathAtom(new InsetMathDots(l));
478 return MathAtom(new InsetMathBox(buf, l->name));
479 // if (inset == "fbox")
480 // return MathAtom(new InsetMathFBox(l));
481 if (inset == "style")
482 return MathAtom(new InsetMathSize(buf, l));
484 return MathAtom(new InsetMathFont(buf, l));
485 if (inset == "oldfont")
486 return MathAtom(new InsetMathFontOld(buf, l));
487 if (inset == "matrix")
488 return MathAtom(new InsetMathAMSArray(buf, s));
489 if (inset == "split")
490 return MathAtom(new InsetMathSplit(buf, s));
492 // we can't create a InsetMathBig, since the argument
494 return MathAtom(new InsetMathUnknown(s));
495 return MathAtom(new InsetMathSymbol(l));
498 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
499 return MathAtom(new InsetMathMacroArgument(s[1] - '0'));
500 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
501 && s[2] >= '1' && s[2] <= '9')
502 return MathAtom(new InsetMathMacroArgument(s[2] - '0'));
504 return MathAtom(new InsetMathBoxed(buf));
506 return MathAtom(new InsetMathFBox(buf));
508 return MathAtom(new InsetMathMakebox(buf, true));
510 return MathAtom(new InsetMathMakebox(buf, false));
511 if (s.substr(0, 8) == "xymatrix") {
512 char spacing_code = '\0';
514 bool equal_spacing = false;
515 size_t const len = s.length();
517 if (i < len && s[i] == '@') {
519 if (i < len && s[i] == '!') {
520 equal_spacing = true;
527 spacing_code = static_cast<char>(s[i]);
530 } else if (i < len) {
538 spacing_code = static_cast<char>(s[i]);
542 if (i < len && s[i] == '=') {
544 spacing = Length(to_ascii(s.substr(i)));
548 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
553 return MathAtom(new InsetMathDiagram(buf));
554 if (s == "xrightarrow" || s == "xleftarrow" ||
555 s == "xhookrightarrow" || s == "xhookleftarrow" ||
556 s == "xRightarrow" || s == "xLeftarrow" ||
557 s == "xleftrightarrow" || s == "xLeftrightarrow" ||
558 s == "xrightharpoondown" || s == "xrightharpoonup" ||
559 s == "xleftharpoondown" || s == "xleftharpoonup" ||
560 s == "xleftrightharpoons" || s == "xrightleftharpoons" ||
562 return MathAtom(new InsetMathXArrow(buf, s));
563 if (s == "split" || s == "alignedat")
564 return MathAtom(new InsetMathSplit(buf, s));
566 return MathAtom(new InsetMathCases(buf));
568 return MathAtom(new InsetMathSubstack(buf));
569 if (s == "subarray" || s == "array")
570 return MathAtom(new InsetMathArray(buf, s, 1, 1));
572 return MathAtom(new InsetMathSqrt(buf));
574 return MathAtom(new InsetMathRoot(buf));
576 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
578 return MathAtom(new InsetMathStackrel(buf, false));
579 // This string value is only for math toolbar use, no LaTeX name
580 if (s == "stackrelthree")
581 return MathAtom(new InsetMathStackrel(buf, true));
583 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
585 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
587 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
589 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
591 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
593 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
595 return MathAtom(new InsetMathFrac(buf));
597 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
599 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
601 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
603 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
605 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
607 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
608 // These string values are only for math toolbar use, no LaTeX names
609 if (s == "unitfracthree")
610 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
612 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
614 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
615 if (s == "cfracleft")
616 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
617 if (s == "cfracright")
618 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
620 // return MathAtom(new MathInferInset);
622 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
624 return MathAtom(new InsetMathLefteqn(buf));
625 if (s == "boldsymbol")
626 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
628 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
629 if (s == "heavysymbol" || s == "hm")
630 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
631 if (s == "color" || s == "normalcolor")
632 return MathAtom(new InsetMathColor(buf, true));
633 if (s == "textcolor")
634 return MathAtom(new InsetMathColor(buf, false));
636 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
638 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
640 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
642 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::cancel));
644 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::bcancel));
646 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::xcancel));
648 return MathAtom(new InsetMathCancelto(buf));
650 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smash));
651 // The following 2 string values are only for math toolbar use, no LaTeX names
653 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smashb));
655 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smasht));
657 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathclap));
659 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathllap));
661 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathrlap));
662 if (s == "ensuremath")
663 return MathAtom(new InsetMathEnsureMath(buf));
665 return MathAtom(new InsetMathSideset(buf, true, true));
666 // The following 3 string values are only for math toolbar use, no LaTeX names
668 return MathAtom(new InsetMathSideset(buf, false, true));
670 return MathAtom(new InsetMathSideset(buf, true, false));
672 return MathAtom(new InsetMathSideset(buf, false, false));
673 if (isSpecialChar(s))
674 return MathAtom(new InsetMathSpecialChar(s));
676 return MathAtom(new InsetMathSpace(" ", ""));
678 return MathAtom(new InsetMathHull(buf, hullRegexp));
680 return MathAtom(new InsetMathMacro(buf, s));
684 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
687 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
689 docstring body = split(str, name, ' ');
692 InsetCommandParams icp(REF_CODE);
694 InsetCommand::string2params(to_utf8(str), icp);
695 Encoding const * const utf8 = encodings.fromLyXName("utf8");
696 OutputParams op(utf8);
697 mathed_parse_cell(ar, icp.getCommand(op));
698 } else if (name == "mathspace") {
699 InsetSpaceParams isp(true);
700 InsetSpace::string2params(to_utf8(str), isp);
702 odocstringstream ods;
704 Encoding const * const ascii = encodings.fromLyXName("ascii");
705 OutputParams op(ascii);
707 mathed_parse_cell(ar, ods.str());
708 if (ar.size() == 2) {
710 if (ar[1].nucleus()->asBraceInset())
719 return ar[0].nucleus();