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
166 // backward compatibility
167 if (tmp.requires == "esintoramsmath")
168 tmp.requires = from_ascii("esint|amsmath");
170 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
171 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' '
172 << to_utf8(tmp.extra));
176 if (isFontName(tmp.inset)) {
177 // tmp.inset _is_ the fontname here.
178 // create fallbacks if necessary
180 // store requirements as long as we can
181 if (tmp.requires.empty()) {
182 if (tmp.inset == "msa" || tmp.inset == "msb")
183 tmp.requires = from_ascii("amssymb");
184 else if (tmp.inset == "wasy")
185 tmp.requires = from_ascii("wasysym");
186 else if (tmp.inset == "mathscr")
187 tmp.requires = from_ascii("mathrsfs");
190 // symbol font is not available sometimes
191 docstring symbol_font = from_ascii("lyxsymbol");
193 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
194 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
196 } else if (isMathFontAvailable(tmp.inset)) {
197 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
198 tmp.draw.push_back(char_type(charid));
199 } else if (fallbackid && isMathFontAvailable(symbol_font)) {
200 if (tmp.inset == "cmex")
201 tmp.inset = from_ascii("lyxsymbol");
203 tmp.inset = from_ascii("lyxboldsymbol");
204 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
205 tmp.draw.push_back(char_type(fallbackid));
207 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
209 tmp.inset = from_ascii("lyxtex");
212 // it's a proper inset
213 LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
214 << " used for " << to_utf8(tmp.name));
217 if (theMathWordList.find(tmp.name) != theMathWordList.end())
218 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
219 << " already exists.");
221 theMathWordList[tmp.name] = tmp;
223 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
224 << " inset: " << to_utf8(tmp.inset)
225 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
226 << " extra: " << to_utf8(tmp.extra)
227 << " requires: " << to_utf8(tmp.requires) << '\'');
229 docstring tmp = from_ascii("cmm");
230 docstring tmp2 = from_ascii("cmsy");
231 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
235 bool isSpecialChar(docstring const & name)
237 if (name.size() != 1)
238 return name == "textasciicircum" || name == "mathcircumflex" ||
239 name == "textasciitilde" || name == "textbackslash";
241 char_type const c = name.at(0);
242 return c == '{' || c == '}' || c == '&' || c == '$' ||
243 c == '#' || c == '%' || c == '_';
249 MathWordList const & mathedWordList()
251 return theMathWordList;
257 static bool initialized = false;
266 bool ensureMath(WriteStream & os, bool needs_math_mode, bool macro)
268 bool brace = os.pendingBrace();
269 os.pendingBrace(false);
272 if (os.textMode() && needs_math_mode) {
273 os << "\\ensuremath{";
276 } else if (macro && brace && !needs_math_mode) {
277 // This is a user defined macro, but not a MathMacro, so we
278 // cannot be sure what mode is needed. As it was entered in
279 // a text box, we restore the text mode.
288 int ensureMode(WriteStream & os, InsetMath::mode_type mode,
289 bool locked, bool ascii)
291 bool textmode = mode == InsetMath::TEXT_MODE;
292 if (os.latex() && textmode && os.pendingBrace()) {
294 os.pendingBrace(false);
295 os.pendingSpace(false);
298 int oldmodes = os.textMode() ? 0x01 : 0;
299 os.textMode(textmode);
300 oldmodes |= os.lockedMode() ? 0x02 : 0;
301 os.lockedMode(locked);
302 oldmodes |= os.asciiOnly() ? 0x04 : 0;
308 latexkeys const * in_word_set(docstring const & str)
310 MathWordList::iterator it = theMathWordList.find(str);
311 return it != theMathWordList.end() ? &(it->second) : 0;
315 MathAtom createInsetMath(char const * const s, Buffer * buf)
317 return createInsetMath(from_utf8(s), buf);
321 MathAtom createInsetMath(docstring const & s, Buffer * buf)
323 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
324 if ((s == "ce" || s == "cf") && buf
325 && buf->params().use_package("mhchem") == BufferParams::package_off)
326 return MathAtom(new MathMacro(buf, s));
328 latexkeys const * l = in_word_set(s);
330 docstring const & inset = l->inset;
331 //lyxerr << " found inset: '" << inset << '\'' << endl;
333 return MathAtom(new InsetMathRef(buf, l->name));
334 if (inset == "overset")
335 return MathAtom(new InsetMathOverset(buf));
336 if (inset == "underset")
337 return MathAtom(new InsetMathUnderset(buf));
338 if (inset == "decoration")
339 return MathAtom(new InsetMathDecoration(buf, l));
340 if (inset == "space")
341 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
343 return MathAtom(new InsetMathDots(l));
345 // return MathAtom(new InsetMathMBox);
346 // InsetMathMBox is proposed to replace InsetMathBox,
347 // but is not ready yet (it needs a BufferView for
349 return MathAtom(new InsetMathBox(buf, l->name));
350 // if (inset == "fbox")
351 // return MathAtom(new InsetMathFBox(l));
352 if (inset == "style")
353 return MathAtom(new InsetMathSize(buf, l));
355 return MathAtom(new InsetMathFont(buf, l));
356 if (inset == "oldfont")
357 return MathAtom(new InsetMathFontOld(buf, l));
358 if (inset == "matrix")
359 return MathAtom(new InsetMathAMSArray(buf, s));
360 if (inset == "split")
361 return MathAtom(new InsetMathSplit(buf, s));
363 // we can't create a InsetMathBig, since the argument
365 return MathAtom(new InsetMathUnknown(s));
366 return MathAtom(new InsetMathSymbol(l));
369 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
370 return MathAtom(new MathMacroArgument(s[1] - '0'));
371 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
372 && s[2] >= '1' && s[2] <= '9')
373 return MathAtom(new MathMacroArgument(s[2] - '0'));
375 return MathAtom(new InsetMathBoxed(buf));
377 return MathAtom(new InsetMathFBox(buf));
379 return MathAtom(new InsetMathMakebox(buf, true));
381 return MathAtom(new InsetMathMakebox(buf, false));
383 return MathAtom(new InsetMathKern);
384 if (s.substr(0, 8) == "xymatrix") {
385 char spacing_code = '\0';
387 bool equal_spacing = false;
388 size_t const len = s.length();
390 if (i < len && s[i] == '@') {
392 if (i < len && s[i] == '!') {
393 equal_spacing = true;
400 spacing_code = static_cast<char>(s[i]);
403 } else if (i < len) {
411 spacing_code = static_cast<char>(s[i]);
415 if (i < len && s[i] == '=') {
417 spacing = Length(to_ascii(s.substr(i)));
421 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
426 return MathAtom(new InsetMathDiagram(buf));
427 if (s == "xrightarrow" || s == "xleftarrow")
428 return MathAtom(new InsetMathXArrow(buf, s));
429 if (s == "split" || s == "alignedat")
430 return MathAtom(new InsetMathSplit(buf, s));
432 return MathAtom(new InsetMathCases(buf));
434 return MathAtom(new InsetMathSubstack(buf));
435 if (s == "subarray" || s == "array")
436 return MathAtom(new InsetMathArray(buf, s, 1, 1));
438 return MathAtom(new InsetMathSqrt(buf));
440 return MathAtom(new InsetMathRoot(buf));
442 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
444 return MathAtom(new InsetMathStackrel(buf));
446 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
448 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
450 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
452 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
454 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
456 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
458 return MathAtom(new InsetMathFrac(buf));
460 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
462 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
464 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
466 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
468 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
470 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
471 // These string values are only for math toolbar use, no LaTeX names
472 if (s == "unitfracthree")
473 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
475 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
477 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
478 if (s == "cfracleft")
479 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
480 if (s == "cfracright")
481 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
483 // return MathAtom(new MathInferInset);
485 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
487 return MathAtom(new InsetMathLefteqn(buf));
488 if (s == "boldsymbol")
489 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
491 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
492 if (s == "heavysymbol" || s == "hm")
493 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
494 if (s == "color" || s == "normalcolor")
495 return MathAtom(new InsetMathColor(buf, true));
496 if (s == "textcolor")
497 return MathAtom(new InsetMathColor(buf, false));
499 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
501 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
503 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
504 if (s == "ensuremath")
505 return MathAtom(new InsetMathEnsureMath(buf));
506 if (isSpecialChar(s))
507 return MathAtom(new InsetMathSpecialChar(s));
509 return MathAtom(new InsetMathSpace(" ", ""));
512 return MathAtom(new InsetMathHull(buf, hullRegexp));
514 return MathAtom(new MathMacro(buf, s));
518 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
521 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
523 docstring body = split(str, name, ' ');
526 InsetCommandParams icp(REF_CODE);
528 InsetCommand::string2params(to_utf8(str), icp);
529 Encoding const * const utf8 = encodings.fromLyXName("utf8");
530 OutputParams op(utf8);
531 mathed_parse_cell(ar, icp.getCommand(op));
532 } else if (name == "mathspace") {
533 InsetSpaceParams isp(true);
534 InsetSpace::string2params(to_utf8(str), isp);
537 odocstringstream ods;
538 otexstream os(ods, texrow);
539 Encoding const * const ascii = encodings.fromLyXName("ascii");
540 OutputParams op(ascii);
542 mathed_parse_cell(ar, ods.str());
543 if (ar.size() == 2) {
545 if (ar[1].nucleus()->asBraceInset())
554 return ar[0].nucleus();
558 bool isAsciiOrMathAlpha(char_type c)
560 return c < 0x80 || Encodings::isMathAlpha(c);