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 "BufferView.h"
15 #include "DispatchResult.h"
16 #include "FuncRequest.h"
17 #include "InsetMathFont.h"
18 #include "InsetMathScript.h"
19 #include "InsetMathSymbol.h"
21 #include "MathStream.h"
22 #include "MathSupport.h"
24 #include "support/debug.h"
26 #include "support/lassert.h"
33 InsetMathScript::InsetMathScript()
34 : InsetMathNest(1), cell_1_is_up_(false), limits_(0)
38 InsetMathScript::InsetMathScript(bool up)
39 : InsetMathNest(2), cell_1_is_up_(up), limits_(0)
43 InsetMathScript::InsetMathScript(MathAtom const & at, bool up)
44 : InsetMathNest(2), cell_1_is_up_(up), limits_(0)
46 LASSERT(nargs() >= 1, /**/);
47 cell(0).push_back(at);
51 Inset * InsetMathScript::clone() const
53 return new InsetMathScript(*this);
57 InsetMathScript const * InsetMathScript::asScriptInset() const
63 InsetMathScript * InsetMathScript::asScriptInset()
69 bool InsetMathScript::idxFirst(Cursor & cur) const
77 bool InsetMathScript::idxLast(Cursor & cur) const
80 cur.pos() = nuc().size();
85 MathData const & InsetMathScript::down() const
89 LASSERT(nargs() > 1, /**/);
94 MathData & InsetMathScript::down()
98 LASSERT(nargs() > 1, /**/);
103 MathData const & InsetMathScript::up() const
105 LASSERT(nargs() > 1, /**/);
110 MathData & InsetMathScript::up()
112 LASSERT(nargs() > 1, /**/);
117 void InsetMathScript::ensure(bool up)
120 // just nucleus so far
121 cells_.push_back(MathData());
123 } else if (nargs() == 2 && !has(up)) {
125 cells_.push_back(cell(1));
128 cells_.push_back(MathData());
134 MathData const & InsetMathScript::nuc() const
140 MathData & InsetMathScript::nuc()
148 bool isAlphaSymbol(MathAtom const & at)
150 if (at->asCharInset() ||
151 (at->asSymbolInset() &&
152 at->asSymbolInset()->isOrdAlpha()))
155 if (at->asFontInset()) {
156 MathData const & ar = at->asFontInset()->cell(0);
157 for (size_t i = 0; i < ar.size(); ++i) {
158 if (!(ar[i]->asCharInset() ||
159 (ar[i]->asSymbolInset() &&
160 ar[i]->asSymbolInset()->isOrdAlpha())))
171 int InsetMathScript::dy01(BufferView const & bv, int asc, int des, int what) const
175 bool isCharBox = nuc().size() ? isAlphaSymbol(nuc().back()) : false;
177 Dimension const & dimdown = down().dimension(bv);
178 dasc = dimdown.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 Dimension const & dimup = up().dimension(bv);
188 int minasc = nuc().minasc();
189 int ascdrop = isCharBox ? 0 : asc - up().mindes();
190 int udes = dimup.descent();
191 asc = udes + nuc().sshift();
192 asc = max(ascdrop, asc);
193 asc = max(minasc, asc);
195 int del = asc - udes - dasc;
196 if (del + des <= 2) {
197 int newdes = 2 - del;
198 del = slevel - asc + udes;
203 des = max(des, newdes);
207 return what ? asc : des;
211 int InsetMathScript::dy0(BufferView const & bv) const
216 int des = down().dimension(bv).ascent();
221 des = dy01(bv, na, nd, 0);
227 int InsetMathScript::dy1(BufferView const & bv) const
232 int asc = up().dimension(bv).descent();
237 asc = dy01(bv, na, nd, 1);
244 int InsetMathScript::dx0(BufferView const & bv) const
246 LASSERT(hasDown(), /**/);
247 Dimension const dim = dimension(bv);
248 return hasLimits() ? (dim.wid - down().dimension(bv).width()) / 2 : nwid(bv);
252 int InsetMathScript::dx1(BufferView const & bv) const
254 LASSERT(hasUp(), /**/);
255 Dimension const dim = dimension(bv);
256 return hasLimits() ? (dim.wid - up().dimension(bv).width()) / 2 : nwid(bv) + nker(&bv);
260 int InsetMathScript::dxx(BufferView const & bv) const
262 Dimension const dim = dimension(bv);
263 return hasLimits() ? (dim.wid - nwid(bv)) / 2 : 0;
267 int InsetMathScript::nwid(BufferView const & bv) const
269 return nuc().size() ? nuc().dimension(bv).width() : 2;
273 int InsetMathScript::nasc(BufferView const & bv) const
275 return nuc().size() ? nuc().dimension(bv).ascent() : 5;
279 int InsetMathScript::ndes(BufferView const & bv) const
281 return nuc().size() ? nuc().dimension(bv).descent() : 0;
285 int InsetMathScript::nker(BufferView const * bv) const
288 int kerning = nuc().kerning(bv);
289 return kerning > 0 ? kerning : 0;
295 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
300 cell(0).metrics(mi, dim0);
301 ScriptChanger dummy(mi.base);
303 cell(1).metrics(mi, dim1);
305 cell(2).metrics(mi, dim2);
308 BufferView & bv = *mi.base.bv;
309 // FIXME: data copying... not very efficient.
313 dimup = up().dimension(bv);
315 dimdown = down().dimension(bv);
320 dim.wid = max(dim.wid, dimup.width());
322 dim.wid = max(dim.wid, dimdown.width());
325 dim.wid = max(dim.wid, nker(mi.base.bv) + dimup.width());
327 dim.wid = max(dim.wid, dimdown.width());
332 int asc = dy1(bv) + dimup.ascent();
333 dim.asc = max(na, asc);
338 int des = dy0(bv) + dimdown.descent();
339 dim.des = max(nd, des);
346 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
348 BufferView & bv = *pi.base.bv;
350 nuc().draw(pi, x + dxx(bv), y);
352 nuc().setXY(bv, x + dxx(bv), y);
354 pi.draw(x + dxx(bv), y, char_type('.'));
356 ScriptChanger dummy(pi.base);
358 up().draw(pi, x + dx1(bv), y - dy1(bv));
360 down().draw(pi, x + dx0(bv), y + dy0(bv));
361 drawMarkers(pi, x, y);
365 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
368 up().metricsT(mi, dim);
370 down().metricsT(mi, dim);
371 nuc().metricsT(mi, dim);
375 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
379 nuc().drawT(pain, x + 1, y);
381 up().drawT(pain, x + 1, y - 1 /*dy1()*/);
383 down().drawT(pain, x + 1, y + 1 /*dy0()*/);
388 bool InsetMathScript::hasLimits() const
396 // we can only display limits if the nucleus wants some
399 if (!nuc().back()->isScriptable())
402 if (nuc().back()->asSymbolInset()) {
403 // \intop is an alias for \int\limits, \ointop == \oint\limits
404 if (nuc().back()->asSymbolInset()->name().find(from_ascii("intop")) != string::npos)
406 // per default \int has limits beside the \int even in displayed formulas
407 if (nuc().back()->asSymbolInset()->name().find(from_ascii("int")) != string::npos)
411 // assume "real" limits for everything else
416 void InsetMathScript::removeScript(bool up)
419 if (up == cell_1_is_up_)
421 } else if (nargs() == 3) {
423 swap(cells_[1], cells_[2]);
424 cell_1_is_up_ = false;
426 cell_1_is_up_ = true;
433 bool InsetMathScript::has(bool up) const
435 return idxOfScript(up);
439 bool InsetMathScript::hasUp() const
441 //lyxerr << "1up: " << bool(cell_1_is_up_));
442 //lyxerr << "hasUp: " << bool(idxOfScript(true)));
443 return idxOfScript(true);
447 bool InsetMathScript::hasDown() const
449 //LYXERR0("1up: " << bool(cell_1_is_up_));
450 //LYXERR0("hasDown: " << bool(idxOfScript(false)));
451 return idxOfScript(false);
455 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
460 return (cell_1_is_up_ == up) ? 1 : 0;
463 LASSERT(false, /**/);
469 bool InsetMathScript::idxForward(Cursor &) const
475 bool InsetMathScript::idxBackward(Cursor &) const
481 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
484 if (cur.idx() == 0) {
485 // don't go up/down if there is no cell in this direction
488 // go up/down only if in the last position
489 // or in the first position of something with displayed limits
490 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
491 cur.idx() = idxOfScript(up);
499 if (cur.idx() == idxOfScript(true)) {
500 // can't go further up
503 // otherwise go to last position in the nucleus
505 cur.pos() = cur.lastpos();
510 if (cur.idx() == idxOfScript(false)) {
511 // can't go further down
514 // otherwise go to last position in the nucleus
516 cur.pos() = cur.lastpos();
524 void InsetMathScript::write(WriteStream & os) const
528 //if (nuc().back()->takesLimits()) {
536 LYXERR(Debug::MATHED, "suppressing {} when writing");
541 if (hasDown() /*&& down().size()*/)
542 os << "_{" << down() << '}';
544 if (hasUp() /*&& up().size()*/)
545 os << "^{" << up() << '}';
547 if (lock_ && !os.latex())
552 void InsetMathScript::normalize(NormalStream & os) const
554 bool d = hasDown() && down().size();
555 bool u = hasUp() && up().size();
570 os << down() << ' ' << up() << ']';
578 void InsetMathScript::maple(MapleStream & os) const
582 if (hasDown() && down().size())
583 os << '[' << down() << ']';
584 if (hasUp() && up().size())
585 os << "^(" << up() << ')';
589 void InsetMathScript::mathematica(MathematicaStream & os) const
591 bool d = hasDown() && down().size();
592 bool u = hasUp() && up().size();
596 os << "Subscript[" << nuc();
602 os << "^(" << up() << ')';
606 os << ',' << down() << ']';
611 void InsetMathScript::mathmlize(MathStream & os) const
613 bool d = hasDown() && down().size();
614 bool u = hasUp() && up().size();
617 os << MTag("msubsup");
629 os << down() << up() << ETag("msubsup");
631 os << up() << ETag("msup");
633 os << down() << ETag("msub");
637 void InsetMathScript::octave(OctaveStream & os) const
641 if (hasDown() && down().size())
642 os << '[' << down() << ']';
643 if (hasUp() && up().size())
644 os << "^(" << up() << ')';
648 void InsetMathScript::infoize(odocstream & os) const
654 void InsetMathScript::infoize2(odocstream & os) const
657 os << from_ascii(limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
661 bool InsetMathScript::notifyCursorLeaves(Cursor const & old, Cursor & cur)
663 InsetMathNest::notifyCursorLeaves(old, cur);
665 //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
667 // Remove empty scripts if possible:
669 // The case of two scripts, but only one got empty (1 = super, 2 = sub).
670 // We keep the script inset, but remove the empty script.
671 if (nargs() > 2 && (!cell(1).empty() || !cell(2).empty())) {
672 if (cell(2).empty()) {
673 // must be a subscript...
675 cur.updateFlags(cur.disp_.update() | Update::SinglePar);
677 } else if (cell(1).empty()) {
678 // must be a superscript...
680 cur.updateFlags(cur.disp_.update() | Update::SinglePar);
684 // Now the two suicide cases:
685 // * we have only one script which is empty
686 // * we have two scripts which are both empty.
687 // The script inset is removed completely.
688 if ((nargs() == 2 && cell(1).empty())
689 || (nargs() == 3 && cell(1).empty() && cell(2).empty())) {
690 // Make undo step. We cannot use cur for this because
691 // it does not necessarily point to us anymore. But we
692 // should be on top of the cursor old.
693 Cursor insetCur = old;
694 int scriptSlice = insetCur.find(this);
695 LASSERT(scriptSlice != -1, /**/);
696 insetCur.cutOff(scriptSlice);
697 insetCur.recordUndoInset();
699 // Let the script inset commit suicide. This is
700 // modelled on Cursor.pullArg(), but tries not to
701 // invoke notifyCursorLeaves again and does not touch
702 // cur (since the top slice will be deleted
704 MathData ar = cell(0);
706 insetCur.cell().erase(insetCur.pos());
707 insetCur.cell().insert(insetCur.pos(), ar);
710 cur.updateFlags(cur.disp_.update() | Update::SinglePar);
714 //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
719 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
721 //LYXERR("InsetMathScript: request: " << cmd);
723 if (cmd.action == LFUN_MATH_LIMITS) {
724 if (!cmd.argument().empty()) {
725 if (cmd.argument() == "limits")
727 else if (cmd.argument() == "nolimits")
731 } else if (limits_ == 0)
732 limits_ = hasLimits() ? -1 : 1;
738 InsetMathNest::doDispatch(cur, cmd);