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::write(WriteStream & os) const
94 if (!os.latex() || os.lockedMode()) {
99 docstring::const_iterator cit = str_.begin();
100 docstring::const_iterator end = str_.end();
102 // We may already be inside an \ensuremath command.
103 bool in_forced_mode = os.pendingBrace();
105 // We will take care of matching braces.
106 os.pendingBrace(false);
109 bool mathmode = in_forced_mode ? os.textMode() : !os.textMode();
110 char_type const c = *cit;
111 docstring command(1, c);
113 if (c < 0x80 || Encodings::latexMathChar(c, mathmode, os.encoding(), command)) {
115 if (in_forced_mode) {
116 // we were inside \lyxmathsym
119 in_forced_mode = false;
121 if (c >= 0x80 && os.textMode()) {
122 os << "\\ensuremath{";
124 in_forced_mode = true;
126 } else if (c < 0x80 && in_forced_mode) {
127 // we were inside \ensuremath
130 in_forced_mode = false;
132 } else if (!os.textMode()) {
133 if (in_forced_mode) {
134 // we were inside \ensuremath
136 in_forced_mode = false;
138 os << "\\lyxmathsym{";
139 in_forced_mode = true;
144 // We may need a space if the command contains a macro
145 // and the last char is ASCII.
146 if (lyx::support::contains(command, '\\')
147 && isAlphaASCII(command[command.size() - 1]))
148 os.pendingSpace(true);
149 } catch (EncodingException & e) {
150 switch (os.output()) {
151 case WriteStream::wsDryrun: {
152 os << "<" << _("LyX Warning: ")
153 << _("uncodable character") << " '";
154 os << docstring(1, e.failed_char);
158 case WriteStream::wsPreview: {
159 // indicate the encoding error by a boxed '?'
160 os << "{\\fboxsep=1pt\\fbox{?}}";;
161 LYXERR0("Uncodable character" << " '"
162 << docstring(1, e.failed_char)
166 case WriteStream::wsDefault:
175 if (in_forced_mode && os.textMode()) {
176 // We have to care for closing \lyxmathsym
180 os.pendingBrace(in_forced_mode);