]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathSymbol.C
Almost fix 'make check'. The only remaining problem is an undefined
[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 bool 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         if (dim_ == dim)
88                 return false;
89         dim_ = dim;
90         return true;
91 }
92
93
94 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
95 {
96         //lyxerr << "metrics: symbol: '" << sym_->name
97         //      << "' in font: '" << sym_->inset
98         //      << "' drawn as: '" << sym_->draw
99         //      << "'" << std::endl;
100         int const em = mathed_char_width(pi.base.font, 'M');
101         if (isRelOp())
102                 x += static_cast<int>(0.25*em+0.5);
103         else
104                 x += static_cast<int>(0.0833*em+0.5);
105
106         FontSetChanger dummy(pi.base, sym_->inset.c_str());
107         pi.draw(x, y - h_, sym_->draw);
108 }
109
110
111 bool InsetMathSymbol::isRelOp() const
112 {
113         return sym_->extra == "mathrel";
114 }
115
116
117 bool InsetMathSymbol::isScriptable() const
118 {
119         return scriptable_;
120 }
121
122
123 bool InsetMathSymbol::takesLimits() const
124 {
125         return
126                 sym_->inset == "cmex" ||
127                 sym_->inset == "lyxboldsymb" ||
128                 sym_->inset == "esint" ||
129                 sym_->extra == "funclim";
130 }
131
132
133 void InsetMathSymbol::validate(LaTeXFeatures & features) const
134 {
135         if (!sym_->requires.empty())
136                 features.require(to_utf8(sym_->requires));
137 }
138
139
140 void InsetMathSymbol::normalize(NormalStream & os) const
141 {
142         os << "[symbol " << name() << ']';
143 }
144
145
146 void InsetMathSymbol::maple(MapleStream & os) const
147 {
148         if (name() == "cdot")
149                 os << '*';
150         else if (name() == "infty")
151                 os << "infinity";
152         else
153                 os << name();
154 }
155
156 void InsetMathSymbol::maxima(MaximaStream & os) const
157 {
158         if (name() == "cdot")
159                 os << '*';
160         else if (name() == "infty")
161                 os << "inf";
162         else if (name() == "pi")
163                 os << "%pi";
164         else
165                 os << name();
166 }
167
168
169 void InsetMathSymbol::mathematica(MathematicaStream & os) const
170 {
171         if ( name() == "pi")    { os << "Pi"; return;}
172         if ( name() == "infty") { os << "Infinity"; return;}
173         if ( name() == "cdot")  { os << '*'; return;}
174         os << name();
175 }
176
177
178 char const * MathMLtype(docstring const & s)
179 {
180         if (s == "mathop")
181                 return "mo";
182         return "mi";
183 }
184
185
186 void InsetMathSymbol::mathmlize(MathStream & os) const
187 {
188         char const * type = MathMLtype(sym_->extra);
189         os << '<' << type << "> ";
190         if (sym_->xmlname == "x") // unknown so far
191                 os << name();
192         else
193                 os << sym_->xmlname;
194         os << " </" << type << '>';
195 }
196
197
198 void InsetMathSymbol::octave(OctaveStream & os) const
199 {
200         if (name() == "cdot")
201                 os << '*';
202         else
203                 os << name();
204 }
205
206
207 void InsetMathSymbol::write(WriteStream & os) const
208 {
209         os << '\\' << name();
210         os.pendingSpace(true);
211 }
212
213
214 void InsetMathSymbol::infoize2(odocstream & os) const
215 {
216         os << "Symbol: " << name();
217 }
218
219
220 } // namespace lyx