]> git.lyx.org Git - features.git/blob - src/mathed/InsetMathString.cpp
Introduce a return value for mathmlize(). We will need this to be able
[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::write(WriteStream & os) const
93 {
94         if (!os.latex() || os.lockedMode()) {
95                 os << str_;
96                 return;
97         }
98
99         docstring::const_iterator cit = str_.begin();
100         docstring::const_iterator end = str_.end();
101
102         // We may already be inside an \ensuremath command.
103         bool in_forced_mode = os.pendingBrace();
104
105         // We will take care of matching braces.
106         os.pendingBrace(false);
107
108         while (cit != end) {
109                 bool mathmode = in_forced_mode ? os.textMode() : !os.textMode();
110                 char_type const c = *cit;
111                 docstring command(1, c);
112                 try {
113                         if (c < 0x80 || Encodings::latexMathChar(c, mathmode, os.encoding(), command)) {
114                                 if (os.textMode()) {
115                                         if (in_forced_mode) {
116                                                 // we were inside \lyxmathsym
117                                                 os << '}';
118                                                 os.textMode(false);
119                                                 in_forced_mode = false;
120                                         }
121                                         if (c >= 0x80 && os.textMode()) {
122                                                 os << "\\ensuremath{";
123                                                 os.textMode(false);
124                                                 in_forced_mode = true;
125                                         }
126                                 } else if (c < 0x80 && in_forced_mode) {
127                                         // we were inside \ensuremath
128                                         os << '}';
129                                         os.textMode(true);
130                                         in_forced_mode = false;
131                                 }
132                         } else if (!os.textMode()) {
133                                         if (in_forced_mode) {
134                                                 // we were inside \ensuremath
135                                                 os << '}';
136                                                 in_forced_mode = false;
137                                         } else {
138                                                 os << "\\lyxmathsym{";
139                                                 in_forced_mode = true;
140                                         }
141                                         os.textMode(true);
142                         }
143                         os << command;
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);
155                                 os << "'>";
156                                 break;
157                         }
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)
163                                         << "'");
164                                 break;
165                         }
166                         case WriteStream::wsDefault:
167                         default:
168                                 // throw again
169                                 throw(e);
170                         }
171                 }
172                 ++cit;
173         }
174
175         if (in_forced_mode && os.textMode()) {
176                 // We have to care for closing \lyxmathsym
177                 os << '}';
178                 os.textMode(false);
179         } else {
180                 os.pendingBrace(in_forced_mode);
181         }
182 }
183
184
185 } // namespace lyx