7 #include "support/LOstream.h"
8 #include "support/LAssert.h"
11 #pragma implementation
14 #include "math_scriptinset.h"
17 using std::ostringstream;
20 MathScriptInset::MathScriptInset()
21 : MathNestInset(2), limits_(0)
28 MathScriptInset::MathScriptInset(bool up)
29 : MathNestInset(2), limits_(0)
36 MathInset * MathScriptInset::clone() const
38 return new MathScriptInset(*this);
42 MathScriptInset const * MathScriptInset::asScriptInset() const
48 MathScriptInset * MathScriptInset::asScriptInset()
54 MathXArray const & MathScriptInset::up() const
60 MathXArray const & MathScriptInset::down() const
66 MathXArray & MathScriptInset::up()
72 MathXArray & MathScriptInset::down()
78 void MathScriptInset::ensure(bool up)
84 int MathScriptInset::dy0(MathInset const * nuc) const
89 int des = down().ascent();
93 des = std::max(des, nd);
98 int MathScriptInset::dy1(MathInset const * nuc) const
103 int asc = up().descent();
107 asc = std::max(asc, na);
108 asc = std::max(asc, mathed_char_ascent(LM_TC_VAR, mi_, 'I'));
113 int MathScriptInset::dx0(MathInset const * nuc) const
115 lyx::Assert(hasDown());
116 return hasLimits(nuc) ? (width(nuc) - down().width()) / 2 : nwid(nuc);
120 int MathScriptInset::dx1(MathInset const * nuc) const
122 lyx::Assert(hasUp());
123 return hasLimits(nuc) ? (width(nuc) - up().width()) / 2 : nwid(nuc);
127 int MathScriptInset::dxx(MathInset const * nuc) const
129 //lyx::Assert(nuc());
130 return hasLimits(nuc) ? (width(nuc) - nwid(nuc)) / 2 : 0;
134 int MathScriptInset::ascent(MathInset const * nuc) const
136 return dy1(nuc) + (hasUp() ? up().ascent() : 0);
140 int MathScriptInset::descent(MathInset const * nuc) const
142 return dy0(nuc) + (hasDown() ? down().descent() : 0);
146 int MathScriptInset::width(MathInset const * nuc) const
149 if (hasLimits(nuc)) {
152 wid = std::max(wid, up().width());
154 wid = std::max(wid, down().width());
157 wid = std::max(wid, up().width());
159 wid = std::max(wid, down().width());
166 int MathScriptInset::nwid(MathInset const * nuc) const
170 mathed_char_width(LM_TC_TEX, mi_, '.');
174 int MathScriptInset::nasc(MathInset const * nuc) const
176 return nuc ? nuc->ascent()
177 : mathed_char_ascent(LM_TC_VAR, mi_, 'I');
181 int MathScriptInset::ndes(MathInset const * nuc) const
183 return nuc ? nuc->descent()
184 : mathed_char_descent(LM_TC_VAR, mi_, 'I');
188 void MathScriptInset::metrics(MathMetricsInfo const & mi) const
194 void MathScriptInset::metrics(MathInset const * nuc,
195 MathMetricsInfo const & mi) const
197 MathNestInset::metrics(mi);
201 ascent_ = ascent(nuc);
202 descent_ = descent(nuc);
207 void MathScriptInset::draw(Painter & pain, int x, int y) const
209 //lyxerr << "unexpected call to MathScriptInset::draw()\n";
214 void MathScriptInset::draw(MathInset const * nuc, Painter & pain,
218 nuc->draw(pain, x + dxx(nuc), y);
220 drawStr(pain, LM_TC_TEX, mi_, x + dxx(nuc), y, ".");
223 up().draw(pain, x + dx1(nuc), y - dy1(nuc));
226 down().draw(pain, x + dx0(nuc), y + dy0(nuc));
230 void MathScriptInset::write(MathWriteInfo & os) const
232 //lyxerr << "unexpected call to MathScriptInset::write()\n";
237 void MathScriptInset::write(MathInset const * nuc, MathWriteInfo & os) const
241 if (nuc->takesLimits()) {
251 if (hasDown() && down().data_.size())
252 os << "_{" << down().data_ << '}';
254 if (hasUp() && up().data_.size())
255 os << "^{" << up().data_ << '}';
259 void MathScriptInset::writeNormal(ostream & os) const
261 //lyxerr << "unexpected call to MathScriptInset::writeNormal()\n";
266 void MathScriptInset::writeNormal(MathInset const * nuc, ostream & os) const
268 bool d = hasDown() && down().data_.size();
269 bool u = hasUp() && up().data_.size();
273 nuc->writeNormal(osb);
276 string base = osb.str();
279 os << "[sup [sub " << osb.str() << " ";
280 down().data_.writeNormal(os);
282 up().data_.writeNormal(os);
285 os << "[sup " << osb.str() << " ";
286 up().data_.writeNormal(os);
289 os << "[sub " << osb.str() << " ";
290 down().data_.writeNormal(os);
296 bool MathScriptInset::hasLimits(MathInset const * nuc) const
298 return limits_ == 1 || (limits_ == 0 && nuc && nuc->isScriptable());
302 void MathScriptInset::removeEmptyScripts()
304 for (int i = 0; i <= 1; ++i)
305 if (script_[i] && !cell(i).size())
310 void MathScriptInset::removeScript(bool up)
317 bool MathScriptInset::has(bool up) const
323 bool MathScriptInset::hasUp() const
329 bool MathScriptInset::hasDown() const
335 bool MathScriptInset::idxRight(MathInset::idx_type &,
336 MathInset::pos_type &) const
342 bool MathScriptInset::idxLeft(MathInset::idx_type &,
343 MathInset::pos_type &) const
349 string MathScriptInset::maplize(MathInset const * nuc) const
353 res += nuc->maplize();
354 if (hasDown() && down().data_.size())
355 res += "[" + down().data_.maplize() + "]";
356 if (hasUp() && up().data_.size())
357 res += "^(" + up().data_.maplize() + ")";
362 string MathScriptInset::octavize(MathInset const * nuc) const
368 string MathScriptInset::mathmlize(MathInset const * nuc) const
371 return nuc->mathmlize();