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