]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathSymbol.cpp
28d20bad079afaccbedf751a1328d241c23e58a9
[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 bool 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         if (dim_ == dim)
87                 return false;
88
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::isOrdAlpha() const
118 {
119         return sym_->extra == "mathord" || sym_->extra == "mathalpha";
120 }
121
122
123 bool InsetMathSymbol::isScriptable() const
124 {
125         return scriptable_;
126 }
127
128
129 bool InsetMathSymbol::takesLimits() const
130 {
131         return
132                 sym_->inset == "cmex" ||
133                 sym_->inset == "lyxboldsymb" ||
134                 sym_->inset == "esint" ||
135                 sym_->extra == "funclim";
136 }
137
138
139 void InsetMathSymbol::validate(LaTeXFeatures & features) const
140 {
141         if (!sym_->requires.empty())
142                 features.require(to_utf8(sym_->requires));
143 }
144
145
146 void InsetMathSymbol::normalize(NormalStream & os) const
147 {
148         os << "[symbol " << name() << ']';
149 }
150
151
152 void InsetMathSymbol::maple(MapleStream & os) const
153 {
154         if (name() == "cdot")
155                 os << '*';
156         else if (name() == "infty")
157                 os << "infinity";
158         else
159                 os << name();
160 }
161
162 void InsetMathSymbol::maxima(MaximaStream & os) const
163 {
164         if (name() == "cdot")
165                 os << '*';
166         else if (name() == "infty")
167                 os << "inf";
168         else if (name() == "pi")
169                 os << "%pi";
170         else
171                 os << name();
172 }
173
174
175 void InsetMathSymbol::mathematica(MathematicaStream & os) const
176 {
177         if ( name() == "pi")    { os << "Pi"; return;}
178         if ( name() == "infty") { os << "Infinity"; return;}
179         if ( name() == "cdot")  { os << '*'; return;}
180         os << name();
181 }
182
183
184 char const * MathMLtype(docstring const & s)
185 {
186         if (s == "mathop")
187                 return "mo";
188         return "mi";
189 }
190
191
192 void InsetMathSymbol::mathmlize(MathStream & os) const
193 {
194         char const * type = MathMLtype(sym_->extra);
195         os << '<' << type << "> ";
196         if (sym_->xmlname == "x") // unknown so far
197                 os << name();
198         else
199                 os << sym_->xmlname;
200         os << " </" << type << '>';
201 }
202
203
204 void InsetMathSymbol::octave(OctaveStream & os) const
205 {
206         if (name() == "cdot")
207                 os << '*';
208         else
209                 os << name();
210 }
211
212
213 void InsetMathSymbol::write(WriteStream & os) const
214 {
215         os << '\\' << name();
216
217         // $,#, etc. In theory the restriction based on catcodes, but then
218         // we do not handle catcodes very well, let alone cat code changes,
219         // so being outside the alpha range is good enough.
220         if (name().size() == 1 && !isAlphaASCII(name()[0]))
221                 return;
222
223         os.pendingSpace(true);
224 }
225
226
227 void InsetMathSymbol::infoize2(odocstream & os) const
228 {
229         os << "Symbol: " << name();
230 }
231
232
233 } // namespace lyx