]> git.lyx.org Git - lyx.git/blob - src/mathed/math_factory.C
add some debug msg
[lyx.git] / src / mathed / math_factory.C
1 #include <config.h>
2
3 #include "math_parser.h"
4 #include "math_arrayinset.h"
5 #include "math_amsarrayinset.h"
6 #include "math_binominset.h"
7 #include "math_boxinset.h"
8 #include "math_casesinset.h"
9 #include "math_decorationinset.h"
10 #include "math_dotsinset.h"
11 #include "math_fboxinset.h"
12 #include "math_fontinset.h"
13 #include "math_fracinset.h"
14 #include "math_kerninset.h"
15 #include "math_lefteqninset.h"
16 #include "math_macro.h"
17 #include "math_macrotable.h"
18 #include "math_macroarg.h"
19 #include "math_notinset.h"
20 #include "math_rootinset.h"
21 #include "math_sizeinset.h"
22 #include "math_spaceinset.h"
23 #include "math_splitinset.h"
24 #include "math_sqrtinset.h"
25 #include "math_stackrelinset.h"
26 #include "math_substackinset.h"
27 #include "math_symbolinset.h"
28 #include "math_undersetinset.h"
29 #include "math_unknowninset.h"
30 #include "math_xarrowinset.h"
31 #include "math_xymatrixinset.h"
32 #include "math_xyarrowinset.h"
33
34 #include "ref_inset.h"
35
36 #include "math_metricsinfo.h"
37 #include "debug.h"
38 #include "math_support.h"
39 #include "Lsstream.h"
40 #include "support/filetools.h" // LibFileSearch
41 #include "frontends/font_loader.h"
42
43 #include <map>
44 #include <fstream>
45
46
47 namespace {
48
49 // file scope
50 typedef std::map<string, latexkeys> WordList;
51 WordList theWordList;
52
53
54 struct key_type {
55         ///
56         string name;
57         ///
58         string inset;
59         ///
60         string extra;
61 };
62
63
64 key_type wordlist_array[] =
65 {
66         {"!",  "space", ""},
67         //{"(",  "begin", ""},
68         //{")",  "end", ""},
69         {",",  "space", ""},
70         {":",  "space", ""},
71         {";",  "space", ""},
72         //{"[",  "begin", ""},
73         //{"]",  "end", ""},
74         {"Vmatrix",  "matrix", ""},
75         {"acute",  "decoration", ""},
76         {"bar",  "decoration", ""},
77         {"begin",  "begin", ""},
78         {"bf",  "oldfont", ""},
79         {"bmatrix",  "matrix", ""},
80         {"acute",  "decoration", ""},
81         {"breve",  "decoration", ""},
82         {"cal",  "oldfont", ""},
83         {"cdots",  "dots", ""},
84         {"check",  "decoration", ""},
85         {"ddot",  "decoration", ""},
86         {"dddot",  "decoration", ""},
87         {"ddots",  "dots", ""},
88         {"displaystyle",  "style", ""},
89         {"dot",  "decoration", ""},
90         {"dotsb",  "dots", ""},
91         {"dotsc",  "dots", ""},
92         {"dotsi",  "dots", ""},
93         {"dotsm",  "dots", ""},
94         {"dotso",  "dots", ""},
95         {"end",  "end", ""},
96         {"fbox",  "fbox", ""},
97         {"frak",  "font", ""},
98         {"grave",  "decoration", ""},
99         {"hat",  "decoration", ""},
100         {"it",  "oldfont", ""},
101         {"label",  "label", ""},
102         {"ldots",  "dots", ""},
103         {"left",  "left", ""},
104         {"limits",  "limit", ""},
105         {"lyxbox",  "box", ""},
106         {"lyxnegspace",  "space", ""},
107         {"lyxposspace",  "space", ""},
108         {"mathbb",  "font", ""},
109         {"mathbf",  "font", ""},
110         {"mathcal",  "font", ""},
111         {"mathfrak",  "font", ""},
112         {"mathit",  "font", ""},
113         {"mathnormal",  "font", ""},
114         {"mathring",  "decoration", ""},
115         {"mathrm",  "font", ""},
116         {"mathsf",  "font", ""},
117         {"mathtt",  "font", ""},
118         {"matrix",  "matrix", ""},
119         {"mbox",  "box", ""},
120         {"newcommand",  "newcommand", ""},
121         {"nolimits",  "limit", ""},
122         {"nonumber",  "nonum", ""},
123         {"overbrace",  "decoration", ""},
124         {"overleftarrow",  "decoration", ""},
125         {"overline",  "decoration", ""},
126         {"overrightarrow",  "decoration", ""},
127         {"overleftrightarrow", "decoration", ""},
128         {"pmatrix",  "matrix", ""},
129         {"protect",  "protect", ""},
130         {"qquad",  "space", ""},
131         {"quad",  "space", ""},
132         {"right",  "right", ""},
133         {"rm",  "oldfont", ""},
134         {"scriptscriptstyle",  "style", ""},
135         {"scriptstyle",  "style", ""},
136         {"textbf",  "font", "mathtext"},
137         {"textit",  "font", "mathtext"},
138         {"textmd",  "font", "mathtext"},
139         {"textrm",  "font", "mathtext"},
140         {"textsl",  "font", "mathtext"},
141         {"textup",  "font", "mathtext"},
142         {"textstyle",  "style", ""},
143         {"tilde",  "decoration", ""},
144         {"tt",  "oldfont", ""},
145         {"underbar",  "decoration", ""},
146         {"underbrace",  "decoration", ""},
147         {"underleftarrow", "decoration", ""},
148         {"underline",  "decoration", ""},
149         {"underrightarrow", "decoration", ""},
150         {"underleftrightarrow", "decoration", ""},
151         {"underset",  "underset", ""},
152         {"vdots",  "dots", ""},
153         {"vec",  "decoration", ""},
154         {"vmatrix",  "matrix", ""},
155         {"widehat",  "decoration", ""},
156         {"widetilde",  "decoration", ""}
157 };
158
159
160 bool math_font_available(string & name)
161 {
162         LyXFont f;
163         augmentFont(f, name);
164
165         // Do we have the font proper?
166         if (fontloader.available(f))
167                 return true;
168
169         // can we fake it?
170         if (name == "eufrak") {
171                 name = "lyxfakefrak";
172                 return true;
173         }
174
175         lyxerr[Debug::MATHED] << "font " << name << " not available and I can't fake it\n";
176         return false;
177 }
178
179
180 void readSymbols(string const & filename)
181 {
182         lyxerr[Debug::MATHED] << "read symbols from " << filename << "\n";
183         std::ifstream fs(filename.c_str());
184         while (fs) {
185                 int charid     = 0;
186                 int fallbackid = 0;
187                 latexkeys tmp;
188                 string line;
189                 getline(fs, line);
190                 istringstream is(line);
191                 is      >> tmp.name
192                                 >> tmp.inset
193                                 >> charid
194                                 >> fallbackid
195                                 >> tmp.extra
196                                 >> tmp.xmlname;
197                 if (!is)
198                         continue;
199
200                 // tmp.inset _is_ the fontname here.
201                 // create fallbacks if necessary
202                 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra=="special") {
203                         lyxerr[Debug::MATHED] << "symbol abuse for " << tmp.name << "\n";
204                         tmp.draw = tmp.name;
205                 } else if (math_font_available(tmp.inset)) {
206                         lyxerr[Debug::MATHED] << "symbol available for " << tmp.name << "\n";
207                         tmp.draw += char(charid);
208                 } else if (fallbackid) {
209                         if (tmp.inset == "cmex")
210                                 tmp.inset  = "lyxsymbol";
211                         else
212                                 tmp.inset  = "lyxboldsymbol";
213                         lyxerr[Debug::MATHED] << "symbol fallback for " << tmp.name << "\n";
214                         tmp.draw += char(fallbackid); 
215                 } else {
216                         lyxerr[Debug::MATHED] << "faking " << tmp.name << "\n";
217                         tmp.draw = tmp.name;
218                         tmp.inset = "lyxtex";
219                 }
220
221                 if (theWordList.find(tmp.name) != theWordList.end())
222                         lyxerr[Debug::MATHED] << "readSymbols: inset " << tmp.name
223                                << " already exists.\n";
224                 else
225                         theWordList[tmp.name] = tmp;
226                 lyxerr[Debug::MATHED] << "read symbol '" << tmp.name
227                                         <<  "  inset: " << tmp.inset
228                                         <<  "  draw: " << int(tmp.draw[0])
229                                         <<  "  extra: " << tmp.extra
230                                         << "'\n";
231         }
232 }
233
234
235 void initSymbols()
236 {
237         unsigned const n = sizeof(wordlist_array) / sizeof(wordlist_array[0]);
238         for (key_type * p = wordlist_array; p != wordlist_array + n; ++p) {
239                 latexkeys tmp;
240                 tmp.name  = p->name;
241                 tmp.inset = p->inset;
242                 tmp.draw  = p->name;
243                 theWordList[p->name] = tmp;
244         }
245
246         lyxerr[Debug::MATHED] << "reading symbols file\n";
247         string const file = LibFileSearch(string(), "symbols");
248         if (file.empty())
249                 lyxerr << "Could not find symbols file\n";
250         else
251                 readSymbols(file);
252 }
253
254
255 } // namespace anon
256
257
258 latexkeys const * in_word_set(string const & str)
259 {
260         static bool initialized = false;
261
262         if (!initialized) {
263                 initSymbols();
264                 initialized = true;
265         }
266
267         WordList::iterator it = theWordList.find(str);
268         //lyxerr << "looking up '" << str << "' found: "
269         // << (it != theWordList.end()) << "\n";
270         return (it != theWordList.end()) ? &(it->second) : 0;
271 }
272
273
274 MathAtom createMathInset(string const & s)
275 {
276         lyxerr[Debug::MATHED] << "creating inset with name: '" << s << "'\n";
277         if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
278                 return MathAtom(new MathMacroArgument(s[1] - '0'));
279
280         if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
281                         && s[2] >= '1' && s[2] <= '9')
282                 return MathAtom(new MathMacroArgument(s[2] - '0'));
283         if (s == "kern")
284                 return MathAtom(new MathKernInset);
285         if (s == "xymatrix")
286                 return MathAtom(new MathXYMatrixInset);
287         if (s == "xrightarrow" || s == "xleftarrow")
288                 return MathAtom(new MathXArrowInset(s));
289         if (s == "split" || s == "gathered" || s == "aligned")
290                 return MathAtom(new MathSplitInset(s));
291         if (s == "cases")
292                 return MathAtom(new MathCasesInset);
293         if (s == "substack")
294                 return MathAtom(new MathSubstackInset);
295         if (s == "subarray" || s == "array")
296                 return MathAtom(new MathArrayInset(s, 1, 1));
297         if (s == "sqrt")
298                 return MathAtom(new MathSqrtInset);
299         if (s == "root")
300                 return MathAtom(new MathRootInset);
301         if (s == "stack")
302                 return MathAtom(new MathStackrelInset);
303         if (s == "binom" || s == "choose")
304                 return MathAtom(new MathBinomInset);
305         if (s == "over" || s == "frac")
306                 return MathAtom(new MathFracInset);
307         if (s == "atop")
308                 return MathAtom(new MathFracInset(true));
309         if (s == "not")
310                 return MathAtom(new MathNotInset);
311         if (s == "lefteqn")
312                 return MathAtom(new MathLefteqnInset);
313         if (s == "ref")
314                 return MathAtom(new RefInset);
315
316
317         latexkeys const * l = in_word_set(s);
318         if (l) {
319                 string const & inset = l->inset;
320                 lyxerr[Debug::MATHED] << " found inset: '" << inset << "'\n";
321                 if (inset == "underset")
322                         return MathAtom(new MathUndersetInset);
323                 if (inset == "decoration")
324                         return MathAtom(new MathDecorationInset(l->name));
325                 if (inset == "space")
326                         return MathAtom(new MathSpaceInset(l->name));
327                 if (inset == "dots")
328                         return MathAtom(new MathDotsInset(l->name));
329                 if (inset == "box")
330                         return MathAtom(new MathBoxInset(l->name));
331                 if (inset == "fbox")
332                         return MathAtom(new MathFboxInset);
333                 if (inset == "style")
334                         return MathAtom(new MathSizeInset(l));
335                 if (inset == "font")
336                         return MathAtom(new MathFontInset(l->name));
337                 if (inset == "oldfont")
338                         return MathAtom(new MathFontInset(l->name));
339                 if (inset == "matrix")
340                         return MathAtom(new MathAMSArrayInset(s));
341                 return MathAtom(new MathSymbolInset(l));
342         }
343
344         if (MathMacroTable::has(s))
345                 return MathAtom(new MathMacro(s));
346
347         //lyxerr[Debug::MATHED] << "creating inset 2 with name: '" << s << "'\n";
348         return MathAtom(new MathUnknownInset(s));
349 }