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 "InsetMathScript.h"
15 #include "InsetMathBrace.h"
16 #include "InsetMathSymbol.h"
18 #include "MathStream.h"
19 #include "MathSupport.h"
21 #include "BufferView.h"
23 #include "DispatchResult.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "LaTeXFeatures.h"
27 #include "MetricsInfo.h"
29 #include "support/debug.h"
30 #include "support/gettext.h"
31 #include "support/lassert.h"
39 InsetMathScript::InsetMathScript(Buffer * buf)
40 : InsetMathNest(buf, 1), cell_1_is_up_(false), limits_(0)
44 InsetMathScript::InsetMathScript(Buffer * buf, bool up)
45 : InsetMathNest(buf, 2), cell_1_is_up_(up), limits_(0)
49 InsetMathScript::InsetMathScript(Buffer * buf, MathAtom const & at, bool up)
50 : InsetMathNest(buf, 2), cell_1_is_up_(up), limits_(0)
52 LATTEST(nargs() >= 1);
53 cell(0).push_back(at);
57 Inset * InsetMathScript::clone() const
59 return new InsetMathScript(*this);
63 InsetMathScript const * InsetMathScript::asScriptInset() const
69 InsetMathScript * InsetMathScript::asScriptInset()
75 MathData const & InsetMathScript::down() const
84 MathData & InsetMathScript::down()
93 MathData const & InsetMathScript::up() const
100 MathData & InsetMathScript::up()
102 LBUFERR(nargs() > 1);
107 void InsetMathScript::ensure(bool up)
110 // just nucleus so far
111 cells_.push_back(MathData());
113 } else if (nargs() == 2 && !has(up)) {
115 cells_.push_back(cell(1));
118 cells_.push_back(MathData());
124 MathData const & InsetMathScript::nuc() const
130 MathData & InsetMathScript::nuc()
136 int InsetMathScript::dy01(BufferView const & bv, int asc, int des, int what) const
140 bool isCharBox = !nuc().empty() ? isAlphaSymbol(nuc().back()) : false;
142 Dimension const & dimdown = down().dimension(bv);
143 dasc = dimdown.ascent();
144 slevel = nuc().slevel();
145 int ascdrop = dasc - slevel;
146 int desdrop = isCharBox ? 0 : des + nuc().sshift();
147 int mindes = nuc().mindes();
148 des = max(desdrop, ascdrop);
149 des = max(mindes, des);
152 Dimension const & dimup = up().dimension(bv);
153 int minasc = nuc().minasc();
154 int ascdrop = isCharBox ? 0 : asc - up().mindes();
155 int udes = dimup.descent();
156 asc = udes + nuc().sshift();
157 asc = max(ascdrop, asc);
158 asc = max(minasc, asc);
160 int del = asc - udes - dasc;
161 if (del + des <= 2) {
162 int newdes = 2 - del;
163 del = slevel - asc + udes;
168 des = max(des, newdes);
172 return what ? asc : des;
176 int InsetMathScript::dy0(BufferView const & bv) const
181 int des = down().dimension(bv).ascent();
186 des = dy01(bv, na, nd, 0);
192 int InsetMathScript::dy1(BufferView const & bv) const
197 int asc = up().dimension(bv).descent();
202 asc = dy01(bv, na, nd, 1);
209 int InsetMathScript::dx0(BufferView const & bv) const
211 LASSERT(hasDown(), return 0);
212 Dimension const dim = dimension(bv);
213 return hasLimits() ? (dim.wid - down().dimension(bv).width()) / 2
214 : nwid(bv) + min(nker(&bv), 0);
218 int InsetMathScript::dx1(BufferView const & bv) const
220 LASSERT(hasUp(), return 0);
221 Dimension const dim = dimension(bv);
222 return hasLimits() ? (dim.wid - up().dimension(bv).width()) / 2
223 : nwid(bv) + max(nker(&bv), 0);
227 int InsetMathScript::dxx(BufferView const & bv) const
229 Dimension const dim = dimension(bv);
230 return hasLimits() ? (dim.wid - nwid(bv)) / 2 : 0;
234 int InsetMathScript::nwid(BufferView const & bv) const
236 return !nuc().empty() ? nuc().dimension(bv).width() : 2;
240 int InsetMathScript::nasc(BufferView const & bv) const
242 return !nuc().empty() ? nuc().dimension(bv).ascent() : 5;
246 int InsetMathScript::ndes(BufferView const & bv) const
248 return !nuc().empty() ? nuc().dimension(bv).descent() : 0;
252 int InsetMathScript::nker(BufferView const * bv) const
255 return nuc().kerning(bv);
260 MathClass InsetMathScript::mathClass() const
262 // FIXME: this is a hack, since the class will not be correct if
263 // the nucleus has several elements or if the last element is a math macro
264 // or a macro argument proxy.
265 // The correct implementation would require to linearize the nucleus.
269 // return the class of last element since this is the one that counts.
270 MathClass mc = nuc().back()->mathClass();
271 return (mc == MC_UNKNOWN) ? MC_ORD : mc;
276 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
278 Changer dummy2 = mi.base.changeEnsureMath();
282 cell(0).metrics(mi, dim0);
283 Changer dummy = mi.base.changeScript();
285 cell(1).metrics(mi, dim1);
287 cell(2).metrics(mi, dim2);
290 BufferView & bv = *mi.base.bv;
291 // FIXME: data copying... not very efficient.
295 dimup = up().dimension(bv);
297 dimdown = down().dimension(bv);
302 dim.wid = max(dim.wid, dimup.width());
304 dim.wid = max(dim.wid, dimdown.width());
307 dim.wid = max(dim.wid, max(nker(mi.base.bv), 0) + dimup.width());
309 dim.wid = max(dim.wid, min(nker(mi.base.bv), 0) + dimdown.width());
314 int asc = dy1(bv) + dimup.ascent();
315 dim.asc = max(na, asc);
320 int des = dy0(bv) + dimdown.descent();
321 dim.des = max(nd, des);
327 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
329 Changer dummy2 = pi.base.changeEnsureMath();
330 BufferView & bv = *pi.base.bv;
332 nuc().draw(pi, x + dxx(bv), y);
334 nuc().setXY(bv, x + dxx(bv), y);
336 pi.draw(x + dxx(bv), y, char_type('.'));
338 Changer dummy = pi.base.changeScript();
340 up().draw(pi, x + dx1(bv), y - dy1(bv));
342 down().draw(pi, x + dx0(bv), y + dy0(bv));
346 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
349 up().metricsT(mi, dim);
351 down().metricsT(mi, dim);
352 nuc().metricsT(mi, dim);
356 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
360 nuc().drawT(pain, x + 1, y);
362 up().drawT(pain, x + 1, y - 1 /*dy1()*/);
364 down().drawT(pain, x + 1, y + 1 /*dy0()*/);
369 bool InsetMathScript::hasLimits() const
377 // we can only display limits if the nucleus wants some
380 if (!nuc().back()->isScriptable())
383 if (nuc().back()->asSymbolInset()) {
384 // \intop is an alias for \int\limits, \ointop == \oint\limits
385 if (nuc().back()->asSymbolInset()->name().find(from_ascii("intop")) != string::npos)
387 // per default \int has limits beside the \int even in displayed formulas
388 if (nuc().back()->asSymbolInset()->name().find(from_ascii("int")) != string::npos)
392 // assume "real" limits for everything else
397 void InsetMathScript::removeScript(bool up)
400 if (up == cell_1_is_up_)
402 } else if (nargs() == 3) {
404 swap(cells_[1], cells_[2]);
405 cell_1_is_up_ = false;
407 cell_1_is_up_ = true;
414 bool InsetMathScript::has(bool up) const
416 return idxOfScript(up);
420 bool InsetMathScript::hasUp() const
422 //lyxerr << "1up: " << bool(cell_1_is_up_));
423 //lyxerr << "hasUp: " << bool(idxOfScript(true)));
424 return idxOfScript(true);
428 bool InsetMathScript::hasDown() const
430 //LYXERR0("1up: " << bool(cell_1_is_up_));
431 //LYXERR0("hasDown: " << bool(idxOfScript(false)));
432 return idxOfScript(false);
436 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
441 return (cell_1_is_up_ == up) ? 1 : 0;
444 LASSERT(false, return 0);
448 bool InsetMathScript::idxForward(Cursor &) const
454 bool InsetMathScript::idxBackward(Cursor &) const
460 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
463 if (cur.idx() == 0) {
464 // don't go up/down if there is no cell in this direction
467 // go up/down only if in the last position
468 // or in the first position of something with displayed limits
469 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
470 cur.idx() = idxOfScript(up);
478 if (cur.idx() == idxOfScript(true)) {
479 // can't go further up
482 // otherwise go to last position in the nucleus
484 cur.pos() = cur.lastpos();
489 if (cur.idx() == idxOfScript(false)) {
490 // can't go further down
493 // otherwise go to last position in the nucleus
495 cur.pos() = cur.lastpos();
503 void InsetMathScript::write(WriteStream & os) const
505 MathEnsurer ensurer(os);
507 if (!nuc().empty()) {
509 //if (nuc().back()->takesLimits()) {
517 LYXERR(Debug::MATHED, "suppressing {} when writing");
522 if (hasDown() /*&& !down().empty()*/)
523 os << "_{" << down() << '}';
525 if (hasUp() /*&& !up().empty()*/) {
526 // insert space if up() is empty or an empty brace inset
528 if (os.latex() && (up().empty() ||
529 (up().size() == 1 && up().back()->asBraceInset() &&
530 up().back()->asBraceInset()->cell(0).empty())))
533 os << "^{" << up() << '}';
536 if (lock_ && !os.latex())
541 void InsetMathScript::normalize(NormalStream & os) const
543 bool d = hasDown() && !down().empty();
544 bool u = hasUp() && !up().empty();
559 os << down() << ' ' << up() << ']';
567 void InsetMathScript::maple(MapleStream & os) const
571 if (hasDown() && !down().empty())
572 os << '[' << down() << ']';
573 if (hasUp() && !up().empty())
574 os << "^(" << up() << ')';
578 void InsetMathScript::mathematica(MathematicaStream & os) const
580 bool d = hasDown() && !down().empty();
581 bool u = hasUp() && !up().empty();
583 if (!nuc().empty()) {
585 os << "Subscript[" << nuc();
591 os << "^(" << up() << ')';
593 if (!nuc().empty()) {
595 os << ',' << down() << ']';
600 void InsetMathScript::mathmlize(MathStream & os) const
602 bool d = hasDown() && !down().empty();
603 bool u = hasUp() && !up().empty();
604 bool l = hasLimits();
607 os << MTag(l ? "munderover" : "msubsup");
609 os << MTag(l ? "mover" : "msup");
611 os << MTag(l ? "munder" : "msub");
614 os << MTag("mrow") << nuc() << ETag("mrow");
619 os << MTag("mrow") << down() << ETag("mrow")
620 << MTag("mrow") << up() << ETag("mrow")
621 << ETag(l ? "munderover" : "msubsup");
623 os << MTag("mrow") << up() << ETag("mrow") << ETag(l ? "mover" : "msup");
625 os << MTag("mrow") << down() << ETag("mrow") << ETag(l ? "munder" : "msub");
629 void InsetMathScript::htmlize(HtmlStream & os) const
631 bool d = hasDown() && !down().empty();
632 bool u = hasUp() && !up().empty();
638 os << MTag("span", "class='scripts'")
639 << MTag("span") << up() << ETag("span")
640 << MTag("span") << down() << ETag("span")
643 os << MTag("sup", "class='math'") << up() << ETag("sup");
645 os << MTag("sub", "class='math'") << down() << ETag("sub");
649 void InsetMathScript::octave(OctaveStream & os) const
653 if (hasDown() && !down().empty())
654 os << '[' << down() << ']';
655 if (hasUp() && !up().empty())
656 os << "^(" << up() << ')';
660 void InsetMathScript::infoize(odocstream & os) const
666 void InsetMathScript::infoize2(odocstream & os) const
669 os << from_ascii(limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
673 bool InsetMathScript::notifyCursorLeaves(Cursor const & old, Cursor & cur)
675 InsetMathNest::notifyCursorLeaves(old, cur);
677 //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
679 // Remove empty scripts if possible:
681 // The case of two scripts, but only one got empty (1 = super, 2 = sub).
682 // We keep the script inset, but remove the empty script.
683 if (nargs() > 2 && (!cell(1).empty() || !cell(2).empty())) {
684 if (cell(2).empty()) {
685 // must be a subscript...
686 old.recordUndoInset();
688 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
690 } else if (cell(1).empty()) {
691 // must be a superscript...
692 old.recordUndoInset();
694 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
698 // Now the two suicide cases:
699 // * we have only one script which is empty
700 // * we have two scripts which are both empty.
701 // The script inset is removed completely.
702 if ((nargs() == 2 && cell(1).empty())
703 || (nargs() == 3 && cell(1).empty() && cell(2).empty())) {
704 // Make undo step. We cannot use cur for this because
705 // it does not necessarily point to us anymore. But we
706 // should be on top of the cursor old.
707 Cursor insetCur = old;
708 int scriptSlice = insetCur.find(this);
709 LASSERT(scriptSlice != -1, /**/);
710 insetCur.cutOff(scriptSlice);
711 insetCur.recordUndoInset();
713 // Let the script inset commit suicide. This is
714 // modelled on Cursor.pullArg(), but tries not to
715 // invoke notifyCursorLeaves again and does not touch
716 // cur (since the top slice will be deleted
718 MathData ar = cell(0);
720 insetCur.cell().erase(insetCur.pos());
721 insetCur.cell().insert(insetCur.pos(), ar);
724 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
728 //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
733 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
735 //LYXERR("InsetMathScript: request: " << cmd);
737 if (cmd.action() == LFUN_MATH_LIMITS) {
738 cur.recordUndoInset();
739 if (!cmd.argument().empty()) {
740 if (cmd.argument() == "limits")
742 else if (cmd.argument() == "nolimits")
746 } else if (limits_ == 0)
747 limits_ = hasLimits() ? -1 : 1;
753 InsetMathNest::doDispatch(cur, cmd);
757 bool InsetMathScript::getStatus(Cursor & cur, FuncRequest const & cmd,
758 FuncStatus & flag) const
760 if (cmd.action() == LFUN_MATH_LIMITS) {
761 if (!cmd.argument().empty()) {
762 if (cmd.argument() == "limits")
763 flag.setOnOff(limits_ == 1);
764 else if (cmd.argument() == "nolimits")
765 flag.setOnOff(limits_ == -1);
767 flag.setOnOff(limits_ == 0);
769 flag.setEnabled(true);
773 return InsetMathNest::getStatus(cur, cmd, flag);
777 // the idea for dual scripts came from the eLyXer code
778 void InsetMathScript::validate(LaTeXFeatures & features) const
780 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
781 features.addCSSSnippet(
782 "span.scripts{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
783 "span.scripts span {display: block;}\n"
784 "sub.math{font-size: 75%;}\n"
785 "sup.math{font-size: 75%;}");
786 InsetMathNest::validate(features);