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);
188 is >> macro >> requires;
189 if ((is >> xmlname)) {
191 if (!(is >> requires))
195 MacroTable::iterator it = MacroTable::globalMacros().insert(
196 0, from_utf8(macro));
197 if (!extra.empty() || !xmlname.empty() || !requires.empty()) {
198 MathWordList::iterator wit = theMathWordList.find(it->first);
199 if (wit != theMathWordList.end())
200 LYXERR(Debug::MATHED, "readSymbols: inset "
201 << to_utf8(it->first) << " already exists.");
205 tmp.name = it->first;
206 tmp.extra = from_utf8(extra);
207 tmp.xmlname = from_utf8(xmlname);
208 tmp.requires = requires;
209 theMathWordList[it->first] = tmp;
210 wit = theMathWordList.find(it->first);
211 it->second.setSymbol(&(wit->second));
214 // If you change the following output, please adjust
215 // development/tools/generate_symbols_images.py.
216 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(it->first)
219 << " extra: " << extra
220 << " xml: " << xmlname
221 << " requires: " << requires << '\'');
225 idocstringstream is(from_utf8(line));
228 is >> tmp.name >> help;
229 tmp.inset = to_ascii(help);
230 if (isFontName(help))
231 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
234 // requires is optional
237 // backward compatibility
238 if (help == "esintoramsmath")
239 tmp.requires = "esint|amsmath";
241 tmp.requires = to_ascii(help);
244 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
245 << to_utf8(tmp.name) << ' ' << tmp.inset << ' '
246 << to_utf8(tmp.extra));
250 if (isFontName(from_ascii(tmp.inset))) {
251 // tmp.inset _is_ the fontname here.
252 // create fallbacks if necessary
254 // store requirements as long as we can
255 if (tmp.requires.empty()) {
256 if (tmp.inset == "msa" || tmp.inset == "msb")
257 tmp.requires = "amssymb";
258 else if (tmp.inset == "wasy")
259 tmp.requires = "wasysym";
260 else if (tmp.inset == "mathscr")
261 tmp.requires = "mathrsfs";
264 // symbol font is not available sometimes
265 string symbol_font = "lyxsymbol";
266 char_type unicodesymbol = 0;
268 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
269 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
271 } else if (isMathFontAvailable(tmp.inset) && canBeDisplayed(charid)) {
272 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
273 tmp.draw.push_back(char_type(charid));
274 } else if (fallbackid && isMathFontAvailable(symbol_font) &&
275 canBeDisplayed(fallbackid)) {
276 if (tmp.inset == "cmex")
277 tmp.inset = "lyxsymbol";
279 tmp.inset = "lyxboldsymbol";
280 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
281 tmp.draw.push_back(char_type(fallbackid));
282 } else if (isUnicodeSymbolAvailable(tmp.name, unicodesymbol)) {
283 LYXERR(Debug::MATHED, "unicode fallback for " << to_utf8(tmp.name));
284 tmp.inset = "mathnormal";
285 tmp.draw.push_back(unicodesymbol);
287 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
289 tmp.inset = "lyxtex";
292 // it's a proper inset
293 LYXERR(Debug::MATHED, "inset " << tmp.inset
294 << " used for " << to_utf8(tmp.name));
297 if (theMathWordList.find(tmp.name) != theMathWordList.end())
298 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
299 << " already exists.");
301 theMathWordList[tmp.name] = tmp;
303 // If you change the following output, please adjust
304 // development/tools/generate_symbols_images.py.
305 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
306 << " inset: " << tmp.inset
307 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
308 << " extra: " << to_utf8(tmp.extra)
309 << " xml: " << to_utf8(tmp.xmlname)
310 << " requires: " << tmp.requires << '\'');
313 string tmp2 = "cmsy";
314 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
318 bool isSpecialChar(docstring const & name)
320 if (name.size() != 1)
321 return name == "textasciicircum" || name == "mathcircumflex" ||
322 name == "textasciitilde" || name == "textbackslash";
324 char_type const c = name.at(0);
325 return c == '{' || c == '}' || c == '&' || c == '$' ||
326 c == '#' || c == '%' || c == '_';
332 MathWordList const & mathedWordList()
334 return theMathWordList;
340 static bool initialized = false;
349 bool ensureMath(WriteStream & os, bool needs_math_mode, bool macro)
351 bool brace = os.pendingBrace();
352 os.pendingBrace(false);
355 if (os.textMode() && needs_math_mode) {
356 os << "\\ensuremath{";
359 } else if (macro && brace && !needs_math_mode) {
360 // This is a user defined macro, but not a MathMacro, so we
361 // cannot be sure what mode is needed. As it was entered in
362 // a text box, we restore the text mode.
371 int ensureMode(WriteStream & os, InsetMath::mode_type mode,
372 bool locked, bool ascii)
374 bool textmode = mode == InsetMath::TEXT_MODE;
375 if (os.latex() && textmode && os.pendingBrace()) {
377 os.pendingBrace(false);
378 os.pendingSpace(false);
381 int oldmodes = os.textMode() ? 0x01 : 0;
382 os.textMode(textmode);
383 oldmodes |= os.lockedMode() ? 0x02 : 0;
384 os.lockedMode(locked);
385 oldmodes |= os.asciiOnly() ? 0x04 : 0;
391 latexkeys const * in_word_set(docstring const & str)
393 MathWordList::iterator it = theMathWordList.find(str);
394 if (it == theMathWordList.end())
396 if (it->second.inset == "macro")
398 return &(it->second);
402 MathAtom createInsetMath(char const * const s, Buffer * buf)
404 return createInsetMath(from_utf8(s), buf);
408 MathAtom createInsetMath(docstring const & s, Buffer * buf)
410 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
411 if ((s == "ce" || s == "cf") && buf
412 && buf->params().use_package("mhchem") == BufferParams::package_off)
413 return MathAtom(new MathMacro(buf, s));
415 latexkeys const * l = in_word_set(s);
417 string const & inset = l->inset;
418 //lyxerr << " found inset: '" << inset << '\'' << endl;
420 return MathAtom(new InsetMathRef(buf, l->name));
421 if (inset == "overset")
422 return MathAtom(new InsetMathOverset(buf));
423 if (inset == "underset")
424 return MathAtom(new InsetMathUnderset(buf));
425 if (inset == "decoration")
426 return MathAtom(new InsetMathDecoration(buf, l));
427 if (inset == "space")
428 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
430 return MathAtom(new InsetMathDots(l));
432 return MathAtom(new InsetMathBox(buf, l->name));
433 // if (inset == "fbox")
434 // return MathAtom(new InsetMathFBox(l));
435 if (inset == "style")
436 return MathAtom(new InsetMathSize(buf, l));
438 return MathAtom(new InsetMathFont(buf, l));
439 if (inset == "oldfont")
440 return MathAtom(new InsetMathFontOld(buf, l));
441 if (inset == "matrix")
442 return MathAtom(new InsetMathAMSArray(buf, s));
443 if (inset == "split")
444 return MathAtom(new InsetMathSplit(buf, s));
446 // we can't create a InsetMathBig, since the argument
448 return MathAtom(new InsetMathUnknown(s));
449 return MathAtom(new InsetMathSymbol(l));
452 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
453 return MathAtom(new MathMacroArgument(s[1] - '0'));
454 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
455 && s[2] >= '1' && s[2] <= '9')
456 return MathAtom(new MathMacroArgument(s[2] - '0'));
458 return MathAtom(new InsetMathBoxed(buf));
460 return MathAtom(new InsetMathFBox(buf));
462 return MathAtom(new InsetMathMakebox(buf, true));
464 return MathAtom(new InsetMathMakebox(buf, false));
466 return MathAtom(new InsetMathKern);
467 if (s.substr(0, 8) == "xymatrix") {
468 char spacing_code = '\0';
470 bool equal_spacing = false;
471 size_t const len = s.length();
473 if (i < len && s[i] == '@') {
475 if (i < len && s[i] == '!') {
476 equal_spacing = true;
483 spacing_code = static_cast<char>(s[i]);
486 } else if (i < len) {
494 spacing_code = static_cast<char>(s[i]);
498 if (i < len && s[i] == '=') {
500 spacing = Length(to_ascii(s.substr(i)));
504 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
509 return MathAtom(new InsetMathDiagram(buf));
510 if (s == "xrightarrow" || s == "xleftarrow" ||
511 s == "xhookrightarrow" || s == "xhookleftarrow" ||
512 s == "xRightarrow" || s == "xLeftarrow" ||
513 s == "xleftrightarrow" || s == "xLeftrightarrow" ||
514 s == "xrightharpoondown" || s == "xrightharpoonup" ||
515 s == "xleftharpoondown" || s == "xleftharpoonup" ||
516 s == "xleftrightharpoons" || s == "xrightleftharpoons" ||
518 return MathAtom(new InsetMathXArrow(buf, s));
519 if (s == "split" || s == "alignedat")
520 return MathAtom(new InsetMathSplit(buf, s));
522 return MathAtom(new InsetMathCases(buf));
524 return MathAtom(new InsetMathSubstack(buf));
525 if (s == "subarray" || s == "array")
526 return MathAtom(new InsetMathArray(buf, s, 1, 1));
528 return MathAtom(new InsetMathSqrt(buf));
530 return MathAtom(new InsetMathRoot(buf));
532 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
534 return MathAtom(new InsetMathStackrel(buf, false));
535 // This string value is only for math toolbar use, no LaTeX name
536 if (s == "stackrelthree")
537 return MathAtom(new InsetMathStackrel(buf, true));
539 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
541 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
543 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
545 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
547 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
549 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
551 return MathAtom(new InsetMathFrac(buf));
553 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
555 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
557 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
559 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
561 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
563 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
564 // These string values are only for math toolbar use, no LaTeX names
565 if (s == "unitfracthree")
566 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
568 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
570 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
571 if (s == "cfracleft")
572 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
573 if (s == "cfracright")
574 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
576 // return MathAtom(new MathInferInset);
578 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
580 return MathAtom(new InsetMathLefteqn(buf));
581 if (s == "boldsymbol")
582 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
584 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
585 if (s == "heavysymbol" || s == "hm")
586 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
587 if (s == "color" || s == "normalcolor")
588 return MathAtom(new InsetMathColor(buf, true));
589 if (s == "textcolor")
590 return MathAtom(new InsetMathColor(buf, false));
592 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
594 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
596 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
598 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::cancel));
600 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::bcancel));
602 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::xcancel));
604 return MathAtom(new InsetMathCancelto(buf));
606 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smash));
607 // The following 2 string values are only for math toolbar use, no LaTeX names
609 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smashb));
611 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smasht));
613 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathclap));
615 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathllap));
617 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathrlap));
618 if (s == "ensuremath")
619 return MathAtom(new InsetMathEnsureMath(buf));
621 return MathAtom(new InsetMathSideset(buf, true, true));
622 // The following 3 string values are only for math toolbar use, no LaTeX names
624 return MathAtom(new InsetMathSideset(buf, false, true));
626 return MathAtom(new InsetMathSideset(buf, true, false));
628 return MathAtom(new InsetMathSideset(buf, false, false));
629 if (isSpecialChar(s))
630 return MathAtom(new InsetMathSpecialChar(s));
632 return MathAtom(new InsetMathSpace(" ", ""));
635 return MathAtom(new InsetMathHull(buf, hullRegexp));
637 return MathAtom(new MathMacro(buf, s));
641 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
644 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
646 docstring body = split(str, name, ' ');
649 InsetCommandParams icp(REF_CODE);
651 InsetCommand::string2params(to_utf8(str), icp);
652 Encoding const * const utf8 = encodings.fromLyXName("utf8");
653 OutputParams op(utf8);
654 mathed_parse_cell(ar, icp.getCommand(op));
655 } else if (name == "mathspace") {
656 InsetSpaceParams isp(true);
657 InsetSpace::string2params(to_utf8(str), isp);
660 odocstringstream ods;
661 otexstream os(ods, texrow);
662 Encoding const * const ascii = encodings.fromLyXName("ascii");
663 OutputParams op(ascii);
665 mathed_parse_cell(ar, ods.str());
666 if (ar.size() == 2) {
668 if (ar[1].nucleus()->asBraceInset())
677 return ar[0].nucleus();
681 bool isAsciiOrMathAlpha(char_type c)
683 return isASCII(c) || Encodings::isMathAlpha(c);