]> git.lyx.org Git - features.git/blob - src/mathed/InsetMathString.cpp
squash warning
[features.git] / src / mathed / InsetMathString.cpp
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.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetMathString.h"
14 #include "MathFactory.h"
15 #include "MathStream.h"
16 #include "MathSupport.h"
17
18 #include "Encoding.h"
19
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"
25
26
27 namespace lyx {
28
29 InsetMathString::InsetMathString(docstring const & s)
30         : str_(s)
31 {}
32
33
34 Inset * InsetMathString::clone() const
35 {
36         return new InsetMathString(*this);
37 }
38
39
40 void InsetMathString::metrics(MetricsInfo & mi, Dimension & dim) const
41 {
42         mathed_string_dim(mi.base.font, str_, dim);
43 }
44
45
46 void InsetMathString::draw(PainterInfo & pi, int x, int y) const
47 {
48         pi.draw(x, y, str_);
49 }
50
51
52 void InsetMathString::normalize(NormalStream & os) const
53 {
54         os << "[string " << str_ << ' ' << "mathalpha" << ']';
55 }
56
57
58 void InsetMathString::maple(MapleStream & os) const
59 {
60         if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
61                 os << ' ' << str_ << ' ';
62                 return;
63         }
64
65         // insert '*' between adjacent chars if type is LM_TC_VAR
66         os << str_[0];
67         for (size_t i = 1; i < str_.size(); ++i)
68                 os << str_[i];
69 }
70
71
72 void InsetMathString::mathematica(MathematicaStream & os) const
73 {
74         os << ' ' << str_ << ' ';
75 }
76
77
78 void InsetMathString::octave(OctaveStream & os) const
79 {
80         if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
81                 os << ' ' << str_ << ' ';
82                 return;
83         }
84
85         // insert '*' between adjacent chars if type is LM_TC_VAR
86         os << str_[0];
87         for (size_t i = 1; i < str_.size(); ++i)
88                 os << str_[i];
89 }
90
91
92 void InsetMathString::mathmlize(MathStream & /*os*/) const
93 {
94         // useless, no doubt, but we should not be here
95         LASSERT(false, /* */);
96 }
97
98
99 void InsetMathString::write(WriteStream & os) const
100 {
101         if (!os.latex() || os.lockedMode()) {
102                 os << str_;
103                 return;
104         }
105
106         docstring::const_iterator cit = str_.begin();
107         docstring::const_iterator end = str_.end();
108
109         // We may already be inside an \ensuremath command.
110         bool in_forced_mode = os.pendingBrace();
111
112         // We will take care of matching braces.
113         os.pendingBrace(false);
114
115         while (cit != end) {
116                 bool mathmode = in_forced_mode ? os.textMode() : !os.textMode();
117                 char_type const c = *cit;
118                 docstring command(1, c);
119                 try {
120                         if (c < 0x80 || Encodings::latexMathChar(c, mathmode, os.encoding(), command)) {
121                                 if (os.textMode()) {
122                                         if (in_forced_mode) {
123                                                 // we were inside \lyxmathsym
124                                                 os << '}';
125                                                 os.textMode(false);
126                                                 in_forced_mode = false;
127                                         }
128                                         if (c >= 0x80 && os.textMode()) {
129                                                 os << "\\ensuremath{";
130                                                 os.textMode(false);
131                                                 in_forced_mode = true;
132                                         }
133                                 } else if (c < 0x80 && in_forced_mode) {
134                                         // we were inside \ensuremath
135                                         os << '}';
136                                         os.textMode(true);
137                                         in_forced_mode = false;
138                                 }
139                         } else if (!os.textMode()) {
140                                         if (in_forced_mode) {
141                                                 // we were inside \ensuremath
142                                                 os << '}';
143                                                 in_forced_mode = false;
144                                         } else {
145                                                 os << "\\lyxmathsym{";
146                                                 in_forced_mode = true;
147                                         }
148                                         os.textMode(true);
149                         }
150                         os << command;
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);
162                                 os << "'>";
163                                 break;
164                         }
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)
170                                         << "'");
171                                 break;
172                         }
173                         case WriteStream::wsDefault:
174                         default:
175                                 // throw again
176                                 throw(e);
177                         }
178                 }
179                 ++cit;
180         }
181
182         if (in_forced_mode && os.textMode()) {
183                 // We have to care for closing \lyxmathsym
184                 os << '}';
185                 os.textMode(false);
186         } else {
187                 os.pendingBrace(in_forced_mode);
188         }
189 }
190
191
192 } // namespace lyx