]> git.lyx.org Git - lyx.git/blob - src/mathed/math_factory.C
support for AMS's \boxed{} command
[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_fontoldinset.h"
14 #include "math_fracinset.h"
15 #include "math_kerninset.h"
16 #include "math_lefteqninset.h"
17 #include "math_macro.h"
18 #include "math_macrotable.h"
19 #include "math_macrotemplate.h"
20 #include "math_macroarg.h"
21 #include "math_parboxinset.h"
22 #include "math_rootinset.h"
23 #include "math_sizeinset.h"
24 #include "math_spaceinset.h"
25 #include "math_splitinset.h"
26 #include "math_sqrtinset.h"
27 #include "math_stackrelinset.h"
28 #include "math_substackinset.h"
29 #include "math_symbolinset.h"
30 #include "math_undersetinset.h"
31 #include "math_unknowninset.h"
32 #include "math_xarrowinset.h"
33 #include "math_xymatrixinset.h"
34 #include "math_xyarrowinset.h"
35
36 //#include "insets/insetref.h"
37 #include "ref_inset.h"
38
39 #include "math_metricsinfo.h"
40 #include "debug.h"
41 #include "math_support.h"
42 #include "Lsstream.h"
43 #include "support/filetools.h" // LibFileSearch
44 #include "frontends/lyx_gui.h"
45
46 #include <map>
47 #include <fstream>
48
49
50 namespace {
51
52 // file scope
53 typedef std::map<string, latexkeys> WordList;
54 WordList theWordList;
55
56
57 bool math_font_available(string & name)
58 {
59         LyXFont f;
60         augmentFont(f, name);
61
62         // Do we have the font proper?
63         if (lyx_gui::font_available(f))
64                 return true;
65
66         // can we fake it?
67         if (name == "eufrak") {
68                 name = "lyxfakefrak";
69                 return true;
70         }
71
72         lyxerr[Debug::MATHED]
73                 << "font " << name << " not available and I can't fake it\n";
74         return false;
75 }
76
77
78 void initSymbols()
79 {
80         string const filename = LibFileSearch(string(), "symbols");
81         lyxerr[Debug::MATHED] << "read symbols from " << filename << "\n";
82         if (filename.empty()) {
83                 lyxerr << "Could not find symbols file\n";
84                 return;
85         }
86
87         std::ifstream fs(filename.c_str());
88         string line;
89         while (getline(fs, line)) {
90                 int charid     = 0;
91                 int fallbackid = 0;
92                 latexkeys tmp;
93                 if (line.size() > 0 && line[0] == '#')
94                         continue;
95
96                 // special case of pre-defined macros
97                 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
98                         lyxerr << "defining: '" << line << "'\n";
99                         istringstream is(line);
100                         MathMacroTable::create(MathAtom(new MathMacroTemplate(is)));
101                         continue;
102                 }
103
104                 istringstream is(line);
105                 is >> tmp.name >> tmp.inset;
106                 if (isFontName(tmp.inset)) 
107                         is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
108                 else
109                         is >> tmp.extra;
110                 if (!is) {
111                         lyxerr[Debug::MATHED] << "skipping line '" << line << "'\n";
112                         lyxerr[Debug::MATHED]
113                                 << tmp.name << ' ' << tmp.inset << ' ' << tmp.extra << "\n";
114                         continue;
115                 }
116
117                 if (isFontName(tmp.inset)) {
118                         // tmp.inset _is_ the fontname here.
119                         // create fallbacks if necessary
120                         if (tmp.extra=="func" || tmp.extra=="funclim" || tmp.extra=="special") {
121                                 lyxerr[Debug::MATHED] << "symbol abuse for " << tmp.name << "\n";
122                                 tmp.draw = tmp.name;
123                         } else if (math_font_available(tmp.inset)) {
124                                 lyxerr[Debug::MATHED] << "symbol available for " << tmp.name << "\n";
125                                 tmp.draw += char(charid);
126                         } else if (fallbackid) {
127                                 if (tmp.inset == "cmex")
128                                         tmp.inset  = "lyxsymbol";
129                                 else
130                                         tmp.inset  = "lyxboldsymbol";
131                                 lyxerr[Debug::MATHED] << "symbol fallback for " << tmp.name << "\n";
132                                 tmp.draw += char(fallbackid); 
133                         } else {
134                                 lyxerr[Debug::MATHED] << "faking " << tmp.name << "\n";
135                                 tmp.draw = tmp.name;
136                                 tmp.inset = "lyxtex";
137                         }
138                 } else {
139                         // it's a proper inset
140                         lyxerr[Debug::MATHED] << "inset " << tmp.inset << " used for "
141                                 << tmp.name << "\n";
142                 }
143
144                 if (theWordList.find(tmp.name) != theWordList.end())
145                         lyxerr[Debug::MATHED] << "readSymbols: inset " << tmp.name
146                                 << " already exists.\n";
147                 else
148                         theWordList[tmp.name] = tmp;
149                 lyxerr[Debug::MATHED] << "read symbol '" << tmp.name
150                                         <<  "  inset: " << tmp.inset
151                                         <<  "  draw: " << int(tmp.draw[0])
152                                         <<  "  extra: " << tmp.extra
153                                         << "'\n";
154         }
155 }
156
157
158 } // namespace anon
159
160
161 void initMath()
162 {
163         static bool initialized = false;
164         if (!initialized) {
165                 initSymbols();
166                 initialized = true;
167         }
168 }
169
170
171 latexkeys const * in_word_set(string const & str)
172 {
173         WordList::iterator it = theWordList.find(str);
174         //lyxerr << "looking up '" << str << "' found: "
175         // << (it != theWordList.end()) << "\n";
176         return (it != theWordList.end()) ? &(it->second) : 0;
177 }
178
179
180 MathAtom createMathInset(string const & s)
181 {
182         lyxerr[Debug::MATHED] << "creating inset with name: '" << s << "'\n";
183         if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
184                 return MathAtom(new MathMacroArgument(s[1] - '0'));
185
186         if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
187                         && s[2] >= '1' && s[2] <= '9')
188                 return MathAtom(new MathMacroArgument(s[2] - '0'));
189         if (s == "kern")
190                 return MathAtom(new MathKernInset);
191         if (s == "xymatrix")
192                 return MathAtom(new MathXYMatrixInset);
193         if (s == "xrightarrow" || s == "xleftarrow")
194                 return MathAtom(new MathXArrowInset(s));
195         if (s == "split" || s == "gathered" || s == "aligned")
196                 return MathAtom(new MathSplitInset(s));
197         if (s == "cases")
198                 return MathAtom(new MathCasesInset);
199         if (s == "substack")
200                 return MathAtom(new MathSubstackInset);
201         if (s == "subarray" || s == "array")
202                 return MathAtom(new MathArrayInset(s, 1, 1));
203         if (s == "sqrt")
204                 return MathAtom(new MathSqrtInset);
205         if (s == "root")
206                 return MathAtom(new MathRootInset);
207         if (s == "stackrel")
208                 return MathAtom(new MathStackrelInset);
209         if (s == "binom" || s == "choose")
210                 return MathAtom(new MathBinomInset(s == "choose"));
211         if (s == "over" || s == "frac")
212                 return MathAtom(new MathFracInset);
213         if (s == "atop")
214                 return MathAtom(new MathFracInset(true));
215         if (s == "lefteqn")
216                 return MathAtom(new MathLefteqnInset);
217
218         latexkeys const * l = in_word_set(s);
219         if (l) {
220                 string const & inset = l->inset;
221                 lyxerr[Debug::MATHED] << " found inset: '" << inset << "'\n";
222                 if (inset == "ref")
223                         return MathAtom(new RefInset(l->name));
224                 if (inset == "underset")
225                         return MathAtom(new MathUndersetInset);
226                 if (inset == "decoration")
227                         return MathAtom(new MathDecorationInset(l));
228                 if (inset == "space")
229                         return MathAtom(new MathSpaceInset(l->name));
230                 if (inset == "dots")
231                         return MathAtom(new MathDotsInset(l));
232                 if (inset == "mbox")
233                         return MathAtom(new MathBoxInset(l->name));
234                 if (inset == "parbox")
235                         return MathAtom(new MathParboxInset);
236                 if (inset == "fbox")
237                         return MathAtom(new MathFboxInset(l));
238                 if (inset == "style")
239                         return MathAtom(new MathSizeInset(l));
240                 if (inset == "font")
241                         return MathAtom(new MathFontInset(l));
242                 if (inset == "oldfont")
243                         return MathAtom(new MathFontOldInset(l));
244                 if (inset == "matrix")
245                         return MathAtom(new MathAMSArrayInset(s));
246                 return MathAtom(new MathSymbolInset(l));
247         }
248
249         if (MathMacroTable::has(s))
250                 return MathAtom(new MathMacro(s));
251
252         //lyxerr[Debug::MATHED] << "creating inset 2 with name: '" << s << "'\n";
253         return MathAtom(new MathUnknownInset(s));
254 }