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(BufferView const & bv) const
245 BOOST_ASSERT(hasDown());
246 Dimension const dim = dimension(bv);
247 return hasLimits() ? (dim.wid - down().width()) / 2 : nwid();
251 int InsetMathScript::dx1(BufferView const & bv) const
253 BOOST_ASSERT(hasUp());
254 Dimension const dim = dimension(bv);
255 return hasLimits() ? (dim.wid - up().width()) / 2 : nwid() + nker();
259 int InsetMathScript::dxx(BufferView const & bv) const
261 Dimension const dim = dimension(bv);
262 return hasLimits() ? (dim.wid - nwid()) / 2 : 0;
266 int InsetMathScript::nwid() const
268 return nuc().size() ? nuc().width() : 2;
272 int InsetMathScript::nasc() const
274 return nuc().size() ? nuc().ascent() : 5;
278 int InsetMathScript::ndes() const
280 return nuc().size() ? nuc().descent() : 0;
284 int InsetMathScript::nker() const
287 int kerning = nuc().kerning();
288 return kerning > 0 ? kerning : 0;
294 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
297 ScriptChanger dummy(mi.base);
306 dim.wid = max(dim.wid, up().width());
308 dim.wid = max(dim.wid, down().width());
311 dim.wid = max(dim.wid, nker() + up().width());
313 dim.wid = max(dim.wid, down().width());
318 int asc = dy1() + up().ascent();
319 dim.asc = max(na, asc);
324 int des = dy0() + down().descent();
325 dim.des = max(nd, des);
329 // Cache the inset dimension.
330 setDimCache(mi, dim);
334 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
337 nuc().draw(pi, x + dxx(*pi.base.bv), y);
339 nuc().setXY(*pi.base.bv, x + dxx(*pi.base.bv), y);
340 if (editing(pi.base.bv))
341 pi.draw(x + dxx(*pi.base.bv), y, char_type('.'));
343 ScriptChanger dummy(pi.base);
345 up().draw(pi, x + dx1(*pi.base.bv), y - dy1());
347 down().draw(pi, x + dx0(*pi.base.bv), y + dy0());
348 drawMarkers(pi, x, y);
352 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
355 up().metricsT(mi, dim);
357 down().metricsT(mi, dim);
358 nuc().metricsT(mi, dim);
362 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
365 nuc().drawT(pain, x + 1, y);
367 up().drawT(pain, x + 1, y - dy1());
369 down().drawT(pain, x + 1, y + dy0());
374 bool InsetMathScript::hasLimits() const
382 // we can only display limits if the nucleus wants some
385 if (!nuc().back()->isScriptable())
388 if (nuc().back()->asSymbolInset()) {
389 // \intop is an alias for \int\limits, \ointop == \oint\limits
390 if (nuc().back()->asSymbolInset()->name().find(from_ascii("intop")) != string::npos)
392 // per default \int has limits beside the \int even in displayed formulas
393 if (nuc().back()->asSymbolInset()->name().find(from_ascii("int")) != string::npos)
397 // assume "real" limits for everything else
402 void InsetMathScript::removeScript(bool up)
405 if (up == cell_1_is_up_)
407 } else if (nargs() == 3) {
409 swap(cells_[1], cells_[2]);
410 cell_1_is_up_ = false;
412 cell_1_is_up_ = true;
419 bool InsetMathScript::has(bool up) const
421 return idxOfScript(up);
425 bool InsetMathScript::hasUp() const
427 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
428 //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl;
429 return idxOfScript(true);
433 bool InsetMathScript::hasDown() const
435 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
436 //lyxerr << "hasDown: " << bool(idxOfScript(false)) << endl;
437 return idxOfScript(false);
441 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
446 return (cell_1_is_up_ == up) ? 1 : 0;
455 bool InsetMathScript::idxRight(Cursor &) const
461 bool InsetMathScript::idxLeft(Cursor &) const
467 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
470 if (cur.idx() == 0) {
471 // don't go up/down if there is no cell in this direction
474 // go up/down only if in the last position
475 // or in the first position of something with displayed limits
476 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
477 cur.idx() = idxOfScript(up);
485 if (cur.idx() == idxOfScript(true)) {
486 // can't go further up
489 // otherwise go to last position in the nucleus
491 cur.pos() = cur.lastpos();
496 if (cur.idx() == idxOfScript(false)) {
497 // can't go further down
500 // otherwise go to last position in the nucleus
502 cur.pos() = cur.lastpos();
510 void InsetMathScript::write(WriteStream & os) const
514 //if (nuc().back()->takesLimits()) {
522 LYXERR(Debug::MATHED) << "suppressing {} when writing"
528 if (hasDown() /*&& down().size()*/)
529 os << "_{" << down() << '}';
531 if (hasUp() /*&& up().size()*/)
532 os << "^{" << up() << '}';
534 if (lock_ && !os.latex())
539 void InsetMathScript::normalize(NormalStream & os) const
541 bool d = hasDown() && down().size();
542 bool u = hasUp() && up().size();
557 os << down() << ' ' << up() << ']';
565 void InsetMathScript::maple(MapleStream & os) const
569 if (hasDown() && down().size())
570 os << '[' << down() << ']';
571 if (hasUp() && up().size())
572 os << "^(" << up() << ')';
576 void InsetMathScript::mathematica(MathematicaStream & os) const
578 bool d = hasDown() && down().size();
579 bool u = hasUp() && up().size();
583 os << "Subscript[" << nuc();
589 os << "^(" << up() << ')';
593 os << ',' << down() << ']';
598 void InsetMathScript::mathmlize(MathStream & os) const
600 bool d = hasDown() && down().size();
601 bool u = hasUp() && up().size();
604 os << MTag("msubsup");
616 os << down() << up() << ETag("msubsup");
618 os << up() << ETag("msup");
620 os << down() << ETag("msub");
624 void InsetMathScript::octave(OctaveStream & os) const
628 if (hasDown() && down().size())
629 os << '[' << down() << ']';
630 if (hasUp() && up().size())
631 os << "^(" << up() << ')';
635 void InsetMathScript::infoize(odocstream & os) const
641 void InsetMathScript::infoize2(odocstream & os) const
644 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
648 bool InsetMathScript::notifyCursorLeaves(Cursor & cur)
650 InsetMathNest::notifyCursorLeaves(cur);
652 //lyxerr << "InsetMathScript::notifyCursorLeaves: 1 " << cur << endl;
654 // remove empty scripts if possible
656 // Case of two scripts. In this case, 1 = super, 2 = sub
657 if (cur.idx() == 2 && cell(2).empty()) {
658 // must be a subscript...
659 recordUndoInset(cur);
662 } else if (cur.idx() == 1 && cell(1).empty()) {
663 // must be a superscript...
664 recordUndoInset(cur);
668 } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
669 // could be either subscript or super script
670 recordUndoInset(cur);
671 removeScript(cell_1_is_up_);
672 // Let the script inset commit suicide. This is
673 // modelled on Cursor.pullArg(), but tries not to
674 // invoke notifyCursorLeaves again and does not touch
675 // cur (since the top slice will be deleted
677 MathData ar = cell(0);
680 tmpcur.cell().erase(tmpcur.pos());
681 tmpcur.cell().insert(tmpcur.pos(), ar);
685 //lyxerr << "InsetMathScript::notifyCursorLeaves: 2 " << cur << endl;
690 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
692 //lyxerr << "InsetMathScript: request: " << cmd << std::endl;
694 if (cmd.action == LFUN_MATH_LIMITS) {
695 if (!cmd.argument().empty()) {
696 if (cmd.argument() == "limits")
698 else if (cmd.argument() == "nolimits")
702 } else if (limits_ == 0)
703 limits_ = hasLimits() ? -1 : 1;
709 InsetMathNest::doDispatch(cur, cmd);