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"
29 InsetMathSymbol::InsetMathSymbol(latexkeys const * l)
30 : sym_(l), h_(0), scriptable_(false)
34 InsetMathSymbol::InsetMathSymbol(char const * name)
35 : sym_(in_word_set(from_ascii(name))), h_(0), scriptable_(false)
39 InsetMathSymbol::InsetMathSymbol(docstring const & name)
40 : sym_(in_word_set(name)), h_(0), scriptable_(false)
44 Inset * InsetMathSymbol::clone() const
46 return new InsetMathSymbol(*this);
50 docstring InsetMathSymbol::name() const
56 void InsetMathSymbol::metrics(MetricsInfo & mi, Dimension & dim) const
58 //lyxerr << "metrics: symbol: '" << sym_->name
59 // << "' in font: '" << sym_->inset
60 // << "' drawn as: '" << sym_->draw
63 bool const italic_upcase_greek = sym_->inset == "cmr" &&
64 sym_->extra == "mathalpha" &&
65 mi.base.fontname == "mathit";
66 docstring const font = italic_upcase_greek ? from_ascii("cmm") : sym_->inset;
67 int const em = mathed_char_width(mi.base.font, 'M');
68 FontSetChanger dummy(mi.base, font);
69 mathed_string_dim(mi.base.font, sym_->draw, dim);
70 docstring::const_reverse_iterator rit = sym_->draw.rbegin();
71 kerning_ = mathed_char_kerning(mi.base.font, *rit);
72 // correct height for broken cmex and wasy font
73 if (sym_->inset == "cmex" || sym_->inset == "wasy") {
78 // seperate things a bit
80 dim.wid += static_cast<int>(0.5 * em + 0.5);
82 dim.wid += static_cast<int>(0.1667 * em + 0.5);
85 if (mi.base.style == LM_ST_DISPLAY)
86 if (sym_->inset == "cmex" || sym_->inset == "esint" ||
87 sym_->extra == "funclim")
92 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
94 //lyxerr << "metrics: symbol: '" << sym_->name
95 // << "' in font: '" << sym_->inset
96 // << "' drawn as: '" << sym_->draw
99 bool const italic_upcase_greek = sym_->inset == "cmr" &&
100 sym_->extra == "mathalpha" &&
101 pi.base.fontname == "mathit";
102 docstring const font = italic_upcase_greek ? from_ascii("cmm") : sym_->inset;
103 int const em = mathed_char_width(pi.base.font, 'M');
105 x += static_cast<int>(0.25*em+0.5);
107 x += static_cast<int>(0.0833*em+0.5);
109 FontSetChanger dummy(pi.base, font);
110 pi.draw(x, y - h_, sym_->draw);
114 bool InsetMathSymbol::isRelOp() const
116 return sym_->extra == "mathrel";
120 bool InsetMathSymbol::isOrdAlpha() const
122 return sym_->extra == "mathord" || sym_->extra == "mathalpha";
126 bool InsetMathSymbol::isScriptable() const
132 bool InsetMathSymbol::takesLimits() const
135 sym_->inset == "cmex" ||
136 sym_->inset == "lyxboldsymb" ||
137 sym_->inset == "esint" ||
138 sym_->extra == "funclim";
142 void InsetMathSymbol::normalize(NormalStream & os) const
144 os << "[symbol " << name() << ']';
148 void InsetMathSymbol::maple(MapleStream & os) const
150 if (name() == "cdot")
152 else if (name() == "infty")
158 void InsetMathSymbol::maxima(MaximaStream & os) const
160 if (name() == "cdot")
162 else if (name() == "infty")
164 else if (name() == "pi")
171 void InsetMathSymbol::mathematica(MathematicaStream & os) const
173 if ( name() == "pi") { os << "Pi"; return;}
174 if ( name() == "infty") { os << "Infinity"; return;}
175 if ( name() == "cdot") { os << '*'; return;}
180 // FIXME This will likely need some work.
181 char const * MathMLtype(docstring const & s)
189 void InsetMathSymbol::mathmlize(MathStream & os) const
191 // FIXME We may need to do more interesting things
193 char const * type = MathMLtype(sym_->extra);
194 os << '<' << type << "> ";
195 if (sym_->xmlname == "x")
200 os << " </" << type << '>';
204 void InsetMathSymbol::htmlize(HtmlStream & os, bool spacing) const
206 // FIXME We may need to do more interesting things
208 char const * type = MathMLtype(sym_->extra);
209 bool op = (std::string(type) == "mo");
211 if (sym_->xmlname == "x")
213 os << ' ' << name() << ' ';
214 else if (op && spacing)
215 os << ' ' << sym_->xmlname << ' ';
221 void InsetMathSymbol::htmlize(HtmlStream & os) const
227 void InsetMathSymbol::octave(OctaveStream & os) const
229 if (name() == "cdot")
236 void InsetMathSymbol::write(WriteStream & os) const
238 MathEnsurer ensurer(os);
239 os << '\\' << name();
241 // $,#, etc. In theory the restriction based on catcodes, but then
242 // we do not handle catcodes very well, let alone cat code changes,
243 // so being outside the alpha range is good enough.
244 if (name().size() == 1 && !isAlphaASCII(name()[0]))
247 os.pendingSpace(true);
251 void InsetMathSymbol::infoize2(odocstream & os) const
253 os << from_ascii("Symbol: ") << name();
257 void InsetMathSymbol::validate(LaTeXFeatures & features) const
259 // this is not really the ideal place to do this, but we can't
260 // validate in InsetMathExInt.
261 if (features.runparams().math_flavor == OutputParams::MathAsHTML
262 && sym_->name == from_ascii("int")) {
263 features.addCSSSnippet(
264 "span.limits{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
265 "span.limits span{display: block;}\n"
266 "span.intsym{font-size: 150%;}\n"
267 "sub.limit{font-size: 75%;}\n"
268 "sup.limit{font-size: 75%;}");
270 if (!sym_->requires.empty())
271 features.require(to_utf8(sym_->requires));