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
63 mathedSymbolDim(mi.base, dim, sym_);
64 if (sym_->draw != sym_->name) {
66 // FIXME: this should depend on BufferView
68 kerning_ = mathed_char_kerning(mi.base.font,
69 mathedSymbol(mi.base, sym_).back());
71 // align character vertically
72 // FIXME: this should depend on BufferView
74 if (mathClass() == MC_OP) {
75 // center the symbol around the fraction axis
76 // See rule 13 of Appendix G of the TeXbook.
77 h_ = axis_height(mi.base) + (dim.des - dim.asc) / 2;
78 } else if (sym_->inset == "wasy") {
79 // correct height for broken wasy font
87 //FIXME: get rid of that? Only "funclim" probably, along with
88 // class==MC_OP. The issue is to implement \limits properly.
90 if (mi.base.font.style() == DISPLAY_STYLE)
91 if (sym_->inset == "cmex" || sym_->inset == "esint" ||
92 sym_->extra == "funclim" ||
93 (sym_->inset == "stmry" && sym_->extra == "mathop"))
98 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
100 mathedSymbolDraw(pi, x, y - h_, sym_);
104 InsetMath::mode_type InsetMathSymbol::currentMode() const
106 return sym_->extra == "textmode" ? TEXT_MODE : MATH_MODE;
110 bool InsetMathSymbol::isOrdAlpha() const
112 return sym_->extra == "mathord" || sym_->extra == "mathalpha";
116 MathClass InsetMathSymbol::mathClass() const
118 if (sym_->extra == "func" || sym_->extra == "funclim")
120 MathClass const mc = string_to_class(sym_->extra);
121 return (mc == MC_UNKNOWN) ? MC_ORD : mc;
125 bool InsetMathSymbol::isScriptable() const
131 bool InsetMathSymbol::takesLimits() const
134 sym_->inset == "cmex" ||
135 sym_->inset == "lyxboldsymb" ||
136 sym_->inset == "esint" ||
137 sym_->extra == "funclim" ||
138 (sym_->inset == "stmry" && sym_->extra == "mathop");
142 void InsetMathSymbol::normalize(NormalStream & os) const
144 os << "[symbol " << name() << ']';
148 void InsetMathSymbol::maple(MapleStream & os) const
150 if (name() == "cdot")
152 else if (name() == "infty")
158 void InsetMathSymbol::maxima(MaximaStream & os) const
160 if (name() == "cdot")
162 else if (name() == "infty")
164 else if (name() == "pi")
171 void InsetMathSymbol::mathematica(MathematicaStream & os) const
173 if ( name() == "pi") { os << "Pi"; return;}
174 if ( name() == "infty") { os << "Infinity"; return;}
175 if ( name() == "cdot") { os << '*'; return;}
180 void InsetMathSymbol::mathmlize(MathStream & ms) const
182 // FIXME We may need to do more interesting things
184 docstring tag = from_ascii(ms.namespacedTag(sym_->MathMLtype()));
185 ms << '<' << tag << ">";
186 if ((ms.xmlMode() && sym_->xmlname == "x") || (!ms.xmlMode() && sym_->htmlname == "x"))
189 else if (ms.xmlMode())
192 ms << sym_->htmlname;
193 ms << "</" << tag << '>';
197 void InsetMathSymbol::htmlize(HtmlStream & os, bool spacing) const
199 // FIXME We may need to do more interesting things
201 char const * type = sym_->MathMLtype();
202 bool op = (std::string(type) == "mo");
204 if (sym_->htmlname == "x")
206 os << ' ' << name() << ' ';
207 else if (op && spacing)
208 os << ' ' << sym_->htmlname << ' ';
210 os << sym_->htmlname;
214 void InsetMathSymbol::htmlize(HtmlStream & os) const
220 void InsetMathSymbol::octave(OctaveStream & os) const
222 if (name() == "cdot")
229 void InsetMathSymbol::write(WriteStream & os) const
231 unique_ptr<MathEnsurer> ensurer;
232 if (currentMode() != TEXT_MODE)
233 ensurer = make_unique<MathEnsurer>(os);
235 ensurer = make_unique<MathEnsurer>(os, false, true, true);
236 os << '\\' << name();
238 // $,#, etc. In theory the restriction based on catcodes, but then
239 // we do not handle catcodes very well, let alone cat code changes,
240 // so being outside the alpha range is good enough.
241 if (name().size() == 1 && !isAlphaASCII(name()[0]))
244 os.pendingSpace(true);
248 void InsetMathSymbol::infoize2(odocstream & os) const
250 os << from_ascii("Symbol: ") << name();
254 void InsetMathSymbol::validate(LaTeXFeatures & features) const
256 // this is not really the ideal place to do this, but we can't
257 // validate in InsetMathExInt.
258 if (features.runparams().math_flavor == OutputParams::MathAsHTML
259 && sym_->name == from_ascii("int")) {
260 features.addCSSSnippet(
261 "span.limits{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
262 "span.limits span{display: block;}\n"
263 "span.intsym{font-size: 150%;}\n"
264 "sub.limit{font-size: 75%;}\n"
265 "sup.limit{font-size: 75%;}");
267 if (!sym_->required.empty())
268 features.require(sym_->required);