]> git.lyx.org Git - lyx.git/blob - src/mathed/math_factory.C
small up/down tweaking
[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
150                         // symbol font is not available sometimes
151                         string symbol_font = "lyxsymbol";
152
153                         if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
154                                 lyxerr[Debug::MATHED] << "symbol abuse for " << tmp.name << endl;
155                                 tmp.draw = tmp.name;
156                         } else if (math_font_available(tmp.inset)) {
157                                 lyxerr[Debug::MATHED] << "symbol available for " << tmp.name << endl;
158                                 tmp.draw += char(charid);
159                         } else if (fallbackid && math_font_available(symbol_font)) {
160                                 if (tmp.inset == "cmex")
161                                         tmp.inset  = "lyxsymbol";
162                                 else
163                                         tmp.inset  = "lyxboldsymbol";
164                                 lyxerr[Debug::MATHED] << "symbol fallback for " << tmp.name << endl;
165                                 tmp.draw += char(fallbackid);
166                         } else {
167                                 lyxerr[Debug::MATHED] << "faking " << tmp.name << endl;
168                                 tmp.draw = tmp.name;
169                                 tmp.inset = "lyxtex";
170                         }
171                 } else {
172                         // it's a proper inset
173                         lyxerr[Debug::MATHED] << "inset " << tmp.inset
174                                               << " used for " << tmp.name
175                                               << endl;
176                 }
177
178                 if (theWordList.find(tmp.name) != theWordList.end())
179                         lyxerr[Debug::MATHED]
180                                 << "readSymbols: inset " << tmp.name
181                                 << " already exists." << endl;
182                 else
183                         theWordList[tmp.name] = tmp;
184
185                 lyxerr[Debug::MATHED]
186                         << "read symbol '" << tmp.name
187                         << "  inset: " << tmp.inset
188                         << "  draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
189                         << "  extra: " << tmp.extra
190                         << '\'' << endl;
191         }
192         string tmp = "cmm";
193         string tmp2 = "cmsy";
194         has_math_fonts = math_font_available(tmp) && math_font_available(tmp2);
195 }
196
197
198 } // namespace anon
199
200
201 void initMath()
202 {
203         static bool initialized = false;
204         if (!initialized) {
205                 initSymbols();
206                 initialized = true;
207         }
208 }
209
210
211 latexkeys const * in_word_set(string const & str)
212 {
213         WordList::iterator it = theWordList.find(str);
214         //lyxerr << "looking up '" << str << "' found: "
215         // << (it != theWordList.end()) << endl;
216         return (it != theWordList.end()) ? &(it->second) : 0;
217 }
218
219
220 MathAtom createMathInset(string const & s)
221 {
222         lyxerr[Debug::MATHED] << "creating inset with name: '"
223                               << s << '\'' << endl;;
224         latexkeys const * l = in_word_set(s);
225         if (l) {
226                 string const & inset = l->inset;
227                 lyxerr[Debug::MATHED] << " found inset: '" <<
228                         inset << '\'' << endl;
229                 if (inset == "ref")
230                         return MathAtom(new RefInset(l->name));
231                 if (inset == "underset")
232                         return MathAtom(new MathUndersetInset);
233                 if (inset == "decoration")
234                         return MathAtom(new MathDecorationInset(l));
235                 if (inset == "space")
236                         return MathAtom(new MathSpaceInset(l->name));
237                 if (inset == "dots")
238                         return MathAtom(new MathDotsInset(l));
239                 if (inset == "mbox")
240                         return MathAtom(new MathBoxInset(l->name));
241                 if (inset == "parbox")
242                         return MathAtom(new MathParboxInset);
243                 if (inset == "fbox")
244                         return MathAtom(new MathFboxInset(l));
245                 if (inset == "style")
246                         return MathAtom(new MathSizeInset(l));
247                 if (inset == "font")
248                         return MathAtom(new MathFontInset(l));
249                 if (inset == "oldfont")
250                         return MathAtom(new MathFontOldInset(l));
251                 if (inset == "matrix")
252                         return MathAtom(new MathAMSArrayInset(s));
253                 return MathAtom(new MathSymbolInset(l));
254         }
255
256         if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
257                 return MathAtom(new MathMacroArgument(s[1] - '0'));
258         if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
259                         && s[2] >= '1' && s[2] <= '9')
260                 return MathAtom(new MathMacroArgument(s[2] - '0'));
261         if (s == "framebox")
262                 return MathAtom(new MathFrameboxInset);
263         if (s == "kern")
264                 return MathAtom(new MathKernInset);
265         if (s == "xymatrix")
266                 return MathAtom(new MathXYMatrixInset);
267         if (s == "xrightarrow" || s == "xleftarrow")
268                 return MathAtom(new MathXArrowInset(s));
269         if (s == "split" || s == "gathered" || s == "aligned")
270                 return MathAtom(new MathSplitInset(s));
271         if (s == "cases")
272                 return MathAtom(new MathCasesInset);
273         if (s == "substack")
274                 return MathAtom(new MathSubstackInset);
275         if (s == "subarray" || s == "array")
276                 return MathAtom(new MathArrayInset(s, 1, 1));
277         if (s == "sqrt")
278                 return MathAtom(new MathSqrtInset);
279         if (s == "root")
280                 return MathAtom(new MathRootInset);
281         if (s == "stackrel")
282                 return MathAtom(new MathStackrelInset);
283         if (s == "binom" || s == "choose")
284                 return MathAtom(new MathBinomInset(s == "choose"));
285         if (s == "over" || s == "frac")
286                 return MathAtom(new MathFracInset);
287         //if (s == "infer")
288         //      return MathAtom(new MathInferInset);
289         if (s == "atop")
290                 return MathAtom(new MathFracInset(true));
291         if (s == "lefteqn")
292                 return MathAtom(new MathLefteqnInset);
293         if (s == "lyxert")
294                 return MathAtom(new MathErtInset);
295
296         if (MathMacroTable::has(s))
297                 return MathAtom(new MathMacro(s));
298
299         //lyxerr[Debug::MATHED] << "creating inset 2 with name: '" << s << '\'' << endl;
300         return MathAtom(new MathUnknownInset(s));
301 }