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