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"
33 InsetMathSymbol::InsetMathSymbol(latexkeys const * l)
34 : sym_(l), h_(0), kerning_(0), scriptable_(false)
38 InsetMathSymbol::InsetMathSymbol(char const * name)
39 : sym_(in_word_set(from_ascii(name))), h_(0),
40 kerning_(0), scriptable_(false)
44 InsetMathSymbol::InsetMathSymbol(docstring const & name)
45 : sym_(in_word_set(name)), h_(0), kerning_(0), scriptable_(false)
49 Inset * InsetMathSymbol::clone() const
51 return new InsetMathSymbol(*this);
55 docstring InsetMathSymbol::name() const
61 void InsetMathSymbol::metrics(MetricsInfo & mi, Dimension & dim) const
64 mathedSymbolDim(mi.base, dim, sym_);
66 kerning_ = mathed_char_kerning(mi.base.font, *sym_->draw.rbegin());
67 // correct height for broken cmex and wasy font
68 if (sym_->inset == "cmex" || sym_->inset == "wasy") {
75 if (mi.base.font.style() == DISPLAY_STYLE)
76 if (sym_->inset == "cmex" || sym_->inset == "esint" ||
77 sym_->extra == "funclim" ||
78 (sym_->inset == "stmry" && sym_->extra == "mathop"))
83 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
85 mathedSymbolDraw(pi, x, y - h_, sym_);
89 InsetMath::mode_type InsetMathSymbol::currentMode() const
91 return sym_->extra == "textmode" ? TEXT_MODE : MATH_MODE;
95 bool InsetMathSymbol::isOrdAlpha() const
97 return sym_->extra == "mathord" || sym_->extra == "mathalpha";
101 MathClass InsetMathSymbol::mathClass() const
103 if (sym_->extra == "func" || sym_->extra == "funclim")
105 MathClass const mc = string_to_class(sym_->extra);
106 return (mc == MC_UNKNOWN) ? MC_ORD : mc;
110 bool InsetMathSymbol::isScriptable() const
116 bool InsetMathSymbol::takesLimits() const
119 sym_->inset == "cmex" ||
120 sym_->inset == "lyxboldsymb" ||
121 sym_->inset == "esint" ||
122 sym_->extra == "funclim" ||
123 (sym_->inset == "stmry" && sym_->extra == "mathop");
127 void InsetMathSymbol::normalize(NormalStream & os) const
129 os << "[symbol " << name() << ']';
133 void InsetMathSymbol::maple(MapleStream & os) const
135 if (name() == "cdot")
137 else if (name() == "infty")
143 void InsetMathSymbol::maxima(MaximaStream & os) const
145 if (name() == "cdot")
147 else if (name() == "infty")
149 else if (name() == "pi")
156 void InsetMathSymbol::mathematica(MathematicaStream & os) const
158 if ( name() == "pi") { os << "Pi"; return;}
159 if ( name() == "infty") { os << "Infinity"; return;}
160 if ( name() == "cdot") { os << '*'; return;}
165 void InsetMathSymbol::mathmlize(MathStream & os) const
167 // FIXME We may need to do more interesting things
169 char const * type = sym_->MathMLtype();
170 os << '<' << type << "> ";
171 if (sym_->xmlname == "x")
176 os << " </" << type << '>';
180 void InsetMathSymbol::htmlize(HtmlStream & os, bool spacing) const
182 // FIXME We may need to do more interesting things
184 char const * type = sym_->MathMLtype();
185 bool op = (std::string(type) == "mo");
187 if (sym_->xmlname == "x")
189 os << ' ' << name() << ' ';
190 else if (op && spacing)
191 os << ' ' << sym_->xmlname << ' ';
197 void InsetMathSymbol::htmlize(HtmlStream & os) const
203 void InsetMathSymbol::octave(OctaveStream & os) const
205 if (name() == "cdot")
212 void InsetMathSymbol::write(WriteStream & os) const
214 unique_ptr<MathEnsurer> ensurer;
215 if (currentMode() != TEXT_MODE)
216 ensurer = make_unique<MathEnsurer>(os);
218 ensurer = make_unique<MathEnsurer>(os, false, true, true);
219 os << '\\' << name();
221 // $,#, etc. In theory the restriction based on catcodes, but then
222 // we do not handle catcodes very well, let alone cat code changes,
223 // so being outside the alpha range is good enough.
224 if (name().size() == 1 && !isAlphaASCII(name()[0]))
227 os.pendingSpace(true);
231 void InsetMathSymbol::infoize2(odocstream & os) const
233 os << from_ascii("Symbol: ") << name();
237 void InsetMathSymbol::validate(LaTeXFeatures & features) const
239 // this is not really the ideal place to do this, but we can't
240 // validate in InsetMathExInt.
241 if (features.runparams().math_flavor == OutputParams::MathAsHTML
242 && sym_->name == from_ascii("int")) {
243 features.addCSSSnippet(
244 "span.limits{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
245 "span.limits span{display: block;}\n"
246 "span.intsym{font-size: 150%;}\n"
247 "sub.limit{font-size: 75%;}\n"
248 "sup.limit{font-size: 75%;}");
250 if (!sym_->required.empty())
251 features.require(sym_->required);