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