]> git.lyx.org Git - features.git/blob - src/mathed/InsetMathString.cpp
Fix bug 3938
[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 "MathStream.h"
15 #include "MathSupport.h"
16
17 #include "Encoding.h"
18
19 #include "support/gettext.h"
20
21
22 namespace lyx {
23
24 InsetMathString::InsetMathString(docstring const & s)
25         : str_(s)
26 {}
27
28
29 Inset * InsetMathString::clone() const
30 {
31         return new InsetMathString(*this);
32 }
33
34
35 void InsetMathString::metrics(MetricsInfo & mi, Dimension & dim) const
36 {
37         mathed_string_dim(mi.base.font, str_, dim);
38 }
39
40
41 void InsetMathString::draw(PainterInfo & pi, int x, int y) const
42 {
43         pi.draw(x, y, str_);
44 }
45
46
47 void InsetMathString::normalize(NormalStream & os) const
48 {
49         os << "[string " << str_ << ' ' << "mathalpha" << ']';
50 }
51
52
53 void InsetMathString::maple(MapleStream & os) const
54 {
55         if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
56                 os << ' ' << str_ << ' ';
57                 return;
58         }
59
60         // insert '*' between adjacent chars if type is LM_TC_VAR
61         os << str_[0];
62         for (size_t i = 1; i < str_.size(); ++i)
63                 os << str_[i];
64 }
65
66
67 void InsetMathString::mathematica(MathematicaStream & os) const
68 {
69         os << ' ' << str_ << ' ';
70 }
71
72
73 void InsetMathString::octave(OctaveStream & os) const
74 {
75         if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
76                 os << ' ' << str_ << ' ';
77                 return;
78         }
79
80         // insert '*' between adjacent chars if type is LM_TC_VAR
81         os << str_[0];
82         for (size_t i = 1; i < str_.size(); ++i)
83                 os << str_[i];
84 }
85
86
87 void InsetMathString::mathmlize(MathStream & os) const
88 {
89 /*
90         if (code_ == LM_TC_VAR)
91                 os << "<mi> " << str_ << " </mi>";
92         else if (code_ == LM_TC_CONST)
93                 os << "<mn> " << str_ << " </mn>";
94         else if (code_ == LM_TC_RM || code_ == LM_TC_TEXTRM)
95                 os << "<mtext> " << str_ <<  " </mtext>";
96         else
97 */
98                 os << str_;
99 }
100
101
102 void InsetMathString::write(WriteStream & os) const
103 {
104         if (!os.latex()) {
105                 os << str_;
106                 return;
107         }
108
109         // We need the latex macros defined in the unicodesymbols file.
110         // As they do not depend on the encoding, simply use the first
111         // available encoding.
112         Encodings::const_iterator encit = encodings.begin();
113         bool can_encode = encit != encodings.end();
114
115         docstring::const_iterator cit = str_.begin();
116         docstring::const_iterator end = str_.end();
117
118         bool in_mathsym = false;
119         while (cit != end) {
120                 char_type const c = *cit;
121                 try {
122                         if (c < 0x80) {
123                                 if (in_mathsym) {
124                                         os << '}';
125                                         in_mathsym = false;
126                                 }
127                                 os << docstring(1, c);
128                         } else if (can_encode) {
129                                 if (!in_mathsym) {
130                                         os << "\\mathsym{";
131                                         in_mathsym = true;
132                                 }
133                                 os << encit->latexChar(c, true);
134                         } else {
135                                 throw EncodingException(c);
136                         }
137                 } catch (EncodingException & e) {
138                         if (os.dryrun()) {
139                                 // FIXME: this is OK for View->Source
140                                 // but math preview will likely fail.
141                                 os << "<" << _("LyX Warning: ")
142                                    << _("uncodable character") << " '";
143                                 os << docstring(1, e.failed_char);
144                                 os << "'>";
145                         } else {
146                                 // throw again
147                                 throw(e);
148                         }
149                 }
150                 ++cit;
151         }
152         if (in_mathsym)
153                 os << '}';
154 }
155
156
157 } // namespace lyx