]> git.lyx.org Git - lyx.git/blob - src/mathed/math_symbolinset.C
Standardise the header blurb in mathed.
[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 (sym_->inset == "cmex" || sym_->inset == "wasy") {
65                 h_ = 4 * dim.des / 5;
66                 dim.asc += h_;
67                 dim.des -= h_;
68         }
69         // seperate things a bit
70         if (isRelOp())
71                 dim.wid += static_cast<int>(0.5 * em + 0.5);
72         else
73                 dim.wid += static_cast<int>(0.1667 * em + 0.5);
74
75         scriptable_ = false;
76         if (mi.base.style == LM_ST_DISPLAY)
77                 if (sym_->inset == "cmex" || sym_->extra == "funclim")
78                         scriptable_ = true;
79 }
80
81
82 void MathSymbolInset::draw(PainterInfo & pi, int x, int y) const
83 {
84         //lyxerr << "metrics: symbol: '" << sym_->name
85         //      << "' in font: '" << sym_->inset
86         //      << "' drawn as: '" << sym_->draw
87         //      << "'\n";
88         int const em = mathed_char_width(pi.base.font, 'M');
89         if (isRelOp())
90                 x += static_cast<int>(0.25*em+0.5);
91         else
92                 x += static_cast<int>(0.0833*em+0.5);
93
94         FontSetChanger dummy(pi.base, sym_->inset.c_str());
95         drawStr(pi, pi.base.font, x, y - h_, sym_->draw);
96 }
97
98
99 bool MathSymbolInset::isRelOp() const
100 {
101         return sym_->extra == "mathrel";
102 }
103
104
105 bool MathSymbolInset::isScriptable() const
106 {
107         return scriptable_;
108 }
109
110
111 bool MathSymbolInset::takesLimits() const
112 {
113         return
114                 sym_->inset == "cmex" ||
115                 sym_->inset == "lyxboldsymb" ||
116                 sym_->extra == "funclim";
117 }
118
119
120 void MathSymbolInset::validate(LaTeXFeatures & features) const
121 {
122         if (sym_->inset == "msa" || sym_->inset == "msb")
123                 features.require("amssymb");
124 }
125
126
127 void MathSymbolInset::normalize(NormalStream & os) const
128 {
129         os << "[symbol " << name() << ']';
130 }
131
132
133 void MathSymbolInset::maple(MapleStream & os) const
134 {
135         if (name() == "cdot")
136                 os << '*';
137         else if (name() == "infty")
138                 os << "infinity";
139         else
140                 os << name();
141 }
142
143 void MathSymbolInset::maxima(MaximaStream & os) const
144 {
145         if (name() == "cdot")
146                 os << '*';
147         else if (name() == "infty")
148                 os << "INF";
149         else
150                 os << name();
151 }
152
153
154 void MathSymbolInset::mathematica(MathematicaStream & os) const
155 {
156         if ( name() == "pi")    { os << "Pi"; return;}
157         if ( name() == "infty") { os << "Infinity"; return;}
158         os << name();
159 }
160
161
162 char const * MathMLtype(string const & s)
163 {
164         if (s == "mathop")
165                 return "mo";
166         return "mi";
167 }
168
169
170 bool MathSymbolInset::match(MathAtom const & at) const
171 {
172         MathSymbolInset const * q = at->asSymbolInset();
173         return q && name() == q->name();
174 }
175
176
177 void MathSymbolInset::mathmlize(MathMLStream & os) const
178 {
179         char const * type = MathMLtype(sym_->extra);
180         os << '<' << type << "> ";
181         if (sym_->xmlname == "x") // unknown so far
182                 os << name();
183         else
184                 os << sym_->xmlname;
185         os << " </" << type << '>';
186 }
187
188
189 void MathSymbolInset::octave(OctaveStream & os) const
190 {
191         if (name() == "cdot")
192                 os << '*';
193         else
194                 os << name();
195 }
196
197
198 void MathSymbolInset::write(WriteStream & os) const
199 {
200         os << '\\' << name();
201         os.pendingSpace(true);
202 }
203
204
205 void MathSymbolInset::infoize2(std::ostream & os) const
206 {
207         os << "Symbol: " << name();
208 }