2 * \file InsetMathString.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 "InsetMathString.h"
14 #include "MathFactory.h"
15 #include "MathStream.h"
16 #include "MathSupport.h"
20 #include "support/debug.h"
21 #include "support/gettext.h"
22 #include "support/lstrings.h"
23 #include "support/textutils.h"
28 InsetMathString::InsetMathString(docstring
const & s
)
33 Inset
* InsetMathString::clone() const
35 return new InsetMathString(*this);
39 void InsetMathString::metrics(MetricsInfo
& mi
, Dimension
& dim
) const
41 mathed_string_dim(mi
.base
.font
, str_
, dim
);
45 void InsetMathString::draw(PainterInfo
& pi
, int x
, int y
) const
51 void InsetMathString::normalize(NormalStream
& os
) const
53 os
<< "[string " << str_
<< ' ' << "mathalpha" << ']';
57 void InsetMathString::maple(MapleStream
& os
) const
59 if (/*code_ != LM_TC_VAR ||*/ str_
.size() <= 1) {
60 os
<< ' ' << str_
<< ' ';
64 // insert '*' between adjacent chars if type is LM_TC_VAR
66 for (size_t i
= 1; i
< str_
.size(); ++i
)
71 void InsetMathString::mathematica(MathematicaStream
& os
) const
73 os
<< ' ' << str_
<< ' ';
77 void InsetMathString::octave(OctaveStream
& os
) const
79 if (/*code_ != LM_TC_VAR ||*/ str_
.size() <= 1) {
80 os
<< ' ' << str_
<< ' ';
84 // insert '*' between adjacent chars if type is LM_TC_VAR
86 for (size_t i
= 1; i
< str_
.size(); ++i
)
91 void InsetMathString::mathmlize(MathStream
& os
) const
94 if (code_ == LM_TC_VAR)
95 os << "<mi> " << str_ << " </mi>";
96 else if (code_ == LM_TC_CONST)
97 os << "<mn> " << str_ << " </mn>";
98 else if (code_ == LM_TC_RM || code_ == LM_TC_TEXTRM)
99 os << "<mtext> " << str_ << " </mtext>";
106 void InsetMathString::write(WriteStream
& os
) const
108 if (!os
.latex() || os
.lockedMode()) {
113 docstring::const_iterator cit
= str_
.begin();
114 docstring::const_iterator end
= str_
.end();
116 // We may already be inside an \ensuremath command.
117 bool in_forced_mode
= os
.pendingBrace();
119 // We will take care of matching braces.
120 os
.pendingBrace(false);
123 bool mathmode
= in_forced_mode
? os
.textMode() : !os
.textMode();
124 char_type
const c
= *cit
;
125 docstring
command(1, c
);
127 if (c
< 0x80 || Encodings::latexMathChar(c
, mathmode
, os
.encoding(), command
)) {
129 if (in_forced_mode
) {
130 // we were inside \lyxmathsym
133 in_forced_mode
= false;
135 if (c
>= 0x80 && os
.textMode()) {
136 os
<< "\\ensuremath{";
138 in_forced_mode
= true;
140 } else if (c
< 0x80 && in_forced_mode
) {
141 // we were inside \ensuremath
144 in_forced_mode
= false;
146 } else if (!os
.textMode()) {
147 if (in_forced_mode
) {
148 // we were inside \ensuremath
150 in_forced_mode
= false;
152 os
<< "\\lyxmathsym{";
153 in_forced_mode
= true;
158 // We may need a space if the command contains a macro
159 // and the last char is ASCII.
160 if (lyx::support::contains(command
, '\\')
161 && isAlphaASCII(command
[command
.size() - 1]))
162 os
.pendingSpace(true);
163 } catch (EncodingException
& e
) {
164 switch (os
.output()) {
165 case WriteStream::wsDryrun
: {
166 os
<< "<" << _("LyX Warning: ")
167 << _("uncodable character") << " '";
168 os
<< docstring(1, e
.failed_char
);
172 case WriteStream::wsPreview
: {
173 // indicate the encoding error by a boxed '?'
174 os
<< "{\\fboxsep=1pt\\fbox{?}}";;
175 LYXERR0("Uncodable character" << " '"
176 << docstring(1, e
.failed_char
)
180 case WriteStream::wsDefault
:
189 if (in_forced_mode
&& os
.textMode()) {
190 // We have to care for closing \lyxmathsym
194 os
.pendingBrace(in_forced_mode
);