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 "FuncStatus.h"
18 #include "InsetMathBrace.h"
19 #include "InsetMathFont.h"
20 #include "InsetMathScript.h"
21 #include "InsetMathSymbol.h"
22 #include "LaTeXFeatures.h"
24 #include "MathStream.h"
25 #include "MathSupport.h"
27 #include "support/debug.h"
29 #include "support/lassert.h"
38 InsetMathScript::InsetMathScript(Buffer * buf)
39 : InsetMathNest(buf, 1), cell_1_is_up_(false), limits_(0)
43 InsetMathScript::InsetMathScript(Buffer * buf, bool up)
44 : InsetMathNest(buf, 2), cell_1_is_up_(up), limits_(0)
48 InsetMathScript::InsetMathScript(Buffer * buf, MathAtom const & at, bool up)
49 : InsetMathNest(buf, 2), cell_1_is_up_(up), limits_(0)
51 LASSERT(nargs() >= 1, /**/);
52 cell(0).push_back(at);
56 Inset * InsetMathScript::clone() const
58 return new InsetMathScript(*this);
62 InsetMathScript const * InsetMathScript::asScriptInset() const
68 InsetMathScript * InsetMathScript::asScriptInset()
74 bool InsetMathScript::idxFirst(Cursor & cur) const
82 bool InsetMathScript::idxLast(Cursor & cur) const
85 cur.pos() = nuc().size();
90 MathData const & InsetMathScript::down() const
94 LASSERT(nargs() > 1, /**/);
99 MathData & InsetMathScript::down()
103 LASSERT(nargs() > 1, /**/);
108 MathData const & InsetMathScript::up() const
110 LASSERT(nargs() > 1, /**/);
115 MathData & InsetMathScript::up()
117 LASSERT(nargs() > 1, /**/);
122 void InsetMathScript::ensure(bool up)
125 // just nucleus so far
126 cells_.push_back(MathData());
128 } else if (nargs() == 2 && !has(up)) {
130 cells_.push_back(cell(1));
133 cells_.push_back(MathData());
139 MathData const & InsetMathScript::nuc() const
145 MathData & InsetMathScript::nuc()
153 bool isAlphaSymbol(MathAtom const & at)
155 if (at->asCharInset() ||
156 (at->asSymbolInset() &&
157 at->asSymbolInset()->isOrdAlpha()))
160 if (at->asFontInset()) {
161 MathData const & ar = at->asFontInset()->cell(0);
162 for (size_t i = 0; i < ar.size(); ++i) {
163 if (!(ar[i]->asCharInset() ||
164 (ar[i]->asSymbolInset() &&
165 ar[i]->asSymbolInset()->isOrdAlpha())))
176 int InsetMathScript::dy01(BufferView const & bv, int asc, int des, int what) const
180 bool isCharBox = nuc().size() ? isAlphaSymbol(nuc().back()) : false;
182 Dimension const & dimdown = down().dimension(bv);
183 dasc = dimdown.ascent();
184 slevel = nuc().slevel();
185 int ascdrop = dasc - slevel;
186 int desdrop = isCharBox ? 0 : des + nuc().sshift();
187 int mindes = nuc().mindes();
188 des = max(desdrop, ascdrop);
189 des = max(mindes, des);
192 Dimension const & dimup = up().dimension(bv);
193 int minasc = nuc().minasc();
194 int ascdrop = isCharBox ? 0 : asc - up().mindes();
195 int udes = dimup.descent();
196 asc = udes + nuc().sshift();
197 asc = max(ascdrop, asc);
198 asc = max(minasc, asc);
200 int del = asc - udes - dasc;
201 if (del + des <= 2) {
202 int newdes = 2 - del;
203 del = slevel - asc + udes;
208 des = max(des, newdes);
212 return what ? asc : des;
216 int InsetMathScript::dy0(BufferView const & bv) const
221 int des = down().dimension(bv).ascent();
226 des = dy01(bv, na, nd, 0);
232 int InsetMathScript::dy1(BufferView const & bv) const
237 int asc = up().dimension(bv).descent();
242 asc = dy01(bv, na, nd, 1);
249 int InsetMathScript::dx0(BufferView const & bv) const
251 LASSERT(hasDown(), /**/);
252 Dimension const dim = dimension(bv);
253 return hasLimits() ? (dim.wid - down().dimension(bv).width()) / 2 : nwid(bv);
257 int InsetMathScript::dx1(BufferView const & bv) const
259 LASSERT(hasUp(), /**/);
260 Dimension const dim = dimension(bv);
261 return hasLimits() ? (dim.wid - up().dimension(bv).width()) / 2 : nwid(bv) + nker(&bv);
265 int InsetMathScript::dxx(BufferView const & bv) const
267 Dimension const dim = dimension(bv);
268 return hasLimits() ? (dim.wid - nwid(bv)) / 2 : 0;
272 int InsetMathScript::nwid(BufferView const & bv) const
274 return nuc().size() ? nuc().dimension(bv).width() : 2;
278 int InsetMathScript::nasc(BufferView const & bv) const
280 return nuc().size() ? nuc().dimension(bv).ascent() : 5;
284 int InsetMathScript::ndes(BufferView const & bv) const
286 return nuc().size() ? nuc().dimension(bv).descent() : 0;
290 int InsetMathScript::nker(BufferView const * bv) const
293 int kerning = nuc().kerning(bv);
294 return kerning > 0 ? kerning : 0;
300 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
305 cell(0).metrics(mi, dim0);
306 ScriptChanger dummy(mi.base);
308 cell(1).metrics(mi, dim1);
310 cell(2).metrics(mi, dim2);
313 BufferView & bv = *mi.base.bv;
314 // FIXME: data copying... not very efficient.
318 dimup = up().dimension(bv);
320 dimdown = down().dimension(bv);
325 dim.wid = max(dim.wid, dimup.width());
327 dim.wid = max(dim.wid, dimdown.width());
330 dim.wid = max(dim.wid, nker(mi.base.bv) + dimup.width());
332 dim.wid = max(dim.wid, dimdown.width());
337 int asc = dy1(bv) + dimup.ascent();
338 dim.asc = max(na, asc);
343 int des = dy0(bv) + dimdown.descent();
344 dim.des = max(nd, des);
351 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
353 BufferView & bv = *pi.base.bv;
355 nuc().draw(pi, x + dxx(bv), y);
357 nuc().setXY(bv, x + dxx(bv), y);
359 pi.draw(x + dxx(bv), y, char_type('.'));
361 ScriptChanger dummy(pi.base);
363 up().draw(pi, x + dx1(bv), y - dy1(bv));
365 down().draw(pi, x + dx0(bv), y + dy0(bv));
366 drawMarkers(pi, x, y);
370 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
373 up().metricsT(mi, dim);
375 down().metricsT(mi, dim);
376 nuc().metricsT(mi, dim);
380 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
384 nuc().drawT(pain, x + 1, y);
386 up().drawT(pain, x + 1, y - 1 /*dy1()*/);
388 down().drawT(pain, x + 1, y + 1 /*dy0()*/);
393 bool InsetMathScript::hasLimits() const
401 // we can only display limits if the nucleus wants some
404 if (!nuc().back()->isScriptable())
407 if (nuc().back()->asSymbolInset()) {
408 // \intop is an alias for \int\limits, \ointop == \oint\limits
409 if (nuc().back()->asSymbolInset()->name().find(from_ascii("intop")) != string::npos)
411 // per default \int has limits beside the \int even in displayed formulas
412 if (nuc().back()->asSymbolInset()->name().find(from_ascii("int")) != string::npos)
416 // assume "real" limits for everything else
421 void InsetMathScript::removeScript(bool up)
424 if (up == cell_1_is_up_)
426 } else if (nargs() == 3) {
428 swap(cells_[1], cells_[2]);
429 cell_1_is_up_ = false;
431 cell_1_is_up_ = true;
438 bool InsetMathScript::has(bool up) const
440 return idxOfScript(up);
444 bool InsetMathScript::hasUp() const
446 //lyxerr << "1up: " << bool(cell_1_is_up_));
447 //lyxerr << "hasUp: " << bool(idxOfScript(true)));
448 return idxOfScript(true);
452 bool InsetMathScript::hasDown() const
454 //LYXERR0("1up: " << bool(cell_1_is_up_));
455 //LYXERR0("hasDown: " << bool(idxOfScript(false)));
456 return idxOfScript(false);
460 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
465 return (cell_1_is_up_ == up) ? 1 : 0;
468 LASSERT(false, /**/);
474 bool InsetMathScript::idxForward(Cursor &) const
480 bool InsetMathScript::idxBackward(Cursor &) const
486 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
489 if (cur.idx() == 0) {
490 // don't go up/down if there is no cell in this direction
493 // go up/down only if in the last position
494 // or in the first position of something with displayed limits
495 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
496 cur.idx() = idxOfScript(up);
504 if (cur.idx() == idxOfScript(true)) {
505 // can't go further up
508 // otherwise go to last position in the nucleus
510 cur.pos() = cur.lastpos();
515 if (cur.idx() == idxOfScript(false)) {
516 // can't go further down
519 // otherwise go to last position in the nucleus
521 cur.pos() = cur.lastpos();
529 void InsetMathScript::write(WriteStream & os) const
531 MathEnsurer ensurer(os);
535 //if (nuc().back()->takesLimits()) {
543 LYXERR(Debug::MATHED, "suppressing {} when writing");
548 if (hasDown() /*&& down().size()*/)
549 os << "_{" << down() << '}';
551 if (hasUp() /*&& up().size()*/) {
552 // insert space if up() is empty or an empty brace inset
554 if (os.latex() && (up().size() == 0 ||
555 (up().size() == 1 && up().back()->asBraceInset() &&
556 up().back()->asBraceInset()->cell(0).empty())))
559 os << "^{" << up() << '}';
562 if (lock_ && !os.latex())
567 void InsetMathScript::normalize(NormalStream & os) const
569 bool d = hasDown() && down().size();
570 bool u = hasUp() && up().size();
585 os << down() << ' ' << up() << ']';
593 void InsetMathScript::maple(MapleStream & os) const
597 if (hasDown() && down().size())
598 os << '[' << down() << ']';
599 if (hasUp() && up().size())
600 os << "^(" << up() << ')';
604 void InsetMathScript::mathematica(MathematicaStream & os) const
606 bool d = hasDown() && down().size();
607 bool u = hasUp() && up().size();
611 os << "Subscript[" << nuc();
617 os << "^(" << up() << ')';
621 os << ',' << down() << ']';
627 // It may be worth trying to output munder, mover, and munderover
628 // in certain cases, e.g., for display formulas. But then we would
629 // need to know if we're in a display formula.
630 void InsetMathScript::mathmlize(MathStream & os) const
632 bool d = hasDown() && down().size();
633 bool u = hasUp() && up().size();
636 os << MTag("msubsup");
643 os << MTag("mrow") << nuc() << ETag("mrow");
648 os << MTag("mrow") << down() << ETag("mrow")
649 << MTag("mrow") << up() << ETag("mrow")
652 os << MTag("mrow") << up() << ETag("mrow") << ETag("msup");
654 os << MTag("mrow") << down() << ETag("mrow") << ETag("msub");
658 void InsetMathScript::htmlize(HtmlStream & os) const
660 bool d = hasDown() && down().size();
661 bool u = hasUp() && up().size();
667 os << MTag("span", "class='scripts'")
668 << MTag("span") << up() << ETag("span")
669 << MTag("span") << down() << ETag("span")
672 os << MTag("sup", "class='math'") << up() << ETag("sup");
674 os << MTag("sub", "class='math'") << down() << ETag("sub");
678 void InsetMathScript::octave(OctaveStream & os) const
682 if (hasDown() && down().size())
683 os << '[' << down() << ']';
684 if (hasUp() && up().size())
685 os << "^(" << up() << ')';
689 void InsetMathScript::infoize(odocstream & os) const
695 void InsetMathScript::infoize2(odocstream & os) const
698 os << from_ascii(limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
702 bool InsetMathScript::notifyCursorLeaves(Cursor const & old, Cursor & cur)
704 InsetMathNest::notifyCursorLeaves(old, cur);
706 //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
708 // Remove empty scripts if possible:
710 // The case of two scripts, but only one got empty (1 = super, 2 = sub).
711 // We keep the script inset, but remove the empty script.
712 if (nargs() > 2 && (!cell(1).empty() || !cell(2).empty())) {
713 if (cell(2).empty()) {
714 // must be a subscript...
715 old.recordUndoInset();
717 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
719 } else if (cell(1).empty()) {
720 // must be a superscript...
721 old.recordUndoInset();
723 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
727 // Now the two suicide cases:
728 // * we have only one script which is empty
729 // * we have two scripts which are both empty.
730 // The script inset is removed completely.
731 if ((nargs() == 2 && cell(1).empty())
732 || (nargs() == 3 && cell(1).empty() && cell(2).empty())) {
733 // Make undo step. We cannot use cur for this because
734 // it does not necessarily point to us anymore. But we
735 // should be on top of the cursor old.
736 Cursor insetCur = old;
737 int scriptSlice = insetCur.find(this);
738 LASSERT(scriptSlice != -1, /**/);
739 insetCur.cutOff(scriptSlice);
740 insetCur.recordUndoInset();
742 // Let the script inset commit suicide. This is
743 // modelled on Cursor.pullArg(), but tries not to
744 // invoke notifyCursorLeaves again and does not touch
745 // cur (since the top slice will be deleted
747 MathData ar = cell(0);
749 insetCur.cell().erase(insetCur.pos());
750 insetCur.cell().insert(insetCur.pos(), ar);
753 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
757 //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
762 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
764 //LYXERR("InsetMathScript: request: " << cmd);
766 if (cmd.action() == LFUN_MATH_LIMITS) {
767 cur.recordUndoInset();
768 if (!cmd.argument().empty()) {
769 if (cmd.argument() == "limits")
771 else if (cmd.argument() == "nolimits")
775 } else if (limits_ == 0)
776 limits_ = hasLimits() ? -1 : 1;
782 InsetMathNest::doDispatch(cur, cmd);
786 bool InsetMathScript::getStatus(Cursor & cur, FuncRequest const & cmd,
787 FuncStatus & flag) const
789 if (cmd.action() == LFUN_MATH_LIMITS) {
790 if (!cmd.argument().empty()) {
791 if (cmd.argument() == "limits")
792 flag.setOnOff(limits_ == 1);
793 else if (cmd.argument() == "nolimits")
794 flag.setOnOff(limits_ == -1);
796 flag.setOnOff(limits_ == 0);
798 flag.setEnabled(true);
802 return InsetMathNest::getStatus(cur, cmd, flag);
806 // the idea for dual scripts came from the eLyXer code
807 void InsetMathScript::validate(LaTeXFeatures & features) const
809 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
810 features.addCSSSnippet(
811 "span.scripts{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
812 "span.scripts span {display: block;}\n"
813 "sub.math{font-size: 75%;}\n"
814 "sup.math{font-size: 75%;}");
815 InsetMathNest::validate(features);