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 "InsetMathBM.h"
18 #include "InsetMathBoldSymbol.h"
19 #include "InsetMathBox.h"
20 #include "InsetMathCases.h"
21 #include "InsetMathColor.h"
22 #include "InsetMathDecoration.h"
23 #include "InsetMathDots.h"
24 #include "InsetMathFont.h"
25 #include "InsetMathFontOld.h"
26 #include "InsetMathFrac.h"
27 #include "InsetMathKern.h"
28 #include "InsetMathLefteqn.h"
29 #include "InsetMathOverset.h"
30 #include "InsetMathPhantom.h"
31 #include "InsetMathRef.h"
32 #include "InsetMathRoot.h"
33 #include "InsetMathSize.h"
34 #include "InsetMathSpace.h"
35 #include "InsetMathSplit.h"
36 #include "InsetMathSqrt.h"
37 #include "InsetMathStackrel.h"
38 #include "InsetMathSubstack.h"
39 #include "InsetMathSymbol.h"
40 #include "InsetMathTabular.h"
41 #include "InsetMathUnderset.h"
42 #include "InsetMathUnknown.h"
43 #include "InsetMathXArrow.h"
44 #include "InsetMathXYMatrix.h"
45 #include "MacroTable.h"
46 #include "MathMacro.h"
47 #include "MathMacroArgument.h"
48 #include "MathParser.h"
49 #include "MathSupport.h"
51 #include "insets/InsetCommand.h"
53 #include "support/debug.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"
61 #include "LyX.h" // use_gui
64 using namespace lyx::support;
73 MathWordList theWordList;
76 bool isMathFontAvailable(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");
102 FileName const filename = libFileSearch(string(), "symbols");
103 LYXERR(Debug::MATHED, "read symbols from " << filename);
104 if (filename.empty()) {
105 lyxerr << "Could not find symbols file" << endl;
109 ifstream fs(filename.toFilesystemEncoding().c_str());
112 while (getline(fs, line)) {
115 if (line.empty() || line[0] == '#')
118 // special case of iffont/else/endif
119 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
120 istringstream is(line);
124 docstring t = from_utf8(tmp);
125 skip = !isMathFontAvailable(t);
127 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
129 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
135 // special case of pre-defined macros
136 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
137 //lyxerr << "macro definition: '" << line << '\'' << endl;
138 istringstream is(line);
141 is >> macro >> requires;
142 MacroTable::globalMacros().insert(from_utf8(macro), requires);
146 idocstringstream is(from_utf8(line));
148 is >> tmp.name >> tmp.inset;
149 if (isFontName(tmp.inset))
150 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
153 // requires is optional
157 LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
158 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' '
159 << to_utf8(tmp.extra));
163 if (isFontName(tmp.inset)) {
164 // tmp.inset _is_ the fontname here.
165 // create fallbacks if necessary
167 // store requirements as long as we can
168 if (tmp.requires.empty()) {
169 if (tmp.inset == "msa" || tmp.inset == "msb")
170 tmp.requires = from_ascii("amssymb");
171 else if (tmp.inset == "wasy")
172 tmp.requires = from_ascii("wasysym");
175 // symbol font is not available sometimes
176 docstring symbol_font = from_ascii("lyxsymbol");
178 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
179 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
181 } else if (isMathFontAvailable(tmp.inset)) {
182 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
183 tmp.draw.push_back(char_type(charid));
184 } else if (fallbackid && isMathFontAvailable(symbol_font)) {
185 if (tmp.inset == "cmex")
186 tmp.inset = from_ascii("lyxsymbol");
188 tmp.inset = from_ascii("lyxboldsymbol");
189 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
190 tmp.draw.push_back(char_type(fallbackid));
192 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
194 tmp.inset = from_ascii("lyxtex");
197 // it's a proper inset
198 LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
199 << " used for " << to_utf8(tmp.name));
202 if (theWordList.find(tmp.name) != theWordList.end())
203 LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
204 << " already exists.");
206 theWordList[tmp.name] = tmp;
208 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
209 << " inset: " << to_utf8(tmp.inset)
210 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
211 << " extra: " << to_utf8(tmp.extra)
212 << " requires: " << to_utf8(tmp.requires) << '\'');
214 docstring tmp = from_ascii("cmm");
215 docstring tmp2 = from_ascii("cmsy");
216 has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
222 MathWordList const & mathedWordList()
230 static bool initialized = false;
239 latexkeys const * in_word_set(docstring const & str)
241 MathWordList::iterator it = theWordList.find(str);
242 return it != theWordList.end() ? &(it->second) : 0;
246 MathAtom createInsetMath(char const * const s)
248 return createInsetMath(from_utf8(s));
252 MathAtom createInsetMath(docstring const & s)
254 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
255 latexkeys const * l = in_word_set(s);
257 docstring const & inset = l->inset;
258 //lyxerr << " found inset: '" << inset << '\'' << endl;
260 return MathAtom(new InsetMathRef(l->name));
261 if (inset == "overset")
262 return MathAtom(new InsetMathOverset);
263 if (inset == "underset")
264 return MathAtom(new InsetMathUnderset);
265 if (inset == "decoration")
266 return MathAtom(new InsetMathDecoration(l));
267 if (inset == "space")
268 return MathAtom(new InsetMathSpace(l->name));
270 return MathAtom(new InsetMathDots(l));
272 // return MathAtom(new InsetMathMBox);
273 // InsetMathMBox is proposed to replace InsetMathBox,
274 // but is not ready yet (it needs a BufferView for
276 return MathAtom(new InsetMathBox(l->name));
277 // if (inset == "fbox")
278 // return MathAtom(new InsetMathFBox(l));
279 if (inset == "style")
280 return MathAtom(new InsetMathSize(l));
282 return MathAtom(new InsetMathFont(l));
283 if (inset == "oldfont")
284 return MathAtom(new InsetMathFontOld(l));
285 if (inset == "matrix")
286 return MathAtom(new InsetMathAMSArray(s));
287 if (inset == "split")
288 return MathAtom(new InsetMathSplit(s));
290 // we can't create a InsetMathBig, since the argument
292 return MathAtom(new InsetMathUnknown(s));
293 return MathAtom(new InsetMathSymbol(l));
296 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
297 return MathAtom(new MathMacroArgument(s[1] - '0'));
298 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
299 && s[2] >= '1' && s[2] <= '9')
300 return MathAtom(new MathMacroArgument(s[2] - '0'));
302 return MathAtom(new InsetMathBoxed());
304 return MathAtom(new InsetMathFBox());
306 return MathAtom(new InsetMathMakebox(true));
308 return MathAtom(new InsetMathMakebox(false));
310 return MathAtom(new InsetMathKern);
311 if (s.substr(0, 8) == "xymatrix") {
312 char spacing_code = '\0';
314 size_t const len = s.length();
316 if (i < len && s[i] == '@') {
326 spacing_code = static_cast<char>(s[i]);
331 if (i < len && s[i] == '=') {
333 spacing = Length(to_ascii(s.substr(i)));
336 return MathAtom(new InsetMathXYMatrix(spacing, spacing_code));
338 if (s == "xrightarrow" || s == "xleftarrow")
339 return MathAtom(new InsetMathXArrow(s));
340 if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
341 return MathAtom(new InsetMathSplit(s));
343 return MathAtom(new InsetMathCases);
345 return MathAtom(new InsetMathSubstack);
346 if (s == "subarray" || s == "array")
347 return MathAtom(new InsetMathArray(s, 1, 1));
349 return MathAtom(new InsetMathSqrt);
351 return MathAtom(new InsetMathRoot);
353 return MathAtom(new InsetMathTabular(s, 1, 1));
355 return MathAtom(new InsetMathStackrel);
356 if (s == "binom" || s == "choose")
357 return MathAtom(new InsetMathBinom(s == "choose"));
359 return MathAtom(new InsetMathFrac);
361 return MathAtom(new InsetMathFrac(InsetMathFrac::OVER));
363 return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC));
365 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC));
366 // This string value is only for math toolbar use. Not a LaTeX name
367 if (s == "unitfracthree")
368 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3));
370 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1));
372 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT));
374 // return MathAtom(new MathInferInset);
376 return MathAtom(new InsetMathFrac(InsetMathFrac::ATOP));
378 return MathAtom(new InsetMathLefteqn);
379 if (s == "boldsymbol")
380 return MathAtom(new InsetMathBoldSymbol);
382 return MathAtom(new InsetMathBM);
383 if (s == "color" || s == "normalcolor")
384 return MathAtom(new InsetMathColor(true));
385 if (s == "textcolor")
386 return MathAtom(new InsetMathColor(false));
388 return MathAtom(new InsetMathDFrac);
390 return MathAtom(new InsetMathTFrac);
392 return MathAtom(new InsetMathDBinom);
394 return MathAtom(new InsetMathTBinom);
396 return MathAtom(new InsetMathPhantom(InsetMathPhantom::hphantom));
398 return MathAtom(new InsetMathPhantom(InsetMathPhantom::phantom));
400 return MathAtom(new InsetMathPhantom(InsetMathPhantom::vphantom));
402 return MathAtom(new MathMacro(s));
406 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
409 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
411 docstring body = split(str, name, ' ');
416 InsetCommandParams icp(REF_CODE);
418 InsetCommandMailer::string2params("ref", to_utf8(str), icp);
419 mathed_parse_cell(ar, icp.getCommand());
423 return ar[0].nucleus();