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"
56 #include "support/debug.h"
57 #include "support/docstream.h"
58 #include "support/FileName.h"
59 #include "support/filetools.h" // LibFileSearch
60 #include "support/lstrings.h"
62 #include "frontends/FontLoader.h"
64 #include "LyX.h" // use_gui
67 using namespace lyx::support;
76 MathWordList theWordList;
79 bool isMathFontAvailable(docstring & name)
87 // Do we have the font proper?
88 if (theFontLoader().available(f))
92 if (name == "eufrak") {
93 name = from_ascii("lyxfakefrak");
98 "font " << to_utf8(name) << " not available and I can't fake it");
105 FileName const filename = libFileSearch(string(), "symbols");
106 LYXERR(Debug::MATHED, "read symbols from " << filename);
107 if (filename.empty()) {
108 lyxerr << "Could not find symbols file" << endl;
112 ifstream fs(filename.toFilesystemEncoding().c_str());
115 while (getline(fs, line)) {
118 if (line.empty() || line[0] == '#')
121 // special case of iffont/else/endif
122 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
123 istringstream is(line);
127 docstring t = from_utf8(tmp);
128 skip = !isMathFontAvailable(t);
130 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
132 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
138 // special case of pre-defined macros
139 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
140 //lyxerr << "macro definition: '" << line << '\'' << endl;
141 istringstream is(line);
144 is >> macro >> requires;
145 MacroTable::globalMacros().insert(from_utf8(macro), requires);
149 idocstringstream is(from_utf8(line));
151 is >> tmp.name >> tmp.inset;
152 if (isFontName(tmp.inset))
153 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
156 // requires is optional
160 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
161 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' '
162 << to_utf8(tmp.extra));
166 if (isFontName(tmp.inset)) {
167 // tmp.inset _is_ the fontname here.
168 // create fallbacks if necessary
170 // store requirements as long as we can
171 if (tmp.requires.empty()) {
172 if (tmp.inset == "msa" || tmp.inset == "msb")
173 tmp.requires = from_ascii("amssymb");
174 else if (tmp.inset == "wasy")
175 tmp.requires = from_ascii("wasysym");
178 // symbol font is not available sometimes
179 docstring symbol_font = from_ascii("lyxsymbol");
181 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
182 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
184 } else if (isMathFontAvailable(tmp.inset)) {
185 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
186 tmp.draw.push_back(char_type(charid));
187 } else if (fallbackid && isMathFontAvailable(symbol_font)) {
188 if (tmp.inset == "cmex")
189 tmp.inset = from_ascii("lyxsymbol");
191 tmp.inset = from_ascii("lyxboldsymbol");
192 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
193 tmp.draw.push_back(char_type(fallbackid));
195 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
197 tmp.inset = from_ascii("lyxtex");
200 // it's a proper inset
201 LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
202 << " used for " << to_utf8(tmp.name));
205 if (theWordList.find(tmp.name) != theWordList.end())
206 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
207 << " already exists.");
209 theWordList[tmp.name] = tmp;
211 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
212 << " inset: " << to_utf8(tmp.inset)
213 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
214 << " extra: " << to_utf8(tmp.extra)
215 << " requires: " << to_utf8(tmp.requires) << '\'');
217 docstring tmp = from_ascii("cmm");
218 docstring tmp2 = from_ascii("cmsy");
219 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
223 bool isSpecialChar(docstring const & name)
225 if (name.size() != 1)
226 return name == "textasciicircum" || name == "mathcircumflex" ||
227 name == "textasciitilde" || name == "textbackslash";
229 char_type const c = name.at(0);
230 return c == '{' || c == '}' || c == '&' || c == '$' ||
231 c == '#' || c == '%' || c == '_' || c == ' ';
237 MathWordList const & mathedWordList()
245 static bool initialized = false;
254 bool ensureMath(WriteStream & os, bool needs_math_mode, bool macro)
256 bool brace = os.pendingBrace();
257 os.pendingBrace(false);
260 if (os.textMode() && needs_math_mode) {
261 os << "\\ensuremath{";
264 } else if (macro && brace && !needs_math_mode) {
265 // This is a user defined macro, but not a MathMacro, so we
266 // cannot be sure what mode is needed. As it was entered in
267 // a text box, we restore the text mode.
276 bool ensureMode(WriteStream & os, InsetMath::mode_type mode)
278 bool textmode = mode == InsetMath::TEXT_MODE;
279 if (os.latex() && textmode && os.pendingBrace()) {
281 os.pendingBrace(false);
282 os.pendingSpace(false);
285 bool oldmode = os.textMode();
286 os.textMode(textmode);
291 latexkeys const * in_word_set(docstring const & str)
293 MathWordList::iterator it = theWordList.find(str);
294 return it != theWordList.end() ? &(it->second) : 0;
298 MathAtom createInsetMath(char const * const s)
300 return createInsetMath(from_utf8(s));
304 MathAtom createInsetMath(docstring const & s)
306 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
307 latexkeys const * l = in_word_set(s);
309 docstring const & inset = l->inset;
310 //lyxerr << " found inset: '" << inset << '\'' << endl;
312 return MathAtom(new InsetMathRef(l->name));
313 if (inset == "overset")
314 return MathAtom(new InsetMathOverset);
315 if (inset == "underset")
316 return MathAtom(new InsetMathUnderset);
317 if (inset == "decoration")
318 return MathAtom(new InsetMathDecoration(l));
319 if (inset == "space")
320 return MathAtom(new InsetMathSpace(l->name));
322 return MathAtom(new InsetMathDots(l));
324 // return MathAtom(new InsetMathMBox);
325 // InsetMathMBox is proposed to replace InsetMathBox,
326 // but is not ready yet (it needs a BufferView for
328 return MathAtom(new InsetMathBox(l->name));
329 // if (inset == "fbox")
330 // return MathAtom(new InsetMathFBox(l));
331 if (inset == "style")
332 return MathAtom(new InsetMathSize(l));
334 return MathAtom(new InsetMathFont(l));
335 if (inset == "oldfont")
336 return MathAtom(new InsetMathFontOld(l));
337 if (inset == "matrix")
338 return MathAtom(new InsetMathAMSArray(s));
339 if (inset == "split")
340 return MathAtom(new InsetMathSplit(s));
342 // we can't create a InsetMathBig, since the argument
344 return MathAtom(new InsetMathUnknown(s));
345 return MathAtom(new InsetMathSymbol(l));
348 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
349 return MathAtom(new MathMacroArgument(s[1] - '0'));
350 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
351 && s[2] >= '1' && s[2] <= '9')
352 return MathAtom(new MathMacroArgument(s[2] - '0'));
354 return MathAtom(new InsetMathBoxed());
356 return MathAtom(new InsetMathFBox());
358 return MathAtom(new InsetMathMakebox(true));
360 return MathAtom(new InsetMathMakebox(false));
362 return MathAtom(new InsetMathKern);
363 if (s.substr(0, 8) == "xymatrix") {
364 char spacing_code = '\0';
366 size_t const len = s.length();
368 if (i < len && s[i] == '@') {
378 spacing_code = static_cast<char>(s[i]);
383 if (i < len && s[i] == '=') {
385 spacing = Length(to_ascii(s.substr(i)));
388 return MathAtom(new InsetMathXYMatrix(spacing, spacing_code));
390 if (s == "xrightarrow" || s == "xleftarrow")
391 return MathAtom(new InsetMathXArrow(s));
392 if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
393 return MathAtom(new InsetMathSplit(s));
395 return MathAtom(new InsetMathCases);
397 return MathAtom(new InsetMathSubstack);
398 if (s == "subarray" || s == "array")
399 return MathAtom(new InsetMathArray(s, 1, 1));
401 return MathAtom(new InsetMathSqrt);
403 return MathAtom(new InsetMathRoot);
405 return MathAtom(new InsetMathTabular(s, 1, 1));
407 return MathAtom(new InsetMathStackrel);
409 return MathAtom(new InsetMathBinom(InsetMathBinom::BINOM));
411 return MathAtom(new InsetMathBinom(InsetMathBinom::CHOOSE));
413 return MathAtom(new InsetMathBinom(InsetMathBinom::BRACE));
415 return MathAtom(new InsetMathBinom(InsetMathBinom::BRACK));
417 return MathAtom(new InsetMathFrac);
419 return MathAtom(new InsetMathFrac(InsetMathFrac::OVER));
421 return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC));
423 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC));
424 // This string value is only for math toolbar use. Not a LaTeX name
425 if (s == "unitfracthree")
426 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3));
428 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1));
430 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT));
432 // return MathAtom(new MathInferInset);
434 return MathAtom(new InsetMathFrac(InsetMathFrac::ATOP));
436 return MathAtom(new InsetMathLefteqn);
437 if (s == "boldsymbol")
438 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::AMS_BOLD));
440 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::BM_BOLD));
441 if (s == "heavysymbol" || s == "hm")
442 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::BM_HEAVY));
443 if (s == "color" || s == "normalcolor")
444 return MathAtom(new InsetMathColor(true));
445 if (s == "textcolor")
446 return MathAtom(new InsetMathColor(false));
448 return MathAtom(new InsetMathDFrac);
450 return MathAtom(new InsetMathTFrac);
452 return MathAtom(new InsetMathDBinom);
454 return MathAtom(new InsetMathTBinom);
456 return MathAtom(new InsetMathPhantom(InsetMathPhantom::hphantom));
458 return MathAtom(new InsetMathPhantom(InsetMathPhantom::phantom));
460 return MathAtom(new InsetMathPhantom(InsetMathPhantom::vphantom));
461 if (s == "ensuremath")
462 return MathAtom(new InsetMathEnsureMath);
463 if (isSpecialChar(s))
464 return MathAtom(new InsetMathSpecialChar(s));
467 return MathAtom(new InsetMathHull(hullRegexp));
469 return MathAtom(new MathMacro(s));
473 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
476 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
478 docstring body = split(str, name, ' ');
483 InsetCommandParams icp(REF_CODE);
485 InsetCommand::string2params("ref", to_utf8(str), icp);
486 mathed_parse_cell(ar, icp.getCommand());
490 return ar[0].nucleus();