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 "LyX.h" // use_gui
67 #include "OutputParams.h"
70 using namespace lyx::support;
79 MathWordList theWordList;
82 bool isMathFontAvailable(docstring & name)
90 // Do we have the font proper?
91 if (theFontLoader().available(f))
95 if (name == "eufrak") {
96 name = from_ascii("lyxfakefrak");
100 LYXERR(Debug::MATHED,
101 "font " << to_utf8(name) << " not available and I can't fake it");
108 FileName const filename = libFileSearch(string(), "symbols");
109 LYXERR(Debug::MATHED, "read symbols from " << filename);
110 if (filename.empty()) {
111 lyxerr << "Could not find symbols file" << endl;
115 ifstream fs(filename.toFilesystemEncoding().c_str());
118 while (getline(fs, line)) {
121 if (line.empty() || line[0] == '#')
124 // special case of iffont/else/endif
125 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
126 istringstream is(line);
130 docstring t = from_utf8(tmp);
131 skip = !isMathFontAvailable(t);
133 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
135 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
141 // special case of pre-defined macros
142 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
143 //lyxerr << "macro definition: '" << line << '\'' << endl;
144 istringstream is(line);
147 is >> macro >> requires;
148 MacroTable::globalMacros().insert(from_utf8(macro), requires);
152 idocstringstream is(from_utf8(line));
154 is >> tmp.name >> tmp.inset;
155 if (isFontName(tmp.inset))
156 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
159 // requires is optional
163 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
164 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' '
165 << to_utf8(tmp.extra));
169 if (isFontName(tmp.inset)) {
170 // tmp.inset _is_ the fontname here.
171 // create fallbacks if necessary
173 // store requirements as long as we can
174 if (tmp.requires.empty()) {
175 if (tmp.inset == "msa" || tmp.inset == "msb")
176 tmp.requires = from_ascii("amssymb");
177 else if (tmp.inset == "wasy")
178 tmp.requires = from_ascii("wasysym");
181 // symbol font is not available sometimes
182 docstring symbol_font = from_ascii("lyxsymbol");
184 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
185 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
187 } else if (isMathFontAvailable(tmp.inset)) {
188 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
189 tmp.draw.push_back(char_type(charid));
190 } else if (fallbackid && isMathFontAvailable(symbol_font)) {
191 if (tmp.inset == "cmex")
192 tmp.inset = from_ascii("lyxsymbol");
194 tmp.inset = from_ascii("lyxboldsymbol");
195 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
196 tmp.draw.push_back(char_type(fallbackid));
198 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
200 tmp.inset = from_ascii("lyxtex");
203 // it's a proper inset
204 LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
205 << " used for " << to_utf8(tmp.name));
208 if (theWordList.find(tmp.name) != theWordList.end())
209 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
210 << " already exists.");
212 theWordList[tmp.name] = tmp;
214 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
215 << " inset: " << to_utf8(tmp.inset)
216 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
217 << " extra: " << to_utf8(tmp.extra)
218 << " requires: " << to_utf8(tmp.requires) << '\'');
220 docstring tmp = from_ascii("cmm");
221 docstring tmp2 = from_ascii("cmsy");
222 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
226 bool isSpecialChar(docstring const & name)
228 if (name.size() != 1)
229 return name == "textasciicircum" || name == "mathcircumflex" ||
230 name == "textasciitilde" || name == "textbackslash";
232 char_type const c = name.at(0);
233 return c == '{' || c == '}' || c == '&' || c == '$' ||
234 c == '#' || c == '%' || c == '_' || c == ' ';
240 MathWordList const & mathedWordList()
248 static bool initialized = false;
257 bool ensureMath(WriteStream & os, bool needs_math_mode, bool macro)
259 bool brace = os.pendingBrace();
260 os.pendingBrace(false);
263 if (os.textMode() && needs_math_mode) {
264 os << "\\ensuremath{";
267 } else if (macro && brace && !needs_math_mode) {
268 // This is a user defined macro, but not a MathMacro, so we
269 // cannot be sure what mode is needed. As it was entered in
270 // a text box, we restore the text mode.
279 bool ensureMode(WriteStream & os, InsetMath::mode_type mode)
281 bool textmode = mode == InsetMath::TEXT_MODE;
282 if (os.latex() && textmode && os.pendingBrace()) {
284 os.pendingBrace(false);
285 os.pendingSpace(false);
288 bool oldmode = os.textMode();
289 os.textMode(textmode);
294 latexkeys const * in_word_set(docstring const & str)
296 MathWordList::iterator it = theWordList.find(str);
297 return it != theWordList.end() ? &(it->second) : 0;
301 MathAtom createInsetMath(char const * const s)
303 return createInsetMath(from_utf8(s));
307 MathAtom createInsetMath(docstring const & s)
309 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
310 latexkeys const * l = in_word_set(s);
312 docstring const & inset = l->inset;
313 //lyxerr << " found inset: '" << inset << '\'' << endl;
315 return MathAtom(new InsetMathRef(l->name));
316 if (inset == "overset")
317 return MathAtom(new InsetMathOverset);
318 if (inset == "underset")
319 return MathAtom(new InsetMathUnderset);
320 if (inset == "decoration")
321 return MathAtom(new InsetMathDecoration(l));
322 if (inset == "space")
323 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
325 return MathAtom(new InsetMathDots(l));
327 // return MathAtom(new InsetMathMBox);
328 // InsetMathMBox is proposed to replace InsetMathBox,
329 // but is not ready yet (it needs a BufferView for
331 return MathAtom(new InsetMathBox(l->name));
332 // if (inset == "fbox")
333 // return MathAtom(new InsetMathFBox(l));
334 if (inset == "style")
335 return MathAtom(new InsetMathSize(l));
337 return MathAtom(new InsetMathFont(l));
338 if (inset == "oldfont")
339 return MathAtom(new InsetMathFontOld(l));
340 if (inset == "matrix")
341 return MathAtom(new InsetMathAMSArray(s));
342 if (inset == "split")
343 return MathAtom(new InsetMathSplit(s));
345 // we can't create a InsetMathBig, since the argument
347 return MathAtom(new InsetMathUnknown(s));
348 return MathAtom(new InsetMathSymbol(l));
351 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
352 return MathAtom(new MathMacroArgument(s[1] - '0'));
353 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
354 && s[2] >= '1' && s[2] <= '9')
355 return MathAtom(new MathMacroArgument(s[2] - '0'));
357 return MathAtom(new InsetMathBoxed());
359 return MathAtom(new InsetMathFBox());
361 return MathAtom(new InsetMathMakebox(true));
363 return MathAtom(new InsetMathMakebox(false));
365 return MathAtom(new InsetMathKern);
366 if (s.substr(0, 8) == "xymatrix") {
367 char spacing_code = '\0';
369 size_t const len = s.length();
371 if (i < len && s[i] == '@') {
381 spacing_code = static_cast<char>(s[i]);
386 if (i < len && s[i] == '=') {
388 spacing = Length(to_ascii(s.substr(i)));
391 return MathAtom(new InsetMathXYMatrix(spacing, spacing_code));
393 if (s == "xrightarrow" || s == "xleftarrow")
394 return MathAtom(new InsetMathXArrow(s));
395 if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
396 return MathAtom(new InsetMathSplit(s));
398 return MathAtom(new InsetMathCases);
400 return MathAtom(new InsetMathSubstack);
401 if (s == "subarray" || s == "array")
402 return MathAtom(new InsetMathArray(s, 1, 1));
404 return MathAtom(new InsetMathSqrt);
406 return MathAtom(new InsetMathRoot);
408 return MathAtom(new InsetMathTabular(s, 1, 1));
410 return MathAtom(new InsetMathStackrel);
412 return MathAtom(new InsetMathBinom(InsetMathBinom::BINOM));
414 return MathAtom(new InsetMathBinom(InsetMathBinom::DBINOM));
416 return MathAtom(new InsetMathBinom(InsetMathBinom::TBINOM));
418 return MathAtom(new InsetMathBinom(InsetMathBinom::CHOOSE));
420 return MathAtom(new InsetMathBinom(InsetMathBinom::BRACE));
422 return MathAtom(new InsetMathBinom(InsetMathBinom::BRACK));
424 return MathAtom(new InsetMathFrac);
426 return MathAtom(new InsetMathFrac(InsetMathFrac::CFRAC));
428 return MathAtom(new InsetMathFrac(InsetMathFrac::DFRAC));
430 return MathAtom(new InsetMathFrac(InsetMathFrac::TFRAC));
432 return MathAtom(new InsetMathFrac(InsetMathFrac::OVER));
434 return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC));
436 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC));
437 // These string values are only for math toolbar use, no LaTeX names
438 if (s == "unitfracthree")
439 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3));
441 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1));
443 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT));
444 if (s == "cfracleft")
445 return MathAtom(new InsetMathFrac(InsetMathFrac::CFRACLEFT));
446 if (s == "cfracright")
447 return MathAtom(new InsetMathFrac(InsetMathFrac::CFRACRIGHT));
449 // return MathAtom(new MathInferInset);
451 return MathAtom(new InsetMathFrac(InsetMathFrac::ATOP));
453 return MathAtom(new InsetMathLefteqn);
454 if (s == "boldsymbol")
455 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::AMS_BOLD));
457 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::BM_BOLD));
458 if (s == "heavysymbol" || s == "hm")
459 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::BM_HEAVY));
460 if (s == "color" || s == "normalcolor")
461 return MathAtom(new InsetMathColor(true));
462 if (s == "textcolor")
463 return MathAtom(new InsetMathColor(false));
465 return MathAtom(new InsetMathPhantom(InsetMathPhantom::hphantom));
467 return MathAtom(new InsetMathPhantom(InsetMathPhantom::phantom));
469 return MathAtom(new InsetMathPhantom(InsetMathPhantom::vphantom));
470 if (s == "ensuremath")
471 return MathAtom(new InsetMathEnsureMath);
472 if (isSpecialChar(s))
473 return MathAtom(new InsetMathSpecialChar(s));
476 return MathAtom(new InsetMathHull(hullRegexp));
478 return MathAtom(new MathMacro(s));
482 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
485 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
487 docstring body = split(str, name, ' ');
490 InsetCommandParams icp(REF_CODE);
492 InsetCommand::string2params("ref", to_utf8(str), icp);
493 mathed_parse_cell(ar, icp.getCommand());
494 } else if (name == "mathspace") {
495 InsetSpaceParams isp(true);
496 InsetSpace::string2params(to_utf8(str), isp);
499 Encoding const * const ascii = encodings.fromLyXName("ascii");
500 OutputParams op(ascii);
502 mathed_parse_cell(ar, os.str());
503 if (ar.size() == 2) {
505 if (ar[1].nucleus()->asBraceInset())
514 return ar[0].nucleus();
518 bool isAsciiOrMathAlpha(char_type c)
520 return c < 0x80 || Encodings::isMathAlpha(c);