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 if (c < 0x80 || Encodings::latexMathChar(c, mathmode, os.encoding(), command)) {
124 if (in_forced_mode) {
125 // we were inside \lyxmathsym
128 in_forced_mode = false;
130 if (c >= 0x80 && os.textMode()) {
131 os << "\\ensuremath{";
133 in_forced_mode = true;
135 } else if (c < 0x80 && in_forced_mode) {
136 // we were inside \ensuremath
139 in_forced_mode = false;
141 } else if (!os.textMode()) {
142 if (in_forced_mode) {
143 // we were inside \ensuremath
145 in_forced_mode = false;
147 os << "\\lyxmathsym{";
148 in_forced_mode = true;
153 // We may need a space if the command contains a macro
154 // and the last char is ASCII.
155 if (lyx::support::contains(command, '\\')
156 && isAlphaASCII(command[command.size() - 1]))
157 os.pendingSpace(true);
158 } catch (EncodingException & e) {
159 switch (os.output()) {
160 case WriteStream::wsDryrun: {
161 os << "<" << _("LyX Warning: ")
162 << _("uncodable character") << " '";
163 os << docstring(1, e.failed_char);
167 case WriteStream::wsPreview: {
168 // indicate the encoding error by a boxed '?'
169 os << "{\\fboxsep=1pt\\fbox{?}}";;
170 LYXERR0("Uncodable character" << " '"
171 << docstring(1, e.failed_char)
175 case WriteStream::wsDefault:
184 if (in_forced_mode && os.textMode()) {
185 // We have to care for closing \lyxmathsym
189 os.pendingBrace(in_forced_mode);