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