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"
61 using namespace lyx::support;
71 typedef map<docstring, latexkeys> WordList;
76 bool math_font_available(docstring & name)
81 // Do we have the font proper?
82 if (theFontLoader().available(f))
86 if (name == "eufrak") {
87 name = from_ascii("lyxfakefrak");
92 "font " << to_utf8(name) << " not available and I can't fake it");
99 support::FileName const filename = libFileSearch(string(), "symbols");
100 LYXERR(Debug::MATHED, "read symbols from " << filename);
101 if (filename.empty()) {
102 lyxerr << "Could not find symbols file" << endl;
106 ifstream fs(filename.toFilesystemEncoding().c_str());
109 while (getline(fs, line)) {
112 if (line.empty() || line[0] == '#')
115 // special case of iffont/else/endif
116 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
117 istringstream is(line);
121 docstring t = from_utf8(tmp);
122 skip = !math_font_available(t);
124 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
126 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
132 // special case of pre-defined macros
133 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
134 //lyxerr << "macro definition: '" << line << '\'' << endl;
135 istringstream is(line);
138 is >> macro >> requires;
139 MacroTable::globalMacros().insert(from_utf8(macro), requires);
143 idocstringstream is(from_utf8(line));
145 is >> tmp.name >> tmp.inset;
146 if (isFontName(tmp.inset))
147 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
150 // requires is optional
154 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
155 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' '
156 << to_utf8(tmp.extra));
160 if (isFontName(tmp.inset)) {
161 // tmp.inset _is_ the fontname here.
162 // create fallbacks if necessary
164 // store requirements as long as we can
165 if (tmp.requires.empty()) {
166 if (tmp.inset == "msa" || tmp.inset == "msb")
167 tmp.requires = from_ascii("amssymb");
168 else if (tmp.inset == "wasy")
169 tmp.requires = from_ascii("wasysym");
172 // symbol font is not available sometimes
173 docstring symbol_font = from_ascii("lyxsymbol");
175 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
176 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
178 } else if (math_font_available(tmp.inset)) {
179 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
180 tmp.draw.push_back(char_type(charid));
181 } else if (fallbackid && math_font_available(symbol_font)) {
182 if (tmp.inset == "cmex")
183 tmp.inset = from_ascii("lyxsymbol");
185 tmp.inset = from_ascii("lyxboldsymbol");
186 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
187 tmp.draw.push_back(char_type(fallbackid));
189 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
191 tmp.inset = from_ascii("lyxtex");
194 // it's a proper inset
195 LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
196 << " used for " << to_utf8(tmp.name));
199 if (theWordList.find(tmp.name) != theWordList.end())
200 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
201 << " already exists.");
203 theWordList[tmp.name] = tmp;
205 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
206 << " inset: " << to_utf8(tmp.inset)
207 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
208 << " extra: " << to_utf8(tmp.extra)
209 << " requires: " << to_utf8(tmp.requires) << '\'');
211 docstring tmp = from_ascii("cmm");
212 docstring tmp2 = from_ascii("cmsy");
213 has_math_fonts = math_font_available(tmp) && math_font_available(tmp2);
222 static bool initialized = false;
231 latexkeys const * in_word_set(docstring const & str)
233 WordList::iterator it = theWordList.find(str);
234 return it != theWordList.end() ? &(it->second) : 0;
238 MathAtom createInsetMath(char const * const s)
240 return createInsetMath(from_utf8(s));
244 MathAtom createInsetMath(docstring const & s)
246 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
247 latexkeys const * l = in_word_set(s);
249 docstring const & inset = l->inset;
250 //lyxerr << " found inset: '" << inset << '\'' << endl;
252 return MathAtom(new InsetMathRef(l->name));
253 if (inset == "overset")
254 return MathAtom(new InsetMathOverset);
255 if (inset == "underset")
256 return MathAtom(new InsetMathUnderset);
257 if (inset == "decoration")
258 return MathAtom(new InsetMathDecoration(l));
259 if (inset == "space")
260 return MathAtom(new InsetMathSpace(l->name));
262 return MathAtom(new InsetMathDots(l));
264 // return MathAtom(new InsetMathMBox);
265 // InsetMathMBox is proposed to replace InsetMathBox,
266 // but is not ready yet (it needs a BufferView for
268 return MathAtom(new InsetMathBox(l->name));
269 // if (inset == "fbox")
270 // return MathAtom(new InsetMathFBox(l));
271 if (inset == "style")
272 return MathAtom(new InsetMathSize(l));
274 return MathAtom(new InsetMathFont(l));
275 if (inset == "oldfont")
276 return MathAtom(new InsetMathFontOld(l));
277 if (inset == "matrix")
278 return MathAtom(new InsetMathAMSArray(s));
279 if (inset == "split")
280 return MathAtom(new InsetMathSplit(s));
282 // we can't create a InsetMathBig, since the argument
284 return MathAtom(new InsetMathUnknown(s));
285 return MathAtom(new InsetMathSymbol(l));
288 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
289 return MathAtom(new MathMacroArgument(s[1] - '0'));
290 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
291 && s[2] >= '1' && s[2] <= '9')
292 return MathAtom(new MathMacroArgument(s[2] - '0'));
294 return MathAtom(new InsetMathBoxed());
296 return MathAtom(new InsetMathFBox());
298 return MathAtom(new InsetMathFrameBox);
300 return MathAtom(new InsetMathMakebox);
302 return MathAtom(new InsetMathKern);
303 if (s.substr(0, 8) == "xymatrix") {
304 char spacing_code = '\0';
306 size_t const len = s.length();
308 if (i < len && s[i] == '@') {
318 spacing_code = static_cast<char>(s[i]);
323 if (i < len && s[i] == '=') {
325 spacing = Length(to_ascii(s.substr(i)));
328 return MathAtom(new InsetMathXYMatrix(spacing, spacing_code));
330 if (s == "xrightarrow" || s == "xleftarrow")
331 return MathAtom(new InsetMathXArrow(s));
332 if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
333 return MathAtom(new InsetMathSplit(s));
335 return MathAtom(new InsetMathCases);
337 return MathAtom(new InsetMathSubstack);
338 if (s == "subarray" || s == "array")
339 return MathAtom(new InsetMathArray(s, 1, 1));
341 return MathAtom(new InsetMathSqrt);
343 return MathAtom(new InsetMathRoot);
345 return MathAtom(new InsetMathTabular(s, 1, 1));
347 return MathAtom(new InsetMathStackrel);
348 if (s == "binom" || s == "choose")
349 return MathAtom(new InsetMathBinom(s == "choose"));
351 return MathAtom(new InsetMathFrac);
353 return MathAtom(new InsetMathFrac(InsetMathFrac::OVER));
355 return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC));
357 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC));
358 // This string value is only for math toolbar use. Not a LaTeX name
359 if (s == "unitfracthree")
360 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3));
362 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1));
364 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT));
366 // return MathAtom(new MathInferInset);
368 return MathAtom(new InsetMathFrac(InsetMathFrac::ATOP));
370 return MathAtom(new InsetMathLefteqn);
371 if (s == "boldsymbol")
372 return MathAtom(new InsetMathBoldSymbol);
373 if (s == "color" || s == "normalcolor")
374 return MathAtom(new InsetMathColor(true));
375 if (s == "textcolor")
376 return MathAtom(new InsetMathColor(false));
378 return MathAtom(new InsetMathDFrac);
380 return MathAtom(new InsetMathTFrac);
382 return MathAtom(new InsetMathDBinom);
384 return MathAtom(new InsetMathTBinom);
386 return MathAtom(new InsetMathPhantom(InsetMathPhantom::hphantom));
388 return MathAtom(new InsetMathPhantom(InsetMathPhantom::phantom));
390 return MathAtom(new InsetMathPhantom(InsetMathPhantom::vphantom));
392 return MathAtom(new MathMacro(s));
396 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
399 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
401 docstring body = split(str, name, ' ');
406 InsetCommandParams icp(REF_CODE);
408 InsetCommandMailer::string2params("ref", to_utf8(str), icp);
409 mathed_parse_cell(ar, icp.getCommand());
413 return ar[0].nucleus();