]> git.lyx.org Git - lyx.git/blob - src/mathed/math_symbolinset.C
Make Helge happy: no more crash on arrow up/down in math macro
[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::doClone() 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         width_ = dim.wid;
96 }
97
98
99 void MathSymbolInset::draw(PainterInfo & pi, int x, int y) const
100 {
101         //lyxerr << "metrics: symbol: '" << sym_->name
102         //      << "' in font: '" << sym_->inset
103         //      << "' drawn as: '" << sym_->draw
104         //      << "'" << std::endl;
105         int const em = mathed_char_width(pi.base.font, 'M');
106         if (isRelOp())
107                 x += static_cast<int>(0.25*em+0.5);
108         else
109                 x += static_cast<int>(0.0833*em+0.5);
110
111         FontSetChanger dummy(pi.base, sym_->inset.c_str());
112         drawStr(pi, pi.base.font, x, y - h_, sym_->draw);
113 }
114
115
116 bool MathSymbolInset::isRelOp() const
117 {
118         return sym_->extra == "mathrel";
119 }
120
121
122 bool MathSymbolInset::isScriptable() const
123 {
124         return scriptable_;
125 }
126
127
128 bool MathSymbolInset::takesLimits() const
129 {
130         return
131                 sym_->inset == "cmex" ||
132                 sym_->inset == "lyxboldsymb" ||
133                 sym_->extra == "funclim";
134 }
135
136
137 void MathSymbolInset::validate(LaTeXFeatures & features) const
138 {
139         if (sym_->inset == "msa" || sym_->inset == "msb")
140                 features.require("amssymb");
141         else if (sym_->inset == "wasy")
142                 features.require("wasysym");
143 }
144
145
146 void MathSymbolInset::normalize(NormalStream & os) const
147 {
148         os << "[symbol " << name() << ']';
149 }
150
151
152 void MathSymbolInset::maple(MapleStream & os) const
153 {
154         if (name() == "cdot")
155                 os << '*';
156         else if (name() == "infty")
157                 os << "infinity";
158         else
159                 os << name();
160 }
161
162 void MathSymbolInset::maxima(MaximaStream & os) const
163 {
164         if (name() == "cdot")
165                 os << '*';
166         else if (name() == "infty")
167                 os << "INF";
168         else
169                 os << name();
170 }
171
172
173 void MathSymbolInset::mathematica(MathematicaStream & os) const
174 {
175         if ( name() == "pi")    { os << "Pi"; return;}
176         if ( name() == "infty") { os << "Infinity"; return;}
177         os << name();
178 }
179
180
181 char const * MathMLtype(string const & s)
182 {
183         if (s == "mathop")
184                 return "mo";
185         return "mi";
186 }
187
188
189 void MathSymbolInset::mathmlize(MathMLStream & os) const
190 {
191         char const * type = MathMLtype(sym_->extra);
192         os << '<' << type << "> ";
193         if (sym_->xmlname == "x") // unknown so far
194                 os << name();
195         else
196                 os << sym_->xmlname;
197         os << " </" << type << '>';
198 }
199
200
201 void MathSymbolInset::octave(OctaveStream & os) const
202 {
203         if (name() == "cdot")
204                 os << '*';
205         else
206                 os << name();
207 }
208
209
210 void MathSymbolInset::write(WriteStream & os) const
211 {
212         os << '\\' << name();
213         os.pendingSpace(true);
214 }
215
216
217 void MathSymbolInset::infoize2(std::ostream & os) const
218 {
219         os << "Symbol: " << name();
220 }