]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathSymbol.cpp
a04f6e0bcf7bed47bd99dd7593ed81854d00591f
[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
15 #include "Dimension.h"
16 #include "LaTeXFeatures.h"
17 #include "MathAtom.h"
18 #include "MathParser.h"
19 #include "MathStream.h"
20 #include "MathSupport.h"
21
22 #include "support/debug.h"
23 #include "support/docstream.h"
24 #include "support/textutils.h"
25
26 #include <boost/scoped_ptr.hpp>
27
28
29 namespace lyx {
30
31 InsetMathSymbol::InsetMathSymbol(latexkeys const * l)
32         : sym_(l), h_(0), kerning_(0), scriptable_(false)
33 {}
34
35
36 InsetMathSymbol::InsetMathSymbol(char const * name)
37         : sym_(in_word_set(from_ascii(name))), h_(0),
38           kerning_(0), scriptable_(false)
39 {}
40
41
42 InsetMathSymbol::InsetMathSymbol(docstring const & name)
43         : sym_(in_word_set(name)), h_(0), kerning_(0), scriptable_(false)
44 {}
45
46
47 Inset * InsetMathSymbol::clone() const
48 {
49         return new InsetMathSymbol(*this);
50 }
51
52
53 docstring InsetMathSymbol::name() const
54 {
55         return sym_->name;
56 }
57
58
59 void 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         //      << "'" << endl;
65
66         bool const italic_upcase_greek = sym_->inset == "cmr" &&
67                                          sym_->extra == "mathalpha" &&
68                                          mi.base.fontname == "mathit";
69         std::string const font = italic_upcase_greek ? "cmm" : sym_->inset;
70         FontSetChanger dummy(mi.base, from_ascii(font));
71         mathed_string_dim(mi.base.font, sym_->draw, dim);
72         docstring::const_reverse_iterator rit = sym_->draw.rbegin();
73         kerning_ = mathed_char_kerning(mi.base.font, *rit);
74         // correct height for broken cmex and wasy font
75         if (sym_->inset == "cmex" || sym_->inset == "wasy") {
76                 h_ = 4 * dim.des / 5;
77                 dim.asc += h_;
78                 dim.des -= h_;
79         }
80         // seperate things a bit
81         if (isMathBin())
82                 dim.wid += 2 * mathed_medmuskip(mi.base.font);
83         else if (isMathRel())
84                 dim.wid += 2 * mathed_thickmuskip(mi.base.font);
85         else if (isMathPunct())
86                 dim.wid += mathed_thinmuskip(mi.base.font);
87         // FIXME: I see no reason for this
88         //else
89         //      dim.wid += support::iround(0.1667 * em);
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                     (sym_->inset == "stmry" && sym_->extra == "mathop"))
96                         scriptable_ = true;
97 }
98
99
100 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
101 {
102         //lyxerr << "metrics: symbol: '" << sym_->name
103         //      << "' in font: '" << sym_->inset
104         //      << "' drawn as: '" << sym_->draw
105         //      << "'" << endl;
106
107         bool const italic_upcase_greek = sym_->inset == "cmr" &&
108                                          sym_->extra == "mathalpha" &&
109                                          pi.base.fontname == "mathit";
110         std::string const font = italic_upcase_greek ? "cmm" : sym_->inset;
111         if (isMathBin())
112                 x += mathed_medmuskip(pi.base.font);
113         else if (isMathRel())
114                 x += mathed_thickmuskip(pi.base.font);
115         // FIXME: I see no reason for this
116         //else
117         //      x += support::iround(0.0833 * em);
118
119         FontSetChanger dummy(pi.base, from_ascii(font));
120         pi.draw(x, y - h_, sym_->draw);
121 }
122
123
124 InsetMath::mode_type InsetMathSymbol::currentMode() const
125 {
126         return sym_->extra == "textmode" ? TEXT_MODE : MATH_MODE;
127 }
128
129
130 bool InsetMathSymbol::isMathBin() const
131 {
132         return sym_->extra == "mathbin";
133 }
134
135
136 bool InsetMathSymbol::isMathRel() const
137 {
138         return sym_->extra == "mathrel";
139 }
140
141
142 bool InsetMathSymbol::isMathPunct() const
143 {
144         return sym_->extra == "mathpunct";
145 }
146
147
148 bool InsetMathSymbol::isOrdAlpha() const
149 {
150         return sym_->extra == "mathord" || sym_->extra == "mathalpha";
151 }
152
153
154 bool InsetMathSymbol::isScriptable() const
155 {
156         return scriptable_;
157 }
158
159
160 bool InsetMathSymbol::takesLimits() const
161 {
162         return
163                 sym_->inset == "cmex" ||
164                 sym_->inset == "lyxboldsymb" ||
165                 sym_->inset == "esint" ||
166                 sym_->extra == "funclim" ||
167                 (sym_->inset == "stmry" && sym_->extra == "mathop");
168 }
169
170
171 void InsetMathSymbol::normalize(NormalStream & os) const
172 {
173         os << "[symbol " << name() << ']';
174 }
175
176
177 void InsetMathSymbol::maple(MapleStream & os) const
178 {
179         if (name() == "cdot")
180                 os << '*';
181         else if (name() == "infty")
182                 os << "infinity";
183         else
184                 os << name();
185 }
186
187 void InsetMathSymbol::maxima(MaximaStream & os) const
188 {
189         if (name() == "cdot")
190                 os << '*';
191         else if (name() == "infty")
192                 os << "inf";
193         else if (name() == "pi")
194                 os << "%pi";
195         else
196                 os << name();
197 }
198
199
200 void InsetMathSymbol::mathematica(MathematicaStream & os) const
201 {
202         if ( name() == "pi")    { os << "Pi"; return;}
203         if ( name() == "infty") { os << "Infinity"; return;}
204         if ( name() == "cdot")  { os << '*'; return;}
205         os << name();
206 }
207
208
209 void InsetMathSymbol::mathmlize(MathStream & os) const
210 {
211         // FIXME We may need to do more interesting things
212         // with MathMLtype.
213         char const * type = sym_->MathMLtype();
214         os << '<' << type << "> ";
215         if (sym_->xmlname == "x")
216                 // unknown so far
217                 os << name();
218         else
219                 os << sym_->xmlname;
220         os << " </" << type << '>';
221 }
222
223
224 void InsetMathSymbol::htmlize(HtmlStream & os, bool spacing) const
225 {
226         // FIXME We may need to do more interesting things
227         // with MathMLtype.
228         char const * type = sym_->MathMLtype();
229         bool op = (std::string(type) == "mo");
230
231         if (sym_->xmlname == "x")
232                 // unknown so far
233                 os << ' ' << name() << ' ';
234         else if (op && spacing)
235                 os << ' ' << sym_->xmlname << ' ';
236         else
237                 os << sym_->xmlname;
238 }
239
240
241 void InsetMathSymbol::htmlize(HtmlStream & os) const
242 {
243         htmlize(os, true);
244 }
245
246
247 void InsetMathSymbol::octave(OctaveStream & os) const
248 {
249         if (name() == "cdot")
250                 os << '*';
251         else
252                 os << name();
253 }
254
255
256 void InsetMathSymbol::write(WriteStream & os) const
257 {
258         boost::scoped_ptr<MathEnsurer> ensurer;
259         if (currentMode() != TEXT_MODE)
260                 ensurer.reset(new MathEnsurer(os));
261         os << '\\' << name();
262
263         // $,#, etc. In theory the restriction based on catcodes, but then
264         // we do not handle catcodes very well, let alone cat code changes,
265         // so being outside the alpha range is good enough.
266         if (name().size() == 1 && !isAlphaASCII(name()[0]))
267                 return;
268
269         os.pendingSpace(true);
270 }
271
272
273 void InsetMathSymbol::infoize2(odocstream & os) const
274 {
275         os << from_ascii("Symbol: ") << name();
276 }
277
278
279 void InsetMathSymbol::validate(LaTeXFeatures & features) const
280 {
281         // this is not really the ideal place to do this, but we can't
282         // validate in InsetMathExInt.
283         if (features.runparams().math_flavor == OutputParams::MathAsHTML
284             && sym_->name == from_ascii("int")) {
285                 features.addCSSSnippet(
286                         "span.limits{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
287                         "span.limits span{display: block;}\n"
288                         "span.intsym{font-size: 150%;}\n"
289                         "sub.limit{font-size: 75%;}\n"
290                         "sup.limit{font-size: 75%;}");
291         } else {
292                 if (!sym_->requires.empty())
293                         features.require(sym_->requires);
294         }
295 }
296
297 } // namespace lyx