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 "MathMacro.h"
54 #include "MathMacroArgument.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());
154 while (getline(fs, line)) {
157 if (line.empty() || line[0] == '#')
160 // special case of iffont/else/endif
161 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
162 istringstream is(line);
166 skip = !isMathFontAvailable(tmp);
168 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
171 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
177 // special case of pre-defined macros
178 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
179 //lyxerr << "macro definition: '" << line << '\'' << endl;
181 // \def\macroname{definition}
183 // \def\macroname{definition} requires
185 // \def\macroname{definition} extra xmlname requires
186 istringstream is(line);
192 is >> setw(65536) >> macro >> requires;
193 if ((is >> xmlname)) {
195 if (!(is >> requires))
199 MacroTable::iterator it = MacroTable::globalMacros().insert(
200 0, from_utf8(macro));
201 if (!extra.empty() || !xmlname.empty() || !requires.empty()) {
202 MathWordList::iterator wit = theMathWordList.find(it->first);
203 if (wit != theMathWordList.end())
204 LYXERR(Debug::MATHED, "readSymbols: inset "
205 << to_utf8(it->first) << " already exists.");
209 tmp.name = it->first;
210 tmp.extra = from_utf8(extra);
211 tmp.xmlname = from_utf8(xmlname);
212 if (requires == "hiddensymbol") {
214 tmp.hidden = hidden = true;
216 tmp.requires = requires;
217 theMathWordList[it->first] = tmp;
218 wit = theMathWordList.find(it->first);
219 it->second.setSymbol(&(wit->second));
222 // If you change the following output, please adjust
223 // development/tools/generate_symbols_images.py.
224 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(it->first)
227 << " extra: " << extra
228 << " xml: " << xmlname
229 << " requires: " << requires
230 << " hidden: " << hidden << '\'');
234 idocstringstream is(from_utf8(line));
237 is >> tmp.name >> help;
238 tmp.inset = to_ascii(help);
239 if (isFontName(tmp.inset))
240 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
243 // requires is optional
246 // backward compatibility
247 if (help == "esintoramsmath")
248 tmp.requires = "esint|amsmath";
250 tmp.requires = to_ascii(help);
253 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
254 << to_utf8(tmp.name) << ' ' << tmp.inset << ' '
255 << to_utf8(tmp.extra));
259 if (isFontName(tmp.inset)) {
260 // tmp.inset _is_ the fontname here.
261 // create fallbacks if necessary
263 // store requirements as long as we can
264 if (tmp.requires.empty()) {
265 if (tmp.inset == "msa" || tmp.inset == "msb")
266 tmp.requires = "amssymb";
267 else if (tmp.inset == "wasy")
268 tmp.requires = "wasysym";
269 else if (tmp.inset == "mathscr")
270 tmp.requires = "mathrsfs";
273 // symbol font is not available sometimes
274 string symbol_font = "lyxsymbol";
275 char_type unicodesymbol = 0;
277 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
278 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
280 } else if (isMathFontAvailable(tmp.inset) && canBeDisplayed(charid)) {
281 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
282 tmp.draw.push_back(char_type(charid));
283 } else if (fallbackid && isMathFontAvailable(symbol_font) &&
284 canBeDisplayed(fallbackid)) {
285 if (tmp.inset == "cmex")
286 tmp.inset = "lyxsymbol";
288 tmp.inset = "lyxboldsymbol";
289 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
290 tmp.draw.push_back(char_type(fallbackid));
291 } else if (isUnicodeSymbolAvailable(tmp.name, unicodesymbol)) {
292 LYXERR(Debug::MATHED, "unicode fallback for " << to_utf8(tmp.name));
293 tmp.inset = "mathnormal";
294 tmp.draw.push_back(unicodesymbol);
296 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
298 tmp.inset = "lyxtex";
301 // it's a proper inset
302 LYXERR(Debug::MATHED, "inset " << tmp.inset
303 << " used for " << to_utf8(tmp.name));
306 if (tmp.requires == "hiddensymbol")
312 if (theMathWordList.find(tmp.name) != theMathWordList.end())
313 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
314 << " already exists.");
316 theMathWordList[tmp.name] = tmp;
318 // If you change the following output, please adjust
319 // development/tools/generate_symbols_images.py.
320 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
321 << " inset: " << tmp.inset
322 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
323 << " extra: " << to_utf8(tmp.extra)
324 << " xml: " << to_utf8(tmp.xmlname)
325 << " requires: " << tmp.requires
326 << " hidden: " << tmp.hidden << '\'');
329 string tmp2 = "cmsy";
330 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
334 bool isSpecialChar(docstring const & name)
336 if (name.size() != 1)
337 return name == "textasciicircum" || name == "mathcircumflex" ||
338 name == "textasciitilde" || name == "textbackslash";
340 char_type const c = name.at(0);
341 return c == '{' || c == '}' || c == '&' || c == '$' ||
342 c == '#' || c == '%' || c == '_';
348 MathWordList const & mathedWordList()
350 return theMathWordList;
356 static bool initialized = false;
365 bool ensureMath(WriteStream & os, bool needs_mathmode, bool macro,
368 bool brace = os.pendingBrace();
369 os.pendingBrace(false);
372 if (os.textMode() && needs_mathmode) {
378 os << "\\ensuremath{";
382 } else if (macro && textmode_macro && !os.textMode()) {
388 os << "\\lyxmathsym{";
392 } else if (macro && brace && !needs_mathmode && !textmode_macro) {
393 // This is a user defined macro, not a MathMacro, so we
394 // cannot be sure what mode is needed. We leave it in the
395 // same environment it was entered by closing either \lyxmathsym
396 // or \ensuremath, whichever was opened.
399 os.textMode(!os.textMode());
405 int ensureMode(WriteStream & os, InsetMath::mode_type mode,
406 bool locked, bool ascii)
408 bool textmode = mode == InsetMath::TEXT_MODE;
409 if (os.latex() && textmode && os.pendingBrace()) {
411 os.pendingBrace(false);
412 os.pendingSpace(false);
415 int oldmodes = os.textMode() ? 0x01 : 0;
416 os.textMode(textmode);
417 oldmodes |= os.lockedMode() ? 0x02 : 0;
418 os.lockedMode(locked);
419 oldmodes |= os.asciiOnly() ? 0x04 : 0;
425 latexkeys const * in_word_set(docstring const & str)
427 MathWordList::iterator it = theMathWordList.find(str);
428 if (it == theMathWordList.end())
430 if (it->second.inset == "macro")
432 return &(it->second);
436 MathAtom createInsetMath(char const * const s, Buffer * buf)
438 return createInsetMath(from_utf8(s), buf);
442 MathAtom createInsetMath(docstring const & s, Buffer * buf)
444 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
445 if ((s == "ce" || s == "cf") && buf
446 && buf->params().use_package("mhchem") == BufferParams::package_off)
447 return MathAtom(new MathMacro(buf, s));
449 latexkeys const * l = in_word_set(s);
451 string const & inset = l->inset;
452 //lyxerr << " found inset: '" << inset << '\'' << endl;
454 return MathAtom(new InsetMathRef(buf, l->name));
455 if (inset == "overset")
456 return MathAtom(new InsetMathOverset(buf));
457 if (inset == "underset")
458 return MathAtom(new InsetMathUnderset(buf));
459 if (inset == "decoration")
460 return MathAtom(new InsetMathDecoration(buf, l));
461 if (inset == "space")
462 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
463 if (inset == "class")
464 return MathAtom(new InsetMathClass(buf, string_to_class(s)));
466 return MathAtom(new InsetMathDots(l));
468 return MathAtom(new InsetMathBox(buf, l->name));
469 // if (inset == "fbox")
470 // return MathAtom(new InsetMathFBox(l));
471 if (inset == "style")
472 return MathAtom(new InsetMathSize(buf, l));
474 return MathAtom(new InsetMathFont(buf, l));
475 if (inset == "oldfont")
476 return MathAtom(new InsetMathFontOld(buf, l));
477 if (inset == "matrix")
478 return MathAtom(new InsetMathAMSArray(buf, s));
479 if (inset == "split")
480 return MathAtom(new InsetMathSplit(buf, s));
482 // we can't create a InsetMathBig, since the argument
484 return MathAtom(new InsetMathUnknown(s));
485 return MathAtom(new InsetMathSymbol(l));
488 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
489 return MathAtom(new MathMacroArgument(s[1] - '0'));
490 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
491 && s[2] >= '1' && s[2] <= '9')
492 return MathAtom(new MathMacroArgument(s[2] - '0'));
494 return MathAtom(new InsetMathBoxed(buf));
496 return MathAtom(new InsetMathFBox(buf));
498 return MathAtom(new InsetMathMakebox(buf, true));
500 return MathAtom(new InsetMathMakebox(buf, false));
501 if (s.substr(0, 8) == "xymatrix") {
502 char spacing_code = '\0';
504 bool equal_spacing = false;
505 size_t const len = s.length();
507 if (i < len && s[i] == '@') {
509 if (i < len && s[i] == '!') {
510 equal_spacing = true;
517 spacing_code = static_cast<char>(s[i]);
520 } else if (i < len) {
528 spacing_code = static_cast<char>(s[i]);
532 if (i < len && s[i] == '=') {
534 spacing = Length(to_ascii(s.substr(i)));
538 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
543 return MathAtom(new InsetMathDiagram(buf));
544 if (s == "xrightarrow" || s == "xleftarrow" ||
545 s == "xhookrightarrow" || s == "xhookleftarrow" ||
546 s == "xRightarrow" || s == "xLeftarrow" ||
547 s == "xleftrightarrow" || s == "xLeftrightarrow" ||
548 s == "xrightharpoondown" || s == "xrightharpoonup" ||
549 s == "xleftharpoondown" || s == "xleftharpoonup" ||
550 s == "xleftrightharpoons" || s == "xrightleftharpoons" ||
552 return MathAtom(new InsetMathXArrow(buf, s));
553 if (s == "split" || s == "alignedat")
554 return MathAtom(new InsetMathSplit(buf, s));
556 return MathAtom(new InsetMathCases(buf));
558 return MathAtom(new InsetMathSubstack(buf));
559 if (s == "subarray" || s == "array")
560 return MathAtom(new InsetMathArray(buf, s, 1, 1));
562 return MathAtom(new InsetMathSqrt(buf));
564 return MathAtom(new InsetMathRoot(buf));
566 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
568 return MathAtom(new InsetMathStackrel(buf, false));
569 // This string value is only for math toolbar use, no LaTeX name
570 if (s == "stackrelthree")
571 return MathAtom(new InsetMathStackrel(buf, true));
573 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
575 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
577 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
579 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
581 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
583 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
585 return MathAtom(new InsetMathFrac(buf));
587 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
589 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
591 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
593 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
595 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
597 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
598 // These string values are only for math toolbar use, no LaTeX names
599 if (s == "unitfracthree")
600 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
602 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
604 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
605 if (s == "cfracleft")
606 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
607 if (s == "cfracright")
608 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
610 // return MathAtom(new MathInferInset);
612 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
614 return MathAtom(new InsetMathLefteqn(buf));
615 if (s == "boldsymbol")
616 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
618 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
619 if (s == "heavysymbol" || s == "hm")
620 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
621 if (s == "color" || s == "normalcolor")
622 return MathAtom(new InsetMathColor(buf, true));
623 if (s == "textcolor")
624 return MathAtom(new InsetMathColor(buf, false));
626 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
628 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
630 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
632 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::cancel));
634 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::bcancel));
636 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::xcancel));
638 return MathAtom(new InsetMathCancelto(buf));
640 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smash));
641 // The following 2 string values are only for math toolbar use, no LaTeX names
643 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smashb));
645 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smasht));
647 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathclap));
649 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathllap));
651 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathrlap));
652 if (s == "ensuremath")
653 return MathAtom(new InsetMathEnsureMath(buf));
655 return MathAtom(new InsetMathSideset(buf, true, true));
656 // The following 3 string values are only for math toolbar use, no LaTeX names
658 return MathAtom(new InsetMathSideset(buf, false, true));
660 return MathAtom(new InsetMathSideset(buf, true, false));
662 return MathAtom(new InsetMathSideset(buf, false, false));
663 if (isSpecialChar(s))
664 return MathAtom(new InsetMathSpecialChar(s));
666 return MathAtom(new InsetMathSpace(" ", ""));
668 return MathAtom(new InsetMathHull(buf, hullRegexp));
670 return MathAtom(new MathMacro(buf, s));
674 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
677 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
679 docstring body = split(str, name, ' ');
682 InsetCommandParams icp(REF_CODE);
684 InsetCommand::string2params(to_utf8(str), icp);
685 Encoding const * const utf8 = encodings.fromLyXName("utf8");
686 OutputParams op(utf8);
687 mathed_parse_cell(ar, icp.getCommand(op));
688 } else if (name == "mathspace") {
689 InsetSpaceParams isp(true);
690 InsetSpace::string2params(to_utf8(str), isp);
692 odocstringstream ods;
694 Encoding const * const ascii = encodings.fromLyXName("ascii");
695 OutputParams op(ascii);
697 mathed_parse_cell(ar, ods.str());
698 if (ar.size() == 2) {
700 if (ar[1].nucleus()->asBraceInset())
709 return ar[0].nucleus();
713 bool isAsciiOrMathAlpha(char_type c)
715 return isASCII(c) || Encodings::isMathAlpha(c);