2 * \file InsetMathScript.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetMathScript.h"
15 #include "MathStream.h"
16 #include "MathSupport.h"
17 #include "InsetMathSymbol.h"
18 #include "InsetMathFont.h"
19 #include "DispatchResult.h"
22 #include "FuncRequest.h"
25 #include <boost/assert.hpp>
34 InsetMathScript::InsetMathScript()
35 : InsetMathNest(1), cell_1_is_up_(false), limits_(0)
39 InsetMathScript::InsetMathScript(bool up)
40 : InsetMathNest(2), cell_1_is_up_(up), limits_(0)
44 InsetMathScript::InsetMathScript(MathAtom const & at, bool up)
45 : InsetMathNest(2), cell_1_is_up_(up), limits_(0)
47 BOOST_ASSERT(nargs() >= 1);
48 cell(0).push_back(at);
52 Inset * InsetMathScript::clone() const
54 return new InsetMathScript(*this);
58 InsetMathScript const * InsetMathScript::asScriptInset() const
64 InsetMathScript * InsetMathScript::asScriptInset()
70 bool InsetMathScript::idxFirst(Cursor & cur) const
78 bool InsetMathScript::idxLast(Cursor & cur) const
81 cur.pos() = nuc().size();
86 MathData const & InsetMathScript::down() const
90 BOOST_ASSERT(nargs() > 1);
95 MathData & InsetMathScript::down()
99 BOOST_ASSERT(nargs() > 1);
104 MathData const & InsetMathScript::up() const
106 BOOST_ASSERT(nargs() > 1);
111 MathData & InsetMathScript::up()
113 BOOST_ASSERT(nargs() > 1);
118 void InsetMathScript::ensure(bool up)
121 // just nucleus so far
122 cells_.push_back(MathData());
124 } else if (nargs() == 2 && !has(up)) {
126 cells_.push_back(cell(1));
129 cells_.push_back(MathData());
135 MathData const & InsetMathScript::nuc() const
141 MathData & InsetMathScript::nuc()
149 bool isAlphaSymbol(MathAtom const & at)
151 if (at->asCharInset() ||
152 (at->asSymbolInset() &&
153 at->asSymbolInset()->isOrdAlpha()))
156 if (at->asFontInset()) {
157 MathData const & ar = at->asFontInset()->cell(0);
158 for (size_t i = 0; i < ar.size(); ++i) {
159 if (!(ar[i]->asCharInset() ||
160 (ar[i]->asSymbolInset() &&
161 ar[i]->asSymbolInset()->isOrdAlpha())))
172 int InsetMathScript::dy01(int asc, int des, int what) const
176 bool isCharBox = nuc().size() ? isAlphaSymbol(nuc().back()) : false;
178 dasc = down().ascent();
179 slevel = nuc().slevel();
180 int ascdrop = dasc - slevel;
181 int desdrop = isCharBox ? 0 : des + nuc().sshift();
182 int mindes = nuc().mindes();
183 des = max(desdrop, ascdrop);
184 des = max(mindes, des);
187 int minasc = nuc().minasc();
188 int ascdrop = isCharBox ? 0 : asc - up().mindes();
189 int udes = up().descent();
190 asc = udes + nuc().sshift();
191 asc = max(ascdrop, asc);
192 asc = max(minasc, asc);
194 int del = asc - udes - dasc;
195 if (del + des <= 2) {
196 int newdes = 2 - del;
197 del = slevel - asc + udes;
202 des = max(des, newdes);
206 return what ? asc : des;
210 int InsetMathScript::dy0() const
215 int des = down().ascent();
220 des = dy01(na, nd, 0);
226 int InsetMathScript::dy1() const
231 int asc = up().descent();
236 asc = dy01(na, nd, 1);
243 int InsetMathScript::dx0() const
245 BOOST_ASSERT(hasDown());
246 return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
250 int InsetMathScript::dx1() const
252 BOOST_ASSERT(hasUp());
253 return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid() + nker();
257 int InsetMathScript::dxx() const
259 return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
263 int InsetMathScript::nwid() const
265 return nuc().size() ? nuc().width() : 2;
269 int InsetMathScript::nasc() const
271 return nuc().size() ? nuc().ascent() : 5;
275 int InsetMathScript::ndes() const
277 return nuc().size() ? nuc().descent() : 0;
281 int InsetMathScript::nker() const
284 int kerning = nuc().kerning();
285 return kerning > 0 ? kerning : 0;
291 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
294 ScriptChanger dummy(mi.base);
303 dim.wid = max(dim.wid, up().width());
305 dim.wid = max(dim.wid, down().width());
308 dim.wid = max(dim.wid, nker() + up().width());
310 dim.wid = max(dim.wid, down().width());
315 int asc = dy1() + up().ascent();
316 dim.asc = max(na, asc);
321 int des = dy0() + down().descent();
322 dim.des = max(nd, des);
330 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
333 nuc().draw(pi, x + dxx(), y);
335 nuc().setXY(*pi.base.bv, x + dxx(), y);
336 if (editing(pi.base.bv))
337 pi.draw(x + dxx(), y, char_type('.'));
339 ScriptChanger dummy(pi.base);
341 up().draw(pi, x + dx1(), y - dy1());
343 down().draw(pi, x + dx0(), y + dy0());
344 drawMarkers(pi, x, y);
348 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
351 up().metricsT(mi, dim);
353 down().metricsT(mi, dim);
354 nuc().metricsT(mi, dim);
358 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
361 nuc().drawT(pain, x + dxx(), y);
363 up().drawT(pain, x + dx1(), y - dy1());
365 down().drawT(pain, x + dx0(), y + dy0());
370 bool InsetMathScript::hasLimits() const
378 // we can only display limits if the nucleus wants some
381 if (!nuc().back()->isScriptable())
384 if (nuc().back()->asSymbolInset()) {
385 // \intop is an alias for \int\limits, \ointop == \oint\limits
386 if (nuc().back()->asSymbolInset()->name().find(from_ascii("intop")) != string::npos)
388 // per default \int has limits beside the \int even in displayed formulas
389 if (nuc().back()->asSymbolInset()->name().find(from_ascii("int")) != string::npos)
393 // assume "real" limits for everything else
398 void InsetMathScript::removeScript(bool up)
401 if (up == cell_1_is_up_)
403 } else if (nargs() == 3) {
405 swap(cells_[1], cells_[2]);
406 cell_1_is_up_ = false;
408 cell_1_is_up_ = true;
415 bool InsetMathScript::has(bool up) const
417 return idxOfScript(up);
421 bool InsetMathScript::hasUp() const
423 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
424 //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl;
425 return idxOfScript(true);
429 bool InsetMathScript::hasDown() const
431 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
432 //lyxerr << "hasDown: " << bool(idxOfScript(false)) << endl;
433 return idxOfScript(false);
437 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
442 return (cell_1_is_up_ == up) ? 1 : 0;
451 bool InsetMathScript::idxRight(Cursor &) const
457 bool InsetMathScript::idxLeft(Cursor &) const
463 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
466 if (cur.idx() == 0) {
467 // don't go up/down if there is no cell in this direction
470 // go up/down only if in the last position
471 // or in the first position of something with displayed limits
472 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
473 cur.idx() = idxOfScript(up);
481 if (cur.idx() == idxOfScript(true)) {
482 // can't go further up
485 // otherwise go to last position in the nucleus
487 cur.pos() = cur.lastpos();
492 if (cur.idx() == idxOfScript(false)) {
493 // can't go further down
496 // otherwise go to last position in the nucleus
498 cur.pos() = cur.lastpos();
506 void InsetMathScript::write(WriteStream & os) const
510 //if (nuc().back()->takesLimits()) {
518 LYXERR(Debug::MATHED) << "suppressing {} when writing"
524 if (hasDown() /*&& down().size()*/)
525 os << "_{" << down() << '}';
527 if (hasUp() /*&& up().size()*/)
528 os << "^{" << up() << '}';
530 if (lock_ && !os.latex())
535 void InsetMathScript::normalize(NormalStream & os) const
537 bool d = hasDown() && down().size();
538 bool u = hasUp() && up().size();
553 os << down() << ' ' << up() << ']';
561 void InsetMathScript::maple(MapleStream & os) const
565 if (hasDown() && down().size())
566 os << '[' << down() << ']';
567 if (hasUp() && up().size())
568 os << "^(" << up() << ')';
572 void InsetMathScript::mathematica(MathematicaStream & os) const
574 bool d = hasDown() && down().size();
575 bool u = hasUp() && up().size();
579 os << "Subscript[" << nuc();
585 os << "^(" << up() << ')';
589 os << ',' << down() << ']';
594 void InsetMathScript::mathmlize(MathStream & os) const
596 bool d = hasDown() && down().size();
597 bool u = hasUp() && up().size();
600 os << MTag("msubsup");
612 os << down() << up() << ETag("msubsup");
614 os << up() << ETag("msup");
616 os << down() << ETag("msub");
620 void InsetMathScript::octave(OctaveStream & os) const
624 if (hasDown() && down().size())
625 os << '[' << down() << ']';
626 if (hasUp() && up().size())
627 os << "^(" << up() << ')';
631 void InsetMathScript::infoize(odocstream & os) const
637 void InsetMathScript::infoize2(odocstream & os) const
640 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
644 bool InsetMathScript::notifyCursorLeaves(Cursor & cur)
646 InsetMathNest::notifyCursorLeaves(cur);
648 //lyxerr << "InsetMathScript::notifyCursorLeaves: 1 " << cur << endl;
650 // remove empty scripts if possible
652 // Case of two scripts. In this case, 1 = super, 2 = sub
653 if (cur.idx() == 2 && cell(2).empty()) {
654 // must be a subscript...
655 recordUndoInset(cur);
658 } else if (cur.idx() == 1 && cell(1).empty()) {
659 // must be a superscript...
660 recordUndoInset(cur);
664 } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
665 // could be either subscript or super script
666 recordUndoInset(cur);
667 removeScript(cell_1_is_up_);
668 // Let the script inset commit suicide. This is
669 // modelled on Cursor.pullArg(), but tries not to
670 // invoke notifyCursorLeaves again and does not touch
671 // cur (since the top slice will be deleted
673 MathData ar = cell(0);
676 tmpcur.cell().erase(tmpcur.pos());
677 tmpcur.cell().insert(tmpcur.pos(), ar);
681 //lyxerr << "InsetMathScript::notifyCursorLeaves: 2 " << cur << endl;
686 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
688 //lyxerr << "InsetMathScript: request: " << cmd << std::endl;
690 if (cmd.action == LFUN_MATH_LIMITS) {
691 if (!cmd.argument().empty()) {
692 if (cmd.argument() == "limits")
694 else if (cmd.argument() == "nolimits")
698 } else if (limits_ == 0)
699 limits_ = hasLimits() ? -1 : 1;
705 InsetMathNest::doDispatch(cur, cmd);