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;
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 docstring t = from_utf8(tmp);
167 skip = !isMathFontAvailable(t);
169 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
172 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
178 // special case of pre-defined macros
179 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
180 //lyxerr << "macro definition: '" << line << '\'' << endl;
181 istringstream is(line);
184 is >> macro >> requires;
185 MacroTable::iterator it = MacroTable::globalMacros().insert(
186 0, from_utf8(macro), requires);
187 // If you change the following output, please adjust
188 // development/tools/generate_symbols_images.py.
189 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(it->first)
193 << " requires: " << requires << '\'');
197 idocstringstream is(from_utf8(line));
199 is >> tmp.name >> tmp.inset;
200 if (isFontName(tmp.inset))
201 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
204 // requires is optional
207 // backward compatibility
208 if (tmp.requires == "esintoramsmath")
209 tmp.requires = from_ascii("esint|amsmath");
211 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
212 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' '
213 << to_utf8(tmp.extra));
217 if (isFontName(tmp.inset)) {
218 // tmp.inset _is_ the fontname here.
219 // create fallbacks if necessary
221 // store requirements as long as we can
222 if (tmp.requires.empty()) {
223 if (tmp.inset == "msa" || tmp.inset == "msb")
224 tmp.requires = from_ascii("amssymb");
225 else if (tmp.inset == "wasy")
226 tmp.requires = from_ascii("wasysym");
227 else if (tmp.inset == "mathscr")
228 tmp.requires = from_ascii("mathrsfs");
231 // symbol font is not available sometimes
232 docstring symbol_font = from_ascii("lyxsymbol");
233 char_type unicodesymbol = 0;
235 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
236 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
238 } else if (isMathFontAvailable(tmp.inset) && canBeDisplayed(charid)) {
239 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
240 tmp.draw.push_back(char_type(charid));
241 } else if (fallbackid && isMathFontAvailable(symbol_font) &&
242 canBeDisplayed(fallbackid)) {
243 if (tmp.inset == "cmex")
244 tmp.inset = from_ascii("lyxsymbol");
246 tmp.inset = from_ascii("lyxboldsymbol");
247 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
248 tmp.draw.push_back(char_type(fallbackid));
249 } else if (isUnicodeSymbolAvailable(tmp.name, unicodesymbol)) {
250 LYXERR(Debug::MATHED, "unicode fallback for " << to_utf8(tmp.name));
251 tmp.inset = from_ascii("mathnormal");
252 tmp.draw.push_back(unicodesymbol);
254 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
256 tmp.inset = from_ascii("lyxtex");
259 // it's a proper inset
260 LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
261 << " used for " << to_utf8(tmp.name));
264 if (theMathWordList.find(tmp.name) != theMathWordList.end())
265 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
266 << " already exists.");
268 theMathWordList[tmp.name] = tmp;
270 // If you change the following output, please adjust
271 // development/tools/generate_symbols_images.py.
272 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
273 << " inset: " << to_utf8(tmp.inset)
274 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
275 << " extra: " << to_utf8(tmp.extra)
276 << " requires: " << to_utf8(tmp.requires) << '\'');
278 docstring tmp = from_ascii("cmm");
279 docstring tmp2 = from_ascii("cmsy");
280 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
284 bool isSpecialChar(docstring const & name)
286 if (name.size() != 1)
287 return name == "textasciicircum" || name == "mathcircumflex" ||
288 name == "textasciitilde" || name == "textbackslash";
290 char_type const c = name.at(0);
291 return c == '{' || c == '}' || c == '&' || c == '$' ||
292 c == '#' || c == '%' || c == '_';
298 MathWordList const & mathedWordList()
300 return theMathWordList;
306 static bool initialized = false;
315 bool ensureMath(WriteStream & os, bool needs_math_mode, bool macro)
317 bool brace = os.pendingBrace();
318 os.pendingBrace(false);
321 if (os.textMode() && needs_math_mode) {
322 os << "\\ensuremath{";
325 } else if (macro && brace && !needs_math_mode) {
326 // This is a user defined macro, but not a MathMacro, so we
327 // cannot be sure what mode is needed. As it was entered in
328 // a text box, we restore the text mode.
337 int ensureMode(WriteStream & os, InsetMath::mode_type mode,
338 bool locked, bool ascii)
340 bool textmode = mode == InsetMath::TEXT_MODE;
341 if (os.latex() && textmode && os.pendingBrace()) {
343 os.pendingBrace(false);
344 os.pendingSpace(false);
347 int oldmodes = os.textMode() ? 0x01 : 0;
348 os.textMode(textmode);
349 oldmodes |= os.lockedMode() ? 0x02 : 0;
350 os.lockedMode(locked);
351 oldmodes |= os.asciiOnly() ? 0x04 : 0;
357 latexkeys const * in_word_set(docstring const & str)
359 MathWordList::iterator it = theMathWordList.find(str);
360 return it != theMathWordList.end() ? &(it->second) : 0;
364 MathAtom createInsetMath(char const * const s, Buffer * buf)
366 return createInsetMath(from_utf8(s), buf);
370 MathAtom createInsetMath(docstring const & s, Buffer * buf)
372 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
373 if ((s == "ce" || s == "cf") && buf
374 && buf->params().use_package("mhchem") == BufferParams::package_off)
375 return MathAtom(new MathMacro(buf, s));
377 latexkeys const * l = in_word_set(s);
379 docstring const & inset = l->inset;
380 //lyxerr << " found inset: '" << inset << '\'' << endl;
382 return MathAtom(new InsetMathRef(buf, l->name));
383 if (inset == "overset")
384 return MathAtom(new InsetMathOverset(buf));
385 if (inset == "underset")
386 return MathAtom(new InsetMathUnderset(buf));
387 if (inset == "decoration")
388 return MathAtom(new InsetMathDecoration(buf, l));
389 if (inset == "space")
390 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
392 return MathAtom(new InsetMathDots(l));
394 return MathAtom(new InsetMathBox(buf, l->name));
395 // if (inset == "fbox")
396 // return MathAtom(new InsetMathFBox(l));
397 if (inset == "style")
398 return MathAtom(new InsetMathSize(buf, l));
400 return MathAtom(new InsetMathFont(buf, l));
401 if (inset == "oldfont")
402 return MathAtom(new InsetMathFontOld(buf, l));
403 if (inset == "matrix")
404 return MathAtom(new InsetMathAMSArray(buf, s));
405 if (inset == "split")
406 return MathAtom(new InsetMathSplit(buf, s));
408 // we can't create a InsetMathBig, since the argument
410 return MathAtom(new InsetMathUnknown(s));
411 return MathAtom(new InsetMathSymbol(l));
414 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
415 return MathAtom(new MathMacroArgument(s[1] - '0'));
416 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
417 && s[2] >= '1' && s[2] <= '9')
418 return MathAtom(new MathMacroArgument(s[2] - '0'));
420 return MathAtom(new InsetMathBoxed(buf));
422 return MathAtom(new InsetMathFBox(buf));
424 return MathAtom(new InsetMathMakebox(buf, true));
426 return MathAtom(new InsetMathMakebox(buf, false));
428 return MathAtom(new InsetMathKern);
429 if (s.substr(0, 8) == "xymatrix") {
430 char spacing_code = '\0';
432 bool equal_spacing = false;
433 size_t const len = s.length();
435 if (i < len && s[i] == '@') {
437 if (i < len && s[i] == '!') {
438 equal_spacing = true;
445 spacing_code = static_cast<char>(s[i]);
448 } else if (i < len) {
456 spacing_code = static_cast<char>(s[i]);
460 if (i < len && s[i] == '=') {
462 spacing = Length(to_ascii(s.substr(i)));
466 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
471 return MathAtom(new InsetMathDiagram(buf));
472 if (s == "xrightarrow" || s == "xleftarrow")
473 return MathAtom(new InsetMathXArrow(buf, s));
474 if (s == "split" || s == "alignedat")
475 return MathAtom(new InsetMathSplit(buf, s));
477 return MathAtom(new InsetMathCases(buf));
479 return MathAtom(new InsetMathSubstack(buf));
480 if (s == "subarray" || s == "array")
481 return MathAtom(new InsetMathArray(buf, s, 1, 1));
483 return MathAtom(new InsetMathSqrt(buf));
485 return MathAtom(new InsetMathRoot(buf));
487 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
489 return MathAtom(new InsetMathStackrel(buf, false));
490 // This string value is only for math toolbar use, no LaTeX name
491 if (s == "stackrelthree")
492 return MathAtom(new InsetMathStackrel(buf, true));
494 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
496 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
498 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
500 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
502 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
504 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
506 return MathAtom(new InsetMathFrac(buf));
508 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
510 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
512 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
514 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
516 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
518 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
519 // These string values are only for math toolbar use, no LaTeX names
520 if (s == "unitfracthree")
521 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
523 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
525 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
526 if (s == "cfracleft")
527 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
528 if (s == "cfracright")
529 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
531 // return MathAtom(new MathInferInset);
533 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
535 return MathAtom(new InsetMathLefteqn(buf));
536 if (s == "boldsymbol")
537 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
539 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
540 if (s == "heavysymbol" || s == "hm")
541 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
542 if (s == "color" || s == "normalcolor")
543 return MathAtom(new InsetMathColor(buf, true));
544 if (s == "textcolor")
545 return MathAtom(new InsetMathColor(buf, false));
547 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
549 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
551 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
553 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::cancel));
555 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::bcancel));
557 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::xcancel));
559 return MathAtom(new InsetMathCancelto(buf));
561 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smash));
563 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathclap));
565 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathllap));
567 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathrlap));
568 if (s == "ensuremath")
569 return MathAtom(new InsetMathEnsureMath(buf));
571 return MathAtom(new InsetMathSideset(buf, true, true));
572 // The following 3 string values are only for math toolbar use, no LaTeX names
574 return MathAtom(new InsetMathSideset(buf, false, true));
576 return MathAtom(new InsetMathSideset(buf, true, false));
578 return MathAtom(new InsetMathSideset(buf, false, false));
579 if (isSpecialChar(s))
580 return MathAtom(new InsetMathSpecialChar(s));
582 return MathAtom(new InsetMathSpace(" ", ""));
585 return MathAtom(new InsetMathHull(buf, hullRegexp));
587 return MathAtom(new MathMacro(buf, s));
591 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
594 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
596 docstring body = split(str, name, ' ');
599 InsetCommandParams icp(REF_CODE);
601 InsetCommand::string2params(to_utf8(str), icp);
602 Encoding const * const utf8 = encodings.fromLyXName("utf8");
603 OutputParams op(utf8);
604 mathed_parse_cell(ar, icp.getCommand(op));
605 } else if (name == "mathspace") {
606 InsetSpaceParams isp(true);
607 InsetSpace::string2params(to_utf8(str), isp);
610 odocstringstream ods;
611 otexstream os(ods, texrow);
612 Encoding const * const ascii = encodings.fromLyXName("ascii");
613 OutputParams op(ascii);
615 mathed_parse_cell(ar, ods.str());
616 if (ar.size() == 2) {
618 if (ar[1].nucleus()->asBraceInset())
627 return ar[0].nucleus();
631 bool isAsciiOrMathAlpha(char_type c)
633 return isASCII(c) || Encodings::isMathAlpha(c);