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/lyxlib.h"
25 #include "support/textutils.h"
27 #include <boost/scoped_ptr.hpp>
32 InsetMathSymbol::InsetMathSymbol(latexkeys const * l)
33 : sym_(l), h_(0), kerning_(0), scriptable_(false)
37 InsetMathSymbol::InsetMathSymbol(char const * name)
38 : sym_(in_word_set(from_ascii(name))), h_(0),
39 kerning_(0), scriptable_(false)
43 InsetMathSymbol::InsetMathSymbol(docstring const & name)
44 : sym_(in_word_set(name)), h_(0), kerning_(0), scriptable_(false)
48 Inset * InsetMathSymbol::clone() const
50 return new InsetMathSymbol(*this);
54 docstring InsetMathSymbol::name() const
60 void InsetMathSymbol::metrics(MetricsInfo & mi, Dimension & dim) const
62 //lyxerr << "metrics: symbol: '" << sym_->name
63 // << "' in font: '" << sym_->inset
64 // << "' drawn as: '" << sym_->draw
67 bool const italic_upcase_greek = sym_->inset == "cmr" &&
68 sym_->extra == "mathalpha" &&
69 mi.base.fontname == "mathit";
70 std::string const font = italic_upcase_greek ? "cmm" : sym_->inset;
71 int const em = mathed_font_em(mi.base.font);
72 FontSetChanger dummy(mi.base, from_ascii(font));
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") {
82 // seperate things a bit
84 dim.wid += support::iround(0.5 * em);
86 dim.wid += support::iround(0.1667 * em);
89 if (mi.base.style == LM_ST_DISPLAY)
90 if (sym_->inset == "cmex" || sym_->inset == "esint" ||
91 sym_->extra == "funclim" ||
92 (sym_->inset == "stmry" && sym_->extra == "mathop"))
97 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
99 //lyxerr << "metrics: symbol: '" << sym_->name
100 // << "' in font: '" << sym_->inset
101 // << "' drawn as: '" << sym_->draw
104 bool const italic_upcase_greek = sym_->inset == "cmr" &&
105 sym_->extra == "mathalpha" &&
106 pi.base.fontname == "mathit";
107 std::string const font = italic_upcase_greek ? "cmm" : sym_->inset;
108 int const em = mathed_font_em(pi.base.font);
110 x += support::iround(0.25 * em);
112 x += support::iround(0.0833 * em);
114 FontSetChanger dummy(pi.base, from_ascii(font));
115 pi.draw(x, y - h_, sym_->draw);
119 InsetMath::mode_type InsetMathSymbol::currentMode() const
121 return sym_->extra == "textmode" ? TEXT_MODE : MATH_MODE;
125 bool InsetMathSymbol::isRelOp() const
127 return sym_->extra == "mathrel";
131 bool InsetMathSymbol::isOrdAlpha() const
133 return sym_->extra == "mathord" || sym_->extra == "mathalpha";
137 bool InsetMathSymbol::isScriptable() const
143 bool InsetMathSymbol::takesLimits() const
146 sym_->inset == "cmex" ||
147 sym_->inset == "lyxboldsymb" ||
148 sym_->inset == "esint" ||
149 sym_->extra == "funclim" ||
150 (sym_->inset == "stmry" && sym_->extra == "mathop");
154 void InsetMathSymbol::normalize(NormalStream & os) const
156 os << "[symbol " << name() << ']';
160 void InsetMathSymbol::maple(MapleStream & os) const
162 if (name() == "cdot")
164 else if (name() == "infty")
170 void InsetMathSymbol::maxima(MaximaStream & os) const
172 if (name() == "cdot")
174 else if (name() == "infty")
176 else if (name() == "pi")
183 void InsetMathSymbol::mathematica(MathematicaStream & os) const
185 if ( name() == "pi") { os << "Pi"; return;}
186 if ( name() == "infty") { os << "Infinity"; return;}
187 if ( name() == "cdot") { os << '*'; return;}
192 void InsetMathSymbol::mathmlize(MathStream & os) const
194 // FIXME We may need to do more interesting things
196 char const * type = sym_->MathMLtype();
197 os << '<' << type << "> ";
198 if (sym_->xmlname == "x")
203 os << " </" << type << '>';
207 void InsetMathSymbol::htmlize(HtmlStream & os, bool spacing) const
209 // FIXME We may need to do more interesting things
211 char const * type = sym_->MathMLtype();
212 bool op = (std::string(type) == "mo");
214 if (sym_->xmlname == "x")
216 os << ' ' << name() << ' ';
217 else if (op && spacing)
218 os << ' ' << sym_->xmlname << ' ';
224 void InsetMathSymbol::htmlize(HtmlStream & os) const
230 void InsetMathSymbol::octave(OctaveStream & os) const
232 if (name() == "cdot")
239 void InsetMathSymbol::write(WriteStream & os) const
241 boost::scoped_ptr<MathEnsurer> ensurer;
242 if (currentMode() != TEXT_MODE)
243 ensurer.reset(new MathEnsurer(os));
244 os << '\\' << name();
246 // $,#, etc. In theory the restriction based on catcodes, but then
247 // we do not handle catcodes very well, let alone cat code changes,
248 // so being outside the alpha range is good enough.
249 if (name().size() == 1 && !isAlphaASCII(name()[0]))
252 os.pendingSpace(true);
256 void InsetMathSymbol::infoize2(odocstream & os) const
258 os << from_ascii("Symbol: ") << name();
262 void InsetMathSymbol::validate(LaTeXFeatures & features) const
264 // this is not really the ideal place to do this, but we can't
265 // validate in InsetMathExInt.
266 if (features.runparams().math_flavor == OutputParams::MathAsHTML
267 && sym_->name == from_ascii("int")) {
268 features.addCSSSnippet(
269 "span.limits{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
270 "span.limits span{display: block;}\n"
271 "span.intsym{font-size: 150%;}\n"
272 "sub.limit{font-size: 75%;}\n"
273 "sup.limit{font-size: 75%;}");
275 if (!sym_->requires.empty())
276 features.require(sym_->requires);