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 "InsetMathDiagram.h"
48 #include "MacroTable.h"
49 #include "MathMacro.h"
50 #include "MathMacroArgument.h"
51 #include "MathParser.h"
52 #include "MathStream.h"
53 #include "MathSupport.h"
55 #include "insets/InsetCommand.h"
56 #include "insets/InsetSpace.h"
58 #include "support/debug.h"
59 #include "support/docstream.h"
60 #include "support/FileName.h"
61 #include "support/filetools.h" // LibFileSearch
62 #include "support/lstrings.h"
64 #include "frontends/FontLoader.h"
67 #include "BufferParams.h"
69 #include "LyX.h" // use_gui
70 #include "OutputParams.h"
73 using namespace lyx::support;
82 MathWordList theMathWordList;
85 bool isMathFontAvailable(docstring & name)
93 // Do we have the font proper?
94 if (theFontLoader().available(f))
98 if (name == "eufrak") {
99 name = from_ascii("lyxfakefrak");
103 LYXERR(Debug::MATHED,
104 "font " << to_utf8(name) << " not available and I can't fake it");
111 FileName const filename = libFileSearch(string(), "symbols");
112 LYXERR(Debug::MATHED, "read symbols from " << filename);
113 if (filename.empty()) {
114 lyxerr << "Could not find symbols file" << endl;
118 ifstream fs(filename.toFilesystemEncoding().c_str());
121 while (getline(fs, line)) {
124 if (line.empty() || line[0] == '#')
127 // special case of iffont/else/endif
128 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
129 istringstream is(line);
133 docstring t = from_utf8(tmp);
134 skip = !isMathFontAvailable(t);
136 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
139 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
145 // special case of pre-defined macros
146 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
147 //lyxerr << "macro definition: '" << line << '\'' << endl;
148 istringstream is(line);
151 is >> macro >> requires;
152 MacroTable::globalMacros().insert(0, from_utf8(macro), requires);
156 idocstringstream is(from_utf8(line));
158 is >> tmp.name >> tmp.inset;
159 if (isFontName(tmp.inset))
160 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
163 // requires is optional
167 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
168 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' '
169 << to_utf8(tmp.extra));
173 if (isFontName(tmp.inset)) {
174 // tmp.inset _is_ the fontname here.
175 // create fallbacks if necessary
177 // store requirements as long as we can
178 if (tmp.requires.empty()) {
179 if (tmp.inset == "msa" || tmp.inset == "msb")
180 tmp.requires = from_ascii("amssymb");
181 else if (tmp.inset == "wasy")
182 tmp.requires = from_ascii("wasysym");
183 else if (tmp.inset == "mathscr")
184 tmp.requires = from_ascii("mathrsfs");
187 // symbol font is not available sometimes
188 docstring symbol_font = from_ascii("lyxsymbol");
190 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
191 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
193 } else if (isMathFontAvailable(tmp.inset)) {
194 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
195 tmp.draw.push_back(char_type(charid));
196 } else if (fallbackid && isMathFontAvailable(symbol_font)) {
197 if (tmp.inset == "cmex")
198 tmp.inset = from_ascii("lyxsymbol");
200 tmp.inset = from_ascii("lyxboldsymbol");
201 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
202 tmp.draw.push_back(char_type(fallbackid));
204 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
206 tmp.inset = from_ascii("lyxtex");
209 // it's a proper inset
210 LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
211 << " used for " << to_utf8(tmp.name));
214 if (theMathWordList.find(tmp.name) != theMathWordList.end())
215 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
216 << " already exists.");
218 theMathWordList[tmp.name] = tmp;
220 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
221 << " inset: " << to_utf8(tmp.inset)
222 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
223 << " extra: " << to_utf8(tmp.extra)
224 << " requires: " << to_utf8(tmp.requires) << '\'');
226 docstring tmp = from_ascii("cmm");
227 docstring tmp2 = from_ascii("cmsy");
228 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
232 bool isSpecialChar(docstring const & name)
234 if (name.size() != 1)
235 return name == "textasciicircum" || name == "mathcircumflex" ||
236 name == "textasciitilde" || name == "textbackslash";
238 char_type const c = name.at(0);
239 return c == '{' || c == '}' || c == '&' || c == '$' ||
240 c == '#' || c == '%' || c == '_' || c == ' ';
246 MathWordList const & mathedWordList()
248 return theMathWordList;
254 static bool initialized = false;
263 bool ensureMath(WriteStream & os, bool needs_math_mode, bool macro)
265 bool brace = os.pendingBrace();
266 os.pendingBrace(false);
269 if (os.textMode() && needs_math_mode) {
270 os << "\\ensuremath{";
273 } else if (macro && brace && !needs_math_mode) {
274 // This is a user defined macro, but not a MathMacro, so we
275 // cannot be sure what mode is needed. As it was entered in
276 // a text box, we restore the text mode.
285 int ensureMode(WriteStream & os, InsetMath::mode_type mode,
286 bool locked, bool ascii)
288 bool textmode = mode == InsetMath::TEXT_MODE;
289 if (os.latex() && textmode && os.pendingBrace()) {
291 os.pendingBrace(false);
292 os.pendingSpace(false);
295 int oldmodes = os.textMode() ? 0x01 : 0;
296 os.textMode(textmode);
297 oldmodes |= os.lockedMode() ? 0x02 : 0;
298 os.lockedMode(locked);
299 oldmodes |= os.asciiOnly() ? 0x04 : 0;
305 latexkeys const * in_word_set(docstring const & str)
307 MathWordList::iterator it = theMathWordList.find(str);
308 return it != theMathWordList.end() ? &(it->second) : 0;
312 MathAtom createInsetMath(char const * const s, Buffer * buf)
314 return createInsetMath(from_utf8(s), buf);
318 MathAtom createInsetMath(docstring const & s, Buffer * buf)
320 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
321 if ((s == "ce" || s == "cf") && buf
322 && buf->params().use_mhchem == BufferParams::package_off)
323 return MathAtom(new MathMacro(buf, s));
325 latexkeys const * l = in_word_set(s);
327 docstring const & inset = l->inset;
328 //lyxerr << " found inset: '" << inset << '\'' << endl;
330 return MathAtom(new InsetMathRef(buf, l->name));
331 if (inset == "overset")
332 return MathAtom(new InsetMathOverset(buf));
333 if (inset == "underset")
334 return MathAtom(new InsetMathUnderset(buf));
335 if (inset == "decoration")
336 return MathAtom(new InsetMathDecoration(buf, l));
337 if (inset == "space")
338 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
340 return MathAtom(new InsetMathDots(l));
342 // return MathAtom(new InsetMathMBox);
343 // InsetMathMBox is proposed to replace InsetMathBox,
344 // but is not ready yet (it needs a BufferView for
346 return MathAtom(new InsetMathBox(buf, l->name));
347 // if (inset == "fbox")
348 // return MathAtom(new InsetMathFBox(l));
349 if (inset == "style")
350 return MathAtom(new InsetMathSize(buf, l));
352 return MathAtom(new InsetMathFont(buf, l));
353 if (inset == "oldfont")
354 return MathAtom(new InsetMathFontOld(buf, l));
355 if (inset == "matrix")
356 return MathAtom(new InsetMathAMSArray(buf, s));
357 if (inset == "split")
358 return MathAtom(new InsetMathSplit(buf, s));
360 // we can't create a InsetMathBig, since the argument
362 return MathAtom(new InsetMathUnknown(s));
363 return MathAtom(new InsetMathSymbol(l));
366 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
367 return MathAtom(new MathMacroArgument(s[1] - '0'));
368 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
369 && s[2] >= '1' && s[2] <= '9')
370 return MathAtom(new MathMacroArgument(s[2] - '0'));
372 return MathAtom(new InsetMathBoxed(buf));
374 return MathAtom(new InsetMathFBox(buf));
376 return MathAtom(new InsetMathMakebox(buf, true));
378 return MathAtom(new InsetMathMakebox(buf, false));
380 return MathAtom(new InsetMathKern);
381 if (s.substr(0, 8) == "xymatrix") {
382 char spacing_code = '\0';
384 bool equal_spacing = false;
385 size_t const len = s.length();
387 if (i < len && s[i] == '@') {
389 if (i < len && s[i] == '!') {
390 equal_spacing = true;
397 spacing_code = static_cast<char>(s[i]);
400 } else if (i < len) {
408 spacing_code = static_cast<char>(s[i]);
412 if (i < len && s[i] == '=') {
414 spacing = Length(to_ascii(s.substr(i)));
418 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
423 return MathAtom(new InsetMathDiagram(buf));
424 if (s == "xrightarrow" || s == "xleftarrow")
425 return MathAtom(new InsetMathXArrow(buf, s));
426 if (s == "split" || s == "alignedat")
427 return MathAtom(new InsetMathSplit(buf, s));
429 return MathAtom(new InsetMathCases(buf));
431 return MathAtom(new InsetMathSubstack(buf));
432 if (s == "subarray" || s == "array")
433 return MathAtom(new InsetMathArray(buf, s, 1, 1));
435 return MathAtom(new InsetMathSqrt(buf));
437 return MathAtom(new InsetMathRoot(buf));
439 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
441 return MathAtom(new InsetMathStackrel(buf));
443 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
445 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
447 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
449 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
451 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
453 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
455 return MathAtom(new InsetMathFrac(buf));
457 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
459 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
461 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
463 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
465 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
467 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
468 // These string values are only for math toolbar use, no LaTeX names
469 if (s == "unitfracthree")
470 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
472 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
474 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
475 if (s == "cfracleft")
476 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
477 if (s == "cfracright")
478 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
480 // return MathAtom(new MathInferInset);
482 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
484 return MathAtom(new InsetMathLefteqn(buf));
485 if (s == "boldsymbol")
486 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
488 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
489 if (s == "heavysymbol" || s == "hm")
490 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
491 if (s == "color" || s == "normalcolor")
492 return MathAtom(new InsetMathColor(buf, true));
493 if (s == "textcolor")
494 return MathAtom(new InsetMathColor(buf, false));
496 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
498 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
500 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
501 if (s == "ensuremath")
502 return MathAtom(new InsetMathEnsureMath(buf));
503 if (isSpecialChar(s))
504 return MathAtom(new InsetMathSpecialChar(s));
507 return MathAtom(new InsetMathHull(buf, hullRegexp));
509 return MathAtom(new MathMacro(buf, s));
513 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
516 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
518 docstring body = split(str, name, ' ');
521 InsetCommandParams icp(REF_CODE);
523 InsetCommand::string2params(to_utf8(str), icp);
524 Encoding const * const utf8 = encodings.fromLyXName("utf8");
525 OutputParams op(utf8);
526 mathed_parse_cell(ar, icp.getCommand(op));
527 } else if (name == "mathspace") {
528 InsetSpaceParams isp(true);
529 InsetSpace::string2params(to_utf8(str), isp);
532 odocstringstream ods;
533 otexstream os(ods, texrow);
534 Encoding const * const ascii = encodings.fromLyXName("ascii");
535 OutputParams op(ascii);
537 mathed_parse_cell(ar, ods.str());
538 if (ar.size() == 2) {
540 if (ar[1].nucleus()->asBraceInset())
549 return ar[0].nucleus();
553 bool isAsciiOrMathAlpha(char_type c)
555 return c < 0x80 || Encodings::isMathAlpha(c);