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");
115 FileName const filename = libFileSearch(string(), "symbols");
116 LYXERR(Debug::MATHED, "read symbols from " << filename);
117 if (filename.empty()) {
118 lyxerr << "Could not find symbols file" << endl;
122 ifstream fs(filename.toFilesystemEncoding().c_str());
125 while (getline(fs, line)) {
128 if (line.empty() || line[0] == '#')
131 // special case of iffont/else/endif
132 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
133 istringstream is(line);
137 docstring t = from_utf8(tmp);
138 skip = !isMathFontAvailable(t);
140 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
143 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
149 // special case of pre-defined macros
150 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
151 //lyxerr << "macro definition: '" << line << '\'' << endl;
152 istringstream is(line);
155 is >> macro >> requires;
156 MacroTable::iterator it = MacroTable::globalMacros().insert(
157 0, from_utf8(macro), requires);
158 // If you change the following output, please adjust
159 // development/tools/generate_symbols_images.py.
160 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(it->first)
164 << " requires: " << requires << '\'');
168 idocstringstream is(from_utf8(line));
170 is >> tmp.name >> tmp.inset;
171 if (isFontName(tmp.inset))
172 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
175 // requires is optional
178 // backward compatibility
179 if (tmp.requires == "esintoramsmath")
180 tmp.requires = from_ascii("esint|amsmath");
182 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
183 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' '
184 << to_utf8(tmp.extra));
188 if (isFontName(tmp.inset)) {
189 // tmp.inset _is_ the fontname here.
190 // create fallbacks if necessary
192 // store requirements as long as we can
193 if (tmp.requires.empty()) {
194 if (tmp.inset == "msa" || tmp.inset == "msb")
195 tmp.requires = from_ascii("amssymb");
196 else if (tmp.inset == "wasy")
197 tmp.requires = from_ascii("wasysym");
198 else if (tmp.inset == "mathscr")
199 tmp.requires = from_ascii("mathrsfs");
202 // symbol font is not available sometimes
203 docstring symbol_font = from_ascii("lyxsymbol");
205 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
206 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
208 } else if (isMathFontAvailable(tmp.inset)) {
209 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
210 tmp.draw.push_back(char_type(charid));
211 } else if (fallbackid && isMathFontAvailable(symbol_font)) {
212 if (tmp.inset == "cmex")
213 tmp.inset = from_ascii("lyxsymbol");
215 tmp.inset = from_ascii("lyxboldsymbol");
216 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
217 tmp.draw.push_back(char_type(fallbackid));
219 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
221 tmp.inset = from_ascii("lyxtex");
224 // it's a proper inset
225 LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
226 << " used for " << to_utf8(tmp.name));
229 if (theMathWordList.find(tmp.name) != theMathWordList.end())
230 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
231 << " already exists.");
233 theMathWordList[tmp.name] = tmp;
235 // If you change the following output, please adjust
236 // development/tools/generate_symbols_images.py.
237 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
238 << " inset: " << to_utf8(tmp.inset)
239 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
240 << " extra: " << to_utf8(tmp.extra)
241 << " requires: " << to_utf8(tmp.requires) << '\'');
243 docstring tmp = from_ascii("cmm");
244 docstring tmp2 = from_ascii("cmsy");
245 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
249 bool isSpecialChar(docstring const & name)
251 if (name.size() != 1)
252 return name == "textasciicircum" || name == "mathcircumflex" ||
253 name == "textasciitilde" || name == "textbackslash";
255 char_type const c = name.at(0);
256 return c == '{' || c == '}' || c == '&' || c == '$' ||
257 c == '#' || c == '%' || c == '_';
263 MathWordList const & mathedWordList()
265 return theMathWordList;
271 static bool initialized = false;
280 bool ensureMath(WriteStream & os, bool needs_math_mode, bool macro)
282 bool brace = os.pendingBrace();
283 os.pendingBrace(false);
286 if (os.textMode() && needs_math_mode) {
287 os << "\\ensuremath{";
290 } else if (macro && brace && !needs_math_mode) {
291 // This is a user defined macro, but not a MathMacro, so we
292 // cannot be sure what mode is needed. As it was entered in
293 // a text box, we restore the text mode.
302 int ensureMode(WriteStream & os, InsetMath::mode_type mode,
303 bool locked, bool ascii)
305 bool textmode = mode == InsetMath::TEXT_MODE;
306 if (os.latex() && textmode && os.pendingBrace()) {
308 os.pendingBrace(false);
309 os.pendingSpace(false);
312 int oldmodes = os.textMode() ? 0x01 : 0;
313 os.textMode(textmode);
314 oldmodes |= os.lockedMode() ? 0x02 : 0;
315 os.lockedMode(locked);
316 oldmodes |= os.asciiOnly() ? 0x04 : 0;
322 latexkeys const * in_word_set(docstring const & str)
324 MathWordList::iterator it = theMathWordList.find(str);
325 return it != theMathWordList.end() ? &(it->second) : 0;
329 MathAtom createInsetMath(char const * const s, Buffer * buf)
331 return createInsetMath(from_utf8(s), buf);
335 MathAtom createInsetMath(docstring const & s, Buffer * buf)
337 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
338 if ((s == "ce" || s == "cf") && buf
339 && buf->params().use_package("mhchem") == BufferParams::package_off)
340 return MathAtom(new MathMacro(buf, s));
342 latexkeys const * l = in_word_set(s);
344 docstring const & inset = l->inset;
345 //lyxerr << " found inset: '" << inset << '\'' << endl;
347 return MathAtom(new InsetMathRef(buf, l->name));
348 if (inset == "overset")
349 return MathAtom(new InsetMathOverset(buf));
350 if (inset == "underset")
351 return MathAtom(new InsetMathUnderset(buf));
352 if (inset == "decoration")
353 return MathAtom(new InsetMathDecoration(buf, l));
354 if (inset == "space")
355 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
357 return MathAtom(new InsetMathDots(l));
359 // return MathAtom(new InsetMathMBox);
360 // InsetMathMBox is proposed to replace InsetMathBox,
361 // but is not ready yet (it needs a BufferView for
363 return MathAtom(new InsetMathBox(buf, l->name));
364 // if (inset == "fbox")
365 // return MathAtom(new InsetMathFBox(l));
366 if (inset == "style")
367 return MathAtom(new InsetMathSize(buf, l));
369 return MathAtom(new InsetMathFont(buf, l));
370 if (inset == "oldfont")
371 return MathAtom(new InsetMathFontOld(buf, l));
372 if (inset == "matrix")
373 return MathAtom(new InsetMathAMSArray(buf, s));
374 if (inset == "split")
375 return MathAtom(new InsetMathSplit(buf, s));
377 // we can't create a InsetMathBig, since the argument
379 return MathAtom(new InsetMathUnknown(s));
380 return MathAtom(new InsetMathSymbol(l));
383 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
384 return MathAtom(new MathMacroArgument(s[1] - '0'));
385 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
386 && s[2] >= '1' && s[2] <= '9')
387 return MathAtom(new MathMacroArgument(s[2] - '0'));
389 return MathAtom(new InsetMathBoxed(buf));
391 return MathAtom(new InsetMathFBox(buf));
393 return MathAtom(new InsetMathMakebox(buf, true));
395 return MathAtom(new InsetMathMakebox(buf, false));
397 return MathAtom(new InsetMathKern);
398 if (s.substr(0, 8) == "xymatrix") {
399 char spacing_code = '\0';
401 bool equal_spacing = false;
402 size_t const len = s.length();
404 if (i < len && s[i] == '@') {
406 if (i < len && s[i] == '!') {
407 equal_spacing = true;
414 spacing_code = static_cast<char>(s[i]);
417 } else if (i < len) {
425 spacing_code = static_cast<char>(s[i]);
429 if (i < len && s[i] == '=') {
431 spacing = Length(to_ascii(s.substr(i)));
435 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
440 return MathAtom(new InsetMathDiagram(buf));
441 if (s == "xrightarrow" || s == "xleftarrow")
442 return MathAtom(new InsetMathXArrow(buf, s));
443 if (s == "split" || s == "alignedat")
444 return MathAtom(new InsetMathSplit(buf, s));
446 return MathAtom(new InsetMathCases(buf));
448 return MathAtom(new InsetMathSubstack(buf));
449 if (s == "subarray" || s == "array")
450 return MathAtom(new InsetMathArray(buf, s, 1, 1));
452 return MathAtom(new InsetMathSqrt(buf));
454 return MathAtom(new InsetMathRoot(buf));
456 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
458 return MathAtom(new InsetMathStackrel(buf, false));
459 // This string value is only for math toolbar use, no LaTeX name
460 if (s == "stackrelthree")
461 return MathAtom(new InsetMathStackrel(buf, true));
463 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
465 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
467 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
469 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
471 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
473 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
475 return MathAtom(new InsetMathFrac(buf));
477 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
479 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
481 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
483 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
485 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
487 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
488 // These string values are only for math toolbar use, no LaTeX names
489 if (s == "unitfracthree")
490 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
492 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
494 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
495 if (s == "cfracleft")
496 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
497 if (s == "cfracright")
498 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
500 // return MathAtom(new MathInferInset);
502 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
504 return MathAtom(new InsetMathLefteqn(buf));
505 if (s == "boldsymbol")
506 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
508 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
509 if (s == "heavysymbol" || s == "hm")
510 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
511 if (s == "color" || s == "normalcolor")
512 return MathAtom(new InsetMathColor(buf, true));
513 if (s == "textcolor")
514 return MathAtom(new InsetMathColor(buf, false));
516 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
518 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
520 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
522 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::cancel));
524 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::bcancel));
526 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::xcancel));
528 return MathAtom(new InsetMathCancelto(buf));
530 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smash));
532 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathclap));
534 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathllap));
536 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathrlap));
537 if (s == "ensuremath")
538 return MathAtom(new InsetMathEnsureMath(buf));
540 return MathAtom(new InsetMathSideset(buf, true, true));
541 // The following 3 string values are only for math toolbar use, no LaTeX names
543 return MathAtom(new InsetMathSideset(buf, false, true));
545 return MathAtom(new InsetMathSideset(buf, true, false));
547 return MathAtom(new InsetMathSideset(buf, false, false));
548 if (isSpecialChar(s))
549 return MathAtom(new InsetMathSpecialChar(s));
551 return MathAtom(new InsetMathSpace(" ", ""));
554 return MathAtom(new InsetMathHull(buf, hullRegexp));
556 return MathAtom(new MathMacro(buf, s));
560 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
563 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
565 docstring body = split(str, name, ' ');
568 InsetCommandParams icp(REF_CODE);
570 InsetCommand::string2params(to_utf8(str), icp);
571 Encoding const * const utf8 = encodings.fromLyXName("utf8");
572 OutputParams op(utf8);
573 mathed_parse_cell(ar, icp.getCommand(op));
574 } else if (name == "mathspace") {
575 InsetSpaceParams isp(true);
576 InsetSpace::string2params(to_utf8(str), isp);
579 odocstringstream ods;
580 otexstream os(ods, texrow);
581 Encoding const * const ascii = encodings.fromLyXName("ascii");
582 OutputParams op(ascii);
584 mathed_parse_cell(ar, ods.str());
585 if (ar.size() == 2) {
587 if (ar[1].nucleus()->asBraceInset())
596 return ar[0].nucleus();
600 bool isAsciiOrMathAlpha(char_type c)
602 return isASCII(c) || Encodings::isMathAlpha(c);