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"
29 InsetMathString::InsetMathString(docstring const & s)
34 Inset * InsetMathString::clone() const
36 return new InsetMathString(*this);
40 void InsetMathString::metrics(MetricsInfo & mi, Dimension & dim) const
42 mathed_string_dim(mi.base.font, str_, dim);
46 void InsetMathString::draw(PainterInfo & pi, int x, int y) const
52 void InsetMathString::normalize(NormalStream & os) const
54 os << "[string " << str_ << ' ' << "mathalpha" << ']';
58 void InsetMathString::maple(MapleStream & os) const
60 if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
61 os << ' ' << str_ << ' ';
65 // insert '*' between adjacent chars if type is LM_TC_VAR
67 for (size_t i = 1; i < str_.size(); ++i)
72 void InsetMathString::mathematica(MathematicaStream & os) const
74 os << ' ' << str_ << ' ';
78 void InsetMathString::octave(OctaveStream & os) const
80 if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
81 os << ' ' << str_ << ' ';
85 // insert '*' between adjacent chars if type is LM_TC_VAR
87 for (size_t i = 1; i < str_.size(); ++i)
92 void InsetMathString::mathmlize(MathStream & os) const
94 // useless, no doubt, but we should not be here
95 LASSERT(false, /* */);
99 void InsetMathString::write(WriteStream & os) const
101 if (!os.latex() || os.lockedMode()) {
106 docstring::const_iterator cit = str_.begin();
107 docstring::const_iterator end = str_.end();
109 // We may already be inside an \ensuremath command.
110 bool in_forced_mode = os.pendingBrace();
112 // We will take care of matching braces.
113 os.pendingBrace(false);
116 bool mathmode = in_forced_mode ? os.textMode() : !os.textMode();
117 char_type const c = *cit;
118 docstring command(1, c);
120 if (c < 0x80 || Encodings::latexMathChar(c, mathmode, os.encoding(), command)) {
122 if (in_forced_mode) {
123 // we were inside \lyxmathsym
126 in_forced_mode = false;
128 if (c >= 0x80 && os.textMode()) {
129 os << "\\ensuremath{";
131 in_forced_mode = true;
133 } else if (c < 0x80 && in_forced_mode) {
134 // we were inside \ensuremath
137 in_forced_mode = false;
139 } else if (!os.textMode()) {
140 if (in_forced_mode) {
141 // we were inside \ensuremath
143 in_forced_mode = false;
145 os << "\\lyxmathsym{";
146 in_forced_mode = true;
151 // We may need a space if the command contains a macro
152 // and the last char is ASCII.
153 if (lyx::support::contains(command, '\\')
154 && isAlphaASCII(command[command.size() - 1]))
155 os.pendingSpace(true);
156 } catch (EncodingException & e) {
157 switch (os.output()) {
158 case WriteStream::wsDryrun: {
159 os << "<" << _("LyX Warning: ")
160 << _("uncodable character") << " '";
161 os << docstring(1, e.failed_char);
165 case WriteStream::wsPreview: {
166 // indicate the encoding error by a boxed '?'
167 os << "{\\fboxsep=1pt\\fbox{?}}";;
168 LYXERR0("Uncodable character" << " '"
169 << docstring(1, e.failed_char)
173 case WriteStream::wsDefault:
182 if (in_forced_mode && os.textMode()) {
183 // We have to care for closing \lyxmathsym
187 os.pendingBrace(in_forced_mode);