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 "InsetMathBinom.h"
18 #include "InsetMathBoldSymbol.h"
19 #include "InsetMathBoxed.h"
20 #include "InsetMathBox.h"
21 #include "InsetMathCases.h"
22 #include "InsetMathColor.h"
23 #include "InsetMathDecoration.h"
24 #include "InsetMathDFrac.h"
25 #include "InsetMathDots.h"
26 #include "InsetMathFBox.h"
27 #include "InsetMathFont.h"
28 #include "InsetMathFontOld.h"
29 #include "InsetMathFrac.h"
30 #include "InsetMathFrameBox.h"
31 #include "InsetMathKern.h"
32 #include "InsetMathLefteqn.h"
33 #include "MathMacro.h"
34 #include "InsetMathMakebox.h"
35 #include "InsetMathOverset.h"
36 #include "InsetMathPhantom.h"
37 #include "InsetMathRef.h"
38 #include "InsetMathRoot.h"
39 #include "InsetMathSize.h"
40 #include "InsetMathSpace.h"
41 #include "InsetMathSplit.h"
42 #include "InsetMathSqrt.h"
43 #include "InsetMathStackrel.h"
44 #include "InsetMathSubstack.h"
45 #include "InsetMathSymbol.h"
46 #include "InsetMathTabular.h"
47 #include "InsetMathTFrac.h"
48 #include "InsetMathUnderset.h"
49 #include "InsetMathUnknown.h"
50 #include "InsetMathXArrow.h"
51 #include "InsetMathXYMatrix.h"
52 #include "MathMacroArgument.h"
53 #include "MathMacroTable.h"
54 #include "MathParser.h"
55 #include "MathSupport.h"
59 #include "insets/InsetCommand.h"
61 #include "support/filetools.h" // LibFileSearch
62 #include "support/lstrings.h"
64 #include "frontends/FontLoader.h"
69 using support::libFileSearch;
74 using std::istringstream;
83 typedef std::map<docstring, latexkeys> WordList;
88 bool math_font_available(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"
112 support::FileName const filename = libFileSearch(string(), "symbols");
113 LYXERR(Debug::MATHED) << "read symbols from " << filename << endl;
114 if (filename.empty()) {
115 lyxerr << "Could not find symbols file" << endl;
119 std::ifstream fs(filename.toFilesystemEncoding().c_str());
122 while (getline(fs, line)) {
125 if (line.empty() || line[0] == '#')
128 // special case of iffont/else/endif
129 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
130 istringstream is(line);
134 docstring t = from_utf8(tmp);
135 skip = !math_font_available(t);
137 } 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(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
167 LYXERR(Debug::MATHED) << "skipping line '" << line << '\'' << endl;
168 LYXERR(Debug::MATHED)
169 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' ' << to_utf8(tmp.extra) << endl;
173 if (isFontName(tmp.inset)) {
174 // tmp.inset _is_ the fontname here.
175 // create fallbacks if necessary
177 // store requirements as long as we can
178 if (tmp.requires.empty() &&
179 (tmp.inset == "msa" || tmp.inset == "msb"))
180 tmp.requires = from_ascii("amssymb");
181 else if (tmp.inset == "wasy")
182 tmp.requires = from_ascii("wasysym");
184 // symbol font is not available sometimes
185 docstring symbol_font = from_ascii("lyxsymbol");
187 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
188 LYXERR(Debug::MATHED) << "symbol abuse for " << to_utf8(tmp.name) << endl;
190 } else if (math_font_available(tmp.inset)) {
191 LYXERR(Debug::MATHED) << "symbol available for " << to_utf8(tmp.name) << endl;
192 tmp.draw.push_back(char_type(charid));
193 } else if (fallbackid && math_font_available(symbol_font)) {
194 if (tmp.inset == "cmex")
195 tmp.inset = from_ascii("lyxsymbol");
197 tmp.inset = from_ascii("lyxboldsymbol");
198 LYXERR(Debug::MATHED) << "symbol fallback for " << to_utf8(tmp.name) << endl;
199 tmp.draw.push_back(char_type(fallbackid));
201 LYXERR(Debug::MATHED) << "faking " << to_utf8(tmp.name) << endl;
203 tmp.inset = from_ascii("lyxtex");
206 // it's a proper inset
207 LYXERR(Debug::MATHED) << "inset " << to_utf8(tmp.inset)
208 << " used for " << to_utf8(tmp.name)
212 if (theWordList.find(tmp.name) != theWordList.end())
213 LYXERR(Debug::MATHED)
214 << "readSymbols: inset " << to_utf8(tmp.name)
215 << " already exists." << endl;
217 theWordList[tmp.name] = tmp;
219 LYXERR(Debug::MATHED)
220 << "read symbol '" << to_utf8(tmp.name)
221 << " inset: " << to_utf8(tmp.inset)
222 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
223 << " extra: " << to_utf8(tmp.extra)
224 << " requires: " << to_utf8(tmp.requires)
227 docstring tmp = from_ascii("cmm");
228 docstring tmp2 = from_ascii("cmsy");
229 has_math_fonts = math_font_available(tmp) && math_font_available(tmp2);
238 static bool initialized = false;
247 latexkeys const * in_word_set(docstring const & str)
249 WordList::iterator it = theWordList.find(str);
250 return it != theWordList.end() ? &(it->second) : 0;
254 MathAtom createInsetMath(char const * const s)
256 return createInsetMath(from_utf8(s));
260 MathAtom createInsetMath(docstring const & s)
262 //lyxerr << "creating inset with name: '" << s << '\'' << endl;
263 latexkeys const * l = in_word_set(s);
265 docstring const & inset = l->inset;
266 //lyxerr << " found inset: '" << inset << '\'' << endl;
268 return MathAtom(new RefInset(l->name));
269 if (inset == "overset")
270 return MathAtom(new InsetMathOverset);
271 if (inset == "underset")
272 return MathAtom(new InsetMathUnderset);
273 if (inset == "decoration")
274 return MathAtom(new InsetMathDecoration(l));
275 if (inset == "space")
276 return MathAtom(new InsetMathSpace(l->name));
278 return MathAtom(new InsetMathDots(l));
280 // return MathAtom(new InsetMathMBox);
281 // InsetMathMBox is proposed to replace InsetMathBox,
282 // but is not ready yet (it needs a BufferView for
284 return MathAtom(new InsetMathBox(l->name));
285 // if (inset == "fbox")
286 // return MathAtom(new InsetMathFBox(l));
287 if (inset == "style")
288 return MathAtom(new InsetMathSize(l));
290 return MathAtom(new InsetMathFont(l));
291 if (inset == "oldfont")
292 return MathAtom(new InsetMathFontOld(l));
293 if (inset == "matrix")
294 return MathAtom(new InsetMathAMSArray(s));
295 if (inset == "split")
296 return MathAtom(new InsetMathSplit(s));
298 // we can't create a InsetMathBig, since the argument
300 return MathAtom(new InsetMathUnknown(s));
301 return MathAtom(new InsetMathSymbol(l));
304 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
305 return MathAtom(new MathMacroArgument(s[1] - '0'));
306 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
307 && s[2] >= '1' && s[2] <= '9')
308 return MathAtom(new MathMacroArgument(s[2] - '0'));
310 return MathAtom(new InsetMathBoxed());
312 return MathAtom(new InsetMathFBox());
314 return MathAtom(new InsetMathFrameBox);
316 return MathAtom(new InsetMathMakebox);
318 return MathAtom(new InsetMathKern);
319 if (s.substr(0, 8) == "xymatrix") {
320 char spacing_code = '\0';
322 size_t const len = s.length();
324 if (i < len && s[i] == '@') {
334 spacing_code = static_cast<char>(s[i]);
339 if (i < len && s[i] == '=') {
341 spacing = LyXLength(to_ascii(s.substr(i)));
344 return MathAtom(new InsetMathXYMatrix(spacing, spacing_code));
346 if (s == "xrightarrow" || s == "xleftarrow")
347 return MathAtom(new InsetMathXArrow(s));
348 if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
349 return MathAtom(new InsetMathSplit(s));
351 return MathAtom(new InsetMathCases);
353 return MathAtom(new InsetMathSubstack);
354 if (s == "subarray" || s == "array")
355 return MathAtom(new InsetMathArray(s, 1, 1));
357 return MathAtom(new InsetMathSqrt);
359 return MathAtom(new InsetMathRoot);
361 return MathAtom(new InsetMathTabular(s, 1, 1));
363 return MathAtom(new InsetMathStackrel);
364 if (s == "binom" || s == "choose")
365 return MathAtom(new InsetMathBinom(s == "choose"));
367 return MathAtom(new InsetMathFrac);
369 return MathAtom(new InsetMathFrac(InsetMathFrac::OVER));
371 return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC));
373 // return MathAtom(new MathInferInset);
375 return MathAtom(new InsetMathFrac(InsetMathFrac::ATOP));
377 return MathAtom(new InsetMathLefteqn);
378 if (s == "boldsymbol")
379 return MathAtom(new InsetMathBoldSymbol);
380 if (s == "color" || s == "normalcolor")
381 return MathAtom(new InsetMathColor(true));
382 if (s == "textcolor")
383 return MathAtom(new InsetMathColor(false));
385 return MathAtom(new InsetMathDFrac);
387 return MathAtom(new InsetMathTFrac);
389 return MathAtom(new InsetMathPhantom(InsetMathPhantom::hphantom));
391 return MathAtom(new InsetMathPhantom(InsetMathPhantom::phantom));
393 return MathAtom(new InsetMathPhantom(InsetMathPhantom::vphantom));
395 if (MacroTable::globalMacros().has(s))
396 return MathAtom(new MathMacro(s,
397 MacroTable::globalMacros().get(s).numargs()));
398 //if (MacroTable::localMacros().has(s))
399 // return MathAtom(new MathMacro(s,
400 // MacroTable::localMacros().get(s).numargs()));
402 //lyxerr << "creating unknown inset '" << s << "'" << endl;
403 return MathAtom(new InsetMathUnknown(s));
407 bool createInsetMath_fromDialogStr(docstring const & str, MathArray & ar)
410 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
412 docstring body = split(str, name, ' ');
417 InsetCommandParams icp("ref");
419 InsetCommandMailer::string2params("ref", to_utf8(str), icp);
420 mathed_parse_cell(ar, icp.getCommand());
424 return ar[0].nucleus();