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(BufferView const & bv, int asc, int des, int what) const
176 bool isCharBox = nuc().size() ? isAlphaSymbol(nuc().back()) : false;
178 Dimension const & dimdown = down().dimension(bv);
179 dasc = dimdown.ascent();
180 slevel = nuc().slevel();
181 int ascdrop = dasc - slevel;
182 int desdrop = isCharBox ? 0 : des + nuc().sshift();
183 int mindes = nuc().mindes();
184 des = max(desdrop, ascdrop);
185 des = max(mindes, des);
188 Dimension const & dimup = up().dimension(bv);
189 int minasc = nuc().minasc();
190 int ascdrop = isCharBox ? 0 : asc - up().mindes();
191 int udes = dimup.descent();
192 asc = udes + nuc().sshift();
193 asc = max(ascdrop, asc);
194 asc = max(minasc, asc);
196 int del = asc - udes - dasc;
197 if (del + des <= 2) {
198 int newdes = 2 - del;
199 del = slevel - asc + udes;
204 des = max(des, newdes);
208 return what ? asc : des;
212 int InsetMathScript::dy0(BufferView const & bv) const
217 int des = down().dimension(bv).ascent();
222 des = dy01(bv, na, nd, 0);
228 int InsetMathScript::dy1(BufferView const & bv) const
233 int asc = up().dimension(bv).descent();
238 asc = dy01(bv, na, nd, 1);
245 int InsetMathScript::dx0(BufferView const & bv) const
247 BOOST_ASSERT(hasDown());
248 Dimension const dim = dimension(bv);
249 return hasLimits() ? (dim.wid - down().dimension(bv).width()) / 2 : nwid(bv);
253 int InsetMathScript::dx1(BufferView const & bv) const
255 BOOST_ASSERT(hasUp());
256 Dimension const dim = dimension(bv);
257 return hasLimits() ? (dim.wid - up().dimension(bv).width()) / 2 : nwid(bv) + nker();
261 int InsetMathScript::dxx(BufferView const & bv) const
263 Dimension const dim = dimension(bv);
264 return hasLimits() ? (dim.wid - nwid(bv)) / 2 : 0;
268 int InsetMathScript::nwid(BufferView const & bv) const
270 return nuc().size() ? nuc().dimension(bv).width() : 2;
274 int InsetMathScript::nasc(BufferView const & bv) const
276 return nuc().size() ? nuc().dimension(bv).ascent() : 5;
280 int InsetMathScript::ndes(BufferView const & bv) const
282 return nuc().size() ? nuc().dimension(bv).descent() : 0;
286 int InsetMathScript::nker() const
289 int kerning = nuc().kerning();
290 return kerning > 0 ? kerning : 0;
296 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
301 cell(0).metrics(mi, dim0);
302 ScriptChanger dummy(mi.base);
304 cell(1).metrics(mi, dim1);
306 cell(2).metrics(mi, dim2);
309 BufferView & bv = *mi.base.bv;
310 // FIXME: data copying... not very efficient.
314 dimup = up().dimension(bv);
316 dimdown = down().dimension(bv);
321 dim.wid = max(dim.wid, dimup.width());
323 dim.wid = max(dim.wid, dimdown.width());
326 dim.wid = max(dim.wid, nker() + dimup.width());
328 dim.wid = max(dim.wid, dimdown.width());
333 int asc = dy1(bv) + dimup.ascent();
334 dim.asc = max(na, asc);
339 int des = dy0(bv) + dimdown.descent();
340 dim.des = max(nd, des);
344 // Cache the inset dimension.
345 setDimCache(mi, dim);
349 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
351 BufferView & bv = *pi.base.bv;
353 nuc().draw(pi, x + dxx(bv), y);
355 nuc().setXY(bv, x + dxx(bv), y);
357 pi.draw(x + dxx(bv), y, char_type('.'));
359 ScriptChanger dummy(pi.base);
361 up().draw(pi, x + dx1(bv), y - dy1(bv));
363 down().draw(pi, x + dx0(bv), y + dy0(bv));
364 drawMarkers(pi, x, y);
368 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
371 up().metricsT(mi, dim);
373 down().metricsT(mi, dim);
374 nuc().metricsT(mi, dim);
378 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
382 nuc().drawT(pain, x + 1, y);
384 up().drawT(pain, x + 1, y - 1 /*dy1()*/);
386 down().drawT(pain, x + 1, y + 1 /*dy0()*/);
391 bool InsetMathScript::hasLimits() const
399 // we can only display limits if the nucleus wants some
402 if (!nuc().back()->isScriptable())
405 if (nuc().back()->asSymbolInset()) {
406 // \intop is an alias for \int\limits, \ointop == \oint\limits
407 if (nuc().back()->asSymbolInset()->name().find(from_ascii("intop")) != string::npos)
409 // per default \int has limits beside the \int even in displayed formulas
410 if (nuc().back()->asSymbolInset()->name().find(from_ascii("int")) != string::npos)
414 // assume "real" limits for everything else
419 void InsetMathScript::removeScript(bool up)
422 if (up == cell_1_is_up_)
424 } else if (nargs() == 3) {
426 swap(cells_[1], cells_[2]);
427 cell_1_is_up_ = false;
429 cell_1_is_up_ = true;
436 bool InsetMathScript::has(bool up) const
438 return idxOfScript(up);
442 bool InsetMathScript::hasUp() const
444 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
445 //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl;
446 return idxOfScript(true);
450 bool InsetMathScript::hasDown() const
452 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
453 //lyxerr << "hasDown: " << bool(idxOfScript(false)) << endl;
454 return idxOfScript(false);
458 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
463 return (cell_1_is_up_ == up) ? 1 : 0;
472 bool InsetMathScript::idxRight(Cursor &) const
478 bool InsetMathScript::idxLeft(Cursor &) const
484 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
487 if (cur.idx() == 0) {
488 // don't go up/down if there is no cell in this direction
491 // go up/down only if in the last position
492 // or in the first position of something with displayed limits
493 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
494 cur.idx() = idxOfScript(up);
502 if (cur.idx() == idxOfScript(true)) {
503 // can't go further up
506 // otherwise go to last position in the nucleus
508 cur.pos() = cur.lastpos();
513 if (cur.idx() == idxOfScript(false)) {
514 // can't go further down
517 // otherwise go to last position in the nucleus
519 cur.pos() = cur.lastpos();
527 void InsetMathScript::write(WriteStream & os) const
531 //if (nuc().back()->takesLimits()) {
539 LYXERR(Debug::MATHED) << "suppressing {} when writing"
545 if (hasDown() /*&& down().size()*/)
546 os << "_{" << down() << '}';
548 if (hasUp() /*&& up().size()*/)
549 os << "^{" << up() << '}';
551 if (lock_ && !os.latex())
556 void InsetMathScript::normalize(NormalStream & os) const
558 bool d = hasDown() && down().size();
559 bool u = hasUp() && up().size();
574 os << down() << ' ' << up() << ']';
582 void InsetMathScript::maple(MapleStream & os) const
586 if (hasDown() && down().size())
587 os << '[' << down() << ']';
588 if (hasUp() && up().size())
589 os << "^(" << up() << ')';
593 void InsetMathScript::mathematica(MathematicaStream & os) const
595 bool d = hasDown() && down().size();
596 bool u = hasUp() && up().size();
600 os << "Subscript[" << nuc();
606 os << "^(" << up() << ')';
610 os << ',' << down() << ']';
615 void InsetMathScript::mathmlize(MathStream & os) const
617 bool d = hasDown() && down().size();
618 bool u = hasUp() && up().size();
621 os << MTag("msubsup");
633 os << down() << up() << ETag("msubsup");
635 os << up() << ETag("msup");
637 os << down() << ETag("msub");
641 void InsetMathScript::octave(OctaveStream & os) const
645 if (hasDown() && down().size())
646 os << '[' << down() << ']';
647 if (hasUp() && up().size())
648 os << "^(" << up() << ')';
652 void InsetMathScript::infoize(odocstream & os) const
658 void InsetMathScript::infoize2(odocstream & os) const
661 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
665 bool InsetMathScript::notifyCursorLeaves(Cursor & cur)
667 InsetMathNest::notifyCursorLeaves(cur);
669 //lyxerr << "InsetMathScript::notifyCursorLeaves: 1 " << cur << endl;
671 // remove empty scripts if possible
673 // Case of two scripts. In this case, 1 = super, 2 = sub
674 if (cur.idx() == 2 && cell(2).empty()) {
675 // must be a subscript...
676 recordUndoInset(cur);
679 } else if (cur.idx() == 1 && cell(1).empty()) {
680 // must be a superscript...
681 recordUndoInset(cur);
685 } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
686 // could be either subscript or super script
687 recordUndoInset(cur);
688 removeScript(cell_1_is_up_);
689 // Let the script inset commit suicide. This is
690 // modelled on Cursor.pullArg(), but tries not to
691 // invoke notifyCursorLeaves again and does not touch
692 // cur (since the top slice will be deleted
694 MathData ar = cell(0);
697 tmpcur.cell().erase(tmpcur.pos());
698 tmpcur.cell().insert(tmpcur.pos(), ar);
702 //lyxerr << "InsetMathScript::notifyCursorLeaves: 2 " << cur << endl;
707 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
709 //lyxerr << "InsetMathScript: request: " << cmd << std::endl;
711 if (cmd.action == LFUN_MATH_LIMITS) {
712 if (!cmd.argument().empty()) {
713 if (cmd.argument() == "limits")
715 else if (cmd.argument() == "nolimits")
719 } else if (limits_ == 0)
720 limits_ = hasLimits() ? -1 : 1;
726 InsetMathNest::doDispatch(cur, cmd);