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 "InsetMathBrace.h"
16 #include "InsetMathSymbol.h"
18 #include "MathStream.h"
19 #include "MathSupport.h"
21 #include "BufferView.h"
23 #include "DispatchResult.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "LaTeXFeatures.h"
27 #include "MetricsInfo.h"
29 #include "support/debug.h"
30 #include "support/gettext.h"
31 #include "support/lassert.h"
39 InsetMathScript::InsetMathScript(Buffer * buf)
40 : InsetMathNest(buf, 1), cell_1_is_up_(false), limits_(0)
44 InsetMathScript::InsetMathScript(Buffer * buf, bool up)
45 : InsetMathNest(buf, 2), cell_1_is_up_(up), limits_(0)
49 InsetMathScript::InsetMathScript(Buffer * buf, MathAtom const & at, bool up)
50 : InsetMathNest(buf, 2), cell_1_is_up_(up), limits_(0)
52 LATTEST(nargs() >= 1);
53 cell(0).push_back(at);
57 Inset * InsetMathScript::clone() const
59 return new InsetMathScript(*this);
63 InsetMathScript const * InsetMathScript::asScriptInset() const
69 InsetMathScript * InsetMathScript::asScriptInset()
75 bool InsetMathScript::idxFirst(Cursor & cur) const
83 bool InsetMathScript::idxLast(Cursor & cur) const
86 cur.pos() = nuc().size();
91 MathData const & InsetMathScript::down() const
100 MathData & InsetMathScript::down()
104 LBUFERR(nargs() > 1);
109 MathData const & InsetMathScript::up() const
111 LBUFERR(nargs() > 1);
116 MathData & InsetMathScript::up()
118 LBUFERR(nargs() > 1);
123 void InsetMathScript::ensure(bool up)
126 // just nucleus so far
127 cells_.push_back(MathData());
129 } else if (nargs() == 2 && !has(up)) {
131 cells_.push_back(cell(1));
134 cells_.push_back(MathData());
140 MathData const & InsetMathScript::nuc() const
146 MathData & InsetMathScript::nuc()
152 int InsetMathScript::dy01(BufferView const & bv, int asc, int des, int what) const
156 bool isCharBox = !nuc().empty() ? isAlphaSymbol(nuc().back()) : false;
158 Dimension const & dimdown = down().dimension(bv);
159 dasc = dimdown.ascent();
160 slevel = nuc().slevel();
161 int ascdrop = dasc - slevel;
162 int desdrop = isCharBox ? 0 : des + nuc().sshift();
163 int mindes = nuc().mindes();
164 des = max(desdrop, ascdrop);
165 des = max(mindes, des);
168 Dimension const & dimup = up().dimension(bv);
169 int minasc = nuc().minasc();
170 int ascdrop = isCharBox ? 0 : asc - up().mindes();
171 int udes = dimup.descent();
172 asc = udes + nuc().sshift();
173 asc = max(ascdrop, asc);
174 asc = max(minasc, asc);
176 int del = asc - udes - dasc;
177 if (del + des <= 2) {
178 int newdes = 2 - del;
179 del = slevel - asc + udes;
184 des = max(des, newdes);
188 return what ? asc : des;
192 int InsetMathScript::dy0(BufferView const & bv) const
197 int des = down().dimension(bv).ascent();
202 des = dy01(bv, na, nd, 0);
208 int InsetMathScript::dy1(BufferView const & bv) const
213 int asc = up().dimension(bv).descent();
218 asc = dy01(bv, na, nd, 1);
225 int InsetMathScript::dx0(BufferView const & bv) const
227 LASSERT(hasDown(), return 0);
228 Dimension const dim = dimension(bv);
229 return hasLimits() ? (dim.wid - down().dimension(bv).width()) / 2 : nwid(bv);
233 int InsetMathScript::dx1(BufferView const & bv) const
235 LASSERT(hasUp(), return 0);
236 Dimension const dim = dimension(bv);
237 return hasLimits() ? (dim.wid - up().dimension(bv).width()) / 2 : nwid(bv) + nker(&bv);
241 int InsetMathScript::dxx(BufferView const & bv) const
243 Dimension const dim = dimension(bv);
244 return hasLimits() ? (dim.wid - nwid(bv)) / 2 : 0;
248 int InsetMathScript::nwid(BufferView const & bv) const
250 return !nuc().empty() ? nuc().dimension(bv).width() : 2;
254 int InsetMathScript::nasc(BufferView const & bv) const
256 return !nuc().empty() ? nuc().dimension(bv).ascent() : 5;
260 int InsetMathScript::ndes(BufferView const & bv) const
262 return !nuc().empty() ? nuc().dimension(bv).descent() : 0;
266 int InsetMathScript::nker(BufferView const * bv) const
268 if (!nuc().empty()) {
269 int kerning = nuc().kerning(bv);
270 return kerning > 0 ? kerning : 0;
276 MathClass InsetMathScript::mathClass() const
278 // FIXME: this is a hack, since the class will not be correct if
279 // the nucleus has several elements.
280 // The correct implementation would require to linearize the nucleus.
284 // return the class of last element since this is the one that counts.
285 return nuc().back()->mathClass();
289 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
291 Changer dummy2 = mi.base.changeEnsureMath();
295 cell(0).metrics(mi, dim0);
296 Changer dummy = mi.base.changeScript();
298 cell(1).metrics(mi, dim1);
300 cell(2).metrics(mi, dim2);
303 BufferView & bv = *mi.base.bv;
304 // FIXME: data copying... not very efficient.
308 dimup = up().dimension(bv);
310 dimdown = down().dimension(bv);
315 dim.wid = max(dim.wid, dimup.width());
317 dim.wid = max(dim.wid, dimdown.width());
320 dim.wid = max(dim.wid, nker(mi.base.bv) + dimup.width());
322 dim.wid = max(dim.wid, dimdown.width());
327 int asc = dy1(bv) + dimup.ascent();
328 dim.asc = max(na, asc);
333 int des = dy0(bv) + dimdown.descent();
334 dim.des = max(nd, des);
337 metricsMarkers(mi, dim);
341 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
343 Changer dummy2 = pi.base.changeEnsureMath();
344 BufferView & bv = *pi.base.bv;
346 nuc().draw(pi, x + dxx(bv), y);
348 nuc().setXY(bv, x + dxx(bv), y);
350 pi.draw(x + dxx(bv), y, char_type('.'));
352 Changer dummy = pi.base.changeScript();
354 up().draw(pi, x + dx1(bv), y - dy1(bv));
356 down().draw(pi, x + dx0(bv), y + dy0(bv));
357 drawMarkers(pi, x, y);
361 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
364 up().metricsT(mi, dim);
366 down().metricsT(mi, dim);
367 nuc().metricsT(mi, dim);
371 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
375 nuc().drawT(pain, x + 1, y);
377 up().drawT(pain, x + 1, y - 1 /*dy1()*/);
379 down().drawT(pain, x + 1, y + 1 /*dy0()*/);
384 bool InsetMathScript::hasLimits() const
392 // we can only display limits if the nucleus wants some
395 if (!nuc().back()->isScriptable())
398 if (nuc().back()->asSymbolInset()) {
399 // \intop is an alias for \int\limits, \ointop == \oint\limits
400 if (nuc().back()->asSymbolInset()->name().find(from_ascii("intop")) != string::npos)
402 // per default \int has limits beside the \int even in displayed formulas
403 if (nuc().back()->asSymbolInset()->name().find(from_ascii("int")) != string::npos)
407 // assume "real" limits for everything else
412 void InsetMathScript::removeScript(bool up)
415 if (up == cell_1_is_up_)
417 } else if (nargs() == 3) {
419 swap(cells_[1], cells_[2]);
420 cell_1_is_up_ = false;
422 cell_1_is_up_ = true;
429 bool InsetMathScript::has(bool up) const
431 return idxOfScript(up);
435 bool InsetMathScript::hasUp() const
437 //lyxerr << "1up: " << bool(cell_1_is_up_));
438 //lyxerr << "hasUp: " << bool(idxOfScript(true)));
439 return idxOfScript(true);
443 bool InsetMathScript::hasDown() const
445 //LYXERR0("1up: " << bool(cell_1_is_up_));
446 //LYXERR0("hasDown: " << bool(idxOfScript(false)));
447 return idxOfScript(false);
451 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
456 return (cell_1_is_up_ == up) ? 1 : 0;
459 LASSERT(false, return 0);
463 bool InsetMathScript::idxForward(Cursor &) const
469 bool InsetMathScript::idxBackward(Cursor &) const
475 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
478 if (cur.idx() == 0) {
479 // don't go up/down if there is no cell in this direction
482 // go up/down only if in the last position
483 // or in the first position of something with displayed limits
484 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
485 cur.idx() = idxOfScript(up);
493 if (cur.idx() == idxOfScript(true)) {
494 // can't go further up
497 // otherwise go to last position in the nucleus
499 cur.pos() = cur.lastpos();
504 if (cur.idx() == idxOfScript(false)) {
505 // can't go further down
508 // otherwise go to last position in the nucleus
510 cur.pos() = cur.lastpos();
518 void InsetMathScript::write(WriteStream & os) const
520 MathEnsurer ensurer(os);
522 if (!nuc().empty()) {
524 //if (nuc().back()->takesLimits()) {
532 LYXERR(Debug::MATHED, "suppressing {} when writing");
537 if (hasDown() /*&& !down().empty()*/)
538 os << "_{" << down() << '}';
540 if (hasUp() /*&& !up().empty()*/) {
541 // insert space if up() is empty or an empty brace inset
543 if (os.latex() && (up().empty() ||
544 (up().size() == 1 && up().back()->asBraceInset() &&
545 up().back()->asBraceInset()->cell(0).empty())))
548 os << "^{" << up() << '}';
551 if (lock_ && !os.latex())
556 void InsetMathScript::normalize(NormalStream & os) const
558 bool d = hasDown() && !down().empty();
559 bool u = hasUp() && !up().empty();
574 os << down() << ' ' << up() << ']';
582 void InsetMathScript::maple(MapleStream & os) const
586 if (hasDown() && !down().empty())
587 os << '[' << down() << ']';
588 if (hasUp() && !up().empty())
589 os << "^(" << up() << ')';
593 void InsetMathScript::mathematica(MathematicaStream & os) const
595 bool d = hasDown() && !down().empty();
596 bool u = hasUp() && !up().empty();
598 if (!nuc().empty()) {
600 os << "Subscript[" << nuc();
606 os << "^(" << up() << ')';
608 if (!nuc().empty()) {
610 os << ',' << down() << ']';
615 void InsetMathScript::mathmlize(MathStream & os) const
617 bool d = hasDown() && !down().empty();
618 bool u = hasUp() && !up().empty();
619 bool l = hasLimits();
622 os << MTag(l ? "munderover" : "msubsup");
624 os << MTag(l ? "mover" : "msup");
626 os << MTag(l ? "munder" : "msub");
629 os << MTag("mrow") << nuc() << ETag("mrow");
634 os << MTag("mrow") << down() << ETag("mrow")
635 << MTag("mrow") << up() << ETag("mrow")
636 << ETag(l ? "munderover" : "msubsup");
638 os << MTag("mrow") << up() << ETag("mrow") << ETag(l ? "mover" : "msup");
640 os << MTag("mrow") << down() << ETag("mrow") << ETag(l ? "munder" : "msub");
644 void InsetMathScript::htmlize(HtmlStream & os) const
646 bool d = hasDown() && !down().empty();
647 bool u = hasUp() && !up().empty();
653 os << MTag("span", "class='scripts'")
654 << MTag("span") << up() << ETag("span")
655 << MTag("span") << down() << ETag("span")
658 os << MTag("sup", "class='math'") << up() << ETag("sup");
660 os << MTag("sub", "class='math'") << down() << ETag("sub");
664 void InsetMathScript::octave(OctaveStream & os) const
668 if (hasDown() && !down().empty())
669 os << '[' << down() << ']';
670 if (hasUp() && !up().empty())
671 os << "^(" << up() << ')';
675 void InsetMathScript::infoize(odocstream & os) const
681 void InsetMathScript::infoize2(odocstream & os) const
684 os << from_ascii(limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
688 bool InsetMathScript::notifyCursorLeaves(Cursor const & old, Cursor & cur)
690 InsetMathNest::notifyCursorLeaves(old, cur);
692 //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
694 // Remove empty scripts if possible:
696 // The case of two scripts, but only one got empty (1 = super, 2 = sub).
697 // We keep the script inset, but remove the empty script.
698 if (nargs() > 2 && (!cell(1).empty() || !cell(2).empty())) {
699 if (cell(2).empty()) {
700 // must be a subscript...
701 old.recordUndoInset();
703 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
705 } else if (cell(1).empty()) {
706 // must be a superscript...
707 old.recordUndoInset();
709 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
713 // Now the two suicide cases:
714 // * we have only one script which is empty
715 // * we have two scripts which are both empty.
716 // The script inset is removed completely.
717 if ((nargs() == 2 && cell(1).empty())
718 || (nargs() == 3 && cell(1).empty() && cell(2).empty())) {
719 // Make undo step. We cannot use cur for this because
720 // it does not necessarily point to us anymore. But we
721 // should be on top of the cursor old.
722 Cursor insetCur = old;
723 int scriptSlice = insetCur.find(this);
724 LASSERT(scriptSlice != -1, /**/);
725 insetCur.cutOff(scriptSlice);
726 insetCur.recordUndoInset();
728 // Let the script inset commit suicide. This is
729 // modelled on Cursor.pullArg(), but tries not to
730 // invoke notifyCursorLeaves again and does not touch
731 // cur (since the top slice will be deleted
733 MathData ar = cell(0);
735 insetCur.cell().erase(insetCur.pos());
736 insetCur.cell().insert(insetCur.pos(), ar);
739 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
743 //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
748 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
750 //LYXERR("InsetMathScript: request: " << cmd);
752 if (cmd.action() == LFUN_MATH_LIMITS) {
753 cur.recordUndoInset();
754 if (!cmd.argument().empty()) {
755 if (cmd.argument() == "limits")
757 else if (cmd.argument() == "nolimits")
761 } else if (limits_ == 0)
762 limits_ = hasLimits() ? -1 : 1;
768 InsetMathNest::doDispatch(cur, cmd);
772 bool InsetMathScript::getStatus(Cursor & cur, FuncRequest const & cmd,
773 FuncStatus & flag) const
775 if (cmd.action() == LFUN_MATH_LIMITS) {
776 if (!cmd.argument().empty()) {
777 if (cmd.argument() == "limits")
778 flag.setOnOff(limits_ == 1);
779 else if (cmd.argument() == "nolimits")
780 flag.setOnOff(limits_ == -1);
782 flag.setOnOff(limits_ == 0);
784 flag.setEnabled(true);
788 return InsetMathNest::getStatus(cur, cmd, flag);
792 // the idea for dual scripts came from the eLyXer code
793 void InsetMathScript::validate(LaTeXFeatures & features) const
795 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
796 features.addCSSSnippet(
797 "span.scripts{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
798 "span.scripts span {display: block;}\n"
799 "sub.math{font-size: 75%;}\n"
800 "sup.math{font-size: 75%;}");
801 InsetMathNest::validate(features);