]> git.lyx.org Git - lyx.git/blob - src/mathed/math_symbolinset.C
Few improvements.
[lyx.git] / src / mathed / math_symbolinset.C
1
2 #ifdef __GNUG__
3 #pragma implementation 
4 #endif
5
6 #include <config.h>
7
8 #include "math_symbolinset.h"
9 #include "math_mathmlstream.h"
10 #include "math_streamstr.h"
11 #include "math_support.h"
12 #include "math_parser.h"
13 #include "LaTeXFeatures.h"
14 #include "debug.h"
15
16 MathSymbolInset::MathSymbolInset(const latexkeys * l)
17         : sym_(l), h_(0)
18 {}
19
20
21 MathSymbolInset::MathSymbolInset(const char * name)
22         : sym_(in_word_set(name)), h_(0)
23 {}
24
25
26 MathSymbolInset::MathSymbolInset(string const & name)
27         : sym_(in_word_set(name.c_str())), h_(0)
28 {}
29
30
31
32 MathInset * MathSymbolInset::clone() const
33 {
34         return new MathSymbolInset(*this);
35 }
36
37
38 string MathSymbolInset::name() const
39 {
40         return sym_->name;
41 }
42
43
44 void MathSymbolInset::metrics(MathMetricsInfo & mi) const
45 {
46         //lyxerr << "metrics: symbol: '" << sym_->name
47         //      << "' in font: '" << sym_->inset
48         //      << "' drawn as: '" << sym_->draw
49         //      << "'\n";
50
51         int const em = mathed_char_width(mi.base.font, 'M');
52         MathFontSetChanger dummy(mi.base, sym_->inset.c_str());
53         mathed_string_dim(mi.base.font, sym_->draw, dim_);
54         // correct height for broken cmex and wasy font
55         if (sym_->inset == "cmex" || sym_->inset == "wasy") {
56                 h_ = 4 * dim_.d / 5;
57                 dim_.a += h_;
58                 dim_.d -= h_;
59         }
60         // seperate things a bit
61         if (isRelOp())
62                 dim_.w += static_cast<int>(0.5*em+0.5);
63         else
64                 dim_.w += static_cast<int>(0.1667*em+0.5);
65
66         scriptable_ = false;
67         if (mi.base.style == LM_ST_DISPLAY)
68                 if (sym_->inset == "cmex" || sym_->extra == "funclim")
69                         scriptable_ = true;
70 }
71
72
73 void MathSymbolInset::draw(MathPainterInfo & pi, int x, int y) const
74 {
75         //lyxerr << "metrics: symbol: '" << sym_->name
76         //      << "' in font: '" << sym_->inset
77         //      << "' drawn as: '" << sym_->draw
78         //      << "'\n";
79         int const em = mathed_char_width(pi.base.font, 'M');
80         if (isRelOp())
81                 x += static_cast<int>(0.25*em+0.5);
82         else
83                 x += static_cast<int>(0.0833*em+0.5);
84
85         MathFontSetChanger dummy(pi.base, sym_->inset.c_str());
86         drawStr(pi, pi.base.font, x, y - h_, sym_->draw);
87 }
88
89
90 bool MathSymbolInset::isRelOp() const
91 {
92         return sym_->extra == "mathrel";
93 }
94
95
96 bool MathSymbolInset::isScriptable() const
97 {
98         return scriptable_;
99 }
100
101
102 bool MathSymbolInset::takesLimits() const
103 {
104         return
105                 sym_->inset == "cmex" ||
106                 sym_->inset == "lyxboldsymb" ||
107                 sym_->extra == "funclim";
108 }
109
110
111 void MathSymbolInset::validate(LaTeXFeatures & features) const
112 {
113         if (sym_->inset == "msa" || sym_->inset == "msb")
114                 features.require("amssymb");
115 }
116
117
118 void MathSymbolInset::normalize(NormalStream & os) const
119 {
120         os << "[symbol " << name() << "]";
121 }
122
123
124 void MathSymbolInset::maplize(MapleStream & os) const
125 {
126         if (name() == "cdot")
127                 os << '*';
128         else if (name() == "infty")
129                 os << "infinity";
130         else
131                 os << name();
132 }
133
134 void MathSymbolInset::mathematicize(MathematicaStream & os) const
135 {
136         if ( name() == "pi")    { os << "Pi"; return;}
137         if ( name() == "infty") { os << "Infinity"; return;}
138         os << name();
139 }
140
141
142 char const * MathMLtype(string const & s)
143 {
144         if (s == "mathop")
145                 return "mo";
146         return "mi";
147 }
148
149
150 bool MathSymbolInset::match(MathAtom const & at) const
151 {
152         MathSymbolInset const * q = at->asSymbolInset();
153         return q && name() == q->name();
154 }
155
156
157 void MathSymbolInset::mathmlize(MathMLStream & os) const
158 {
159         char const * type = MathMLtype(sym_->extra);
160         os << '<' << type << "> ";
161         if (sym_->xmlname == "x") // unknown so far
162                 os << name();
163         else
164                 os << sym_->xmlname;
165         os << " </" << type << '>';
166 }
167
168
169 void MathSymbolInset::octavize(OctaveStream & os) const
170 {
171         if (name() == "cdot")
172                 os << '*';
173         else
174                 os << name();
175 }
176
177
178 void MathSymbolInset::write(WriteStream & os) const
179 {
180         os << '\\' << name();
181         os.pendingSpace(true);
182 }
183
184
185 void MathSymbolInset::infoize(std::ostream & os) const
186 {
187         os << "Symbol: " << name();
188 }