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
71 mathedSymbolDim(mi.base, dim, sym_);
72 if (sym_->draw != sym_->name) {
74 // FIXME: this should depend on BufferView
76 kerning_ = mathed_char_kerning(mi.base.font,
77 mathedSymbol(mi.base, sym_).back());
79 // align character vertically
80 // FIXME: this should depend on BufferView
82 if (mathClass() == MC_OP) {
83 // center the symbol around the fraction axis
84 // See rule 13 of Appendix G of the TeXbook.
85 h_ = axis_height(mi.base) + (dim.des - dim.asc) / 2;
86 } else if (sym_->inset == "wasy") {
87 // correct height for broken wasy font
96 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
98 mathedSymbolDraw(pi, x, y - h_, sym_);
102 InsetMath::mode_type InsetMathSymbol::currentMode() const
104 return sym_->extra == "textmode" ? TEXT_MODE : MATH_MODE;
108 bool InsetMathSymbol::isOrdAlpha() const
110 return sym_->extra == "mathord" || sym_->extra == "mathalpha";
114 MathClass InsetMathSymbol::mathClass() const
116 if (sym_->extra == "func" || sym_->extra == "funclim")
118 MathClass const mc = string_to_class(sym_->extra);
119 return (mc == MC_UNKNOWN) ? MC_ORD : mc;
123 void InsetMathSymbol::normalize(NormalStream & os) const
125 os << "[symbol " << name() << ']';
129 void InsetMathSymbol::maple(MapleStream & os) const
131 if (name() == "cdot")
133 else if (name() == "infty")
139 void InsetMathSymbol::maxima(MaximaStream & os) const
141 if (name() == "cdot")
143 else if (name() == "infty")
145 else if (name() == "pi")
152 void InsetMathSymbol::mathematica(MathematicaStream & os) const
154 if ( name() == "pi") { os << "Pi"; return;}
155 if ( name() == "infty") { os << "Infinity"; return;}
156 if ( name() == "cdot") { os << '*'; return;}
161 void InsetMathSymbol::mathmlize(MathStream & ms) const
163 // FIXME We may need to do more interesting things
165 docstring tag = from_ascii(ms.namespacedTag(sym_->MathMLtype()));
166 ms << '<' << tag << ">";
167 if ((ms.xmlMode() && sym_->xmlname == "x") || (!ms.xmlMode() && sym_->htmlname == "x"))
170 else if (ms.xmlMode())
173 ms << sym_->htmlname;
174 ms << "</" << tag << '>';
178 void InsetMathSymbol::htmlize(HtmlStream & os, bool spacing) const
180 // FIXME We may need to do more interesting things
182 char const * type = sym_->MathMLtype();
183 bool op = (std::string(type) == "mo");
185 if (sym_->htmlname == "x")
187 os << ' ' << name() << ' ';
188 else if (op && spacing)
189 os << ' ' << sym_->htmlname << ' ';
191 os << sym_->htmlname;
195 void InsetMathSymbol::htmlize(HtmlStream & os) const
201 void InsetMathSymbol::octave(OctaveStream & os) const
203 if (name() == "cdot")
210 void InsetMathSymbol::write(WriteStream & os) const
212 unique_ptr<MathEnsurer> ensurer;
213 if (currentMode() != TEXT_MODE)
214 ensurer = make_unique<MathEnsurer>(os);
216 ensurer = make_unique<MathEnsurer>(os, false, true, true);
217 os << '\\' << name();
219 // $,#, etc. In theory the restriction based on catcodes, but then
220 // we do not handle catcodes very well, let alone cat code changes,
221 // so being outside the alpha range is good enough.
222 if (name().size() == 1 && !isAlphaASCII(name()[0]))
225 os.pendingSpace(true);
230 void InsetMathSymbol::infoize2(odocstream & os) const
232 os << from_ascii("Symbol: ") << name();
236 void InsetMathSymbol::validate(LaTeXFeatures & features) const
238 // this is not really the ideal place to do this, but we can't
239 // validate in InsetMathExInt.
240 if (features.runparams().math_flavor == OutputParams::MathAsHTML
241 && sym_->name == from_ascii("int")) {
242 features.addCSSSnippet(
243 "span.limits{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
244 "span.limits span{display: block;}\n"
245 "span.intsym{font-size: 150%;}\n"
246 "sub.limit{font-size: 75%;}\n"
247 "sup.limit{font-size: 75%;}");
249 if (!sym_->required.empty())
250 features.require(sym_->required);