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