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 "InsetMathColor.h"
23 #include "InsetMathDecoration.h"
24 #include "InsetMathDots.h"
25 #include "InsetMathEnsureMath.h"
26 #include "InsetMathFont.h"
27 #include "InsetMathFontOld.h"
28 #include "InsetMathFrac.h"
29 #include "InsetMathKern.h"
30 #include "InsetMathLefteqn.h"
31 #include "InsetMathOverset.h"
32 #include "InsetMathPhantom.h"
33 #include "InsetMathRef.h"
34 #include "InsetMathRoot.h"
35 #include "InsetMathSideset.h"
36 #include "InsetMathSize.h"
37 #include "InsetMathSpace.h"
38 #include "InsetMathSpecialChar.h"
39 #include "InsetMathSplit.h"
40 #include "InsetMathSqrt.h"
41 #include "InsetMathStackrel.h"
42 #include "InsetMathSubstack.h"
43 #include "InsetMathSymbol.h"
44 #include "InsetMathTabular.h"
45 #include "InsetMathUnderset.h"
46 #include "InsetMathUnknown.h"
47 #include "InsetMathHull.h"
48 #include "InsetMathXArrow.h"
49 #include "InsetMathXYMatrix.h"
50 #include "InsetMathDiagram.h"
51 #include "MacroTable.h"
52 #include "MathMacro.h"
53 #include "MathMacroArgument.h"
54 #include "MathParser.h"
55 #include "MathStream.h"
56 #include "MathSupport.h"
58 #include "insets/InsetCommand.h"
59 #include "insets/InsetSpace.h"
61 #include "support/debug.h"
62 #include "support/docstream.h"
63 #include "support/FileName.h"
64 #include "support/filetools.h" // LibFileSearch
65 #include "support/lstrings.h"
66 #include "support/textutils.h"
68 #include "frontends/FontLoader.h"
71 #include "BufferParams.h"
73 #include "LyX.h" // use_gui
74 #include "OutputParams.h"
78 using namespace lyx::support;
87 MathWordList theMathWordList;
90 bool isMathFontAvailable(string & name)
98 // Do we have the font proper?
99 if (theFontLoader().available(f))
103 if (name == "eufrak") {
104 name = "lyxfakefrak";
108 LYXERR(Debug::MATHED,
109 "font " << name << " not available and I can't fake it");
114 bool canBeDisplayed(char_type c)
118 return theFontLoader().canBeDisplayed(c);
122 bool isUnicodeSymbolAvailable(docstring const & name, char_type & c)
124 docstring cmd(from_ascii("\\") + name);
127 c = Encodings::fromLaTeXCommand(cmd, Encodings::MATH_CMD,
128 is_combining, termination);
129 if (c == 0 && name == "varOmega") {
130 // fallback for bug 7954, unicodesymbols does not list
131 // \\varOmega because of requirements, but this might change
132 cmd = from_ascii("\\mathit{\\Omega}");
133 c = Encodings::fromLaTeXCommand(cmd, Encodings::MATH_CMD,
134 is_combining, termination);
136 return c != 0 && !is_combining;
142 FileName const filename = libFileSearch(string(), "symbols");
143 LYXERR(Debug::MATHED, "read symbols from " << filename);
144 if (filename.empty()) {
145 lyxerr << "Could not find symbols file" << endl;
149 ifstream fs(filename.toFilesystemEncoding().c_str());
152 while (getline(fs, line)) {
155 if (line.empty() || line[0] == '#')
158 // special case of iffont/else/endif
159 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
160 istringstream is(line);
164 skip = !isMathFontAvailable(tmp);
166 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
169 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
175 // special case of pre-defined macros
176 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
177 //lyxerr << "macro definition: '" << line << '\'' << endl;
179 // \def\macroname{definition}
181 // \def\macroname{definition} requires
183 // \def\macroname{definition} extra xmlname requires
184 istringstream is(line);
190 is >> macro >> requires;
191 if ((is >> xmlname)) {
193 if (!(is >> requires))
197 MacroTable::iterator it = MacroTable::globalMacros().insert(
198 0, from_utf8(macro));
199 if (!extra.empty() || !xmlname.empty() || !requires.empty()) {
200 MathWordList::iterator wit = theMathWordList.find(it->first);
201 if (wit != theMathWordList.end())
202 LYXERR(Debug::MATHED, "readSymbols: inset "
203 << to_utf8(it->first) << " already exists.");
207 tmp.name = it->first;
208 tmp.extra = from_utf8(extra);
209 tmp.xmlname = from_utf8(xmlname);
210 if (requires == "hiddensymbol") {
212 tmp.hidden = hidden = true;
214 tmp.requires = requires;
215 theMathWordList[it->first] = tmp;
216 wit = theMathWordList.find(it->first);
217 it->second.setSymbol(&(wit->second));
220 // If you change the following output, please adjust
221 // development/tools/generate_symbols_images.py.
222 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(it->first)
225 << " extra: " << extra
226 << " xml: " << xmlname
227 << " requires: " << requires
228 << " hidden: " << hidden << '\'');
232 idocstringstream is(from_utf8(line));
235 is >> tmp.name >> help;
236 tmp.inset = to_ascii(help);
237 if (isFontName(tmp.inset))
238 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
241 // requires is optional
244 // backward compatibility
245 if (help == "esintoramsmath")
246 tmp.requires = "esint|amsmath";
248 tmp.requires = to_ascii(help);
251 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
252 << to_utf8(tmp.name) << ' ' << tmp.inset << ' '
253 << to_utf8(tmp.extra));
257 if (isFontName(tmp.inset)) {
258 // tmp.inset _is_ the fontname here.
259 // create fallbacks if necessary
261 // store requirements as long as we can
262 if (tmp.requires.empty()) {
263 if (tmp.inset == "msa" || tmp.inset == "msb")
264 tmp.requires = "amssymb";
265 else if (tmp.inset == "wasy")
266 tmp.requires = "wasysym";
267 else if (tmp.inset == "mathscr")
268 tmp.requires = "mathrsfs";
271 // symbol font is not available sometimes
272 string symbol_font = "lyxsymbol";
273 char_type unicodesymbol = 0;
275 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
276 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
278 } else if (isMathFontAvailable(tmp.inset) && canBeDisplayed(charid)) {
279 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
280 tmp.draw.push_back(char_type(charid));
281 } else if (fallbackid && isMathFontAvailable(symbol_font) &&
282 canBeDisplayed(fallbackid)) {
283 if (tmp.inset == "cmex")
284 tmp.inset = "lyxsymbol";
286 tmp.inset = "lyxboldsymbol";
287 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
288 tmp.draw.push_back(char_type(fallbackid));
289 } else if (isUnicodeSymbolAvailable(tmp.name, unicodesymbol)) {
290 LYXERR(Debug::MATHED, "unicode fallback for " << to_utf8(tmp.name));
291 tmp.inset = "mathnormal";
292 tmp.draw.push_back(unicodesymbol);
294 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
296 tmp.inset = "lyxtex";
299 // it's a proper inset
300 LYXERR(Debug::MATHED, "inset " << tmp.inset
301 << " used for " << to_utf8(tmp.name));
304 if (tmp.requires == "hiddensymbol")
310 if (theMathWordList.find(tmp.name) != theMathWordList.end())
311 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
312 << " already exists.");
314 theMathWordList[tmp.name] = tmp;
316 // If you change the following output, please adjust
317 // development/tools/generate_symbols_images.py.
318 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
319 << " inset: " << tmp.inset
320 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
321 << " extra: " << to_utf8(tmp.extra)
322 << " xml: " << to_utf8(tmp.xmlname)
323 << " requires: " << tmp.requires
324 << " hidden: " << tmp.hidden << '\'');
327 string tmp2 = "cmsy";
328 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
332 bool isSpecialChar(docstring const & name)
334 if (name.size() != 1)
335 return name == "textasciicircum" || name == "mathcircumflex" ||
336 name == "textasciitilde" || name == "textbackslash";
338 char_type const c = name.at(0);
339 return c == '{' || c == '}' || c == '&' || c == '$' ||
340 c == '#' || c == '%' || c == '_';
346 MathWordList const & mathedWordList()
348 return theMathWordList;
354 static bool initialized = false;
363 bool ensureMath(WriteStream & os, bool needs_mathmode, bool macro,
366 bool brace = os.pendingBrace();
367 os.pendingBrace(false);
370 if (os.textMode() && needs_mathmode) {
376 os << "\\ensuremath{";
380 } else if (macro && textmode_macro && !os.textMode()) {
386 os << "\\lyxmathsym{";
390 } else if (macro && brace && !needs_mathmode && !textmode_macro) {
391 // This is a user defined macro, not a MathMacro, so we
392 // cannot be sure what mode is needed. We leave it in the
393 // same environment it was entered by closing either \lyxmathsym
394 // or \ensuremath, whichever was opened.
397 os.textMode(!os.textMode());
403 int ensureMode(WriteStream & os, InsetMath::mode_type mode,
404 bool locked, bool ascii)
406 bool textmode = mode == InsetMath::TEXT_MODE;
407 if (os.latex() && textmode && os.pendingBrace()) {
409 os.pendingBrace(false);
410 os.pendingSpace(false);
413 int oldmodes = os.textMode() ? 0x01 : 0;
414 os.textMode(textmode);
415 oldmodes |= os.lockedMode() ? 0x02 : 0;
416 os.lockedMode(locked);
417 oldmodes |= os.asciiOnly() ? 0x04 : 0;
423 latexkeys const * in_word_set(docstring const & str)
425 MathWordList::iterator it = theMathWordList.find(str);
426 if (it == theMathWordList.end())
428 if (it->second.inset == "macro")
430 return &(it->second);
434 MathAtom createInsetMath(char const * const s, Buffer * buf)
436 return createInsetMath(from_utf8(s), buf);
440 MathAtom createInsetMath(docstring const & s, Buffer * buf)
442 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
443 if ((s == "ce" || s == "cf") && buf
444 && buf->params().use_package("mhchem") == BufferParams::package_off)
445 return MathAtom(new MathMacro(buf, s));
447 latexkeys const * l = in_word_set(s);
449 string const & inset = l->inset;
450 //lyxerr << " found inset: '" << inset << '\'' << endl;
452 return MathAtom(new InsetMathRef(buf, l->name));
453 if (inset == "overset")
454 return MathAtom(new InsetMathOverset(buf));
455 if (inset == "underset")
456 return MathAtom(new InsetMathUnderset(buf));
457 if (inset == "decoration")
458 return MathAtom(new InsetMathDecoration(buf, l));
459 if (inset == "space")
460 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
462 return MathAtom(new InsetMathDots(l));
464 return MathAtom(new InsetMathBox(buf, l->name));
465 // if (inset == "fbox")
466 // return MathAtom(new InsetMathFBox(l));
467 if (inset == "style")
468 return MathAtom(new InsetMathSize(buf, l));
470 return MathAtom(new InsetMathFont(buf, l));
471 if (inset == "oldfont")
472 return MathAtom(new InsetMathFontOld(buf, l));
473 if (inset == "matrix")
474 return MathAtom(new InsetMathAMSArray(buf, s));
475 if (inset == "split")
476 return MathAtom(new InsetMathSplit(buf, s));
478 // we can't create a InsetMathBig, since the argument
480 return MathAtom(new InsetMathUnknown(s));
481 return MathAtom(new InsetMathSymbol(l));
484 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
485 return MathAtom(new MathMacroArgument(s[1] - '0'));
486 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
487 && s[2] >= '1' && s[2] <= '9')
488 return MathAtom(new MathMacroArgument(s[2] - '0'));
490 return MathAtom(new InsetMathBoxed(buf));
492 return MathAtom(new InsetMathFBox(buf));
494 return MathAtom(new InsetMathMakebox(buf, true));
496 return MathAtom(new InsetMathMakebox(buf, false));
498 return MathAtom(new InsetMathKern);
499 if (s.substr(0, 8) == "xymatrix") {
500 char spacing_code = '\0';
502 bool equal_spacing = false;
503 size_t const len = s.length();
505 if (i < len && s[i] == '@') {
507 if (i < len && s[i] == '!') {
508 equal_spacing = true;
515 spacing_code = static_cast<char>(s[i]);
518 } else if (i < len) {
526 spacing_code = static_cast<char>(s[i]);
530 if (i < len && s[i] == '=') {
532 spacing = Length(to_ascii(s.substr(i)));
536 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
541 return MathAtom(new InsetMathDiagram(buf));
542 if (s == "xrightarrow" || s == "xleftarrow" ||
543 s == "xhookrightarrow" || s == "xhookleftarrow" ||
544 s == "xRightarrow" || s == "xLeftarrow" ||
545 s == "xleftrightarrow" || s == "xLeftrightarrow" ||
546 s == "xrightharpoondown" || s == "xrightharpoonup" ||
547 s == "xleftharpoondown" || s == "xleftharpoonup" ||
548 s == "xleftrightharpoons" || s == "xrightleftharpoons" ||
550 return MathAtom(new InsetMathXArrow(buf, s));
551 if (s == "split" || s == "alignedat")
552 return MathAtom(new InsetMathSplit(buf, s));
554 return MathAtom(new InsetMathCases(buf));
556 return MathAtom(new InsetMathSubstack(buf));
557 if (s == "subarray" || s == "array")
558 return MathAtom(new InsetMathArray(buf, s, 1, 1));
560 return MathAtom(new InsetMathSqrt(buf));
562 return MathAtom(new InsetMathRoot(buf));
564 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
566 return MathAtom(new InsetMathStackrel(buf, false));
567 // This string value is only for math toolbar use, no LaTeX name
568 if (s == "stackrelthree")
569 return MathAtom(new InsetMathStackrel(buf, true));
571 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
573 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
575 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
577 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
579 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
581 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
583 return MathAtom(new InsetMathFrac(buf));
585 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
587 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
589 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
591 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
593 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
595 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
596 // These string values are only for math toolbar use, no LaTeX names
597 if (s == "unitfracthree")
598 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
600 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
602 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
603 if (s == "cfracleft")
604 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
605 if (s == "cfracright")
606 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
608 // return MathAtom(new MathInferInset);
610 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
612 return MathAtom(new InsetMathLefteqn(buf));
613 if (s == "boldsymbol")
614 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
616 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
617 if (s == "heavysymbol" || s == "hm")
618 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
619 if (s == "color" || s == "normalcolor")
620 return MathAtom(new InsetMathColor(buf, true));
621 if (s == "textcolor")
622 return MathAtom(new InsetMathColor(buf, false));
624 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
626 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
628 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
630 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::cancel));
632 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::bcancel));
634 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::xcancel));
636 return MathAtom(new InsetMathCancelto(buf));
638 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smash));
639 // The following 2 string values are only for math toolbar use, no LaTeX names
641 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smashb));
643 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smasht));
645 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathclap));
647 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathllap));
649 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathrlap));
650 if (s == "ensuremath")
651 return MathAtom(new InsetMathEnsureMath(buf));
653 return MathAtom(new InsetMathSideset(buf, true, true));
654 // The following 3 string values are only for math toolbar use, no LaTeX names
656 return MathAtom(new InsetMathSideset(buf, false, true));
658 return MathAtom(new InsetMathSideset(buf, true, false));
660 return MathAtom(new InsetMathSideset(buf, false, false));
661 if (isSpecialChar(s))
662 return MathAtom(new InsetMathSpecialChar(s));
664 return MathAtom(new InsetMathSpace(" ", ""));
667 return MathAtom(new InsetMathHull(buf, hullRegexp));
669 return MathAtom(new MathMacro(buf, s));
673 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
676 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
678 docstring body = split(str, name, ' ');
681 InsetCommandParams icp(REF_CODE);
683 InsetCommand::string2params(to_utf8(str), icp);
684 Encoding const * const utf8 = encodings.fromLyXName("utf8");
685 OutputParams op(utf8);
686 mathed_parse_cell(ar, icp.getCommand(op));
687 } else if (name == "mathspace") {
688 InsetSpaceParams isp(true);
689 InsetSpace::string2params(to_utf8(str), isp);
691 odocstringstream ods;
693 Encoding const * const ascii = encodings.fromLyXName("ascii");
694 OutputParams op(ascii);
696 mathed_parse_cell(ar, ods.str());
697 if (ar.size() == 2) {
699 if (ar[1].nucleus()->asBraceInset())
708 return ar[0].nucleus();
712 bool isAsciiOrMathAlpha(char_type c)
714 return isASCII(c) || Encodings::isMathAlpha(c);