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(latexkeys const * l)
38 InsetMathSymbol::InsetMathSymbol(char const * name)
39 : sym_(in_word_set(from_ascii(name)))
43 InsetMathSymbol::InsetMathSymbol(docstring const & name)
44 : 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 docstring tag = from_ascii(ms.namespacedTag(sym_->MathMLtype()));
164 ms << '<' << tag << ">";
165 if ((ms.xmlMode() && sym_->xmlname == "x") || (!ms.xmlMode() && sym_->htmlname == "x"))
168 else if (ms.xmlMode())
171 ms << sym_->htmlname;
172 ms << "</" << tag << '>';
176 void InsetMathSymbol::htmlize(HtmlStream & os, bool spacing) const
178 // FIXME We may need to do more interesting things
180 char const * type = sym_->MathMLtype();
181 bool op = (std::string(type) == "mo");
183 if (sym_->htmlname == "x")
185 os << ' ' << name() << ' ';
186 else if (op && spacing)
187 os << ' ' << sym_->htmlname << ' ';
189 os << sym_->htmlname;
193 void InsetMathSymbol::htmlize(HtmlStream & os) const
199 void InsetMathSymbol::octave(OctaveStream & os) const
201 if (name() == "cdot")
208 void InsetMathSymbol::write(TeXMathStream & os) const
210 unique_ptr<MathEnsurer> ensurer;
211 if (currentMode() != TEXT_MODE)
212 ensurer = make_unique<MathEnsurer>(os);
214 ensurer = make_unique<MathEnsurer>(os, false, true, true);
215 os << '\\' << name();
217 // $,#, etc. In theory the restriction based on catcodes, but then
218 // we do not handle catcodes very well, let alone cat code changes,
219 // so being outside the alpha range is good enough.
220 if (name().size() == 1 && !isAlphaASCII(name()[0]))
223 os.pendingSpace(true);
228 void InsetMathSymbol::infoize2(odocstream & os) const
230 os << from_ascii("Symbol: ") << name();
234 void InsetMathSymbol::validate(LaTeXFeatures & features) const
236 // this is not really the ideal place to do this, but we can't
237 // validate in InsetMathExInt.
238 if (features.runparams().math_flavor == OutputParams::MathAsHTML
239 && sym_->name == from_ascii("int")) {
240 features.addCSSSnippet(
241 "span.limits{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
242 "span.limits span{display: block;}\n"
243 "span.intsym{font-size: 150%;}\n"
244 "sub.limit{font-size: 75%;}\n"
245 "sup.limit{font-size: 75%;}");
247 if (!sym_->required.empty())
248 features.require(sym_->required);