7 #include "support/LOstream.h"
8 #include "support/LAssert.h"
11 #pragma implementation
14 #include "math_scriptinset.h"
16 using std::ostringstream;
19 MathScriptInset::MathScriptInset()
20 : MathNestInset(2), limits_(0)
27 MathScriptInset::MathScriptInset(bool up)
28 : MathNestInset(2), limits_(0)
35 MathInset * MathScriptInset::clone() const
37 return new MathScriptInset(*this);
41 MathScriptInset const * MathScriptInset::asScriptInset() const
47 MathScriptInset * MathScriptInset::asScriptInset()
53 MathXArray const & MathScriptInset::up() const
59 MathXArray const & MathScriptInset::down() const
65 MathXArray & MathScriptInset::up()
71 MathXArray & MathScriptInset::down()
77 void MathScriptInset::ensure(bool up)
83 int MathScriptInset::dy0(MathInset const * nuc) const
88 int des = down().ascent();
92 des = std::max(des, nd);
97 int MathScriptInset::dy1(MathInset const * nuc) const
102 int asc = up().descent();
106 asc = std::max(asc, na);
107 asc = std::max(asc, mathed_char_ascent(LM_TC_VAR, mi_, 'I'));
112 int MathScriptInset::dx0(MathInset const * nuc) const
114 lyx::Assert(hasDown());
115 return hasLimits(nuc) ? (width(nuc) - down().width()) / 2 : nwid(nuc);
119 int MathScriptInset::dx1(MathInset const * nuc) const
121 lyx::Assert(hasUp());
122 return hasLimits(nuc) ? (width(nuc) - up().width()) / 2 : nwid(nuc);
126 int MathScriptInset::dxx(MathInset const * nuc) const
128 //lyx::Assert(nuc());
129 return hasLimits(nuc) ? (width(nuc) - nwid(nuc)) / 2 : 0;
133 int MathScriptInset::ascent(MathInset const * nuc) const
135 return dy1(nuc) + (hasUp() ? up().ascent() : 0);
139 int MathScriptInset::descent(MathInset const * nuc) const
141 return dy0(nuc) + (hasDown() ? down().descent() : 0);
145 int MathScriptInset::width(MathInset const * nuc) const
148 if (hasLimits(nuc)) {
151 wid = std::max(wid, up().width());
153 wid = std::max(wid, down().width());
156 wid = std::max(wid, up().width());
158 wid = std::max(wid, down().width());
165 int MathScriptInset::nwid(MathInset const * nuc) const
169 mathed_char_width(LM_TC_TEX, mi_, '.');
173 int MathScriptInset::nasc(MathInset const * nuc) const
175 return nuc ? nuc->ascent()
176 : mathed_char_ascent(LM_TC_VAR, mi_, 'I');
180 int MathScriptInset::ndes(MathInset const * nuc) const
182 return nuc ? nuc->descent()
183 : mathed_char_descent(LM_TC_VAR, mi_, 'I');
187 void MathScriptInset::metrics(MathMetricsInfo const & mi) const
193 void MathScriptInset::metrics(MathInset const * nuc,
194 MathMetricsInfo const & mi) const
196 MathNestInset::metrics(mi);
200 ascent_ = ascent(nuc);
201 descent_ = descent(nuc);
206 void MathScriptInset::draw(Painter & pain, int x, int y) const
208 //lyxerr << "unexpected call to MathScriptInset::draw()\n";
213 void MathScriptInset::draw(MathInset const * nuc, Painter & pain,
217 nuc->draw(pain, x + dxx(nuc), y);
219 drawStr(pain, LM_TC_TEX, mi_, x + dxx(nuc), y, ".");
222 up().draw(pain, x + dx1(nuc), y - dy1(nuc));
225 down().draw(pain, x + dx0(nuc), y + dy0(nuc));
229 void MathScriptInset::write(MathWriteInfo & os) const
231 //lyxerr << "unexpected call to MathScriptInset::write()\n";
236 void MathScriptInset::write(MathInset const * nuc, MathWriteInfo & os) const
240 if (nuc->takesLimits()) {
250 if (hasDown() && down().data_.size())
251 os << "_{" << down().data_ << '}';
253 if (hasUp() && up().data_.size())
254 os << "^{" << up().data_ << '}';
258 void MathScriptInset::writeNormal(ostream & os) const
260 //lyxerr << "unexpected call to MathScriptInset::writeNormal()\n";
265 void MathScriptInset::writeNormal(MathInset const * nuc, ostream & os) const
267 bool d = hasDown() && down().data_.size();
268 bool u = hasUp() && up().data_.size();
272 nuc->writeNormal(osb);
275 string base = osb.str();
278 os << "[sup [sub " << osb.str() << " ";
279 down().data_.writeNormal(os);
281 up().data_.writeNormal(os);
284 os << "[sup " << osb.str() << " ";
285 up().data_.writeNormal(os);
288 os << "[sub " << osb.str() << " ";
289 down().data_.writeNormal(os);
295 bool MathScriptInset::hasLimits(MathInset const * nuc) const
297 return limits_ == 1 || (limits_ == 0 && nuc && nuc->isScriptable());
301 void MathScriptInset::removeEmptyScripts()
303 for (int i = 0; i <= 1; ++i)
304 if (script_[i] && !cell(i).size())
309 void MathScriptInset::removeScript(bool up)
316 bool MathScriptInset::has(bool up) const
322 bool MathScriptInset::hasUp() const
328 bool MathScriptInset::hasDown() const
334 bool MathScriptInset::idxRight(MathInset::idx_type &,
335 MathInset::pos_type &) const
341 bool MathScriptInset::idxLeft(MathInset::idx_type &,
342 MathInset::pos_type &) const