]> git.lyx.org Git - lyx.git/blob - src/mathed/math_factory.C
several smallish changes/bugfixes/left overs from Porto
[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         std::ifstream fs(filename.c_str());
183         while (fs) {
184                 int charid     = 0;
185                 int fallbackid = 0;
186                 latexkeys tmp;
187                 string line;
188                 getline(fs, line);
189                 istringstream is(line);
190                 is      >> tmp.name
191                                 >> tmp.inset
192                                 >> charid
193                                 >> fallbackid
194                                 >> tmp.extra
195                                 >> tmp.xmlname;
196                 if (!is)
197                         continue;
198
199                 // tmp.inset _is_ the fontname here.
200                 // create fallbacks if necessary
201                 if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra=="special") {
202                         lyxerr[Debug::MATHED] << "symbol abuse for " << tmp.name << "\n";
203                         tmp.draw = tmp.name;
204                 } else if (math_font_available(tmp.inset)) {
205                         lyxerr[Debug::MATHED] << "symbol available for " << tmp.name << "\n";
206                         tmp.draw += char(charid);
207                 } else if (fallbackid) {
208                         if (tmp.inset == "cmex")
209                                 tmp.inset  = "lyxsymbol";
210                         else
211                                 tmp.inset  = "lyxboldsymbol";
212                         lyxerr[Debug::MATHED] << "symbol fallback for " << tmp.name << "\n";
213                         tmp.draw += char(fallbackid); 
214                 } else {
215                         lyxerr[Debug::MATHED] << "faking " << tmp.name << "\n";
216                         tmp.draw = tmp.name;
217                         tmp.inset = "lyxtex";
218                 }
219
220                 if (theWordList.find(tmp.name) != theWordList.end())
221                         lyxerr[Debug::MATHED] << "readSymbols: inset " << tmp.name
222                                << " already exists.\n";
223                 else
224                         theWordList[tmp.name] = tmp;
225                 lyxerr[Debug::MATHED] << "read symbol '" << tmp.name
226                                         <<  "  inset: " << tmp.inset
227                                         <<  "  draw: " << int(tmp.draw[0])
228                                         <<  "  extra: " << tmp.extra
229                                         << "'\n";
230         }
231 }
232
233
234 void initSymbols()
235 {
236         unsigned const n = sizeof(wordlist_array) / sizeof(wordlist_array[0]);
237         for (key_type * p = wordlist_array; p != wordlist_array + n; ++p) {
238                 latexkeys tmp;
239                 tmp.name  = p->name;
240                 tmp.inset = p->inset;
241                 tmp.draw  = p->name;
242                 theWordList[p->name] = tmp;
243         }
244
245         lyxerr[Debug::MATHED] << "reading symbols file\n";
246         string const file = LibFileSearch(string(), "symbols");
247         if (file.empty())
248                 lyxerr << "Could not find symbols file\n";
249         else
250                 readSymbols(file);
251 }
252
253
254 } // namespace anon
255
256
257 latexkeys const * in_word_set(string const & str)
258 {
259         static bool initialized = false;
260
261         if (!initialized) {
262                 initSymbols();
263                 initialized = true;
264         }
265
266         WordList::iterator it = theWordList.find(str);
267         //lyxerr << "looking up '" << str << "' found: "
268         // << (it != theWordList.end()) << "\n";
269         return (it != theWordList.end()) ? &(it->second) : 0;
270 }
271
272
273 MathAtom createMathInset(string const & s)
274 {
275         lyxerr[Debug::MATHED] << "creating inset with name: '" << s << "'\n";
276         if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
277                 return MathAtom(new MathMacroArgument(s[1] - '0'));
278
279         if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
280                         && s[2] >= '1' && s[2] <= '9')
281                 return MathAtom(new MathMacroArgument(s[2] - '0'));
282         if (s == "kern")
283                 return MathAtom(new MathKernInset);
284         if (s == "xymatrix")
285                 return MathAtom(new MathXYMatrixInset);
286         if (s == "xrightarrow" || s == "xleftarrow")
287                 return MathAtom(new MathXArrowInset(s));
288         if (s == "split" || s == "gathered" || s == "aligned")
289                 return MathAtom(new MathSplitInset(s));
290         if (s == "cases")
291                 return MathAtom(new MathCasesInset);
292         if (s == "substack")
293                 return MathAtom(new MathSubstackInset);
294         if (s == "subarray" || s == "array")
295                 return MathAtom(new MathArrayInset(s, 1, 1));
296         if (s == "sqrt")
297                 return MathAtom(new MathSqrtInset);
298         if (s == "root")
299                 return MathAtom(new MathRootInset);
300         if (s == "stack")
301                 return MathAtom(new MathStackrelInset);
302         if (s == "binom" || s == "choose")
303                 return MathAtom(new MathBinomInset);
304         if (s == "over" || s == "frac")
305                 return MathAtom(new MathFracInset);
306         if (s == "atop")
307                 return MathAtom(new MathFracInset(true));
308         if (s == "not")
309                 return MathAtom(new MathNotInset);
310         if (s == "lefteqn")
311                 return MathAtom(new MathLefteqnInset);
312         if (s == "ref")
313                 return MathAtom(new RefInset);
314
315
316         latexkeys const * l = in_word_set(s);
317         if (l) {
318                 string const & inset = l->inset;
319                 lyxerr[Debug::MATHED] << " found inset: '" << inset << "'\n";
320                 if (inset == "underset")
321                         return MathAtom(new MathUndersetInset);
322                 if (inset == "decoration")
323                         return MathAtom(new MathDecorationInset(l->name));
324                 if (inset == "space")
325                         return MathAtom(new MathSpaceInset(l->name));
326                 if (inset == "dots")
327                         return MathAtom(new MathDotsInset(l->name));
328                 if (inset == "box")
329                         return MathAtom(new MathBoxInset(l->name));
330                 if (inset == "fbox")
331                         return MathAtom(new MathFboxInset);
332                 if (inset == "style")
333                         return MathAtom(new MathSizeInset(l));
334                 if (inset == "font")
335                         return MathAtom(new MathFontInset(l->name));
336                 if (inset == "oldfont")
337                         return MathAtom(new MathFontInset(l->name));
338                 if (inset == "matrix")
339                         return MathAtom(new MathAMSArrayInset(s));
340                 return MathAtom(new MathSymbolInset(l));
341         }
342
343         if (MathMacroTable::has(s))
344                 return MathAtom(new MathMacro(s));
345
346         //lyxerr[Debug::MATHED] << "creating inset 2 with name: '" << s << "'\n";
347         return MathAtom(new MathUnknownInset(s));
348 }