]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathString.cpp
Allow to specify either text mode or math mode commands (or both) in the
[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
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         docstring::const_iterator cit = str_.begin();
110         docstring::const_iterator end = str_.end();
111
112         bool in_lyxmathsym = false;
113         while (cit != end) {
114                 char_type const c = *cit;
115                 try {
116                         docstring command(1, c);
117                         if (c < 0x80 || Encodings::latexMathChar(c, command)) {
118                                 if (in_lyxmathsym) {
119                                         os << '}';
120                                         in_lyxmathsym = false;
121                                 }
122                                 os << command;
123                         } else {
124                                 if (!in_lyxmathsym) {
125                                         os << "\\lyxmathsym{";
126                                         in_lyxmathsym = true;
127                                 }
128                                 os << command;
129                         }
130                 } catch (EncodingException & e) {
131                         if (os.dryrun()) {
132                                 // FIXME: this is OK for View->Source
133                                 // but math preview will likely fail.
134                                 os << "<" << _("LyX Warning: ")
135                                    << _("uncodable character") << " '";
136                                 os << docstring(1, e.failed_char);
137                                 os << "'>";
138                         } else {
139                                 // throw again
140                                 throw(e);
141                         }
142                 }
143                 ++cit;
144         }
145         if (in_lyxmathsym)
146                 os << '}';
147 }
148
149
150 } // namespace lyx