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 bool 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);
333 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
336 nuc().draw(pi, x + dxx(), y);
338 nuc().setXY(*pi.base.bv, x + dxx(), y);
339 if (editing(pi.base.bv))
340 pi.draw(x + dxx(), y, char_type('.'));
342 ScriptChanger dummy(pi.base);
344 up().draw(pi, x + dx1(), y - dy1());
346 down().draw(pi, x + dx0(), y + dy0());
347 drawMarkers(pi, x, y);
351 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
354 up().metricsT(mi, dim);
356 down().metricsT(mi, dim);
357 nuc().metricsT(mi, dim);
361 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
364 nuc().drawT(pain, x + dxx(), y);
366 up().drawT(pain, x + dx1(), y - dy1());
368 down().drawT(pain, x + dx0(), y + dy0());
373 bool InsetMathScript::hasLimits() const
381 // we can only display limits if the nucleus wants some
384 if (!nuc().back()->isScriptable())
387 if (nuc().back()->asSymbolInset()) {
388 // \intop is an alias for \int\limits, \ointop == \oint\limits
389 if (nuc().back()->asSymbolInset()->name().find(from_ascii("intop")) != string::npos)
391 // per default \int has limits beside the \int even in displayed formulas
392 if (nuc().back()->asSymbolInset()->name().find(from_ascii("int")) != string::npos)
396 // assume "real" limits for everything else
401 void InsetMathScript::removeScript(bool up)
404 if (up == cell_1_is_up_)
406 } else if (nargs() == 3) {
408 swap(cells_[1], cells_[2]);
409 cell_1_is_up_ = false;
411 cell_1_is_up_ = true;
418 bool InsetMathScript::has(bool up) const
420 return idxOfScript(up);
424 bool InsetMathScript::hasUp() const
426 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
427 //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl;
428 return idxOfScript(true);
432 bool InsetMathScript::hasDown() const
434 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
435 //lyxerr << "hasDown: " << bool(idxOfScript(false)) << endl;
436 return idxOfScript(false);
440 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
445 return (cell_1_is_up_ == up) ? 1 : 0;
454 bool InsetMathScript::idxRight(Cursor &) const
460 bool InsetMathScript::idxLeft(Cursor &) const
466 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
469 if (cur.idx() == 0) {
470 // don't go up/down if there is no cell in this direction
473 // go up/down only if in the last position
474 // or in the first position of something with displayed limits
475 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
476 cur.idx() = idxOfScript(up);
484 if (cur.idx() == idxOfScript(true)) {
485 // can't go further up
488 // otherwise go to last position in the nucleus
490 cur.pos() = cur.lastpos();
495 if (cur.idx() == idxOfScript(false)) {
496 // can't go further down
499 // otherwise go to last position in the nucleus
501 cur.pos() = cur.lastpos();
509 void InsetMathScript::write(WriteStream & os) const
513 //if (nuc().back()->takesLimits()) {
521 LYXERR(Debug::MATHED) << "suppressing {} when writing"
527 if (hasDown() /*&& down().size()*/)
528 os << "_{" << down() << '}';
530 if (hasUp() /*&& up().size()*/)
531 os << "^{" << up() << '}';
533 if (lock_ && !os.latex())
538 void InsetMathScript::normalize(NormalStream & os) const
540 bool d = hasDown() && down().size();
541 bool u = hasUp() && up().size();
556 os << down() << ' ' << up() << ']';
564 void InsetMathScript::maple(MapleStream & os) const
568 if (hasDown() && down().size())
569 os << '[' << down() << ']';
570 if (hasUp() && up().size())
571 os << "^(" << up() << ')';
575 void InsetMathScript::mathematica(MathematicaStream & os) const
577 bool d = hasDown() && down().size();
578 bool u = hasUp() && up().size();
582 os << "Subscript[" << nuc();
588 os << "^(" << up() << ')';
592 os << ',' << down() << ']';
597 void InsetMathScript::mathmlize(MathStream & os) const
599 bool d = hasDown() && down().size();
600 bool u = hasUp() && up().size();
603 os << MTag("msubsup");
615 os << down() << up() << ETag("msubsup");
617 os << up() << ETag("msup");
619 os << down() << ETag("msub");
623 void InsetMathScript::octave(OctaveStream & os) const
627 if (hasDown() && down().size())
628 os << '[' << down() << ']';
629 if (hasUp() && up().size())
630 os << "^(" << up() << ')';
634 void InsetMathScript::infoize(odocstream & os) const
640 void InsetMathScript::infoize2(odocstream & os) const
643 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
647 bool InsetMathScript::notifyCursorLeaves(Cursor & cur)
649 InsetMathNest::notifyCursorLeaves(cur);
651 //lyxerr << "InsetMathScript::notifyCursorLeaves: 1 " << cur << endl;
653 // remove empty scripts if possible
655 // Case of two scripts. In this case, 1 = super, 2 = sub
656 if (cur.idx() == 2 && cell(2).empty()) {
657 // must be a subscript...
658 recordUndoInset(cur);
661 } else if (cur.idx() == 1 && cell(1).empty()) {
662 // must be a superscript...
663 recordUndoInset(cur);
667 } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
668 // could be either subscript or super script
669 recordUndoInset(cur);
670 removeScript(cell_1_is_up_);
671 // Let the script inset commit suicide. This is
672 // modelled on Cursor.pullArg(), but tries not to
673 // invoke notifyCursorLeaves again and does not touch
674 // cur (since the top slice will be deleted
676 MathData ar = cell(0);
679 tmpcur.cell().erase(tmpcur.pos());
680 tmpcur.cell().insert(tmpcur.pos(), ar);
684 //lyxerr << "InsetMathScript::notifyCursorLeaves: 2 " << cur << endl;
689 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
691 //lyxerr << "InsetMathScript: request: " << cmd << std::endl;
693 if (cmd.action == LFUN_MATH_LIMITS) {
694 if (!cmd.argument().empty()) {
695 if (cmd.argument() == "limits")
697 else if (cmd.argument() == "nolimits")
701 } else if (limits_ == 0)
702 limits_ = hasLimits() ? -1 : 1;
708 InsetMathNest::doDispatch(cur, cmd);