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