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