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
230 : nwid(bv) + min(nker(&bv), 0);
234 int InsetMathScript::dx1(BufferView const & bv) const
236 LASSERT(hasUp(), return 0);
237 Dimension const dim = dimension(bv);
238 return hasLimits() ? (dim.wid - up().dimension(bv).width()) / 2
239 : nwid(bv) + max(nker(&bv), 0);
243 int InsetMathScript::dxx(BufferView const & bv) const
245 Dimension const dim = dimension(bv);
246 return hasLimits() ? (dim.wid - nwid(bv)) / 2 : 0;
250 int InsetMathScript::nwid(BufferView const & bv) const
252 return !nuc().empty() ? nuc().dimension(bv).width() : 2;
256 int InsetMathScript::nasc(BufferView const & bv) const
258 return !nuc().empty() ? nuc().dimension(bv).ascent() : 5;
262 int InsetMathScript::ndes(BufferView const & bv) const
264 return !nuc().empty() ? nuc().dimension(bv).descent() : 0;
268 int InsetMathScript::nker(BufferView const * bv) const
271 return nuc().kerning(bv);
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 or if the last element is a math macro
280 // or a macro argument proxy.
281 // The correct implementation would require to linearize the nucleus.
285 // return the class of last element since this is the one that counts.
286 MathClass mc = nuc().back()->mathClass();
287 return (mc == MC_UNKNOWN) ? MC_ORD : mc;
292 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
294 Changer dummy2 = mi.base.changeEnsureMath();
298 cell(0).metrics(mi, dim0);
299 Changer dummy = mi.base.changeScript();
301 cell(1).metrics(mi, dim1);
303 cell(2).metrics(mi, dim2);
306 BufferView & bv = *mi.base.bv;
307 // FIXME: data copying... not very efficient.
311 dimup = up().dimension(bv);
313 dimdown = down().dimension(bv);
318 dim.wid = max(dim.wid, dimup.width());
320 dim.wid = max(dim.wid, dimdown.width());
323 dim.wid = max(dim.wid, max(nker(mi.base.bv), 0) + dimup.width());
325 dim.wid = max(dim.wid, min(nker(mi.base.bv), 0) + dimdown.width());
330 int asc = dy1(bv) + dimup.ascent();
331 dim.asc = max(na, asc);
336 int des = dy0(bv) + dimdown.descent();
337 dim.des = max(nd, des);
343 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
345 Changer dummy2 = pi.base.changeEnsureMath();
346 BufferView & bv = *pi.base.bv;
348 nuc().draw(pi, x + dxx(bv), y);
350 nuc().setXY(bv, x + dxx(bv), y);
352 pi.draw(x + dxx(bv), y, char_type('.'));
354 Changer dummy = pi.base.changeScript();
356 up().draw(pi, x + dx1(bv), y - dy1(bv));
358 down().draw(pi, x + dx0(bv), y + dy0(bv));
362 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
365 up().metricsT(mi, dim);
367 down().metricsT(mi, dim);
368 nuc().metricsT(mi, dim);
372 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
376 nuc().drawT(pain, x + 1, y);
378 up().drawT(pain, x + 1, y - 1 /*dy1()*/);
380 down().drawT(pain, x + 1, y + 1 /*dy0()*/);
385 bool InsetMathScript::hasLimits() const
393 // we can only display limits if the nucleus wants some
396 if (!nuc().back()->isScriptable())
399 if (nuc().back()->asSymbolInset()) {
400 // \intop is an alias for \int\limits, \ointop == \oint\limits
401 if (nuc().back()->asSymbolInset()->name().find(from_ascii("intop")) != string::npos)
403 // per default \int has limits beside the \int even in displayed formulas
404 if (nuc().back()->asSymbolInset()->name().find(from_ascii("int")) != string::npos)
408 // assume "real" limits for everything else
413 void InsetMathScript::removeScript(bool up)
416 if (up == cell_1_is_up_)
418 } else if (nargs() == 3) {
420 swap(cells_[1], cells_[2]);
421 cell_1_is_up_ = false;
423 cell_1_is_up_ = true;
430 bool InsetMathScript::has(bool up) const
432 return idxOfScript(up);
436 bool InsetMathScript::hasUp() const
438 //lyxerr << "1up: " << bool(cell_1_is_up_));
439 //lyxerr << "hasUp: " << bool(idxOfScript(true)));
440 return idxOfScript(true);
444 bool InsetMathScript::hasDown() const
446 //LYXERR0("1up: " << bool(cell_1_is_up_));
447 //LYXERR0("hasDown: " << bool(idxOfScript(false)));
448 return idxOfScript(false);
452 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
457 return (cell_1_is_up_ == up) ? 1 : 0;
460 LASSERT(false, return 0);
464 bool InsetMathScript::idxForward(Cursor &) const
470 bool InsetMathScript::idxBackward(Cursor &) const
476 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
479 if (cur.idx() == 0) {
480 // don't go up/down if there is no cell in this direction
483 // go up/down only if in the last position
484 // or in the first position of something with displayed limits
485 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
486 cur.idx() = idxOfScript(up);
494 if (cur.idx() == idxOfScript(true)) {
495 // can't go further up
498 // otherwise go to last position in the nucleus
500 cur.pos() = cur.lastpos();
505 if (cur.idx() == idxOfScript(false)) {
506 // can't go further down
509 // otherwise go to last position in the nucleus
511 cur.pos() = cur.lastpos();
519 void InsetMathScript::write(WriteStream & os) const
521 MathEnsurer ensurer(os);
523 if (!nuc().empty()) {
525 //if (nuc().back()->takesLimits()) {
533 LYXERR(Debug::MATHED, "suppressing {} when writing");
538 if (hasDown() /*&& !down().empty()*/)
539 os << "_{" << down() << '}';
541 if (hasUp() /*&& !up().empty()*/) {
542 // insert space if up() is empty or an empty brace inset
544 if (os.latex() && (up().empty() ||
545 (up().size() == 1 && up().back()->asBraceInset() &&
546 up().back()->asBraceInset()->cell(0).empty())))
549 os << "^{" << up() << '}';
552 if (lock_ && !os.latex())
557 void InsetMathScript::normalize(NormalStream & os) const
559 bool d = hasDown() && !down().empty();
560 bool u = hasUp() && !up().empty();
575 os << down() << ' ' << up() << ']';
583 void InsetMathScript::maple(MapleStream & os) const
587 if (hasDown() && !down().empty())
588 os << '[' << down() << ']';
589 if (hasUp() && !up().empty())
590 os << "^(" << up() << ')';
594 void InsetMathScript::mathematica(MathematicaStream & os) const
596 bool d = hasDown() && !down().empty();
597 bool u = hasUp() && !up().empty();
599 if (!nuc().empty()) {
601 os << "Subscript[" << nuc();
607 os << "^(" << up() << ')';
609 if (!nuc().empty()) {
611 os << ',' << down() << ']';
616 void InsetMathScript::mathmlize(MathStream & os) const
618 bool d = hasDown() && !down().empty();
619 bool u = hasUp() && !up().empty();
620 bool l = hasLimits();
623 os << MTag(l ? "munderover" : "msubsup");
625 os << MTag(l ? "mover" : "msup");
627 os << MTag(l ? "munder" : "msub");
630 os << MTag("mrow") << nuc() << ETag("mrow");
635 os << MTag("mrow") << down() << ETag("mrow")
636 << MTag("mrow") << up() << ETag("mrow")
637 << ETag(l ? "munderover" : "msubsup");
639 os << MTag("mrow") << up() << ETag("mrow") << ETag(l ? "mover" : "msup");
641 os << MTag("mrow") << down() << ETag("mrow") << ETag(l ? "munder" : "msub");
645 void InsetMathScript::htmlize(HtmlStream & os) const
647 bool d = hasDown() && !down().empty();
648 bool u = hasUp() && !up().empty();
654 os << MTag("span", "class='scripts'")
655 << MTag("span") << up() << ETag("span")
656 << MTag("span") << down() << ETag("span")
659 os << MTag("sup", "class='math'") << up() << ETag("sup");
661 os << MTag("sub", "class='math'") << down() << ETag("sub");
665 void InsetMathScript::octave(OctaveStream & os) const
669 if (hasDown() && !down().empty())
670 os << '[' << down() << ']';
671 if (hasUp() && !up().empty())
672 os << "^(" << up() << ')';
676 void InsetMathScript::infoize(odocstream & os) const
682 void InsetMathScript::infoize2(odocstream & os) const
685 os << from_ascii(limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
689 bool InsetMathScript::notifyCursorLeaves(Cursor const & old, Cursor & cur)
691 InsetMathNest::notifyCursorLeaves(old, cur);
693 //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
695 // Remove empty scripts if possible:
697 // The case of two scripts, but only one got empty (1 = super, 2 = sub).
698 // We keep the script inset, but remove the empty script.
699 if (nargs() > 2 && (!cell(1).empty() || !cell(2).empty())) {
700 if (cell(2).empty()) {
701 // must be a subscript...
702 old.recordUndoInset();
704 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
706 } else if (cell(1).empty()) {
707 // must be a superscript...
708 old.recordUndoInset();
710 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
714 // Now the two suicide cases:
715 // * we have only one script which is empty
716 // * we have two scripts which are both empty.
717 // The script inset is removed completely.
718 if ((nargs() == 2 && cell(1).empty())
719 || (nargs() == 3 && cell(1).empty() && cell(2).empty())) {
720 // Make undo step. We cannot use cur for this because
721 // it does not necessarily point to us anymore. But we
722 // should be on top of the cursor old.
723 Cursor insetCur = old;
724 int scriptSlice = insetCur.find(this);
725 LASSERT(scriptSlice != -1, /**/);
726 insetCur.cutOff(scriptSlice);
727 insetCur.recordUndoInset();
729 // Let the script inset commit suicide. This is
730 // modelled on Cursor.pullArg(), but tries not to
731 // invoke notifyCursorLeaves again and does not touch
732 // cur (since the top slice will be deleted
734 MathData ar = cell(0);
736 insetCur.cell().erase(insetCur.pos());
737 insetCur.cell().insert(insetCur.pos(), ar);
740 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
744 //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
749 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
751 //LYXERR("InsetMathScript: request: " << cmd);
753 if (cmd.action() == LFUN_MATH_LIMITS) {
754 cur.recordUndoInset();
755 if (!cmd.argument().empty()) {
756 if (cmd.argument() == "limits")
758 else if (cmd.argument() == "nolimits")
762 } else if (limits_ == 0)
763 limits_ = hasLimits() ? -1 : 1;
769 InsetMathNest::doDispatch(cur, cmd);
773 bool InsetMathScript::getStatus(Cursor & cur, FuncRequest const & cmd,
774 FuncStatus & flag) const
776 if (cmd.action() == LFUN_MATH_LIMITS) {
777 if (!cmd.argument().empty()) {
778 if (cmd.argument() == "limits")
779 flag.setOnOff(limits_ == 1);
780 else if (cmd.argument() == "nolimits")
781 flag.setOnOff(limits_ == -1);
783 flag.setOnOff(limits_ == 0);
785 flag.setEnabled(true);
789 return InsetMathNest::getStatus(cur, cmd, flag);
793 // the idea for dual scripts came from the eLyXer code
794 void InsetMathScript::validate(LaTeXFeatures & features) const
796 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
797 features.addCSSSnippet(
798 "span.scripts{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
799 "span.scripts span {display: block;}\n"
800 "sub.math{font-size: 75%;}\n"
801 "sup.math{font-size: 75%;}");
802 InsetMathNest::validate(features);