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