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"
52 #include "insets/InsetCommand.h"
54 #include "support/docstream.h"
55 #include "support/filetools.h" // LibFileSearch
56 #include "support/lstrings.h"
58 #include "frontends/FontLoader.h"
63 using support::libFileSearch;
68 using std::istringstream;
77 typedef std::map<docstring, latexkeys> WordList;
82 bool math_font_available(docstring & name)
87 // Do we have the font proper?
88 if (theFontLoader().available(f))
92 if (name == "eufrak") {
93 name = from_ascii("lyxfakefrak");
98 "font " << to_utf8(name) << " not available and I can't fake it");
105 support::FileName const filename = libFileSearch(string(), "symbols");
106 LYXERR(Debug::MATHED, "read symbols from " << filename);
107 if (filename.empty()) {
108 lyxerr << "Could not find symbols file" << endl;
112 std::ifstream fs(filename.toFilesystemEncoding().c_str());
115 while (getline(fs, line)) {
118 if (line.empty() || line[0] == '#')
121 // special case of iffont/else/endif
122 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
123 istringstream is(line);
127 docstring t = from_utf8(tmp);
128 skip = !math_font_available(t);
130 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
132 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
138 // special case of pre-defined macros
139 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
140 //lyxerr << "macro definition: '" << line << '\'' << endl;
141 istringstream is(line);
144 is >> macro >> requires;
145 MacroTable::globalMacros().insert(from_utf8(macro), requires);
149 idocstringstream is(from_utf8(line));
151 is >> tmp.name >> tmp.inset;
152 if (isFontName(tmp.inset))
153 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
156 // requires is optional
160 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
161 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' '
162 << to_utf8(tmp.extra));
166 if (isFontName(tmp.inset)) {
167 // tmp.inset _is_ the fontname here.
168 // create fallbacks if necessary
170 // store requirements as long as we can
171 if (tmp.requires.empty()) {
172 if (tmp.inset == "msa" || tmp.inset == "msb")
173 tmp.requires = from_ascii("amssymb");
174 else if (tmp.inset == "wasy")
175 tmp.requires = from_ascii("wasysym");
178 // symbol font is not available sometimes
179 docstring symbol_font = from_ascii("lyxsymbol");
181 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
182 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
184 } else if (math_font_available(tmp.inset)) {
185 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
186 tmp.draw.push_back(char_type(charid));
187 } else if (fallbackid && math_font_available(symbol_font)) {
188 if (tmp.inset == "cmex")
189 tmp.inset = from_ascii("lyxsymbol");
191 tmp.inset = from_ascii("lyxboldsymbol");
192 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
193 tmp.draw.push_back(char_type(fallbackid));
195 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
197 tmp.inset = from_ascii("lyxtex");
200 // it's a proper inset
201 LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
202 << " used for " << to_utf8(tmp.name));
205 if (theWordList.find(tmp.name) != theWordList.end())
206 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
207 << " already exists.");
209 theWordList[tmp.name] = tmp;
211 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
212 << " inset: " << to_utf8(tmp.inset)
213 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
214 << " extra: " << to_utf8(tmp.extra)
215 << " requires: " << to_utf8(tmp.requires) << '\'');
217 docstring tmp = from_ascii("cmm");
218 docstring tmp2 = from_ascii("cmsy");
219 has_math_fonts = math_font_available(tmp) && math_font_available(tmp2);
228 static bool initialized = false;
237 latexkeys const * in_word_set(docstring const & str)
239 WordList::iterator it = theWordList.find(str);
240 return it != theWordList.end() ? &(it->second) : 0;
244 MathAtom createInsetMath(char const * const s)
246 return createInsetMath(from_utf8(s));
250 MathAtom createInsetMath(docstring const & s)
252 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
253 latexkeys const * l = in_word_set(s);
255 docstring const & inset = l->inset;
256 //lyxerr << " found inset: '" << inset << '\'' << endl;
258 return MathAtom(new InsetMathRef(l->name));
259 if (inset == "overset")
260 return MathAtom(new InsetMathOverset);
261 if (inset == "underset")
262 return MathAtom(new InsetMathUnderset);
263 if (inset == "decoration")
264 return MathAtom(new InsetMathDecoration(l));
265 if (inset == "space")
266 return MathAtom(new InsetMathSpace(l->name));
268 return MathAtom(new InsetMathDots(l));
270 // return MathAtom(new InsetMathMBox);
271 // InsetMathMBox is proposed to replace InsetMathBox,
272 // but is not ready yet (it needs a BufferView for
274 return MathAtom(new InsetMathBox(l->name));
275 // if (inset == "fbox")
276 // return MathAtom(new InsetMathFBox(l));
277 if (inset == "style")
278 return MathAtom(new InsetMathSize(l));
280 return MathAtom(new InsetMathFont(l));
281 if (inset == "oldfont")
282 return MathAtom(new InsetMathFontOld(l));
283 if (inset == "matrix")
284 return MathAtom(new InsetMathAMSArray(s));
285 if (inset == "split")
286 return MathAtom(new InsetMathSplit(s));
288 // we can't create a InsetMathBig, since the argument
290 return MathAtom(new InsetMathUnknown(s));
291 return MathAtom(new InsetMathSymbol(l));
294 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
295 return MathAtom(new MathMacroArgument(s[1] - '0'));
296 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
297 && s[2] >= '1' && s[2] <= '9')
298 return MathAtom(new MathMacroArgument(s[2] - '0'));
300 return MathAtom(new InsetMathBoxed());
302 return MathAtom(new InsetMathFBox());
304 return MathAtom(new InsetMathFrameBox);
306 return MathAtom(new InsetMathMakebox);
308 return MathAtom(new InsetMathKern);
309 if (s.substr(0, 8) == "xymatrix") {
310 char spacing_code = '\0';
312 size_t const len = s.length();
314 if (i < len && s[i] == '@') {
324 spacing_code = static_cast<char>(s[i]);
329 if (i < len && s[i] == '=') {
331 spacing = Length(to_ascii(s.substr(i)));
334 return MathAtom(new InsetMathXYMatrix(spacing, spacing_code));
336 if (s == "xrightarrow" || s == "xleftarrow")
337 return MathAtom(new InsetMathXArrow(s));
338 if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
339 return MathAtom(new InsetMathSplit(s));
341 return MathAtom(new InsetMathCases);
343 return MathAtom(new InsetMathSubstack);
344 if (s == "subarray" || s == "array")
345 return MathAtom(new InsetMathArray(s, 1, 1));
347 return MathAtom(new InsetMathSqrt);
349 return MathAtom(new InsetMathRoot);
351 return MathAtom(new InsetMathTabular(s, 1, 1));
353 return MathAtom(new InsetMathStackrel);
354 if (s == "binom" || s == "choose")
355 return MathAtom(new InsetMathBinom(s == "choose"));
357 return MathAtom(new InsetMathFrac);
359 return MathAtom(new InsetMathFrac(InsetMathFrac::OVER));
361 return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC));
363 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC));
364 // This string value is only for math toolbar use. Not a LaTeX name
365 if (s == "unitfracthree")
366 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3));
368 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1));
370 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT));
372 // return MathAtom(new MathInferInset);
374 return MathAtom(new InsetMathFrac(InsetMathFrac::ATOP));
376 return MathAtom(new InsetMathLefteqn);
377 if (s == "boldsymbol")
378 return MathAtom(new InsetMathBoldSymbol);
379 if (s == "color" || s == "normalcolor")
380 return MathAtom(new InsetMathColor(true));
381 if (s == "textcolor")
382 return MathAtom(new InsetMathColor(false));
384 return MathAtom(new InsetMathDFrac);
386 return MathAtom(new InsetMathTFrac);
388 return MathAtom(new InsetMathDBinom);
390 return MathAtom(new InsetMathTBinom);
392 return MathAtom(new InsetMathPhantom(InsetMathPhantom::hphantom));
394 return MathAtom(new InsetMathPhantom(InsetMathPhantom::phantom));
396 return MathAtom(new InsetMathPhantom(InsetMathPhantom::vphantom));
398 return MathAtom(new MathMacro(s));
402 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
405 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
407 docstring body = split(str, name, ' ');
412 InsetCommandParams icp(REF_CODE);
414 InsetCommandMailer::string2params("ref", to_utf8(str), icp);
415 mathed_parse_cell(ar, icp.getCommand());
419 return ar[0].nucleus();