]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathString.cpp
more latin1..utf8 schanges. all of src/* should be utf8 now
[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 "MathStream.h"
15 #include "MathSupport.h"
16
17 #include "Encoding.h"
18
19 #include "support/gettext.h"
20 #include "support/lstrings.h"
21 #include "support/textutils.h"
22
23
24 namespace lyx {
25
26 InsetMathString::InsetMathString(docstring const & s)
27         : str_(s)
28 {}
29
30
31 Inset * InsetMathString::clone() const
32 {
33         return new InsetMathString(*this);
34 }
35
36
37 void InsetMathString::metrics(MetricsInfo & mi, Dimension & dim) const
38 {
39         mathed_string_dim(mi.base.font, str_, dim);
40 }
41
42
43 void InsetMathString::draw(PainterInfo & pi, int x, int y) const
44 {
45         pi.draw(x, y, str_);
46 }
47
48
49 void InsetMathString::normalize(NormalStream & os) const
50 {
51         os << "[string " << str_ << ' ' << "mathalpha" << ']';
52 }
53
54
55 void InsetMathString::maple(MapleStream & os) const
56 {
57         if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
58                 os << ' ' << str_ << ' ';
59                 return;
60         }
61
62         // insert '*' between adjacent chars if type is LM_TC_VAR
63         os << str_[0];
64         for (size_t i = 1; i < str_.size(); ++i)
65                 os << str_[i];
66 }
67
68
69 void InsetMathString::mathematica(MathematicaStream & os) const
70 {
71         os << ' ' << str_ << ' ';
72 }
73
74
75 void InsetMathString::octave(OctaveStream & os) const
76 {
77         if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
78                 os << ' ' << str_ << ' ';
79                 return;
80         }
81
82         // insert '*' between adjacent chars if type is LM_TC_VAR
83         os << str_[0];
84         for (size_t i = 1; i < str_.size(); ++i)
85                 os << str_[i];
86 }
87
88
89 void InsetMathString::mathmlize(MathStream & os) const
90 {
91 /*
92         if (code_ == LM_TC_VAR)
93                 os << "<mi> " << str_ << " </mi>";
94         else if (code_ == LM_TC_CONST)
95                 os << "<mn> " << str_ << " </mn>";
96         else if (code_ == LM_TC_RM || code_ == LM_TC_TEXTRM)
97                 os << "<mtext> " << str_ <<  " </mtext>";
98         else
99 */
100                 os << str_;
101 }
102
103
104 void InsetMathString::write(WriteStream & os) const
105 {
106         if (!os.latex()) {
107                 os << str_;
108                 return;
109         }
110
111         docstring::const_iterator cit = str_.begin();
112         docstring::const_iterator end = str_.end();
113
114         // We may already be inside an \ensuremath command.
115         bool in_forced_mode = os.pendingBrace();
116
117         // We will take care of matching braces.
118         os.pendingBrace(false);
119
120         while (cit != end) {
121                 bool mathmode = in_forced_mode ? os.textMode() : !os.textMode();
122                 char_type const c = *cit;
123                 docstring command(1, c);
124                 try {
125                         if (c < 0x80 || Encodings::latexMathChar(c, mathmode, os.encoding(), command)) {
126                                 if (os.textMode()) {
127                                         if (in_forced_mode) {
128                                                 // we were inside \lyxmathsym
129                                                 os << '}';
130                                                 os.textMode(false);
131                                                 in_forced_mode = false;
132                                         }
133                                         if (c >= 0x80 && os.textMode()) {
134                                                 os << "\\ensuremath{";
135                                                 os.textMode(false);
136                                                 in_forced_mode = true;
137                                         }
138                                 } else if (c < 0x80 && in_forced_mode) {
139                                         // we were inside \ensuremath
140                                         os << '}';
141                                         os.textMode(true);
142                                         in_forced_mode = false;
143                                 }
144                         } else if (!os.textMode()) {
145                                         if (in_forced_mode) {
146                                                 // we were inside \ensuremath
147                                                 os << '}';
148                                                 in_forced_mode = false;
149                                         } else {
150                                                 os << "\\lyxmathsym{";
151                                                 in_forced_mode = true;
152                                         }
153                                         os.textMode(true);
154                         }
155                         os << command;
156                         // We may need a space if the command contains a macro
157                         // and the last char is ASCII.
158                         if (lyx::support::contains(command, '\\')
159                             && isAlphaASCII(command[command.size() - 1]))
160                                 os.pendingSpace(true);
161                 } catch (EncodingException & e) {
162                         if (os.dryrun()) {
163                                 // FIXME: this is OK for View->Source
164                                 // but math preview will likely fail.
165                                 os << "<" << _("LyX Warning: ")
166                                    << _("uncodable character") << " '";
167                                 os << docstring(1, e.failed_char);
168                                 os << "'>";
169                         } else {
170                                 // throw again
171                                 throw(e);
172                         }
173                 }
174                 ++cit;
175         }
176
177         if (in_forced_mode && os.textMode()) {
178                 // We have to care for closing \lyxmathsym
179                 os << '}';
180                 os.textMode(false);
181         } else {
182                 os.pendingBrace(in_forced_mode);
183         }
184 }
185
186
187 } // namespace lyx