2 * \file MathFactory.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
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"
66 #include "LyX.h" // use_gui
67 #include "OutputParams.h"
70 using namespace lyx::support
;
79 MathWordList theWordList
;
82 bool isMathFontAvailable(docstring
& name
)
90 // Do we have the font proper?
91 if (theFontLoader().available(f
))
95 if (name
== "eufrak") {
96 name
= from_ascii("lyxfakefrak");
100 LYXERR(Debug::MATHED
,
101 "font " << to_utf8(name
) << " not available and I can't fake it");
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
;
115 ifstream
fs(filename
.toFilesystemEncoding().c_str());
118 while (getline(fs
, line
)) {
121 if (line
.empty() || line
[0] == '#')
124 // special case of iffont/else/endif
125 if (line
.size() >= 7 && line
.substr(0, 6) == "iffont") {
126 istringstream
is(line
);
130 docstring t
= from_utf8(tmp
);
131 skip
= !isMathFontAvailable(t
);
133 } else if (line
.size() >= 4 && line
.substr(0, 4) == "else") {
135 } else if (line
.size() >= 5 && line
.substr(0, 5) == "endif") {
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
);
147 is
>> macro
>> requires
;
148 MacroTable::globalMacros().insert(0, from_utf8(macro
), requires
);
152 idocstringstream
is(from_utf8(line
));
154 is
>> tmp
.name
>> tmp
.inset
;
155 if (isFontName(tmp
.inset
))
156 is
>> charid
>> fallbackid
>> tmp
.extra
>> tmp
.xmlname
;
159 // requires is optional
163 LYXERR(Debug::MATHED
, "skipping line '" << line
<< "'\n"
164 << to_utf8(tmp
.name
) << ' ' << to_utf8(tmp
.inset
) << ' '
165 << to_utf8(tmp
.extra
));
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
));
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");
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
));
198 LYXERR(Debug::MATHED
, "faking " << to_utf8(tmp
.name
));
200 tmp
.inset
= from_ascii("lyxtex");
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.");
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
== ' ';
240 MathWordList
const & mathedWordList()
248 static bool initialized
= false;
257 bool ensureMath(WriteStream
& os
, bool needs_math_mode
, bool macro
)
259 bool brace
= os
.pendingBrace();
260 os
.pendingBrace(false);
263 if (os
.textMode() && needs_math_mode
) {
264 os
<< "\\ensuremath{";
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.
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()) {
284 os
.pendingBrace(false);
285 os
.pendingSpace(false);
288 int oldmodes
= os
.textMode() ? 1 : 0;
289 os
.textMode(textmode
);
290 oldmodes
|= os
.lockedMode() ? 2 : 0;
291 os
.lockedMode(locked
);
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
);
314 docstring
const & inset
= l
->inset
;
315 //lyxerr << " found inset: '" << inset << '\'' << endl;
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
), ""));
327 return MathAtom(new InsetMathDots(l
));
329 // return MathAtom(new InsetMathMBox);
330 // InsetMathMBox is proposed to replace InsetMathBox,
331 // but is not ready yet (it needs a BufferView for
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
));
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
));
347 // we can't create a InsetMathBig, since the argument
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'));
359 return MathAtom(new InsetMathBoxed(buf
));
361 return MathAtom(new InsetMathFBox(buf
));
363 return MathAtom(new InsetMathMakebox(buf
, true));
365 return MathAtom(new InsetMathMakebox(buf
, false));
367 return MathAtom(new InsetMathKern
);
368 if (s
.substr(0, 8) == "xymatrix") {
369 char spacing_code
= '\0';
371 size_t const len
= s
.length();
373 if (i
< len
&& s
[i
] == '@') {
383 spacing_code
= static_cast<char>(s
[i
]);
388 if (i
< len
&& s
[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
));
400 return MathAtom(new InsetMathCases(buf
));
402 return MathAtom(new InsetMathSubstack(buf
));
403 if (s
== "subarray" || s
== "array")
404 return MathAtom(new InsetMathArray(buf
, s
, 1, 1));
406 return MathAtom(new InsetMathSqrt(buf
));
408 return MathAtom(new InsetMathRoot(buf
));
410 return MathAtom(new InsetMathTabular(buf
, s
, 1, 1));
412 return MathAtom(new InsetMathStackrel(buf
));
414 return MathAtom(new InsetMathBinom(buf
, InsetMathBinom::BINOM
));
416 return MathAtom(new InsetMathBinom(buf
, InsetMathBinom::DBINOM
));
418 return MathAtom(new InsetMathBinom(buf
, InsetMathBinom::TBINOM
));
420 return MathAtom(new InsetMathBinom(buf
, InsetMathBinom::CHOOSE
));
422 return MathAtom(new InsetMathBinom(buf
, InsetMathBinom::BRACE
));
424 return MathAtom(new InsetMathBinom(buf
, InsetMathBinom::BRACK
));
426 return MathAtom(new InsetMathFrac(buf
));
428 return MathAtom(new InsetMathFrac(buf
, InsetMathFrac::CFRAC
));
430 return MathAtom(new InsetMathFrac(buf
, InsetMathFrac::DFRAC
));
432 return MathAtom(new InsetMathFrac(buf
, InsetMathFrac::TFRAC
));
434 return MathAtom(new InsetMathFrac(buf
, InsetMathFrac::OVER
));
436 return MathAtom(new InsetMathFrac(buf
, InsetMathFrac::NICEFRAC
));
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));
443 return MathAtom(new InsetMathFrac(buf
, InsetMathFrac::UNIT
, 1));
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
));
451 // return MathAtom(new MathInferInset);
453 return MathAtom(new InsetMathFrac(buf
, InsetMathFrac::ATOP
));
455 return MathAtom(new InsetMathLefteqn(buf
));
456 if (s
== "boldsymbol")
457 return MathAtom(new InsetMathBoldSymbol(buf
, InsetMathBoldSymbol::AMS_BOLD
));
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));
467 return MathAtom(new InsetMathPhantom(buf
, InsetMathPhantom::hphantom
));
469 return MathAtom(new InsetMathPhantom(buf
, InsetMathPhantom::phantom
));
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
));
478 return MathAtom(new InsetMathHull(buf
, hullRegexp
));
480 return MathAtom(new MathMacro(buf
, s
));
484 bool createInsetMath_fromDialogStr(docstring
const & str
, MathData
& ar
)
487 // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
489 docstring body
= split(str
, name
, ' ');
492 InsetCommandParams
icp(REF_CODE
);
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
);
501 Encoding
const * const ascii
= encodings
.fromLyXName("ascii");
502 OutputParams
op(ascii
);
504 mathed_parse_cell(ar
, os
.str());
505 if (ar
.size() == 2) {
507 if (ar
[1].nucleus()->asBraceInset())
516 return ar
[0].nucleus();
520 bool isAsciiOrMathAlpha(char_type c
)
522 return c
< 0x80 || Encodings::isMathAlpha(c
);