+ if (nargs() == 3)
+ return cell(2);
+ BOOST_ASSERT(nargs() > 1);
+ return cell(1);
+}
+
+
+MathArray & MathScriptInset::down()
+{
+ if (nargs() == 3)
+ return cell(2);
+ BOOST_ASSERT(nargs() > 1);
+ return cell(1);
+}
+
+
+MathArray const & MathScriptInset::up() const
+{
+ BOOST_ASSERT(nargs() > 1);
+ return cell(1);
+}
+
+
+MathArray & MathScriptInset::up()
+{
+ BOOST_ASSERT(nargs() > 1);
+ return cell(1);
+}
+
+
+void MathScriptInset::ensure(bool up)
+{
+ if (nargs() == 1) {
+ // just nucleus so far
+ cells_.push_back(MathArray());
+ cell_1_is_up_ = up;
+ } else if (nargs() == 2 && !has(up)) {
+ if (up) {
+ cells_.push_back(cell(1));
+ cell(1).clear();
+ } else {
+ cells_.push_back(MathArray());
+ }
+ }
+}
+
+
+MathArray const & MathScriptInset::nuc() const
+{
+ return cell(0);
+}
+
+
+MathArray & MathScriptInset::nuc()
+{
+ return cell(0);
+}
+
+
+int MathScriptInset::dy0() const
+{
+ int nd = ndes();
+ if (!hasDown())
+ return nd;
+ int des = down().ascent();
+ if (hasLimits())
+ des += nd + 2;
+ else
+ des = max(des, nd);
+ return des;
+}
+
+
+int MathScriptInset::dy1() const
+{
+ int na = nasc();
+ if (!hasUp())
+ return na;
+ int asc = up().descent();
+ if (hasLimits())
+ asc += na + 2;
+ else
+ asc = max(asc, na);
+ asc = max(asc, 5);
+ return asc;
+}
+
+
+int MathScriptInset::dx0() const
+{
+ BOOST_ASSERT(hasDown());
+ return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
+}
+
+
+int MathScriptInset::dx1() const
+{
+ BOOST_ASSERT(hasUp());
+ return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
+}
+
+
+int MathScriptInset::dxx() const
+{
+ return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
+}
+
+
+int MathScriptInset::nwid() const
+{
+ return nuc().size() ? nuc().width() : 2;
+}
+
+
+int MathScriptInset::nasc() const
+{
+ return nuc().size() ? nuc().ascent() : 5;
+}
+
+
+int MathScriptInset::ndes() const
+{
+ return nuc().size() ? nuc().descent() : 0;
+}
+
+
+void MathScriptInset::metrics(MetricsInfo & mi, Dimension & dim) const
+{
+ cell(0).metrics(mi);
+ ScriptChanger dummy(mi.base);
+ if (nargs() > 1)
+ cell(1).metrics(mi);
+ if (nargs() > 2)
+ cell(2).metrics(mi);
+ dim.wid = 0;
+ if (hasLimits()) {
+ dim.wid = nwid();
+ if (hasUp())
+ dim.wid = max(dim.wid, up().width());
+ if (hasDown())
+ dim.wid = max(dim.wid, down().width());
+ } else {
+ if (hasUp())
+ dim.wid = max(dim.wid, up().width());
+ if (hasDown())
+ dim.wid = max(dim.wid, down().width());
+ dim.wid += nwid();
+ }
+ dim.asc = dy1() + (hasUp() ? up().ascent() : 0);
+ dim.des = dy0() + (hasDown() ? down().descent() : 0);
+ metricsMarkers(dim);
+ dim_ = dim;
+}
+
+
+void MathScriptInset::draw(PainterInfo & pi, int x, int y) const
+{
+ if (nuc().size())
+ nuc().draw(pi, x + dxx(), y);
+ else {
+ nuc().setXY(x + dxx(), y);
+ if (editing(pi.base.bv))
+ drawStr(pi, pi.base.font, x + dxx(), y, ".");
+ }
+ ScriptChanger dummy(pi.base);
+ if (hasUp())
+ up().draw(pi, x + dx1(), y - dy1());
+ if (hasDown())
+ down().draw(pi, x + dx0(), y + dy0());
+ drawMarkers(pi, x, y);