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 "math_factory.h"
14 #include "math_parser.h"
15 #include "math_arrayinset.h"
16 #include "math_amsarrayinset.h"
17 #include "math_binominset.h"
18 #include "math_boxinset.h"
19 #include "math_boxedinset.h"
20 #include "math_boldsymbolinset.h"
21 #include "math_casesinset.h"
22 #include "math_colorinset.h"
23 #include "math_decorationinset.h"
24 #include "math_dfracinset.h"
25 #include "math_dotsinset.h"
26 #include "math_fboxinset.h"
27 #include "math_frameboxinset.h"
28 #include "math_fontinset.h"
29 #include "math_fontoldinset.h"
30 #include "math_fracinset.h"
31 #include "math_kerninset.h"
32 #include "math_lefteqninset.h"
33 #include "math_macro.h"
34 #include "math_macroarg.h"
35 #include "math_macrotable.h"
36 #include "math_macrotemplate.h"
37 #include "math_makeboxinset.h"
38 #include "math_oversetinset.h"
39 #include "math_parser.h"
40 #include "math_rootinset.h"
41 #include "math_sizeinset.h"
42 #include "math_spaceinset.h"
43 #include "math_splitinset.h"
44 #include "math_sqrtinset.h"
45 #include "math_stackrelinset.h"
46 #include "math_substackinset.h"
47 #include "math_symbolinset.h"
48 #include "math_tabularinset.h"
49 #include "math_tfracinset.h"
50 #include "math_undersetinset.h"
51 #include "math_unknowninset.h"
52 #include "math_xarrowinset.h"
53 #include "math_xymatrixinset.h"
55 //#include "insets/insetref.h"
56 #include "ref_inset.h"
59 #include "math_support.h"
61 #include "support/filetools.h" // LibFileSearch
62 #include "support/lstrings.h"
64 #include "frontends/lyx_gui.h"
69 using lyx::support::libFileSearch;
70 using lyx::support::split;
74 using std::istringstream;
82 typedef std::map<string, latexkeys> WordList;
86 bool math_font_available(string & name)
91 // Do we have the font proper?
92 if (lyx_gui::font_available(f))
96 if (name == "eufrak") {
101 lyxerr[Debug::MATHED]
102 << "font " << name << " not available and I can't fake it"
110 string 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.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 skip = !math_font_available(tmp);
134 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
136 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
142 // special case of pre-defined macros
143 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
144 //lyxerr << "macro definition: '" << line << '\'' << endl;
145 MacroTable::globalMacros().insert(line);
149 istringstream is(line);
151 is >> tmp.name >> tmp.inset;
152 if (isFontName(tmp.inset))
153 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
157 lyxerr[Debug::MATHED] << "skipping line '" << line << '\'' << endl;
158 lyxerr[Debug::MATHED]
159 << tmp.name << ' ' << tmp.inset << ' ' << tmp.extra << endl;
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.inset == "msa" || tmp.inset == "msb")
169 tmp.requires = "amssymb";
170 // See http://bugzilla.lyx.org/show_bug.cgi?id=1942
171 // else if (tmp.inset == "wasy")
172 // tmp.requires = "wasysym";
174 // symbol font is not available sometimes
175 string symbol_font = "lyxsymbol";
177 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
178 lyxerr[Debug::MATHED] << "symbol abuse for " << tmp.name << endl;
180 } else if (math_font_available(tmp.inset)) {
181 lyxerr[Debug::MATHED] << "symbol available for " << tmp.name << endl;
182 tmp.draw += char(charid);
183 } else if (fallbackid && math_font_available(symbol_font)) {
184 if (tmp.inset == "cmex")
185 tmp.inset = "lyxsymbol";
187 tmp.inset = "lyxboldsymbol";
188 lyxerr[Debug::MATHED] << "symbol fallback for " << tmp.name << endl;
189 tmp.draw += char(fallbackid);
191 lyxerr[Debug::MATHED] << "faking " << tmp.name << endl;
193 tmp.inset = "lyxtex";
196 // it's a proper inset
197 lyxerr[Debug::MATHED] << "inset " << tmp.inset
198 << " used for " << tmp.name
202 if (theWordList.find(tmp.name) != theWordList.end())
203 lyxerr[Debug::MATHED]
204 << "readSymbols: inset " << tmp.name
205 << " already exists." << endl;
207 theWordList[tmp.name] = tmp;
209 lyxerr[Debug::MATHED]
210 << "read symbol '" << tmp.name
211 << " inset: " << tmp.inset
212 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
213 << " extra: " << tmp.extra
217 string tmp2 = "cmsy";
218 has_math_fonts = math_font_available(tmp) && math_font_available(tmp2);
227 static bool initialized = false;
236 latexkeys const * in_word_set(string const & str)
238 WordList::iterator it = theWordList.find(str);
239 return it != theWordList.end() ? &(it->second) : 0;
243 MathAtom createMathInset(string const & s)
245 //lyxerr << "creating inset with name: '" << s << '\'' << endl;
246 latexkeys const * l = in_word_set(s);
248 string const & inset = l->inset;
249 //lyxerr << " found inset: '" << inset << '\'' << endl;
251 return MathAtom(new RefInset(l->name));
252 if (inset == "overset")
253 return MathAtom(new MathOversetInset);
254 if (inset == "underset")
255 return MathAtom(new MathUndersetInset);
256 if (inset == "decoration")
257 return MathAtom(new MathDecorationInset(l));
258 if (inset == "space")
259 return MathAtom(new MathSpaceInset(l->name));
261 return MathAtom(new MathDotsInset(l));
263 // return MathAtom(new MathMBoxInset);
264 // MathMBoxInset is proposed to replace MathBoxInset,
265 // but is not ready yet (it needs a BufferView for
267 return MathAtom(new MathBoxInset(l->name));
268 // if (inset == "fbox")
269 // return MathAtom(new MathFboxInset(l));
270 if (inset == "style")
271 return MathAtom(new MathSizeInset(l));
273 return MathAtom(new MathFontInset(l));
274 if (inset == "oldfont")
275 return MathAtom(new MathFontOldInset(l));
276 if (inset == "matrix")
277 return MathAtom(new MathAMSArrayInset(s));
278 return MathAtom(new MathSymbolInset(l));
281 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
282 return MathAtom(new MathMacroArgument(s[1] - '0'));
283 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
284 && s[2] >= '1' && s[2] <= '9')
285 return MathAtom(new MathMacroArgument(s[2] - '0'));
287 return MathAtom(new MathBoxedInset());
289 return MathAtom(new MathFboxInset());
291 return MathAtom(new MathFrameboxInset);
293 return MathAtom(new MathMakeboxInset);
295 return MathAtom(new MathKernInset);
297 return MathAtom(new MathXYMatrixInset);
298 if (s == "xrightarrow" || s == "xleftarrow")
299 return MathAtom(new MathXArrowInset(s));
300 if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
301 return MathAtom(new MathSplitInset(s));
303 return MathAtom(new MathCasesInset);
305 return MathAtom(new MathSubstackInset);
306 if (s == "subarray" || s == "array")
307 return MathAtom(new MathArrayInset(s, 1, 1));
309 return MathAtom(new MathSqrtInset);
311 return MathAtom(new MathRootInset);
313 return MathAtom(new MathTabularInset(s, 1, 1));
315 return MathAtom(new MathStackrelInset);
316 if (s == "binom" || s == "choose")
317 return MathAtom(new MathBinomInset(s == "choose"));
318 if (s == "over" || s == "frac")
319 return MathAtom(new MathFracInset);
321 return MathAtom(new MathFracInset(MathFracInset::NICEFRAC));
323 // return MathAtom(new MathInferInset);
325 return MathAtom(new MathFracInset(MathFracInset::ATOP));
327 return MathAtom(new MathLefteqnInset);
328 if (s == "boldsymbol")
329 return MathAtom(new MathBoldsymbolInset);
330 if (s == "color" || s == "normalcolor")
331 return MathAtom(new MathColorInset(true));
332 if (s == "textcolor")
333 return MathAtom(new MathColorInset(false));
335 return MathAtom(new MathDfracInset);
337 return MathAtom(new MathTfracInset);
339 if (MacroTable::globalMacros().has(s))
340 return MathAtom(new MathMacro(s,
341 MacroTable::globalMacros().get(s).numargs()));
342 //if (MacroTable::localMacros().has(s))
343 // return MathAtom(new MathMacro(s,
344 // MacroTable::localMacros().get(s).numargs()));
346 //lyxerr << "creating unknown inset '" << s << "'" << endl;
347 return MathAtom(new MathUnknownInset(s));
351 bool createMathInset_fromDialogStr(string const & str, MathArray & ar)
354 // "ref LatexCommand \\ref{sec:Title}\n\\end_inset\n\n";
356 string body = split(str, name, ' ');
361 // body comes with a head "LatexCommand " and a
362 // tail "\nend_inset\n\n". Strip them off.
364 body = split(body, trimmed, ' ');
365 split(body, trimmed, '\n');
367 mathed_parse_cell(ar, trimmed);
371 return ar[0].nucleus();