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 xmlname requires
190 istringstream is(line);
196 is >> setw(65536) >> macro >> requires;
197 if ((is >> xmlname)) {
199 if (!(is >> requires))
203 MacroTable::iterator it = MacroTable::globalMacros().insert(
204 0, from_utf8(macro));
205 if (!extra.empty() || !xmlname.empty() || !requires.empty()) {
206 MathWordList::iterator wit = theMathWordList.find(it->first);
207 if (wit != theMathWordList.end())
208 LYXERR(Debug::MATHED, "readSymbols: inset "
209 << to_utf8(it->first) << " already exists.");
213 tmp.name = it->first;
214 tmp.extra = from_utf8(extra);
215 tmp.xmlname = from_utf8(xmlname);
216 if (requires == "hiddensymbol") {
218 tmp.hidden = hidden = true;
220 tmp.requires = requires;
221 theMathWordList[it->first] = tmp;
222 wit = theMathWordList.find(it->first);
223 it->second.setSymbol(&(wit->second));
226 // If you change the following output, please adjust
227 // development/tools/generate_symbols_images.py.
228 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(it->first)
231 << " extra: " << extra
232 << " xml: " << xmlname
233 << " requires: " << requires
234 << " hidden: " << hidden << '\'');
238 idocstringstream is(from_utf8(line));
241 is >> tmp.name >> help;
242 tmp.inset = to_ascii(help);
243 if (isFontName(tmp.inset))
244 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
247 // requires is optional
250 // backward compatibility
251 if (help == "esintoramsmath")
252 tmp.requires = "esint|amsmath";
254 tmp.requires = to_ascii(help);
257 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
258 << to_utf8(tmp.name) << ' ' << tmp.inset << ' '
259 << to_utf8(tmp.extra));
263 if (isFontName(tmp.inset)) {
264 // tmp.inset _is_ the fontname here.
265 // create fallbacks if necessary
267 // store requirements as long as we can
268 if (tmp.requires.empty()) {
269 if (tmp.inset == "msa" || tmp.inset == "msb")
270 tmp.requires = "amssymb";
271 else if (tmp.inset == "wasy")
272 tmp.requires = "wasysym";
273 else if (tmp.inset == "mathscr")
274 tmp.requires = "mathrsfs";
275 else if (tmp.inset == "mathds")
276 tmp.requires = "dsfont";
279 // symbol font is not available sometimes
280 string symbol_font = "lyxsymbol";
281 char_type unicodesymbol = 0;
283 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
284 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
286 } else if (isMathFontAvailable(tmp.inset) && canBeDisplayed(charid)) {
287 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
288 tmp.draw.push_back(char_type(charid));
289 } else if (fallbackid && isMathFontAvailable(symbol_font) &&
290 canBeDisplayed(fallbackid)) {
291 if (tmp.inset == "cmex")
292 tmp.inset = "lyxsymbol";
294 tmp.inset = "lyxboldsymbol";
295 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
296 tmp.draw.push_back(char_type(fallbackid));
297 } else if (isUnicodeSymbolAvailable(tmp.name, unicodesymbol)) {
298 LYXERR(Debug::MATHED, "unicode fallback for " << to_utf8(tmp.name));
299 tmp.inset = "mathnormal";
300 tmp.draw.push_back(unicodesymbol);
302 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
304 tmp.inset = "lyxtex";
307 // it's a proper inset
308 LYXERR(Debug::MATHED, "inset " << tmp.inset
309 << " used for " << to_utf8(tmp.name));
312 if (tmp.requires == "hiddensymbol")
318 if (theMathWordList.find(tmp.name) != theMathWordList.end())
319 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
320 << " already exists.");
322 theMathWordList[tmp.name] = tmp;
324 // If you change the following output, please adjust
325 // development/tools/generate_symbols_images.py.
326 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
327 << " inset: " << tmp.inset
328 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
329 << " extra: " << to_utf8(tmp.extra)
330 << " xml: " << to_utf8(tmp.xmlname)
331 << " requires: " << tmp.requires
332 << " hidden: " << tmp.hidden << '\'');
335 string tmp2 = "cmsy";
336 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
340 bool isSpecialChar(docstring const & name)
342 if (name.size() != 1)
343 return name == "textasciicircum" || name == "mathcircumflex" ||
344 name == "textasciitilde" || name == "textbackslash";
346 char_type const c = name.at(0);
347 return c == '{' || c == '}' || c == '&' || c == '$' ||
348 c == '#' || c == '%' || c == '_';
354 MathWordList const & mathedWordList()
356 return theMathWordList;
362 static bool initialized = false;
371 bool ensureMath(WriteStream & os, bool needs_mathmode, bool macro,
374 bool brace = os.pendingBrace();
375 os.pendingBrace(false);
378 if (os.textMode() && needs_mathmode) {
384 os << "\\ensuremath{";
388 } else if (macro && textmode_macro && !os.textMode()) {
394 os << "\\lyxmathsym{";
398 } else if (macro && brace && !needs_mathmode && !textmode_macro) {
399 // This is a user defined macro, not a InsetMathMacro, so we
400 // cannot be sure what mode is needed. We leave it in the
401 // same environment it was entered by closing either \lyxmathsym
402 // or \ensuremath, whichever was opened.
405 os.textMode(!os.textMode());
411 int ensureMode(WriteStream & os, InsetMath::mode_type mode,
412 bool locked, bool ascii)
414 bool textmode = mode == InsetMath::TEXT_MODE;
415 if (os.latex() && textmode && os.pendingBrace()) {
417 os.pendingBrace(false);
418 os.pendingSpace(false);
421 int oldmodes = os.textMode() ? 0x01 : 0;
422 os.textMode(textmode);
423 oldmodes |= os.lockedMode() ? 0x02 : 0;
424 os.lockedMode(locked);
425 oldmodes |= os.asciiOnly() ? 0x04 : 0;
431 latexkeys const * in_word_set(docstring const & str)
433 MathWordList::iterator it = theMathWordList.find(str);
434 if (it == theMathWordList.end())
436 if (it->second.inset == "macro")
438 return &(it->second);
442 MathAtom createInsetMath(char const * const s, Buffer * buf)
444 return createInsetMath(from_utf8(s), buf);
448 MathAtom createInsetMath(docstring const & s, Buffer * buf)
450 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
451 if ((s == "ce" || s == "cf") && buf
452 && buf->params().use_package("mhchem") == BufferParams::package_off)
453 return MathAtom(new InsetMathMacro(buf, s));
455 latexkeys const * l = in_word_set(s);
457 string const & inset = l->inset;
458 //lyxerr << " found inset: '" << inset << '\'' << endl;
460 return MathAtom(new InsetMathRef(buf, l->name));
461 if (inset == "overset")
462 return MathAtom(new InsetMathOverset(buf));
463 if (inset == "underset")
464 return MathAtom(new InsetMathUnderset(buf));
465 if (inset == "decoration")
466 return MathAtom(new InsetMathDecoration(buf, l));
467 if (inset == "space")
468 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
469 if (inset == "class")
470 return MathAtom(new InsetMathClass(buf, string_to_class(s)));
472 return MathAtom(new InsetMathDots(l));
474 return MathAtom(new InsetMathBox(buf, l->name));
475 // if (inset == "fbox")
476 // return MathAtom(new InsetMathFBox(l));
477 if (inset == "style")
478 return MathAtom(new InsetMathSize(buf, l));
480 return MathAtom(new InsetMathFont(buf, l));
481 if (inset == "oldfont")
482 return MathAtom(new InsetMathFontOld(buf, l));
483 if (inset == "matrix")
484 return MathAtom(new InsetMathAMSArray(buf, s));
485 if (inset == "split")
486 return MathAtom(new InsetMathSplit(buf, s));
488 // we can't create a InsetMathBig, since the argument
490 return MathAtom(new InsetMathUnknown(s));
491 return MathAtom(new InsetMathSymbol(l));
494 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
495 return MathAtom(new InsetMathMacroArgument(s[1] - '0'));
496 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
497 && s[2] >= '1' && s[2] <= '9')
498 return MathAtom(new InsetMathMacroArgument(s[2] - '0'));
500 return MathAtom(new InsetMathBoxed(buf));
502 return MathAtom(new InsetMathFBox(buf));
504 return MathAtom(new InsetMathMakebox(buf, true));
506 return MathAtom(new InsetMathMakebox(buf, false));
507 if (s.substr(0, 8) == "xymatrix") {
508 char spacing_code = '\0';
510 bool equal_spacing = false;
511 size_t const len = s.length();
513 if (i < len && s[i] == '@') {
515 if (i < len && s[i] == '!') {
516 equal_spacing = true;
523 spacing_code = static_cast<char>(s[i]);
526 } else if (i < len) {
534 spacing_code = static_cast<char>(s[i]);
538 if (i < len && s[i] == '=') {
540 spacing = Length(to_ascii(s.substr(i)));
544 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
549 return MathAtom(new InsetMathDiagram(buf));
550 if (s == "xrightarrow" || s == "xleftarrow" ||
551 s == "xhookrightarrow" || s == "xhookleftarrow" ||
552 s == "xRightarrow" || s == "xLeftarrow" ||
553 s == "xleftrightarrow" || s == "xLeftrightarrow" ||
554 s == "xrightharpoondown" || s == "xrightharpoonup" ||
555 s == "xleftharpoondown" || s == "xleftharpoonup" ||
556 s == "xleftrightharpoons" || s == "xrightleftharpoons" ||
558 return MathAtom(new InsetMathXArrow(buf, s));
559 if (s == "split" || s == "alignedat")
560 return MathAtom(new InsetMathSplit(buf, s));
562 return MathAtom(new InsetMathCases(buf));
564 return MathAtom(new InsetMathSubstack(buf));
565 if (s == "subarray" || s == "array")
566 return MathAtom(new InsetMathArray(buf, s, 1, 1));
568 return MathAtom(new InsetMathSqrt(buf));
570 return MathAtom(new InsetMathRoot(buf));
572 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
574 return MathAtom(new InsetMathStackrel(buf, false));
575 // This string value is only for math toolbar use, no LaTeX name
576 if (s == "stackrelthree")
577 return MathAtom(new InsetMathStackrel(buf, true));
579 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
581 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
583 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
585 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
587 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
589 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
591 return MathAtom(new InsetMathFrac(buf));
593 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
595 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
597 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
599 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
601 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
603 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
604 // These string values are only for math toolbar use, no LaTeX names
605 if (s == "unitfracthree")
606 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
608 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
610 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
611 if (s == "cfracleft")
612 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
613 if (s == "cfracright")
614 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
616 // return MathAtom(new MathInferInset);
618 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
620 return MathAtom(new InsetMathLefteqn(buf));
621 if (s == "boldsymbol")
622 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
624 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
625 if (s == "heavysymbol" || s == "hm")
626 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
627 if (s == "color" || s == "normalcolor")
628 return MathAtom(new InsetMathColor(buf, true));
629 if (s == "textcolor")
630 return MathAtom(new InsetMathColor(buf, false));
632 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
634 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
636 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
638 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::cancel));
640 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::bcancel));
642 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::xcancel));
644 return MathAtom(new InsetMathCancelto(buf));
646 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smash));
647 // The following 2 string values are only for math toolbar use, no LaTeX names
649 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smashb));
651 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smasht));
653 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathclap));
655 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathllap));
657 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathrlap));
658 if (s == "ensuremath")
659 return MathAtom(new InsetMathEnsureMath(buf));
661 return MathAtom(new InsetMathSideset(buf, true, true));
662 // The following 3 string values are only for math toolbar use, no LaTeX names
664 return MathAtom(new InsetMathSideset(buf, false, true));
666 return MathAtom(new InsetMathSideset(buf, true, false));
668 return MathAtom(new InsetMathSideset(buf, false, false));
669 if (isSpecialChar(s))
670 return MathAtom(new InsetMathSpecialChar(s));
672 return MathAtom(new InsetMathSpace(" ", ""));
674 return MathAtom(new InsetMathHull(buf, hullRegexp));
676 return MathAtom(new InsetMathMacro(buf, s));
680 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
683 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
685 docstring body = split(str, name, ' ');
688 InsetCommandParams icp(REF_CODE);
690 InsetCommand::string2params(to_utf8(str), icp);
691 Encoding const * const utf8 = encodings.fromLyXName("utf8");
692 OutputParams op(utf8);
693 mathed_parse_cell(ar, icp.getCommand(op));
694 } else if (name == "mathspace") {
695 InsetSpaceParams isp(true);
696 InsetSpace::string2params(to_utf8(str), isp);
698 odocstringstream ods;
700 Encoding const * const ascii = encodings.fromLyXName("ascii");
701 OutputParams op(ascii);
703 mathed_parse_cell(ar, ods.str());
704 if (ar.size() == 2) {
706 if (ar[1].nucleus()->asBraceInset())
715 return ar[0].nucleus();