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 theMathWordList;
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") {
138 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
144 // special case of pre-defined macros
145 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
146 //lyxerr << "macro definition: '" << line << '\'' << endl;
147 istringstream is(line);
150 is >> macro >> requires;
151 MacroTable::globalMacros().insert(0, from_utf8(macro), requires);
155 idocstringstream is(from_utf8(line));
157 is >> tmp.name >> tmp.inset;
158 if (isFontName(tmp.inset))
159 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
162 // requires is optional
166 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
167 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' '
168 << to_utf8(tmp.extra));
172 if (isFontName(tmp.inset)) {
173 // tmp.inset _is_ the fontname here.
174 // create fallbacks if necessary
176 // store requirements as long as we can
177 if (tmp.requires.empty()) {
178 if (tmp.inset == "msa" || tmp.inset == "msb")
179 tmp.requires = from_ascii("amssymb");
180 else if (tmp.inset == "wasy")
181 tmp.requires = from_ascii("wasysym");
184 // symbol font is not available sometimes
185 docstring symbol_font = from_ascii("lyxsymbol");
187 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
188 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
190 } else if (isMathFontAvailable(tmp.inset)) {
191 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
192 tmp.draw.push_back(char_type(charid));
193 } else if (fallbackid && isMathFontAvailable(symbol_font)) {
194 if (tmp.inset == "cmex")
195 tmp.inset = from_ascii("lyxsymbol");
197 tmp.inset = from_ascii("lyxboldsymbol");
198 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
199 tmp.draw.push_back(char_type(fallbackid));
201 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
203 tmp.inset = from_ascii("lyxtex");
206 // it's a proper inset
207 LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
208 << " used for " << to_utf8(tmp.name));
211 if (theMathWordList.find(tmp.name) != theMathWordList.end())
212 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
213 << " already exists.");
215 theMathWordList[tmp.name] = tmp;
217 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
218 << " inset: " << to_utf8(tmp.inset)
219 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
220 << " extra: " << to_utf8(tmp.extra)
221 << " requires: " << to_utf8(tmp.requires) << '\'');
223 docstring tmp = from_ascii("cmm");
224 docstring tmp2 = from_ascii("cmsy");
225 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
229 bool isSpecialChar(docstring const & name)
231 if (name.size() != 1)
232 return name == "textasciicircum" || name == "mathcircumflex" ||
233 name == "textasciitilde" || name == "textbackslash";
235 char_type const c = name.at(0);
236 return c == '{' || c == '}' || c == '&' || c == '$' ||
237 c == '#' || c == '%' || c == '_' || c == ' ';
243 MathWordList const & mathedWordList()
245 return theMathWordList;
251 static bool initialized = false;
260 bool ensureMath(WriteStream & os, bool needs_math_mode, bool macro)
262 bool brace = os.pendingBrace();
263 os.pendingBrace(false);
266 if (os.textMode() && needs_math_mode) {
267 os << "\\ensuremath{";
270 } else if (macro && brace && !needs_math_mode) {
271 // This is a user defined macro, but not a MathMacro, so we
272 // cannot be sure what mode is needed. As it was entered in
273 // a text box, we restore the text mode.
282 int ensureMode(WriteStream & os, InsetMath::mode_type mode,
283 bool locked, bool ascii)
285 bool textmode = mode == InsetMath::TEXT_MODE;
286 if (os.latex() && textmode && os.pendingBrace()) {
288 os.pendingBrace(false);
289 os.pendingSpace(false);
292 int oldmodes = os.textMode() ? 0x01 : 0;
293 os.textMode(textmode);
294 oldmodes |= os.lockedMode() ? 0x02 : 0;
295 os.lockedMode(locked);
296 oldmodes |= os.asciiOnly() ? 0x04 : 0;
302 latexkeys const * in_word_set(docstring const & str)
304 MathWordList::iterator it = theMathWordList.find(str);
305 return it != theMathWordList.end() ? &(it->second) : 0;
309 MathAtom createInsetMath(char const * const s, Buffer * buf)
311 return createInsetMath(from_utf8(s), buf);
315 MathAtom createInsetMath(docstring const & s, Buffer * buf)
317 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
318 if ((s == "ce" || s == "cf") && buf
319 && buf->params().use_mhchem == BufferParams::package_off)
320 return MathAtom(new MathMacro(buf, s));
322 latexkeys const * l = in_word_set(s);
324 docstring const & inset = l->inset;
325 //lyxerr << " found inset: '" << inset << '\'' << endl;
327 return MathAtom(new InsetMathRef(buf, l->name));
328 if (inset == "overset")
329 return MathAtom(new InsetMathOverset(buf));
330 if (inset == "underset")
331 return MathAtom(new InsetMathUnderset(buf));
332 if (inset == "decoration")
333 return MathAtom(new InsetMathDecoration(buf, l));
334 if (inset == "space")
335 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
337 return MathAtom(new InsetMathDots(l));
339 // return MathAtom(new InsetMathMBox);
340 // InsetMathMBox is proposed to replace InsetMathBox,
341 // but is not ready yet (it needs a BufferView for
343 return MathAtom(new InsetMathBox(buf, l->name));
344 // if (inset == "fbox")
345 // return MathAtom(new InsetMathFBox(l));
346 if (inset == "style")
347 return MathAtom(new InsetMathSize(buf, l));
349 return MathAtom(new InsetMathFont(buf, l));
350 if (inset == "oldfont")
351 return MathAtom(new InsetMathFontOld(buf, l));
352 if (inset == "matrix")
353 return MathAtom(new InsetMathAMSArray(buf, s));
354 if (inset == "split")
355 return MathAtom(new InsetMathSplit(buf, s));
357 // we can't create a InsetMathBig, since the argument
359 return MathAtom(new InsetMathUnknown(s));
360 return MathAtom(new InsetMathSymbol(l));
363 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
364 return MathAtom(new MathMacroArgument(s[1] - '0'));
365 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
366 && s[2] >= '1' && s[2] <= '9')
367 return MathAtom(new MathMacroArgument(s[2] - '0'));
369 return MathAtom(new InsetMathBoxed(buf));
371 return MathAtom(new InsetMathFBox(buf));
373 return MathAtom(new InsetMathMakebox(buf, true));
375 return MathAtom(new InsetMathMakebox(buf, false));
377 return MathAtom(new InsetMathKern);
378 if (s.substr(0, 8) == "xymatrix") {
379 char spacing_code = '\0';
381 bool equal_spacing = false;
382 size_t const len = s.length();
384 if (i < len && s[i] == '@') {
386 if (i < len && s[i] == '!') {
387 equal_spacing = true;
394 spacing_code = static_cast<char>(s[i]);
397 } else if (i < len) {
405 spacing_code = static_cast<char>(s[i]);
409 if (i < len && s[i] == '=') {
411 spacing = Length(to_ascii(s.substr(i)));
415 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
418 if (s == "xrightarrow" || s == "xleftarrow")
419 return MathAtom(new InsetMathXArrow(buf, s));
420 if (s == "split" || s == "alignedat")
421 return MathAtom(new InsetMathSplit(buf, s));
423 return MathAtom(new InsetMathCases(buf));
425 return MathAtom(new InsetMathSubstack(buf));
426 if (s == "subarray" || s == "array")
427 return MathAtom(new InsetMathArray(buf, s, 1, 1));
429 return MathAtom(new InsetMathSqrt(buf));
431 return MathAtom(new InsetMathRoot(buf));
433 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
435 return MathAtom(new InsetMathStackrel(buf));
437 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
439 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
441 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
443 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
445 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
447 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
449 return MathAtom(new InsetMathFrac(buf));
451 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
453 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
455 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
457 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
459 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
461 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
462 // These string values are only for math toolbar use, no LaTeX names
463 if (s == "unitfracthree")
464 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
466 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
468 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
469 if (s == "cfracleft")
470 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
471 if (s == "cfracright")
472 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
474 // return MathAtom(new MathInferInset);
476 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
478 return MathAtom(new InsetMathLefteqn(buf));
479 if (s == "boldsymbol")
480 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
482 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
483 if (s == "heavysymbol" || s == "hm")
484 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
485 if (s == "color" || s == "normalcolor")
486 return MathAtom(new InsetMathColor(buf, true));
487 if (s == "textcolor")
488 return MathAtom(new InsetMathColor(buf, false));
490 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
492 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
494 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
495 if (s == "ensuremath")
496 return MathAtom(new InsetMathEnsureMath(buf));
497 if (isSpecialChar(s))
498 return MathAtom(new InsetMathSpecialChar(s));
501 return MathAtom(new InsetMathHull(buf, hullRegexp));
503 return MathAtom(new MathMacro(buf, s));
507 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
510 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
512 docstring body = split(str, name, ' ');
515 InsetCommandParams icp(REF_CODE);
517 InsetCommand::string2params("ref", to_utf8(str), icp);
518 Encoding const * const utf8 = encodings.fromLyXName("utf8");
519 OutputParams op(utf8);
520 mathed_parse_cell(ar, icp.getCommand(op));
521 } else if (name == "mathspace") {
522 InsetSpaceParams isp(true);
523 InsetSpace::string2params(to_utf8(str), isp);
526 Encoding const * const ascii = encodings.fromLyXName("ascii");
527 OutputParams op(ascii);
529 mathed_parse_cell(ar, os.str());
530 if (ar.size() == 2) {
532 if (ar[1].nucleus()->asBraceInset())
541 return ar[0].nucleus();
545 bool isAsciiOrMathAlpha(char_type c)
547 return c < 0x80 || Encodings::isMathAlpha(c);