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