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"
14 #include "dimension.h"
15 #include "MathStream.h"
16 #include "MathStream.h"
17 #include "MathSupport.h"
18 #include "MathParser.h"
20 #include "LaTeXFeatures.h"
23 #include "support/textutils.h"
31 InsetMathSymbol::InsetMathSymbol(latexkeys const * l)
32 : sym_(l), h_(0), scriptable_(false), font_cache_(LyXFont::ALL_IGNORE)
36 InsetMathSymbol::InsetMathSymbol(char const * name)
37 : sym_(in_word_set(from_ascii(name))), h_(0), scriptable_(false),
38 font_cache_(LyXFont::ALL_IGNORE)
42 InsetMathSymbol::InsetMathSymbol(docstring const & name)
43 : sym_(in_word_set(name)), h_(0), scriptable_(false), font_cache_(LyXFont::ALL_IGNORE)
47 auto_ptr<InsetBase> InsetMathSymbol::doClone() const
49 return auto_ptr<InsetBase>(new InsetMathSymbol(*this));
53 docstring InsetMathSymbol::name() const
59 bool InsetMathSymbol::metrics(MetricsInfo & mi, Dimension & dim) const
61 //lyxerr << "metrics: symbol: '" << sym_->name
62 // << "' in font: '" << sym_->inset
63 // << "' drawn as: '" << sym_->draw
64 // << "'" << std::endl;
66 bool dim_unchanged = (mi.base.font == font_cache_);
70 font_cache_ = mi.base.font;
71 int const em = mathed_char_width(mi.base.font, 'M');
72 FontSetChanger dummy(mi.base, sym_->inset);
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 += static_cast<int>(0.5 * em + 0.5);
86 dim.wid += static_cast<int>(0.1667 * em + 0.5);
92 if (mi.base.style == LM_ST_DISPLAY)
93 if (sym_->inset == "cmex" || sym_->inset == "esint" ||
94 sym_->extra == "funclim")
101 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
103 //lyxerr << "metrics: symbol: '" << sym_->name
104 // << "' in font: '" << sym_->inset
105 // << "' drawn as: '" << sym_->draw
106 // << "'" << std::endl;
107 int const em = mathed_char_width(pi.base.font, 'M');
109 x += static_cast<int>(0.25*em+0.5);
111 x += static_cast<int>(0.0833*em+0.5);
113 FontSetChanger dummy(pi.base, sym_->inset.c_str());
114 pi.draw(x, y - h_, sym_->draw);
118 bool InsetMathSymbol::isRelOp() const
120 return sym_->extra == "mathrel";
124 bool InsetMathSymbol::isOrdAlpha() const
126 return sym_->extra == "mathord" || sym_->extra == "mathalpha";
130 bool InsetMathSymbol::isScriptable() const
136 bool InsetMathSymbol::takesLimits() const
139 sym_->inset == "cmex" ||
140 sym_->inset == "lyxboldsymb" ||
141 sym_->inset == "esint" ||
142 sym_->extra == "funclim";
146 void InsetMathSymbol::validate(LaTeXFeatures & features) const
148 if (!sym_->requires.empty())
149 features.require(to_utf8(sym_->requires));
153 void InsetMathSymbol::normalize(NormalStream & os) const
155 os << "[symbol " << name() << ']';
159 void InsetMathSymbol::maple(MapleStream & os) const
161 if (name() == "cdot")
163 else if (name() == "infty")
169 void InsetMathSymbol::maxima(MaximaStream & os) const
171 if (name() == "cdot")
173 else if (name() == "infty")
175 else if (name() == "pi")
182 void InsetMathSymbol::mathematica(MathematicaStream & os) const
184 if ( name() == "pi") { os << "Pi"; return;}
185 if ( name() == "infty") { os << "Infinity"; return;}
186 if ( name() == "cdot") { os << '*'; return;}
191 char const * MathMLtype(docstring const & s)
199 void InsetMathSymbol::mathmlize(MathStream & os) const
201 char const * type = MathMLtype(sym_->extra);
202 os << '<' << type << "> ";
203 if (sym_->xmlname == "x") // unknown so far
207 os << " </" << type << '>';
211 void InsetMathSymbol::octave(OctaveStream & os) const
213 if (name() == "cdot")
220 void InsetMathSymbol::write(WriteStream & os) const
222 os << '\\' << name();
224 // $,#, etc. In theory the restriction based on catcodes, but then
225 // we do not handle catcodes very well, let alone cat code changes,
226 // so being outside the alpha range is good enough.
227 if (name().size() == 1 && !isAlphaASCII(name()[0]))
230 os.pendingSpace(true);
234 void InsetMathSymbol::infoize2(odocstream & os) const
236 os << "Symbol: " << name();