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 "InsetMathFont.h"
24 #include "InsetMathFontOld.h"
25 #include "InsetMathFrac.h"
26 #include "InsetMathKern.h"
27 #include "InsetMathLefteqn.h"
28 #include "InsetMathOverset.h"
29 #include "InsetMathPhantom.h"
30 #include "InsetMathRef.h"
31 #include "InsetMathRoot.h"
32 #include "InsetMathSize.h"
33 #include "InsetMathSpace.h"
34 #include "InsetMathSplit.h"
35 #include "InsetMathSqrt.h"
36 #include "InsetMathStackrel.h"
37 #include "InsetMathSubstack.h"
38 #include "InsetMathSymbol.h"
39 #include "InsetMathTabular.h"
40 #include "InsetMathUnderset.h"
41 #include "InsetMathUnknown.h"
42 #include "InsetMathXArrow.h"
43 #include "InsetMathXYMatrix.h"
44 #include "MacroTable.h"
45 #include "MathMacro.h"
46 #include "MathMacroArgument.h"
47 #include "MathParser.h"
48 #include "MathStream.h"
49 #include "MathSupport.h"
51 #include "insets/InsetCommand.h"
53 #include "support/debug.h"
54 #include "support/docstream.h"
55 #include "support/FileName.h"
56 #include "support/filetools.h" // LibFileSearch
57 #include "support/lstrings.h"
59 #include "frontends/FontLoader.h"
61 #include "LyX.h" // use_gui
64 using namespace lyx::support;
73 MathWordList theWordList;
76 bool isMathFontAvailable(docstring & name)
81 // Do we have the font proper?
82 if (theFontLoader().available(f))
86 if (name == "eufrak") {
87 name = from_ascii("lyxfakefrak");
92 "font " << to_utf8(name) << " not available and I can't fake it");
102 FileName const filename = libFileSearch(string(), "symbols");
103 LYXERR(Debug::MATHED, "read symbols from " << filename);
104 if (filename.empty()) {
105 lyxerr << "Could not find symbols file" << endl;
109 ifstream fs(filename.toFilesystemEncoding().c_str());
112 while (getline(fs, line)) {
115 if (line.empty() || line[0] == '#')
118 // special case of iffont/else/endif
119 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
120 istringstream is(line);
124 docstring t = from_utf8(tmp);
125 skip = !isMathFontAvailable(t);
127 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
129 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
135 // special case of pre-defined macros
136 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
137 //lyxerr << "macro definition: '" << line << '\'' << endl;
138 istringstream is(line);
141 is >> macro >> requires;
142 MacroTable::globalMacros().insert(from_utf8(macro), requires);
146 idocstringstream is(from_utf8(line));
148 is >> tmp.name >> tmp.inset;
149 if (isFontName(tmp.inset))
150 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
153 // requires is optional
157 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
158 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' '
159 << to_utf8(tmp.extra));
163 if (isFontName(tmp.inset)) {
164 // tmp.inset _is_ the fontname here.
165 // create fallbacks if necessary
167 // store requirements as long as we can
168 if (tmp.requires.empty()) {
169 if (tmp.inset == "msa" || tmp.inset == "msb")
170 tmp.requires = from_ascii("amssymb");
171 else if (tmp.inset == "wasy")
172 tmp.requires = from_ascii("wasysym");
175 // symbol font is not available sometimes
176 docstring symbol_font = from_ascii("lyxsymbol");
178 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
179 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
181 } else if (isMathFontAvailable(tmp.inset)) {
182 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
183 tmp.draw.push_back(char_type(charid));
184 } else if (fallbackid && isMathFontAvailable(symbol_font)) {
185 if (tmp.inset == "cmex")
186 tmp.inset = from_ascii("lyxsymbol");
188 tmp.inset = from_ascii("lyxboldsymbol");
189 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
190 tmp.draw.push_back(char_type(fallbackid));
192 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
194 tmp.inset = from_ascii("lyxtex");
197 // it's a proper inset
198 LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
199 << " used for " << to_utf8(tmp.name));
202 if (theWordList.find(tmp.name) != theWordList.end())
203 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
204 << " already exists.");
206 theWordList[tmp.name] = tmp;
208 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
209 << " inset: " << to_utf8(tmp.inset)
210 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
211 << " extra: " << to_utf8(tmp.extra)
212 << " requires: " << to_utf8(tmp.requires) << '\'');
214 docstring tmp = from_ascii("cmm");
215 docstring tmp2 = from_ascii("cmsy");
216 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
222 MathWordList const & mathedWordList()
230 static bool initialized = false;
239 bool ensureMath(WriteStream & os, bool needs_math_mode, bool macro)
241 bool brace = os.pendingBrace();
242 os.pendingBrace(false);
245 if (os.textMode() && needs_math_mode) {
246 os << "\\ensuremath{";
249 } else if (macro && brace && !needs_math_mode) {
250 // This is a user defined macro, but not a MathMacro, so we
251 // cannot be sure what mode is needed. As it was entered in
252 // a text box, we restore the text mode.
261 bool ensureMode(WriteStream & os, InsetMath::mode_type mode)
263 bool textmode = mode == InsetMath::TEXT_MODE;
264 if (os.latex() && textmode && os.pendingBrace()) {
266 os.pendingBrace(false);
267 os.pendingSpace(false);
270 bool oldmode = os.textMode();
271 os.textMode(textmode);
276 latexkeys const * in_word_set(docstring const & str)
278 MathWordList::iterator it = theWordList.find(str);
279 return it != theWordList.end() ? &(it->second) : 0;
283 MathAtom createInsetMath(char const * const s)
285 return createInsetMath(from_utf8(s));
289 MathAtom createInsetMath(docstring const & s)
291 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
292 latexkeys const * l = in_word_set(s);
294 docstring const & inset = l->inset;
295 //lyxerr << " found inset: '" << inset << '\'' << endl;
297 return MathAtom(new InsetMathRef(l->name));
298 if (inset == "overset")
299 return MathAtom(new InsetMathOverset);
300 if (inset == "underset")
301 return MathAtom(new InsetMathUnderset);
302 if (inset == "decoration")
303 return MathAtom(new InsetMathDecoration(l));
304 if (inset == "space")
305 return MathAtom(new InsetMathSpace(l->name));
307 return MathAtom(new InsetMathDots(l));
309 // return MathAtom(new InsetMathMBox);
310 // InsetMathMBox is proposed to replace InsetMathBox,
311 // but is not ready yet (it needs a BufferView for
313 return MathAtom(new InsetMathBox(l->name));
314 // if (inset == "fbox")
315 // return MathAtom(new InsetMathFBox(l));
316 if (inset == "style")
317 return MathAtom(new InsetMathSize(l));
319 return MathAtom(new InsetMathFont(l));
320 if (inset == "oldfont")
321 return MathAtom(new InsetMathFontOld(l));
322 if (inset == "matrix")
323 return MathAtom(new InsetMathAMSArray(s));
324 if (inset == "split")
325 return MathAtom(new InsetMathSplit(s));
327 // we can't create a InsetMathBig, since the argument
329 return MathAtom(new InsetMathUnknown(s));
330 return MathAtom(new InsetMathSymbol(l));
333 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
334 return MathAtom(new MathMacroArgument(s[1] - '0'));
335 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
336 && s[2] >= '1' && s[2] <= '9')
337 return MathAtom(new MathMacroArgument(s[2] - '0'));
339 return MathAtom(new InsetMathBoxed());
341 return MathAtom(new InsetMathFBox());
343 return MathAtom(new InsetMathMakebox(true));
345 return MathAtom(new InsetMathMakebox(false));
347 return MathAtom(new InsetMathKern);
348 if (s.substr(0, 8) == "xymatrix") {
349 char spacing_code = '\0';
351 size_t const len = s.length();
353 if (i < len && s[i] == '@') {
363 spacing_code = static_cast<char>(s[i]);
368 if (i < len && s[i] == '=') {
370 spacing = Length(to_ascii(s.substr(i)));
373 return MathAtom(new InsetMathXYMatrix(spacing, spacing_code));
375 if (s == "xrightarrow" || s == "xleftarrow")
376 return MathAtom(new InsetMathXArrow(s));
377 if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
378 return MathAtom(new InsetMathSplit(s));
380 return MathAtom(new InsetMathCases);
382 return MathAtom(new InsetMathSubstack);
383 if (s == "subarray" || s == "array")
384 return MathAtom(new InsetMathArray(s, 1, 1));
386 return MathAtom(new InsetMathSqrt);
388 return MathAtom(new InsetMathRoot);
390 return MathAtom(new InsetMathTabular(s, 1, 1));
392 return MathAtom(new InsetMathStackrel);
394 return MathAtom(new InsetMathBinom(InsetMathBinom::BINOM));
396 return MathAtom(new InsetMathBinom(InsetMathBinom::CHOOSE));
398 return MathAtom(new InsetMathBinom(InsetMathBinom::BRACE));
400 return MathAtom(new InsetMathBinom(InsetMathBinom::BRACK));
402 return MathAtom(new InsetMathFrac);
404 return MathAtom(new InsetMathFrac(InsetMathFrac::OVER));
406 return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC));
408 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC));
409 // This string value is only for math toolbar use. Not a LaTeX name
410 if (s == "unitfracthree")
411 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3));
413 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1));
415 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT));
417 // return MathAtom(new MathInferInset);
419 return MathAtom(new InsetMathFrac(InsetMathFrac::ATOP));
421 return MathAtom(new InsetMathLefteqn);
422 if (s == "boldsymbol")
423 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::AMS_BOLD));
425 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::BM_BOLD));
426 if (s == "heavysymbol" || s == "hm")
427 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::BM_HEAVY));
428 if (s == "color" || s == "normalcolor")
429 return MathAtom(new InsetMathColor(true));
430 if (s == "textcolor")
431 return MathAtom(new InsetMathColor(false));
433 return MathAtom(new InsetMathDFrac);
435 return MathAtom(new InsetMathTFrac);
437 return MathAtom(new InsetMathDBinom);
439 return MathAtom(new InsetMathTBinom);
441 return MathAtom(new InsetMathPhantom(InsetMathPhantom::hphantom));
443 return MathAtom(new InsetMathPhantom(InsetMathPhantom::phantom));
445 return MathAtom(new InsetMathPhantom(InsetMathPhantom::vphantom));
447 return MathAtom(new MathMacro(s));
451 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
454 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
456 docstring body = split(str, name, ' ');
461 InsetCommandParams icp(REF_CODE);
463 InsetCommand::string2params("ref", to_utf8(str), icp);
464 mathed_parse_cell(ar, icp.getCommand());
468 return ar[0].nucleus();