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"
15 #include "Dimension.h"
16 #include "LaTeXFeatures.h"
18 #include "MathParser.h"
19 #include "MathStream.h"
20 #include "MathSupport.h"
22 #include "support/debug.h"
23 #include "support/docstream.h"
24 #include "support/textutils.h"
25 #include "support/unique_ptr.h"
30 InsetMathSymbol::InsetMathSymbol(latexkeys const * l)
31 : sym_(l), h_(0), kerning_(0), scriptable_(false)
35 InsetMathSymbol::InsetMathSymbol(char const * name)
36 : sym_(in_word_set(from_ascii(name))), h_(0),
37 kerning_(0), scriptable_(false)
41 InsetMathSymbol::InsetMathSymbol(docstring const & name)
42 : sym_(in_word_set(name)), h_(0), kerning_(0), scriptable_(false)
46 Inset * InsetMathSymbol::clone() const
48 return new InsetMathSymbol(*this);
52 docstring InsetMathSymbol::name() const
58 void InsetMathSymbol::metrics(MetricsInfo & mi, Dimension & dim) const
60 //lyxerr << "metrics: symbol: '" << sym_->name
61 // << "' in font: '" << sym_->inset
62 // << "' drawn as: '" << sym_->draw
65 bool const italic_upcase_greek = sym_->inset == "cmr" &&
66 sym_->extra == "mathalpha" &&
67 mi.base.fontname == "mathit";
68 std::string const font = italic_upcase_greek ? "cmm" : sym_->inset;
69 FontSetChanger dummy(mi.base, from_ascii(font));
70 mathed_string_dim(mi.base.font, sym_->draw, dim);
71 docstring::const_reverse_iterator rit = sym_->draw.rbegin();
72 kerning_ = mathed_char_kerning(mi.base.font, *rit);
73 // correct height for broken cmex and wasy font
74 if (sym_->inset == "cmex" || sym_->inset == "wasy") {
79 // seperate things a bit
81 dim.wid += 2 * mathed_medmuskip(mi.base.font);
83 dim.wid += 2 * mathed_thickmuskip(mi.base.font);
84 else if (isMathPunct())
85 dim.wid += mathed_thinmuskip(mi.base.font);
86 // FIXME: I see no reason for this
88 // dim.wid += support::iround(0.1667 * em);
91 if (mi.base.style == LM_ST_DISPLAY)
92 if (sym_->inset == "cmex" || sym_->inset == "esint" ||
93 sym_->extra == "funclim" ||
94 (sym_->inset == "stmry" && sym_->extra == "mathop"))
99 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
101 //lyxerr << "metrics: symbol: '" << sym_->name
102 // << "' in font: '" << sym_->inset
103 // << "' drawn as: '" << sym_->draw
106 bool const italic_upcase_greek = sym_->inset == "cmr" &&
107 sym_->extra == "mathalpha" &&
108 pi.base.fontname == "mathit";
109 std::string const font = italic_upcase_greek ? "cmm" : sym_->inset;
111 x += mathed_medmuskip(pi.base.font);
112 else if (isMathRel())
113 x += mathed_thickmuskip(pi.base.font);
114 // FIXME: I see no reason for this
116 // x += support::iround(0.0833 * em);
118 FontSetChanger dummy(pi.base, from_ascii(font));
119 pi.draw(x, y - h_, sym_->draw);
123 InsetMath::mode_type InsetMathSymbol::currentMode() const
125 return sym_->extra == "textmode" ? TEXT_MODE : MATH_MODE;
129 bool InsetMathSymbol::isMathBin() const
131 return sym_->extra == "mathbin";
135 bool InsetMathSymbol::isMathRel() const
137 return sym_->extra == "mathrel";
141 bool InsetMathSymbol::isMathPunct() const
143 return sym_->extra == "mathpunct";
147 bool InsetMathSymbol::isOrdAlpha() const
149 return sym_->extra == "mathord" || sym_->extra == "mathalpha";
153 bool InsetMathSymbol::isScriptable() const
159 bool InsetMathSymbol::takesLimits() const
162 sym_->inset == "cmex" ||
163 sym_->inset == "lyxboldsymb" ||
164 sym_->inset == "esint" ||
165 sym_->extra == "funclim" ||
166 (sym_->inset == "stmry" && sym_->extra == "mathop");
170 void InsetMathSymbol::normalize(NormalStream & os) const
172 os << "[symbol " << name() << ']';
176 void InsetMathSymbol::maple(MapleStream & os) const
178 if (name() == "cdot")
180 else if (name() == "infty")
186 void InsetMathSymbol::maxima(MaximaStream & os) const
188 if (name() == "cdot")
190 else if (name() == "infty")
192 else if (name() == "pi")
199 void InsetMathSymbol::mathematica(MathematicaStream & os) const
201 if ( name() == "pi") { os << "Pi"; return;}
202 if ( name() == "infty") { os << "Infinity"; return;}
203 if ( name() == "cdot") { os << '*'; return;}
208 void InsetMathSymbol::mathmlize(MathStream & os) const
210 // FIXME We may need to do more interesting things
212 char const * type = sym_->MathMLtype();
213 os << '<' << type << "> ";
214 if (sym_->xmlname == "x")
219 os << " </" << type << '>';
223 void InsetMathSymbol::htmlize(HtmlStream & os, bool spacing) const
225 // FIXME We may need to do more interesting things
227 char const * type = sym_->MathMLtype();
228 bool op = (std::string(type) == "mo");
230 if (sym_->xmlname == "x")
232 os << ' ' << name() << ' ';
233 else if (op && spacing)
234 os << ' ' << sym_->xmlname << ' ';
240 void InsetMathSymbol::htmlize(HtmlStream & os) const
246 void InsetMathSymbol::octave(OctaveStream & os) const
248 if (name() == "cdot")
255 void InsetMathSymbol::write(WriteStream & os) const
257 unique_ptr<MathEnsurer> ensurer;
258 if (currentMode() != TEXT_MODE)
259 ensurer = make_unique<MathEnsurer>(os);
260 os << '\\' << name();
262 // $,#, etc. In theory the restriction based on catcodes, but then
263 // we do not handle catcodes very well, let alone cat code changes,
264 // so being outside the alpha range is good enough.
265 if (name().size() == 1 && !isAlphaASCII(name()[0]))
268 os.pendingSpace(true);
272 void InsetMathSymbol::infoize2(odocstream & os) const
274 os << from_ascii("Symbol: ") << name();
278 void InsetMathSymbol::validate(LaTeXFeatures & features) const
280 // this is not really the ideal place to do this, but we can't
281 // validate in InsetMathExInt.
282 if (features.runparams().math_flavor == OutputParams::MathAsHTML
283 && sym_->name == from_ascii("int")) {
284 features.addCSSSnippet(
285 "span.limits{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
286 "span.limits span{display: block;}\n"
287 "span.intsym{font-size: 150%;}\n"
288 "sub.limit{font-size: 75%;}\n"
289 "sup.limit{font-size: 75%;}");
291 if (!sym_->requires.empty())
292 features.require(sym_->requires);