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_math_mode, bool macro)
365 bool brace = os.pendingBrace();
366 os.pendingBrace(false);
369 if (os.textMode() && needs_math_mode) {
370 os << "\\ensuremath{";
373 } else if (macro && brace && !needs_math_mode) {
374 // This is a user defined macro, but not a MathMacro, so we
375 // cannot be sure what mode is needed. As it was entered in
376 // a text box, we restore the text mode.
385 int ensureMode(WriteStream & os, InsetMath::mode_type mode,
386 bool locked, bool ascii)
388 bool textmode = mode == InsetMath::TEXT_MODE;
389 if (os.latex() && textmode && os.pendingBrace()) {
391 os.pendingBrace(false);
392 os.pendingSpace(false);
395 int oldmodes = os.textMode() ? 0x01 : 0;
396 os.textMode(textmode);
397 oldmodes |= os.lockedMode() ? 0x02 : 0;
398 os.lockedMode(locked);
399 oldmodes |= os.asciiOnly() ? 0x04 : 0;
405 latexkeys const * in_word_set(docstring const & str)
407 MathWordList::iterator it = theMathWordList.find(str);
408 if (it == theMathWordList.end())
410 if (it->second.inset == "macro")
412 return &(it->second);
416 MathAtom createInsetMath(char const * const s, Buffer * buf)
418 return createInsetMath(from_utf8(s), buf);
422 MathAtom createInsetMath(docstring const & s, Buffer * buf)
424 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
425 if ((s == "ce" || s == "cf") && buf
426 && buf->params().use_package("mhchem") == BufferParams::package_off)
427 return MathAtom(new MathMacro(buf, s));
429 latexkeys const * l = in_word_set(s);
431 string const & inset = l->inset;
432 //lyxerr << " found inset: '" << inset << '\'' << endl;
434 return MathAtom(new InsetMathRef(buf, l->name));
435 if (inset == "overset")
436 return MathAtom(new InsetMathOverset(buf));
437 if (inset == "underset")
438 return MathAtom(new InsetMathUnderset(buf));
439 if (inset == "decoration")
440 return MathAtom(new InsetMathDecoration(buf, l));
441 if (inset == "space")
442 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
444 return MathAtom(new InsetMathDots(l));
446 return MathAtom(new InsetMathBox(buf, l->name));
447 // if (inset == "fbox")
448 // return MathAtom(new InsetMathFBox(l));
449 if (inset == "style")
450 return MathAtom(new InsetMathSize(buf, l));
452 return MathAtom(new InsetMathFont(buf, l));
453 if (inset == "oldfont")
454 return MathAtom(new InsetMathFontOld(buf, l));
455 if (inset == "matrix")
456 return MathAtom(new InsetMathAMSArray(buf, s));
457 if (inset == "split")
458 return MathAtom(new InsetMathSplit(buf, s));
460 // we can't create a InsetMathBig, since the argument
462 return MathAtom(new InsetMathUnknown(s));
463 return MathAtom(new InsetMathSymbol(l));
466 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
467 return MathAtom(new MathMacroArgument(s[1] - '0'));
468 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
469 && s[2] >= '1' && s[2] <= '9')
470 return MathAtom(new MathMacroArgument(s[2] - '0'));
472 return MathAtom(new InsetMathBoxed(buf));
474 return MathAtom(new InsetMathFBox(buf));
476 return MathAtom(new InsetMathMakebox(buf, true));
478 return MathAtom(new InsetMathMakebox(buf, false));
480 return MathAtom(new InsetMathKern);
481 if (s.substr(0, 8) == "xymatrix") {
482 char spacing_code = '\0';
484 bool equal_spacing = false;
485 size_t const len = s.length();
487 if (i < len && s[i] == '@') {
489 if (i < len && s[i] == '!') {
490 equal_spacing = true;
497 spacing_code = static_cast<char>(s[i]);
500 } else if (i < len) {
508 spacing_code = static_cast<char>(s[i]);
512 if (i < len && s[i] == '=') {
514 spacing = Length(to_ascii(s.substr(i)));
518 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
523 return MathAtom(new InsetMathDiagram(buf));
524 if (s == "xrightarrow" || s == "xleftarrow" ||
525 s == "xhookrightarrow" || s == "xhookleftarrow" ||
526 s == "xRightarrow" || s == "xLeftarrow" ||
527 s == "xleftrightarrow" || s == "xLeftrightarrow" ||
528 s == "xrightharpoondown" || s == "xrightharpoonup" ||
529 s == "xleftharpoondown" || s == "xleftharpoonup" ||
530 s == "xleftrightharpoons" || s == "xrightleftharpoons" ||
532 return MathAtom(new InsetMathXArrow(buf, s));
533 if (s == "split" || s == "alignedat")
534 return MathAtom(new InsetMathSplit(buf, s));
536 return MathAtom(new InsetMathCases(buf));
538 return MathAtom(new InsetMathSubstack(buf));
539 if (s == "subarray" || s == "array")
540 return MathAtom(new InsetMathArray(buf, s, 1, 1));
542 return MathAtom(new InsetMathSqrt(buf));
544 return MathAtom(new InsetMathRoot(buf));
546 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
548 return MathAtom(new InsetMathStackrel(buf, false));
549 // This string value is only for math toolbar use, no LaTeX name
550 if (s == "stackrelthree")
551 return MathAtom(new InsetMathStackrel(buf, true));
553 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
555 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
557 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
559 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
561 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
563 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
565 return MathAtom(new InsetMathFrac(buf));
567 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
569 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
571 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
573 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
575 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
577 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
578 // These string values are only for math toolbar use, no LaTeX names
579 if (s == "unitfracthree")
580 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
582 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
584 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
585 if (s == "cfracleft")
586 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
587 if (s == "cfracright")
588 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
590 // return MathAtom(new MathInferInset);
592 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
594 return MathAtom(new InsetMathLefteqn(buf));
595 if (s == "boldsymbol")
596 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
598 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
599 if (s == "heavysymbol" || s == "hm")
600 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
601 if (s == "color" || s == "normalcolor")
602 return MathAtom(new InsetMathColor(buf, true));
603 if (s == "textcolor")
604 return MathAtom(new InsetMathColor(buf, false));
606 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
608 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
610 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
612 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::cancel));
614 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::bcancel));
616 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::xcancel));
618 return MathAtom(new InsetMathCancelto(buf));
620 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smash));
621 // The following 2 string values are only for math toolbar use, no LaTeX names
623 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smashb));
625 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smasht));
627 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathclap));
629 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathllap));
631 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathrlap));
632 if (s == "ensuremath")
633 return MathAtom(new InsetMathEnsureMath(buf));
635 return MathAtom(new InsetMathSideset(buf, true, true));
636 // The following 3 string values are only for math toolbar use, no LaTeX names
638 return MathAtom(new InsetMathSideset(buf, false, true));
640 return MathAtom(new InsetMathSideset(buf, true, false));
642 return MathAtom(new InsetMathSideset(buf, false, false));
643 if (isSpecialChar(s))
644 return MathAtom(new InsetMathSpecialChar(s));
646 return MathAtom(new InsetMathSpace(" ", ""));
649 return MathAtom(new InsetMathHull(buf, hullRegexp));
651 return MathAtom(new MathMacro(buf, s));
655 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
658 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
660 docstring body = split(str, name, ' ');
663 InsetCommandParams icp(REF_CODE);
665 InsetCommand::string2params(to_utf8(str), icp);
666 Encoding const * const utf8 = encodings.fromLyXName("utf8");
667 OutputParams op(utf8);
668 mathed_parse_cell(ar, icp.getCommand(op));
669 } else if (name == "mathspace") {
670 InsetSpaceParams isp(true);
671 InsetSpace::string2params(to_utf8(str), isp);
673 odocstringstream ods;
674 otexstream os(ods, false);
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);