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