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 "InsetMathBoxed.h"
19 #include "InsetMathBox.h"
20 #include "InsetMathCases.h"
21 #include "InsetMathColor.h"
22 #include "InsetMathDecoration.h"
23 #include "InsetMathDots.h"
24 #include "InsetMathFBox.h"
25 #include "InsetMathFont.h"
26 #include "InsetMathFontOld.h"
27 #include "InsetMathFrac.h"
28 #include "InsetMathFrameBox.h"
29 #include "InsetMathKern.h"
30 #include "InsetMathLefteqn.h"
31 #include "InsetMathMakebox.h"
32 #include "InsetMathOverset.h"
33 #include "InsetMathPhantom.h"
34 #include "InsetMathRef.h"
35 #include "InsetMathRoot.h"
36 #include "InsetMathSize.h"
37 #include "InsetMathSpace.h"
38 #include "InsetMathSplit.h"
39 #include "InsetMathSqrt.h"
40 #include "InsetMathStackrel.h"
41 #include "InsetMathSubstack.h"
42 #include "InsetMathSymbol.h"
43 #include "InsetMathTabular.h"
44 #include "InsetMathUnderset.h"
45 #include "InsetMathUnknown.h"
46 #include "InsetMathXArrow.h"
47 #include "InsetMathXYMatrix.h"
48 #include "MacroTable.h"
49 #include "MathMacro.h"
50 #include "MathMacroArgument.h"
51 #include "MathParser.h"
52 #include "MathSupport.h"
56 #include "insets/InsetCommand.h"
58 #include "support/docstream.h"
59 #include "support/filetools.h" // LibFileSearch
60 #include "support/lstrings.h"
62 #include "frontends/FontLoader.h"
67 using support::libFileSearch;
72 using std::istringstream;
81 typedef std::map<docstring, latexkeys> WordList;
86 bool math_font_available(docstring & name)
91 // Do we have the font proper?
92 if (theFontLoader().available(f))
96 if (name == "eufrak") {
97 name = from_ascii("lyxfakefrak");
101 LYXERR(Debug::MATHED)
102 << "font " << to_utf8(name) << " not available and I can't fake it"
110 support::FileName const filename = libFileSearch(string(), "symbols");
111 LYXERR(Debug::MATHED) << "read symbols from " << filename << endl;
112 if (filename.empty()) {
113 lyxerr << "Could not find symbols file" << endl;
117 std::ifstream fs(filename.toFilesystemEncoding().c_str());
120 while (getline(fs, line)) {
123 if (line.empty() || line[0] == '#')
126 // special case of iffont/else/endif
127 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
128 istringstream is(line);
132 docstring t = from_utf8(tmp);
133 skip = !math_font_available(t);
135 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
137 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
143 // special case of pre-defined macros
144 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
145 //lyxerr << "macro definition: '" << line << '\'' << endl;
146 istringstream is(line);
149 is >> macro >> requires;
150 MacroTable::globalMacros().insert(from_utf8(macro), requires);
154 idocstringstream is(from_utf8(line));
156 is >> tmp.name >> tmp.inset;
157 if (isFontName(tmp.inset))
158 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
161 // requires is optional
165 LYXERR(Debug::MATHED) << "skipping line '" << line << '\'' << endl;
166 LYXERR(Debug::MATHED)
167 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' ' << to_utf8(tmp.extra) << endl;
171 if (isFontName(tmp.inset)) {
172 // tmp.inset _is_ the fontname here.
173 // create fallbacks if necessary
175 // store requirements as long as we can
176 if (tmp.requires.empty()) {
177 if (tmp.inset == "msa" || tmp.inset == "msb")
178 tmp.requires = from_ascii("amssymb");
179 else if (tmp.inset == "wasy")
180 tmp.requires = from_ascii("wasysym");
183 // symbol font is not available sometimes
184 docstring symbol_font = from_ascii("lyxsymbol");
186 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
187 LYXERR(Debug::MATHED) << "symbol abuse for " << to_utf8(tmp.name) << endl;
189 } else if (math_font_available(tmp.inset)) {
190 LYXERR(Debug::MATHED) << "symbol available for " << to_utf8(tmp.name) << endl;
191 tmp.draw.push_back(char_type(charid));
192 } else if (fallbackid && math_font_available(symbol_font)) {
193 if (tmp.inset == "cmex")
194 tmp.inset = from_ascii("lyxsymbol");
196 tmp.inset = from_ascii("lyxboldsymbol");
197 LYXERR(Debug::MATHED) << "symbol fallback for " << to_utf8(tmp.name) << endl;
198 tmp.draw.push_back(char_type(fallbackid));
200 LYXERR(Debug::MATHED) << "faking " << to_utf8(tmp.name) << endl;
202 tmp.inset = from_ascii("lyxtex");
205 // it's a proper inset
206 LYXERR(Debug::MATHED) << "inset " << to_utf8(tmp.inset)
207 << " used for " << to_utf8(tmp.name)
211 if (theWordList.find(tmp.name) != theWordList.end())
212 LYXERR(Debug::MATHED)
213 << "readSymbols: inset " << to_utf8(tmp.name)
214 << " already exists." << endl;
216 theWordList[tmp.name] = tmp;
218 LYXERR(Debug::MATHED)
219 << "read symbol '" << to_utf8(tmp.name)
220 << " inset: " << to_utf8(tmp.inset)
221 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
222 << " extra: " << to_utf8(tmp.extra)
223 << " requires: " << to_utf8(tmp.requires)
226 docstring tmp = from_ascii("cmm");
227 docstring tmp2 = from_ascii("cmsy");
228 has_math_fonts = math_font_available(tmp) && math_font_available(tmp2);
237 static bool initialized = false;
246 latexkeys const * in_word_set(docstring const & str)
248 WordList::iterator it = theWordList.find(str);
249 return it != theWordList.end() ? &(it->second) : 0;
253 MathAtom createInsetMath(char const * const s)
255 return createInsetMath(from_utf8(s));
259 MathAtom createInsetMath(docstring const & s)
261 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
262 latexkeys const * l = in_word_set(s);
264 docstring const & inset = l->inset;
265 //lyxerr << " found inset: '" << inset << '\'' << endl;
267 return MathAtom(new InsetMathRef(l->name));
268 if (inset == "overset")
269 return MathAtom(new InsetMathOverset);
270 if (inset == "underset")
271 return MathAtom(new InsetMathUnderset);
272 if (inset == "decoration")
273 return MathAtom(new InsetMathDecoration(l));
274 if (inset == "space")
275 return MathAtom(new InsetMathSpace(l->name));
277 return MathAtom(new InsetMathDots(l));
279 // return MathAtom(new InsetMathMBox);
280 // InsetMathMBox is proposed to replace InsetMathBox,
281 // but is not ready yet (it needs a BufferView for
283 return MathAtom(new InsetMathBox(l->name));
284 // if (inset == "fbox")
285 // return MathAtom(new InsetMathFBox(l));
286 if (inset == "style")
287 return MathAtom(new InsetMathSize(l));
289 return MathAtom(new InsetMathFont(l));
290 if (inset == "oldfont")
291 return MathAtom(new InsetMathFontOld(l));
292 if (inset == "matrix")
293 return MathAtom(new InsetMathAMSArray(s));
294 if (inset == "split")
295 return MathAtom(new InsetMathSplit(s));
297 // we can't create a InsetMathBig, since the argument
299 return MathAtom(new InsetMathUnknown(s));
300 return MathAtom(new InsetMathSymbol(l));
303 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
304 return MathAtom(new MathMacroArgument(s[1] - '0'));
305 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
306 && s[2] >= '1' && s[2] <= '9')
307 return MathAtom(new MathMacroArgument(s[2] - '0'));
309 return MathAtom(new InsetMathBoxed());
311 return MathAtom(new InsetMathFBox());
313 return MathAtom(new InsetMathFrameBox);
315 return MathAtom(new InsetMathMakebox);
317 return MathAtom(new InsetMathKern);
318 if (s.substr(0, 8) == "xymatrix") {
319 char spacing_code = '\0';
321 size_t const len = s.length();
323 if (i < len && s[i] == '@') {
333 spacing_code = static_cast<char>(s[i]);
338 if (i < len && s[i] == '=') {
340 spacing = Length(to_ascii(s.substr(i)));
343 return MathAtom(new InsetMathXYMatrix(spacing, spacing_code));
345 if (s == "xrightarrow" || s == "xleftarrow")
346 return MathAtom(new InsetMathXArrow(s));
347 if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
348 return MathAtom(new InsetMathSplit(s));
350 return MathAtom(new InsetMathCases);
352 return MathAtom(new InsetMathSubstack);
353 if (s == "subarray" || s == "array")
354 return MathAtom(new InsetMathArray(s, 1, 1));
356 return MathAtom(new InsetMathSqrt);
358 return MathAtom(new InsetMathRoot);
360 return MathAtom(new InsetMathTabular(s, 1, 1));
362 return MathAtom(new InsetMathStackrel);
363 if (s == "binom" || s == "choose")
364 return MathAtom(new InsetMathBinom(s == "choose"));
366 return MathAtom(new InsetMathFrac);
368 return MathAtom(new InsetMathFrac(InsetMathFrac::OVER));
370 return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC));
372 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC));
373 // This string value is only for math toolbar use. Not a LaTeX name
374 if (s == "unitfracthree")
375 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3));
377 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1));
379 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT));
381 // return MathAtom(new MathInferInset);
383 return MathAtom(new InsetMathFrac(InsetMathFrac::ATOP));
385 return MathAtom(new InsetMathLefteqn);
386 if (s == "boldsymbol")
387 return MathAtom(new InsetMathBoldSymbol);
388 if (s == "color" || s == "normalcolor")
389 return MathAtom(new InsetMathColor(true));
390 if (s == "textcolor")
391 return MathAtom(new InsetMathColor(false));
393 return MathAtom(new InsetMathDFrac);
395 return MathAtom(new InsetMathTFrac);
397 return MathAtom(new InsetMathDBinom);
399 return MathAtom(new InsetMathTBinom);
401 return MathAtom(new InsetMathPhantom(InsetMathPhantom::hphantom));
403 return MathAtom(new InsetMathPhantom(InsetMathPhantom::phantom));
405 return MathAtom(new InsetMathPhantom(InsetMathPhantom::vphantom));
407 return MathAtom(new MathMacro(s));
411 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
414 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
416 docstring body = split(str, name, ' ');
421 InsetCommandParams icp(REF_CODE);
423 InsetCommandMailer::string2params("ref", to_utf8(str), icp);
424 mathed_parse_cell(ar, icp.getCommand());
428 return ar[0].nucleus();