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_macroarg.h"
33 #include "math_makeboxinset.h"
34 #include "math_oversetinset.h"
35 #include "math_parboxinset.h"
36 #include "math_rootinset.h"
37 #include "math_sizeinset.h"
38 #include "math_spaceinset.h"
39 #include "math_splitinset.h"
40 #include "math_sqrtinset.h"
41 #include "math_stackrelinset.h"
42 #include "math_substackinset.h"
43 #include "math_symbolinset.h"
44 #include "math_tabularinset.h"
45 #include "math_undersetinset.h"
46 #include "math_unknowninset.h"
47 #include "math_xarrowinset.h"
49 //#include "insets/insetref.h"
50 #include "ref_inset.h"
52 #include "metricsinfo.h"
53 #include "math_data.h"
55 #include "math_support.h"
57 #include "support/filetools.h" // LibFileSearch
58 #include "support/lstrings.h"
59 #include "frontends/lyx_gui.h"
64 using namespace lyx::support;
74 typedef std::map<string, latexkeys> WordList;
78 bool math_font_available(string & name)
83 // Do we have the font proper?
84 if (lyx_gui::font_available(f))
88 if (name == "eufrak") {
94 << "font " << name << " not available and I can't fake it"
102 string const filename = LibFileSearch(string(), "symbols");
103 lyxerr[Debug::MATHED] << "read symbols from " << filename << endl;
104 if (filename.empty()) {
105 lyxerr << "Could not find symbols file" << endl;
109 std::ifstream fs(filename.c_str());
112 while (getline(fs, line)) {
115 if (!line.empty() && line[0] == '#')
118 // special case of iffont/else/endif
119 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
120 istringstream is(STRCONV(line));
124 skip = !math_font_available(tmp);
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 << "defining: '" << line << '\'' << endl;
137 istringstream is(STRCONV(line));
138 MathMacroTable::create(MathAtom(new MathMacroTemplate(is)));
142 istringstream is(STRCONV(line));
144 is >> tmp.name >> tmp.inset;
145 if (isFontName(tmp.inset))
146 is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
150 lyxerr[Debug::MATHED] << "skipping line '" << line << '\'' << endl;
151 lyxerr[Debug::MATHED]
152 << tmp.name << ' ' << tmp.inset << ' ' << tmp.extra << endl;
156 if (isFontName(tmp.inset)) {
157 // tmp.inset _is_ the fontname here.
158 // create fallbacks if necessary
160 // symbol font is not available sometimes
161 string symbol_font = "lyxsymbol";
163 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
164 lyxerr[Debug::MATHED] << "symbol abuse for " << tmp.name << endl;
166 } else if (math_font_available(tmp.inset)) {
167 lyxerr[Debug::MATHED] << "symbol available for " << tmp.name << endl;
168 tmp.draw += char(charid);
169 } else if (fallbackid && math_font_available(symbol_font)) {
170 if (tmp.inset == "cmex")
171 tmp.inset = "lyxsymbol";
173 tmp.inset = "lyxboldsymbol";
174 lyxerr[Debug::MATHED] << "symbol fallback for " << tmp.name << endl;
175 tmp.draw += char(fallbackid);
177 lyxerr[Debug::MATHED] << "faking " << tmp.name << endl;
179 tmp.inset = "lyxtex";
182 // it's a proper inset
183 lyxerr[Debug::MATHED] << "inset " << tmp.inset
184 << " used for " << tmp.name
188 if (theWordList.find(tmp.name) != theWordList.end())
189 lyxerr[Debug::MATHED]
190 << "readSymbols: inset " << tmp.name
191 << " already exists." << endl;
193 theWordList[tmp.name] = tmp;
195 lyxerr[Debug::MATHED]
196 << "read symbol '" << tmp.name
197 << " inset: " << tmp.inset
198 << " draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
199 << " extra: " << tmp.extra
203 string tmp2 = "cmsy";
204 has_math_fonts = math_font_available(tmp) && math_font_available(tmp2);
213 static bool initialized = false;
221 latexkeys const * in_word_set(string const & str)
223 WordList::iterator it = theWordList.find(str);
224 //lyxerr << "looking up '" << str << "' found: "
225 // << (it != theWordList.end()) << endl;
226 return (it != theWordList.end()) ? &(it->second) : 0;
230 MathAtom createMathInset(string const & s)
232 lyxerr[Debug::MATHED] << "creating inset with name: '"
233 << s << '\'' << endl;;
234 latexkeys const * l = in_word_set(s);
236 string const & inset = l->inset;
237 lyxerr[Debug::MATHED] << " found inset: '" <<
238 inset << '\'' << endl;
240 return MathAtom(new RefInset(l->name));
241 if (inset == "overset")
242 return MathAtom(new MathOversetInset);
243 if (inset == "underset")
244 return MathAtom(new MathUndersetInset);
245 if (inset == "decoration")
246 return MathAtom(new MathDecorationInset(l));
247 if (inset == "space")
248 return MathAtom(new MathSpaceInset(l->name));
250 return MathAtom(new MathDotsInset(l));
252 return MathAtom(new MathBoxInset(l->name));
253 if (inset == "parbox")
254 return MathAtom(new MathParboxInset);
256 return MathAtom(new MathFboxInset(l));
257 if (inset == "style")
258 return MathAtom(new MathSizeInset(l));
260 return MathAtom(new MathFontInset(l));
261 if (inset == "oldfont")
262 return MathAtom(new MathFontOldInset(l));
263 if (inset == "matrix")
264 return MathAtom(new MathAMSArrayInset(s));
265 return MathAtom(new MathSymbolInset(l));
268 if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
269 return MathAtom(new MathMacroArgument(s[1] - '0'));
270 if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
271 && s[2] >= '1' && s[2] <= '9')
272 return MathAtom(new MathMacroArgument(s[2] - '0'));
274 return MathAtom(new MathFrameboxInset);
276 return MathAtom(new MathMakeboxInset);
278 return MathAtom(new MathKernInset);
279 if (s == "xrightarrow" || s == "xleftarrow")
280 return MathAtom(new MathXArrowInset(s));
281 if (s == "split" || s == "gathered" || s == "aligned")
282 return MathAtom(new MathSplitInset(s));
284 return MathAtom(new MathCasesInset);
286 return MathAtom(new MathSubstackInset);
287 if (s == "subarray" || s == "array")
288 return MathAtom(new MathArrayInset(s, 1, 1));
290 return MathAtom(new MathSqrtInset);
292 return MathAtom(new MathRootInset);
294 return MathAtom(new MathTabularInset(s, 1, 1));
296 return MathAtom(new MathStackrelInset);
297 if (s == "binom" || s == "choose")
298 return MathAtom(new MathBinomInset(s == "choose"));
299 if (s == "over" || s == "frac")
300 return MathAtom(new MathFracInset);
302 // return MathAtom(new MathInferInset);
304 return MathAtom(new MathFracInset(true));
306 return MathAtom(new MathLefteqnInset);
308 return MathAtom(new MathErtInset);
310 if (MathMacroTable::has(s))
311 return MathAtom(new MathMacro(s));
313 //lyxerr[Debug::MATHED] << "creating inset 2 with name: '" << s << '\'' << endl;
314 return MathAtom(new MathUnknownInset(s));
318 bool createMathInset_fromDialogStr(string const & str, MathArray & ar)
321 // "ref LatexCommand \\ref{sec:Title}\n\\end_inset\n\n";
323 string body = split(str, name, ' ');
328 // body comes with a head "LatexCommand " and a
329 // tail "\nend_inset\n\n". Strip them off.
331 body = split(body, trimmed, ' ');
332 split(body, trimmed, '\n');
334 mathed_parse_cell(ar, trimmed);
338 return ar[0].nucleus();