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