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/filetools.h" // LibFileSearch
56 #include "support/lstrings.h"
58 #include "frontends/FontLoader.h"
64 using support::libFileSearch;
73 typedef std::map<docstring, latexkeys> WordList;
78 bool math_font_available(docstring & name)
83 // Do we have the font proper?
84 if (theFontLoader().available(f))
88 if (name == "eufrak") {
89 name = from_ascii("lyxfakefrak");
94 "font " << to_utf8(name) << " not available and I can't fake it");
101 support::FileName const filename = libFileSearch(string(), "symbols");
102 LYXERR(Debug::MATHED, "read symbols from " << filename);
103 if (filename.empty()) {
104 lyxerr << "Could not find symbols file" << endl;
108 std::ifstream fs(filename.toFilesystemEncoding().c_str());
111 while (getline(fs, line)) {
114 if (line.empty() || line[0] == '#')
117 // special case of iffont/else/endif
118 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
119 istringstream is(line);
123 docstring t = from_utf8(tmp);
124 skip = !math_font_available(t);
126 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
128 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
134 // special case of pre-defined macros
135 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
136 //lyxerr << "macro definition: '" << line << '\'' << endl;
137 istringstream is(line);
140 is >> macro >> requires;
141 MacroTable::globalMacros().insert(from_utf8(macro), requires);
145 idocstringstream is(from_utf8(line));
147 is >> tmp.name >> tmp.inset;
148 if (isFontName(tmp.inset))
149 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
152 // requires is optional
156 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
157 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' '
158 << to_utf8(tmp.extra));
162 if (isFontName(tmp.inset)) {
163 // tmp.inset _is_ the fontname here.
164 // create fallbacks if necessary
166 // store requirements as long as we can
167 if (tmp.requires.empty()) {
168 if (tmp.inset == "msa" || tmp.inset == "msb")
169 tmp.requires = from_ascii("amssymb");
170 else if (tmp.inset == "wasy")
171 tmp.requires = from_ascii("wasysym");
174 // symbol font is not available sometimes
175 docstring symbol_font = from_ascii("lyxsymbol");
177 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
178 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
180 } else if (math_font_available(tmp.inset)) {
181 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
182 tmp.draw.push_back(char_type(charid));
183 } else if (fallbackid && math_font_available(symbol_font)) {
184 if (tmp.inset == "cmex")
185 tmp.inset = from_ascii("lyxsymbol");
187 tmp.inset = from_ascii("lyxboldsymbol");
188 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
189 tmp.draw.push_back(char_type(fallbackid));
191 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
193 tmp.inset = from_ascii("lyxtex");
196 // it's a proper inset
197 LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
198 << " used for " << to_utf8(tmp.name));
201 if (theWordList.find(tmp.name) != theWordList.end())
202 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
203 << " already exists.");
205 theWordList[tmp.name] = tmp;
207 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
208 << " inset: " << to_utf8(tmp.inset)
209 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
210 << " extra: " << to_utf8(tmp.extra)
211 << " requires: " << to_utf8(tmp.requires) << '\'');
213 docstring tmp = from_ascii("cmm");
214 docstring tmp2 = from_ascii("cmsy");
215 has_math_fonts = math_font_available(tmp) && math_font_available(tmp2);
224 static bool initialized = false;
233 latexkeys const * in_word_set(docstring const & str)
235 WordList::iterator it = theWordList.find(str);
236 return it != theWordList.end() ? &(it->second) : 0;
240 MathAtom createInsetMath(char const * const s)
242 return createInsetMath(from_utf8(s));
246 MathAtom createInsetMath(docstring const & s)
248 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
249 latexkeys const * l = in_word_set(s);
251 docstring const & inset = l->inset;
252 //lyxerr << " found inset: '" << inset << '\'' << endl;
254 return MathAtom(new InsetMathRef(l->name));
255 if (inset == "overset")
256 return MathAtom(new InsetMathOverset);
257 if (inset == "underset")
258 return MathAtom(new InsetMathUnderset);
259 if (inset == "decoration")
260 return MathAtom(new InsetMathDecoration(l));
261 if (inset == "space")
262 return MathAtom(new InsetMathSpace(l->name));
264 return MathAtom(new InsetMathDots(l));
266 // return MathAtom(new InsetMathMBox);
267 // InsetMathMBox is proposed to replace InsetMathBox,
268 // but is not ready yet (it needs a BufferView for
270 return MathAtom(new InsetMathBox(l->name));
271 // if (inset == "fbox")
272 // return MathAtom(new InsetMathFBox(l));
273 if (inset == "style")
274 return MathAtom(new InsetMathSize(l));
276 return MathAtom(new InsetMathFont(l));
277 if (inset == "oldfont")
278 return MathAtom(new InsetMathFontOld(l));
279 if (inset == "matrix")
280 return MathAtom(new InsetMathAMSArray(s));
281 if (inset == "split")
282 return MathAtom(new InsetMathSplit(s));
284 // we can't create a InsetMathBig, since the argument
286 return MathAtom(new InsetMathUnknown(s));
287 return MathAtom(new InsetMathSymbol(l));
290 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
291 return MathAtom(new MathMacroArgument(s[1] - '0'));
292 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
293 && s[2] >= '1' && s[2] <= '9')
294 return MathAtom(new MathMacroArgument(s[2] - '0'));
296 return MathAtom(new InsetMathBoxed());
298 return MathAtom(new InsetMathFBox());
300 return MathAtom(new InsetMathFrameBox);
302 return MathAtom(new InsetMathMakebox);
304 return MathAtom(new InsetMathKern);
305 if (s.substr(0, 8) == "xymatrix") {
306 char spacing_code = '\0';
308 size_t const len = s.length();
310 if (i < len && s[i] == '@') {
320 spacing_code = static_cast<char>(s[i]);
325 if (i < len && s[i] == '=') {
327 spacing = Length(to_ascii(s.substr(i)));
330 return MathAtom(new InsetMathXYMatrix(spacing, spacing_code));
332 if (s == "xrightarrow" || s == "xleftarrow")
333 return MathAtom(new InsetMathXArrow(s));
334 if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
335 return MathAtom(new InsetMathSplit(s));
337 return MathAtom(new InsetMathCases);
339 return MathAtom(new InsetMathSubstack);
340 if (s == "subarray" || s == "array")
341 return MathAtom(new InsetMathArray(s, 1, 1));
343 return MathAtom(new InsetMathSqrt);
345 return MathAtom(new InsetMathRoot);
347 return MathAtom(new InsetMathTabular(s, 1, 1));
349 return MathAtom(new InsetMathStackrel);
350 if (s == "binom" || s == "choose")
351 return MathAtom(new InsetMathBinom(s == "choose"));
353 return MathAtom(new InsetMathFrac);
355 return MathAtom(new InsetMathFrac(InsetMathFrac::OVER));
357 return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC));
359 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC));
360 // This string value is only for math toolbar use. Not a LaTeX name
361 if (s == "unitfracthree")
362 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3));
364 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1));
366 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT));
368 // return MathAtom(new MathInferInset);
370 return MathAtom(new InsetMathFrac(InsetMathFrac::ATOP));
372 return MathAtom(new InsetMathLefteqn);
373 if (s == "boldsymbol")
374 return MathAtom(new InsetMathBoldSymbol);
375 if (s == "color" || s == "normalcolor")
376 return MathAtom(new InsetMathColor(true));
377 if (s == "textcolor")
378 return MathAtom(new InsetMathColor(false));
380 return MathAtom(new InsetMathDFrac);
382 return MathAtom(new InsetMathTFrac);
384 return MathAtom(new InsetMathDBinom);
386 return MathAtom(new InsetMathTBinom);
388 return MathAtom(new InsetMathPhantom(InsetMathPhantom::hphantom));
390 return MathAtom(new InsetMathPhantom(InsetMathPhantom::phantom));
392 return MathAtom(new InsetMathPhantom(InsetMathPhantom::vphantom));
394 return MathAtom(new MathMacro(s));
398 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
401 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
403 docstring body = split(str, name, ' ');
408 InsetCommandParams icp(REF_CODE);
410 InsetCommandMailer::string2params("ref", to_utf8(str), icp);
411 mathed_parse_cell(ar, icp.getCommand());
415 return ar[0].nucleus();