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 "InsetMathFont.h"
18 #include "InsetMathScript.h"
19 #include "InsetMathSymbol.h"
20 #include "LaTeXFeatures.h"
22 #include "MathStream.h"
23 #include "MathSupport.h"
25 #include "support/debug.h"
27 #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()
151 bool isAlphaSymbol(MathAtom const & at)
153 if (at->asCharInset() ||
154 (at->asSymbolInset() &&
155 at->asSymbolInset()->isOrdAlpha()))
158 if (at->asFontInset()) {
159 MathData const & ar = at->asFontInset()->cell(0);
160 for (size_t i = 0; i < ar.size(); ++i) {
161 if (!(ar[i]->asCharInset() ||
162 (ar[i]->asSymbolInset() &&
163 ar[i]->asSymbolInset()->isOrdAlpha())))
174 int InsetMathScript::dy01(BufferView const & bv, int asc, int des, int what) const
178 bool isCharBox = nuc().size() ? isAlphaSymbol(nuc().back()) : false;
180 Dimension const & dimdown = down().dimension(bv);
181 dasc = dimdown.ascent();
182 slevel = nuc().slevel();
183 int ascdrop = dasc - slevel;
184 int desdrop = isCharBox ? 0 : des + nuc().sshift();
185 int mindes = nuc().mindes();
186 des = max(desdrop, ascdrop);
187 des = max(mindes, des);
190 Dimension const & dimup = up().dimension(bv);
191 int minasc = nuc().minasc();
192 int ascdrop = isCharBox ? 0 : asc - up().mindes();
193 int udes = dimup.descent();
194 asc = udes + nuc().sshift();
195 asc = max(ascdrop, asc);
196 asc = max(minasc, asc);
198 int del = asc - udes - dasc;
199 if (del + des <= 2) {
200 int newdes = 2 - del;
201 del = slevel - asc + udes;
206 des = max(des, newdes);
210 return what ? asc : des;
214 int InsetMathScript::dy0(BufferView const & bv) const
219 int des = down().dimension(bv).ascent();
224 des = dy01(bv, na, nd, 0);
230 int InsetMathScript::dy1(BufferView const & bv) const
235 int asc = up().dimension(bv).descent();
240 asc = dy01(bv, na, nd, 1);
247 int InsetMathScript::dx0(BufferView const & bv) const
249 LASSERT(hasDown(), /**/);
250 Dimension const dim = dimension(bv);
251 return hasLimits() ? (dim.wid - down().dimension(bv).width()) / 2 : nwid(bv);
255 int InsetMathScript::dx1(BufferView const & bv) const
257 LASSERT(hasUp(), /**/);
258 Dimension const dim = dimension(bv);
259 return hasLimits() ? (dim.wid - up().dimension(bv).width()) / 2 : nwid(bv) + nker(&bv);
263 int InsetMathScript::dxx(BufferView const & bv) const
265 Dimension const dim = dimension(bv);
266 return hasLimits() ? (dim.wid - nwid(bv)) / 2 : 0;
270 int InsetMathScript::nwid(BufferView const & bv) const
272 return nuc().size() ? nuc().dimension(bv).width() : 2;
276 int InsetMathScript::nasc(BufferView const & bv) const
278 return nuc().size() ? nuc().dimension(bv).ascent() : 5;
282 int InsetMathScript::ndes(BufferView const & bv) const
284 return nuc().size() ? nuc().dimension(bv).descent() : 0;
288 int InsetMathScript::nker(BufferView const * bv) const
291 int kerning = nuc().kerning(bv);
292 return kerning > 0 ? kerning : 0;
298 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
303 cell(0).metrics(mi, dim0);
304 ScriptChanger dummy(mi.base);
306 cell(1).metrics(mi, dim1);
308 cell(2).metrics(mi, dim2);
311 BufferView & bv = *mi.base.bv;
312 // FIXME: data copying... not very efficient.
316 dimup = up().dimension(bv);
318 dimdown = down().dimension(bv);
323 dim.wid = max(dim.wid, dimup.width());
325 dim.wid = max(dim.wid, dimdown.width());
328 dim.wid = max(dim.wid, nker(mi.base.bv) + dimup.width());
330 dim.wid = max(dim.wid, dimdown.width());
335 int asc = dy1(bv) + dimup.ascent();
336 dim.asc = max(na, asc);
341 int des = dy0(bv) + dimdown.descent();
342 dim.des = max(nd, des);
349 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
351 BufferView & bv = *pi.base.bv;
353 nuc().draw(pi, x + dxx(bv), y);
355 nuc().setXY(bv, x + dxx(bv), y);
357 pi.draw(x + dxx(bv), y, char_type('.'));
359 ScriptChanger dummy(pi.base);
361 up().draw(pi, x + dx1(bv), y - dy1(bv));
363 down().draw(pi, x + dx0(bv), y + dy0(bv));
364 drawMarkers(pi, x, y);
368 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
371 up().metricsT(mi, dim);
373 down().metricsT(mi, dim);
374 nuc().metricsT(mi, dim);
378 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
382 nuc().drawT(pain, x + 1, y);
384 up().drawT(pain, x + 1, y - 1 /*dy1()*/);
386 down().drawT(pain, x + 1, y + 1 /*dy0()*/);
391 bool InsetMathScript::hasLimits() const
399 // we can only display limits if the nucleus wants some
402 if (!nuc().back()->isScriptable())
405 if (nuc().back()->asSymbolInset()) {
406 // \intop is an alias for \int\limits, \ointop == \oint\limits
407 if (nuc().back()->asSymbolInset()->name().find(from_ascii("intop")) != string::npos)
409 // per default \int has limits beside the \int even in displayed formulas
410 if (nuc().back()->asSymbolInset()->name().find(from_ascii("int")) != string::npos)
414 // assume "real" limits for everything else
419 void InsetMathScript::removeScript(bool up)
422 if (up == cell_1_is_up_)
424 } else if (nargs() == 3) {
426 swap(cells_[1], cells_[2]);
427 cell_1_is_up_ = false;
429 cell_1_is_up_ = true;
436 bool InsetMathScript::has(bool up) const
438 return idxOfScript(up);
442 bool InsetMathScript::hasUp() const
444 //lyxerr << "1up: " << bool(cell_1_is_up_));
445 //lyxerr << "hasUp: " << bool(idxOfScript(true)));
446 return idxOfScript(true);
450 bool InsetMathScript::hasDown() const
452 //LYXERR0("1up: " << bool(cell_1_is_up_));
453 //LYXERR0("hasDown: " << bool(idxOfScript(false)));
454 return idxOfScript(false);
458 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
463 return (cell_1_is_up_ == up) ? 1 : 0;
466 LASSERT(false, /**/);
472 bool InsetMathScript::idxForward(Cursor &) const
478 bool InsetMathScript::idxBackward(Cursor &) const
484 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
487 if (cur.idx() == 0) {
488 // don't go up/down if there is no cell in this direction
491 // go up/down only if in the last position
492 // or in the first position of something with displayed limits
493 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
494 cur.idx() = idxOfScript(up);
502 if (cur.idx() == idxOfScript(true)) {
503 // can't go further up
506 // otherwise go to last position in the nucleus
508 cur.pos() = cur.lastpos();
513 if (cur.idx() == idxOfScript(false)) {
514 // can't go further down
517 // otherwise go to last position in the nucleus
519 cur.pos() = cur.lastpos();
527 void InsetMathScript::write(WriteStream & os) const
529 MathEnsurer ensurer(os);
533 //if (nuc().back()->takesLimits()) {
541 LYXERR(Debug::MATHED, "suppressing {} when writing");
546 if (hasDown() /*&& down().size()*/)
547 os << "_{" << down() << '}';
549 if (hasUp() /*&& up().size()*/)
550 os << "^{" << up() << '}';
552 if (lock_ && !os.latex())
557 void InsetMathScript::normalize(NormalStream & os) const
559 bool d = hasDown() && down().size();
560 bool u = hasUp() && up().size();
575 os << down() << ' ' << up() << ']';
583 void InsetMathScript::maple(MapleStream & os) const
587 if (hasDown() && down().size())
588 os << '[' << down() << ']';
589 if (hasUp() && up().size())
590 os << "^(" << up() << ')';
594 void InsetMathScript::mathematica(MathematicaStream & os) const
596 bool d = hasDown() && down().size();
597 bool u = hasUp() && up().size();
601 os << "Subscript[" << nuc();
607 os << "^(" << up() << ')';
611 os << ',' << down() << ']';
617 // It may be worth trying to output munder, mover, and munderover
618 // in certain cases, e.g., for display formulas. But then we would
619 // need to know if we're in a display formula.
620 void InsetMathScript::mathmlize(MathStream & os) const
622 bool d = hasDown() && down().size();
623 bool u = hasUp() && up().size();
626 os << MTag("msubsup");
633 os << MTag("mrow") << nuc() << ETag("mrow");
638 os << MTag("mrow") << down() << ETag("mrow")
639 << MTag("mrow") << up() << ETag("mrow")
642 os << MTag("mrow") << up() << ETag("mrow") << ETag("msup");
644 os << MTag("mrow") << down() << ETag("mrow") << ETag("msub");
648 void InsetMathScript::htmlize(HtmlStream & os) const
650 bool d = hasDown() && down().size();
651 bool u = hasUp() && up().size();
657 os << MTag("span", "class='scripts'")
658 << MTag("span") << up() << ETag("span")
659 << MTag("span") << down() << ETag("span")
662 os << MTag("sup", "class='math'") << up() << ETag("sup");
664 os << MTag("sub", "class='math'") << down() << ETag("sub");
668 void InsetMathScript::octave(OctaveStream & os) const
672 if (hasDown() && down().size())
673 os << '[' << down() << ']';
674 if (hasUp() && up().size())
675 os << "^(" << up() << ')';
679 void InsetMathScript::infoize(odocstream & os) const
685 void InsetMathScript::infoize2(odocstream & os) const
688 os << from_ascii(limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
692 bool InsetMathScript::notifyCursorLeaves(Cursor const & old, Cursor & cur)
694 InsetMathNest::notifyCursorLeaves(old, cur);
696 //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
698 // Remove empty scripts if possible:
700 // The case of two scripts, but only one got empty (1 = super, 2 = sub).
701 // We keep the script inset, but remove the empty script.
702 if (nargs() > 2 && (!cell(1).empty() || !cell(2).empty())) {
703 if (cell(2).empty()) {
704 // must be a subscript...
705 old.recordUndoInset();
707 cur.screenUpdateFlags(cur.result().update() | Update::SinglePar);
709 } else if (cell(1).empty()) {
710 // must be a superscript...
711 old.recordUndoInset();
713 cur.screenUpdateFlags(cur.result().update() | Update::SinglePar);
717 // Now the two suicide cases:
718 // * we have only one script which is empty
719 // * we have two scripts which are both empty.
720 // The script inset is removed completely.
721 if ((nargs() == 2 && cell(1).empty())
722 || (nargs() == 3 && cell(1).empty() && cell(2).empty())) {
723 // Make undo step. We cannot use cur for this because
724 // it does not necessarily point to us anymore. But we
725 // should be on top of the cursor old.
726 Cursor insetCur = old;
727 int scriptSlice = insetCur.find(this);
728 LASSERT(scriptSlice != -1, /**/);
729 insetCur.cutOff(scriptSlice);
730 insetCur.recordUndoInset();
732 // Let the script inset commit suicide. This is
733 // modelled on Cursor.pullArg(), but tries not to
734 // invoke notifyCursorLeaves again and does not touch
735 // cur (since the top slice will be deleted
737 MathData ar = cell(0);
739 insetCur.cell().erase(insetCur.pos());
740 insetCur.cell().insert(insetCur.pos(), ar);
743 cur.screenUpdateFlags(cur.result().update() | Update::SinglePar);
747 //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
752 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
754 //LYXERR("InsetMathScript: request: " << cmd);
756 if (cmd.action() == LFUN_MATH_LIMITS) {
757 if (!cmd.argument().empty()) {
758 if (cmd.argument() == "limits")
760 else if (cmd.argument() == "nolimits")
764 } else if (limits_ == 0)
765 limits_ = hasLimits() ? -1 : 1;
771 InsetMathNest::doDispatch(cur, cmd);
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.addPreambleSnippet("<style type=\"text/css\">\n"
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%;}\n"
785 InsetMathNest::validate(features);