]> git.lyx.org Git - features.git/blob - src/mathed/InsetMathString.cpp
Catch encoding exception when performing instant preview on an uncodable char (bug...
[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/lstrings.h"
23 #include "support/textutils.h"
24
25
26 namespace lyx {
27
28 InsetMathString::InsetMathString(docstring const & s)
29         : str_(s)
30 {}
31
32
33 Inset * InsetMathString::clone() const
34 {
35         return new InsetMathString(*this);
36 }
37
38
39 void InsetMathString::metrics(MetricsInfo & mi, Dimension & dim) const
40 {
41         mathed_string_dim(mi.base.font, str_, dim);
42 }
43
44
45 void InsetMathString::draw(PainterInfo & pi, int x, int y) const
46 {
47         pi.draw(x, y, str_);
48 }
49
50
51 void InsetMathString::normalize(NormalStream & os) const
52 {
53         os << "[string " << str_ << ' ' << "mathalpha" << ']';
54 }
55
56
57 void InsetMathString::maple(MapleStream & os) const
58 {
59         if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
60                 os << ' ' << str_ << ' ';
61                 return;
62         }
63
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];
68 }
69
70
71 void InsetMathString::mathematica(MathematicaStream & os) const
72 {
73         os << ' ' << str_ << ' ';
74 }
75
76
77 void InsetMathString::octave(OctaveStream & os) const
78 {
79         if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
80                 os << ' ' << str_ << ' ';
81                 return;
82         }
83
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];
88 }
89
90
91 void InsetMathString::mathmlize(MathStream & os) const
92 {
93 /*
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
101 */
102                 os << str_;
103 }
104
105
106 void InsetMathString::write(WriteStream & os) const
107 {
108         if (!os.latex() || os.lockedMode()) {
109                 os << str_;
110                 return;
111         }
112
113         docstring::const_iterator cit = str_.begin();
114         docstring::const_iterator end = str_.end();
115
116         // We may already be inside an \ensuremath command.
117         bool in_forced_mode = os.pendingBrace();
118
119         // We will take care of matching braces.
120         os.pendingBrace(false);
121
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;
134                                         }
135                                         if (c >= 0x80 && os.textMode()) {
136                                                 os << "\\ensuremath{";
137                                                 os.textMode(false);
138                                                 in_forced_mode = true;
139                                         }
140                                 } else if (c < 0x80 && in_forced_mode) {
141                                         // we were inside \ensuremath
142                                         os << '}';
143                                         os.textMode(true);
144                                         in_forced_mode = false;
145                                 }
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;
154                                         }
155                                         os.textMode(true);
156                         }
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;
171                         }
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;
179                         }
180                         case WriteStream::wsDefault:
181                         default:
182                                 // throw again
183                                 throw(e);
184                         }
185                 }
186                 ++cit;
187         }
188
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);
195         }
196 }
197
198
199 } // namespace lyx