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