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/textutils.h"
27 #include "support/unique_ptr.h"
33 InsetMathSymbol::InsetMathSymbol(Buffer * buf, latexkeys const * l)
34 : InsetMath(buf), sym_(l)
38 InsetMathSymbol::InsetMathSymbol(Buffer * buf, char const * name)
39 : InsetMath(buf), sym_(in_word_set(from_ascii(name)))
43 InsetMathSymbol::InsetMathSymbol(Buffer * buf, docstring const & name)
44 : InsetMath(buf), sym_(in_word_set(name))
48 Inset * InsetMathSymbol::clone() const
50 return new InsetMathSymbol(*this);
54 docstring InsetMathSymbol::name() const
60 /// The default limits value
61 Limits InsetMathSymbol::defaultLimits(bool display) const
63 if (allowsLimitsChange() && sym_->extra != "func" && display)
70 void InsetMathSymbol::metrics(MetricsInfo & mi, Dimension & dim) const
73 // FIXME: this should depend on BufferView
74 // set negative kerning_ so that a subscript is moved leftward
75 kerning_ = -mathedSymbolDim(mi.base, dim, sym_);
76 if (sym_->draw != sym_->name) {
77 // align character vertically
78 // FIXME: this should depend on BufferView
80 if (mathClass() == MC_OP) {
81 // center the symbol around the fraction axis
82 // See rule 13 of Appendix G of the TeXbook.
83 h_ = axis_height(mi.base) + (dim.des - dim.asc) / 2;
84 } else if (sym_->inset == "wasy") {
85 // correct height for broken wasy font
94 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
96 mathedSymbolDraw(pi, x, y - h_, sym_);
100 InsetMath::mode_type InsetMathSymbol::currentMode() const
102 return sym_->extra == "textmode" ? TEXT_MODE : MATH_MODE;
106 bool InsetMathSymbol::isOrdAlpha() const
108 return sym_->extra == "mathord" || sym_->extra == "mathalpha";
112 MathClass InsetMathSymbol::mathClass() const
114 if (sym_->extra == "func" || sym_->extra == "funclim")
116 MathClass const mc = string_to_class(sym_->extra);
117 return (mc == MC_UNKNOWN) ? MC_ORD : mc;
121 void InsetMathSymbol::normalize(NormalStream & os) const
123 os << "[symbol " << name() << ']';
127 void InsetMathSymbol::maple(MapleStream & os) const
129 if (name() == "cdot")
131 else if (name() == "infty")
137 void InsetMathSymbol::maxima(MaximaStream & os) const
139 if (name() == "cdot")
141 else if (name() == "infty")
143 else if (name() == "pi")
150 void InsetMathSymbol::mathematica(MathematicaStream & os) const
152 if ( name() == "pi") { os << "Pi"; return;}
153 if ( name() == "infty") { os << "Infinity"; return;}
154 if ( name() == "cdot") { os << '*'; return;}
159 void InsetMathSymbol::mathmlize(MathMLStream & ms) const
161 // FIXME We may need to do more interesting things
163 ms << MTagInline(sym_->MathMLtype());
164 if (sym_->xmlname == "x")
169 ms << ETagInline(sym_->MathMLtype());
173 void InsetMathSymbol::htmlize(HtmlStream & os, bool spacing) const
175 // FIXME We may need to do more interesting things
177 char const * type = sym_->MathMLtype();
178 bool op = (std::string(type) == "mo");
180 if (sym_->xmlname == "x")
182 os << ' ' << name() << ' ';
183 else if (op && spacing)
184 os << ' ' << sym_->xmlname << ' ';
190 void InsetMathSymbol::htmlize(HtmlStream & os) const
196 void InsetMathSymbol::octave(OctaveStream & os) const
198 if (name() == "cdot")
205 void InsetMathSymbol::write(TeXMathStream & os) const
207 unique_ptr<MathEnsurer> ensurer;
208 if (currentMode() != TEXT_MODE)
209 ensurer = make_unique<MathEnsurer>(os);
211 ensurer = make_unique<MathEnsurer>(os, false, true, true);
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);
225 void InsetMathSymbol::infoize2(odocstream & os) const
227 os << from_ascii("Symbol: ") << name();
231 void InsetMathSymbol::validate(LaTeXFeatures & features) const
233 // this is not really the ideal place to do this, but we can't
234 // validate in InsetMathExInt.
235 if (features.runparams().math_flavor == OutputParams::MathAsHTML
236 && sym_->name == from_ascii("int")) {
237 features.addCSSSnippet(
238 "span.limits{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
239 "span.limits span{display: block;}\n"
240 "span.intsym{font-size: 150%;}\n"
241 "sub.limit{font-size: 75%;}\n"
242 "sup.limit{font-size: 75%;}");
244 if (!sym_->required.empty())
245 features.require(sym_->required);