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