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_parser.h"
14 #include "math_arrayinset.h"
15 #include "math_amsarrayinset.h"
16 #include "math_binominset.h"
17 #include "math_boxinset.h"
18 #include "math_casesinset.h"
19 #include "math_decorationinset.h"
20 #include "math_dotsinset.h"
21 #include "math_ertinset.h"
22 #include "math_fboxinset.h"
23 #include "math_frameboxinset.h"
24 #include "math_fontinset.h"
25 #include "math_fontoldinset.h"
26 #include "math_fracinset.h"
27 #include "math_kerninset.h"
28 #include "math_lefteqninset.h"
29 #include "math_macro.h"
30 #include "math_macrotable.h"
31 #include "math_macrotemplate.h"
32 #include "math_makeboxinset.h"
33 #include "math_oversetinset.h"
34 #include "math_parboxinset.h"
35 #include "math_rootinset.h"
36 #include "math_sizeinset.h"
37 #include "math_spaceinset.h"
38 #include "math_splitinset.h"
39 #include "math_sqrtinset.h"
40 #include "math_stackrelinset.h"
41 #include "math_substackinset.h"
42 #include "math_symbolinset.h"
43 #include "math_tabularinset.h"
44 #include "math_undersetinset.h"
45 #include "math_unknowninset.h"
46 #include "math_xarrowinset.h"
48 //#include "insets/insetref.h"
49 #include "ref_inset.h"
52 #include "math_support.h"
54 #include "support/filetools.h" // LibFileSearch
55 #include "support/lstrings.h"
56 #include "frontends/lyx_gui.h"
60 using namespace lyx::support;
70 typedef std::map<string, latexkeys> WordList;
74 bool math_font_available(string & name)
79 // Do we have the font proper?
80 if (lyx_gui::font_available(f))
84 if (name == "eufrak") {
90 << "font " << name << " not available and I can't fake it"
98 string const filename = LibFileSearch(string(), "symbols");
99 lyxerr[Debug::MATHED] << "read symbols from " << filename << endl;
100 if (filename.empty()) {
101 lyxerr << "Could not find symbols file" << endl;
105 std::ifstream fs(filename.c_str());
108 while (getline(fs, line)) {
111 if (!line.empty() && line[0] == '#')
114 // special case of iffont/else/endif
115 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
116 istringstream is(STRCONV(line));
120 skip = !math_font_available(tmp);
122 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
124 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
130 // special case of pre-defined macros
131 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
132 //lyxerr << "defining: '" << line << '\'' << endl;
133 istringstream is(STRCONV(line));
134 MathMacroTable::create(MathAtom(new MathMacroTemplate(is)));
138 istringstream is(STRCONV(line));
140 is >> tmp.name >> tmp.inset;
141 if (isFontName(tmp.inset))
142 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
146 lyxerr[Debug::MATHED] << "skipping line '" << line << '\'' << endl;
147 lyxerr[Debug::MATHED]
148 << tmp.name << ' ' << tmp.inset << ' ' << tmp.extra << endl;
152 if (isFontName(tmp.inset)) {
153 // tmp.inset _is_ the fontname here.
154 // create fallbacks if necessary
156 // symbol font is not available sometimes
157 string symbol_font = "lyxsymbol";
159 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
160 lyxerr[Debug::MATHED] << "symbol abuse for " << tmp.name << endl;
162 } else if (math_font_available(tmp.inset)) {
163 lyxerr[Debug::MATHED] << "symbol available for " << tmp.name << endl;
164 tmp.draw += char(charid);
165 } else if (fallbackid && math_font_available(symbol_font)) {
166 if (tmp.inset == "cmex")
167 tmp.inset = "lyxsymbol";
169 tmp.inset = "lyxboldsymbol";
170 lyxerr[Debug::MATHED] << "symbol fallback for " << tmp.name << endl;
171 tmp.draw += char(fallbackid);
173 lyxerr[Debug::MATHED] << "faking " << tmp.name << endl;
175 tmp.inset = "lyxtex";
178 // it's a proper inset
179 lyxerr[Debug::MATHED] << "inset " << tmp.inset
180 << " used for " << tmp.name
184 if (theWordList.find(tmp.name) != theWordList.end())
185 lyxerr[Debug::MATHED]
186 << "readSymbols: inset " << tmp.name
187 << " already exists." << endl;
189 theWordList[tmp.name] = tmp;
191 lyxerr[Debug::MATHED]
192 << "read symbol '" << tmp.name
193 << " inset: " << tmp.inset
194 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
195 << " extra: " << tmp.extra
199 string tmp2 = "cmsy";
200 has_math_fonts = math_font_available(tmp) && math_font_available(tmp2);
209 static bool initialized = false;
217 latexkeys const * in_word_set(string const & str)
219 WordList::iterator it = theWordList.find(str);
220 //lyxerr << "looking up '" << str << "' found: "
221 // << (it != theWordList.end()) << endl;
222 return (it != theWordList.end()) ? &(it->second) : 0;
226 MathAtom createMathInset(string const & s)
228 lyxerr[Debug::MATHED] << "creating inset with name: '"
229 << s << '\'' << endl;;
230 latexkeys const * l = in_word_set(s);
232 string const & inset = l->inset;
233 lyxerr[Debug::MATHED] << " found inset: '" <<
234 inset << '\'' << endl;
236 return MathAtom(new RefInset(l->name));
237 if (inset == "overset")
238 return MathAtom(new MathOversetInset);
239 if (inset == "underset")
240 return MathAtom(new MathUndersetInset);
241 if (inset == "decoration")
242 return MathAtom(new MathDecorationInset(l));
243 if (inset == "space")
244 return MathAtom(new MathSpaceInset(l->name));
246 return MathAtom(new MathDotsInset(l));
248 return MathAtom(new MathBoxInset(l->name));
249 if (inset == "parbox")
250 return MathAtom(new MathParboxInset);
252 return MathAtom(new MathFboxInset(l));
253 if (inset == "style")
254 return MathAtom(new MathSizeInset(l));
256 return MathAtom(new MathFontInset(l));
257 if (inset == "oldfont")
258 return MathAtom(new MathFontOldInset(l));
259 if (inset == "matrix")
260 return MathAtom(new MathAMSArrayInset(s));
261 return MathAtom(new MathSymbolInset(l));
264 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
265 return MathAtom(new MathMacroArgument(s[1] - '0'));
266 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
267 && s[2] >= '1' && s[2] <= '9')
268 return MathAtom(new MathMacroArgument(s[2] - '0'));
270 return MathAtom(new MathFrameboxInset);
272 return MathAtom(new MathMakeboxInset);
274 return MathAtom(new MathKernInset);
275 if (s == "xrightarrow" || s == "xleftarrow")
276 return MathAtom(new MathXArrowInset(s));
277 if (s == "split" || s == "gathered" || s == "aligned")
278 return MathAtom(new MathSplitInset(s));
280 return MathAtom(new MathCasesInset);
282 return MathAtom(new MathSubstackInset);
283 if (s == "subarray" || s == "array")
284 return MathAtom(new MathArrayInset(s, 1, 1));
286 return MathAtom(new MathSqrtInset);
288 return MathAtom(new MathRootInset);
290 return MathAtom(new MathTabularInset(s, 1, 1));
292 return MathAtom(new MathStackrelInset);
293 if (s == "binom" || s == "choose")
294 return MathAtom(new MathBinomInset(s == "choose"));
295 if (s == "over" || s == "frac")
296 return MathAtom(new MathFracInset);
298 // return MathAtom(new MathInferInset);
300 return MathAtom(new MathFracInset(true));
302 return MathAtom(new MathLefteqnInset);
304 return MathAtom(new MathErtInset);
306 if (MathMacroTable::has(s))
307 return MathAtom(new MathMacro(s));
309 //lyxerr[Debug::MATHED] << "creating inset 2 with name: '" << s << '\'' << endl;
310 return MathAtom(new MathUnknownInset(s));
314 bool createMathInset_fromDialogStr(string const & str, MathArray & ar)
317 // "ref LatexCommand \\ref{sec:Title}\n\\end_inset\n\n";
319 string body = split(str, name, ' ');
324 // body comes with a head "LatexCommand " and a
325 // tail "\nend_inset\n\n". Strip them off.
327 body = split(body, trimmed, ' ');
328 split(body, trimmed, '\n');
330 mathed_parse_cell(ar, trimmed);
334 return ar[0].nucleus();