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"
34 InsetMathSymbol::InsetMathSymbol(latexkeys const * l)
39 InsetMathSymbol::InsetMathSymbol(char const * name)
40 : sym_(in_word_set(from_ascii(name)))
44 InsetMathSymbol::InsetMathSymbol(docstring const & name)
45 : sym_(in_word_set(name))
49 Inset * InsetMathSymbol::clone() const
51 return new InsetMathSymbol(*this);
55 docstring InsetMathSymbol::name() const
61 /// The default limits value
62 Limits InsetMathSymbol::defaultLimits() const
64 return (allowsLimitsChange() && sym_->extra != "func")
69 void InsetMathSymbol::metrics(MetricsInfo & mi, Dimension & dim) const
72 // FIXME: this should depend on BufferView
73 // set negative kerning_ so that a subscript is moved leftward
74 kerning_ = -mathedSymbolDim(mi.base, dim, sym_);
75 if (sym_->draw != sym_->name) {
76 // align character vertically
77 // FIXME: this should depend on BufferView
79 if (mathClass() == MC_OP) {
80 // center the symbol around the fraction axis
81 // See rule 13 of Appendix G of the TeXbook.
82 h_ = axis_height(mi.base) + (dim.des - dim.asc) / 2;
83 } else if (sym_->inset == "wasy") {
84 // correct height for broken wasy font
93 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
95 mathedSymbolDraw(pi, x, y - h_, sym_);
99 InsetMath::mode_type InsetMathSymbol::currentMode() const
101 return sym_->extra == "textmode" ? TEXT_MODE : MATH_MODE;
105 bool InsetMathSymbol::isOrdAlpha() const
107 return sym_->extra == "mathord" || sym_->extra == "mathalpha";
111 MathClass InsetMathSymbol::mathClass() const
113 if (sym_->extra == "func" || sym_->extra == "funclim")
115 MathClass const mc = string_to_class(sym_->extra);
116 return (mc == MC_UNKNOWN) ? MC_ORD : mc;
120 void InsetMathSymbol::normalize(NormalStream & os) const
122 os << "[symbol " << name() << ']';
126 void InsetMathSymbol::maple(MapleStream & os) const
128 if (name() == "cdot")
130 else if (name() == "infty")
136 void InsetMathSymbol::maxima(MaximaStream & os) const
138 if (name() == "cdot")
140 else if (name() == "infty")
142 else if (name() == "pi")
149 void InsetMathSymbol::mathematica(MathematicaStream & os) const
151 if ( name() == "pi") { os << "Pi"; return;}
152 if ( name() == "infty") { os << "Infinity"; return;}
153 if ( name() == "cdot") { os << '*'; return;}
158 void InsetMathSymbol::mathmlize(MathStream & ms) const
160 // FIXME We may need to do more interesting things
162 docstring tag = from_ascii(ms.namespacedTag(sym_->MathMLtype()));
163 ms << '<' << tag << ">";
164 if ((ms.xmlMode() && sym_->xmlname == "x") || (!ms.xmlMode() && sym_->htmlname == "x"))
167 else if (ms.xmlMode())
170 ms << sym_->htmlname;
171 ms << "</" << tag << '>';
175 void InsetMathSymbol::htmlize(HtmlStream & os, bool spacing) const
177 // FIXME We may need to do more interesting things
179 char const * type = sym_->MathMLtype();
180 bool op = (std::string(type) == "mo");
182 if (sym_->htmlname == "x")
184 os << ' ' << name() << ' ';
185 else if (op && spacing)
186 os << ' ' << sym_->htmlname << ' ';
188 os << sym_->htmlname;
192 void InsetMathSymbol::htmlize(HtmlStream & os) const
198 void InsetMathSymbol::octave(OctaveStream & os) const
200 if (name() == "cdot")
207 void InsetMathSymbol::write(WriteStream & os) const
209 unique_ptr<MathEnsurer> ensurer;
210 if (currentMode() != TEXT_MODE)
211 ensurer = make_unique<MathEnsurer>(os);
213 ensurer = make_unique<MathEnsurer>(os, false, true, true);
214 os << '\\' << name();
216 // $,#, etc. In theory the restriction based on catcodes, but then
217 // we do not handle catcodes very well, let alone cat code changes,
218 // so being outside the alpha range is good enough.
219 if (name().size() == 1 && !isAlphaASCII(name()[0]))
222 os.pendingSpace(true);
227 void InsetMathSymbol::infoize2(odocstream & os) const
229 os << from_ascii("Symbol: ") << name();
233 void InsetMathSymbol::validate(LaTeXFeatures & features) const
235 // this is not really the ideal place to do this, but we can't
236 // validate in InsetMathExInt.
237 if (features.runparams().math_flavor == OutputParams::MathAsHTML
238 && sym_->name == from_ascii("int")) {
239 features.addCSSSnippet(
240 "span.limits{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
241 "span.limits span{display: block;}\n"
242 "span.intsym{font-size: 150%;}\n"
243 "sub.limit{font-size: 75%;}\n"
244 "sup.limit{font-size: 75%;}");
246 if (!sym_->required.empty())
247 features.require(sym_->required);