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
61 // << "'" << std::endl;
63 int const em = mathed_char_width(mi.base.font, 'M');
64 FontSetChanger dummy(mi.base, sym_->inset);
65 mathed_string_dim(mi.base.font, sym_->draw, dim);
66 docstring::const_reverse_iterator rit = sym_->draw.rbegin();
67 kerning_ = mathed_char_kerning(mi.base.font, *rit);
68 // correct height for broken cmex and wasy font
69 if (sym_->inset == "cmex" || sym_->inset == "wasy") {
74 // seperate things a bit
76 dim.wid += static_cast<int>(0.5 * em + 0.5);
78 dim.wid += static_cast<int>(0.1667 * em + 0.5);
81 if (mi.base.style == LM_ST_DISPLAY)
82 if (sym_->inset == "cmex" || sym_->inset == "esint" ||
83 sym_->extra == "funclim")
86 // Cache the inset dimension.
91 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
93 //lyxerr << "metrics: symbol: '" << sym_->name
94 // << "' in font: '" << sym_->inset
95 // << "' drawn as: '" << sym_->draw
96 // << "'" << std::endl;
97 int const em = mathed_char_width(pi.base.font, 'M');
99 x += static_cast<int>(0.25*em+0.5);
101 x += static_cast<int>(0.0833*em+0.5);
103 FontSetChanger dummy(pi.base, sym_->inset.c_str());
104 pi.draw(x, y - h_, sym_->draw);
108 bool InsetMathSymbol::isRelOp() const
110 return sym_->extra == "mathrel";
114 bool InsetMathSymbol::isOrdAlpha() const
116 return sym_->extra == "mathord" || sym_->extra == "mathalpha";
120 bool InsetMathSymbol::isScriptable() const
126 bool InsetMathSymbol::takesLimits() const
129 sym_->inset == "cmex" ||
130 sym_->inset == "lyxboldsymb" ||
131 sym_->inset == "esint" ||
132 sym_->extra == "funclim";
136 void InsetMathSymbol::validate(LaTeXFeatures & features) const
138 if (!sym_->requires.empty())
139 features.require(to_utf8(sym_->requires));
143 void InsetMathSymbol::normalize(NormalStream & os) const
145 os << "[symbol " << name() << ']';
149 void InsetMathSymbol::maple(MapleStream & os) const
151 if (name() == "cdot")
153 else if (name() == "infty")
159 void InsetMathSymbol::maxima(MaximaStream & os) const
161 if (name() == "cdot")
163 else if (name() == "infty")
165 else if (name() == "pi")
172 void InsetMathSymbol::mathematica(MathematicaStream & os) const
174 if ( name() == "pi") { os << "Pi"; return;}
175 if ( name() == "infty") { os << "Infinity"; return;}
176 if ( name() == "cdot") { os << '*'; return;}
181 char const * MathMLtype(docstring const & s)
189 void InsetMathSymbol::mathmlize(MathStream & os) const
191 char const * type = MathMLtype(sym_->extra);
192 os << '<' << type << "> ";
193 if (sym_->xmlname == "x") // unknown so far
197 os << " </" << type << '>';
201 void InsetMathSymbol::octave(OctaveStream & os) const
203 if (name() == "cdot")
210 void InsetMathSymbol::write(WriteStream & os) const
212 os << '\\' << name();
214 // $,#, etc. In theory the restriction based on catcodes, but then
215 // we do not handle catcodes very well, let alone cat code changes,
216 // so being outside the alpha range is good enough.
217 if (name().size() == 1 && !isAlphaASCII(name()[0]))
220 os.pendingSpace(true);
224 void InsetMathSymbol::infoize2(odocstream & os) const
226 os << from_ascii("Symbol: ") << name();