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