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/lyxlib.h"
25 #include "support/textutils.h"
26 #include "support/unique_ptr.h"
31 InsetMathSymbol::InsetMathSymbol(latexkeys const * l)
32 : sym_(l), h_(0), kerning_(0), scriptable_(false)
36 InsetMathSymbol::InsetMathSymbol(char const * name)
37 : sym_(in_word_set(from_ascii(name))), h_(0),
38 kerning_(0), scriptable_(false)
42 InsetMathSymbol::InsetMathSymbol(docstring const & name)
43 : sym_(in_word_set(name)), h_(0), kerning_(0), scriptable_(false)
47 Inset * InsetMathSymbol::clone() const
49 return new InsetMathSymbol(*this);
53 docstring InsetMathSymbol::name() const
59 void InsetMathSymbol::metrics(MetricsInfo & mi, Dimension & dim) const
61 //lyxerr << "metrics: symbol: '" << sym_->name
62 // << "' in font: '" << sym_->inset
63 // << "' drawn as: '" << sym_->draw
66 bool const italic_upcase_greek = sym_->inset == "cmr" &&
67 sym_->extra == "mathalpha" &&
68 mi.base.fontname == "mathit";
69 std::string const font = italic_upcase_greek ? "cmm" : sym_->inset;
70 Changer dummy = mi.base.changeFontSet(from_ascii(font));
71 mathed_string_dim(mi.base.font, sym_->draw, dim);
72 docstring::const_reverse_iterator rit = sym_->draw.rbegin();
73 kerning_ = mathed_char_kerning(mi.base.font, *rit);
74 // correct height for broken cmex and wasy font
75 if (sym_->inset == "cmex" || sym_->inset == "wasy") {
80 // seperate things a bit
82 dim.wid += 2 * mathed_medmuskip(mi.base.font);
84 dim.wid += 2 * mathed_thickmuskip(mi.base.font);
85 else if (isMathPunct())
86 dim.wid += mathed_thinmuskip(mi.base.font);
87 // FIXME: I see no reason for this
89 // dim.wid += support::iround(0.1667 * em);
92 if (mi.base.style == LM_ST_DISPLAY)
93 if (sym_->inset == "cmex" || sym_->inset == "esint" ||
94 sym_->extra == "funclim" ||
95 (sym_->inset == "stmry" && sym_->extra == "mathop"))
100 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
102 //lyxerr << "metrics: symbol: '" << sym_->name
103 // << "' in font: '" << sym_->inset
104 // << "' drawn as: '" << sym_->draw
107 bool const italic_upcase_greek = sym_->inset == "cmr" &&
108 sym_->extra == "mathalpha" &&
109 pi.base.fontname == "mathit";
110 std::string const font = italic_upcase_greek ? "cmm" : sym_->inset;
112 x += mathed_medmuskip(pi.base.font);
113 else if (isMathRel())
114 x += mathed_thickmuskip(pi.base.font);
115 // FIXME: I see no reason for this
117 // x += support::iround(0.0833 * em);
119 Changer dummy = pi.base.changeFontSet(from_ascii(font));
120 pi.draw(x, y - h_, sym_->draw);
124 InsetMath::mode_type InsetMathSymbol::currentMode() const
126 return sym_->extra == "textmode" ? TEXT_MODE : MATH_MODE;
130 bool InsetMathSymbol::isMathBin() const
132 return sym_->extra == "mathbin";
136 bool InsetMathSymbol::isMathRel() const
138 return sym_->extra == "mathrel";
142 bool InsetMathSymbol::isMathPunct() const
144 return sym_->extra == "mathpunct";
148 bool InsetMathSymbol::isOrdAlpha() const
150 return sym_->extra == "mathord" || sym_->extra == "mathalpha";
154 bool InsetMathSymbol::isScriptable() const
160 bool InsetMathSymbol::takesLimits() const
163 sym_->inset == "cmex" ||
164 sym_->inset == "lyxboldsymb" ||
165 sym_->inset == "esint" ||
166 sym_->extra == "funclim" ||
167 (sym_->inset == "stmry" && sym_->extra == "mathop");
171 void InsetMathSymbol::normalize(NormalStream & os) const
173 os << "[symbol " << name() << ']';
177 void InsetMathSymbol::maple(MapleStream & os) const
179 if (name() == "cdot")
181 else if (name() == "infty")
187 void InsetMathSymbol::maxima(MaximaStream & os) const
189 if (name() == "cdot")
191 else if (name() == "infty")
193 else if (name() == "pi")
200 void InsetMathSymbol::mathematica(MathematicaStream & os) const
202 if ( name() == "pi") { os << "Pi"; return;}
203 if ( name() == "infty") { os << "Infinity"; return;}
204 if ( name() == "cdot") { os << '*'; return;}
209 void InsetMathSymbol::mathmlize(MathStream & os) const
211 // FIXME We may need to do more interesting things
213 char const * type = sym_->MathMLtype();
214 os << '<' << type << "> ";
215 if (sym_->xmlname == "x")
220 os << " </" << type << '>';
224 void InsetMathSymbol::htmlize(HtmlStream & os, bool spacing) const
226 // FIXME We may need to do more interesting things
228 char const * type = sym_->MathMLtype();
229 bool op = (std::string(type) == "mo");
231 if (sym_->xmlname == "x")
233 os << ' ' << name() << ' ';
234 else if (op && spacing)
235 os << ' ' << sym_->xmlname << ' ';
241 void InsetMathSymbol::htmlize(HtmlStream & os) const
247 void InsetMathSymbol::octave(OctaveStream & os) const
249 if (name() == "cdot")
256 void InsetMathSymbol::write(WriteStream & os) const
258 unique_ptr<MathEnsurer> ensurer;
259 if (currentMode() != TEXT_MODE)
260 ensurer = make_unique<MathEnsurer>(os);
261 os << '\\' << name();
263 // $,#, etc. In theory the restriction based on catcodes, but then
264 // we do not handle catcodes very well, let alone cat code changes,
265 // so being outside the alpha range is good enough.
266 if (name().size() == 1 && !isAlphaASCII(name()[0]))
269 os.pendingSpace(true);
273 void InsetMathSymbol::infoize2(odocstream & os) const
275 os << from_ascii("Symbol: ") << name();
279 void InsetMathSymbol::validate(LaTeXFeatures & features) const
281 // this is not really the ideal place to do this, but we can't
282 // validate in InsetMathExInt.
283 if (features.runparams().math_flavor == OutputParams::MathAsHTML
284 && sym_->name == from_ascii("int")) {
285 features.addCSSSnippet(
286 "span.limits{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
287 "span.limits span{display: block;}\n"
288 "span.intsym{font-size: 150%;}\n"
289 "sub.limit{font-size: 75%;}\n"
290 "sup.limit{font-size: 75%;}");
292 if (!sym_->requires.empty())
293 features.require(sym_->requires);