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"
16 #include "MathParser.h"
17 #include "MathStream.h"
18 #include "MathSupport.h"
20 #include "Dimension.h"
21 #include "LaTeXFeatures.h"
22 #include "MetricsInfo.h"
24 #include "support/debug.h"
25 #include "support/docstream.h"
26 #include "support/lyxlib.h"
27 #include "support/textutils.h"
28 #include "support/unique_ptr.h"
33 InsetMathSymbol::InsetMathSymbol(latexkeys const * l)
34 : sym_(l), h_(0), kerning_(0), scriptable_(false)
38 InsetMathSymbol::InsetMathSymbol(char const * name)
39 : sym_(in_word_set(from_ascii(name))), h_(0),
40 kerning_(0), scriptable_(false)
44 InsetMathSymbol::InsetMathSymbol(docstring const & name)
45 : sym_(in_word_set(name)), h_(0), kerning_(0), scriptable_(false)
49 Inset * InsetMathSymbol::clone() const
51 return new InsetMathSymbol(*this);
55 docstring InsetMathSymbol::name() const
61 void InsetMathSymbol::metrics(MetricsInfo & mi, Dimension & dim) const
64 mathedSymbolDim(mi, dim, sym_);
66 kerning_ = mathed_char_kerning(mi.base.font, *sym_->draw.rbegin());
67 // correct height for broken cmex and wasy font
68 if (sym_->inset == "cmex" || sym_->inset == "wasy") {
75 if (mi.base.style == LM_ST_DISPLAY)
76 if (sym_->inset == "cmex" || sym_->inset == "esint" ||
77 sym_->extra == "funclim" ||
78 (sym_->inset == "stmry" && sym_->extra == "mathop"))
83 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
85 mathedSymbolDraw(pi, x, y - h_, sym_);
89 InsetMath::mode_type InsetMathSymbol::currentMode() const
91 return sym_->extra == "textmode" ? TEXT_MODE : MATH_MODE;
95 bool InsetMathSymbol::isMathBin() const
97 return sym_->extra == "mathbin";
101 bool InsetMathSymbol::isMathRel() const
103 return sym_->extra == "mathrel";
107 bool InsetMathSymbol::isMathPunct() const
109 return sym_->extra == "mathpunct";
113 bool InsetMathSymbol::isOrdAlpha() const
115 return sym_->extra == "mathord" || sym_->extra == "mathalpha";
119 bool InsetMathSymbol::isScriptable() const
125 bool InsetMathSymbol::takesLimits() const
128 sym_->inset == "cmex" ||
129 sym_->inset == "lyxboldsymb" ||
130 sym_->inset == "esint" ||
131 sym_->extra == "funclim" ||
132 (sym_->inset == "stmry" && sym_->extra == "mathop");
136 void InsetMathSymbol::normalize(NormalStream & os) const
138 os << "[symbol " << name() << ']';
142 void InsetMathSymbol::maple(MapleStream & os) const
144 if (name() == "cdot")
146 else if (name() == "infty")
152 void InsetMathSymbol::maxima(MaximaStream & os) const
154 if (name() == "cdot")
156 else if (name() == "infty")
158 else if (name() == "pi")
165 void InsetMathSymbol::mathematica(MathematicaStream & os) const
167 if ( name() == "pi") { os << "Pi"; return;}
168 if ( name() == "infty") { os << "Infinity"; return;}
169 if ( name() == "cdot") { os << '*'; return;}
174 void InsetMathSymbol::mathmlize(MathStream & os) const
176 // FIXME We may need to do more interesting things
178 char const * type = sym_->MathMLtype();
179 os << '<' << type << "> ";
180 if (sym_->xmlname == "x")
185 os << " </" << type << '>';
189 void InsetMathSymbol::htmlize(HtmlStream & os, bool spacing) const
191 // FIXME We may need to do more interesting things
193 char const * type = sym_->MathMLtype();
194 bool op = (std::string(type) == "mo");
196 if (sym_->xmlname == "x")
198 os << ' ' << name() << ' ';
199 else if (op && spacing)
200 os << ' ' << sym_->xmlname << ' ';
206 void InsetMathSymbol::htmlize(HtmlStream & os) const
212 void InsetMathSymbol::octave(OctaveStream & os) const
214 if (name() == "cdot")
221 void InsetMathSymbol::write(WriteStream & os) const
223 unique_ptr<MathEnsurer> ensurer;
224 if (currentMode() != TEXT_MODE)
225 ensurer = make_unique<MathEnsurer>(os);
227 ensurer = make_unique<MathEnsurer>(os, false, true, true);
228 os << '\\' << name();
230 // $,#, etc. In theory the restriction based on catcodes, but then
231 // we do not handle catcodes very well, let alone cat code changes,
232 // so being outside the alpha range is good enough.
233 if (name().size() == 1 && !isAlphaASCII(name()[0]))
236 os.pendingSpace(true);
240 void InsetMathSymbol::infoize2(odocstream & os) const
242 os << from_ascii("Symbol: ") << name();
246 void InsetMathSymbol::validate(LaTeXFeatures & features) const
248 // this is not really the ideal place to do this, but we can't
249 // validate in InsetMathExInt.
250 if (features.runparams().math_flavor == OutputParams::MathAsHTML
251 && sym_->name == from_ascii("int")) {
252 features.addCSSSnippet(
253 "span.limits{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
254 "span.limits span{display: block;}\n"
255 "span.intsym{font-size: 150%;}\n"
256 "sub.limit{font-size: 75%;}\n"
257 "sup.limit{font-size: 75%;}");
259 if (!sym_->requires.empty())
260 features.require(sym_->requires);