]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathString.cpp
Fix bug #7106: iterator out of range while copying multi-row math.
[lyx.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 using lyx::support::escape;
27
28
29 namespace lyx {
30
31 InsetMathString::InsetMathString(docstring const & s)
32         : str_(s)
33 {}
34
35
36 Inset * InsetMathString::clone() const
37 {
38         return new InsetMathString(*this);
39 }
40
41
42 void InsetMathString::metrics(MetricsInfo & mi, Dimension & dim) const
43 {
44         mathed_string_dim(mi.base.font, str_, dim);
45 }
46
47
48 void InsetMathString::draw(PainterInfo & pi, int x, int y) const
49 {
50         pi.draw(x, y, str_);
51 }
52
53
54 void InsetMathString::normalize(NormalStream & os) const
55 {
56         os << "[string " << str_ << ' ' << "mathalpha" << ']';
57 }
58
59
60 void InsetMathString::maple(MapleStream & os) const
61 {
62         if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
63                 os << ' ' << str_ << ' ';
64                 return;
65         }
66
67         // insert '*' between adjacent chars if type is LM_TC_VAR
68         os << str_[0];
69         for (size_t i = 1; i < str_.size(); ++i)
70                 os << str_[i];
71 }
72
73
74 void InsetMathString::mathematica(MathematicaStream & os) const
75 {
76         os << ' ' << str_ << ' ';
77 }
78
79
80 void InsetMathString::octave(OctaveStream & os) const
81 {
82         if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
83                 os << ' ' << str_ << ' ';
84                 return;
85         }
86
87         // insert '*' between adjacent chars if type is LM_TC_VAR
88         os << str_[0];
89         for (size_t i = 1; i < str_.size(); ++i)
90                 os << str_[i];
91 }
92
93
94 void InsetMathString::mathmlize(MathStream & /*os*/) const
95 {
96         // useless, no doubt, but we should not be here
97         LASSERT(false, /* */);
98 }
99
100
101 void InsetMathString::write(WriteStream & os) const
102 {
103         if (!os.latex() || os.lockedMode()) {
104                 os << (os.asciiOnly() ? escape(str_) : str_);
105                 return;
106         }
107
108         docstring::const_iterator cit = str_.begin();
109         docstring::const_iterator end = str_.end();
110
111         // We may already be inside an \ensuremath command.
112         bool in_forced_mode = os.pendingBrace();
113
114         // We will take care of matching braces.
115         os.pendingBrace(false);
116
117         while (cit != end) {
118                 bool mathmode = in_forced_mode ? os.textMode() : !os.textMode();
119                 char_type const c = *cit;
120                 docstring command(1, c);
121                 try {
122                         if (c < 0x80 || Encodings::latexMathChar(c, mathmode, os.encoding(), command)) {
123                                 if (os.textMode()) {
124                                         if (in_forced_mode) {
125                                                 // we were inside \lyxmathsym
126                                                 os << '}';
127                                                 os.textMode(false);
128                                                 in_forced_mode = false;
129                                         }
130                                         if (c >= 0x80 && os.textMode()) {
131                                                 os << "\\ensuremath{";
132                                                 os.textMode(false);
133                                                 in_forced_mode = true;
134                                         }
135                                 } else if (c < 0x80 && in_forced_mode) {
136                                         // we were inside \ensuremath
137                                         os << '}';
138                                         os.textMode(true);
139                                         in_forced_mode = false;
140                                 }
141                         } else if (!os.textMode()) {
142                                         if (in_forced_mode) {
143                                                 // we were inside \ensuremath
144                                                 os << '}';
145                                                 in_forced_mode = false;
146                                         } else {
147                                                 os << "\\lyxmathsym{";
148                                                 in_forced_mode = true;
149                                         }
150                                         os.textMode(true);
151                         }
152                         os << command;
153                         // We may need a space if the command contains a macro
154                         // and the last char is ASCII.
155                         if (lyx::support::contains(command, '\\')
156                             && isAlphaASCII(command[command.size() - 1]))
157                                 os.pendingSpace(true);
158                 } catch (EncodingException & e) {
159                         switch (os.output()) {
160                         case WriteStream::wsDryrun: {
161                                 os << "<" << _("LyX Warning: ")
162                                    << _("uncodable character") << " '";
163                                 os << docstring(1, e.failed_char);
164                                 os << "'>";
165                                 break;
166                         }
167                         case WriteStream::wsPreview: {
168                                 // indicate the encoding error by a boxed '?'
169                                 os << "{\\fboxsep=1pt\\fbox{?}}";
170                                 LYXERR0("Uncodable character" << " '"
171                                         << docstring(1, e.failed_char)
172                                         << "'");
173                                 break;
174                         }
175                         case WriteStream::wsDefault:
176                         default:
177                                 // throw again
178                                 throw(e);
179                         }
180                 }
181                 ++cit;
182         }
183
184         if (in_forced_mode && os.textMode()) {
185                 // We have to care for closing \lyxmathsym
186                 os << '}';
187                 os.textMode(false);
188         } else {
189                 os.pendingBrace(in_forced_mode);
190         }
191 }
192
193
194 } // namespace lyx