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 "InsetMathBinom.h"
18 #include "InsetMathBoldSymbol.h"
19 #include "InsetMathBoxed.h"
20 #include "InsetMathBox.h"
21 #include "InsetMathCases.h"
22 #include "InsetMathColor.h"
23 #include "InsetMathDecoration.h"
24 #include "InsetMathDFrac.h"
25 #include "InsetMathDots.h"
26 #include "InsetMathFBox.h"
27 #include "InsetMathFont.h"
28 #include "InsetMathFontOld.h"
29 #include "InsetMathFrac.h"
30 #include "InsetMathFrameBox.h"
31 #include "InsetMathKern.h"
32 #include "InsetMathLefteqn.h"
33 #include "InsetMathMacro.h"
34 #include "InsetMathMakebox.h"
35 #include "InsetMathOverset.h"
36 #include "InsetMathPhantom.h"
37 #include "InsetMathRef.h"
38 #include "InsetMathRoot.h"
39 #include "InsetMathSize.h"
40 #include "InsetMathSpace.h"
41 #include "InsetMathSplit.h"
42 #include "InsetMathSqrt.h"
43 #include "InsetMathStackrel.h"
44 #include "InsetMathSubstack.h"
45 #include "InsetMathSymbol.h"
46 #include "InsetMathTabular.h"
47 #include "InsetMathTFrac.h"
48 #include "InsetMathUnderset.h"
49 #include "InsetMathUnknown.h"
50 #include "InsetMathXArrow.h"
51 #include "InsetMathXYMatrix.h"
52 #include "MathMacroArgument.h"
53 #include "MathMacroTable.h"
54 #include "MathParser.h"
55 #include "MathSupport.h"
59 #include "support/filetools.h" // LibFileSearch
60 #include "support/lstrings.h"
62 #include "frontends/FontLoader.h"
67 using support::libFileSearch;
72 using std::istringstream;
81 typedef std::map<docstring, latexkeys> WordList;
86 bool math_font_available(docstring & name)
91 // Do we have the font proper?
92 if (theFontLoader().available(f))
96 if (name == "eufrak") {
97 name = from_ascii("lyxfakefrak");
101 lyxerr[Debug::MATHED]
102 << "font " << to_utf8(name) << " not available and I can't fake it"
110 support::FileName const filename = libFileSearch(string(), "symbols");
111 lyxerr[Debug::MATHED] << "read symbols from " << filename << endl;
112 if (filename.empty()) {
113 lyxerr << "Could not find symbols file" << endl;
117 std::ifstream fs(filename.toFilesystemEncoding().c_str());
120 while (getline(fs, line)) {
123 if (line.empty() || line[0] == '#')
126 // special case of iffont/else/endif
127 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
128 istringstream is(line);
132 docstring t = from_utf8(tmp);
133 skip = !math_font_available(t);
135 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
137 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
143 // special case of pre-defined macros
144 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
145 //lyxerr << "macro definition: '" << line << '\'' << endl;
146 istringstream is(line);
149 is >> macro >> requires;
150 MacroTable::globalMacros().insert(from_utf8(macro), requires);
154 idocstringstream is(from_utf8(line));
156 is >> tmp.name >> tmp.inset;
157 if (isFontName(tmp.inset))
158 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
161 // requires is optional
165 lyxerr[Debug::MATHED] << "skipping line '" << line << '\'' << endl;
166 lyxerr[Debug::MATHED]
167 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' ' << to_utf8(tmp.extra) << endl;
171 if (isFontName(tmp.inset)) {
172 // tmp.inset _is_ the fontname here.
173 // create fallbacks if necessary
175 // store requirements as long as we can
176 if (tmp.requires.empty() &&
177 (tmp.inset == "msa" || tmp.inset == "msb"))
178 tmp.requires = from_ascii("amssymb");
179 else if (tmp.inset == "wasy")
180 tmp.requires = from_ascii("wasysym");
182 // symbol font is not available sometimes
183 docstring symbol_font = from_ascii("lyxsymbol");
185 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
186 lyxerr[Debug::MATHED] << "symbol abuse for " << to_utf8(tmp.name) << endl;
188 } else if (math_font_available(tmp.inset)) {
189 lyxerr[Debug::MATHED] << "symbol available for " << to_utf8(tmp.name) << endl;
190 tmp.draw.push_back(char_type(charid));
191 } else if (fallbackid && math_font_available(symbol_font)) {
192 if (tmp.inset == "cmex")
193 tmp.inset = from_ascii("lyxsymbol");
195 tmp.inset = from_ascii("lyxboldsymbol");
196 lyxerr[Debug::MATHED] << "symbol fallback for " << to_utf8(tmp.name) << endl;
197 tmp.draw.push_back(char_type(fallbackid));
199 lyxerr[Debug::MATHED] << "faking " << to_utf8(tmp.name) << endl;
201 tmp.inset = from_ascii("lyxtex");
204 // it's a proper inset
205 lyxerr[Debug::MATHED] << "inset " << to_utf8(tmp.inset)
206 << " used for " << to_utf8(tmp.name)
210 if (theWordList.find(tmp.name) != theWordList.end())
211 lyxerr[Debug::MATHED]
212 << "readSymbols: inset " << to_utf8(tmp.name)
213 << " already exists." << endl;
215 theWordList[tmp.name] = tmp;
217 lyxerr[Debug::MATHED]
218 << "read symbol '" << to_utf8(tmp.name)
219 << " inset: " << to_utf8(tmp.inset)
220 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
221 << " extra: " << to_utf8(tmp.extra)
222 << " requires: " << to_utf8(tmp.requires)
225 docstring tmp = from_ascii("cmm");
226 docstring tmp2 = from_ascii("cmsy");
227 has_math_fonts = math_font_available(tmp) && math_font_available(tmp2);
236 static bool initialized = false;
245 latexkeys const * in_word_set(docstring const & str)
247 WordList::iterator it = theWordList.find(str);
248 return it != theWordList.end() ? &(it->second) : 0;
252 MathAtom createInsetMath(char const * const s)
254 return createInsetMath(from_utf8(s));
258 MathAtom createInsetMath(docstring const & s)
260 //lyxerr << "creating inset with name: '" << s << '\'' << endl;
261 latexkeys const * l = in_word_set(s);
263 docstring const & inset = l->inset;
264 //lyxerr << " found inset: '" << inset << '\'' << endl;
266 return MathAtom(new RefInset(l->name));
267 if (inset == "overset")
268 return MathAtom(new InsetMathOverset);
269 if (inset == "underset")
270 return MathAtom(new InsetMathUnderset);
271 if (inset == "decoration")
272 return MathAtom(new InsetMathDecoration(l));
273 if (inset == "space")
274 return MathAtom(new InsetMathSpace(l->name));
276 return MathAtom(new InsetMathDots(l));
278 // return MathAtom(new InsetMathMBox);
279 // InsetMathMBox is proposed to replace InsetMathBox,
280 // but is not ready yet (it needs a BufferView for
282 return MathAtom(new InsetMathBox(l->name));
283 // if (inset == "fbox")
284 // return MathAtom(new InsetMathFBox(l));
285 if (inset == "style")
286 return MathAtom(new InsetMathSize(l));
288 return MathAtom(new InsetMathFont(l));
289 if (inset == "oldfont")
290 return MathAtom(new InsetMathFontOld(l));
291 if (inset == "matrix")
292 return MathAtom(new InsetMathAMSArray(s));
293 if (inset == "split")
294 return MathAtom(new InsetMathSplit(s));
296 // we can't create a InsetMathBig, since the argument
298 return MathAtom(new InsetMathUnknown(s));
299 return MathAtom(new InsetMathSymbol(l));
302 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
303 return MathAtom(new MathMacroArgument(s[1] - '0'));
304 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
305 && s[2] >= '1' && s[2] <= '9')
306 return MathAtom(new MathMacroArgument(s[2] - '0'));
308 return MathAtom(new InsetMathBoxed());
310 return MathAtom(new InsetMathFBox());
312 return MathAtom(new InsetMathFrameBox);
314 return MathAtom(new InsetMathMakebox);
316 return MathAtom(new InsetMathKern);
317 if (s.substr(0, 8) == "xymatrix") {
318 char spacing_code = '\0';
320 size_t const len = s.length();
322 if (i < len && s[i] == '@') {
332 spacing_code = static_cast<char>(s[i]);
337 if (i < len && s[i] == '=') {
339 spacing = LyXLength(to_ascii(s.substr(i)));
342 return MathAtom(new InsetMathXYMatrix(spacing, spacing_code));
344 if (s == "xrightarrow" || s == "xleftarrow")
345 return MathAtom(new InsetMathXArrow(s));
346 if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
347 return MathAtom(new InsetMathSplit(s));
349 return MathAtom(new InsetMathCases);
351 return MathAtom(new InsetMathSubstack);
352 if (s == "subarray" || s == "array")
353 return MathAtom(new InsetMathArray(s, 1, 1));
355 return MathAtom(new InsetMathSqrt);
357 return MathAtom(new InsetMathRoot);
359 return MathAtom(new InsetMathTabular(s, 1, 1));
361 return MathAtom(new InsetMathStackrel);
362 if (s == "binom" || s == "choose")
363 return MathAtom(new InsetMathBinom(s == "choose"));
365 return MathAtom(new InsetMathFrac);
367 return MathAtom(new InsetMathFrac(InsetMathFrac::OVER));
369 return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC));
371 // return MathAtom(new MathInferInset);
373 return MathAtom(new InsetMathFrac(InsetMathFrac::ATOP));
375 return MathAtom(new InsetMathLefteqn);
376 if (s == "boldsymbol")
377 return MathAtom(new InsetMathBoldSymbol);
378 if (s == "color" || s == "normalcolor")
379 return MathAtom(new InsetMathColor(true));
380 if (s == "textcolor")
381 return MathAtom(new InsetMathColor(false));
383 return MathAtom(new InsetMathDFrac);
385 return MathAtom(new InsetMathTFrac);
387 return MathAtom(new InsetMathPhantom(InsetMathPhantom::hphantom));
389 return MathAtom(new InsetMathPhantom(InsetMathPhantom::phantom));
391 return MathAtom(new InsetMathPhantom(InsetMathPhantom::vphantom));
393 if (MacroTable::globalMacros().has(s))
394 return MathAtom(new MathMacro(s,
395 MacroTable::globalMacros().get(s).numargs()));
396 //if (MacroTable::localMacros().has(s))
397 // return MathAtom(new MathMacro(s,
398 // MacroTable::localMacros().get(s).numargs()));
400 //lyxerr << "creating unknown inset '" << s << "'" << endl;
401 return MathAtom(new InsetMathUnknown(s));
405 bool createInsetMath_fromDialogStr(docstring const & str, MathArray & ar)
408 // "ref LatexCommand \\ref{sec:Title}\n\\end_inset\n\n";
410 docstring body = split(str, name, ' ');
415 // body comes with a head "LatexCommand " and a
416 // tail "\nend_inset\n\n". Strip them off.
418 body = split(body, trimmed, ' ');
419 split(body, trimmed, '\n');
421 mathed_parse_cell(ar, trimmed);
425 return ar[0].nucleus();