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 "InsetMathSize.h"
36 #include "InsetMathSpace.h"
37 #include "InsetMathSpecialChar.h"
38 #include "InsetMathSplit.h"
39 #include "InsetMathSqrt.h"
40 #include "InsetMathStackrel.h"
41 #include "InsetMathSubstack.h"
42 #include "InsetMathSymbol.h"
43 #include "InsetMathTabular.h"
44 #include "InsetMathUnderset.h"
45 #include "InsetMathUnknown.h"
46 #include "InsetMathHull.h"
47 #include "InsetMathXArrow.h"
48 #include "InsetMathXYMatrix.h"
49 #include "InsetMathDiagram.h"
50 #include "MacroTable.h"
51 #include "MathMacro.h"
52 #include "MathMacroArgument.h"
53 #include "MathParser.h"
54 #include "MathStream.h"
55 #include "MathSupport.h"
57 #include "insets/InsetCommand.h"
58 #include "insets/InsetSpace.h"
60 #include "support/debug.h"
61 #include "support/docstream.h"
62 #include "support/FileName.h"
63 #include "support/filetools.h" // LibFileSearch
64 #include "support/lstrings.h"
65 #include "support/textutils.h"
67 #include "frontends/FontLoader.h"
70 #include "BufferParams.h"
72 #include "LyX.h" // use_gui
73 #include "OutputParams.h"
76 using namespace lyx::support;
85 MathWordList theMathWordList;
88 bool isMathFontAvailable(docstring & name)
96 // Do we have the font proper?
97 if (theFontLoader().available(f))
101 if (name == "eufrak") {
102 name = from_ascii("lyxfakefrak");
106 LYXERR(Debug::MATHED,
107 "font " << to_utf8(name) << " not available and I can't fake it");
114 FileName const filename = libFileSearch(string(), "symbols");
115 LYXERR(Debug::MATHED, "read symbols from " << filename);
116 if (filename.empty()) {
117 lyxerr << "Could not find symbols file" << endl;
121 ifstream fs(filename.toFilesystemEncoding().c_str());
124 while (getline(fs, line)) {
127 if (line.empty() || line[0] == '#')
130 // special case of iffont/else/endif
131 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
132 istringstream is(line);
136 docstring t = from_utf8(tmp);
137 skip = !isMathFontAvailable(t);
139 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
142 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
148 // special case of pre-defined macros
149 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
150 //lyxerr << "macro definition: '" << line << '\'' << endl;
151 istringstream is(line);
154 is >> macro >> requires;
155 MacroTable::iterator it = MacroTable::globalMacros().insert(
156 0, from_utf8(macro), requires);
157 // If you change the following output, please adjust
158 // development/tools/generate_symbols_images.py.
159 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(it->first)
163 << " requires: " << requires << '\'');
167 idocstringstream is(from_utf8(line));
169 is >> tmp.name >> tmp.inset;
170 if (isFontName(tmp.inset))
171 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
174 // requires is optional
177 // backward compatibility
178 if (tmp.requires == "esintoramsmath")
179 tmp.requires = from_ascii("esint|amsmath");
181 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
182 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' '
183 << to_utf8(tmp.extra));
187 if (isFontName(tmp.inset)) {
188 // tmp.inset _is_ the fontname here.
189 // create fallbacks if necessary
191 // store requirements as long as we can
192 if (tmp.requires.empty()) {
193 if (tmp.inset == "msa" || tmp.inset == "msb")
194 tmp.requires = from_ascii("amssymb");
195 else if (tmp.inset == "wasy")
196 tmp.requires = from_ascii("wasysym");
197 else if (tmp.inset == "mathscr")
198 tmp.requires = from_ascii("mathrsfs");
201 // symbol font is not available sometimes
202 docstring symbol_font = from_ascii("lyxsymbol");
204 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
205 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
207 } else if (isMathFontAvailable(tmp.inset)) {
208 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
209 tmp.draw.push_back(char_type(charid));
210 } else if (fallbackid && isMathFontAvailable(symbol_font)) {
211 if (tmp.inset == "cmex")
212 tmp.inset = from_ascii("lyxsymbol");
214 tmp.inset = from_ascii("lyxboldsymbol");
215 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
216 tmp.draw.push_back(char_type(fallbackid));
218 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
220 tmp.inset = from_ascii("lyxtex");
223 // it's a proper inset
224 LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
225 << " used for " << to_utf8(tmp.name));
228 if (theMathWordList.find(tmp.name) != theMathWordList.end())
229 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
230 << " already exists.");
232 theMathWordList[tmp.name] = tmp;
234 // If you change the following output, please adjust
235 // development/tools/generate_symbols_images.py.
236 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
237 << " inset: " << to_utf8(tmp.inset)
238 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
239 << " extra: " << to_utf8(tmp.extra)
240 << " requires: " << to_utf8(tmp.requires) << '\'');
242 docstring tmp = from_ascii("cmm");
243 docstring tmp2 = from_ascii("cmsy");
244 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
248 bool isSpecialChar(docstring const & name)
250 if (name.size() != 1)
251 return name == "textasciicircum" || name == "mathcircumflex" ||
252 name == "textasciitilde" || name == "textbackslash";
254 char_type const c = name.at(0);
255 return c == '{' || c == '}' || c == '&' || c == '$' ||
256 c == '#' || c == '%' || c == '_';
262 MathWordList const & mathedWordList()
264 return theMathWordList;
270 static bool initialized = false;
279 bool ensureMath(WriteStream & os, bool needs_math_mode, bool macro)
281 bool brace = os.pendingBrace();
282 os.pendingBrace(false);
285 if (os.textMode() && needs_math_mode) {
286 os << "\\ensuremath{";
289 } else if (macro && brace && !needs_math_mode) {
290 // This is a user defined macro, but not a MathMacro, so we
291 // cannot be sure what mode is needed. As it was entered in
292 // a text box, we restore the text mode.
301 int ensureMode(WriteStream & os, InsetMath::mode_type mode,
302 bool locked, bool ascii)
304 bool textmode = mode == InsetMath::TEXT_MODE;
305 if (os.latex() && textmode && os.pendingBrace()) {
307 os.pendingBrace(false);
308 os.pendingSpace(false);
311 int oldmodes = os.textMode() ? 0x01 : 0;
312 os.textMode(textmode);
313 oldmodes |= os.lockedMode() ? 0x02 : 0;
314 os.lockedMode(locked);
315 oldmodes |= os.asciiOnly() ? 0x04 : 0;
321 latexkeys const * in_word_set(docstring const & str)
323 MathWordList::iterator it = theMathWordList.find(str);
324 return it != theMathWordList.end() ? &(it->second) : 0;
328 MathAtom createInsetMath(char const * const s, Buffer * buf)
330 return createInsetMath(from_utf8(s), buf);
334 MathAtom createInsetMath(docstring const & s, Buffer * buf)
336 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
337 if ((s == "ce" || s == "cf") && buf
338 && buf->params().use_package("mhchem") == BufferParams::package_off)
339 return MathAtom(new MathMacro(buf, s));
341 latexkeys const * l = in_word_set(s);
343 docstring const & inset = l->inset;
344 //lyxerr << " found inset: '" << inset << '\'' << endl;
346 return MathAtom(new InsetMathRef(buf, l->name));
347 if (inset == "overset")
348 return MathAtom(new InsetMathOverset(buf));
349 if (inset == "underset")
350 return MathAtom(new InsetMathUnderset(buf));
351 if (inset == "decoration")
352 return MathAtom(new InsetMathDecoration(buf, l));
353 if (inset == "space")
354 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
356 return MathAtom(new InsetMathDots(l));
358 // return MathAtom(new InsetMathMBox);
359 // InsetMathMBox is proposed to replace InsetMathBox,
360 // but is not ready yet (it needs a BufferView for
362 return MathAtom(new InsetMathBox(buf, l->name));
363 // if (inset == "fbox")
364 // return MathAtom(new InsetMathFBox(l));
365 if (inset == "style")
366 return MathAtom(new InsetMathSize(buf, l));
368 return MathAtom(new InsetMathFont(buf, l));
369 if (inset == "oldfont")
370 return MathAtom(new InsetMathFontOld(buf, l));
371 if (inset == "matrix")
372 return MathAtom(new InsetMathAMSArray(buf, s));
373 if (inset == "split")
374 return MathAtom(new InsetMathSplit(buf, s));
376 // we can't create a InsetMathBig, since the argument
378 return MathAtom(new InsetMathUnknown(s));
379 return MathAtom(new InsetMathSymbol(l));
382 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
383 return MathAtom(new MathMacroArgument(s[1] - '0'));
384 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
385 && s[2] >= '1' && s[2] <= '9')
386 return MathAtom(new MathMacroArgument(s[2] - '0'));
388 return MathAtom(new InsetMathBoxed(buf));
390 return MathAtom(new InsetMathFBox(buf));
392 return MathAtom(new InsetMathMakebox(buf, true));
394 return MathAtom(new InsetMathMakebox(buf, false));
396 return MathAtom(new InsetMathKern);
397 if (s.substr(0, 8) == "xymatrix") {
398 char spacing_code = '\0';
400 bool equal_spacing = false;
401 size_t const len = s.length();
403 if (i < len && s[i] == '@') {
405 if (i < len && s[i] == '!') {
406 equal_spacing = true;
413 spacing_code = static_cast<char>(s[i]);
416 } else if (i < len) {
424 spacing_code = static_cast<char>(s[i]);
428 if (i < len && s[i] == '=') {
430 spacing = Length(to_ascii(s.substr(i)));
434 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
439 return MathAtom(new InsetMathDiagram(buf));
440 if (s == "xrightarrow" || s == "xleftarrow")
441 return MathAtom(new InsetMathXArrow(buf, s));
442 if (s == "split" || s == "alignedat")
443 return MathAtom(new InsetMathSplit(buf, s));
445 return MathAtom(new InsetMathCases(buf));
447 return MathAtom(new InsetMathSubstack(buf));
448 if (s == "subarray" || s == "array")
449 return MathAtom(new InsetMathArray(buf, s, 1, 1));
451 return MathAtom(new InsetMathSqrt(buf));
453 return MathAtom(new InsetMathRoot(buf));
455 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
457 return MathAtom(new InsetMathStackrel(buf, false));
458 // This string value is only for math toolbar use, no LaTeX name
459 if (s == "stackrelthree")
460 return MathAtom(new InsetMathStackrel(buf, true));
462 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
464 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
466 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
468 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
470 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
472 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
474 return MathAtom(new InsetMathFrac(buf));
476 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
478 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
480 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
482 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
484 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
486 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
487 // These string values are only for math toolbar use, no LaTeX names
488 if (s == "unitfracthree")
489 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
491 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
493 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
494 if (s == "cfracleft")
495 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
496 if (s == "cfracright")
497 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
499 // return MathAtom(new MathInferInset);
501 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
503 return MathAtom(new InsetMathLefteqn(buf));
504 if (s == "boldsymbol")
505 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
507 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
508 if (s == "heavysymbol" || s == "hm")
509 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
510 if (s == "color" || s == "normalcolor")
511 return MathAtom(new InsetMathColor(buf, true));
512 if (s == "textcolor")
513 return MathAtom(new InsetMathColor(buf, false));
515 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
517 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
519 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
521 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::cancel));
523 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::bcancel));
525 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::xcancel));
527 return MathAtom(new InsetMathCancelto(buf));
529 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smash));
531 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathclap));
533 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathllap));
535 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathrlap));
536 if (s == "ensuremath")
537 return MathAtom(new InsetMathEnsureMath(buf));
538 if (isSpecialChar(s))
539 return MathAtom(new InsetMathSpecialChar(s));
541 return MathAtom(new InsetMathSpace(" ", ""));
544 return MathAtom(new InsetMathHull(buf, hullRegexp));
546 return MathAtom(new MathMacro(buf, s));
550 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
553 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
555 docstring body = split(str, name, ' ');
558 InsetCommandParams icp(REF_CODE);
560 InsetCommand::string2params(to_utf8(str), icp);
561 Encoding const * const utf8 = encodings.fromLyXName("utf8");
562 OutputParams op(utf8);
563 mathed_parse_cell(ar, icp.getCommand(op));
564 } else if (name == "mathspace") {
565 InsetSpaceParams isp(true);
566 InsetSpace::string2params(to_utf8(str), isp);
569 odocstringstream ods;
570 otexstream os(ods, texrow);
571 Encoding const * const ascii = encodings.fromLyXName("ascii");
572 OutputParams op(ascii);
574 mathed_parse_cell(ar, ods.str());
575 if (ar.size() == 2) {
577 if (ar[1].nucleus()->asBraceInset())
586 return ar[0].nucleus();
590 bool isAsciiOrMathAlpha(char_type c)
592 return isASCII(c) || Encodings::isMathAlpha(c);