5 #include "math_scriptinset.h"
6 #include "math_support.h"
7 #include "math_symbolinset.h"
8 #include "math_mathmlstream.h"
9 #include "support/LAssert.h"
16 MathScriptInset::MathScriptInset()
17 : MathNestInset(2), limits_(0)
24 MathScriptInset::MathScriptInset(bool up)
25 : MathNestInset(2), limits_(0)
32 MathInset * MathScriptInset::clone() const
34 return new MathScriptInset(*this);
38 MathScriptInset const * MathScriptInset::asScriptInset() const
44 MathScriptInset * MathScriptInset::asScriptInset()
50 MathXArray const & MathScriptInset::up() const
56 MathXArray const & MathScriptInset::down() const
62 MathXArray & MathScriptInset::up()
68 MathXArray & MathScriptInset::down()
74 void MathScriptInset::ensure(bool up)
80 int MathScriptInset::dy0(MathInset const * nuc) const
85 int des = down().ascent();
94 int MathScriptInset::dy1(MathInset const * nuc) const
99 int asc = up().descent();
104 asc = max(asc, mathed_char_ascent(font_, 'I'));
109 int MathScriptInset::dx0(MathInset const * nuc) const
111 lyx::Assert(hasDown());
112 return hasLimits(nuc) ? (width2(nuc) - down().width()) / 2 : nwid(nuc);
116 int MathScriptInset::dx1(MathInset const * nuc) const
118 lyx::Assert(hasUp());
119 return hasLimits(nuc) ? (width2(nuc) - up().width()) / 2 : nwid(nuc);
123 int MathScriptInset::dxx(MathInset const * nuc) const
125 //lyx::Assert(nuc());
126 return hasLimits(nuc) ? (width2(nuc) - nwid(nuc)) / 2 : 0;
130 void MathScriptInset::dimensions2(MathInset const * nuc, Dimension & dim) const
132 dim.a = dy1(nuc) + (hasUp() ? up().ascent() : 0);
133 dim.d = dy0(nuc) + (hasDown() ? down().descent() : 0);
138 int MathScriptInset::width2(MathInset const * nuc) const
141 if (hasLimits(nuc)) {
144 w = max(w, up().width());
146 w = max(w, down().width());
149 w = max(w, up().width());
151 w = max(w, down().width());
158 int MathScriptInset::nwid(MathInset const * nuc) const
160 return nuc ? nuc->width() : mathed_char_width(font_, '.');
164 int MathScriptInset::nasc(MathInset const * nuc) const
166 return nuc ? nuc->ascent() : mathed_char_ascent(font_, 'I');
170 int MathScriptInset::ndes(MathInset const * nuc) const
172 return nuc ? nuc->descent() : mathed_char_descent(font_, 'I');
176 void MathScriptInset::metrics(MathMetricsInfo & mi) const
182 void MathScriptInset::metrics(MathInset const * nuc, MathMetricsInfo & mi) const
186 MathNestInset::metrics(mi);
187 MathScriptChanger dummy(mi.base);
188 dimensions2(nuc, dim_);
192 void MathScriptInset::draw(MathPainterInfo & pi, int x, int y) const
194 //lyxerr << "unexpected call to MathScriptInset::draw()\n";
199 void MathScriptInset::draw(MathInset const * nuc, MathPainterInfo & pi,
203 nuc->draw(pi, x + dxx(nuc), y);
205 drawStr(pi, font_, x + dxx(nuc), y, ".");
207 MathScriptChanger dummy(pi.base);
209 up().draw(pi, x + dx1(nuc), y - dy1(nuc));
211 down().draw(pi, x + dx0(nuc), y + dy0(nuc));
215 void MathScriptInset::metricsT(TextMetricsInfo const & mi) const
221 void MathScriptInset::metricsT(MathInset const * nuc,
222 TextMetricsInfo const & mi) const
230 //ascent_ = ascent2(nuc);
231 //descent_ = descent2(nuc);
232 //width_ = width2(nuc);
236 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
238 //lyxerr << "unexpected call to MathScriptInset::draw()\n";
239 drawT(0, pain, x, y);
243 void MathScriptInset::drawT(MathInset const * nuc, TextPainter & pain,
247 nuc->drawT(pain, x + dxx(nuc), y);
249 up().drawT(pain, x + dx1(nuc), y - dy1(nuc));
251 down().drawT(pain, x + dx0(nuc), y + dy0(nuc));
256 bool MathScriptInset::hasLimits(MathInset const * nuc) const
264 // we can only display limits if the nucleus wants some
267 if (!nuc->isScriptable())
270 // per default \int has limits beside the \int even in displayed formulas
271 if (nuc->asSymbolInset())
272 if (nuc->asSymbolInset()->name().find("int") != string::npos)
275 // assume "real" limits for everything else
280 void MathScriptInset::removeEmptyScripts()
282 for (int i = 0; i <= 1; ++i)
283 if (script_[i] && cell(i).size() == 0) {
290 void MathScriptInset::removeScript(bool up)
297 bool MathScriptInset::has(bool up) const
303 bool MathScriptInset::empty() const
305 return !script_[0] && !script_[1];
309 bool MathScriptInset::hasUp() const
315 bool MathScriptInset::hasDown() const
321 bool MathScriptInset::idxRight(MathInset::idx_type &,
322 MathInset::pos_type &) const
328 bool MathScriptInset::idxLeft(MathInset::idx_type &,
329 MathInset::pos_type &) const
335 void MathScriptInset::write(WriteStream & os) const
337 //lyxerr << "unexpected call to MathScriptInset::write()\n";
342 void MathScriptInset::write2(MathInset const * nuc, WriteStream & os) const
346 if (nuc->takesLimits()) {
354 lyxerr[Debug::MATHED] << "suppressing {} when writing\n";
358 if (hasDown() && down().data().size())
359 os << "_{" << down().data() << '}';
361 if (hasUp() && up().data().size())
362 os << "^{" << up().data() << '}';
366 void MathScriptInset::normalize(NormalStream & os) const
368 //lyxerr << "unexpected call to MathScriptInset::normalize()\n";
373 void MathScriptInset::normalize2(MathInset const * nuc, NormalStream & os) const
375 bool d = hasDown() && down().data().size();
376 bool u = hasUp() && up().data().size();
389 os << down().data() << ']';
391 os << up().data() << ']';
395 void MathScriptInset::maplize2(MathInset const * nuc, MapleStream & os) const
399 if (hasDown() && down().data().size())
400 os << '[' << down().data() << ']';
401 if (hasUp() && up().data().size())
402 os << "^(" << up().data() << ')';
406 void MathScriptInset::mathematicize2(MathInset const * nuc, MathematicaStream & os) const
408 bool d = hasDown() && down().data().size();
409 bool u = hasUp() && up().data().size();
412 if (d) //subscript only if nuc !
413 os << "Subscript[" << nuc;
417 os << "^(" << up().data() << ")";
421 os << "," << down().data() << "]";
425 void MathScriptInset::mathmlize2(MathInset const * nuc, MathMLStream & os) const
427 bool d = hasDown() && down().data().size();
428 bool u = hasUp() && up().data().size();
431 os << MTag("msubsup");
443 os << down().data() << up().data() << ETag("msubsup");
445 os << up().data() << ETag("msup");
447 os << down().data() << ETag("msub");
451 void MathScriptInset::octavize2(MathInset const * nuc, OctaveStream & os) const
455 if (hasDown() && down().data().size())
456 os << '[' << down().data() << ']';
457 if (hasUp() && up().data().size())
458 os << "^(" << up().data() << ')';
462 void MathScriptInset::infoize(std::ostream & os) const
465 os << (limits_ == 1 ? "Displayed limits" : "Inlined limits");