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");
182 else if (tmp.inset == "mathscr")
183 tmp.requires = from_ascii("mathrsfs");
186 // symbol font is not available sometimes
187 docstring symbol_font = from_ascii("lyxsymbol");
189 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
190 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
192 } else if (isMathFontAvailable(tmp.inset)) {
193 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
194 tmp.draw.push_back(char_type(charid));
195 } else if (fallbackid && isMathFontAvailable(symbol_font)) {
196 if (tmp.inset == "cmex")
197 tmp.inset = from_ascii("lyxsymbol");
199 tmp.inset = from_ascii("lyxboldsymbol");
200 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
201 tmp.draw.push_back(char_type(fallbackid));
203 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
205 tmp.inset = from_ascii("lyxtex");
208 // it's a proper inset
209 LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
210 << " used for " << to_utf8(tmp.name));
213 if (theMathWordList.find(tmp.name) != theMathWordList.end())
214 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
215 << " already exists.");
217 theMathWordList[tmp.name] = tmp;
219 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
220 << " inset: " << to_utf8(tmp.inset)
221 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
222 << " extra: " << to_utf8(tmp.extra)
223 << " requires: " << to_utf8(tmp.requires) << '\'');
225 docstring tmp = from_ascii("cmm");
226 docstring tmp2 = from_ascii("cmsy");
227 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
231 bool isSpecialChar(docstring const & name)
233 if (name.size() != 1)
234 return name == "textasciicircum" || name == "mathcircumflex" ||
235 name == "textasciitilde" || name == "textbackslash";
237 char_type const c = name.at(0);
238 return c == '{' || c == '}' || c == '&' || c == '$' ||
239 c == '#' || c == '%' || c == '_' || c == ' ';
245 MathWordList const & mathedWordList()
247 return theMathWordList;
253 static bool initialized = false;
262 bool ensureMath(WriteStream & os, bool needs_math_mode, bool macro)
264 bool brace = os.pendingBrace();
265 os.pendingBrace(false);
268 if (os.textMode() && needs_math_mode) {
269 os << "\\ensuremath{";
272 } else if (macro && brace && !needs_math_mode) {
273 // This is a user defined macro, but not a MathMacro, so we
274 // cannot be sure what mode is needed. As it was entered in
275 // a text box, we restore the text mode.
284 int ensureMode(WriteStream & os, InsetMath::mode_type mode,
285 bool locked, bool ascii)
287 bool textmode = mode == InsetMath::TEXT_MODE;
288 if (os.latex() && textmode && os.pendingBrace()) {
290 os.pendingBrace(false);
291 os.pendingSpace(false);
294 int oldmodes = os.textMode() ? 0x01 : 0;
295 os.textMode(textmode);
296 oldmodes |= os.lockedMode() ? 0x02 : 0;
297 os.lockedMode(locked);
298 oldmodes |= os.asciiOnly() ? 0x04 : 0;
304 latexkeys const * in_word_set(docstring const & str)
306 MathWordList::iterator it = theMathWordList.find(str);
307 return it != theMathWordList.end() ? &(it->second) : 0;
311 MathAtom createInsetMath(char const * const s, Buffer * buf)
313 return createInsetMath(from_utf8(s), buf);
317 MathAtom createInsetMath(docstring const & s, Buffer * buf)
319 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
320 if ((s == "ce" || s == "cf") && buf
321 && buf->params().use_mhchem == BufferParams::package_off)
322 return MathAtom(new MathMacro(buf, s));
324 latexkeys const * l = in_word_set(s);
326 docstring const & inset = l->inset;
327 //lyxerr << " found inset: '" << inset << '\'' << endl;
329 return MathAtom(new InsetMathRef(buf, l->name));
330 if (inset == "overset")
331 return MathAtom(new InsetMathOverset(buf));
332 if (inset == "underset")
333 return MathAtom(new InsetMathUnderset(buf));
334 if (inset == "decoration")
335 return MathAtom(new InsetMathDecoration(buf, l));
336 if (inset == "space")
337 return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
339 return MathAtom(new InsetMathDots(l));
341 // return MathAtom(new InsetMathMBox);
342 // InsetMathMBox is proposed to replace InsetMathBox,
343 // but is not ready yet (it needs a BufferView for
345 return MathAtom(new InsetMathBox(buf, l->name));
346 // if (inset == "fbox")
347 // return MathAtom(new InsetMathFBox(l));
348 if (inset == "style")
349 return MathAtom(new InsetMathSize(buf, l));
351 return MathAtom(new InsetMathFont(buf, l));
352 if (inset == "oldfont")
353 return MathAtom(new InsetMathFontOld(buf, l));
354 if (inset == "matrix")
355 return MathAtom(new InsetMathAMSArray(buf, s));
356 if (inset == "split")
357 return MathAtom(new InsetMathSplit(buf, s));
359 // we can't create a InsetMathBig, since the argument
361 return MathAtom(new InsetMathUnknown(s));
362 return MathAtom(new InsetMathSymbol(l));
365 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
366 return MathAtom(new MathMacroArgument(s[1] - '0'));
367 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
368 && s[2] >= '1' && s[2] <= '9')
369 return MathAtom(new MathMacroArgument(s[2] - '0'));
371 return MathAtom(new InsetMathBoxed(buf));
373 return MathAtom(new InsetMathFBox(buf));
375 return MathAtom(new InsetMathMakebox(buf, true));
377 return MathAtom(new InsetMathMakebox(buf, false));
379 return MathAtom(new InsetMathKern);
380 if (s.substr(0, 8) == "xymatrix") {
381 char spacing_code = '\0';
383 bool equal_spacing = false;
384 size_t const len = s.length();
386 if (i < len && s[i] == '@') {
388 if (i < len && s[i] == '!') {
389 equal_spacing = true;
396 spacing_code = static_cast<char>(s[i]);
399 } else if (i < len) {
407 spacing_code = static_cast<char>(s[i]);
411 if (i < len && s[i] == '=') {
413 spacing = Length(to_ascii(s.substr(i)));
417 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
420 if (s == "xrightarrow" || s == "xleftarrow")
421 return MathAtom(new InsetMathXArrow(buf, s));
422 if (s == "split" || s == "alignedat")
423 return MathAtom(new InsetMathSplit(buf, s));
425 return MathAtom(new InsetMathCases(buf));
427 return MathAtom(new InsetMathSubstack(buf));
428 if (s == "subarray" || s == "array")
429 return MathAtom(new InsetMathArray(buf, s, 1, 1));
431 return MathAtom(new InsetMathSqrt(buf));
433 return MathAtom(new InsetMathRoot(buf));
435 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
437 return MathAtom(new InsetMathStackrel(buf));
439 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
441 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
443 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
445 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
447 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
449 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
451 return MathAtom(new InsetMathFrac(buf));
453 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
455 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
457 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
459 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
461 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
463 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
464 // These string values are only for math toolbar use, no LaTeX names
465 if (s == "unitfracthree")
466 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
468 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
470 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
471 if (s == "cfracleft")
472 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
473 if (s == "cfracright")
474 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
476 // return MathAtom(new MathInferInset);
478 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
480 return MathAtom(new InsetMathLefteqn(buf));
481 if (s == "boldsymbol")
482 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
484 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
485 if (s == "heavysymbol" || s == "hm")
486 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
487 if (s == "color" || s == "normalcolor")
488 return MathAtom(new InsetMathColor(buf, true));
489 if (s == "textcolor")
490 return MathAtom(new InsetMathColor(buf, false));
492 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
494 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
496 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
497 if (s == "ensuremath")
498 return MathAtom(new InsetMathEnsureMath(buf));
499 if (isSpecialChar(s))
500 return MathAtom(new InsetMathSpecialChar(s));
503 return MathAtom(new InsetMathHull(buf, hullRegexp));
505 return MathAtom(new MathMacro(buf, s));
509 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
512 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
514 docstring body = split(str, name, ' ');
517 InsetCommandParams icp(REF_CODE);
519 InsetCommand::string2params("ref", to_utf8(str), icp);
520 Encoding const * const utf8 = encodings.fromLyXName("utf8");
521 OutputParams op(utf8);
522 mathed_parse_cell(ar, icp.getCommand(op));
523 } else if (name == "mathspace") {
524 InsetSpaceParams isp(true);
525 InsetSpace::string2params(to_utf8(str), isp);
528 Encoding const * const ascii = encodings.fromLyXName("ascii");
529 OutputParams op(ascii);
531 mathed_parse_cell(ar, os.str());
532 if (ar.size() == 2) {
534 if (ar[1].nucleus()->asBraceInset())
543 return ar[0].nucleus();
547 bool isAsciiOrMathAlpha(char_type c)
549 return c < 0x80 || Encodings::isMathAlpha(c);