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