]> git.lyx.org Git - lyx.git/blob - src/mathed/math_factory.C
fix #1073
[lyx.git] / src / mathed / math_factory.C
1
2 #include <config.h>
3
4 #include "math_parser.h"
5 #include "math_arrayinset.h"
6 #include "math_amsarrayinset.h"
7 #include "math_binominset.h"
8 #include "math_boxinset.h"
9 #include "math_casesinset.h"
10 #include "math_decorationinset.h"
11 #include "math_dotsinset.h"
12 #include "math_ertinset.h"
13 #include "math_fboxinset.h"
14 #include "math_frameboxinset.h"
15 #include "math_fontinset.h"
16 #include "math_fontoldinset.h"
17 #include "math_fracinset.h"
18 #include "math_kerninset.h"
19 #include "math_inferinset.h"
20 #include "math_lefteqninset.h"
21 #include "math_macro.h"
22 #include "math_macrotable.h"
23 #include "math_macrotemplate.h"
24 #include "math_macroarg.h"
25 #include "math_parboxinset.h"
26 #include "math_rootinset.h"
27 #include "math_sizeinset.h"
28 #include "math_spaceinset.h"
29 #include "math_splitinset.h"
30 #include "math_sqrtinset.h"
31 #include "math_stackrelinset.h"
32 #include "math_substackinset.h"
33 #include "math_symbolinset.h"
34 #include "math_tabularinset.h"
35 #include "math_undersetinset.h"
36 #include "math_unknowninset.h"
37 #include "math_xarrowinset.h"
38 #include "math_xymatrixinset.h"
39 #include "math_xyarrowinset.h"
40
41 //#include "insets/insetref.h"
42 #include "ref_inset.h"
43
44 #include "metricsinfo.h"
45 #include "debug.h"
46 #include "math_support.h"
47 #include "Lsstream.h"
48 #include "support/filetools.h" // LibFileSearch
49 #include "support/lstrings.h"
50 #include "frontends/lyx_gui.h"
51
52 #include <map>
53 #include <fstream>
54
55 bool has_math_fonts;
56
57 using std::endl;
58
59
60 namespace {
61
62 // file scope
63 typedef std::map<string, latexkeys> WordList;
64 WordList theWordList;
65
66
67 bool math_font_available(string & name)
68 {
69         LyXFont f;
70         augmentFont(f, name);
71
72         // Do we have the font proper?
73         if (lyx_gui::font_available(f))
74                 return true;
75
76         // can we fake it?
77         if (name == "eufrak") {
78                 name = "lyxfakefrak";
79                 return true;
80         }
81
82         lyxerr[Debug::MATHED]
83                 << "font " << name << " not available and I can't fake it"
84                 << endl;
85         return false;
86 }
87
88
89 void initSymbols()
90 {
91         string const filename = LibFileSearch(string(), "symbols");
92         lyxerr[Debug::MATHED] << "read symbols from " << filename << endl;
93         if (filename.empty()) {
94                 lyxerr << "Could not find symbols file" << endl;
95                 return;
96         }
97
98         std::ifstream fs(filename.c_str());
99         string line;
100         bool skip = false;
101         while (getline(fs, line)) {
102                 int charid     = 0;
103                 int fallbackid = 0;
104                 if (!line.empty() && line[0] == '#')
105                         continue;
106
107                 // special case of iffont/else/endif
108                 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
109                         istringstream is(STRCONV(line));
110                         string tmp;
111                         is >> tmp;
112                         is >> tmp;
113                         skip = !math_font_available(tmp);
114                         continue;
115                 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
116                         skip = !skip;
117                 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
118                         skip = false;
119                         continue;
120                 } else if (skip)
121                         continue;
122
123                 // special case of pre-defined macros
124                 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
125                         //lyxerr << "defining: '" << line << '\'' << endl;
126                         istringstream is(STRCONV(line));
127                         MathMacroTable::create(MathAtom(new MathMacroTemplate(is)));
128                         continue;
129                 }
130
131                 istringstream is(STRCONV(line));
132                 latexkeys tmp;
133                 is >> tmp.name >> tmp.inset;
134                 if (isFontName(tmp.inset))
135                         is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
136                 else
137                         is >> tmp.extra;
138                 if (!is) {
139                         lyxerr[Debug::MATHED] << "skipping line '" << line << '\'' << endl;
140                         lyxerr[Debug::MATHED]
141                                 << tmp.name << ' ' << tmp.inset << ' ' << tmp.extra << endl;
142                         continue;
143                 }
144
145                 if (isFontName(tmp.inset)) {
146                         // tmp.inset _is_ the fontname here.
147                         // create fallbacks if necessary
148
149                         // symbol font is not available sometimes
150                         string symbol_font = "lyxsymbol";
151
152                         if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
153                                 lyxerr[Debug::MATHED] << "symbol abuse for " << tmp.name << endl;
154                                 tmp.draw = tmp.name;
155                         } else if (math_font_available(tmp.inset)) {
156                                 lyxerr[Debug::MATHED] << "symbol available for " << tmp.name << endl;
157                                 tmp.draw += char(charid);
158                         } else if (fallbackid && math_font_available(symbol_font)) {
159                                 if (tmp.inset == "cmex")
160                                         tmp.inset  = "lyxsymbol";
161                                 else
162                                         tmp.inset  = "lyxboldsymbol";
163                                 lyxerr[Debug::MATHED] << "symbol fallback for " << tmp.name << endl;
164                                 tmp.draw += char(fallbackid);
165                         } else {
166                                 lyxerr[Debug::MATHED] << "faking " << tmp.name << endl;
167                                 tmp.draw = tmp.name;
168                                 tmp.inset = "lyxtex";
169                         }
170                 } else {
171                         // it's a proper inset
172                         lyxerr[Debug::MATHED] << "inset " << tmp.inset
173                                               << " used for " << tmp.name
174                                               << endl;
175                 }
176
177                 if (theWordList.find(tmp.name) != theWordList.end())
178                         lyxerr[Debug::MATHED]
179                                 << "readSymbols: inset " << tmp.name
180                                 << " already exists." << endl;
181                 else
182                         theWordList[tmp.name] = tmp;
183
184                 lyxerr[Debug::MATHED]
185                         << "read symbol '" << tmp.name
186                         << "  inset: " << tmp.inset
187                         << "  draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
188                         << "  extra: " << tmp.extra
189                         << '\'' << endl;
190         }
191         string tmp = "cmm";
192         string tmp2 = "cmsy";
193         has_math_fonts = math_font_available(tmp) && math_font_available(tmp2);
194 }
195
196
197 } // namespace anon
198
199
200 void initMath()
201 {
202         static bool initialized = false;
203         if (!initialized) {
204                 initSymbols();
205                 initialized = true;
206         }
207 }
208
209
210 latexkeys const * in_word_set(string const & str)
211 {
212         WordList::iterator it = theWordList.find(str);
213         //lyxerr << "looking up '" << str << "' found: "
214         // << (it != theWordList.end()) << endl;
215         return (it != theWordList.end()) ? &(it->second) : 0;
216 }
217
218
219 MathAtom createMathInset(string const & s)
220 {
221         lyxerr[Debug::MATHED] << "creating inset with name: '"
222                               << s << '\'' << endl;;
223         latexkeys const * l = in_word_set(s);
224         if (l) {
225                 string const & inset = l->inset;
226                 lyxerr[Debug::MATHED] << " found inset: '" <<
227                         inset << '\'' << endl;
228                 if (inset == "ref")
229                         return MathAtom(new RefInset(l->name));
230                 if (inset == "underset")
231                         return MathAtom(new MathUndersetInset);
232                 if (inset == "decoration")
233                         return MathAtom(new MathDecorationInset(l));
234                 if (inset == "space")
235                         return MathAtom(new MathSpaceInset(l->name));
236                 if (inset == "dots")
237                         return MathAtom(new MathDotsInset(l));
238                 if (inset == "mbox")
239                         return MathAtom(new MathBoxInset(l->name));
240                 if (inset == "parbox")
241                         return MathAtom(new MathParboxInset);
242                 if (inset == "fbox")
243                         return MathAtom(new MathFboxInset(l));
244                 if (inset == "style")
245                         return MathAtom(new MathSizeInset(l));
246                 if (inset == "font")
247                         return MathAtom(new MathFontInset(l));
248                 if (inset == "oldfont")
249                         return MathAtom(new MathFontOldInset(l));
250                 if (inset == "matrix")
251                         return MathAtom(new MathAMSArrayInset(s));
252                 return MathAtom(new MathSymbolInset(l));
253         }
254
255         if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
256                 return MathAtom(new MathMacroArgument(s[1] - '0'));
257         if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
258                         && s[2] >= '1' && s[2] <= '9')
259                 return MathAtom(new MathMacroArgument(s[2] - '0'));
260         if (s == "framebox")
261                 return MathAtom(new MathFrameboxInset);
262         if (s == "kern")
263                 return MathAtom(new MathKernInset);
264         if (s == "xymatrix")
265                 return MathAtom(new MathXYMatrixInset);
266         if (s == "xrightarrow" || s == "xleftarrow")
267                 return MathAtom(new MathXArrowInset(s));
268         if (s == "split" || s == "gathered" || s == "aligned")
269                 return MathAtom(new MathSplitInset(s));
270         if (s == "cases")
271                 return MathAtom(new MathCasesInset);
272         if (s == "substack")
273                 return MathAtom(new MathSubstackInset);
274         if (s == "subarray" || s == "array")
275                 return MathAtom(new MathArrayInset(s, 1, 1));
276         if (s == "sqrt")
277                 return MathAtom(new MathSqrtInset);
278         if (s == "root")
279                 return MathAtom(new MathRootInset);
280         if (s == "tabular")
281                 return MathAtom(new MathTabularInset(s, 1, 1));
282         if (s == "stackrel")
283                 return MathAtom(new MathStackrelInset);
284         if (s == "binom" || s == "choose")
285                 return MathAtom(new MathBinomInset(s == "choose"));
286         if (s == "over" || s == "frac")
287                 return MathAtom(new MathFracInset);
288         //if (s == "infer")
289         //      return MathAtom(new MathInferInset);
290         if (s == "atop")
291                 return MathAtom(new MathFracInset(true));
292         if (s == "lefteqn")
293                 return MathAtom(new MathLefteqnInset);
294         if (s == "lyxert")
295                 return MathAtom(new MathErtInset);
296
297         if (MathMacroTable::has(s))
298                 return MathAtom(new MathMacro(s));
299
300         //lyxerr[Debug::MATHED] << "creating inset 2 with name: '" << s << '\'' << endl;
301         return MathAtom(new MathUnknownInset(s));
302 }
303
304
305 bool createMathInset_fromDialogStr(string const & str, MathArray & ar)
306 {
307         // An example str:
308         // "ref LatexCommand \\ref{sec:Title}\n\\end_inset\n\n";
309         string name;
310         string body = split(str, name, ' ');
311
312         if (name != "ref" )
313                 return false;
314
315         // body comes with a head "LatexCommand " and a
316         // tail "\nend_inset\n\n". Strip them off.
317         string trimmed;
318         body = split(body, trimmed, ' ');
319         split(body, trimmed, '\n');
320
321         mathed_parse_cell(ar, trimmed);
322         if (ar.size() != 1)
323                 return false;
324
325         return ar[0].nucleus();
326 }
327