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 "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"
36 InsetMathScript::InsetMathScript(Buffer * buf)
37 : InsetMathNest(buf, 1), cell_1_is_up_(false), limits_(0)
41 InsetMathScript::InsetMathScript(Buffer * buf, bool up)
42 : InsetMathNest(buf, 2), cell_1_is_up_(up), limits_(0)
46 InsetMathScript::InsetMathScript(Buffer * buf, MathAtom const & at, bool up)
47 : InsetMathNest(buf, 2), cell_1_is_up_(up), limits_(0)
49 LASSERT(nargs() >= 1, /**/);
50 cell(0).push_back(at);
54 Inset * InsetMathScript::clone() const
56 return new InsetMathScript(*this);
60 InsetMathScript const * InsetMathScript::asScriptInset() const
66 InsetMathScript * InsetMathScript::asScriptInset()
72 bool InsetMathScript::idxFirst(Cursor & cur) const
80 bool InsetMathScript::idxLast(Cursor & cur) const
83 cur.pos() = nuc().size();
88 MathData const & InsetMathScript::down() const
92 LASSERT(nargs() > 1, /**/);
97 MathData & InsetMathScript::down()
101 LASSERT(nargs() > 1, /**/);
106 MathData const & InsetMathScript::up() const
108 LASSERT(nargs() > 1, /**/);
113 MathData & InsetMathScript::up()
115 LASSERT(nargs() > 1, /**/);
120 void InsetMathScript::ensure(bool up)
123 // just nucleus so far
124 cells_.push_back(MathData());
126 } else if (nargs() == 2 && !has(up)) {
128 cells_.push_back(cell(1));
131 cells_.push_back(MathData());
137 MathData const & InsetMathScript::nuc() const
143 MathData & InsetMathScript::nuc()
149 int InsetMathScript::dy01(BufferView const & bv, int asc, int des, int what) const
153 bool isCharBox = !nuc().empty() ? isAlphaSymbol(nuc().back()) : false;
155 Dimension const & dimdown = down().dimension(bv);
156 dasc = dimdown.ascent();
157 slevel = nuc().slevel();
158 int ascdrop = dasc - slevel;
159 int desdrop = isCharBox ? 0 : des + nuc().sshift();
160 int mindes = nuc().mindes();
161 des = max(desdrop, ascdrop);
162 des = max(mindes, des);
165 Dimension const & dimup = up().dimension(bv);
166 int minasc = nuc().minasc();
167 int ascdrop = isCharBox ? 0 : asc - up().mindes();
168 int udes = dimup.descent();
169 asc = udes + nuc().sshift();
170 asc = max(ascdrop, asc);
171 asc = max(minasc, asc);
173 int del = asc - udes - dasc;
174 if (del + des <= 2) {
175 int newdes = 2 - del;
176 del = slevel - asc + udes;
181 des = max(des, newdes);
185 return what ? asc : des;
189 int InsetMathScript::dy0(BufferView const & bv) const
194 int des = down().dimension(bv).ascent();
199 des = dy01(bv, na, nd, 0);
205 int InsetMathScript::dy1(BufferView const & bv) const
210 int asc = up().dimension(bv).descent();
215 asc = dy01(bv, na, nd, 1);
222 int InsetMathScript::dx0(BufferView const & bv) const
224 LASSERT(hasDown(), /**/);
225 Dimension const dim = dimension(bv);
226 return hasLimits() ? (dim.wid - down().dimension(bv).width()) / 2 : nwid(bv);
230 int InsetMathScript::dx1(BufferView const & bv) const
232 LASSERT(hasUp(), /**/);
233 Dimension const dim = dimension(bv);
234 return hasLimits() ? (dim.wid - up().dimension(bv).width()) / 2 : nwid(bv) + nker(&bv);
238 int InsetMathScript::dxx(BufferView const & bv) const
240 Dimension const dim = dimension(bv);
241 return hasLimits() ? (dim.wid - nwid(bv)) / 2 : 0;
245 int InsetMathScript::nwid(BufferView const & bv) const
247 return !nuc().empty() ? nuc().dimension(bv).width() : 2;
251 int InsetMathScript::nasc(BufferView const & bv) const
253 return !nuc().empty() ? nuc().dimension(bv).ascent() : 5;
257 int InsetMathScript::ndes(BufferView const & bv) const
259 return !nuc().empty() ? nuc().dimension(bv).descent() : 0;
263 int InsetMathScript::nker(BufferView const * bv) const
265 if (!nuc().empty()) {
266 int kerning = nuc().kerning(bv);
267 return kerning > 0 ? kerning : 0;
273 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
278 cell(0).metrics(mi, dim0);
279 ScriptChanger dummy(mi.base);
281 cell(1).metrics(mi, dim1);
283 cell(2).metrics(mi, dim2);
286 BufferView & bv = *mi.base.bv;
287 // FIXME: data copying... not very efficient.
291 dimup = up().dimension(bv);
293 dimdown = down().dimension(bv);
298 dim.wid = max(dim.wid, dimup.width());
300 dim.wid = max(dim.wid, dimdown.width());
303 dim.wid = max(dim.wid, nker(mi.base.bv) + dimup.width());
305 dim.wid = max(dim.wid, dimdown.width());
310 int asc = dy1(bv) + dimup.ascent();
311 dim.asc = max(na, asc);
316 int des = dy0(bv) + dimdown.descent();
317 dim.des = max(nd, des);
324 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
326 BufferView & bv = *pi.base.bv;
328 nuc().draw(pi, x + dxx(bv), y);
330 nuc().setXY(bv, x + dxx(bv), y);
332 pi.draw(x + dxx(bv), y, char_type('.'));
334 ScriptChanger dummy(pi.base);
336 up().draw(pi, x + dx1(bv), y - dy1(bv));
338 down().draw(pi, x + dx0(bv), y + dy0(bv));
339 drawMarkers(pi, x, y);
343 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
346 up().metricsT(mi, dim);
348 down().metricsT(mi, dim);
349 nuc().metricsT(mi, dim);
353 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
357 nuc().drawT(pain, x + 1, y);
359 up().drawT(pain, x + 1, y - 1 /*dy1()*/);
361 down().drawT(pain, x + 1, y + 1 /*dy0()*/);
366 bool InsetMathScript::hasLimits() const
374 // we can only display limits if the nucleus wants some
377 if (!nuc().back()->isScriptable())
380 if (nuc().back()->asSymbolInset()) {
381 // \intop is an alias for \int\limits, \ointop == \oint\limits
382 if (nuc().back()->asSymbolInset()->name().find(from_ascii("intop")) != string::npos)
384 // per default \int has limits beside the \int even in displayed formulas
385 if (nuc().back()->asSymbolInset()->name().find(from_ascii("int")) != string::npos)
389 // assume "real" limits for everything else
394 void InsetMathScript::removeScript(bool up)
397 if (up == cell_1_is_up_)
399 } else if (nargs() == 3) {
401 swap(cells_[1], cells_[2]);
402 cell_1_is_up_ = false;
404 cell_1_is_up_ = true;
411 bool InsetMathScript::has(bool up) const
413 return idxOfScript(up);
417 bool InsetMathScript::hasUp() const
419 //lyxerr << "1up: " << bool(cell_1_is_up_));
420 //lyxerr << "hasUp: " << bool(idxOfScript(true)));
421 return idxOfScript(true);
425 bool InsetMathScript::hasDown() const
427 //LYXERR0("1up: " << bool(cell_1_is_up_));
428 //LYXERR0("hasDown: " << bool(idxOfScript(false)));
429 return idxOfScript(false);
433 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
438 return (cell_1_is_up_ == up) ? 1 : 0;
441 LASSERT(false, /**/);
447 bool InsetMathScript::idxForward(Cursor &) const
453 bool InsetMathScript::idxBackward(Cursor &) const
459 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
462 if (cur.idx() == 0) {
463 // don't go up/down if there is no cell in this direction
466 // go up/down only if in the last position
467 // or in the first position of something with displayed limits
468 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
469 cur.idx() = idxOfScript(up);
477 if (cur.idx() == idxOfScript(true)) {
478 // can't go further up
481 // otherwise go to last position in the nucleus
483 cur.pos() = cur.lastpos();
488 if (cur.idx() == idxOfScript(false)) {
489 // can't go further down
492 // otherwise go to last position in the nucleus
494 cur.pos() = cur.lastpos();
502 void InsetMathScript::write(WriteStream & os) const
504 MathEnsurer ensurer(os);
506 if (!nuc().empty()) {
508 //if (nuc().back()->takesLimits()) {
516 LYXERR(Debug::MATHED, "suppressing {} when writing");
521 if (hasDown() /*&& !down().empty()*/)
522 os << "_{" << down() << '}';
524 if (hasUp() /*&& !up().empty()*/) {
525 // insert space if up() is empty or an empty brace inset
527 if (os.latex() && (up().empty() ||
528 (up().size() == 1 && up().back()->asBraceInset() &&
529 up().back()->asBraceInset()->cell(0).empty())))
532 os << "^{" << up() << '}';
535 if (lock_ && !os.latex())
540 void InsetMathScript::normalize(NormalStream & os) const
542 bool d = hasDown() && !down().empty();
543 bool u = hasUp() && !up().empty();
558 os << down() << ' ' << up() << ']';
566 void InsetMathScript::maple(MapleStream & os) const
570 if (hasDown() && !down().empty())
571 os << '[' << down() << ']';
572 if (hasUp() && !up().empty())
573 os << "^(" << up() << ')';
577 void InsetMathScript::mathematica(MathematicaStream & os) const
579 bool d = hasDown() && !down().empty();
580 bool u = hasUp() && !up().empty();
582 if (!nuc().empty()) {
584 os << "Subscript[" << nuc();
590 os << "^(" << up() << ')';
592 if (!nuc().empty()) {
594 os << ',' << down() << ']';
600 // It may be worth trying to output munder, mover, and munderover
601 // in certain cases, e.g., for display formulas. But then we would
602 // need to know if we're in a display formula.
603 void InsetMathScript::mathmlize(MathStream & os) const
605 bool d = hasDown() && !down().empty();
606 bool u = hasUp() && !up().empty();
609 os << MTag("msubsup");
616 os << MTag("mrow") << nuc() << ETag("mrow");
621 os << MTag("mrow") << down() << ETag("mrow")
622 << MTag("mrow") << up() << ETag("mrow")
625 os << MTag("mrow") << up() << ETag("mrow") << ETag("msup");
627 os << MTag("mrow") << down() << ETag("mrow") << ETag("msub");
631 void InsetMathScript::htmlize(HtmlStream & os) const
633 bool d = hasDown() && !down().empty();
634 bool u = hasUp() && !up().empty();
640 os << MTag("span", "class='scripts'")
641 << MTag("span") << up() << ETag("span")
642 << MTag("span") << down() << ETag("span")
645 os << MTag("sup", "class='math'") << up() << ETag("sup");
647 os << MTag("sub", "class='math'") << down() << ETag("sub");
651 void InsetMathScript::octave(OctaveStream & os) const
655 if (hasDown() && !down().empty())
656 os << '[' << down() << ']';
657 if (hasUp() && !up().empty())
658 os << "^(" << up() << ')';
662 void InsetMathScript::infoize(odocstream & os) const
668 void InsetMathScript::infoize2(odocstream & os) const
671 os << from_ascii(limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
675 bool InsetMathScript::notifyCursorLeaves(Cursor const & old, Cursor & cur)
677 InsetMathNest::notifyCursorLeaves(old, cur);
679 //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
681 // Remove empty scripts if possible:
683 // The case of two scripts, but only one got empty (1 = super, 2 = sub).
684 // We keep the script inset, but remove the empty script.
685 if (nargs() > 2 && (!cell(1).empty() || !cell(2).empty())) {
686 if (cell(2).empty()) {
687 // must be a subscript...
688 old.recordUndoInset();
690 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
692 } else if (cell(1).empty()) {
693 // must be a superscript...
694 old.recordUndoInset();
696 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
700 // Now the two suicide cases:
701 // * we have only one script which is empty
702 // * we have two scripts which are both empty.
703 // The script inset is removed completely.
704 if ((nargs() == 2 && cell(1).empty())
705 || (nargs() == 3 && cell(1).empty() && cell(2).empty())) {
706 // Make undo step. We cannot use cur for this because
707 // it does not necessarily point to us anymore. But we
708 // should be on top of the cursor old.
709 Cursor insetCur = old;
710 int scriptSlice = insetCur.find(this);
711 LASSERT(scriptSlice != -1, /**/);
712 insetCur.cutOff(scriptSlice);
713 insetCur.recordUndoInset();
715 // Let the script inset commit suicide. This is
716 // modelled on Cursor.pullArg(), but tries not to
717 // invoke notifyCursorLeaves again and does not touch
718 // cur (since the top slice will be deleted
720 MathData ar = cell(0);
722 insetCur.cell().erase(insetCur.pos());
723 insetCur.cell().insert(insetCur.pos(), ar);
726 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
730 //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
735 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
737 //LYXERR("InsetMathScript: request: " << cmd);
739 if (cmd.action() == LFUN_MATH_LIMITS) {
740 cur.recordUndoInset();
741 if (!cmd.argument().empty()) {
742 if (cmd.argument() == "limits")
744 else if (cmd.argument() == "nolimits")
748 } else if (limits_ == 0)
749 limits_ = hasLimits() ? -1 : 1;
755 InsetMathNest::doDispatch(cur, cmd);
759 bool InsetMathScript::getStatus(Cursor & cur, FuncRequest const & cmd,
760 FuncStatus & flag) const
762 if (cmd.action() == LFUN_MATH_LIMITS) {
763 if (!cmd.argument().empty()) {
764 if (cmd.argument() == "limits")
765 flag.setOnOff(limits_ == 1);
766 else if (cmd.argument() == "nolimits")
767 flag.setOnOff(limits_ == -1);
769 flag.setOnOff(limits_ == 0);
771 flag.setEnabled(true);
775 return InsetMathNest::getStatus(cur, cmd, flag);
779 // the idea for dual scripts came from the eLyXer code
780 void InsetMathScript::validate(LaTeXFeatures & features) const
782 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
783 features.addCSSSnippet(
784 "span.scripts{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
785 "span.scripts span {display: block;}\n"
786 "sub.math{font-size: 75%;}\n"
787 "sup.math{font-size: 75%;}");
788 InsetMathNest::validate(features);