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
294 cell(0).metrics(mi, dim0);
295 Changer dummy = mi.base.changeScript();
297 cell(1).metrics(mi, dim1);
299 cell(2).metrics(mi, dim2);
302 BufferView & bv = *mi.base.bv;
303 // FIXME: data copying... not very efficient.
307 dimup = up().dimension(bv);
309 dimdown = down().dimension(bv);
314 dim.wid = max(dim.wid, dimup.width());
316 dim.wid = max(dim.wid, dimdown.width());
319 dim.wid = max(dim.wid, nker(mi.base.bv) + dimup.width());
321 dim.wid = max(dim.wid, dimdown.width());
326 int asc = dy1(bv) + dimup.ascent();
327 dim.asc = max(na, asc);
332 int des = dy0(bv) + dimdown.descent();
333 dim.des = max(nd, des);
336 metricsMarkers(mi, dim);
340 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
342 BufferView & bv = *pi.base.bv;
344 nuc().draw(pi, x + dxx(bv), y);
346 nuc().setXY(bv, x + dxx(bv), y);
348 pi.draw(x + dxx(bv), y, char_type('.'));
350 Changer dummy = pi.base.changeScript();
352 up().draw(pi, x + dx1(bv), y - dy1(bv));
354 down().draw(pi, x + dx0(bv), y + dy0(bv));
355 drawMarkers(pi, x, y);
359 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
362 up().metricsT(mi, dim);
364 down().metricsT(mi, dim);
365 nuc().metricsT(mi, dim);
369 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
373 nuc().drawT(pain, x + 1, y);
375 up().drawT(pain, x + 1, y - 1 /*dy1()*/);
377 down().drawT(pain, x + 1, y + 1 /*dy0()*/);
382 bool InsetMathScript::hasLimits() const
390 // we can only display limits if the nucleus wants some
393 if (!nuc().back()->isScriptable())
396 if (nuc().back()->asSymbolInset()) {
397 // \intop is an alias for \int\limits, \ointop == \oint\limits
398 if (nuc().back()->asSymbolInset()->name().find(from_ascii("intop")) != string::npos)
400 // per default \int has limits beside the \int even in displayed formulas
401 if (nuc().back()->asSymbolInset()->name().find(from_ascii("int")) != string::npos)
405 // assume "real" limits for everything else
410 void InsetMathScript::removeScript(bool up)
413 if (up == cell_1_is_up_)
415 } else if (nargs() == 3) {
417 swap(cells_[1], cells_[2]);
418 cell_1_is_up_ = false;
420 cell_1_is_up_ = true;
427 bool InsetMathScript::has(bool up) const
429 return idxOfScript(up);
433 bool InsetMathScript::hasUp() const
435 //lyxerr << "1up: " << bool(cell_1_is_up_));
436 //lyxerr << "hasUp: " << bool(idxOfScript(true)));
437 return idxOfScript(true);
441 bool InsetMathScript::hasDown() const
443 //LYXERR0("1up: " << bool(cell_1_is_up_));
444 //LYXERR0("hasDown: " << bool(idxOfScript(false)));
445 return idxOfScript(false);
449 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
454 return (cell_1_is_up_ == up) ? 1 : 0;
457 LASSERT(false, return 0);
461 bool InsetMathScript::idxForward(Cursor &) const
467 bool InsetMathScript::idxBackward(Cursor &) const
473 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
476 if (cur.idx() == 0) {
477 // don't go up/down if there is no cell in this direction
480 // go up/down only if in the last position
481 // or in the first position of something with displayed limits
482 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
483 cur.idx() = idxOfScript(up);
491 if (cur.idx() == idxOfScript(true)) {
492 // can't go further up
495 // otherwise go to last position in the nucleus
497 cur.pos() = cur.lastpos();
502 if (cur.idx() == idxOfScript(false)) {
503 // can't go further down
506 // otherwise go to last position in the nucleus
508 cur.pos() = cur.lastpos();
516 void InsetMathScript::write(WriteStream & os) const
518 MathEnsurer ensurer(os);
520 if (!nuc().empty()) {
522 //if (nuc().back()->takesLimits()) {
530 LYXERR(Debug::MATHED, "suppressing {} when writing");
535 if (hasDown() /*&& !down().empty()*/)
536 os << "_{" << down() << '}';
538 if (hasUp() /*&& !up().empty()*/) {
539 // insert space if up() is empty or an empty brace inset
541 if (os.latex() && (up().empty() ||
542 (up().size() == 1 && up().back()->asBraceInset() &&
543 up().back()->asBraceInset()->cell(0).empty())))
546 os << "^{" << up() << '}';
549 if (lock_ && !os.latex())
554 void InsetMathScript::normalize(NormalStream & os) const
556 bool d = hasDown() && !down().empty();
557 bool u = hasUp() && !up().empty();
572 os << down() << ' ' << up() << ']';
580 void InsetMathScript::maple(MapleStream & os) const
584 if (hasDown() && !down().empty())
585 os << '[' << down() << ']';
586 if (hasUp() && !up().empty())
587 os << "^(" << up() << ')';
591 void InsetMathScript::mathematica(MathematicaStream & os) const
593 bool d = hasDown() && !down().empty();
594 bool u = hasUp() && !up().empty();
596 if (!nuc().empty()) {
598 os << "Subscript[" << nuc();
604 os << "^(" << up() << ')';
606 if (!nuc().empty()) {
608 os << ',' << down() << ']';
613 void InsetMathScript::mathmlize(MathStream & os) const
615 bool d = hasDown() && !down().empty();
616 bool u = hasUp() && !up().empty();
617 bool l = hasLimits();
620 os << MTag(l ? "munderover" : "msubsup");
622 os << MTag(l ? "mover" : "msup");
624 os << MTag(l ? "munder" : "msub");
627 os << MTag("mrow") << nuc() << ETag("mrow");
632 os << MTag("mrow") << down() << ETag("mrow")
633 << MTag("mrow") << up() << ETag("mrow")
634 << ETag(l ? "munderover" : "msubsup");
636 os << MTag("mrow") << up() << ETag("mrow") << ETag(l ? "mover" : "msup");
638 os << MTag("mrow") << down() << ETag("mrow") << ETag(l ? "munder" : "msub");
642 void InsetMathScript::htmlize(HtmlStream & os) const
644 bool d = hasDown() && !down().empty();
645 bool u = hasUp() && !up().empty();
651 os << MTag("span", "class='scripts'")
652 << MTag("span") << up() << ETag("span")
653 << MTag("span") << down() << ETag("span")
656 os << MTag("sup", "class='math'") << up() << ETag("sup");
658 os << MTag("sub", "class='math'") << down() << ETag("sub");
662 void InsetMathScript::octave(OctaveStream & os) const
666 if (hasDown() && !down().empty())
667 os << '[' << down() << ']';
668 if (hasUp() && !up().empty())
669 os << "^(" << up() << ')';
673 void InsetMathScript::infoize(odocstream & os) const
679 void InsetMathScript::infoize2(odocstream & os) const
682 os << from_ascii(limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
686 bool InsetMathScript::notifyCursorLeaves(Cursor const & old, Cursor & cur)
688 InsetMathNest::notifyCursorLeaves(old, cur);
690 //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
692 // Remove empty scripts if possible:
694 // The case of two scripts, but only one got empty (1 = super, 2 = sub).
695 // We keep the script inset, but remove the empty script.
696 if (nargs() > 2 && (!cell(1).empty() || !cell(2).empty())) {
697 if (cell(2).empty()) {
698 // must be a subscript...
699 old.recordUndoInset();
701 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
703 } else if (cell(1).empty()) {
704 // must be a superscript...
705 old.recordUndoInset();
707 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
711 // Now the two suicide cases:
712 // * we have only one script which is empty
713 // * we have two scripts which are both empty.
714 // The script inset is removed completely.
715 if ((nargs() == 2 && cell(1).empty())
716 || (nargs() == 3 && cell(1).empty() && cell(2).empty())) {
717 // Make undo step. We cannot use cur for this because
718 // it does not necessarily point to us anymore. But we
719 // should be on top of the cursor old.
720 Cursor insetCur = old;
721 int scriptSlice = insetCur.find(this);
722 LASSERT(scriptSlice != -1, /**/);
723 insetCur.cutOff(scriptSlice);
724 insetCur.recordUndoInset();
726 // Let the script inset commit suicide. This is
727 // modelled on Cursor.pullArg(), but tries not to
728 // invoke notifyCursorLeaves again and does not touch
729 // cur (since the top slice will be deleted
731 MathData ar = cell(0);
733 insetCur.cell().erase(insetCur.pos());
734 insetCur.cell().insert(insetCur.pos(), ar);
737 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
741 //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
746 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
748 //LYXERR("InsetMathScript: request: " << cmd);
750 if (cmd.action() == LFUN_MATH_LIMITS) {
751 cur.recordUndoInset();
752 if (!cmd.argument().empty()) {
753 if (cmd.argument() == "limits")
755 else if (cmd.argument() == "nolimits")
759 } else if (limits_ == 0)
760 limits_ = hasLimits() ? -1 : 1;
766 InsetMathNest::doDispatch(cur, cmd);
770 bool InsetMathScript::getStatus(Cursor & cur, FuncRequest const & cmd,
771 FuncStatus & flag) const
773 if (cmd.action() == LFUN_MATH_LIMITS) {
774 if (!cmd.argument().empty()) {
775 if (cmd.argument() == "limits")
776 flag.setOnOff(limits_ == 1);
777 else if (cmd.argument() == "nolimits")
778 flag.setOnOff(limits_ == -1);
780 flag.setOnOff(limits_ == 0);
782 flag.setEnabled(true);
786 return InsetMathNest::getStatus(cur, cmd, flag);
790 // the idea for dual scripts came from the eLyXer code
791 void InsetMathScript::validate(LaTeXFeatures & features) const
793 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
794 features.addCSSSnippet(
795 "span.scripts{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
796 "span.scripts span {display: block;}\n"
797 "sub.math{font-size: 75%;}\n"
798 "sup.math{font-size: 75%;}");
799 InsetMathNest::validate(features);