]> git.lyx.org Git - lyx.git/blob - src/mathed/MathFactory.C
Fix some unicode conversion problems, more work needed.
[lyx.git] / src / mathed / MathFactory.C
1 /**
2  * \file MathFactory.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "MathFactory.h"
14
15 #include "InsetMathAMSArray.h"
16 #include "InsetMathArray.h"
17 #include "InsetMathBinom.h"
18 #include "InsetMathBoldSymbol.h"
19 #include "InsetMathBoxed.h"
20 #include "InsetMathBox.h"
21 #include "InsetMathCases.h"
22 #include "InsetMathColor.h"
23 #include "InsetMathDecoration.h"
24 #include "InsetMathDFrac.h"
25 #include "InsetMathDots.h"
26 #include "InsetMathFBox.h"
27 #include "InsetMathFont.h"
28 #include "InsetMathFontOld.h"
29 #include "InsetMathFrac.h"
30 #include "InsetMathFrameBox.h"
31 #include "InsetMathKern.h"
32 #include "InsetMathLefteqn.h"
33 #include "InsetMathMacro.h"
34 #include "InsetMathMakebox.h"
35 #include "InsetMathOverset.h"
36 #include "InsetMathPhantom.h"
37 #include "InsetMathRef.h"
38 #include "InsetMathRoot.h"
39 #include "InsetMathSize.h"
40 #include "InsetMathSpace.h"
41 #include "InsetMathSplit.h"
42 #include "InsetMathSqrt.h"
43 #include "InsetMathStackrel.h"
44 #include "InsetMathSubstack.h"
45 #include "InsetMathSymbol.h"
46 #include "InsetMathTabular.h"
47 #include "InsetMathTFrac.h"
48 #include "InsetMathUnderset.h"
49 #include "InsetMathUnknown.h"
50 #include "InsetMathXArrow.h"
51 #include "InsetMathXYMatrix.h"
52 #include "MathMacroArgument.h"
53 #include "MathMacroTable.h"
54 #include "MathMacroTemplate.h"
55 #include "MathParser.h"
56 #include "MathSupport.h"
57
58 #include "debug.h"
59
60 #include "support/filetools.h" // LibFileSearch
61 #include "support/lstrings.h"
62
63 #include "frontends/FontLoader.h"
64
65 #include <fstream>
66 #include <sstream>
67
68
69 namespace lyx {
70
71 using support::libFileSearch;
72 using support::split;
73
74 using std::string;
75 using std::endl;
76 using std::istringstream;
77 using std::vector;
78
79 bool has_math_fonts;
80
81
82 namespace {
83
84 // file scope
85 typedef std::map<string, latexkeys> WordList;
86
87 WordList theWordList;
88
89
90 bool math_font_available(string & name)
91 {
92         LyXFont f;
93         augmentFont(f, name);
94
95         // Do we have the font proper?
96         if (theFontLoader().available(f))
97                 return true;
98
99         // can we fake it?
100         if (name == "eufrak") {
101                 name = "lyxfakefrak";
102                 return true;
103         }
104
105         lyxerr[Debug::MATHED]
106                 << "font " << name << " not available and I can't fake it"
107                 << endl;
108         return false;
109 }
110
111
112 void initSymbols()
113 {
114         string const filename = libFileSearch(string(), "symbols");
115         lyxerr[Debug::MATHED] << "read symbols from " << filename << endl;
116         if (filename.empty()) {
117                 lyxerr << "Could not find symbols file" << endl;
118                 return;
119         }
120
121         std::ifstream fs(filename.c_str());
122         string line;
123         bool skip = false;
124         while (getline(fs, line)) {
125                 int charid     = 0;
126                 int fallbackid = 0;
127                 if (!line.empty() && line[0] == '#')
128                         continue;
129
130                 // special case of iffont/else/endif
131                 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
132                         istringstream is(line);
133                         string tmp;
134                         is >> tmp;
135                         is >> tmp;
136                         skip = !math_font_available(tmp);
137                         continue;
138                 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
139                         skip = !skip;
140                 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
141                         skip = false;
142                         continue;
143                 } else if (skip)
144                         continue;
145
146                 // special case of pre-defined macros
147                 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
148                         //lyxerr << "macro definition: '" << line << '\'' << endl;
149                         MacroTable::globalMacros().insert(line);
150                         continue;
151                 }
152
153                 istringstream is(line);
154                 latexkeys tmp;
155                 is >> tmp.name >> tmp.inset;
156                 if (isFontName(tmp.inset))
157                         is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
158                 else
159                         is >> tmp.extra;
160                 if (!is) {
161                         lyxerr[Debug::MATHED] << "skipping line '" << line << '\'' << endl;
162                         lyxerr[Debug::MATHED]
163                                 << tmp.name << ' ' << tmp.inset << ' ' << tmp.extra << endl;
164                         continue;
165                 }
166
167                 if (isFontName(tmp.inset)) {
168                         // tmp.inset _is_ the fontname here.
169                         // create fallbacks if necessary
170
171                         // store requirements as long as we can
172                         if (tmp.inset == "msa" || tmp.inset == "msb")
173                                 tmp.requires = "amssymb";
174                         // See http://bugzilla.lyx.org/show_bug.cgi?id=1942
175                         // else if (tmp.inset == "wasy")
176                         //      tmp.requires = "wasysym";
177
178                         // symbol font is not available sometimes
179                         string symbol_font = "lyxsymbol";
180
181                         if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
182                                 lyxerr[Debug::MATHED] << "symbol abuse for " << tmp.name << endl;
183                                 // FIXME UNICODE
184                                 vector<char_type> n(tmp.name.begin(), tmp.name.end());
185                                 tmp.draw = n;
186                         } else if (math_font_available(tmp.inset)) {
187                                 lyxerr[Debug::MATHED] << "symbol available for " << tmp.name << endl;
188                                 tmp.draw.push_back(char_type(charid));
189                         } else if (fallbackid && math_font_available(symbol_font)) {
190                                 if (tmp.inset == "cmex")
191                                         tmp.inset  = "lyxsymbol";
192                                 else
193                                         tmp.inset  = "lyxboldsymbol";
194                                 lyxerr[Debug::MATHED] << "symbol fallback for " << tmp.name << endl;
195                                 tmp.draw.push_back(char_type(fallbackid));
196                         } else {
197                                 lyxerr[Debug::MATHED] << "faking " << tmp.name << endl;
198                                 vector<char_type> n(tmp.name.begin(),
199                                                     tmp.name.end());
200                                 
201                                 tmp.draw = n;
202                                 tmp.inset = "lyxtex";
203                         }
204                 } else {
205                         // it's a proper inset
206                         lyxerr[Debug::MATHED] << "inset " << tmp.inset
207                                               << " used for " << tmp.name
208                                               << endl;
209                 }
210
211                 if (theWordList.find(tmp.name) != theWordList.end())
212                         lyxerr[Debug::MATHED]
213                                 << "readSymbols: inset " << tmp.name
214                                 << " already exists." << endl;
215                 else
216                         theWordList[tmp.name] = tmp;
217
218                 lyxerr[Debug::MATHED]
219                         << "read symbol '" << tmp.name
220                         << "  inset: " << tmp.inset
221                         << "  draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
222                         << "  extra: " << tmp.extra
223                         << '\'' << endl;
224         }
225         string tmp = "cmm";
226         string tmp2 = "cmsy";
227         has_math_fonts = math_font_available(tmp) && math_font_available(tmp2);
228 }
229
230
231 } // namespace anon
232
233
234 void initMath()
235 {
236         static bool initialized = false;
237         if (!initialized) {
238                 initialized = true;
239                 initParser();
240                 initSymbols();
241         }
242 }
243
244
245 latexkeys const * in_word_set(string const & str)
246 {
247         WordList::iterator it = theWordList.find(str);
248         return it != theWordList.end() ? &(it->second) : 0;
249 }
250
251
252 MathAtom createInsetMath(string const & s)
253 {
254         //lyxerr << "creating inset with name: '" << s << '\'' << endl;
255         latexkeys const * l = in_word_set(s);
256         if (l) {
257                 string const & inset = l->inset;
258                 //lyxerr << " found inset: '" << inset << '\'' << endl;
259                 if (inset == "ref")
260                         return MathAtom(new RefInset(l->name));
261                 if (inset == "overset")
262                         return MathAtom(new InsetMathOverset);
263                 if (inset == "underset")
264                         return MathAtom(new InsetMathUnderset);
265                 if (inset == "decoration")
266                         return MathAtom(new InsetMathDecoration(l));
267                 if (inset == "space")
268                         return MathAtom(new InsetMathSpace(l->name));
269                 if (inset == "dots")
270                         return MathAtom(new InsetMathDots(l));
271                 if (inset == "mbox")
272                         // return MathAtom(new InsetMathMBox);
273                         // InsetMathMBox is proposed to replace InsetMathBox,
274                         // but is not ready yet (it needs a BufferView for
275                         // construction)
276                         return MathAtom(new InsetMathBox(l->name));
277 //              if (inset == "fbox")
278 //                      return MathAtom(new InsetMathFBox(l));
279                 if (inset == "style")
280                         return MathAtom(new InsetMathSize(l));
281                 if (inset == "font")
282                         return MathAtom(new InsetMathFont(l));
283                 if (inset == "oldfont")
284                         return MathAtom(new InsetMathFontOld(l));
285                 if (inset == "matrix")
286                         return MathAtom(new InsetMathAMSArray(s));
287                 if (inset == "split")
288                         return MathAtom(new InsetMathSplit(s));
289                 if (inset == "big")
290                         // we can't create a InsetMathBig, since the argument
291                         // is missing.
292                         return MathAtom(new InsetMathUnknown(s));
293                 return MathAtom(new InsetMathSymbol(l));
294         }
295
296         if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
297                 return MathAtom(new MathMacroArgument(s[1] - '0'));
298         if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
299                         && s[2] >= '1' && s[2] <= '9')
300                 return MathAtom(new MathMacroArgument(s[2] - '0'));
301         if (s == "boxed")
302                 return MathAtom(new InsetMathBoxed());
303         if (s == "fbox")
304                 return MathAtom(new InsetMathFBox());
305         if (s == "framebox")
306                 return MathAtom(new InsetMathFrameBox);
307         if (s == "makebox")
308                 return MathAtom(new InsetMathMakebox);
309         if (s == "kern")
310                 return MathAtom(new InsetMathKern);
311         if (s == "xymatrix")
312                 return MathAtom(new InsetMathXYMatrix);
313         if (s == "xrightarrow" || s == "xleftarrow")
314                 return MathAtom(new InsetMathXArrow(s));
315         if (s == "split" || s == "gathered" || s == "aligned" || s == "alignedat")
316                 return MathAtom(new InsetMathSplit(s));
317         if (s == "cases")
318                 return MathAtom(new InsetMathCases);
319         if (s == "substack")
320                 return MathAtom(new InsetMathSubstack);
321         if (s == "subarray" || s == "array")
322                 return MathAtom(new InsetMathArray(s, 1, 1));
323         if (s == "sqrt")
324                 return MathAtom(new InsetMathSqrt);
325         if (s == "root")
326                 return MathAtom(new InsetMathRoot);
327         if (s == "tabular")
328                 return MathAtom(new InsetMathTabular(s, 1, 1));
329         if (s == "stackrel")
330                 return MathAtom(new InsetMathStackrel);
331         if (s == "binom" || s == "choose")
332                 return MathAtom(new InsetMathBinom(s == "choose"));
333         if (s == "frac")
334                 return MathAtom(new InsetMathFrac);
335         if (s == "over")
336                 return MathAtom(new InsetMathFrac(InsetMathFrac::OVER));
337         if (s == "nicefrac")
338                 return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC));
339         //if (s == "infer")
340         //      return MathAtom(new MathInferInset);
341         if (s == "atop")
342                 return MathAtom(new InsetMathFrac(InsetMathFrac::ATOP));
343         if (s == "lefteqn")
344                 return MathAtom(new InsetMathLefteqn);
345         if (s == "boldsymbol")
346                 return MathAtom(new InsetMathBoldSymbol);
347         if (s == "color" || s == "normalcolor")
348                 return MathAtom(new InsetMathColor(true));
349         if (s == "textcolor")
350                 return MathAtom(new InsetMathColor(false));
351         if (s == "dfrac")
352                 return MathAtom(new InsetMathDFrac);
353         if (s == "tfrac")
354                 return MathAtom(new InsetMathTFrac);
355         if (s == "hphantom")
356                 return MathAtom(new InsetMathPhantom(InsetMathPhantom::hphantom));
357         if (s == "phantom")
358                 return MathAtom(new InsetMathPhantom(InsetMathPhantom::phantom));
359         if (s == "vphantom")
360                 return MathAtom(new InsetMathPhantom(InsetMathPhantom::vphantom));
361
362         if (MacroTable::globalMacros().has(s))
363                 return MathAtom(new MathMacro(s,
364                         MacroTable::globalMacros().get(s).numargs()));
365         //if (MacroTable::localMacros().has(s))
366         //      return MathAtom(new MathMacro(s,
367         //              MacroTable::localMacros().get(s).numargs()));
368
369         //lyxerr << "creating unknown inset '" << s << "'" << endl;
370         return MathAtom(new InsetMathUnknown(s));
371 }
372
373
374 bool createInsetMath_fromDialogStr(string const & str, MathArray & ar)
375 {
376         // An example str:
377         // "ref LatexCommand \\ref{sec:Title}\n\\end_inset\n\n";
378         string name;
379         string body = split(str, name, ' ');
380
381         if (name != "ref" )
382                 return false;
383
384         // body comes with a head "LatexCommand " and a
385         // tail "\nend_inset\n\n". Strip them off.
386         string trimmed;
387         body = split(body, trimmed, ' ');
388         split(body, trimmed, '\n');
389
390         mathed_parse_cell(ar, trimmed);
391         if (ar.size() != 1)
392                 return false;
393
394         return ar[0].nucleus();
395 }
396
397
398 } // namespace lyx