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(docstring & name)
97 // Do we have the font proper?
98 if (theFontLoader().available(f))
102 if (name == "eufrak") {
103 name = from_ascii("lyxfakefrak");
107 LYXERR(Debug::MATHED,
108 "font " << to_utf8(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 // this is too fragile, e.g. prodes W instead of capital omega on OS X
125 docstring cmd(from_ascii("\\") + name);
128 c = Encodings::fromLaTeXCommand(cmd, Encodings::MATH_CMD,
129 is_combining, termination);
130 if (c == 0 && name == "varOmega") {
131 // fallback for bug 7954, unicodesymbols does not list
132 // \\varOmega because of requirements, but this might change
133 cmd = from_ascii("\\mathit{\\Omega}");
134 c = Encodings::fromLaTeXCommand(cmd, Encodings::MATH_CMD,
135 is_combining, termination);
137 return c != 0 && !is_combining;
146 FileName const filename = libFileSearch(string(), "symbols");
147 LYXERR(Debug::MATHED, "read symbols from " << filename);
148 if (filename.empty()) {
149 lyxerr << "Could not find symbols file" << endl;
153 ifstream fs(filename.toFilesystemEncoding().c_str());
156 while (getline(fs, line)) {
159 if (line.empty() || line[0] == '#')
162 // special case of iffont/else/endif
163 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
164 istringstream is(line);
168 docstring t = from_utf8(tmp);
169 skip = !isMathFontAvailable(t);
171 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
174 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
180 // special case of pre-defined macros
181 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
182 //lyxerr << "macro definition: '" << line << '\'' << endl;
183 istringstream is(line);
186 is >> macro >> requires;
187 MacroTable::iterator it = MacroTable::globalMacros().insert(
188 0, from_utf8(macro), requires);
189 // If you change the following output, please adjust
190 // development/tools/generate_symbols_images.py.
191 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(it->first)
195 << " requires: " << requires << '\'');
199 idocstringstream is(from_utf8(line));
201 is >> tmp.name >> tmp.inset;
202 if (isFontName(tmp.inset))
203 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
206 // requires is optional
209 // backward compatibility
210 if (tmp.requires == "esintoramsmath")
211 tmp.requires = from_ascii("esint|amsmath");
213 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
214 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' '
215 << to_utf8(tmp.extra));
219 if (isFontName(tmp.inset)) {
220 // tmp.inset _is_ the fontname here.
221 // create fallbacks if necessary
223 // store requirements as long as we can
224 if (tmp.requires.empty()) {
225 if (tmp.inset == "msa" || tmp.inset == "msb")
226 tmp.requires = from_ascii("amssymb");
227 else if (tmp.inset == "wasy")
228 tmp.requires = from_ascii("wasysym");
229 else if (tmp.inset == "mathscr")
230 tmp.requires = from_ascii("mathrsfs");
233 // symbol font is not available sometimes
234 docstring symbol_font = from_ascii("lyxsymbol");
235 char_type unicodesymbol = 0;
237 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
238 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
240 } else if (isMathFontAvailable(tmp.inset) && canBeDisplayed(charid)) {
241 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
242 tmp.draw.push_back(char_type(charid));
243 } else if (fallbackid && isMathFontAvailable(symbol_font) &&
244 canBeDisplayed(fallbackid)) {
245 if (tmp.inset == "cmex")
246 tmp.inset = from_ascii("lyxsymbol");
248 tmp.inset = from_ascii("lyxboldsymbol");
249 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
250 tmp.draw.push_back(char_type(fallbackid));
251 } else if (isUnicodeSymbolAvailable(tmp.name, unicodesymbol)) {
252 LYXERR(Debug::MATHED, "unicode fallback for " << to_utf8(tmp.name));
253 tmp.inset = from_ascii("mathnormal");
254 tmp.draw.push_back(unicodesymbol);
256 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
258 tmp.inset = from_ascii("lyxtex");
261 // it's a proper inset
262 LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
263 << " used for " << to_utf8(tmp.name));
266 if (theMathWordList.find(tmp.name) != theMathWordList.end())
267 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
268 << " already exists.");
270 theMathWordList[tmp.name] = tmp;
272 // If you change the following output, please adjust
273 // development/tools/generate_symbols_images.py.
274 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
275 << " inset: " << to_utf8(tmp.inset)
276 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
277 << " extra: " << to_utf8(tmp.extra)
278 << " requires: " << to_utf8(tmp.requires) << '\'');
280 docstring tmp = from_ascii("cmm");
281 docstring tmp2 = from_ascii("cmsy");
282 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
286 bool isSpecialChar(docstring const & name)
288 if (name.size() != 1)
289 return name == "textasciicircum" || name == "mathcircumflex" ||
290 name == "textasciitilde" || name == "textbackslash";
292 char_type const c = name.at(0);
293 return c == '{' || c == '}' || c == '&' || c == '$' ||
294 c == '#' || c == '%' || c == '_';
300 MathWordList const & mathedWordList()
302 return theMathWordList;
308 static bool initialized = false;
317 bool ensureMath(WriteStream & os, bool needs_math_mode, bool macro)
319 bool brace = os.pendingBrace();
320 os.pendingBrace(false);
323 if (os.textMode() && needs_math_mode) {
324 os << "\\ensuremath{";
327 } else if (macro && brace && !needs_math_mode) {
328 // This is a user defined macro, but not a MathMacro, so we
329 // cannot be sure what mode is needed. As it was entered in
330 // a text box, we restore the text mode.
339 int ensureMode(WriteStream & os, InsetMath::mode_type mode,
340 bool locked, bool ascii)
342 bool textmode = mode == InsetMath::TEXT_MODE;
343 if (os.latex() && textmode && os.pendingBrace()) {
345 os.pendingBrace(false);
346 os.pendingSpace(false);
349 int oldmodes = os.textMode() ? 0x01 : 0;
350 os.textMode(textmode);
351 oldmodes |= os.lockedMode() ? 0x02 : 0;
352 os.lockedMode(locked);
353 oldmodes |= os.asciiOnly() ? 0x04 : 0;
359 latexkeys const * in_word_set(docstring const & str)
361 MathWordList::iterator it = theMathWordList.find(str);
362 return it != theMathWordList.end() ? &(it->second) : 0;
366 MathAtom createInsetMath(char const * const s, Buffer * buf)
368 return createInsetMath(from_utf8(s), buf);
372 MathAtom createInsetMath(docstring const & s, Buffer * buf)
374 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
375 if ((s == "ce" || s == "cf") && buf
376 && buf->params().use_package("mhchem") == BufferParams::package_off)
377 return MathAtom(new MathMacro(buf, s));
379 latexkeys const * l = in_word_set(s);
381 docstring const & inset = l->inset;
382 //lyxerr << " found inset: '" << inset << '\'' << endl;
384 return MathAtom(new InsetMathRef(buf, l->name));
385 if (inset == "overset")
386 return MathAtom(new InsetMathOverset(buf));
387 if (inset == "underset")
388 return MathAtom(new InsetMathUnderset(buf));
389 if (inset == "decoration")
390 return MathAtom(new InsetMathDecoration(buf, l));
391 if (inset == "space")
392 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
394 return MathAtom(new InsetMathDots(l));
396 return MathAtom(new InsetMathBox(buf, l->name));
397 // if (inset == "fbox")
398 // return MathAtom(new InsetMathFBox(l));
399 if (inset == "style")
400 return MathAtom(new InsetMathSize(buf, l));
402 return MathAtom(new InsetMathFont(buf, l));
403 if (inset == "oldfont")
404 return MathAtom(new InsetMathFontOld(buf, l));
405 if (inset == "matrix")
406 return MathAtom(new InsetMathAMSArray(buf, s));
407 if (inset == "split")
408 return MathAtom(new InsetMathSplit(buf, s));
410 // we can't create a InsetMathBig, since the argument
412 return MathAtom(new InsetMathUnknown(s));
413 return MathAtom(new InsetMathSymbol(l));
416 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
417 return MathAtom(new MathMacroArgument(s[1] - '0'));
418 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
419 && s[2] >= '1' && s[2] <= '9')
420 return MathAtom(new MathMacroArgument(s[2] - '0'));
422 return MathAtom(new InsetMathBoxed(buf));
424 return MathAtom(new InsetMathFBox(buf));
426 return MathAtom(new InsetMathMakebox(buf, true));
428 return MathAtom(new InsetMathMakebox(buf, false));
430 return MathAtom(new InsetMathKern);
431 if (s.substr(0, 8) == "xymatrix") {
432 char spacing_code = '\0';
434 bool equal_spacing = false;
435 size_t const len = s.length();
437 if (i < len && s[i] == '@') {
439 if (i < len && s[i] == '!') {
440 equal_spacing = true;
447 spacing_code = static_cast<char>(s[i]);
450 } else if (i < len) {
458 spacing_code = static_cast<char>(s[i]);
462 if (i < len && s[i] == '=') {
464 spacing = Length(to_ascii(s.substr(i)));
468 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
473 return MathAtom(new InsetMathDiagram(buf));
474 if (s == "xrightarrow" || s == "xleftarrow")
475 return MathAtom(new InsetMathXArrow(buf, s));
476 if (s == "split" || s == "alignedat")
477 return MathAtom(new InsetMathSplit(buf, s));
479 return MathAtom(new InsetMathCases(buf));
481 return MathAtom(new InsetMathSubstack(buf));
482 if (s == "subarray" || s == "array")
483 return MathAtom(new InsetMathArray(buf, s, 1, 1));
485 return MathAtom(new InsetMathSqrt(buf));
487 return MathAtom(new InsetMathRoot(buf));
489 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
491 return MathAtom(new InsetMathStackrel(buf, false));
492 // This string value is only for math toolbar use, no LaTeX name
493 if (s == "stackrelthree")
494 return MathAtom(new InsetMathStackrel(buf, true));
496 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
498 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
500 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
502 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
504 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
506 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
508 return MathAtom(new InsetMathFrac(buf));
510 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
512 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
514 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
516 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
518 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
520 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
521 // These string values are only for math toolbar use, no LaTeX names
522 if (s == "unitfracthree")
523 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
525 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
527 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
528 if (s == "cfracleft")
529 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
530 if (s == "cfracright")
531 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
533 // return MathAtom(new MathInferInset);
535 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
537 return MathAtom(new InsetMathLefteqn(buf));
538 if (s == "boldsymbol")
539 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
541 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
542 if (s == "heavysymbol" || s == "hm")
543 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
544 if (s == "color" || s == "normalcolor")
545 return MathAtom(new InsetMathColor(buf, true));
546 if (s == "textcolor")
547 return MathAtom(new InsetMathColor(buf, false));
549 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
551 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
553 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
555 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::cancel));
557 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::bcancel));
559 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::xcancel));
561 return MathAtom(new InsetMathCancelto(buf));
563 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smash));
564 // The following 2 string values are only for math toolbar use, no LaTeX names
566 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smashb));
568 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smasht));
570 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathclap));
572 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathllap));
574 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathrlap));
575 if (s == "ensuremath")
576 return MathAtom(new InsetMathEnsureMath(buf));
578 return MathAtom(new InsetMathSideset(buf, true, true));
579 // The following 3 string values are only for math toolbar use, no LaTeX names
581 return MathAtom(new InsetMathSideset(buf, false, true));
583 return MathAtom(new InsetMathSideset(buf, true, false));
585 return MathAtom(new InsetMathSideset(buf, false, false));
586 if (isSpecialChar(s))
587 return MathAtom(new InsetMathSpecialChar(s));
589 return MathAtom(new InsetMathSpace(" ", ""));
592 return MathAtom(new InsetMathHull(buf, hullRegexp));
594 return MathAtom(new MathMacro(buf, s));
598 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
601 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
603 docstring body = split(str, name, ' ');
606 InsetCommandParams icp(REF_CODE);
608 InsetCommand::string2params(to_utf8(str), icp);
609 Encoding const * const utf8 = encodings.fromLyXName("utf8");
610 OutputParams op(utf8);
611 mathed_parse_cell(ar, icp.getCommand(op));
612 } else if (name == "mathspace") {
613 InsetSpaceParams isp(true);
614 InsetSpace::string2params(to_utf8(str), isp);
617 odocstringstream ods;
618 otexstream os(ods, texrow);
619 Encoding const * const ascii = encodings.fromLyXName("ascii");
620 OutputParams op(ascii);
622 mathed_parse_cell(ar, ods.str());
623 if (ar.size() == 2) {
625 if (ar[1].nucleus()->asBraceInset())
634 return ar[0].nucleus();
638 bool isAsciiOrMathAlpha(char_type c)
640 return isASCII(c) || Encodings::isMathAlpha(c);