]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathSymbol.cpp
support for the LaTeX commands \x***arrow
[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         docstring const font = italic_upcase_greek ? from_ascii("cmm") : sym_->inset;
70         int const em = mathed_char_width(mi.base.font, 'M');
71         FontSetChanger dummy(mi.base, font);
72         mathed_string_dim(mi.base.font, sym_->draw, dim);
73         docstring::const_reverse_iterator rit = sym_->draw.rbegin();
74         kerning_ = mathed_char_kerning(mi.base.font, *rit);
75         // correct height for broken cmex and wasy font
76         if (sym_->inset == "cmex" || sym_->inset == "wasy") {
77                 h_ = 4 * dim.des / 5;
78                 dim.asc += h_;
79                 dim.des -= h_;
80         }
81         // seperate things a bit
82         if (isRelOp())
83                 dim.wid += static_cast<int>(0.5 * em + 0.5);
84         else
85                 dim.wid += static_cast<int>(0.1667 * em + 0.5);
86
87         scriptable_ = false;
88         if (mi.base.style == LM_ST_DISPLAY)
89                 if (sym_->inset == "cmex" || sym_->inset == "esint" ||
90                     sym_->extra == "funclim")
91                         scriptable_ = true;
92 }
93
94
95 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
96 {
97         //lyxerr << "metrics: symbol: '" << sym_->name
98         //      << "' in font: '" << sym_->inset
99         //      << "' drawn as: '" << sym_->draw
100         //      << "'" << endl;
101
102         bool const italic_upcase_greek = sym_->inset == "cmr" &&
103                                          sym_->extra == "mathalpha" &&
104                                          pi.base.fontname == "mathit";
105         docstring const font = italic_upcase_greek ? from_ascii("cmm") : sym_->inset;
106         int const em = mathed_char_width(pi.base.font, 'M');
107         if (isRelOp())
108                 x += static_cast<int>(0.25*em+0.5);
109         else
110                 x += static_cast<int>(0.0833*em+0.5);
111
112         FontSetChanger dummy(pi.base, font);
113         pi.draw(x, y - h_, sym_->draw);
114 }
115
116
117 InsetMath::mode_type InsetMathSymbol::currentMode() const
118 {
119         return sym_->extra == "textmode" ? TEXT_MODE : MATH_MODE;
120 }
121
122
123 bool InsetMathSymbol::isRelOp() const
124 {
125         return sym_->extra == "mathrel";
126 }
127
128
129 bool InsetMathSymbol::isOrdAlpha() const
130 {
131         return sym_->extra == "mathord" || sym_->extra == "mathalpha";
132 }
133
134
135 bool InsetMathSymbol::isScriptable() const
136 {
137         return scriptable_;
138 }
139
140
141 bool InsetMathSymbol::takesLimits() const
142 {
143         return
144                 sym_->inset == "cmex" ||
145                 sym_->inset == "lyxboldsymb" ||
146                 sym_->inset == "esint" ||
147                 sym_->extra == "funclim";
148 }
149
150
151 void InsetMathSymbol::normalize(NormalStream & os) const
152 {
153         os << "[symbol " << name() << ']';
154 }
155
156
157 void InsetMathSymbol::maple(MapleStream & os) const
158 {
159         if (name() == "cdot")
160                 os << '*';
161         else if (name() == "infty")
162                 os << "infinity";
163         else
164                 os << name();
165 }
166
167 void InsetMathSymbol::maxima(MaximaStream & os) const
168 {
169         if (name() == "cdot")
170                 os << '*';
171         else if (name() == "infty")
172                 os << "inf";
173         else if (name() == "pi")
174                 os << "%pi";
175         else
176                 os << name();
177 }
178
179
180 void InsetMathSymbol::mathematica(MathematicaStream & os) const
181 {
182         if ( name() == "pi")    { os << "Pi"; return;}
183         if ( name() == "infty") { os << "Infinity"; return;}
184         if ( name() == "cdot")  { os << '*'; return;}
185         os << name();
186 }
187
188
189 // FIXME This will likely need some work.
190 char const * MathMLtype(docstring const & s)
191 {
192         if (s == "mathord")
193                 return "mi";
194         return "mo";
195 }
196
197
198 void InsetMathSymbol::mathmlize(MathStream & os) const
199 {
200         // FIXME We may need to do more interesting things 
201         // with MathMLtype.
202         char const * type = MathMLtype(sym_->extra);
203         os << '<' << type << "> ";
204         if (sym_->xmlname == "x") 
205                 // unknown so far
206                 os << name();
207         else
208                 os << sym_->xmlname;
209         os << " </" << type << '>';
210 }
211
212
213 void InsetMathSymbol::htmlize(HtmlStream & os, bool spacing) const
214 {
215         // FIXME We may need to do more interesting things 
216         // with MathMLtype.
217         char const * type = MathMLtype(sym_->extra);
218         bool op = (std::string(type) == "mo");
219         
220         if (sym_->xmlname == "x") 
221                 // unknown so far
222                 os << ' ' << name() << ' ';
223         else if (op && spacing) 
224                 os << ' ' << sym_->xmlname << ' ';
225         else
226                 os << sym_->xmlname;
227 }
228
229
230 void InsetMathSymbol::htmlize(HtmlStream & os) const
231 {
232         htmlize(os, true);
233 }
234
235
236 void InsetMathSymbol::octave(OctaveStream & os) const
237 {
238         if (name() == "cdot")
239                 os << '*';
240         else
241                 os << name();
242 }
243
244
245 void InsetMathSymbol::write(WriteStream & os) const
246 {
247         boost::scoped_ptr<MathEnsurer> ensurer;
248         if (currentMode() != TEXT_MODE)
249                 ensurer.reset(new MathEnsurer(os));
250         os << '\\' << name();
251
252         // $,#, etc. In theory the restriction based on catcodes, but then
253         // we do not handle catcodes very well, let alone cat code changes,
254         // so being outside the alpha range is good enough.
255         if (name().size() == 1 && !isAlphaASCII(name()[0]))
256                 return;
257
258         os.pendingSpace(true);
259 }
260
261
262 void InsetMathSymbol::infoize2(odocstream & os) const
263 {
264         os << from_ascii("Symbol: ") << name();
265 }
266
267
268 void InsetMathSymbol::validate(LaTeXFeatures & features) const
269 {
270         // this is not really the ideal place to do this, but we can't
271         // validate in InsetMathExInt.
272         if (features.runparams().math_flavor == OutputParams::MathAsHTML
273             && sym_->name == from_ascii("int")) {
274                 features.addCSSSnippet(
275                         "span.limits{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
276                         "span.limits span{display: block;}\n"
277                         "span.intsym{font-size: 150%;}\n"
278                         "sub.limit{font-size: 75%;}\n"
279                         "sup.limit{font-size: 75%;}");
280         } else {
281                 if (!sym_->requires.empty())
282                         features.require(to_utf8(sym_->requires));
283         }
284 }
285
286 } // namespace lyx