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 MathData const & InsetMathScript::down() const
84 MathData & InsetMathScript::down()
93 MathData const & InsetMathScript::up() const
100 MathData & InsetMathScript::up()
102 LBUFERR(nargs() > 1);
107 void InsetMathScript::ensure(bool up)
110 // just nucleus so far
111 cells_.push_back(MathData());
113 } else if (nargs() == 2 && !has(up)) {
115 cells_.push_back(cell(1));
118 cells_.push_back(MathData());
124 MathData const & InsetMathScript::nuc() const
130 MathData & InsetMathScript::nuc()
136 int InsetMathScript::dy01(BufferView const & bv, int asc, int des, int what) const
140 bool isCharBox = !nuc().empty() ? isAlphaSymbol(nuc().back()) : false;
142 Dimension const & dimdown = down().dimension(bv);
143 dasc = dimdown.ascent();
144 slevel = nuc().slevel();
145 int ascdrop = dasc - slevel;
146 int desdrop = isCharBox ? 0 : des + nuc().sshift();
147 int mindes = nuc().mindes();
148 des = max(desdrop, ascdrop);
149 des = max(mindes, des);
152 Dimension const & dimup = up().dimension(bv);
153 int minasc = nuc().minasc();
154 int ascdrop = isCharBox ? 0 : asc - up().mindes();
155 int udes = dimup.descent();
156 asc = udes + nuc().sshift();
157 asc = max(ascdrop, asc);
158 asc = max(minasc, asc);
160 int del = asc - udes - dasc;
161 if (del + des <= 2) {
162 int newdes = 2 - del;
163 del = slevel - asc + udes;
168 des = max(des, newdes);
172 return what ? asc : des;
176 int InsetMathScript::dy0(BufferView const & bv) const
181 int des = down().dimension(bv).ascent();
186 des = dy01(bv, na, nd, 0);
192 int InsetMathScript::dy1(BufferView const & bv) const
197 int asc = up().dimension(bv).descent();
202 asc = dy01(bv, na, nd, 1);
209 int InsetMathScript::dx0(BufferView const & bv) const
211 LASSERT(hasDown(), return 0);
212 Dimension const dim = dimension(bv);
213 return hasLimits() ? (dim.wid - down().dimension(bv).width()) / 2
214 : nwid(bv) + min(nker(&bv), 0);
218 int InsetMathScript::dx1(BufferView const & bv) const
220 LASSERT(hasUp(), return 0);
221 Dimension const dim = dimension(bv);
222 return hasLimits() ? (dim.wid - up().dimension(bv).width()) / 2
223 : nwid(bv) + max(nker(&bv), 0);
227 int InsetMathScript::dxx(BufferView const & bv) const
229 Dimension const dim = dimension(bv);
230 return hasLimits() ? (dim.wid - nwid(bv)) / 2 : 0;
234 int InsetMathScript::nwid(BufferView const & bv) const
236 return !nuc().empty() ? nuc().dimension(bv).width() : 2;
240 int InsetMathScript::nasc(BufferView const & bv) const
242 return !nuc().empty() ? nuc().dimension(bv).ascent() : 5;
246 int InsetMathScript::ndes(BufferView const & bv) const
248 return !nuc().empty() ? nuc().dimension(bv).descent() : 0;
252 int InsetMathScript::nker(BufferView const * bv) const
255 return nuc().kerning(bv);
260 MathClass InsetMathScript::mathClass() const
262 // FIXME: this is a hack, since the class will not be correct if
263 // the nucleus has several elements or if the last element is a math macro
264 // or a macro argument proxy.
265 // The correct implementation would require to linearize the nucleus.
269 // return the class of last element since this is the one that counts.
270 MathClass mc = nuc().back()->mathClass();
271 return (mc == MC_UNKNOWN) ? MC_ORD : mc;
276 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
278 Changer dummy2 = mi.base.changeEnsureMath();
282 cell(0).metrics(mi, dim0);
283 Changer dummy = mi.base.changeScript();
285 cell(1).metrics(mi, dim1, !hasLimits());
287 cell(2).metrics(mi, dim2, !hasLimits());
290 BufferView & bv = *mi.base.bv;
291 // FIXME: data copying... not very efficient.
295 dimup = up().dimension(bv);
297 dimdown = down().dimension(bv);
302 dim.wid = max(dim.wid, dimup.width());
304 dim.wid = max(dim.wid, dimdown.width());
307 dim.wid = max(dim.wid, max(nker(mi.base.bv), 0) + dimup.width());
309 dim.wid = max(dim.wid, min(nker(mi.base.bv), 0) + dimdown.width());
314 int asc = dy1(bv) + dimup.ascent();
315 dim.asc = max(na, asc);
320 int des = dy0(bv) + dimdown.descent();
321 dim.des = max(nd, des);
327 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
329 Changer dummy2 = pi.base.changeEnsureMath();
330 BufferView & bv = *pi.base.bv;
332 nuc().draw(pi, x + dxx(bv), y);
334 nuc().setXY(bv, x + dxx(bv), y);
336 pi.draw(x + dxx(bv), y, char_type('.'));
338 Changer dummy = pi.base.changeScript();
340 up().draw(pi, x + dx1(bv), y - dy1(bv));
342 down().draw(pi, x + dx0(bv), y + dy0(bv));
346 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
349 up().metricsT(mi, dim);
351 down().metricsT(mi, dim);
352 nuc().metricsT(mi, dim);
356 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
360 nuc().drawT(pain, x + 1, y);
362 up().drawT(pain, x + 1, y - 1 /*dy1()*/);
364 down().drawT(pain, x + 1, y + 1 /*dy0()*/);
368 // FIXME: See InsetMathSymbol::takesLimits, which seems to attempt the
369 // same in a hardcoded way. takeLimits use is currently commented out in
370 // InsetMathScript::metrics. It seems that the mathop test is general
371 // enough, but only time will tell.
372 bool InsetMathScript::allowsLimits() const
376 // Only makes sense for insets of mathop class
377 if (nuc().back()->mathClass() != MC_OP)
383 bool InsetMathScript::hasLimits() const
391 // we can only display limits if the nucleus wants some
394 // FIXME: this is some hardcoding done in InsetMathSymbol::metrics.
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 & ms) const
617 bool d = hasDown() && !down().empty();
618 bool u = hasUp() && !up().empty();
619 bool l = hasLimits();
622 ms << MTag(l ? "munderover" : "msubsup");
624 ms << MTag(l ? "mover" : "msup");
626 ms << MTag(l ? "munder" : "msub");
629 ms << MTag("mrow") << nuc() << ETag("mrow");
631 ms << "<" << from_ascii(ms.namespacedTag("mrow")) << " />";
634 ms << MTag("mrow") << down() << ETag("mrow")
635 << MTag("mrow") << up() << ETag("mrow")
636 << ETag(l ? "munderover" : "msubsup");
638 ms << MTag("mrow") << up() << ETag("mrow") << ETag(l ? "mover" : "msup");
640 ms << 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 // only when nucleus allows this
756 cur.recordUndoInset();
757 if (!cmd.argument().empty()) {
758 if (cmd.argument() == "limits")
760 else if (cmd.argument() == "nolimits")
764 } else if (limits_ == 0)
765 limits_ = hasLimits() ? -1 : 1;
771 InsetMathNest::doDispatch(cur, cmd);
775 bool InsetMathScript::getStatus(Cursor & cur, FuncRequest const & cmd,
776 FuncStatus & flag) const
778 if (cmd.action() == LFUN_MATH_LIMITS) {
779 // only when nucleus allows this
780 if (allowsLimits()) {
781 if (!cmd.argument().empty()) {
782 if (cmd.argument() == "limits")
783 flag.setOnOff(limits_ == 1);
784 else if (cmd.argument() == "nolimits")
785 flag.setOnOff(limits_ == -1);
787 flag.setOnOff(limits_ == 0);
789 flag.setEnabled(true);
791 flag.setEnabled(false);
795 return InsetMathNest::getStatus(cur, cmd, flag);
799 // the idea for dual scripts came from the eLyXer code
800 void InsetMathScript::validate(LaTeXFeatures & features) const
802 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
803 features.addCSSSnippet(
804 "span.scripts{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
805 "span.scripts span {display: block;}\n"
806 "sub.math{font-size: 75%;}\n"
807 "sup.math{font-size: 75%;}");
808 InsetMathNest::validate(features);