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