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() const
63 return (allowsLimitsChange() && sym_->extra != "func")
68 void InsetMathSymbol::metrics(MetricsInfo & mi, Dimension & dim) const
71 // FIXME: this should depend on BufferView
72 // set negative kerning_ so that a subscript is moved leftward
73 kerning_ = -mathedSymbolDim(mi.base, dim, sym_);
74 if (sym_->draw != sym_->name) {
75 // align character vertically
76 // FIXME: this should depend on BufferView
78 if (mathClass() == MC_OP) {
79 // center the symbol around the fraction axis
80 // See rule 13 of Appendix G of the TeXbook.
81 h_ = axis_height(mi.base) + (dim.des - dim.asc) / 2;
82 } else if (sym_->inset == "wasy") {
83 // correct height for broken wasy font
92 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
94 mathedSymbolDraw(pi, x, y - h_, sym_);
98 InsetMath::mode_type InsetMathSymbol::currentMode() const
100 return sym_->extra == "textmode" ? TEXT_MODE : MATH_MODE;
104 bool InsetMathSymbol::isOrdAlpha() const
106 return sym_->extra == "mathord" || sym_->extra == "mathalpha";
110 MathClass InsetMathSymbol::mathClass() const
112 if (sym_->extra == "func" || sym_->extra == "funclim")
114 MathClass const mc = string_to_class(sym_->extra);
115 return (mc == MC_UNKNOWN) ? MC_ORD : mc;
119 void InsetMathSymbol::normalize(NormalStream & os) const
121 os << "[symbol " << name() << ']';
125 void InsetMathSymbol::maple(MapleStream & os) const
127 if (name() == "cdot")
129 else if (name() == "infty")
135 void InsetMathSymbol::maxima(MaximaStream & os) const
137 if (name() == "cdot")
139 else if (name() == "infty")
141 else if (name() == "pi")
148 void InsetMathSymbol::mathematica(MathematicaStream & os) const
150 if ( name() == "pi") { os << "Pi"; return;}
151 if ( name() == "infty") { os << "Infinity"; return;}
152 if ( name() == "cdot") { os << '*'; return;}
157 void InsetMathSymbol::mathmlize(MathStream & ms) const
159 // FIXME We may need to do more interesting things
161 docstring tag = from_ascii(ms.namespacedTag(sym_->MathMLtype()));
162 ms << '<' << tag << ">";
163 if ((ms.xmlMode() && sym_->xmlname == "x") || (!ms.xmlMode() && sym_->htmlname == "x"))
166 else if (ms.xmlMode())
169 ms << sym_->htmlname;
170 ms << "</" << tag << '>';
174 void InsetMathSymbol::htmlize(HtmlStream & os, bool spacing) const
176 // FIXME We may need to do more interesting things
178 char const * type = sym_->MathMLtype();
179 bool op = (std::string(type) == "mo");
181 if (sym_->htmlname == "x")
183 os << ' ' << name() << ' ';
184 else if (op && spacing)
185 os << ' ' << sym_->htmlname << ' ';
187 os << sym_->htmlname;
191 void InsetMathSymbol::htmlize(HtmlStream & os) const
197 void InsetMathSymbol::octave(OctaveStream & os) const
199 if (name() == "cdot")
206 void InsetMathSymbol::write(WriteStream & os) const
208 unique_ptr<MathEnsurer> ensurer;
209 if (currentMode() != TEXT_MODE)
210 ensurer = make_unique<MathEnsurer>(os);
212 ensurer = make_unique<MathEnsurer>(os, false, true, true);
213 os << '\\' << name();
215 // $,#, etc. In theory the restriction based on catcodes, but then
216 // we do not handle catcodes very well, let alone cat code changes,
217 // so being outside the alpha range is good enough.
218 if (name().size() == 1 && !isAlphaASCII(name()[0]))
221 os.pendingSpace(true);
226 void InsetMathSymbol::infoize2(odocstream & os) const
228 os << from_ascii("Symbol: ") << name();
232 void InsetMathSymbol::validate(LaTeXFeatures & features) const
234 // this is not really the ideal place to do this, but we can't
235 // validate in InsetMathExInt.
236 if (features.runparams().math_flavor == OutputParams::MathAsHTML
237 && sym_->name == from_ascii("int")) {
238 features.addCSSSnippet(
239 "span.limits{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
240 "span.limits span{display: block;}\n"
241 "span.intsym{font-size: 150%;}\n"
242 "sub.limit{font-size: 75%;}\n"
243 "sup.limit{font-size: 75%;}");
245 if (!sym_->required.empty())
246 features.require(sym_->required);