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"
77 using namespace lyx::support;
86 MathWordList theMathWordList;
89 bool isMathFontAvailable(string & name)
95 augmentFont(f, from_ascii(name));
97 // Do we have the font proper?
98 if (theFontLoader().available(f))
102 if (name == "eufrak") {
103 name = "lyxfakefrak";
107 LYXERR(Debug::MATHED,
108 "font " << name << " not available and I can't fake it");
113 bool canBeDisplayed(char_type c)
117 return theFontLoader().canBeDisplayed(c);
121 bool isUnicodeSymbolAvailable(docstring const & name, char_type & c)
123 docstring cmd(from_ascii("\\") + name);
126 c = Encodings::fromLaTeXCommand(cmd, Encodings::MATH_CMD,
127 is_combining, termination);
128 if (c == 0 && name == "varOmega") {
129 // fallback for bug 7954, unicodesymbols does not list
130 // \\varOmega because of requirements, but this might change
131 cmd = from_ascii("\\mathit{\\Omega}");
132 c = Encodings::fromLaTeXCommand(cmd, Encodings::MATH_CMD,
133 is_combining, termination);
135 return c != 0 && !is_combining;
141 FileName const filename = libFileSearch(string(), "symbols");
142 LYXERR(Debug::MATHED, "read symbols from " << filename);
143 if (filename.empty()) {
144 lyxerr << "Could not find symbols file" << endl;
148 ifstream fs(filename.toFilesystemEncoding().c_str());
151 while (getline(fs, line)) {
154 if (line.empty() || line[0] == '#')
157 // special case of iffont/else/endif
158 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
159 istringstream is(line);
163 skip = !isMathFontAvailable(tmp);
165 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
168 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
174 // special case of pre-defined macros
175 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
176 //lyxerr << "macro definition: '" << line << '\'' << endl;
178 // \def\macroname{definition}
180 // \def\macroname{definition} requires
182 // \def\macroname{definition} extra xmlname requires
183 istringstream is(line);
189 is >> macro >> requires;
190 if ((is >> xmlname)) {
192 if (!(is >> requires))
196 MacroTable::iterator it = MacroTable::globalMacros().insert(
197 0, from_utf8(macro));
198 if (!extra.empty() || !xmlname.empty() || !requires.empty()) {
199 MathWordList::iterator wit = theMathWordList.find(it->first);
200 if (wit != theMathWordList.end())
201 LYXERR(Debug::MATHED, "readSymbols: inset "
202 << to_utf8(it->first) << " already exists.");
206 tmp.name = it->first;
207 tmp.extra = from_utf8(extra);
208 tmp.xmlname = from_utf8(xmlname);
209 if (requires == "hiddensymbol") {
211 tmp.hidden = hidden = true;
213 tmp.requires = requires;
214 theMathWordList[it->first] = tmp;
215 wit = theMathWordList.find(it->first);
216 it->second.setSymbol(&(wit->second));
219 // If you change the following output, please adjust
220 // development/tools/generate_symbols_images.py.
221 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(it->first)
224 << " extra: " << extra
225 << " xml: " << xmlname
226 << " requires: " << requires
227 << " hidden: " << hidden << '\'');
231 idocstringstream is(from_utf8(line));
234 is >> tmp.name >> help;
235 tmp.inset = to_ascii(help);
236 if (isFontName(help))
237 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
240 // requires is optional
243 // backward compatibility
244 if (help == "esintoramsmath")
245 tmp.requires = "esint|amsmath";
247 tmp.requires = to_ascii(help);
250 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
251 << to_utf8(tmp.name) << ' ' << tmp.inset << ' '
252 << to_utf8(tmp.extra));
256 if (isFontName(from_ascii(tmp.inset))) {
257 // tmp.inset _is_ the fontname here.
258 // create fallbacks if necessary
260 // store requirements as long as we can
261 if (tmp.requires.empty()) {
262 if (tmp.inset == "msa" || tmp.inset == "msb")
263 tmp.requires = "amssymb";
264 else if (tmp.inset == "wasy")
265 tmp.requires = "wasysym";
266 else if (tmp.inset == "mathscr")
267 tmp.requires = "mathrsfs";
270 // symbol font is not available sometimes
271 string symbol_font = "lyxsymbol";
272 char_type unicodesymbol = 0;
274 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
275 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
277 } else if (isMathFontAvailable(tmp.inset) && canBeDisplayed(charid)) {
278 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
279 tmp.draw.push_back(char_type(charid));
280 } else if (fallbackid && isMathFontAvailable(symbol_font) &&
281 canBeDisplayed(fallbackid)) {
282 if (tmp.inset == "cmex")
283 tmp.inset = "lyxsymbol";
285 tmp.inset = "lyxboldsymbol";
286 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
287 tmp.draw.push_back(char_type(fallbackid));
288 } else if (isUnicodeSymbolAvailable(tmp.name, unicodesymbol)) {
289 LYXERR(Debug::MATHED, "unicode fallback for " << to_utf8(tmp.name));
290 tmp.inset = "mathnormal";
291 tmp.draw.push_back(unicodesymbol);
293 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
295 tmp.inset = "lyxtex";
298 // it's a proper inset
299 LYXERR(Debug::MATHED, "inset " << tmp.inset
300 << " used for " << to_utf8(tmp.name));
303 if (tmp.requires == "hiddensymbol")
309 if (theMathWordList.find(tmp.name) != theMathWordList.end())
310 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
311 << " already exists.");
313 theMathWordList[tmp.name] = tmp;
315 // If you change the following output, please adjust
316 // development/tools/generate_symbols_images.py.
317 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
318 << " inset: " << tmp.inset
319 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
320 << " extra: " << to_utf8(tmp.extra)
321 << " xml: " << to_utf8(tmp.xmlname)
322 << " requires: " << tmp.requires
323 << " hidden: " << tmp.hidden << '\'');
326 string tmp2 = "cmsy";
327 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
331 bool isSpecialChar(docstring const & name)
333 if (name.size() != 1)
334 return name == "textasciicircum" || name == "mathcircumflex" ||
335 name == "textasciitilde" || name == "textbackslash";
337 char_type const c = name.at(0);
338 return c == '{' || c == '}' || c == '&' || c == '$' ||
339 c == '#' || c == '%' || c == '_';
345 MathWordList const & mathedWordList()
347 return theMathWordList;
353 static bool initialized = false;
362 bool ensureMath(WriteStream & os, bool needs_math_mode, bool macro)
364 bool brace = os.pendingBrace();
365 os.pendingBrace(false);
368 if (os.textMode() && needs_math_mode) {
369 os << "\\ensuremath{";
372 } else if (macro && brace && !needs_math_mode) {
373 // This is a user defined macro, but not a MathMacro, so we
374 // cannot be sure what mode is needed. As it was entered in
375 // a text box, we restore the text mode.
384 int ensureMode(WriteStream & os, InsetMath::mode_type mode,
385 bool locked, bool ascii)
387 bool textmode = mode == InsetMath::TEXT_MODE;
388 if (os.latex() && textmode && os.pendingBrace()) {
390 os.pendingBrace(false);
391 os.pendingSpace(false);
394 int oldmodes = os.textMode() ? 0x01 : 0;
395 os.textMode(textmode);
396 oldmodes |= os.lockedMode() ? 0x02 : 0;
397 os.lockedMode(locked);
398 oldmodes |= os.asciiOnly() ? 0x04 : 0;
404 latexkeys const * in_word_set(docstring const & str)
406 MathWordList::iterator it = theMathWordList.find(str);
407 if (it == theMathWordList.end())
409 if (it->second.inset == "macro")
411 return &(it->second);
415 MathAtom createInsetMath(char const * const s, Buffer * buf)
417 return createInsetMath(from_utf8(s), buf);
421 MathAtom createInsetMath(docstring const & s, Buffer * buf)
423 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
424 if ((s == "ce" || s == "cf") && buf
425 && buf->params().use_package("mhchem") == BufferParams::package_off)
426 return MathAtom(new MathMacro(buf, s));
428 latexkeys const * l = in_word_set(s);
430 string const & inset = l->inset;
431 //lyxerr << " found inset: '" << inset << '\'' << endl;
433 return MathAtom(new InsetMathRef(buf, l->name));
434 if (inset == "overset")
435 return MathAtom(new InsetMathOverset(buf));
436 if (inset == "underset")
437 return MathAtom(new InsetMathUnderset(buf));
438 if (inset == "decoration")
439 return MathAtom(new InsetMathDecoration(buf, l));
440 if (inset == "space")
441 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
443 return MathAtom(new InsetMathDots(l));
445 return MathAtom(new InsetMathBox(buf, l->name));
446 // if (inset == "fbox")
447 // return MathAtom(new InsetMathFBox(l));
448 if (inset == "style")
449 return MathAtom(new InsetMathSize(buf, l));
451 return MathAtom(new InsetMathFont(buf, l));
452 if (inset == "oldfont")
453 return MathAtom(new InsetMathFontOld(buf, l));
454 if (inset == "matrix")
455 return MathAtom(new InsetMathAMSArray(buf, s));
456 if (inset == "split")
457 return MathAtom(new InsetMathSplit(buf, s));
459 // we can't create a InsetMathBig, since the argument
461 return MathAtom(new InsetMathUnknown(s));
462 return MathAtom(new InsetMathSymbol(l));
465 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
466 return MathAtom(new MathMacroArgument(s[1] - '0'));
467 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
468 && s[2] >= '1' && s[2] <= '9')
469 return MathAtom(new MathMacroArgument(s[2] - '0'));
471 return MathAtom(new InsetMathBoxed(buf));
473 return MathAtom(new InsetMathFBox(buf));
475 return MathAtom(new InsetMathMakebox(buf, true));
477 return MathAtom(new InsetMathMakebox(buf, false));
479 return MathAtom(new InsetMathKern);
480 if (s.substr(0, 8) == "xymatrix") {
481 char spacing_code = '\0';
483 bool equal_spacing = false;
484 size_t const len = s.length();
486 if (i < len && s[i] == '@') {
488 if (i < len && s[i] == '!') {
489 equal_spacing = true;
496 spacing_code = static_cast<char>(s[i]);
499 } else if (i < len) {
507 spacing_code = static_cast<char>(s[i]);
511 if (i < len && s[i] == '=') {
513 spacing = Length(to_ascii(s.substr(i)));
517 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
522 return MathAtom(new InsetMathDiagram(buf));
523 if (s == "xrightarrow" || s == "xleftarrow" ||
524 s == "xhookrightarrow" || s == "xhookleftarrow" ||
525 s == "xRightarrow" || s == "xLeftarrow" ||
526 s == "xleftrightarrow" || s == "xLeftrightarrow" ||
527 s == "xrightharpoondown" || s == "xrightharpoonup" ||
528 s == "xleftharpoondown" || s == "xleftharpoonup" ||
529 s == "xleftrightharpoons" || s == "xrightleftharpoons" ||
531 return MathAtom(new InsetMathXArrow(buf, s));
532 if (s == "split" || s == "alignedat")
533 return MathAtom(new InsetMathSplit(buf, s));
535 return MathAtom(new InsetMathCases(buf));
537 return MathAtom(new InsetMathSubstack(buf));
538 if (s == "subarray" || s == "array")
539 return MathAtom(new InsetMathArray(buf, s, 1, 1));
541 return MathAtom(new InsetMathSqrt(buf));
543 return MathAtom(new InsetMathRoot(buf));
545 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
547 return MathAtom(new InsetMathStackrel(buf, false));
548 // This string value is only for math toolbar use, no LaTeX name
549 if (s == "stackrelthree")
550 return MathAtom(new InsetMathStackrel(buf, true));
552 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
554 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
556 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
558 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
560 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
562 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
564 return MathAtom(new InsetMathFrac(buf));
566 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
568 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
570 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
572 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
574 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
576 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
577 // These string values are only for math toolbar use, no LaTeX names
578 if (s == "unitfracthree")
579 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
581 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
583 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
584 if (s == "cfracleft")
585 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
586 if (s == "cfracright")
587 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
589 // return MathAtom(new MathInferInset);
591 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
593 return MathAtom(new InsetMathLefteqn(buf));
594 if (s == "boldsymbol")
595 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
597 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
598 if (s == "heavysymbol" || s == "hm")
599 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
600 if (s == "color" || s == "normalcolor")
601 return MathAtom(new InsetMathColor(buf, true));
602 if (s == "textcolor")
603 return MathAtom(new InsetMathColor(buf, false));
605 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
607 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
609 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
611 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::cancel));
613 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::bcancel));
615 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::xcancel));
617 return MathAtom(new InsetMathCancelto(buf));
619 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smash));
620 // The following 2 string values are only for math toolbar use, no LaTeX names
622 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smashb));
624 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smasht));
626 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathclap));
628 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathllap));
630 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathrlap));
631 if (s == "ensuremath")
632 return MathAtom(new InsetMathEnsureMath(buf));
634 return MathAtom(new InsetMathSideset(buf, true, true));
635 // The following 3 string values are only for math toolbar use, no LaTeX names
637 return MathAtom(new InsetMathSideset(buf, false, true));
639 return MathAtom(new InsetMathSideset(buf, true, false));
641 return MathAtom(new InsetMathSideset(buf, false, false));
642 if (isSpecialChar(s))
643 return MathAtom(new InsetMathSpecialChar(s));
645 return MathAtom(new InsetMathSpace(" ", ""));
648 return MathAtom(new InsetMathHull(buf, hullRegexp));
650 return MathAtom(new MathMacro(buf, s));
654 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
657 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
659 docstring body = split(str, name, ' ');
662 InsetCommandParams icp(REF_CODE);
664 InsetCommand::string2params(to_utf8(str), icp);
665 Encoding const * const utf8 = encodings.fromLyXName("utf8");
666 OutputParams op(utf8);
667 mathed_parse_cell(ar, icp.getCommand(op));
668 } else if (name == "mathspace") {
669 InsetSpaceParams isp(true);
670 InsetSpace::string2params(to_utf8(str), isp);
673 odocstringstream ods;
674 otexstream os(ods, texrow);
675 Encoding const * const ascii = encodings.fromLyXName("ascii");
676 OutputParams op(ascii);
678 mathed_parse_cell(ar, ods.str());
679 if (ar.size() == 2) {
681 if (ar[1].nucleus()->asBraceInset())
690 return ar[0].nucleus();
694 bool isAsciiOrMathAlpha(char_type c)
696 return isASCII(c) || Encodings::isMathAlpha(c);