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