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 "InsetMathCases.h"
20 #include "InsetMathColor.h"
21 #include "InsetMathDecoration.h"
22 #include "InsetMathDots.h"
23 #include "InsetMathEnsureMath.h"
24 #include "InsetMathFont.h"
25 #include "InsetMathFontOld.h"
26 #include "InsetMathFrac.h"
27 #include "InsetMathKern.h"
28 #include "InsetMathLefteqn.h"
29 #include "InsetMathOverset.h"
30 #include "InsetMathPhantom.h"
31 #include "InsetMathRef.h"
32 #include "InsetMathRoot.h"
33 #include "InsetMathSize.h"
34 #include "InsetMathSpace.h"
35 #include "InsetMathSpecialChar.h"
36 #include "InsetMathSplit.h"
37 #include "InsetMathSqrt.h"
38 #include "InsetMathStackrel.h"
39 #include "InsetMathSubstack.h"
40 #include "InsetMathSymbol.h"
41 #include "InsetMathTabular.h"
42 #include "InsetMathUnderset.h"
43 #include "InsetMathUnknown.h"
44 #include "InsetMathHull.h"
45 #include "InsetMathXArrow.h"
46 #include "InsetMathXYMatrix.h"
47 #include "MacroTable.h"
48 #include "MathMacro.h"
49 #include "MathMacroArgument.h"
50 #include "MathParser.h"
51 #include "MathStream.h"
52 #include "MathSupport.h"
54 #include "insets/InsetCommand.h"
55 #include "insets/InsetSpace.h"
57 #include "support/debug.h"
58 #include "support/docstream.h"
59 #include "support/FileName.h"
60 #include "support/filetools.h" // LibFileSearch
61 #include "support/lstrings.h"
63 #include "frontends/FontLoader.h"
66 #include "BufferParams.h"
68 #include "LyX.h" // use_gui
69 #include "OutputParams.h"
72 using namespace lyx::support;
81 MathWordList theWordList;
84 bool isMathFontAvailable(docstring & name)
92 // Do we have the font proper?
93 if (theFontLoader().available(f))
97 if (name == "eufrak") {
98 name = from_ascii("lyxfakefrak");
102 LYXERR(Debug::MATHED,
103 "font " << to_utf8(name) << " not available and I can't fake it");
110 FileName const filename = libFileSearch(string(), "symbols");
111 LYXERR(Debug::MATHED, "read symbols from " << filename);
112 if (filename.empty()) {
113 lyxerr << "Could not find symbols file" << endl;
117 ifstream fs(filename.toFilesystemEncoding().c_str());
120 while (getline(fs, line)) {
123 if (line.empty() || line[0] == '#')
126 // special case of iffont/else/endif
127 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
128 istringstream is(line);
132 docstring t = from_utf8(tmp);
133 skip = !isMathFontAvailable(t);
135 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
137 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
143 // special case of pre-defined macros
144 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
145 //lyxerr << "macro definition: '" << line << '\'' << endl;
146 istringstream is(line);
149 is >> macro >> requires;
150 MacroTable::globalMacros().insert(0, from_utf8(macro), requires);
154 idocstringstream is(from_utf8(line));
156 is >> tmp.name >> tmp.inset;
157 if (isFontName(tmp.inset))
158 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
161 // requires is optional
165 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
166 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' '
167 << to_utf8(tmp.extra));
171 if (isFontName(tmp.inset)) {
172 // tmp.inset _is_ the fontname here.
173 // create fallbacks if necessary
175 // store requirements as long as we can
176 if (tmp.requires.empty()) {
177 if (tmp.inset == "msa" || tmp.inset == "msb")
178 tmp.requires = from_ascii("amssymb");
179 else if (tmp.inset == "wasy")
180 tmp.requires = from_ascii("wasysym");
183 // symbol font is not available sometimes
184 docstring symbol_font = from_ascii("lyxsymbol");
186 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
187 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
189 } else if (isMathFontAvailable(tmp.inset)) {
190 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
191 tmp.draw.push_back(char_type(charid));
192 } else if (fallbackid && isMathFontAvailable(symbol_font)) {
193 if (tmp.inset == "cmex")
194 tmp.inset = from_ascii("lyxsymbol");
196 tmp.inset = from_ascii("lyxboldsymbol");
197 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
198 tmp.draw.push_back(char_type(fallbackid));
200 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
202 tmp.inset = from_ascii("lyxtex");
205 // it's a proper inset
206 LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
207 << " used for " << to_utf8(tmp.name));
210 if (theWordList.find(tmp.name) != theWordList.end())
211 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
212 << " already exists.");
214 theWordList[tmp.name] = tmp;
216 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
217 << " inset: " << to_utf8(tmp.inset)
218 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
219 << " extra: " << to_utf8(tmp.extra)
220 << " requires: " << to_utf8(tmp.requires) << '\'');
222 docstring tmp = from_ascii("cmm");
223 docstring tmp2 = from_ascii("cmsy");
224 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
228 bool isSpecialChar(docstring const & name)
230 if (name.size() != 1)
231 return name == "textasciicircum" || name == "mathcircumflex" ||
232 name == "textasciitilde" || name == "textbackslash";
234 char_type const c = name.at(0);
235 return c == '{' || c == '}' || c == '&' || c == '$' ||
236 c == '#' || c == '%' || c == '_' || c == ' ';
242 MathWordList const & mathedWordList()
250 static bool initialized = false;
259 bool ensureMath(WriteStream & os, bool needs_math_mode, bool macro)
261 bool brace = os.pendingBrace();
262 os.pendingBrace(false);
265 if (os.textMode() && needs_math_mode) {
266 os << "\\ensuremath{";
269 } else if (macro && brace && !needs_math_mode) {
270 // This is a user defined macro, but not a MathMacro, so we
271 // cannot be sure what mode is needed. As it was entered in
272 // a text box, we restore the text mode.
281 int ensureMode(WriteStream & os, InsetMath::mode_type mode, bool locked)
283 bool textmode = mode == InsetMath::TEXT_MODE;
284 if (os.latex() && textmode && os.pendingBrace()) {
286 os.pendingBrace(false);
287 os.pendingSpace(false);
290 int oldmodes = os.textMode() ? 1 : 0;
291 os.textMode(textmode);
292 oldmodes |= os.lockedMode() ? 2 : 0;
293 os.lockedMode(locked);
298 latexkeys const * in_word_set(docstring const & str)
300 MathWordList::iterator it = theWordList.find(str);
301 return it != theWordList.end() ? &(it->second) : 0;
305 MathAtom createInsetMath(char const * const s, Buffer * buf)
307 return createInsetMath(from_utf8(s), buf);
311 MathAtom createInsetMath(docstring const & s, Buffer * buf)
313 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
314 if ((s == "ce" || s == "cf") && buf
315 && buf->params().use_mhchem == BufferParams::package_off)
316 return MathAtom(new MathMacro(buf, s));
318 latexkeys const * l = in_word_set(s);
320 docstring const & inset = l->inset;
321 //lyxerr << " found inset: '" << inset << '\'' << endl;
323 return MathAtom(new InsetMathRef(buf, l->name));
324 if (inset == "overset")
325 return MathAtom(new InsetMathOverset(buf));
326 if (inset == "underset")
327 return MathAtom(new InsetMathUnderset(buf));
328 if (inset == "decoration")
329 return MathAtom(new InsetMathDecoration(buf, l));
330 if (inset == "space")
331 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
333 return MathAtom(new InsetMathDots(l));
335 // return MathAtom(new InsetMathMBox);
336 // InsetMathMBox is proposed to replace InsetMathBox,
337 // but is not ready yet (it needs a BufferView for
339 return MathAtom(new InsetMathBox(buf, l->name));
340 // if (inset == "fbox")
341 // return MathAtom(new InsetMathFBox(l));
342 if (inset == "style")
343 return MathAtom(new InsetMathSize(buf, l));
345 return MathAtom(new InsetMathFont(buf, l));
346 if (inset == "oldfont")
347 return MathAtom(new InsetMathFontOld(buf, l));
348 if (inset == "matrix")
349 return MathAtom(new InsetMathAMSArray(buf, s));
350 if (inset == "split")
351 return MathAtom(new InsetMathSplit(buf, s));
353 // we can't create a InsetMathBig, since the argument
355 return MathAtom(new InsetMathUnknown(s));
356 return MathAtom(new InsetMathSymbol(l));
359 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
360 return MathAtom(new MathMacroArgument(s[1] - '0'));
361 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
362 && s[2] >= '1' && s[2] <= '9')
363 return MathAtom(new MathMacroArgument(s[2] - '0'));
365 return MathAtom(new InsetMathBoxed(buf));
367 return MathAtom(new InsetMathFBox(buf));
369 return MathAtom(new InsetMathMakebox(buf, true));
371 return MathAtom(new InsetMathMakebox(buf, false));
373 return MathAtom(new InsetMathKern);
374 if (s.substr(0, 8) == "xymatrix") {
375 char spacing_code = '\0';
377 bool equal_spacing = false;
378 size_t const len = s.length();
380 if (i < len && s[i] == '@') {
382 if (i < len && s[i] == '!') {
383 equal_spacing = true;
390 spacing_code = static_cast<char>(s[i]);
393 } else if (i < len) {
401 spacing_code = static_cast<char>(s[i]);
405 if (i < len && s[i] == '=') {
407 spacing = Length(to_ascii(s.substr(i)));
411 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
414 if (s == "xrightarrow" || s == "xleftarrow")
415 return MathAtom(new InsetMathXArrow(buf, s));
416 if (s == "split" || s == "alignedat")
417 return MathAtom(new InsetMathSplit(buf, s));
419 return MathAtom(new InsetMathCases(buf));
421 return MathAtom(new InsetMathSubstack(buf));
422 if (s == "subarray" || s == "array")
423 return MathAtom(new InsetMathArray(buf, s, 1, 1));
425 return MathAtom(new InsetMathSqrt(buf));
427 return MathAtom(new InsetMathRoot(buf));
429 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
431 return MathAtom(new InsetMathStackrel(buf));
433 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
435 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
437 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
439 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
441 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
443 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
445 return MathAtom(new InsetMathFrac(buf));
447 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
449 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
451 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
453 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
455 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
457 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
458 // These string values are only for math toolbar use, no LaTeX names
459 if (s == "unitfracthree")
460 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
462 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
464 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
465 if (s == "cfracleft")
466 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
467 if (s == "cfracright")
468 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
470 // return MathAtom(new MathInferInset);
472 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
474 return MathAtom(new InsetMathLefteqn(buf));
475 if (s == "boldsymbol")
476 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
478 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
479 if (s == "heavysymbol" || s == "hm")
480 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
481 if (s == "color" || s == "normalcolor")
482 return MathAtom(new InsetMathColor(buf, true));
483 if (s == "textcolor")
484 return MathAtom(new InsetMathColor(buf, false));
486 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
488 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
490 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
491 if (s == "ensuremath")
492 return MathAtom(new InsetMathEnsureMath(buf));
493 if (isSpecialChar(s))
494 return MathAtom(new InsetMathSpecialChar(s));
497 return MathAtom(new InsetMathHull(buf, hullRegexp));
499 return MathAtom(new MathMacro(buf, s));
503 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
506 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
508 docstring body = split(str, name, ' ');
511 InsetCommandParams icp(REF_CODE);
513 InsetCommand::string2params("ref", to_utf8(str), icp);
514 mathed_parse_cell(ar, icp.getCommand());
515 } else if (name == "mathspace") {
516 InsetSpaceParams isp(true);
517 InsetSpace::string2params(to_utf8(str), isp);
520 Encoding const * const ascii = encodings.fromLyXName("ascii");
521 OutputParams op(ascii);
523 mathed_parse_cell(ar, os.str());
524 if (ar.size() == 2) {
526 if (ar[1].nucleus()->asBraceInset())
535 return ar[0].nucleus();
539 bool isAsciiOrMathAlpha(char_type c)
541 return c < 0x80 || Encodings::isMathAlpha(c);