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 "InsetMathSplit.h"
36 #include "InsetMathSqrt.h"
37 #include "InsetMathStackrel.h"
38 #include "InsetMathSubstack.h"
39 #include "InsetMathSymbol.h"
40 #include "InsetMathTabular.h"
41 #include "InsetMathUnderset.h"
42 #include "InsetMathUnknown.h"
43 #include "InsetMathXArrow.h"
44 #include "InsetMathXYMatrix.h"
45 #include "MacroTable.h"
46 #include "MathMacro.h"
47 #include "MathMacroArgument.h"
48 #include "MathParser.h"
49 #include "MathStream.h"
50 #include "MathSupport.h"
52 #include "insets/InsetCommand.h"
54 #include "support/debug.h"
55 #include "support/docstream.h"
56 #include "support/FileName.h"
57 #include "support/filetools.h" // LibFileSearch
58 #include "support/lstrings.h"
60 #include "frontends/FontLoader.h"
62 #include "LyX.h" // use_gui
65 using namespace lyx::support;
74 MathWordList theWordList;
77 bool isMathFontAvailable(docstring & name)
85 // Do we have the font proper?
86 if (theFontLoader().available(f))
90 if (name == "eufrak") {
91 name = from_ascii("lyxfakefrak");
96 "font " << to_utf8(name) << " not available and I can't fake it");
103 FileName const filename = libFileSearch(string(), "symbols");
104 LYXERR(Debug::MATHED, "read symbols from " << filename);
105 if (filename.empty()) {
106 lyxerr << "Could not find symbols file" << endl;
110 ifstream fs(filename.toFilesystemEncoding().c_str());
113 while (getline(fs, line)) {
116 if (line.empty() || line[0] == '#')
119 // special case of iffont/else/endif
120 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
121 istringstream is(line);
125 docstring t = from_utf8(tmp);
126 skip = !isMathFontAvailable(t);
128 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
130 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
136 // special case of pre-defined macros
137 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
138 //lyxerr << "macro definition: '" << line << '\'' << endl;
139 istringstream is(line);
142 is >> macro >> requires;
143 MacroTable::globalMacros().insert(from_utf8(macro), requires);
147 idocstringstream is(from_utf8(line));
149 is >> tmp.name >> tmp.inset;
150 if (isFontName(tmp.inset))
151 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
154 // requires is optional
158 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
159 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' '
160 << to_utf8(tmp.extra));
164 if (isFontName(tmp.inset)) {
165 // tmp.inset _is_ the fontname here.
166 // create fallbacks if necessary
168 // store requirements as long as we can
169 if (tmp.requires.empty()) {
170 if (tmp.inset == "msa" || tmp.inset == "msb")
171 tmp.requires = from_ascii("amssymb");
172 else if (tmp.inset == "wasy")
173 tmp.requires = from_ascii("wasysym");
176 // symbol font is not available sometimes
177 docstring symbol_font = from_ascii("lyxsymbol");
179 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
180 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
182 } else if (isMathFontAvailable(tmp.inset)) {
183 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
184 tmp.draw.push_back(char_type(charid));
185 } else if (fallbackid && isMathFontAvailable(symbol_font)) {
186 if (tmp.inset == "cmex")
187 tmp.inset = from_ascii("lyxsymbol");
189 tmp.inset = from_ascii("lyxboldsymbol");
190 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
191 tmp.draw.push_back(char_type(fallbackid));
193 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
195 tmp.inset = from_ascii("lyxtex");
198 // it's a proper inset
199 LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
200 << " used for " << to_utf8(tmp.name));
203 if (theWordList.find(tmp.name) != theWordList.end())
204 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
205 << " already exists.");
207 theWordList[tmp.name] = tmp;
209 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
210 << " inset: " << to_utf8(tmp.inset)
211 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
212 << " extra: " << to_utf8(tmp.extra)
213 << " requires: " << to_utf8(tmp.requires) << '\'');
215 docstring tmp = from_ascii("cmm");
216 docstring tmp2 = from_ascii("cmsy");
217 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
223 MathWordList const & mathedWordList()
231 static bool initialized = false;
240 bool ensureMath(WriteStream & os, bool needs_math_mode, bool macro)
242 bool brace = os.pendingBrace();
243 os.pendingBrace(false);
246 if (os.textMode() && needs_math_mode) {
247 os << "\\ensuremath{";
250 } else if (macro && brace && !needs_math_mode) {
251 // This is a user defined macro, but not a MathMacro, so we
252 // cannot be sure what mode is needed. As it was entered in
253 // a text box, we restore the text mode.
262 bool ensureMode(WriteStream & os, InsetMath::mode_type mode)
264 bool textmode = mode == InsetMath::TEXT_MODE;
265 if (os.latex() && textmode && os.pendingBrace()) {
267 os.pendingBrace(false);
268 os.pendingSpace(false);
271 bool oldmode = os.textMode();
272 os.textMode(textmode);
277 latexkeys const * in_word_set(docstring const & str)
279 MathWordList::iterator it = theWordList.find(str);
280 return it != theWordList.end() ? &(it->second) : 0;
284 MathAtom createInsetMath(char const * const s)
286 return createInsetMath(from_utf8(s));
290 MathAtom createInsetMath(docstring const & s)
292 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
293 latexkeys const * l = in_word_set(s);
295 docstring const & inset = l->inset;
296 //lyxerr << " found inset: '" << inset << '\'' << endl;
298 return MathAtom(new InsetMathRef(l->name));
299 if (inset == "overset")
300 return MathAtom(new InsetMathOverset);
301 if (inset == "underset")
302 return MathAtom(new InsetMathUnderset);
303 if (inset == "decoration")
304 return MathAtom(new InsetMathDecoration(l));
305 if (inset == "space")
306 return MathAtom(new InsetMathSpace(l->name));
308 return MathAtom(new InsetMathDots(l));
310 // return MathAtom(new InsetMathMBox);
311 // InsetMathMBox is proposed to replace InsetMathBox,
312 // but is not ready yet (it needs a BufferView for
314 return MathAtom(new InsetMathBox(l->name));
315 // if (inset == "fbox")
316 // return MathAtom(new InsetMathFBox(l));
317 if (inset == "style")
318 return MathAtom(new InsetMathSize(l));
320 return MathAtom(new InsetMathFont(l));
321 if (inset == "oldfont")
322 return MathAtom(new InsetMathFontOld(l));
323 if (inset == "matrix")
324 return MathAtom(new InsetMathAMSArray(s));
325 if (inset == "split")
326 return MathAtom(new InsetMathSplit(s));
328 // we can't create a InsetMathBig, since the argument
330 return MathAtom(new InsetMathUnknown(s));
331 return MathAtom(new InsetMathSymbol(l));
334 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
335 return MathAtom(new MathMacroArgument(s[1] - '0'));
336 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
337 && s[2] >= '1' && s[2] <= '9')
338 return MathAtom(new MathMacroArgument(s[2] - '0'));
340 return MathAtom(new InsetMathBoxed());
342 return MathAtom(new InsetMathFBox());
344 return MathAtom(new InsetMathMakebox(true));
346 return MathAtom(new InsetMathMakebox(false));
348 return MathAtom(new InsetMathKern);
349 if (s.substr(0, 8) == "xymatrix") {
350 char spacing_code = '\0';
352 size_t const len = s.length();
354 if (i < len && s[i] == '@') {
364 spacing_code = static_cast<char>(s[i]);
369 if (i < len && s[i] == '=') {
371 spacing = Length(to_ascii(s.substr(i)));
374 return MathAtom(new InsetMathXYMatrix(spacing, spacing_code));
376 if (s == "xrightarrow" || s == "xleftarrow")
377 return MathAtom(new InsetMathXArrow(s));
378 if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
379 return MathAtom(new InsetMathSplit(s));
381 return MathAtom(new InsetMathCases);
383 return MathAtom(new InsetMathSubstack);
384 if (s == "subarray" || s == "array")
385 return MathAtom(new InsetMathArray(s, 1, 1));
387 return MathAtom(new InsetMathSqrt);
389 return MathAtom(new InsetMathRoot);
391 return MathAtom(new InsetMathTabular(s, 1, 1));
393 return MathAtom(new InsetMathStackrel);
395 return MathAtom(new InsetMathBinom(InsetMathBinom::BINOM));
397 return MathAtom(new InsetMathBinom(InsetMathBinom::CHOOSE));
399 return MathAtom(new InsetMathBinom(InsetMathBinom::BRACE));
401 return MathAtom(new InsetMathBinom(InsetMathBinom::BRACK));
403 return MathAtom(new InsetMathFrac);
405 return MathAtom(new InsetMathFrac(InsetMathFrac::OVER));
407 return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC));
409 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC));
410 // This string value is only for math toolbar use. Not a LaTeX name
411 if (s == "unitfracthree")
412 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3));
414 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1));
416 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT));
418 // return MathAtom(new MathInferInset);
420 return MathAtom(new InsetMathFrac(InsetMathFrac::ATOP));
422 return MathAtom(new InsetMathLefteqn);
423 if (s == "boldsymbol")
424 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::AMS_BOLD));
426 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::BM_BOLD));
427 if (s == "heavysymbol" || s == "hm")
428 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::BM_HEAVY));
429 if (s == "color" || s == "normalcolor")
430 return MathAtom(new InsetMathColor(true));
431 if (s == "textcolor")
432 return MathAtom(new InsetMathColor(false));
434 return MathAtom(new InsetMathDFrac);
436 return MathAtom(new InsetMathTFrac);
438 return MathAtom(new InsetMathDBinom);
440 return MathAtom(new InsetMathTBinom);
442 return MathAtom(new InsetMathPhantom(InsetMathPhantom::hphantom));
444 return MathAtom(new InsetMathPhantom(InsetMathPhantom::phantom));
446 return MathAtom(new InsetMathPhantom(InsetMathPhantom::vphantom));
447 if (s == "ensuremath")
448 return MathAtom(new InsetMathEnsureMath);
450 return MathAtom(new MathMacro(s));
454 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
457 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
459 docstring body = split(str, name, ' ');
464 InsetCommandParams icp(REF_CODE);
466 InsetCommand::string2params("ref", to_utf8(str), icp);
467 mathed_parse_cell(ar, icp.getCommand());
471 return ar[0].nucleus();