2 * \file InsetMathSymbol.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetMathSymbol.h"
15 #include "Dimension.h"
16 #include "LaTeXFeatures.h"
18 #include "MathParser.h"
19 #include "MathStream.h"
20 #include "MathSupport.h"
22 #include "support/debug.h"
23 #include "support/docstream.h"
24 #include "support/textutils.h"
26 #include <boost/scoped_ptr.hpp>
31 InsetMathSymbol::InsetMathSymbol(latexkeys const * l)
32 : sym_(l), h_(0), scriptable_(false)
36 InsetMathSymbol::InsetMathSymbol(char const * name)
37 : sym_(in_word_set(from_ascii(name))), h_(0), scriptable_(false)
41 InsetMathSymbol::InsetMathSymbol(docstring const & name)
42 : sym_(in_word_set(name)), h_(0), scriptable_(false)
46 Inset * InsetMathSymbol::clone() const
48 return new InsetMathSymbol(*this);
52 docstring InsetMathSymbol::name() const
58 void InsetMathSymbol::metrics(MetricsInfo & mi, Dimension & dim) const
60 //lyxerr << "metrics: symbol: '" << sym_->name
61 // << "' in font: '" << sym_->inset
62 // << "' drawn as: '" << sym_->draw
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") {
80 // seperate things a bit
82 dim.wid += static_cast<int>(0.5 * em + 0.5);
84 dim.wid += static_cast<int>(0.1667 * em + 0.5);
87 if (mi.base.style == LM_ST_DISPLAY)
88 if (sym_->inset == "cmex" || sym_->inset == "esint" ||
89 sym_->extra == "funclim")
94 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
96 //lyxerr << "metrics: symbol: '" << sym_->name
97 // << "' in font: '" << sym_->inset
98 // << "' drawn as: '" << sym_->draw
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');
107 x += static_cast<int>(0.25*em+0.5);
109 x += static_cast<int>(0.0833*em+0.5);
111 FontSetChanger dummy(pi.base, font);
112 pi.draw(x, y - h_, sym_->draw);
116 InsetMath::mode_type InsetMathSymbol::currentMode() const
118 return sym_->extra == "textmode" ? TEXT_MODE : MATH_MODE;
122 bool InsetMathSymbol::isRelOp() const
124 return sym_->extra == "mathrel";
128 bool InsetMathSymbol::isOrdAlpha() const
130 return sym_->extra == "mathord" || sym_->extra == "mathalpha";
134 bool InsetMathSymbol::isScriptable() const
140 bool InsetMathSymbol::takesLimits() const
143 sym_->inset == "cmex" ||
144 sym_->inset == "lyxboldsymb" ||
145 sym_->inset == "esint" ||
146 sym_->extra == "funclim";
150 void InsetMathSymbol::normalize(NormalStream & os) const
152 os << "[symbol " << name() << ']';
156 void InsetMathSymbol::maple(MapleStream & os) const
158 if (name() == "cdot")
160 else if (name() == "infty")
166 void InsetMathSymbol::maxima(MaximaStream & os) const
168 if (name() == "cdot")
170 else if (name() == "infty")
172 else if (name() == "pi")
179 void InsetMathSymbol::mathematica(MathematicaStream & os) const
181 if ( name() == "pi") { os << "Pi"; return;}
182 if ( name() == "infty") { os << "Infinity"; return;}
183 if ( name() == "cdot") { os << '*'; return;}
188 // FIXME This will likely need some work.
189 char const * MathMLtype(docstring const & s)
197 void InsetMathSymbol::mathmlize(MathStream & os) const
199 // FIXME We may need to do more interesting things
201 char const * type = MathMLtype(sym_->extra);
202 os << '<' << type << "> ";
203 if (sym_->xmlname == "x")
208 os << " </" << type << '>';
212 void InsetMathSymbol::htmlize(HtmlStream & os, bool spacing) const
214 // FIXME We may need to do more interesting things
216 char const * type = MathMLtype(sym_->extra);
217 bool op = (std::string(type) == "mo");
219 if (sym_->xmlname == "x")
221 os << ' ' << name() << ' ';
222 else if (op && spacing)
223 os << ' ' << sym_->xmlname << ' ';
229 void InsetMathSymbol::htmlize(HtmlStream & os) const
235 void InsetMathSymbol::octave(OctaveStream & os) const
237 if (name() == "cdot")
244 void InsetMathSymbol::write(WriteStream & os) const
246 boost::scoped_ptr<MathEnsurer> ensurer;
247 if (currentMode() != TEXT_MODE)
248 ensurer.reset(new MathEnsurer(os));
249 os << '\\' << name();
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]))
257 os.pendingSpace(true);
261 void InsetMathSymbol::infoize2(odocstream & os) const
263 os << from_ascii("Symbol: ") << name();
267 void InsetMathSymbol::validate(LaTeXFeatures & features) const
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%;}");
280 if (!sym_->requires.empty())
281 features.require(to_utf8(sym_->requires));