A bit more re-organization.
[lyx.git] / src / mathed / InsetMathString.cpp
blob8b9db39c818facae13cab001d4dce3b7f6e6d0c2
1 /**
2 * \file InsetMathString.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author André Pönitz
8 * Full author contact details are available in file CREDITS.
9 */
11 #include <config.h>
13 #include "InsetMathString.h"
14 #include "MathFactory.h"
15 #include "MathStream.h"
16 #include "MathSupport.h"
18 #include "Encoding.h"
20 #include "support/debug.h"
21 #include "support/gettext.h"
22 #include "support/lstrings.h"
23 #include "support/textutils.h"
26 namespace lyx {
28 InsetMathString::InsetMathString(docstring const & s)
29 : str_(s)
33 Inset * InsetMathString::clone() const
35 return new InsetMathString(*this);
39 void InsetMathString::metrics(MetricsInfo & mi, Dimension & dim) const
41 mathed_string_dim(mi.base.font, str_, dim);
45 void InsetMathString::draw(PainterInfo & pi, int x, int y) const
47 pi.draw(x, y, str_);
51 void InsetMathString::normalize(NormalStream & os) const
53 os << "[string " << str_ << ' ' << "mathalpha" << ']';
57 void InsetMathString::maple(MapleStream & os) const
59 if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
60 os << ' ' << str_ << ' ';
61 return;
64 // insert '*' between adjacent chars if type is LM_TC_VAR
65 os << str_[0];
66 for (size_t i = 1; i < str_.size(); ++i)
67 os << str_[i];
71 void InsetMathString::mathematica(MathematicaStream & os) const
73 os << ' ' << str_ << ' ';
77 void InsetMathString::octave(OctaveStream & os) const
79 if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
80 os << ' ' << str_ << ' ';
81 return;
84 // insert '*' between adjacent chars if type is LM_TC_VAR
85 os << str_[0];
86 for (size_t i = 1; i < str_.size(); ++i)
87 os << str_[i];
91 void InsetMathString::mathmlize(MathStream & os) const
94 if (code_ == LM_TC_VAR)
95 os << "<mi> " << str_ << " </mi>";
96 else if (code_ == LM_TC_CONST)
97 os << "<mn> " << str_ << " </mn>";
98 else if (code_ == LM_TC_RM || code_ == LM_TC_TEXTRM)
99 os << "<mtext> " << str_ << " </mtext>";
100 else
102 os << str_;
106 void InsetMathString::write(WriteStream & os) const
108 if (!os.latex() || os.lockedMode()) {
109 os << str_;
110 return;
113 docstring::const_iterator cit = str_.begin();
114 docstring::const_iterator end = str_.end();
116 // We may already be inside an \ensuremath command.
117 bool in_forced_mode = os.pendingBrace();
119 // We will take care of matching braces.
120 os.pendingBrace(false);
122 while (cit != end) {
123 bool mathmode = in_forced_mode ? os.textMode() : !os.textMode();
124 char_type const c = *cit;
125 docstring command(1, c);
126 try {
127 if (c < 0x80 || Encodings::latexMathChar(c, mathmode, os.encoding(), command)) {
128 if (os.textMode()) {
129 if (in_forced_mode) {
130 // we were inside \lyxmathsym
131 os << '}';
132 os.textMode(false);
133 in_forced_mode = false;
135 if (c >= 0x80 && os.textMode()) {
136 os << "\\ensuremath{";
137 os.textMode(false);
138 in_forced_mode = true;
140 } else if (c < 0x80 && in_forced_mode) {
141 // we were inside \ensuremath
142 os << '}';
143 os.textMode(true);
144 in_forced_mode = false;
146 } else if (!os.textMode()) {
147 if (in_forced_mode) {
148 // we were inside \ensuremath
149 os << '}';
150 in_forced_mode = false;
151 } else {
152 os << "\\lyxmathsym{";
153 in_forced_mode = true;
155 os.textMode(true);
157 os << command;
158 // We may need a space if the command contains a macro
159 // and the last char is ASCII.
160 if (lyx::support::contains(command, '\\')
161 && isAlphaASCII(command[command.size() - 1]))
162 os.pendingSpace(true);
163 } catch (EncodingException & e) {
164 switch (os.output()) {
165 case WriteStream::wsDryrun: {
166 os << "<" << _("LyX Warning: ")
167 << _("uncodable character") << " '";
168 os << docstring(1, e.failed_char);
169 os << "'>";
170 break;
172 case WriteStream::wsPreview: {
173 // indicate the encoding error by a boxed '?'
174 os << "{\\fboxsep=1pt\\fbox{?}}";;
175 LYXERR0("Uncodable character" << " '"
176 << docstring(1, e.failed_char)
177 << "'");
178 break;
180 case WriteStream::wsDefault:
181 default:
182 // throw again
183 throw(e);
186 ++cit;
189 if (in_forced_mode && os.textMode()) {
190 // We have to care for closing \lyxmathsym
191 os << '}';
192 os.textMode(false);
193 } else {
194 os.pendingBrace(in_forced_mode);
199 } // namespace lyx