A bit more re-organization.
[lyx.git] / src / mathed / InsetMathSymbol.cpp
blob8a1ba2fd1b4656d763b9d3b88d8ec95ace61058b
1 /**
2 * \file InsetMathSymbol.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author André Pönitz
8 * Full author contact details are available in file CREDITS.
9 */
11 #include <config.h>
13 #include "InsetMathSymbol.h"
15 #include "Dimension.h"
16 #include "LaTeXFeatures.h"
17 #include "MathAtom.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"
27 namespace lyx {
29 InsetMathSymbol::InsetMathSymbol(latexkeys const * l)
30 : sym_(l), h_(0), scriptable_(false)
34 InsetMathSymbol::InsetMathSymbol(char const * name)
35 : sym_(in_word_set(from_ascii(name))), h_(0), scriptable_(false)
39 InsetMathSymbol::InsetMathSymbol(docstring const & name)
40 : sym_(in_word_set(name)), h_(0), scriptable_(false)
44 Inset * InsetMathSymbol::clone() const
46 return new InsetMathSymbol(*this);
50 docstring InsetMathSymbol::name() const
52 return sym_->name;
56 void InsetMathSymbol::metrics(MetricsInfo & mi, Dimension & dim) const
58 //lyxerr << "metrics: symbol: '" << sym_->name
59 // << "' in font: '" << sym_->inset
60 // << "' drawn as: '" << sym_->draw
61 // << "'" << endl;
63 int const em = mathed_char_width(mi.base.font, 'M');
64 FontSetChanger dummy(mi.base, sym_->inset);
65 mathed_string_dim(mi.base.font, sym_->draw, dim);
66 docstring::const_reverse_iterator rit = sym_->draw.rbegin();
67 kerning_ = mathed_char_kerning(mi.base.font, *rit);
68 // correct height for broken cmex and wasy font
69 if (sym_->inset == "cmex" || sym_->inset == "wasy") {
70 h_ = 4 * dim.des / 5;
71 dim.asc += h_;
72 dim.des -= h_;
74 // seperate things a bit
75 if (isRelOp())
76 dim.wid += static_cast<int>(0.5 * em + 0.5);
77 else
78 dim.wid += static_cast<int>(0.1667 * em + 0.5);
80 scriptable_ = false;
81 if (mi.base.style == LM_ST_DISPLAY)
82 if (sym_->inset == "cmex" || sym_->inset == "esint" ||
83 sym_->extra == "funclim")
84 scriptable_ = true;
88 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
90 //lyxerr << "metrics: symbol: '" << sym_->name
91 // << "' in font: '" << sym_->inset
92 // << "' drawn as: '" << sym_->draw
93 // << "'" << endl;
94 int const em = mathed_char_width(pi.base.font, 'M');
95 if (isRelOp())
96 x += static_cast<int>(0.25*em+0.5);
97 else
98 x += static_cast<int>(0.0833*em+0.5);
100 FontSetChanger dummy(pi.base, sym_->inset.c_str());
101 pi.draw(x, y - h_, sym_->draw);
105 bool InsetMathSymbol::isRelOp() const
107 return sym_->extra == "mathrel";
111 bool InsetMathSymbol::isOrdAlpha() const
113 return sym_->extra == "mathord" || sym_->extra == "mathalpha";
117 bool InsetMathSymbol::isScriptable() const
119 return scriptable_;
123 bool InsetMathSymbol::takesLimits() const
125 return
126 sym_->inset == "cmex" ||
127 sym_->inset == "lyxboldsymb" ||
128 sym_->inset == "esint" ||
129 sym_->extra == "funclim";
133 void InsetMathSymbol::validate(LaTeXFeatures & features) const
135 if (!sym_->requires.empty())
136 features.require(to_utf8(sym_->requires));
140 void InsetMathSymbol::normalize(NormalStream & os) const
142 os << "[symbol " << name() << ']';
146 void InsetMathSymbol::maple(MapleStream & os) const
148 if (name() == "cdot")
149 os << '*';
150 else if (name() == "infty")
151 os << "infinity";
152 else
153 os << name();
156 void InsetMathSymbol::maxima(MaximaStream & os) const
158 if (name() == "cdot")
159 os << '*';
160 else if (name() == "infty")
161 os << "inf";
162 else if (name() == "pi")
163 os << "%pi";
164 else
165 os << name();
169 void InsetMathSymbol::mathematica(MathematicaStream & os) const
171 if ( name() == "pi") { os << "Pi"; return;}
172 if ( name() == "infty") { os << "Infinity"; return;}
173 if ( name() == "cdot") { os << '*'; return;}
174 os << name();
178 char const * MathMLtype(docstring const & s)
180 if (s == "mathop")
181 return "mo";
182 return "mi";
186 void InsetMathSymbol::mathmlize(MathStream & os) const
188 char const * type = MathMLtype(sym_->extra);
189 os << '<' << type << "> ";
190 if (sym_->xmlname == "x") // unknown so far
191 os << name();
192 else
193 os << sym_->xmlname;
194 os << " </" << type << '>';
198 void InsetMathSymbol::octave(OctaveStream & os) const
200 if (name() == "cdot")
201 os << '*';
202 else
203 os << name();
207 void InsetMathSymbol::write(WriteStream & os) const
209 MathEnsurer ensurer(os);
210 os << '\\' << name();
212 // $,#, etc. In theory the restriction based on catcodes, but then
213 // we do not handle catcodes very well, let alone cat code changes,
214 // so being outside the alpha range is good enough.
215 if (name().size() == 1 && !isAlphaASCII(name()[0]))
216 return;
218 os.pendingSpace(true);
222 void InsetMathSymbol::infoize2(odocstream & os) const
224 os << from_ascii("Symbol: ") << name();
228 } // namespace lyx