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 "MathSupport.h"
50 #include "support/debug.h"
52 #include "insets/InsetCommand.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"
62 using namespace lyx::support;
72 typedef map<docstring, latexkeys> WordList;
77 bool math_font_available(docstring & name)
82 // Do we have the font proper?
83 if (theFontLoader().available(f))
87 if (name == "eufrak") {
88 name = from_ascii("lyxfakefrak");
93 "font " << to_utf8(name) << " not available and I can't fake it");
100 FileName const filename = libFileSearch(string(), "symbols");
101 LYXERR(Debug::MATHED, "read symbols from " << filename);
102 if (filename.empty()) {
103 lyxerr << "Could not find symbols file" << endl;
107 ifstream fs(filename.toFilesystemEncoding().c_str());
110 while (getline(fs, line)) {
113 if (line.empty() || line[0] == '#')
116 // special case of iffont/else/endif
117 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
118 istringstream is(line);
122 docstring t = from_utf8(tmp);
123 skip = !math_font_available(t);
125 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
127 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
133 // special case of pre-defined macros
134 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
135 //lyxerr << "macro definition: '" << line << '\'' << endl;
136 istringstream is(line);
139 is >> macro >> requires;
140 MacroTable::globalMacros().insert(from_utf8(macro), requires);
144 idocstringstream is(from_utf8(line));
146 is >> tmp.name >> tmp.inset;
147 if (isFontName(tmp.inset))
148 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
151 // requires is optional
155 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
156 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' '
157 << to_utf8(tmp.extra));
161 if (isFontName(tmp.inset)) {
162 // tmp.inset _is_ the fontname here.
163 // create fallbacks if necessary
165 // store requirements as long as we can
166 if (tmp.requires.empty()) {
167 if (tmp.inset == "msa" || tmp.inset == "msb")
168 tmp.requires = from_ascii("amssymb");
169 else if (tmp.inset == "wasy")
170 tmp.requires = from_ascii("wasysym");
173 // symbol font is not available sometimes
174 docstring symbol_font = from_ascii("lyxsymbol");
176 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
177 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
179 } else if (math_font_available(tmp.inset)) {
180 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
181 tmp.draw.push_back(char_type(charid));
182 } else if (fallbackid && math_font_available(symbol_font)) {
183 if (tmp.inset == "cmex")
184 tmp.inset = from_ascii("lyxsymbol");
186 tmp.inset = from_ascii("lyxboldsymbol");
187 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
188 tmp.draw.push_back(char_type(fallbackid));
190 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
192 tmp.inset = from_ascii("lyxtex");
195 // it's a proper inset
196 LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
197 << " used for " << to_utf8(tmp.name));
200 if (theWordList.find(tmp.name) != theWordList.end())
201 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
202 << " already exists.");
204 theWordList[tmp.name] = tmp;
206 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
207 << " inset: " << to_utf8(tmp.inset)
208 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
209 << " extra: " << to_utf8(tmp.extra)
210 << " requires: " << to_utf8(tmp.requires) << '\'');
212 docstring tmp = from_ascii("cmm");
213 docstring tmp2 = from_ascii("cmsy");
214 has_math_fonts = math_font_available(tmp) && math_font_available(tmp2);
223 static bool initialized = false;
232 latexkeys const * in_word_set(docstring const & str)
234 WordList::iterator it = theWordList.find(str);
235 return it != theWordList.end() ? &(it->second) : 0;
239 MathAtom createInsetMath(char const * const s)
241 return createInsetMath(from_utf8(s));
245 MathAtom createInsetMath(docstring const & s)
247 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
248 latexkeys const * l = in_word_set(s);
250 docstring const & inset = l->inset;
251 //lyxerr << " found inset: '" << inset << '\'' << endl;
253 return MathAtom(new InsetMathRef(l->name));
254 if (inset == "overset")
255 return MathAtom(new InsetMathOverset);
256 if (inset == "underset")
257 return MathAtom(new InsetMathUnderset);
258 if (inset == "decoration")
259 return MathAtom(new InsetMathDecoration(l));
260 if (inset == "space")
261 return MathAtom(new InsetMathSpace(l->name));
263 return MathAtom(new InsetMathDots(l));
265 // return MathAtom(new InsetMathMBox);
266 // InsetMathMBox is proposed to replace InsetMathBox,
267 // but is not ready yet (it needs a BufferView for
269 return MathAtom(new InsetMathBox(l->name));
270 // if (inset == "fbox")
271 // return MathAtom(new InsetMathFBox(l));
272 if (inset == "style")
273 return MathAtom(new InsetMathSize(l));
275 return MathAtom(new InsetMathFont(l));
276 if (inset == "oldfont")
277 return MathAtom(new InsetMathFontOld(l));
278 if (inset == "matrix")
279 return MathAtom(new InsetMathAMSArray(s));
280 if (inset == "split")
281 return MathAtom(new InsetMathSplit(s));
283 // we can't create a InsetMathBig, since the argument
285 return MathAtom(new InsetMathUnknown(s));
286 return MathAtom(new InsetMathSymbol(l));
289 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
290 return MathAtom(new MathMacroArgument(s[1] - '0'));
291 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
292 && s[2] >= '1' && s[2] <= '9')
293 return MathAtom(new MathMacroArgument(s[2] - '0'));
295 return MathAtom(new InsetMathBoxed());
297 return MathAtom(new InsetMathFBox());
299 return MathAtom(new InsetMathFrameBox);
301 return MathAtom(new InsetMathMakebox);
303 return MathAtom(new InsetMathKern);
304 if (s.substr(0, 8) == "xymatrix") {
305 char spacing_code = '\0';
307 size_t const len = s.length();
309 if (i < len && s[i] == '@') {
319 spacing_code = static_cast<char>(s[i]);
324 if (i < len && s[i] == '=') {
326 spacing = Length(to_ascii(s.substr(i)));
329 return MathAtom(new InsetMathXYMatrix(spacing, spacing_code));
331 if (s == "xrightarrow" || s == "xleftarrow")
332 return MathAtom(new InsetMathXArrow(s));
333 if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
334 return MathAtom(new InsetMathSplit(s));
336 return MathAtom(new InsetMathCases);
338 return MathAtom(new InsetMathSubstack);
339 if (s == "subarray" || s == "array")
340 return MathAtom(new InsetMathArray(s, 1, 1));
342 return MathAtom(new InsetMathSqrt);
344 return MathAtom(new InsetMathRoot);
346 return MathAtom(new InsetMathTabular(s, 1, 1));
348 return MathAtom(new InsetMathStackrel);
349 if (s == "binom" || s == "choose")
350 return MathAtom(new InsetMathBinom(s == "choose"));
352 return MathAtom(new InsetMathFrac);
354 return MathAtom(new InsetMathFrac(InsetMathFrac::OVER));
356 return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC));
358 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC));
359 // This string value is only for math toolbar use. Not a LaTeX name
360 if (s == "unitfracthree")
361 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3));
363 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1));
365 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT));
367 // return MathAtom(new MathInferInset);
369 return MathAtom(new InsetMathFrac(InsetMathFrac::ATOP));
371 return MathAtom(new InsetMathLefteqn);
372 if (s == "boldsymbol")
373 return MathAtom(new InsetMathBoldSymbol);
374 if (s == "color" || s == "normalcolor")
375 return MathAtom(new InsetMathColor(true));
376 if (s == "textcolor")
377 return MathAtom(new InsetMathColor(false));
379 return MathAtom(new InsetMathDFrac);
381 return MathAtom(new InsetMathTFrac);
383 return MathAtom(new InsetMathDBinom);
385 return MathAtom(new InsetMathTBinom);
387 return MathAtom(new InsetMathPhantom(InsetMathPhantom::hphantom));
389 return MathAtom(new InsetMathPhantom(InsetMathPhantom::phantom));
391 return MathAtom(new InsetMathPhantom(InsetMathPhantom::vphantom));
393 return MathAtom(new MathMacro(s));
397 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
400 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
402 docstring body = split(str, name, ' ');
407 InsetCommandParams icp(REF_CODE);
409 InsetCommandMailer::string2params("ref", to_utf8(str), icp);
410 mathed_parse_cell(ar, icp.getCommand());
414 return ar[0].nucleus();