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,
282 bool locked, bool ascii)
284 bool textmode = mode == InsetMath::TEXT_MODE;
285 if (os.latex() && textmode && os.pendingBrace()) {
287 os.pendingBrace(false);
288 os.pendingSpace(false);
291 int oldmodes = os.textMode() ? 0x01 : 0;
292 os.textMode(textmode);
293 oldmodes |= os.lockedMode() ? 0x02 : 0;
294 os.lockedMode(locked);
295 oldmodes |= os.asciiOnly() ? 0x04 : 0;
301 latexkeys const * in_word_set(docstring const & str)
303 MathWordList::iterator it = theWordList.find(str);
304 return it != theWordList.end() ? &(it->second) : 0;
308 MathAtom createInsetMath(char const * const s, Buffer * buf)
310 return createInsetMath(from_utf8(s), buf);
314 MathAtom createInsetMath(docstring const & s, Buffer * buf)
316 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
317 if ((s == "ce" || s == "cf") && buf
318 && buf->params().use_mhchem == BufferParams::package_off)
319 return MathAtom(new MathMacro(buf, s));
321 latexkeys const * l = in_word_set(s);
323 docstring const & inset = l->inset;
324 //lyxerr << " found inset: '" << inset << '\'' << endl;
326 return MathAtom(new InsetMathRef(buf, l->name));
327 if (inset == "overset")
328 return MathAtom(new InsetMathOverset(buf));
329 if (inset == "underset")
330 return MathAtom(new InsetMathUnderset(buf));
331 if (inset == "decoration")
332 return MathAtom(new InsetMathDecoration(buf, l));
333 if (inset == "space")
334 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
336 return MathAtom(new InsetMathDots(l));
338 // return MathAtom(new InsetMathMBox);
339 // InsetMathMBox is proposed to replace InsetMathBox,
340 // but is not ready yet (it needs a BufferView for
342 return MathAtom(new InsetMathBox(buf, l->name));
343 // if (inset == "fbox")
344 // return MathAtom(new InsetMathFBox(l));
345 if (inset == "style")
346 return MathAtom(new InsetMathSize(buf, l));
348 return MathAtom(new InsetMathFont(buf, l));
349 if (inset == "oldfont")
350 return MathAtom(new InsetMathFontOld(buf, l));
351 if (inset == "matrix")
352 return MathAtom(new InsetMathAMSArray(buf, s));
353 if (inset == "split")
354 return MathAtom(new InsetMathSplit(buf, s));
356 // we can't create a InsetMathBig, since the argument
358 return MathAtom(new InsetMathUnknown(s));
359 return MathAtom(new InsetMathSymbol(l));
362 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
363 return MathAtom(new MathMacroArgument(s[1] - '0'));
364 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
365 && s[2] >= '1' && s[2] <= '9')
366 return MathAtom(new MathMacroArgument(s[2] - '0'));
368 return MathAtom(new InsetMathBoxed(buf));
370 return MathAtom(new InsetMathFBox(buf));
372 return MathAtom(new InsetMathMakebox(buf, true));
374 return MathAtom(new InsetMathMakebox(buf, false));
376 return MathAtom(new InsetMathKern);
377 if (s.substr(0, 8) == "xymatrix") {
378 char spacing_code = '\0';
380 bool equal_spacing = false;
381 size_t const len = s.length();
383 if (i < len && s[i] == '@') {
385 if (i < len && s[i] == '!') {
386 equal_spacing = true;
393 spacing_code = static_cast<char>(s[i]);
396 } else if (i < len) {
404 spacing_code = static_cast<char>(s[i]);
408 if (i < len && s[i] == '=') {
410 spacing = Length(to_ascii(s.substr(i)));
414 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
417 if (s == "xrightarrow" || s == "xleftarrow")
418 return MathAtom(new InsetMathXArrow(buf, s));
419 if (s == "split" || s == "alignedat")
420 return MathAtom(new InsetMathSplit(buf, s));
422 return MathAtom(new InsetMathCases(buf));
424 return MathAtom(new InsetMathSubstack(buf));
425 if (s == "subarray" || s == "array")
426 return MathAtom(new InsetMathArray(buf, s, 1, 1));
428 return MathAtom(new InsetMathSqrt(buf));
430 return MathAtom(new InsetMathRoot(buf));
432 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
434 return MathAtom(new InsetMathStackrel(buf));
436 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
438 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
440 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
442 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
444 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
446 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
448 return MathAtom(new InsetMathFrac(buf));
450 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
452 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
454 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
456 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
458 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
460 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
461 // These string values are only for math toolbar use, no LaTeX names
462 if (s == "unitfracthree")
463 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
465 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
467 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
468 if (s == "cfracleft")
469 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
470 if (s == "cfracright")
471 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
473 // return MathAtom(new MathInferInset);
475 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
477 return MathAtom(new InsetMathLefteqn(buf));
478 if (s == "boldsymbol")
479 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
481 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
482 if (s == "heavysymbol" || s == "hm")
483 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
484 if (s == "color" || s == "normalcolor")
485 return MathAtom(new InsetMathColor(buf, true));
486 if (s == "textcolor")
487 return MathAtom(new InsetMathColor(buf, false));
489 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
491 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
493 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
494 if (s == "ensuremath")
495 return MathAtom(new InsetMathEnsureMath(buf));
496 if (isSpecialChar(s))
497 return MathAtom(new InsetMathSpecialChar(s));
500 return MathAtom(new InsetMathHull(buf, hullRegexp));
502 return MathAtom(new MathMacro(buf, s));
506 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
509 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
511 docstring body = split(str, name, ' ');
514 InsetCommandParams icp(REF_CODE);
516 InsetCommand::string2params("ref", to_utf8(str), icp);
517 Encoding const * const utf8 = encodings.fromLyXName("utf8");
518 OutputParams op(utf8);
519 mathed_parse_cell(ar, icp.getCommand(op));
520 } else if (name == "mathspace") {
521 InsetSpaceParams isp(true);
522 InsetSpace::string2params(to_utf8(str), isp);
525 Encoding const * const ascii = encodings.fromLyXName("ascii");
526 OutputParams op(ascii);
528 mathed_parse_cell(ar, os.str());
529 if (ar.size() == 2) {
531 if (ar[1].nucleus()->asBraceInset())
540 return ar[0].nucleus();
544 bool isAsciiOrMathAlpha(char_type c)
546 return c < 0x80 || Encodings::isMathAlpha(c);