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