]> git.lyx.org Git - lyx.git/blob - src/mathed/MathFactory.cpp
Automate the setting of the ascii only flag.
[lyx.git] / src / mathed / MathFactory.cpp
1 /**
2  * \file MathFactory.cpp
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 "InsetMathBoldSymbol.h"
18 #include "InsetMathBox.h"
19 #include "InsetMathCases.h"
20 #include "InsetMathColor.h"
21 #include "InsetMathDecoration.h"
22 #include "InsetMathDots.h"
23 #include "InsetMathEnsureMath.h"
24 #include "InsetMathFont.h"
25 #include "InsetMathFontOld.h"
26 #include "InsetMathFrac.h"
27 #include "InsetMathKern.h"
28 #include "InsetMathLefteqn.h"
29 #include "InsetMathOverset.h"
30 #include "InsetMathPhantom.h"
31 #include "InsetMathRef.h"
32 #include "InsetMathRoot.h"
33 #include "InsetMathSize.h"
34 #include "InsetMathSpace.h"
35 #include "InsetMathSpecialChar.h"
36 #include "InsetMathSplit.h"
37 #include "InsetMathSqrt.h"
38 #include "InsetMathStackrel.h"
39 #include "InsetMathSubstack.h"
40 #include "InsetMathSymbol.h"
41 #include "InsetMathTabular.h"
42 #include "InsetMathUnderset.h"
43 #include "InsetMathUnknown.h"
44 #include "InsetMathHull.h"
45 #include "InsetMathXArrow.h"
46 #include "InsetMathXYMatrix.h"
47 #include "MacroTable.h"
48 #include "MathMacro.h"
49 #include "MathMacroArgument.h"
50 #include "MathParser.h"
51 #include "MathStream.h"
52 #include "MathSupport.h"
53
54 #include "insets/InsetCommand.h"
55 #include "insets/InsetSpace.h"
56
57 #include "support/debug.h"
58 #include "support/docstream.h"
59 #include "support/FileName.h"
60 #include "support/filetools.h" // LibFileSearch
61 #include "support/lstrings.h"
62
63 #include "frontends/FontLoader.h"
64
65 #include "Buffer.h"
66 #include "BufferParams.h"
67 #include "Encoding.h"
68 #include "LyX.h" // use_gui
69 #include "OutputParams.h"
70
71 using namespace std;
72 using namespace lyx::support;
73
74 namespace lyx {
75
76 bool has_math_fonts;
77
78
79 namespace {
80
81 MathWordList theWordList;
82
83
84 bool isMathFontAvailable(docstring & name)
85 {
86         if (!use_gui)
87                 return false;
88
89         FontInfo f;
90         augmentFont(f, name);
91
92         // Do we have the font proper?
93         if (theFontLoader().available(f))
94                 return true;
95
96         // can we fake it?
97         if (name == "eufrak") {
98                 name = from_ascii("lyxfakefrak");
99                 return true;
100         }
101
102         LYXERR(Debug::MATHED,
103                 "font " << to_utf8(name) << " not available and I can't fake it");
104         return false;
105 }
106
107
108 void initSymbols()
109 {
110         FileName const filename = libFileSearch(string(), "symbols");
111         LYXERR(Debug::MATHED, "read symbols from " << filename);
112         if (filename.empty()) {
113                 lyxerr << "Could not find symbols file" << endl;
114                 return;
115         }
116
117         ifstream fs(filename.toFilesystemEncoding().c_str());
118         string line;
119         bool skip = false;
120         while (getline(fs, line)) {
121                 int charid     = 0;
122                 int fallbackid = 0;
123                 if (line.empty() || line[0] == '#')
124                         continue;
125
126                 // special case of iffont/else/endif
127                 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
128                         istringstream is(line);
129                         string tmp;
130                         is >> tmp;
131                         is >> tmp;
132                         docstring t = from_utf8(tmp);
133                         skip = !isMathFontAvailable(t);
134                         continue;
135                 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
136                         skip = !skip;
137                 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
138                         skip = false;
139                         continue;
140                 } else if (skip)
141                         continue;
142
143                 // special case of pre-defined macros
144                 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
145                         //lyxerr << "macro definition: '" << line << '\'' << endl;
146                         istringstream is(line);
147                         string macro;
148                         string requires;
149                         is >> macro >> requires;
150                         MacroTable::globalMacros().insert(0, from_utf8(macro), requires);
151                         continue;
152                 }
153
154                 idocstringstream is(from_utf8(line));
155                 latexkeys tmp;
156                 is >> tmp.name >> tmp.inset;
157                 if (isFontName(tmp.inset))
158                         is >> charid >> fallbackid >> tmp.extra >> tmp.xmlname;
159                 else
160                         is >> tmp.extra;
161                 // requires is optional
162                 if (is)
163                         is >> tmp.requires;
164                 else {
165                         LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
166                                 << to_utf8(tmp.name) << ' ' << to_utf8(tmp.inset) << ' '
167                                 << to_utf8(tmp.extra));
168                         continue;
169                 }
170
171                 if (isFontName(tmp.inset)) {
172                         // tmp.inset _is_ the fontname here.
173                         // create fallbacks if necessary
174
175                         // store requirements as long as we can
176                         if (tmp.requires.empty()) {
177                                 if (tmp.inset == "msa" || tmp.inset == "msb")
178                                         tmp.requires = from_ascii("amssymb");
179                                 else if (tmp.inset == "wasy")
180                                         tmp.requires = from_ascii("wasysym");
181                         }
182
183                         // symbol font is not available sometimes
184                         docstring symbol_font = from_ascii("lyxsymbol");
185
186                         if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
187                                 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
188                                 tmp.draw = tmp.name;
189                         } else if (isMathFontAvailable(tmp.inset)) {
190                                 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
191                                 tmp.draw.push_back(char_type(charid));
192                         } else if (fallbackid && isMathFontAvailable(symbol_font)) {
193                                 if (tmp.inset == "cmex")
194                                         tmp.inset = from_ascii("lyxsymbol");
195                                 else
196                                         tmp.inset = from_ascii("lyxboldsymbol");
197                                 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
198                                 tmp.draw.push_back(char_type(fallbackid));
199                         } else {
200                                 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
201                                 tmp.draw = tmp.name;
202                                 tmp.inset = from_ascii("lyxtex");
203                         }
204                 } else {
205                         // it's a proper inset
206                         LYXERR(Debug::MATHED, "inset " << to_utf8(tmp.inset)
207                                               << " used for " << to_utf8(tmp.name));
208                 }
209
210                 if (theWordList.find(tmp.name) != theWordList.end())
211                         LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
212                                 << " already exists.");
213                 else
214                         theWordList[tmp.name] = tmp;
215
216                 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
217                         << "  inset: " << to_utf8(tmp.inset)
218                         << "  draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
219                         << "  extra: " << to_utf8(tmp.extra)
220                         << "  requires: " << to_utf8(tmp.requires) << '\'');
221         }
222         docstring tmp = from_ascii("cmm");
223         docstring tmp2 = from_ascii("cmsy");
224         has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
225 }
226
227
228 bool isSpecialChar(docstring const & name)
229 {
230         if (name.size() != 1)
231                 return  name == "textasciicircum" || name == "mathcircumflex" ||
232                         name == "textasciitilde"  || name == "textbackslash";
233
234         char_type const c = name.at(0);
235         return  c == '{' || c == '}' || c == '&' || c == '$' ||
236                 c == '#' || c == '%' || c == '_' || c == ' ';
237 }
238
239
240 } // namespace anon
241
242 MathWordList const & mathedWordList()
243 {
244         return theWordList;
245 }
246
247
248 void initMath()
249 {
250         static bool initialized = false;
251         if (!initialized) {
252                 initialized = true;
253                 initParser();
254                 initSymbols();
255         }
256 }
257
258
259 bool ensureMath(WriteStream & os, bool needs_math_mode, bool macro)
260 {
261         bool brace = os.pendingBrace();
262         os.pendingBrace(false);
263         if (!os.latex())
264                 return brace;
265         if (os.textMode() && needs_math_mode) {
266                 os << "\\ensuremath{";
267                 os.textMode(false);
268                 brace = true;
269         } else if (macro && brace && !needs_math_mode) {
270                 // This is a user defined macro, but not a MathMacro, so we
271                 // cannot be sure what mode is needed. As it was entered in
272                 // a text box, we restore the text mode.
273                 os << '}';
274                 os.textMode(true);
275                 brace = false;
276         }
277         return brace;
278 }
279
280
281 int ensureMode(WriteStream & os, InsetMath::mode_type mode,
282                 bool locked, bool ascii)
283 {
284         bool textmode = mode == InsetMath::TEXT_MODE;
285         if (os.latex() && textmode && os.pendingBrace()) {
286                 os.os() << '}';
287                 os.pendingBrace(false);
288                 os.pendingSpace(false);
289                 os.textMode(true);
290         }
291         int oldmodes = os.textMode() ? 0x01 : 0;
292         os.textMode(textmode);
293         oldmodes |= os.lockedMode() ? 0x02 : 0;
294         os.lockedMode(locked);
295         oldmodes |= os.asciiOnly() ? 0x04 : 0;
296         os.asciiOnly(ascii);
297         return oldmodes;
298 }
299
300
301 latexkeys const * in_word_set(docstring const & str)
302 {
303         MathWordList::iterator it = theWordList.find(str);
304         return it != theWordList.end() ? &(it->second) : 0;
305 }
306
307
308 MathAtom createInsetMath(char const * const s, Buffer * buf)
309 {
310         return createInsetMath(from_utf8(s), buf);
311 }
312
313
314 MathAtom createInsetMath(docstring const & s, Buffer * buf)
315 {
316         //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
317         if ((s == "ce" || s == "cf") && buf
318             && buf->params().use_mhchem == BufferParams::package_off)
319                 return MathAtom(new MathMacro(buf, s));
320
321         latexkeys const * l = in_word_set(s);
322         if (l) {
323                 docstring const & inset = l->inset;
324                 //lyxerr << " found inset: '" << inset << '\'' << endl;
325                 if (inset == "ref")
326                         return MathAtom(new InsetMathRef(buf, l->name));
327                 if (inset == "overset")
328                         return MathAtom(new InsetMathOverset(buf));
329                 if (inset == "underset")
330                         return MathAtom(new InsetMathUnderset(buf));
331                 if (inset == "decoration")
332                         return MathAtom(new InsetMathDecoration(buf, l));
333                 if (inset == "space")
334                         return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
335                 if (inset == "dots")
336                         return MathAtom(new InsetMathDots(l));
337                 if (inset == "mbox")
338                         // return MathAtom(new InsetMathMBox);
339                         // InsetMathMBox is proposed to replace InsetMathBox,
340                         // but is not ready yet (it needs a BufferView for
341                         // construction)
342                         return MathAtom(new InsetMathBox(buf, l->name));
343 //              if (inset == "fbox")
344 //                      return MathAtom(new InsetMathFBox(l));
345                 if (inset == "style")
346                         return MathAtom(new InsetMathSize(buf, l));
347                 if (inset == "font")
348                         return MathAtom(new InsetMathFont(buf, l));
349                 if (inset == "oldfont")
350                         return MathAtom(new InsetMathFontOld(buf, l));
351                 if (inset == "matrix")
352                         return MathAtom(new InsetMathAMSArray(buf, s));
353                 if (inset == "split")
354                         return MathAtom(new InsetMathSplit(buf, s));
355                 if (inset == "big")
356                         // we can't create a InsetMathBig, since the argument
357                         // is missing.
358                         return MathAtom(new InsetMathUnknown(s));
359                 return MathAtom(new InsetMathSymbol(l));
360         }
361
362         if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
363                 return MathAtom(new MathMacroArgument(s[1] - '0'));
364         if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
365                         && s[2] >= '1' && s[2] <= '9')
366                 return MathAtom(new MathMacroArgument(s[2] - '0'));
367         if (s == "boxed")
368                 return MathAtom(new InsetMathBoxed(buf));
369         if (s == "fbox")
370                 return MathAtom(new InsetMathFBox(buf));
371         if (s == "framebox")
372                 return MathAtom(new InsetMathMakebox(buf, true));
373         if (s == "makebox")
374                 return MathAtom(new InsetMathMakebox(buf, false));
375         if (s == "kern")
376                 return MathAtom(new InsetMathKern);
377         if (s.substr(0, 8) == "xymatrix") {
378                 char spacing_code = '\0';
379                 Length spacing;
380                 bool equal_spacing = false;
381                 size_t const len = s.length();
382                 size_t i = 8;
383                 if (i < len && s[i] == '@') {
384                         ++i;
385                         if (i < len && s[i] == '!') {
386                                 equal_spacing = true;
387                                 ++i;
388                                 if (i < len) {
389                                         switch (s[i]) {
390                                         case '0':
391                                         case 'R':
392                                         case 'C':
393                                                 spacing_code = static_cast<char>(s[i]);
394                                         }
395                                 }
396                         } else if (i < len) {
397                                 switch (s[i]) {
398                                 case 'R':
399                                 case 'C':
400                                 case 'M':
401                                 case 'W':
402                                 case 'H':
403                                 case 'L':
404                                         spacing_code = static_cast<char>(s[i]);
405                                         ++i;
406                                         break;
407                                 }
408                                 if (i < len && s[i] == '=') {
409                                         ++i;
410                                         spacing = Length(to_ascii(s.substr(i)));
411                                 }
412                         }
413                 }
414                 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
415                         equal_spacing));
416         }
417         if (s == "xrightarrow" || s == "xleftarrow")
418                 return MathAtom(new InsetMathXArrow(buf, s));
419         if (s == "split" || s == "alignedat")
420                 return MathAtom(new InsetMathSplit(buf, s));
421         if (s == "cases")
422                 return MathAtom(new InsetMathCases(buf));
423         if (s == "substack")
424                 return MathAtom(new InsetMathSubstack(buf));
425         if (s == "subarray" || s == "array")
426                 return MathAtom(new InsetMathArray(buf, s, 1, 1));
427         if (s == "sqrt")
428                 return MathAtom(new InsetMathSqrt(buf));
429         if (s == "root")
430                 return MathAtom(new InsetMathRoot(buf));
431         if (s == "tabular")
432                 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
433         if (s == "stackrel")
434                 return MathAtom(new InsetMathStackrel(buf));
435         if (s == "binom")
436                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
437         if (s == "dbinom")
438                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
439         if (s == "tbinom")
440                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
441         if (s == "choose")
442                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
443         if (s == "brace")
444                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
445         if (s == "brack")
446                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
447         if (s == "frac")
448                 return MathAtom(new InsetMathFrac(buf));
449         if (s == "cfrac")
450                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
451         if (s == "dfrac")
452                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
453         if (s == "tfrac")
454                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
455         if (s == "over")
456                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
457         if (s == "nicefrac")
458                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
459         if (s == "unitfrac")
460                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
461         // These string values are only for math toolbar use, no LaTeX names
462         if (s == "unitfracthree")
463                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
464         if (s == "unitone")
465                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
466         if (s == "unittwo")
467                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
468         if (s == "cfracleft")
469                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
470         if (s == "cfracright")
471                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
472         //if (s == "infer")
473         //      return MathAtom(new MathInferInset);
474         if (s == "atop")
475                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
476         if (s == "lefteqn")
477                 return MathAtom(new InsetMathLefteqn(buf));
478         if (s == "boldsymbol")
479                 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
480         if (s == "bm")
481                 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
482         if (s == "heavysymbol" || s == "hm")
483                 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
484         if (s == "color" || s == "normalcolor")
485                 return MathAtom(new InsetMathColor(buf, true));
486         if (s == "textcolor")
487                 return MathAtom(new InsetMathColor(buf, false));
488         if (s == "hphantom")
489                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
490         if (s == "phantom")
491                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
492         if (s == "vphantom")
493                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
494         if (s == "ensuremath")
495                 return MathAtom(new InsetMathEnsureMath(buf));
496         if (isSpecialChar(s))
497                 return MathAtom(new InsetMathSpecialChar(s));
498
499         if (s == "regexp")
500                 return MathAtom(new InsetMathHull(buf, hullRegexp));
501
502         return MathAtom(new MathMacro(buf, s));
503 }
504
505
506 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
507 {
508         // An example str:
509         // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
510         docstring name;
511         docstring body = split(str, name, ' ');
512
513         if (name == "ref") {
514                 InsetCommandParams icp(REF_CODE);
515                 // FIXME UNICODE
516                 InsetCommand::string2params("ref", to_utf8(str), icp);
517                 mathed_parse_cell(ar, icp.getCommand());
518         } else if (name == "mathspace") {
519                 InsetSpaceParams isp(true);
520                 InsetSpace::string2params(to_utf8(str), isp);
521                 InsetSpace is(isp);
522                 odocstringstream os;
523                 Encoding const * const ascii = encodings.fromLyXName("ascii");
524                 OutputParams op(ascii);
525                 is.latex(os, op);
526                 mathed_parse_cell(ar, os.str());
527                 if (ar.size() == 2) {
528                         // remove "{}"
529                         if (ar[1].nucleus()->asBraceInset())
530                                 ar.pop_back();
531                 }
532         } else
533                 return false;
534
535         if (ar.size() != 1)
536                 return false;
537
538         return ar[0].nucleus();
539 }
540
541
542 bool isAsciiOrMathAlpha(char_type c)
543 {
544         return c < 0x80 || Encodings::isMathAlpha(c);
545 }
546
547
548 } // namespace lyx