]> git.lyx.org Git - features.git/blob - src/mathed/MathFactory.cpp
Refactor OutputParams
[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 "InsetMathClass.h"
23 #include "InsetMathColor.h"
24 #include "InsetMathDecoration.h"
25 #include "InsetMathDots.h"
26 #include "InsetMathEnsureMath.h"
27 #include "InsetMathFont.h"
28 #include "InsetMathFontOld.h"
29 #include "InsetMathFrac.h"
30 #include "InsetMathKern.h"
31 #include "InsetMathLefteqn.h"
32 #include "InsetMathOverset.h"
33 #include "InsetMathPhantom.h"
34 #include "InsetMathRef.h"
35 #include "InsetMathRoot.h"
36 #include "InsetMathSideset.h"
37 #include "InsetMathSize.h"
38 #include "InsetMathSpace.h"
39 #include "InsetMathSpecialChar.h"
40 #include "InsetMathSplit.h"
41 #include "InsetMathSqrt.h"
42 #include "InsetMathStackrel.h"
43 #include "InsetMathSubstack.h"
44 #include "InsetMathSymbol.h"
45 #include "InsetMathTabular.h"
46 #include "InsetMathUnderset.h"
47 #include "InsetMathUnknown.h"
48 #include "InsetMathHull.h"
49 #include "InsetMathXArrow.h"
50 #include "InsetMathXYMatrix.h"
51 #include "InsetMathDiagram.h"
52 #include "MacroTable.h"
53 #include "InsetMathMacro.h"
54 #include "InsetMathMacroArgument.h"
55 #include "MathParser.h"
56 #include "MathStream.h"
57 #include "MathSupport.h"
58
59 #include "insets/InsetCommand.h"
60 #include "insets/InsetSpace.h"
61
62 #include "support/debug.h"
63 #include "support/docstream.h"
64 #include "support/FileName.h"
65 #include "support/filetools.h" // LibFileSearch
66 #include "support/lstrings.h"
67 #include "support/textutils.h"
68
69 #include "frontends/FontLoader.h"
70
71 #include "Buffer.h"
72 #include "BufferParams.h"
73 #include "Encoding.h"
74 #include "LyX.h" // use_gui
75
76 #include <iomanip>
77
78 using namespace std;
79 using namespace lyx::support;
80
81 namespace lyx {
82
83 bool has_math_fonts;
84
85
86 namespace {
87
88 MathWordList theMathWordList;
89
90
91 bool isMathFontAvailable(string & name)
92 {
93         if (!use_gui)
94                 return false;
95
96         FontInfo f;
97         augmentFont(f, name);
98
99         // Do we have the font proper?
100         if (theFontLoader().available(f))
101                 return true;
102
103         // can we fake it?
104         if (name == "eufrak") {
105                 name = "lyxfakefrak";
106                 return true;
107         }
108
109         LYXERR(Debug::MATHED,
110                 "font " << name << " not available and I can't fake it");
111         return false;
112 }
113
114
115 bool canBeDisplayed(char_type c)
116 {
117         if (!use_gui)
118                 return true;
119         return theFontLoader().canBeDisplayed(c);
120 }
121
122
123 bool isUnicodeSymbolAvailable(docstring const & name, char_type & c)
124 {
125         docstring cmd(from_ascii("\\") + name);
126         bool is_combining;
127         bool termination;
128         c = Encodings::fromLaTeXCommand(cmd, Encodings::MATH_CMD,
129                                         is_combining, termination);
130         if (c == 0 && name == "varOmega") {
131                 // fallback for bug 7954, unicodesymbols does not list
132                 // \\varOmega because of requirements, but this might change
133                 cmd = from_ascii("\\mathit{\\Omega}");
134                 c = Encodings::fromLaTeXCommand(cmd, Encodings::MATH_CMD,
135                                                 is_combining, termination);
136         }
137         return c != 0 && !is_combining;
138 }
139
140
141 void initSymbols()
142 {
143         FileName const filename = libFileSearch(string(), "symbols");
144         LYXERR(Debug::MATHED, "read symbols from " << filename);
145         if (filename.empty()) {
146                 lyxerr << "Could not find symbols file" << endl;
147                 return;
148         }
149
150         ifstream fs(filename.toFilesystemEncoding().c_str());
151         // limit the size of strings we read to avoid memory problems
152         fs >> setw(65636);
153         string line;
154         bool skip = false;
155         while (getline(fs, line)) {
156                 int charid     = 0;
157                 int dsp_charid = 0;
158                 int fallbackid = 0;
159                 if (line.empty() || line[0] == '#')
160                         continue;
161
162                 // special case of iffont/else/endif
163                 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
164                         istringstream is(line);
165                         // limit the size of strings we read to avoid memory problems
166                         is >> setw(65636);
167                         string tmp;
168                         is >> tmp;
169                         is >> tmp;
170                         skip = !isMathFontAvailable(tmp);
171                         continue;
172                 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
173                         skip = !skip;
174                         continue;
175                 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
176                         skip = false;
177                         continue;
178                 } else if (skip)
179                         continue;
180
181                 // special case of pre-defined macros
182                 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
183                         //lyxerr << "macro definition: '" << line << '\'' << endl;
184                         // syntax: Either
185                         // \def\macroname{definition}
186                         // or
187                         // \def\macroname{definition} requires
188                         // or
189                         // \def\macroname{definition} extra htmlname xmlname requires
190                         istringstream is(line);
191                         string macro;
192                         string required;
193                         string extra;
194                         string htmlname;
195                         string xmlname;
196                         bool hidden = false;
197                         is >> setw(65536) >> macro >> required;
198                         if ((is >> htmlname >> xmlname)) {
199                                 extra = required;
200                                 if (!(is >> required))
201                                         required = "";
202                         } else
203                                 htmlname = xmlname = "";
204                         MacroTable::iterator it = MacroTable::globalMacros().insert(
205                                         nullptr, from_utf8(macro));
206                         if (!extra.empty() || !htmlname.empty() || !xmlname.empty() || !required.empty()) {
207                                 MathWordList::iterator wit = theMathWordList.find(it->first);
208                                 if (wit != theMathWordList.end())
209                                         LYXERR(Debug::MATHED, "readSymbols: inset "
210                                                 << to_utf8(it->first) << " already exists.");
211                                 else {
212                                         latexkeys tmp;
213                                         tmp.inset = "macro";
214                                         tmp.name = it->first;
215                                         tmp.extra = from_utf8(extra);
216                                         tmp.htmlname = from_utf8(htmlname);
217                                         tmp.xmlname = from_utf8(xmlname);
218                                         if (required == "hiddensymbol") {
219                                                 required = "";
220                                                 tmp.hidden = hidden = true;
221                                         } else
222                                                 tmp.required = required;
223                                         theMathWordList[it->first] = tmp;
224                                         wit = theMathWordList.find(it->first);
225                                         it->second.setSymbol(&(wit->second));
226                                 }
227                         }
228                         // If you change the following output, please adjust
229                         // development/tools/generate_symbols_images.py.
230                         LYXERR(Debug::MATHED, "read symbol '" << to_utf8(it->first)
231                             << "  inset: macro"
232                             << "  draw: 0"
233                             << "  extra: " << extra
234                             << "  html: " << htmlname
235                             << "  xml: " << xmlname
236                                 << "  requires: " << required
237                                 << "  hidden: " << hidden << '\'');
238                         continue;
239                 }
240
241                 idocstringstream is(from_utf8(line));
242                 latexkeys tmp;
243                 docstring help;
244                 is >> tmp.name >> help;
245                 tmp.inset = to_ascii(help);
246                 if (isFontName(tmp.inset)) {
247                         is >> help >> fallbackid >> tmp.extra >> tmp.htmlname >> tmp.xmlname;
248                         docstring cid, dsp_cid;
249                         idocstringstream is2(subst(help, '|', ' '));
250                         is2 >> charid >> dsp_charid;
251                 } else
252                         is >> tmp.extra;
253                 // requires is optional
254                 if (is) {
255                         if ((is >> help)) {
256                                 // backward compatibility
257                                 if (help == "esintoramsmath")
258                                         tmp.required = "esint|amsmath";
259                                 else
260                                         tmp.required = to_ascii(help);
261                         }
262                 } else {
263                         LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
264                                 << to_utf8(tmp.name) << ' ' << tmp.inset << ' '
265                                 << to_utf8(tmp.extra));
266                         continue;
267                 }
268
269                 if (isFontName(tmp.inset)) {
270                         // tmp.inset _is_ the fontname here.
271                         // create fallbacks if necessary
272
273                         // store requirements as long as we can
274                         if (tmp.required.empty()) {
275                                 if (tmp.inset == "msa" || tmp.inset == "msb")
276                                         tmp.required = "amssymb";
277                                 else if (tmp.inset == "wasy")
278                                         tmp.required = "wasysym";
279                                 else if (tmp.inset == "mathscr")
280                                         tmp.required = "mathrsfs";
281                                 else if (tmp.inset == "mathds")
282                                         tmp.required = "dsfont";
283                         }
284
285                         // symbol font is not available sometimes
286                         string symbol_font = "lyxsymbol";
287                         char_type unicodesymbol = 0;
288
289                         if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
290                                 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
291                                 tmp.draw = tmp.name;
292                         } else if (isMathFontAvailable(tmp.inset) && canBeDisplayed(charid)) {
293                                 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
294                                 tmp.draw.push_back(char_type(charid));
295                                 if (dsp_charid && canBeDisplayed(dsp_charid)) {
296                                         LYXERR(Debug::MATHED, "large symbol available for " << to_utf8(tmp.name));
297                                         tmp.dsp_draw.push_back(char_type(dsp_charid));
298                                 }
299                         } else if (fallbackid && isMathFontAvailable(symbol_font) &&
300                                    canBeDisplayed(fallbackid)) {
301                                 if (tmp.inset == "cmex")
302                                         tmp.inset = "lyxsymbol";
303                                 else
304                                         tmp.inset = "lyxboldsymbol";
305                                 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
306                                 tmp.draw.push_back(char_type(fallbackid));
307                         } else if (isUnicodeSymbolAvailable(tmp.name, unicodesymbol)) {
308                                 LYXERR(Debug::MATHED, "unicode fallback for " << to_utf8(tmp.name));
309                                 tmp.inset = "mathnormal";
310                                 tmp.draw.push_back(unicodesymbol);
311                         } else {
312                                 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
313                                 tmp.draw = tmp.name;
314                                 tmp.inset = "lyxtex";
315                         }
316                 } else {
317                         // it's a proper inset
318                         LYXERR(Debug::MATHED, "inset " << tmp.inset
319                                               << " used for " << to_utf8(tmp.name));
320                 }
321
322                 if (tmp.required == "hiddensymbol")
323                 {
324                         tmp.required = "";
325                         tmp.hidden = true;
326                 }
327
328                 if (theMathWordList.find(tmp.name) != theMathWordList.end())
329                         LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
330                                 << " already exists.");
331                 else
332                         theMathWordList[tmp.name] = tmp;
333
334                 // If you change the following output, please adjust
335                 // development/tools/generate_symbols_images.py.
336                 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
337                         << "  inset: " << tmp.inset
338                         << "  draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
339                         << "  extra: " << to_utf8(tmp.extra)
340                         << "  html: " << to_utf8(tmp.htmlname)
341                         << "  xml: " << to_utf8(tmp.xmlname)
342                         << "  requires: " << tmp.required
343                         << "  hidden: " << tmp.hidden << '\'');
344         }
345         string tmp = "cmm";
346         string tmp2 = "cmsy";
347         has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
348 }
349
350
351 bool isSpecialChar(docstring const & name)
352 {
353         if (name.size() != 1)
354                 return  name == "textasciicircum" || name == "mathcircumflex" ||
355                         name == "textasciitilde"  || name == "textbackslash";
356
357         char_type const c = name.at(0);
358         return  c == '{' || c == '}' || c == '&' || c == '$' ||
359                 c == '#' || c == '%' || c == '_';
360 }
361
362
363 } // namespace
364
365 MathWordList const & mathedWordList()
366 {
367         return theMathWordList;
368 }
369
370
371 void initMath()
372 {
373         static bool initialized = false;
374         if (!initialized) {
375                 initialized = true;
376                 initParser();
377                 initSymbols();
378         }
379 }
380
381
382 bool ensureMath(WriteStream & os, bool needs_mathmode, bool macro,
383                 bool textmode_macro)
384 {
385         bool brace = os.pendingBrace();
386         os.pendingBrace(false);
387         if (!os.latex())
388                 return brace;
389         if (os.textMode() && needs_mathmode) {
390                 if (brace) {
391                         // close \lyxmathsym
392                         os << '}';
393                         brace = false;
394                 } else {
395                         os << "\\ensuremath{";
396                         brace = true;
397                 }
398                 os.textMode(false);
399         } else if (macro && textmode_macro && !os.textMode()) {
400                 if (brace) {
401                         // close \ensuremath
402                         os << '}';
403                         brace = false;
404                 } else {
405                         os << "\\lyxmathsym{";
406                         brace = true;
407                 }
408                 os.textMode(true);
409         } else if (macro && brace && !needs_mathmode && !textmode_macro) {
410                 // This is a user defined macro, not a InsetMathMacro, so we
411                 // cannot be sure what mode is needed. We leave it in the
412                 // same environment it was entered by closing either \lyxmathsym
413                 // or \ensuremath, whichever was opened.
414                 os << '}';
415                 brace = false;
416                 os.textMode(!os.textMode());
417         }
418         return brace;
419 }
420
421
422 int ensureMode(WriteStream & os, InsetMath::mode_type mode,
423                 bool locked, bool ascii)
424 {
425         bool textmode = mode == InsetMath::TEXT_MODE;
426         if (os.latex() && textmode && os.pendingBrace()) {
427                 os.os() << '}';
428                 os.pendingBrace(false);
429                 os.pendingSpace(false);
430                 os.textMode(true);
431         }
432         int oldmodes = os.textMode() ? 0x01 : 0;
433         os.textMode(textmode);
434         oldmodes |= os.lockedMode() ? 0x02 : 0;
435         os.lockedMode(locked);
436         oldmodes |= os.asciiOnly() ? 0x04 : 0;
437         os.asciiOnly(ascii);
438         return oldmodes;
439 }
440
441
442 latexkeys const * in_word_set(docstring const & str)
443 {
444         MathWordList::iterator it = theMathWordList.find(str);
445         if (it == theMathWordList.end())
446                 return nullptr;
447         if (it->second.inset == "macro")
448                 return nullptr;
449         return &(it->second);
450 }
451
452
453 MathAtom createInsetMath(char const * const s, Buffer * buf)
454 {
455         return createInsetMath(from_utf8(s), buf);
456 }
457
458
459 MathAtom createInsetMath(docstring const & s, Buffer * buf)
460 {
461         //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
462         if ((s == "ce" || s == "cf") && buf
463             && buf->params().use_package("mhchem") == BufferParams::package_off)
464                 return MathAtom(new InsetMathMacro(buf, s));
465
466         latexkeys const * l = in_word_set(s);
467         if (l) {
468                 string const & inset = l->inset;
469                 //lyxerr << " found inset: '" << inset << '\'' << endl;
470                 if (inset == "ref")
471                         return MathAtom(new InsetMathRef(buf, l->name));
472                 if (inset == "overset")
473                         return MathAtom(new InsetMathOverset(buf));
474                 if (inset == "underset")
475                         return MathAtom(new InsetMathUnderset(buf));
476                 if (inset == "decoration")
477                         return MathAtom(new InsetMathDecoration(buf, l));
478                 if (inset == "space")
479                         return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
480                 if (inset == "class")
481                         return MathAtom(new InsetMathClass(buf, string_to_class(s)));
482                 if (inset == "dots")
483                         return MathAtom(new InsetMathDots(l));
484                 if (inset == "mbox")
485                         return MathAtom(new InsetMathBox(buf, l->name));
486 //              if (inset == "fbox")
487 //                      return MathAtom(new InsetMathFBox(l));
488                 if (inset == "style")
489                         return MathAtom(new InsetMathSize(buf, l));
490                 if (inset == "font")
491                         return MathAtom(new InsetMathFont(buf, l));
492                 if (inset == "oldfont")
493                         return MathAtom(new InsetMathFontOld(buf, l));
494                 if (inset == "matrix")
495                         return MathAtom(new InsetMathAMSArray(buf, s));
496                 if (inset == "split")
497                         return MathAtom(new InsetMathSplit(buf, s));
498                 if (inset == "big")
499                         // we can't create a InsetMathBig, since the argument
500                         // is missing.
501                         return MathAtom(new InsetMathUnknown(s));
502                 return MathAtom(new InsetMathSymbol(l));
503         }
504
505         if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
506                 return MathAtom(new InsetMathMacroArgument(s[1] - '0'));
507         if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
508                         && s[2] >= '1' && s[2] <= '9')
509                 return MathAtom(new InsetMathMacroArgument(s[2] - '0'));
510         if (s == "boxed")
511                 return MathAtom(new InsetMathBoxed(buf));
512         if (s == "fbox")
513                 return MathAtom(new InsetMathFBox(buf));
514         if (s == "framebox")
515                 return MathAtom(new InsetMathMakebox(buf, true));
516         if (s == "makebox")
517                 return MathAtom(new InsetMathMakebox(buf, false));
518         if (s.substr(0, 8) == "xymatrix") {
519                 char spacing_code = '\0';
520                 Length spacing;
521                 bool equal_spacing = false;
522                 size_t const len = s.length();
523                 size_t i = 8;
524                 if (i < len && s[i] == '@') {
525                         ++i;
526                         if (i < len && s[i] == '!') {
527                                 equal_spacing = true;
528                                 ++i;
529                                 if (i < len) {
530                                         switch (s[i]) {
531                                         case '0':
532                                         case 'R':
533                                         case 'C':
534                                                 spacing_code = static_cast<char>(s[i]);
535                                         }
536                                 }
537                         } else if (i < len) {
538                                 switch (s[i]) {
539                                 case 'R':
540                                 case 'C':
541                                 case 'M':
542                                 case 'W':
543                                 case 'H':
544                                 case 'L':
545                                         spacing_code = static_cast<char>(s[i]);
546                                         ++i;
547                                         break;
548                                 }
549                                 if (i < len && s[i] == '=') {
550                                         ++i;
551                                         spacing = Length(to_ascii(s.substr(i)));
552                                 }
553                         }
554                 }
555                 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
556                         equal_spacing));
557         }
558
559         if (s == "Diagram")
560                 return MathAtom(new InsetMathDiagram(buf));
561         if (s == "xrightarrow" || s == "xleftarrow" ||
562                 s == "xhookrightarrow" || s == "xhookleftarrow" ||
563                 s == "xRightarrow" || s == "xLeftarrow" ||
564                 s == "xleftrightarrow" || s == "xLeftrightarrow" ||
565                 s == "xrightharpoondown" || s == "xrightharpoonup" ||
566                 s == "xleftharpoondown" || s == "xleftharpoonup" ||
567                 s == "xleftrightharpoons" || s == "xrightleftharpoons" ||
568                 s == "xmapsto")
569                 return MathAtom(new InsetMathXArrow(buf, s));
570         if (s == "split" || s == "alignedat")
571                 return MathAtom(new InsetMathSplit(buf, s));
572         if (s == "cases")
573                 return MathAtom(new InsetMathCases(buf));
574         if (s == "substack")
575                 return MathAtom(new InsetMathSubstack(buf));
576         if (s == "subarray" || s == "array")
577                 return MathAtom(new InsetMathArray(buf, s, 1, 1));
578         if (s == "sqrt")
579                 return MathAtom(new InsetMathSqrt(buf));
580         if (s == "root")
581                 return MathAtom(new InsetMathRoot(buf));
582         if (s == "tabular")
583                 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
584         if (s == "stackrel")
585                 return MathAtom(new InsetMathStackrel(buf, false));
586         // This string value is only for math toolbar use, no LaTeX name
587         if (s == "stackrelthree")
588                 return MathAtom(new InsetMathStackrel(buf, true));
589         if (s == "binom")
590                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
591         if (s == "dbinom")
592                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
593         if (s == "tbinom")
594                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
595         if (s == "choose")
596                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
597         if (s == "brace")
598                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
599         if (s == "brack")
600                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
601         if (s == "frac")
602                 return MathAtom(new InsetMathFrac(buf));
603         if (s == "cfrac")
604                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
605         if (s == "dfrac")
606                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
607         if (s == "tfrac")
608                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
609         if (s == "over")
610                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
611         if (s == "nicefrac")
612                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
613         if (s == "unitfrac")
614                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
615         // These string values are only for math toolbar use, no LaTeX names
616         if (s == "unitfracthree")
617                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
618         if (s == "unitone")
619                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
620         if (s == "unittwo")
621                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
622         if (s == "cfracleft")
623                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
624         if (s == "cfracright")
625                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
626         //if (s == "infer")
627         //      return MathAtom(new MathInferInset);
628         if (s == "atop")
629                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
630         if (s == "lefteqn")
631                 return MathAtom(new InsetMathLefteqn(buf));
632         if (s == "boldsymbol")
633                 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
634         if (s == "bm")
635                 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
636         if (s == "heavysymbol" || s == "hm")
637                 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
638         if (s == "color" || s == "normalcolor")
639                 return MathAtom(new InsetMathColor(buf, true));
640         if (s == "textcolor")
641                 return MathAtom(new InsetMathColor(buf, false));
642         if (s == "hphantom")
643                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
644         if (s == "phantom")
645                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
646         if (s == "vphantom")
647                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
648         if (s == "cancel")
649                 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::cancel));
650         if (s == "bcancel")
651                 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::bcancel));
652         if (s == "xcancel")
653                 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::xcancel));
654         if (s == "cancelto")
655                 return MathAtom(new InsetMathCancelto(buf));
656         if (s == "smash")
657                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smash));
658         // The following 2 string values are only for math toolbar use, no LaTeX names
659         if (s == "smashb")
660                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smashb));
661         if (s == "smasht")
662                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smasht));
663         if (s == "mathclap")
664                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathclap));
665         if (s == "mathllap")
666                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathllap));
667         if (s == "mathrlap")
668                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathrlap));
669         if (s == "ensuremath")
670                 return MathAtom(new InsetMathEnsureMath(buf));
671         if (s == "sideset")
672                 return MathAtom(new InsetMathSideset(buf, true, true));
673         // The following 3 string values are only for math toolbar use, no LaTeX names
674         if (s == "sidesetr")
675                 return MathAtom(new InsetMathSideset(buf, false, true));
676         if (s == "sidesetl")
677                 return MathAtom(new InsetMathSideset(buf, true, false));
678         if (s == "sidesetn")
679                 return MathAtom(new InsetMathSideset(buf, false, false));
680         if (isSpecialChar(s))
681                 return MathAtom(new InsetMathSpecialChar(s));
682         if (s == " ")
683                 return MathAtom(new InsetMathSpace(" ", ""));
684         if (s == "regexp")
685                 return MathAtom(new InsetMathHull(buf, hullRegexp));
686
687         return MathAtom(new InsetMathMacro(buf, s));
688 }
689
690
691 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
692 {
693         // An example str:
694         // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
695         docstring name;
696         docstring body = split(str, name, ' ');
697
698         if (name == "ref") {
699                 InsetCommandParams icp(REF_CODE);
700                 // FIXME UNICODE
701                 InsetCommand::string2params(to_utf8(str), icp);
702                 Encoding const * const utf8 = encodings.fromLyXName("utf8");
703                 OutputParams op(utf8);
704                 mathed_parse_cell(ar, icp.getCommand(op));
705         } else if (name == "mathspace") {
706                 InsetSpaceParams isp(true);
707                 InsetSpace::string2params(to_utf8(str), isp);
708                 InsetSpace is(isp);
709                 odocstringstream ods;
710                 otexstream os(ods);
711                 Encoding const * const ascii = encodings.fromLyXName("ascii");
712                 OutputParams op(ascii);
713                 is.latex(os, op);
714                 mathed_parse_cell(ar, ods.str());
715                 if (ar.size() == 2) {
716                         // remove "{}"
717                         if (ar[1].nucleus()->asBraceInset())
718                                 ar.pop_back();
719                 }
720         } else
721                 return false;
722
723         if (ar.size() != 1)
724                 return false;
725
726         return ar[0].nucleus();
727 }
728
729
730 } // namespace lyx