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 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 docstring t = from_utf8(tmp);
164 skip = !isMathFontAvailable(t);
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;
178 istringstream is(line);
181 is >> macro >> requires;
182 MacroTable::iterator it = MacroTable::globalMacros().insert(
183 0, from_utf8(macro), requires);
184 // If you change the following output, please adjust
185 // development/tools/generate_symbols_images.py.
186 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(it->first)
190 << " requires: " << requires << '\'');
194 idocstringstream is(from_utf8(line));
196 is >> tmp.name >> tmp.inset;
197 if (isFontName(tmp.inset))
198 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
201 // requires is optional
204 // backward compatibility
205 if (tmp.requires == "esintoramsmath")
206 tmp.requires = from_ascii("esint|amsmath");
208 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
209 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' '
210 << to_utf8(tmp.extra));
214 if (isFontName(tmp.inset)) {
215 // tmp.inset _is_ the fontname here.
216 // create fallbacks if necessary
218 // store requirements as long as we can
219 if (tmp.requires.empty()) {
220 if (tmp.inset == "msa" || tmp.inset == "msb")
221 tmp.requires = from_ascii("amssymb");
222 else if (tmp.inset == "wasy")
223 tmp.requires = from_ascii("wasysym");
224 else if (tmp.inset == "mathscr")
225 tmp.requires = from_ascii("mathrsfs");
228 // symbol font is not available sometimes
229 docstring symbol_font = from_ascii("lyxsymbol");
230 char_type unicodesymbol = 0;
232 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
233 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
235 } else if (isMathFontAvailable(tmp.inset) && canBeDisplayed(charid)) {
236 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
237 tmp.draw.push_back(char_type(charid));
238 } else if (fallbackid && isMathFontAvailable(symbol_font) &&
239 canBeDisplayed(fallbackid)) {
240 if (tmp.inset == "cmex")
241 tmp.inset = from_ascii("lyxsymbol");
243 tmp.inset = from_ascii("lyxboldsymbol");
244 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
245 tmp.draw.push_back(char_type(fallbackid));
246 } else if (isUnicodeSymbolAvailable(tmp.name, unicodesymbol)) {
247 LYXERR(Debug::MATHED, "unicode fallback for " << to_utf8(tmp.name));
248 tmp.inset = from_ascii("mathnormal");
249 tmp.draw.push_back(unicodesymbol);
251 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
253 tmp.inset = from_ascii("lyxtex");
256 // it's a proper inset
257 LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
258 << " used for " << to_utf8(tmp.name));
261 if (theMathWordList.find(tmp.name) != theMathWordList.end())
262 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
263 << " already exists.");
265 theMathWordList[tmp.name] = tmp;
267 // If you change the following output, please adjust
268 // development/tools/generate_symbols_images.py.
269 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
270 << " inset: " << to_utf8(tmp.inset)
271 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
272 << " extra: " << to_utf8(tmp.extra)
273 << " requires: " << to_utf8(tmp.requires) << '\'');
275 docstring tmp = from_ascii("cmm");
276 docstring tmp2 = from_ascii("cmsy");
277 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
281 bool isSpecialChar(docstring const & name)
283 if (name.size() != 1)
284 return name == "textasciicircum" || name == "mathcircumflex" ||
285 name == "textasciitilde" || name == "textbackslash";
287 char_type const c = name.at(0);
288 return c == '{' || c == '}' || c == '&' || c == '$' ||
289 c == '#' || c == '%' || c == '_';
295 MathWordList const & mathedWordList()
297 return theMathWordList;
303 static bool initialized = false;
312 bool ensureMath(WriteStream & os, bool needs_math_mode, bool macro)
314 bool brace = os.pendingBrace();
315 os.pendingBrace(false);
318 if (os.textMode() && needs_math_mode) {
319 os << "\\ensuremath{";
322 } else if (macro && brace && !needs_math_mode) {
323 // This is a user defined macro, but not a MathMacro, so we
324 // cannot be sure what mode is needed. As it was entered in
325 // a text box, we restore the text mode.
334 int ensureMode(WriteStream & os, InsetMath::mode_type mode,
335 bool locked, bool ascii)
337 bool textmode = mode == InsetMath::TEXT_MODE;
338 if (os.latex() && textmode && os.pendingBrace()) {
340 os.pendingBrace(false);
341 os.pendingSpace(false);
344 int oldmodes = os.textMode() ? 0x01 : 0;
345 os.textMode(textmode);
346 oldmodes |= os.lockedMode() ? 0x02 : 0;
347 os.lockedMode(locked);
348 oldmodes |= os.asciiOnly() ? 0x04 : 0;
354 latexkeys const * in_word_set(docstring const & str)
356 MathWordList::iterator it = theMathWordList.find(str);
357 return it != theMathWordList.end() ? &(it->second) : 0;
361 MathAtom createInsetMath(char const * const s, Buffer * buf)
363 return createInsetMath(from_utf8(s), buf);
367 MathAtom createInsetMath(docstring const & s, Buffer * buf)
369 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
370 if ((s == "ce" || s == "cf") && buf
371 && buf->params().use_package("mhchem") == BufferParams::package_off)
372 return MathAtom(new MathMacro(buf, s));
374 latexkeys const * l = in_word_set(s);
376 docstring const & inset = l->inset;
377 //lyxerr << " found inset: '" << inset << '\'' << endl;
379 return MathAtom(new InsetMathRef(buf, l->name));
380 if (inset == "overset")
381 return MathAtom(new InsetMathOverset(buf));
382 if (inset == "underset")
383 return MathAtom(new InsetMathUnderset(buf));
384 if (inset == "decoration")
385 return MathAtom(new InsetMathDecoration(buf, l));
386 if (inset == "space")
387 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
389 return MathAtom(new InsetMathDots(l));
391 return MathAtom(new InsetMathBox(buf, l->name));
392 // if (inset == "fbox")
393 // return MathAtom(new InsetMathFBox(l));
394 if (inset == "style")
395 return MathAtom(new InsetMathSize(buf, l));
397 return MathAtom(new InsetMathFont(buf, l));
398 if (inset == "oldfont")
399 return MathAtom(new InsetMathFontOld(buf, l));
400 if (inset == "matrix")
401 return MathAtom(new InsetMathAMSArray(buf, s));
402 if (inset == "split")
403 return MathAtom(new InsetMathSplit(buf, s));
405 // we can't create a InsetMathBig, since the argument
407 return MathAtom(new InsetMathUnknown(s));
408 return MathAtom(new InsetMathSymbol(l));
411 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
412 return MathAtom(new MathMacroArgument(s[1] - '0'));
413 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
414 && s[2] >= '1' && s[2] <= '9')
415 return MathAtom(new MathMacroArgument(s[2] - '0'));
417 return MathAtom(new InsetMathBoxed(buf));
419 return MathAtom(new InsetMathFBox(buf));
421 return MathAtom(new InsetMathMakebox(buf, true));
423 return MathAtom(new InsetMathMakebox(buf, false));
425 return MathAtom(new InsetMathKern);
426 if (s.substr(0, 8) == "xymatrix") {
427 char spacing_code = '\0';
429 bool equal_spacing = false;
430 size_t const len = s.length();
432 if (i < len && s[i] == '@') {
434 if (i < len && s[i] == '!') {
435 equal_spacing = true;
442 spacing_code = static_cast<char>(s[i]);
445 } else if (i < len) {
453 spacing_code = static_cast<char>(s[i]);
457 if (i < len && s[i] == '=') {
459 spacing = Length(to_ascii(s.substr(i)));
463 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
468 return MathAtom(new InsetMathDiagram(buf));
469 if (s == "xrightarrow" || s == "xleftarrow")
470 return MathAtom(new InsetMathXArrow(buf, s));
471 if (s == "split" || s == "alignedat")
472 return MathAtom(new InsetMathSplit(buf, s));
474 return MathAtom(new InsetMathCases(buf));
476 return MathAtom(new InsetMathSubstack(buf));
477 if (s == "subarray" || s == "array")
478 return MathAtom(new InsetMathArray(buf, s, 1, 1));
480 return MathAtom(new InsetMathSqrt(buf));
482 return MathAtom(new InsetMathRoot(buf));
484 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
486 return MathAtom(new InsetMathStackrel(buf, false));
487 // This string value is only for math toolbar use, no LaTeX name
488 if (s == "stackrelthree")
489 return MathAtom(new InsetMathStackrel(buf, true));
491 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
493 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
495 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
497 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
499 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
501 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
503 return MathAtom(new InsetMathFrac(buf));
505 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
507 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
509 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
511 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
513 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
515 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
516 // These string values are only for math toolbar use, no LaTeX names
517 if (s == "unitfracthree")
518 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
520 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
522 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
523 if (s == "cfracleft")
524 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
525 if (s == "cfracright")
526 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
528 // return MathAtom(new MathInferInset);
530 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
532 return MathAtom(new InsetMathLefteqn(buf));
533 if (s == "boldsymbol")
534 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
536 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
537 if (s == "heavysymbol" || s == "hm")
538 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
539 if (s == "color" || s == "normalcolor")
540 return MathAtom(new InsetMathColor(buf, true));
541 if (s == "textcolor")
542 return MathAtom(new InsetMathColor(buf, false));
544 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
546 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
548 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
550 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::cancel));
552 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::bcancel));
554 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::xcancel));
556 return MathAtom(new InsetMathCancelto(buf));
558 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smash));
559 // The following 2 string values are only for math toolbar use, no LaTeX names
561 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smashb));
563 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smasht));
565 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathclap));
567 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathllap));
569 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathrlap));
570 if (s == "ensuremath")
571 return MathAtom(new InsetMathEnsureMath(buf));
573 return MathAtom(new InsetMathSideset(buf, true, true));
574 // The following 3 string values are only for math toolbar use, no LaTeX names
576 return MathAtom(new InsetMathSideset(buf, false, true));
578 return MathAtom(new InsetMathSideset(buf, true, false));
580 return MathAtom(new InsetMathSideset(buf, false, false));
581 if (isSpecialChar(s))
582 return MathAtom(new InsetMathSpecialChar(s));
584 return MathAtom(new InsetMathSpace(" ", ""));
587 return MathAtom(new InsetMathHull(buf, hullRegexp));
589 return MathAtom(new MathMacro(buf, s));
593 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
596 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
598 docstring body = split(str, name, ' ');
601 InsetCommandParams icp(REF_CODE);
603 InsetCommand::string2params(to_utf8(str), icp);
604 Encoding const * const utf8 = encodings.fromLyXName("utf8");
605 OutputParams op(utf8);
606 mathed_parse_cell(ar, icp.getCommand(op));
607 } else if (name == "mathspace") {
608 InsetSpaceParams isp(true);
609 InsetSpace::string2params(to_utf8(str), isp);
612 odocstringstream ods;
613 otexstream os(ods, texrow);
614 Encoding const * const ascii = encodings.fromLyXName("ascii");
615 OutputParams op(ascii);
617 mathed_parse_cell(ar, ods.str());
618 if (ar.size() == 2) {
620 if (ar[1].nucleus()->asBraceInset())
629 return ar[0].nucleus();
633 bool isAsciiOrMathAlpha(char_type c)
635 return isASCII(c) || Encodings::isMathAlpha(c);