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