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 "InsetMathFont.h"
19 #include "InsetMathScript.h"
20 #include "InsetMathSymbol.h"
21 #include "LaTeXFeatures.h"
23 #include "MathStream.h"
24 #include "MathSupport.h"
26 #include "support/debug.h"
28 #include "support/lassert.h"
37 InsetMathScript::InsetMathScript(Buffer * buf)
38 : InsetMathNest(buf, 1), cell_1_is_up_(false), limits_(0)
42 InsetMathScript::InsetMathScript(Buffer * buf, bool up)
43 : InsetMathNest(buf, 2), cell_1_is_up_(up), limits_(0)
47 InsetMathScript::InsetMathScript(Buffer * buf, MathAtom const & at, bool up)
48 : InsetMathNest(buf, 2), cell_1_is_up_(up), limits_(0)
50 LASSERT(nargs() >= 1, /**/);
51 cell(0).push_back(at);
55 Inset * InsetMathScript::clone() const
57 return new InsetMathScript(*this);
61 InsetMathScript const * InsetMathScript::asScriptInset() const
67 InsetMathScript * InsetMathScript::asScriptInset()
73 bool InsetMathScript::idxFirst(Cursor & cur) const
81 bool InsetMathScript::idxLast(Cursor & cur) const
84 cur.pos() = nuc().size();
89 MathData const & InsetMathScript::down() const
93 LASSERT(nargs() > 1, /**/);
98 MathData & InsetMathScript::down()
102 LASSERT(nargs() > 1, /**/);
107 MathData const & InsetMathScript::up() const
109 LASSERT(nargs() > 1, /**/);
114 MathData & InsetMathScript::up()
116 LASSERT(nargs() > 1, /**/);
121 void InsetMathScript::ensure(bool up)
124 // just nucleus so far
125 cells_.push_back(MathData());
127 } else if (nargs() == 2 && !has(up)) {
129 cells_.push_back(cell(1));
132 cells_.push_back(MathData());
138 MathData const & InsetMathScript::nuc() const
144 MathData & InsetMathScript::nuc()
152 bool isAlphaSymbol(MathAtom const & at)
154 if (at->asCharInset() ||
155 (at->asSymbolInset() &&
156 at->asSymbolInset()->isOrdAlpha()))
159 if (at->asFontInset()) {
160 MathData const & ar = at->asFontInset()->cell(0);
161 for (size_t i = 0; i < ar.size(); ++i) {
162 if (!(ar[i]->asCharInset() ||
163 (ar[i]->asSymbolInset() &&
164 ar[i]->asSymbolInset()->isOrdAlpha())))
175 int InsetMathScript::dy01(BufferView const & bv, int asc, int des, int what) const
179 bool isCharBox = nuc().size() ? isAlphaSymbol(nuc().back()) : false;
181 Dimension const & dimdown = down().dimension(bv);
182 dasc = dimdown.ascent();
183 slevel = nuc().slevel();
184 int ascdrop = dasc - slevel;
185 int desdrop = isCharBox ? 0 : des + nuc().sshift();
186 int mindes = nuc().mindes();
187 des = max(desdrop, ascdrop);
188 des = max(mindes, des);
191 Dimension const & dimup = up().dimension(bv);
192 int minasc = nuc().minasc();
193 int ascdrop = isCharBox ? 0 : asc - up().mindes();
194 int udes = dimup.descent();
195 asc = udes + nuc().sshift();
196 asc = max(ascdrop, asc);
197 asc = max(minasc, asc);
199 int del = asc - udes - dasc;
200 if (del + des <= 2) {
201 int newdes = 2 - del;
202 del = slevel - asc + udes;
207 des = max(des, newdes);
211 return what ? asc : des;
215 int InsetMathScript::dy0(BufferView const & bv) const
220 int des = down().dimension(bv).ascent();
225 des = dy01(bv, na, nd, 0);
231 int InsetMathScript::dy1(BufferView const & bv) const
236 int asc = up().dimension(bv).descent();
241 asc = dy01(bv, na, nd, 1);
248 int InsetMathScript::dx0(BufferView const & bv) const
250 LASSERT(hasDown(), /**/);
251 Dimension const dim = dimension(bv);
252 return hasLimits() ? (dim.wid - down().dimension(bv).width()) / 2 : nwid(bv);
256 int InsetMathScript::dx1(BufferView const & bv) const
258 LASSERT(hasUp(), /**/);
259 Dimension const dim = dimension(bv);
260 return hasLimits() ? (dim.wid - up().dimension(bv).width()) / 2 : nwid(bv) + nker(&bv);
264 int InsetMathScript::dxx(BufferView const & bv) const
266 Dimension const dim = dimension(bv);
267 return hasLimits() ? (dim.wid - nwid(bv)) / 2 : 0;
271 int InsetMathScript::nwid(BufferView const & bv) const
273 return nuc().size() ? nuc().dimension(bv).width() : 2;
277 int InsetMathScript::nasc(BufferView const & bv) const
279 return nuc().size() ? nuc().dimension(bv).ascent() : 5;
283 int InsetMathScript::ndes(BufferView const & bv) const
285 return nuc().size() ? nuc().dimension(bv).descent() : 0;
289 int InsetMathScript::nker(BufferView const * bv) const
292 int kerning = nuc().kerning(bv);
293 return kerning > 0 ? kerning : 0;
299 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
304 cell(0).metrics(mi, dim0);
305 ScriptChanger dummy(mi.base);
307 cell(1).metrics(mi, dim1);
309 cell(2).metrics(mi, dim2);
312 BufferView & bv = *mi.base.bv;
313 // FIXME: data copying... not very efficient.
317 dimup = up().dimension(bv);
319 dimdown = down().dimension(bv);
324 dim.wid = max(dim.wid, dimup.width());
326 dim.wid = max(dim.wid, dimdown.width());
329 dim.wid = max(dim.wid, nker(mi.base.bv) + dimup.width());
331 dim.wid = max(dim.wid, dimdown.width());
336 int asc = dy1(bv) + dimup.ascent();
337 dim.asc = max(na, asc);
342 int des = dy0(bv) + dimdown.descent();
343 dim.des = max(nd, des);
350 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
352 BufferView & bv = *pi.base.bv;
354 nuc().draw(pi, x + dxx(bv), y);
356 nuc().setXY(bv, x + dxx(bv), y);
358 pi.draw(x + dxx(bv), y, char_type('.'));
360 ScriptChanger dummy(pi.base);
362 up().draw(pi, x + dx1(bv), y - dy1(bv));
364 down().draw(pi, x + dx0(bv), y + dy0(bv));
365 drawMarkers(pi, x, y);
369 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
372 up().metricsT(mi, dim);
374 down().metricsT(mi, dim);
375 nuc().metricsT(mi, dim);
379 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
383 nuc().drawT(pain, x + 1, y);
385 up().drawT(pain, x + 1, y - 1 /*dy1()*/);
387 down().drawT(pain, x + 1, y + 1 /*dy0()*/);
392 bool InsetMathScript::hasLimits() const
400 // we can only display limits if the nucleus wants some
403 if (!nuc().back()->isScriptable())
406 if (nuc().back()->asSymbolInset()) {
407 // \intop is an alias for \int\limits, \ointop == \oint\limits
408 if (nuc().back()->asSymbolInset()->name().find(from_ascii("intop")) != string::npos)
410 // per default \int has limits beside the \int even in displayed formulas
411 if (nuc().back()->asSymbolInset()->name().find(from_ascii("int")) != string::npos)
415 // assume "real" limits for everything else
420 void InsetMathScript::removeScript(bool up)
423 if (up == cell_1_is_up_)
425 } else if (nargs() == 3) {
427 swap(cells_[1], cells_[2]);
428 cell_1_is_up_ = false;
430 cell_1_is_up_ = true;
437 bool InsetMathScript::has(bool up) const
439 return idxOfScript(up);
443 bool InsetMathScript::hasUp() const
445 //lyxerr << "1up: " << bool(cell_1_is_up_));
446 //lyxerr << "hasUp: " << bool(idxOfScript(true)));
447 return idxOfScript(true);
451 bool InsetMathScript::hasDown() const
453 //LYXERR0("1up: " << bool(cell_1_is_up_));
454 //LYXERR0("hasDown: " << bool(idxOfScript(false)));
455 return idxOfScript(false);
459 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
464 return (cell_1_is_up_ == up) ? 1 : 0;
467 LASSERT(false, /**/);
473 bool InsetMathScript::idxForward(Cursor &) const
479 bool InsetMathScript::idxBackward(Cursor &) const
485 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
488 if (cur.idx() == 0) {
489 // don't go up/down if there is no cell in this direction
492 // go up/down only if in the last position
493 // or in the first position of something with displayed limits
494 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
495 cur.idx() = idxOfScript(up);
503 if (cur.idx() == idxOfScript(true)) {
504 // can't go further up
507 // otherwise go to last position in the nucleus
509 cur.pos() = cur.lastpos();
514 if (cur.idx() == idxOfScript(false)) {
515 // can't go further down
518 // otherwise go to last position in the nucleus
520 cur.pos() = cur.lastpos();
528 void InsetMathScript::write(WriteStream & os) const
530 MathEnsurer ensurer(os);
534 //if (nuc().back()->takesLimits()) {
542 LYXERR(Debug::MATHED, "suppressing {} when writing");
547 if (hasDown() /*&& down().size()*/)
548 os << "_{" << down() << '}';
550 if (hasUp() /*&& up().size()*/)
551 os << "^{" << up() << '}';
553 if (lock_ && !os.latex())
558 void InsetMathScript::normalize(NormalStream & os) const
560 bool d = hasDown() && down().size();
561 bool u = hasUp() && up().size();
576 os << down() << ' ' << up() << ']';
584 void InsetMathScript::maple(MapleStream & os) const
588 if (hasDown() && down().size())
589 os << '[' << down() << ']';
590 if (hasUp() && up().size())
591 os << "^(" << up() << ')';
595 void InsetMathScript::mathematica(MathematicaStream & os) const
597 bool d = hasDown() && down().size();
598 bool u = hasUp() && up().size();
602 os << "Subscript[" << nuc();
608 os << "^(" << up() << ')';
612 os << ',' << down() << ']';
618 // It may be worth trying to output munder, mover, and munderover
619 // in certain cases, e.g., for display formulas. But then we would
620 // need to know if we're in a display formula.
621 void InsetMathScript::mathmlize(MathStream & os) const
623 bool d = hasDown() && down().size();
624 bool u = hasUp() && up().size();
627 os << MTag("msubsup");
634 os << MTag("mrow") << nuc() << ETag("mrow");
639 os << MTag("mrow") << down() << ETag("mrow")
640 << MTag("mrow") << up() << ETag("mrow")
643 os << MTag("mrow") << up() << ETag("mrow") << ETag("msup");
645 os << MTag("mrow") << down() << ETag("mrow") << ETag("msub");
649 void InsetMathScript::htmlize(HtmlStream & os) const
651 bool d = hasDown() && down().size();
652 bool u = hasUp() && up().size();
658 os << MTag("span", "class='scripts'")
659 << MTag("span") << up() << ETag("span")
660 << MTag("span") << down() << ETag("span")
663 os << MTag("sup", "class='math'") << up() << ETag("sup");
665 os << MTag("sub", "class='math'") << down() << ETag("sub");
669 void InsetMathScript::octave(OctaveStream & os) const
673 if (hasDown() && down().size())
674 os << '[' << down() << ']';
675 if (hasUp() && up().size())
676 os << "^(" << up() << ')';
680 void InsetMathScript::infoize(odocstream & os) const
686 void InsetMathScript::infoize2(odocstream & os) const
689 os << from_ascii(limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
693 bool InsetMathScript::notifyCursorLeaves(Cursor const & old, Cursor & cur)
695 InsetMathNest::notifyCursorLeaves(old, cur);
697 //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
699 // Remove empty scripts if possible:
701 // The case of two scripts, but only one got empty (1 = super, 2 = sub).
702 // We keep the script inset, but remove the empty script.
703 if (nargs() > 2 && (!cell(1).empty() || !cell(2).empty())) {
704 if (cell(2).empty()) {
705 // must be a subscript...
706 old.recordUndoInset();
708 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
710 } else if (cell(1).empty()) {
711 // must be a superscript...
712 old.recordUndoInset();
714 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
718 // Now the two suicide cases:
719 // * we have only one script which is empty
720 // * we have two scripts which are both empty.
721 // The script inset is removed completely.
722 if ((nargs() == 2 && cell(1).empty())
723 || (nargs() == 3 && cell(1).empty() && cell(2).empty())) {
724 // Make undo step. We cannot use cur for this because
725 // it does not necessarily point to us anymore. But we
726 // should be on top of the cursor old.
727 Cursor insetCur = old;
728 int scriptSlice = insetCur.find(this);
729 LASSERT(scriptSlice != -1, /**/);
730 insetCur.cutOff(scriptSlice);
731 insetCur.recordUndoInset();
733 // Let the script inset commit suicide. This is
734 // modelled on Cursor.pullArg(), but tries not to
735 // invoke notifyCursorLeaves again and does not touch
736 // cur (since the top slice will be deleted
738 MathData ar = cell(0);
740 insetCur.cell().erase(insetCur.pos());
741 insetCur.cell().insert(insetCur.pos(), ar);
744 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
748 //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
753 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
755 //LYXERR("InsetMathScript: request: " << cmd);
757 if (cmd.action() == LFUN_MATH_LIMITS) {
758 cur.recordUndoInset();
759 if (!cmd.argument().empty()) {
760 if (cmd.argument() == "limits")
762 else if (cmd.argument() == "nolimits")
766 } else if (limits_ == 0)
767 limits_ = hasLimits() ? -1 : 1;
773 InsetMathNest::doDispatch(cur, cmd);
777 bool InsetMathScript::getStatus(Cursor & cur, FuncRequest const & cmd,
778 FuncStatus & flag) const
780 if (cmd.action() == LFUN_MATH_LIMITS) {
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);
793 return InsetMathNest::getStatus(cur, cmd, flag);
797 // the idea for dual scripts came from the eLyXer code
798 void InsetMathScript::validate(LaTeXFeatures & features) const
800 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
801 features.addCSSSnippet(
802 "span.scripts{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
803 "span.scripts span {display: block;}\n"
804 "sub.math{font-size: 75%;}\n"
805 "sup.math{font-size: 75%;}");
806 InsetMathNest::validate(features);