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 //lyxerr << "metrics: symbol: '" << sym_->name
64 // << "' in font: '" << sym_->inset
65 // << "' drawn as: '" << sym_->draw
68 bool const italic_upcase_greek = sym_->inset == "cmr" &&
69 sym_->extra == "mathalpha" &&
70 mi.base.fontname == "mathit";
71 std::string const font = italic_upcase_greek ? "cmm" : sym_->inset;
72 Changer dummy = mi.base.changeFontSet(from_ascii(font));
73 mathed_string_dim(mi.base.font, sym_->draw, dim);
74 docstring::const_reverse_iterator rit = sym_->draw.rbegin();
75 kerning_ = mathed_char_kerning(mi.base.font, *rit);
76 // correct height for broken cmex and wasy font
77 if (sym_->inset == "cmex" || sym_->inset == "wasy") {
82 // seperate things a bit
84 dim.wid += 2 * mathed_medmuskip(mi.base.font);
86 dim.wid += 2 * mathed_thickmuskip(mi.base.font);
87 else if (isMathPunct())
88 dim.wid += mathed_thinmuskip(mi.base.font);
89 // FIXME: I see no reason for this
91 // dim.wid += support::iround(0.1667 * em);
94 if (mi.base.style == LM_ST_DISPLAY)
95 if (sym_->inset == "cmex" || sym_->inset == "esint" ||
96 sym_->extra == "funclim" ||
97 (sym_->inset == "stmry" && sym_->extra == "mathop"))
102 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
104 //lyxerr << "metrics: symbol: '" << sym_->name
105 // << "' in font: '" << sym_->inset
106 // << "' drawn as: '" << sym_->draw
109 bool const italic_upcase_greek = sym_->inset == "cmr" &&
110 sym_->extra == "mathalpha" &&
111 pi.base.fontname == "mathit";
112 std::string const font = italic_upcase_greek ? "cmm" : sym_->inset;
114 x += mathed_medmuskip(pi.base.font);
115 else if (isMathRel())
116 x += mathed_thickmuskip(pi.base.font);
117 // FIXME: I see no reason for this
119 // x += support::iround(0.0833 * em);
121 Changer dummy = pi.base.changeFontSet(from_ascii(font));
122 pi.draw(x, y - h_, sym_->draw);
126 InsetMath::mode_type InsetMathSymbol::currentMode() const
128 return sym_->extra == "textmode" ? TEXT_MODE : MATH_MODE;
132 bool InsetMathSymbol::isMathBin() const
134 return sym_->extra == "mathbin";
138 bool InsetMathSymbol::isMathRel() const
140 return sym_->extra == "mathrel";
144 bool InsetMathSymbol::isMathPunct() const
146 return sym_->extra == "mathpunct";
150 bool InsetMathSymbol::isOrdAlpha() const
152 return sym_->extra == "mathord" || sym_->extra == "mathalpha";
156 bool InsetMathSymbol::isScriptable() const
162 bool InsetMathSymbol::takesLimits() const
165 sym_->inset == "cmex" ||
166 sym_->inset == "lyxboldsymb" ||
167 sym_->inset == "esint" ||
168 sym_->extra == "funclim" ||
169 (sym_->inset == "stmry" && sym_->extra == "mathop");
173 void InsetMathSymbol::normalize(NormalStream & os) const
175 os << "[symbol " << name() << ']';
179 void InsetMathSymbol::maple(MapleStream & os) const
181 if (name() == "cdot")
183 else if (name() == "infty")
189 void InsetMathSymbol::maxima(MaximaStream & os) const
191 if (name() == "cdot")
193 else if (name() == "infty")
195 else if (name() == "pi")
202 void InsetMathSymbol::mathematica(MathematicaStream & os) const
204 if ( name() == "pi") { os << "Pi"; return;}
205 if ( name() == "infty") { os << "Infinity"; return;}
206 if ( name() == "cdot") { os << '*'; return;}
211 void InsetMathSymbol::mathmlize(MathStream & os) const
213 // FIXME We may need to do more interesting things
215 char const * type = sym_->MathMLtype();
216 os << '<' << type << "> ";
217 if (sym_->xmlname == "x")
222 os << " </" << type << '>';
226 void InsetMathSymbol::htmlize(HtmlStream & os, bool spacing) const
228 // FIXME We may need to do more interesting things
230 char const * type = sym_->MathMLtype();
231 bool op = (std::string(type) == "mo");
233 if (sym_->xmlname == "x")
235 os << ' ' << name() << ' ';
236 else if (op && spacing)
237 os << ' ' << sym_->xmlname << ' ';
243 void InsetMathSymbol::htmlize(HtmlStream & os) const
249 void InsetMathSymbol::octave(OctaveStream & os) const
251 if (name() == "cdot")
258 void InsetMathSymbol::write(WriteStream & os) const
260 unique_ptr<MathEnsurer> ensurer;
261 if (currentMode() != TEXT_MODE)
262 ensurer = make_unique<MathEnsurer>(os);
263 os << '\\' << name();
265 // $,#, etc. In theory the restriction based on catcodes, but then
266 // we do not handle catcodes very well, let alone cat code changes,
267 // so being outside the alpha range is good enough.
268 if (name().size() == 1 && !isAlphaASCII(name()[0]))
271 os.pendingSpace(true);
275 void InsetMathSymbol::infoize2(odocstream & os) const
277 os << from_ascii("Symbol: ") << name();
281 void InsetMathSymbol::validate(LaTeXFeatures & features) const
283 // this is not really the ideal place to do this, but we can't
284 // validate in InsetMathExInt.
285 if (features.runparams().math_flavor == OutputParams::MathAsHTML
286 && sym_->name == from_ascii("int")) {
287 features.addCSSSnippet(
288 "span.limits{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
289 "span.limits span{display: block;}\n"
290 "span.intsym{font-size: 150%;}\n"
291 "sub.limit{font-size: 75%;}\n"
292 "sup.limit{font-size: 75%;}");
294 if (!sym_->requires.empty())
295 features.require(sym_->requires);