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