]> git.lyx.org Git - lyx.git/blob - src/mathed/math_symbolinset.C
Better alignment of \longrightarrow like macros.
[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         MathFontSetChanger dummy(mi.base, sym_->inset.c_str());
52         mathed_string_dim(mi.base.font, sym_->draw, dim_);
53         // correct height for broken cmex and wasy font
54         if (sym_->inset == "cmex" || sym_->inset == "wasy") {
55                 h_ = 4 * dim_.d / 5;
56                 dim_.a += h_;
57                 dim_.d -= h_;
58         }
59         // seperate things a bit
60         int em = mathed_char_width(mi.base.font, 'M');
61         if (name() == "not")
62                 // \not is a special case. 
63                 // It must have 0 width to align properly with the next symbol.
64                 dim_.w = 0;
65         else if (isRelOp())
66                 dim_.w += static_cast<int>(0.5*em+0.5);
67         else
68                 dim_.w += static_cast<int>(0.15*em+0.5);
69
70         scriptable_ = false;
71         if (mi.base.style == LM_ST_DISPLAY)
72                 if (sym_->inset == "cmex" || sym_->extra == "funclim")
73                         scriptable_ = true;
74 }
75
76
77 void MathSymbolInset::draw(MathPainterInfo & pi, int x, int y) const
78 {
79         //lyxerr << "metrics: symbol: '" << sym_->name
80         //      << "' in font: '" << sym_->inset
81         //      << "' drawn as: '" << sym_->draw
82         //      << "'\n";
83         int em = mathed_char_width(pi.base.font, 'M');
84         // Here we don't need a special case for \not, as it needs the same
85         // increase in x as the next symbol.
86         if (isRelOp())
87                 x += static_cast<int>(0.25*em+0.5);
88         else
89                 x += static_cast<int>(0.075*em+0.5);
90
91         MathFontSetChanger dummy(pi.base, sym_->inset.c_str());
92         drawStr(pi, pi.base.font, x, y - h_, sym_->draw);
93 }
94
95
96 bool MathSymbolInset::isRelOp() const
97 {
98         return sym_->extra == "mathrel";
99 }
100
101
102 bool MathSymbolInset::isScriptable() const
103 {
104         return scriptable_;
105 }
106
107
108 bool MathSymbolInset::takesLimits() const
109 {
110         return
111                 sym_->inset == "cmex" ||
112                 sym_->inset == "lyxboldsymb" ||
113                 sym_->extra == "funclim";
114 }
115
116
117 void MathSymbolInset::validate(LaTeXFeatures & features) const
118 {
119         if (sym_->inset == "msa" || sym_->inset == "msb")
120                 features.require("amssymb");
121 }
122
123
124 void MathSymbolInset::normalize(NormalStream & os) const
125 {
126         os << "[symbol " << name() << "]";
127 }
128
129
130 void MathSymbolInset::maplize(MapleStream & os) const
131 {
132         if (name() == "cdot")
133                 os << '*';
134         else if (name() == "infty")
135                 os << "infinity";
136         else
137                 os << name();
138 }
139
140 void MathSymbolInset::mathematicize(MathematicaStream & os) const
141 {
142         if ( name() == "pi")    { os << "Pi"; return;}
143         if ( name() == "infty") { os << "Infinity"; return;}
144         os << name();
145 }
146
147
148 char const * MathMLtype(string const & s)
149 {
150         if (s == "mathop")
151                 return "mo";
152         return "mi";
153 }
154
155
156 bool MathSymbolInset::match(MathAtom const & at) const
157 {
158         MathSymbolInset const * q = at->asSymbolInset();
159         return q && name() == q->name();
160 }
161
162
163 void MathSymbolInset::mathmlize(MathMLStream & os) const
164 {
165         char const * type = MathMLtype(sym_->extra);
166         os << '<' << type << "> ";
167         if (sym_->xmlname == "x") // unknown so far
168                 os << name();
169         else
170                 os << sym_->xmlname;
171         os << " </" << type << '>';
172 }
173
174
175 void MathSymbolInset::octavize(OctaveStream & os) const
176 {
177         if (name() == "cdot")
178                 os << '*';
179         else
180                 os << name();
181 }
182
183
184 void MathSymbolInset::write(WriteStream & os) const
185 {
186         os << '\\' << name();
187         os.pendingSpace(true);
188 }
189
190
191 void MathSymbolInset::infoize(std::ostream & os) const
192 {
193         os << "Symbol: " << name();
194 }