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 "MathStream.h"
15 #include "MathSupport.h"
19 #include "support/gettext.h"
20 #include "support/lstrings.h"
21 #include "support/textutils.h"
26 InsetMathString::InsetMathString(docstring const & s)
31 Inset * InsetMathString::clone() const
33 return new InsetMathString(*this);
37 void InsetMathString::metrics(MetricsInfo & mi, Dimension & dim) const
39 mathed_string_dim(mi.base.font, str_, dim);
43 void InsetMathString::draw(PainterInfo & pi, int x, int y) const
49 void InsetMathString::normalize(NormalStream & os) const
51 os << "[string " << str_ << ' ' << "mathalpha" << ']';
55 void InsetMathString::maple(MapleStream & os) const
57 if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
58 os << ' ' << str_ << ' ';
62 // insert '*' between adjacent chars if type is LM_TC_VAR
64 for (size_t i = 1; i < str_.size(); ++i)
69 void InsetMathString::mathematica(MathematicaStream & os) const
71 os << ' ' << str_ << ' ';
75 void InsetMathString::octave(OctaveStream & os) const
77 if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
78 os << ' ' << str_ << ' ';
82 // insert '*' between adjacent chars if type is LM_TC_VAR
84 for (size_t i = 1; i < str_.size(); ++i)
89 void InsetMathString::mathmlize(MathStream & os) const
92 if (code_ == LM_TC_VAR)
93 os << "<mi> " << str_ << " </mi>";
94 else if (code_ == LM_TC_CONST)
95 os << "<mn> " << str_ << " </mn>";
96 else if (code_ == LM_TC_RM || code_ == LM_TC_TEXTRM)
97 os << "<mtext> " << str_ << " </mtext>";
104 void InsetMathString::write(WriteStream & os) const
111 docstring::const_iterator cit = str_.begin();
112 docstring::const_iterator end = str_.end();
114 // We may already be inside an \ensuremath command.
115 bool in_forced_mode = os.pendingBrace();
117 // We will take care of matching braces.
118 os.pendingBrace(false);
121 char_type const c = *cit;
123 docstring command(1, c);
124 if (c < 0x80 || Encodings::latexMathChar(c, os.encoding(), command)) {
126 if (in_forced_mode) {
127 // we were inside \lyxmathsym
130 in_forced_mode = false;
133 os << "\\ensuremath{";
135 in_forced_mode = true;
137 } else if (c < 0x80 && 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.
157 if (lyx::support::contains(command, '\\')
158 && isAlphaASCII(command[command.size() - 1]))
159 os.pendingSpace(true);
160 } catch (EncodingException & e) {
162 // FIXME: this is OK for View->Source
163 // but math preview will likely fail.
164 os << "<" << _("LyX Warning: ")
165 << _("uncodable character") << " '";
166 os << docstring(1, e.failed_char);
176 if (in_forced_mode && os.textMode()) {
177 // We have to care for closing \lyxmathsym
181 os.pendingBrace(in_forced_mode);