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"
27 #include "support/gettext.h"
29 #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 LATTEST(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
98 MathData & InsetMathScript::down()
102 LBUFERR(nargs() > 1);
107 MathData const & InsetMathScript::up() const
109 LBUFERR(nargs() > 1);
114 MathData & InsetMathScript::up()
116 LBUFERR(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()
150 int InsetMathScript::dy01(BufferView const & bv, int asc, int des, int what) const
154 bool isCharBox = !nuc().empty() ? isAlphaSymbol(nuc().back()) : false;
156 Dimension const & dimdown = down().dimension(bv);
157 dasc = dimdown.ascent();
158 slevel = nuc().slevel();
159 int ascdrop = dasc - slevel;
160 int desdrop = isCharBox ? 0 : des + nuc().sshift();
161 int mindes = nuc().mindes();
162 des = max(desdrop, ascdrop);
163 des = max(mindes, des);
166 Dimension const & dimup = up().dimension(bv);
167 int minasc = nuc().minasc();
168 int ascdrop = isCharBox ? 0 : asc - up().mindes();
169 int udes = dimup.descent();
170 asc = udes + nuc().sshift();
171 asc = max(ascdrop, asc);
172 asc = max(minasc, asc);
174 int del = asc - udes - dasc;
175 if (del + des <= 2) {
176 int newdes = 2 - del;
177 del = slevel - asc + udes;
182 des = max(des, newdes);
186 return what ? asc : des;
190 int InsetMathScript::dy0(BufferView const & bv) const
195 int des = down().dimension(bv).ascent();
200 des = dy01(bv, na, nd, 0);
206 int InsetMathScript::dy1(BufferView const & bv) const
211 int asc = up().dimension(bv).descent();
216 asc = dy01(bv, na, nd, 1);
223 int InsetMathScript::dx0(BufferView const & bv) const
225 LASSERT(hasDown(), return 0);
226 Dimension const dim = dimension(bv);
227 return hasLimits() ? (dim.wid - down().dimension(bv).width()) / 2 : nwid(bv);
231 int InsetMathScript::dx1(BufferView const & bv) const
233 LASSERT(hasUp(), return 0);
234 Dimension const dim = dimension(bv);
235 return hasLimits() ? (dim.wid - up().dimension(bv).width()) / 2 : nwid(bv) + nker(&bv);
239 int InsetMathScript::dxx(BufferView const & bv) const
241 Dimension const dim = dimension(bv);
242 return hasLimits() ? (dim.wid - nwid(bv)) / 2 : 0;
246 int InsetMathScript::nwid(BufferView const & bv) const
248 return !nuc().empty() ? nuc().dimension(bv).width() : 2;
252 int InsetMathScript::nasc(BufferView const & bv) const
254 return !nuc().empty() ? nuc().dimension(bv).ascent() : 5;
258 int InsetMathScript::ndes(BufferView const & bv) const
260 return !nuc().empty() ? nuc().dimension(bv).descent() : 0;
264 int InsetMathScript::nker(BufferView const * bv) const
266 if (!nuc().empty()) {
267 int kerning = nuc().kerning(bv);
268 return kerning > 0 ? kerning : 0;
274 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
279 cell(0).metrics(mi, dim0);
280 ScriptChanger dummy(mi.base);
282 cell(1).metrics(mi, dim1);
284 cell(2).metrics(mi, dim2);
287 BufferView & bv = *mi.base.bv;
288 // FIXME: data copying... not very efficient.
292 dimup = up().dimension(bv);
294 dimdown = down().dimension(bv);
299 dim.wid = max(dim.wid, dimup.width());
301 dim.wid = max(dim.wid, dimdown.width());
304 dim.wid = max(dim.wid, nker(mi.base.bv) + dimup.width());
306 dim.wid = max(dim.wid, dimdown.width());
311 int asc = dy1(bv) + dimup.ascent();
312 dim.asc = max(na, asc);
317 int des = dy0(bv) + dimdown.descent();
318 dim.des = max(nd, des);
325 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
327 BufferView & bv = *pi.base.bv;
329 nuc().draw(pi, x + dxx(bv), y);
331 nuc().setXY(bv, x + dxx(bv), y);
333 pi.draw(x + dxx(bv), y, char_type('.'));
335 ScriptChanger dummy(pi.base);
337 up().draw(pi, x + dx1(bv), y - dy1(bv));
339 down().draw(pi, x + dx0(bv), y + dy0(bv));
340 drawMarkers(pi, x, y);
344 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
347 up().metricsT(mi, dim);
349 down().metricsT(mi, dim);
350 nuc().metricsT(mi, dim);
354 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
358 nuc().drawT(pain, x + 1, y);
360 up().drawT(pain, x + 1, y - 1 /*dy1()*/);
362 down().drawT(pain, x + 1, y + 1 /*dy0()*/);
367 bool InsetMathScript::hasLimits() const
375 // we can only display limits if the nucleus wants some
378 if (!nuc().back()->isScriptable())
381 if (nuc().back()->asSymbolInset()) {
382 // \intop is an alias for \int\limits, \ointop == \oint\limits
383 if (nuc().back()->asSymbolInset()->name().find(from_ascii("intop")) != string::npos)
385 // per default \int has limits beside the \int even in displayed formulas
386 if (nuc().back()->asSymbolInset()->name().find(from_ascii("int")) != string::npos)
390 // assume "real" limits for everything else
395 void InsetMathScript::removeScript(bool up)
398 if (up == cell_1_is_up_)
400 } else if (nargs() == 3) {
402 swap(cells_[1], cells_[2]);
403 cell_1_is_up_ = false;
405 cell_1_is_up_ = true;
412 bool InsetMathScript::has(bool up) const
414 return idxOfScript(up);
418 bool InsetMathScript::hasUp() const
420 //lyxerr << "1up: " << bool(cell_1_is_up_));
421 //lyxerr << "hasUp: " << bool(idxOfScript(true)));
422 return idxOfScript(true);
426 bool InsetMathScript::hasDown() const
428 //LYXERR0("1up: " << bool(cell_1_is_up_));
429 //LYXERR0("hasDown: " << bool(idxOfScript(false)));
430 return idxOfScript(false);
434 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
439 return (cell_1_is_up_ == up) ? 1 : 0;
442 LASSERT(false, return 0);
446 bool InsetMathScript::idxForward(Cursor &) const
452 bool InsetMathScript::idxBackward(Cursor &) const
458 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
461 if (cur.idx() == 0) {
462 // don't go up/down if there is no cell in this direction
465 // go up/down only if in the last position
466 // or in the first position of something with displayed limits
467 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
468 cur.idx() = idxOfScript(up);
476 if (cur.idx() == idxOfScript(true)) {
477 // can't go further up
480 // otherwise go to last position in the nucleus
482 cur.pos() = cur.lastpos();
487 if (cur.idx() == idxOfScript(false)) {
488 // can't go further down
491 // otherwise go to last position in the nucleus
493 cur.pos() = cur.lastpos();
501 void InsetMathScript::write(WriteStream & os) const
503 MathEnsurer ensurer(os);
505 if (!nuc().empty()) {
507 //if (nuc().back()->takesLimits()) {
515 LYXERR(Debug::MATHED, "suppressing {} when writing");
520 if (hasDown() /*&& !down().empty()*/)
521 os << "_{" << down() << '}';
523 if (hasUp() /*&& !up().empty()*/) {
524 // insert space if up() is empty or an empty brace inset
526 if (os.latex() && (up().empty() ||
527 (up().size() == 1 && up().back()->asBraceInset() &&
528 up().back()->asBraceInset()->cell(0).empty())))
531 os << "^{" << up() << '}';
534 if (lock_ && !os.latex())
539 void InsetMathScript::normalize(NormalStream & os) const
541 bool d = hasDown() && !down().empty();
542 bool u = hasUp() && !up().empty();
557 os << down() << ' ' << up() << ']';
565 void InsetMathScript::maple(MapleStream & os) const
569 if (hasDown() && !down().empty())
570 os << '[' << down() << ']';
571 if (hasUp() && !up().empty())
572 os << "^(" << up() << ')';
576 void InsetMathScript::mathematica(MathematicaStream & os) const
578 bool d = hasDown() && !down().empty();
579 bool u = hasUp() && !up().empty();
581 if (!nuc().empty()) {
583 os << "Subscript[" << nuc();
589 os << "^(" << up() << ')';
591 if (!nuc().empty()) {
593 os << ',' << down() << ']';
599 // It may be worth trying to output munder, mover, and munderover
600 // in certain cases, e.g., for display formulas. But then we would
601 // need to know if we're in a display formula.
602 void InsetMathScript::mathmlize(MathStream & os) const
604 bool d = hasDown() && !down().empty();
605 bool u = hasUp() && !up().empty();
608 os << MTag("msubsup");
615 os << MTag("mrow") << nuc() << ETag("mrow");
620 os << MTag("mrow") << down() << ETag("mrow")
621 << MTag("mrow") << up() << ETag("mrow")
624 os << MTag("mrow") << up() << ETag("mrow") << ETag("msup");
626 os << MTag("mrow") << down() << ETag("mrow") << ETag("msub");
630 void InsetMathScript::htmlize(HtmlStream & os) const
632 bool d = hasDown() && !down().empty();
633 bool u = hasUp() && !up().empty();
639 os << MTag("span", "class='scripts'")
640 << MTag("span") << up() << ETag("span")
641 << MTag("span") << down() << ETag("span")
644 os << MTag("sup", "class='math'") << up() << ETag("sup");
646 os << MTag("sub", "class='math'") << down() << ETag("sub");
650 void InsetMathScript::octave(OctaveStream & os) const
654 if (hasDown() && !down().empty())
655 os << '[' << down() << ']';
656 if (hasUp() && !up().empty())
657 os << "^(" << up() << ')';
661 void InsetMathScript::infoize(odocstream & os) const
667 void InsetMathScript::infoize2(odocstream & os) const
670 os << from_ascii(limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
674 bool InsetMathScript::notifyCursorLeaves(Cursor const & old, Cursor & cur)
676 InsetMathNest::notifyCursorLeaves(old, cur);
678 //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
680 // Remove empty scripts if possible:
682 // The case of two scripts, but only one got empty (1 = super, 2 = sub).
683 // We keep the script inset, but remove the empty script.
684 if (nargs() > 2 && (!cell(1).empty() || !cell(2).empty())) {
685 if (cell(2).empty()) {
686 // must be a subscript...
687 old.recordUndoInset();
689 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
691 } else if (cell(1).empty()) {
692 // must be a superscript...
693 old.recordUndoInset();
695 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
699 // Now the two suicide cases:
700 // * we have only one script which is empty
701 // * we have two scripts which are both empty.
702 // The script inset is removed completely.
703 if ((nargs() == 2 && cell(1).empty())
704 || (nargs() == 3 && cell(1).empty() && cell(2).empty())) {
705 // Make undo step. We cannot use cur for this because
706 // it does not necessarily point to us anymore. But we
707 // should be on top of the cursor old.
708 Cursor insetCur = old;
709 int scriptSlice = insetCur.find(this);
710 LASSERT(scriptSlice != -1, /**/);
711 insetCur.cutOff(scriptSlice);
712 insetCur.recordUndoInset();
714 // Let the script inset commit suicide. This is
715 // modelled on Cursor.pullArg(), but tries not to
716 // invoke notifyCursorLeaves again and does not touch
717 // cur (since the top slice will be deleted
719 MathData ar = cell(0);
721 insetCur.cell().erase(insetCur.pos());
722 insetCur.cell().insert(insetCur.pos(), ar);
725 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
729 //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
734 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
736 //LYXERR("InsetMathScript: request: " << cmd);
738 if (cmd.action() == LFUN_MATH_LIMITS) {
739 cur.recordUndoInset();
740 if (!cmd.argument().empty()) {
741 if (cmd.argument() == "limits")
743 else if (cmd.argument() == "nolimits")
747 } else if (limits_ == 0)
748 limits_ = hasLimits() ? -1 : 1;
754 InsetMathNest::doDispatch(cur, cmd);
758 bool InsetMathScript::getStatus(Cursor & cur, FuncRequest const & cmd,
759 FuncStatus & flag) const
761 if (cmd.action() == LFUN_MATH_LIMITS) {
762 if (!cmd.argument().empty()) {
763 if (cmd.argument() == "limits")
764 flag.setOnOff(limits_ == 1);
765 else if (cmd.argument() == "nolimits")
766 flag.setOnOff(limits_ == -1);
768 flag.setOnOff(limits_ == 0);
770 flag.setEnabled(true);
774 return InsetMathNest::getStatus(cur, cmd, flag);
778 // the idea for dual scripts came from the eLyXer code
779 void InsetMathScript::validate(LaTeXFeatures & features) const
781 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
782 features.addCSSSnippet(
783 "span.scripts{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
784 "span.scripts span {display: block;}\n"
785 "sub.math{font-size: 75%;}\n"
786 "sup.math{font-size: 75%;}");
787 InsetMathNest::validate(features);