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