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() << ']';
598 void InsetMathScript::mathmlize(MathStream & os) const
600 bool d = hasDown() && !down().empty();
601 bool u = hasUp() && !up().empty();
602 bool l = hasLimits();
605 os << MTag(l ? "munderover" : "msubsup");
607 os << MTag(l ? "mover" : "msup");
609 os << MTag(l ? "munder" : "msub");
612 os << MTag("mrow") << nuc() << ETag("mrow");
617 os << MTag("mrow") << down() << ETag("mrow")
618 << MTag("mrow") << up() << ETag("mrow")
619 << ETag(l ? "munderover" : "msubsup");
621 os << MTag("mrow") << up() << ETag("mrow") << ETag(l ? "mover" : "msup");
623 os << MTag("mrow") << down() << ETag("mrow") << ETag(l ? "munder" : "msub");
627 void InsetMathScript::htmlize(HtmlStream & os) const
629 bool d = hasDown() && !down().empty();
630 bool u = hasUp() && !up().empty();
636 os << MTag("span", "class='scripts'")
637 << MTag("span") << up() << ETag("span")
638 << MTag("span") << down() << ETag("span")
641 os << MTag("sup", "class='math'") << up() << ETag("sup");
643 os << MTag("sub", "class='math'") << down() << ETag("sub");
647 void InsetMathScript::octave(OctaveStream & os) const
651 if (hasDown() && !down().empty())
652 os << '[' << down() << ']';
653 if (hasUp() && !up().empty())
654 os << "^(" << up() << ')';
658 void InsetMathScript::infoize(odocstream & os) const
664 void InsetMathScript::infoize2(odocstream & os) const
667 os << from_ascii(limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
671 bool InsetMathScript::notifyCursorLeaves(Cursor const & old, Cursor & cur)
673 InsetMathNest::notifyCursorLeaves(old, cur);
675 //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
677 // Remove empty scripts if possible:
679 // The case of two scripts, but only one got empty (1 = super, 2 = sub).
680 // We keep the script inset, but remove the empty script.
681 if (nargs() > 2 && (!cell(1).empty() || !cell(2).empty())) {
682 if (cell(2).empty()) {
683 // must be a subscript...
684 old.recordUndoInset();
686 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
688 } else if (cell(1).empty()) {
689 // must be a superscript...
690 old.recordUndoInset();
692 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
696 // Now the two suicide cases:
697 // * we have only one script which is empty
698 // * we have two scripts which are both empty.
699 // The script inset is removed completely.
700 if ((nargs() == 2 && cell(1).empty())
701 || (nargs() == 3 && cell(1).empty() && cell(2).empty())) {
702 // Make undo step. We cannot use cur for this because
703 // it does not necessarily point to us anymore. But we
704 // should be on top of the cursor old.
705 Cursor insetCur = old;
706 int scriptSlice = insetCur.find(this);
707 LASSERT(scriptSlice != -1, /**/);
708 insetCur.cutOff(scriptSlice);
709 insetCur.recordUndoInset();
711 // Let the script inset commit suicide. This is
712 // modelled on Cursor.pullArg(), but tries not to
713 // invoke notifyCursorLeaves again and does not touch
714 // cur (since the top slice will be deleted
716 MathData ar = cell(0);
718 insetCur.cell().erase(insetCur.pos());
719 insetCur.cell().insert(insetCur.pos(), ar);
722 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
726 //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
731 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
733 //LYXERR("InsetMathScript: request: " << cmd);
735 if (cmd.action() == LFUN_MATH_LIMITS) {
736 cur.recordUndoInset();
737 if (!cmd.argument().empty()) {
738 if (cmd.argument() == "limits")
740 else if (cmd.argument() == "nolimits")
744 } else if (limits_ == 0)
745 limits_ = hasLimits() ? -1 : 1;
751 InsetMathNest::doDispatch(cur, cmd);
755 bool InsetMathScript::getStatus(Cursor & cur, FuncRequest const & cmd,
756 FuncStatus & flag) const
758 if (cmd.action() == LFUN_MATH_LIMITS) {
759 if (!cmd.argument().empty()) {
760 if (cmd.argument() == "limits")
761 flag.setOnOff(limits_ == 1);
762 else if (cmd.argument() == "nolimits")
763 flag.setOnOff(limits_ == -1);
765 flag.setOnOff(limits_ == 0);
767 flag.setEnabled(true);
771 return InsetMathNest::getStatus(cur, cmd, flag);
775 // the idea for dual scripts came from the eLyXer code
776 void InsetMathScript::validate(LaTeXFeatures & features) const
778 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
779 features.addCSSSnippet(
780 "span.scripts{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
781 "span.scripts span {display: block;}\n"
782 "sub.math{font-size: 75%;}\n"
783 "sup.math{font-size: 75%;}");
784 InsetMathNest::validate(features);