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_phantominset.h"
41 #include "math_rootinset.h"
42 #include "math_sizeinset.h"
43 #include "math_spaceinset.h"
44 #include "math_splitinset.h"
45 #include "math_sqrtinset.h"
46 #include "math_stackrelinset.h"
47 #include "math_substackinset.h"
48 #include "math_symbolinset.h"
49 #include "math_tabularinset.h"
50 #include "math_tfracinset.h"
51 #include "math_undersetinset.h"
52 #include "math_unknowninset.h"
53 #include "math_xarrowinset.h"
54 #include "math_xymatrixinset.h"
56 //#include "insets/insetref.h"
57 #include "ref_inset.h"
60 #include "math_support.h"
62 #include "support/filetools.h" // LibFileSearch
63 #include "support/lstrings.h"
65 #include "frontends/lyx_gui.h"
70 using lyx::support::libFileSearch;
71 using lyx::support::split;
75 using std::istringstream;
83 typedef std::map<string, latexkeys> WordList;
87 bool math_font_available(string & name)
92 // Do we have the font proper?
93 if (lyx_gui::font_available(f))
97 if (name == "eufrak") {
102 lyxerr[Debug::MATHED]
103 << "font " << name << " not available and I can't fake it"
111 string const filename = libFileSearch(string(), "symbols");
112 lyxerr[Debug::MATHED] << "read symbols from " << filename << endl;
113 if (filename.empty()) {
114 lyxerr << "Could not find symbols file" << endl;
118 std::ifstream fs(filename.c_str());
121 while (getline(fs, line)) {
124 if (!line.empty() && line[0] == '#')
127 // special case of iffont/else/endif
128 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
129 istringstream is(line);
133 skip = !math_font_available(tmp);
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 MacroTable::globalMacros().insert(line);
150 istringstream is(line);
152 is >> tmp.name >> tmp.inset;
153 if (isFontName(tmp.inset))
154 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
158 lyxerr[Debug::MATHED] << "skipping line '" << line << '\'' << endl;
159 lyxerr[Debug::MATHED]
160 << tmp.name << ' ' << tmp.inset << ' ' << tmp.extra << endl;
164 if (isFontName(tmp.inset)) {
165 // tmp.inset _is_ the fontname here.
166 // create fallbacks if necessary
168 // store requirements as long as we can
169 if (tmp.inset == "msa" || tmp.inset == "msb")
170 tmp.requires = "amssymb";
171 // See http://bugzilla.lyx.org/show_bug.cgi?id=1942
172 // else if (tmp.inset == "wasy")
173 // tmp.requires = "wasysym";
175 // symbol font is not available sometimes
176 string symbol_font = "lyxsymbol";
178 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
179 lyxerr[Debug::MATHED] << "symbol abuse for " << tmp.name << endl;
181 } else if (math_font_available(tmp.inset)) {
182 lyxerr[Debug::MATHED] << "symbol available for " << tmp.name << endl;
183 tmp.draw += char(charid);
184 } else if (fallbackid && math_font_available(symbol_font)) {
185 if (tmp.inset == "cmex")
186 tmp.inset = "lyxsymbol";
188 tmp.inset = "lyxboldsymbol";
189 lyxerr[Debug::MATHED] << "symbol fallback for " << tmp.name << endl;
190 tmp.draw += char(fallbackid);
192 lyxerr[Debug::MATHED] << "faking " << tmp.name << endl;
194 tmp.inset = "lyxtex";
197 // it's a proper inset
198 lyxerr[Debug::MATHED] << "inset " << tmp.inset
199 << " used for " << tmp.name
203 if (theWordList.find(tmp.name) != theWordList.end())
204 lyxerr[Debug::MATHED]
205 << "readSymbols: inset " << tmp.name
206 << " already exists." << endl;
208 theWordList[tmp.name] = tmp;
210 lyxerr[Debug::MATHED]
211 << "read symbol '" << tmp.name
212 << " inset: " << tmp.inset
213 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
214 << " extra: " << tmp.extra
218 string tmp2 = "cmsy";
219 has_math_fonts = math_font_available(tmp) && math_font_available(tmp2);
228 static bool initialized = false;
237 latexkeys const * in_word_set(string const & str)
239 WordList::iterator it = theWordList.find(str);
240 return it != theWordList.end() ? &(it->second) : 0;
244 MathAtom createMathInset(string const & s)
246 //lyxerr << "creating inset with name: '" << s << '\'' << endl;
247 latexkeys const * l = in_word_set(s);
249 string const & inset = l->inset;
250 //lyxerr << " found inset: '" << inset << '\'' << endl;
252 return MathAtom(new RefInset(l->name));
253 if (inset == "overset")
254 return MathAtom(new MathOversetInset);
255 if (inset == "underset")
256 return MathAtom(new MathUndersetInset);
257 if (inset == "decoration")
258 return MathAtom(new MathDecorationInset(l));
259 if (inset == "space")
260 return MathAtom(new MathSpaceInset(l->name));
262 return MathAtom(new MathDotsInset(l));
264 // return MathAtom(new MathMBoxInset);
265 // MathMBoxInset is proposed to replace MathBoxInset,
266 // but is not ready yet (it needs a BufferView for
268 return MathAtom(new MathBoxInset(l->name));
269 // if (inset == "fbox")
270 // return MathAtom(new MathFboxInset(l));
271 if (inset == "style")
272 return MathAtom(new MathSizeInset(l));
274 return MathAtom(new MathFontInset(l));
275 if (inset == "oldfont")
276 return MathAtom(new MathFontOldInset(l));
277 if (inset == "matrix")
278 return MathAtom(new MathAMSArrayInset(s));
280 // we can't create a MathBigInset, since the argument
282 return MathAtom(new MathUnknownInset(s));
283 return MathAtom(new MathSymbolInset(l));
286 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
287 return MathAtom(new MathMacroArgument(s[1] - '0'));
288 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
289 && s[2] >= '1' && s[2] <= '9')
290 return MathAtom(new MathMacroArgument(s[2] - '0'));
292 return MathAtom(new MathBoxedInset());
294 return MathAtom(new MathFboxInset());
296 return MathAtom(new MathFrameboxInset);
298 return MathAtom(new MathMakeboxInset);
300 return MathAtom(new MathKernInset);
302 return MathAtom(new MathXYMatrixInset);
303 if (s == "xrightarrow" || s == "xleftarrow")
304 return MathAtom(new MathXArrowInset(s));
305 if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
306 return MathAtom(new MathSplitInset(s));
308 return MathAtom(new MathCasesInset);
310 return MathAtom(new MathSubstackInset);
311 if (s == "subarray" || s == "array")
312 return MathAtom(new MathArrayInset(s, 1, 1));
314 return MathAtom(new MathSqrtInset);
316 return MathAtom(new MathRootInset);
318 return MathAtom(new MathTabularInset(s, 1, 1));
320 return MathAtom(new MathStackrelInset);
321 if (s == "binom" || s == "choose")
322 return MathAtom(new MathBinomInset(s == "choose"));
324 return MathAtom(new MathFracInset);
326 return MathAtom(new MathFracInset(MathFracInset::OVER));
328 return MathAtom(new MathFracInset(MathFracInset::NICEFRAC));
330 // return MathAtom(new MathInferInset);
332 return MathAtom(new MathFracInset(MathFracInset::ATOP));
334 return MathAtom(new MathLefteqnInset);
335 if (s == "boldsymbol")
336 return MathAtom(new MathBoldsymbolInset);
337 if (s == "color" || s == "normalcolor")
338 return MathAtom(new MathColorInset(true));
339 if (s == "textcolor")
340 return MathAtom(new MathColorInset(false));
342 return MathAtom(new MathDfracInset);
344 return MathAtom(new MathTfracInset);
346 return MathAtom(new MathPhantomInset(MathPhantomInset::hphantom));
348 return MathAtom(new MathPhantomInset(MathPhantomInset::phantom));
350 return MathAtom(new MathPhantomInset(MathPhantomInset::vphantom));
352 if (MacroTable::globalMacros().has(s))
353 return MathAtom(new MathMacro(s,
354 MacroTable::globalMacros().get(s).numargs()));
355 //if (MacroTable::localMacros().has(s))
356 // return MathAtom(new MathMacro(s,
357 // MacroTable::localMacros().get(s).numargs()));
359 //lyxerr << "creating unknown inset '" << s << "'" << endl;
360 return MathAtom(new MathUnknownInset(s));
364 bool createMathInset_fromDialogStr(string const & str, MathArray & ar)
367 // "ref LatexCommand \\ref{sec:Title}\n\\end_inset\n\n";
369 string body = split(str, name, ' ');
374 // body comes with a head "LatexCommand " and a
375 // tail "\nend_inset\n\n". Strip them off.
377 body = split(body, trimmed, ' ');
378 split(body, trimmed, '\n');
380 mathed_parse_cell(ar, trimmed);
384 return ar[0].nucleus();