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