]> git.lyx.org Git - lyx.git/blob - src/mathed/math_symbolinset.C
make LyX/Mac compile and work better
[lyx.git] / src / mathed / math_symbolinset.C
1 /**
2  * \file math_symbolinset.C
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 "math_symbolinset.h"
14 #include "dimension.h"
15 #include "math_mathmlstream.h"
16 #include "math_streamstr.h"
17 #include "math_support.h"
18 #include "math_parser.h"
19 #include "math_atom.h"
20 #include "LaTeXFeatures.h"
21 #include "debug.h"
22
23
24 using std::string;
25 using std::auto_ptr;
26
27
28 MathSymbolInset::MathSymbolInset(latexkeys const * l)
29         : sym_(l), h_(0)
30 {}
31
32
33 MathSymbolInset::MathSymbolInset(char const * name)
34         : sym_(in_word_set(name)), h_(0)
35 {}
36
37
38 MathSymbolInset::MathSymbolInset(string const & name)
39         : sym_(in_word_set(name.c_str())), h_(0)
40 {}
41
42
43 auto_ptr<InsetBase> MathSymbolInset::clone() const
44 {
45         return auto_ptr<InsetBase>(new MathSymbolInset(*this));
46 }
47
48
49 string MathSymbolInset::name() const
50 {
51         return sym_->name;
52 }
53
54
55 void MathSymbolInset::metrics(MetricsInfo & mi, Dimension & dim) const
56 {
57         //lyxerr << "metrics: symbol: '" << sym_->name
58         //      << "' in font: '" << sym_->inset
59         //      << "' drawn as: '" << sym_->draw
60         //      << "'" << std::endl;
61
62         int const em = mathed_char_width(mi.base.font, 'M');
63         FontSetChanger dummy(mi.base, sym_->inset.c_str());
64         mathed_string_dim(mi.base.font, sym_->draw, dim);
65         // correct height for broken cmex and wasy font
66 #if defined(__APPLE__) && defined(__GNUC__)
67         if (sym_->inset == "cmex") {
68                 h_ = 4 * dim.des / 5;
69                 dim.asc += 0*h_;
70                 dim.des -= h_;
71                 h_ = dim.asc;
72         } else if (sym_->inset == "wasy") {
73                 h_ = 4 * dim.des / 5;
74                 dim.asc += h_;
75                 dim.des -= h_;
76         }
77 #else
78         if (sym_->inset == "cmex" || sym_->inset == "wasy") {
79                 h_ = 4 * dim.des / 5;
80                 dim.asc += h_;
81                 dim.des -= h_;
82         }
83 #endif
84         // seperate things a bit
85         if (isRelOp())
86                 dim.wid += static_cast<int>(0.5 * em + 0.5);
87         else
88                 dim.wid += static_cast<int>(0.1667 * em + 0.5);
89
90         scriptable_ = false;
91         if (mi.base.style == LM_ST_DISPLAY)
92                 if (sym_->inset == "cmex" || sym_->extra == "funclim")
93                         scriptable_ = true;
94 }
95
96
97 void MathSymbolInset::draw(PainterInfo & pi, int x, int y) const
98 {
99         //lyxerr << "metrics: symbol: '" << sym_->name
100         //      << "' in font: '" << sym_->inset
101         //      << "' drawn as: '" << sym_->draw
102         //      << "'" << std::endl;
103         int const em = mathed_char_width(pi.base.font, 'M');
104         if (isRelOp())
105                 x += static_cast<int>(0.25*em+0.5);
106         else
107                 x += static_cast<int>(0.0833*em+0.5);
108
109         FontSetChanger dummy(pi.base, sym_->inset.c_str());
110         drawStr(pi, pi.base.font, x, y - h_, sym_->draw);
111 }
112
113
114 bool MathSymbolInset::isRelOp() const
115 {
116         return sym_->extra == "mathrel";
117 }
118
119
120 bool MathSymbolInset::isScriptable() const
121 {
122         return scriptable_;
123 }
124
125
126 bool MathSymbolInset::takesLimits() const
127 {
128         return
129                 sym_->inset == "cmex" ||
130                 sym_->inset == "lyxboldsymb" ||
131                 sym_->extra == "funclim";
132 }
133
134
135 void MathSymbolInset::validate(LaTeXFeatures & features) const
136 {
137         if (sym_->inset == "msa" || sym_->inset == "msb")
138                 features.require("amssymb");
139 }
140
141
142 void MathSymbolInset::normalize(NormalStream & os) const
143 {
144         os << "[symbol " << name() << ']';
145 }
146
147
148 void MathSymbolInset::maple(MapleStream & os) const
149 {
150         if (name() == "cdot")
151                 os << '*';
152         else if (name() == "infty")
153                 os << "infinity";
154         else
155                 os << name();
156 }
157
158 void MathSymbolInset::maxima(MaximaStream & os) const
159 {
160         if (name() == "cdot")
161                 os << '*';
162         else if (name() == "infty")
163                 os << "INF";
164         else
165                 os << name();
166 }
167
168
169 void MathSymbolInset::mathematica(MathematicaStream & os) const
170 {
171         if ( name() == "pi")    { os << "Pi"; return;}
172         if ( name() == "infty") { os << "Infinity"; return;}
173         os << name();
174 }
175
176
177 char const * MathMLtype(string const & s)
178 {
179         if (s == "mathop")
180                 return "mo";
181         return "mi";
182 }
183
184
185 bool MathSymbolInset::match(MathAtom const & at) const
186 {
187         MathSymbolInset const * q = at->asSymbolInset();
188         return q && name() == q->name();
189 }
190
191
192 void MathSymbolInset::mathmlize(MathMLStream & os) const
193 {
194         char const * type = MathMLtype(sym_->extra);
195         os << '<' << type << "> ";
196         if (sym_->xmlname == "x") // unknown so far
197                 os << name();
198         else
199                 os << sym_->xmlname;
200         os << " </" << type << '>';
201 }
202
203
204 void MathSymbolInset::octave(OctaveStream & os) const
205 {
206         if (name() == "cdot")
207                 os << '*';
208         else
209                 os << name();
210 }
211
212
213 void MathSymbolInset::write(WriteStream & os) const
214 {
215         os << '\\' << name();
216         os.pendingSpace(true);
217 }
218
219
220 void MathSymbolInset::infoize2(std::ostream & os) const
221 {
222         os << "Symbol: " << name();
223 }