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 "insets/InsetCommand.h"
52 #include "support/debug.h"
53 #include "support/docstream.h"
54 #include "support/FileName.h"
55 #include "support/filetools.h" // LibFileSearch
56 #include "support/lstrings.h"
58 #include "frontends/FontLoader.h"
60 #include "LyX.h" // use_gui
63 using namespace lyx::support;
72 MathWordList theWordList;
75 bool isMathFontAvailable(docstring & name)
80 // Do we have the font proper?
81 if (theFontLoader().available(f))
85 if (name == "eufrak") {
86 name = from_ascii("lyxfakefrak");
91 "font " << to_utf8(name) << " not available and I can't fake it");
101 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 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 = !isMathFontAvailable(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 (isMathFontAvailable(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 && isMathFontAvailable(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 = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
221 MathWordList const & mathedWordList()
229 static bool initialized = false;
238 latexkeys const * in_word_set(docstring const & str)
240 MathWordList::iterator it = theWordList.find(str);
241 return it != theWordList.end() ? &(it->second) : 0;
245 MathAtom createInsetMath(char const * const s)
247 return createInsetMath(from_utf8(s));
251 MathAtom createInsetMath(docstring const & s)
253 //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
254 latexkeys const * l = in_word_set(s);
256 docstring const & inset = l->inset;
257 //lyxerr << " found inset: '" << inset << '\'' << endl;
259 return MathAtom(new InsetMathRef(l->name));
260 if (inset == "overset")
261 return MathAtom(new InsetMathOverset);
262 if (inset == "underset")
263 return MathAtom(new InsetMathUnderset);
264 if (inset == "decoration")
265 return MathAtom(new InsetMathDecoration(l));
266 if (inset == "space")
267 return MathAtom(new InsetMathSpace(l->name));
269 return MathAtom(new InsetMathDots(l));
271 // return MathAtom(new InsetMathMBox);
272 // InsetMathMBox is proposed to replace InsetMathBox,
273 // but is not ready yet (it needs a BufferView for
275 return MathAtom(new InsetMathBox(l->name));
276 // if (inset == "fbox")
277 // return MathAtom(new InsetMathFBox(l));
278 if (inset == "style")
279 return MathAtom(new InsetMathSize(l));
281 return MathAtom(new InsetMathFont(l));
282 if (inset == "oldfont")
283 return MathAtom(new InsetMathFontOld(l));
284 if (inset == "matrix")
285 return MathAtom(new InsetMathAMSArray(s));
286 if (inset == "split")
287 return MathAtom(new InsetMathSplit(s));
289 // we can't create a InsetMathBig, since the argument
291 return MathAtom(new InsetMathUnknown(s));
292 return MathAtom(new InsetMathSymbol(l));
295 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
296 return MathAtom(new MathMacroArgument(s[1] - '0'));
297 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
298 && s[2] >= '1' && s[2] <= '9')
299 return MathAtom(new MathMacroArgument(s[2] - '0'));
301 return MathAtom(new InsetMathBoxed());
303 return MathAtom(new InsetMathFBox());
305 return MathAtom(new InsetMathMakebox(true));
307 return MathAtom(new InsetMathMakebox(false));
309 return MathAtom(new InsetMathKern);
310 if (s.substr(0, 8) == "xymatrix") {
311 char spacing_code = '\0';
313 size_t const len = s.length();
315 if (i < len && s[i] == '@') {
325 spacing_code = static_cast<char>(s[i]);
330 if (i < len && s[i] == '=') {
332 spacing = Length(to_ascii(s.substr(i)));
335 return MathAtom(new InsetMathXYMatrix(spacing, spacing_code));
337 if (s == "xrightarrow" || s == "xleftarrow")
338 return MathAtom(new InsetMathXArrow(s));
339 if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
340 return MathAtom(new InsetMathSplit(s));
342 return MathAtom(new InsetMathCases);
344 return MathAtom(new InsetMathSubstack);
345 if (s == "subarray" || s == "array")
346 return MathAtom(new InsetMathArray(s, 1, 1));
348 return MathAtom(new InsetMathSqrt);
350 return MathAtom(new InsetMathRoot);
352 return MathAtom(new InsetMathTabular(s, 1, 1));
354 return MathAtom(new InsetMathStackrel);
356 return MathAtom(new InsetMathBinom(InsetMathBinom::BINOM));
358 return MathAtom(new InsetMathBinom(InsetMathBinom::CHOOSE));
360 return MathAtom(new InsetMathBinom(InsetMathBinom::BRACE));
362 return MathAtom(new InsetMathBinom(InsetMathBinom::BRACK));
364 return MathAtom(new InsetMathFrac);
366 return MathAtom(new InsetMathFrac(InsetMathFrac::OVER));
368 return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC));
370 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC));
371 // This string value is only for math toolbar use. Not a LaTeX name
372 if (s == "unitfracthree")
373 return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3));
375 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1));
377 return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT));
379 // return MathAtom(new MathInferInset);
381 return MathAtom(new InsetMathFrac(InsetMathFrac::ATOP));
383 return MathAtom(new InsetMathLefteqn);
384 if (s == "boldsymbol" || s == "bm")
385 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::BOLD));
386 if (s == "heavysymbol" || s == "hm")
387 return MathAtom(new InsetMathBoldSymbol(InsetMathBoldSymbol::HEAVY));
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 InsetCommand::string2params("ref", to_utf8(str), icp);
424 mathed_parse_cell(ar, icp.getCommand());
428 return ar[0].nucleus();