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/lassert.h"
23 #include "support/lstrings.h"
24 #include "support/textutils.h"
26 using lyx::support::escape;
31 InsetMathString::InsetMathString(docstring const & s)
36 Inset * InsetMathString::clone() const
38 return new InsetMathString(*this);
42 void InsetMathString::metrics(MetricsInfo & mi, Dimension & dim) const
44 mathed_string_dim(mi.base.font, str_, dim);
48 void InsetMathString::draw(PainterInfo & pi, int x, int y) const
54 void InsetMathString::normalize(NormalStream & os) const
56 os << "[string " << str_ << ' ' << "mathalpha" << ']';
60 void InsetMathString::maple(MapleStream & os) const
62 if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
63 os << ' ' << str_ << ' ';
67 // insert '*' between adjacent chars if type is LM_TC_VAR
69 for (size_t i = 1; i < str_.size(); ++i)
74 void InsetMathString::mathematica(MathematicaStream & os) const
76 os << ' ' << str_ << ' ';
80 void InsetMathString::octave(OctaveStream & os) const
82 if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
83 os << ' ' << str_ << ' ';
87 // insert '*' between adjacent chars if type is LM_TC_VAR
89 for (size_t i = 1; i < str_.size(); ++i)
94 void InsetMathString::mathmlize(MathStream & /*os*/) const
96 // useless, no doubt, but we should not be here
97 LASSERT(false, /* */);
101 void InsetMathString::write(WriteStream & os) const
103 if (!os.latex() || os.lockedMode()) {
104 os << (os.asciiOnly() ? escape(str_) : str_);
108 docstring::const_iterator cit = str_.begin();
109 docstring::const_iterator end = str_.end();
111 // We may already be inside an \ensuremath command.
112 bool in_forced_mode = os.pendingBrace();
114 // We will take care of matching braces.
115 os.pendingBrace(false);
118 bool mathmode = in_forced_mode ? os.textMode() : !os.textMode();
119 char_type const c = *cit;
120 docstring command(1, c);
122 bool termination = false;
124 Encodings::latexMathChar(c, mathmode, os.encoding(), command, termination)) {
126 if (in_forced_mode) {
127 // we were inside \lyxmathsym
130 in_forced_mode = false;
132 if (!isASCII(c) && os.textMode()) {
133 os << "\\ensuremath{";
135 in_forced_mode = true;
137 } else if (isASCII(c) && in_forced_mode) {
138 // we were inside \ensuremath
141 in_forced_mode = false;
143 } else if (!os.textMode()) {
144 if (in_forced_mode) {
145 // we were inside \ensuremath
147 in_forced_mode = false;
149 os << "\\lyxmathsym{";
150 in_forced_mode = true;
155 // We may need a space if the command contains a macro
156 // and the last char is ASCII.
158 os.pendingSpace(true);
159 } catch (EncodingException const & e) {
160 switch (os.output()) {
161 case WriteStream::wsDryrun: {
162 os << "<" << _("LyX Warning: ")
163 << _("uncodable character") << " '";
164 os << docstring(1, e.failed_char);
168 case WriteStream::wsPreview: {
169 // indicate the encoding error by a boxed '?'
170 os << "{\\fboxsep=1pt\\fbox{?}}";
171 LYXERR0("Uncodable character" << " '"
172 << docstring(1, e.failed_char)
176 case WriteStream::wsDefault:
185 if (in_forced_mode && os.textMode()) {
186 // We have to care for closing \lyxmathsym
190 os.pendingBrace(in_forced_mode);