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"
17 #include "MathStream.h"
18 #include "MathSupport.h"
20 #include "BufferView.h"
22 #include "LaTeXFeatures.h"
23 #include "MetricsInfo.h"
25 #include "support/debug.h"
26 #include "support/gettext.h"
27 #include "support/lassert.h"
35 InsetMathScript::InsetMathScript(Buffer * buf)
36 : InsetMathNest(buf, 1), cell_1_is_up_(false)
40 InsetMathScript::InsetMathScript(Buffer * buf, bool up)
41 : InsetMathNest(buf, 2), cell_1_is_up_(up)
45 InsetMathScript::InsetMathScript(Buffer * buf, MathAtom const & at, bool up)
46 : InsetMathNest(buf, 2), cell_1_is_up_(up)
48 LATTEST(nargs() >= 1);
49 cell(0).push_back(at);
53 Inset * InsetMathScript::clone() const
55 return new InsetMathScript(*this);
59 InsetMathScript const * InsetMathScript::asScriptInset() const
65 InsetMathScript * InsetMathScript::asScriptInset()
71 MathData const & InsetMathScript::down() const
80 MathData & InsetMathScript::down()
89 MathData const & InsetMathScript::up() const
96 MathData & InsetMathScript::up()
103 void InsetMathScript::ensure(bool up)
106 // just nucleus so far
107 cells_.push_back(MathData());
109 } else if (nargs() == 2 && !has(up)) {
111 cells_.push_back(cell(1));
114 cells_.push_back(MathData());
120 MathData const & InsetMathScript::nuc() const
126 MathData & InsetMathScript::nuc()
132 int InsetMathScript::dy01(BufferView const & bv, int asc, int des, int what) const
136 bool isCharBox = !nuc().empty() ? isAlphaSymbol(nuc().back()) : false;
138 Dimension const & dimdown = down().dimension(bv);
139 dasc = dimdown.ascent();
140 slevel = nuc().slevel();
141 int ascdrop = dasc - slevel;
142 int desdrop = isCharBox ? 0 : des + nuc().sshift();
143 int mindes = nuc().mindes();
144 des = max(desdrop, ascdrop);
145 des = max(mindes, des);
148 Dimension const & dimup = up().dimension(bv);
149 int minasc = nuc().minasc();
150 int ascdrop = isCharBox ? 0 : asc - up().mindes();
151 int udes = dimup.descent();
152 asc = udes + nuc().sshift();
153 asc = max(ascdrop, asc);
154 asc = max(minasc, asc);
156 int del = asc - udes - dasc;
157 if (del + des <= 2) {
158 int newdes = 2 - del;
159 del = slevel - asc + udes;
164 des = max(des, newdes);
168 return what ? asc : des;
172 int InsetMathScript::dy0(BufferView const & bv) const
177 int des = down().dimension(bv).ascent();
182 des = dy01(bv, na, nd, 0);
188 int InsetMathScript::dy1(BufferView const & bv) const
193 int asc = up().dimension(bv).descent();
198 asc = dy01(bv, na, nd, 1);
205 int InsetMathScript::dx0(BufferView const & bv) const
207 LASSERT(hasDown(), return 0);
208 Dimension const dim = dimension(bv);
209 return has_limits_ ? (dim.wid - down().dimension(bv).width()) / 2
210 : nwid(bv) + min(nker(&bv), 0);
214 int InsetMathScript::dx1(BufferView const & bv) const
216 LASSERT(hasUp(), return 0);
217 Dimension const dim = dimension(bv);
218 return has_limits_ ? (dim.wid - up().dimension(bv).width()) / 2
219 : nwid(bv) + max(nker(&bv), 0);
223 int InsetMathScript::dxx(BufferView const & bv) const
225 Dimension const dim = dimension(bv);
226 return has_limits_ ? (dim.wid - nwid(bv)) / 2 : 0;
230 int InsetMathScript::nwid(BufferView const & bv) const
232 return !nuc().empty() ? nuc().dimension(bv).width() : 2;
236 int InsetMathScript::nasc(BufferView const & bv) const
238 return !nuc().empty() ? nuc().dimension(bv).ascent() : 5;
242 int InsetMathScript::ndes(BufferView const & bv) const
244 return !nuc().empty() ? nuc().dimension(bv).descent() : 0;
248 int InsetMathScript::nker(BufferView const * bv) const
251 return nuc().kerning(bv);
256 MathClass InsetMathScript::mathClass() const
258 // FIXME: this is a hack, since the class will not be correct if
259 // the nucleus has several elements or if the last element is a math macro
260 // or a macro argument proxy.
261 // The correct implementation would require to linearize the nucleus.
265 // return the class of last element since this is the one that counts.
266 MathClass mc = nuc().back()->mathClass();
267 return (mc == MC_UNKNOWN) ? MC_ORD : mc;
272 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
278 // Keeps the changers local
279 Changer dummy2 = mi.base.changeEnsureMath();
280 cell(0).metrics(mi, dim0);
281 Changer dummy = mi.base.changeScript();
283 cell(1).metrics(mi, dim1, !has_limits_);
285 cell(2).metrics(mi, dim2, !has_limits_);
287 // we store this, because it is much easier
288 has_limits_ = hasLimits(mi.base.font);
291 BufferView & bv = *mi.base.bv;
292 // FIXME: data copying... not very efficient.
296 dimup = up().dimension(bv);
298 dimdown = down().dimension(bv);
303 dim.wid = max(dim.wid, dimup.width());
305 dim.wid = max(dim.wid, dimdown.width());
308 dim.wid = max(dim.wid, max(nker(mi.base.bv), 0) + dimup.width());
310 dim.wid = max(dim.wid, min(nker(mi.base.bv), 0) + dimdown.width());
315 int asc = dy1(bv) + dimup.ascent();
316 dim.asc = max(na, asc);
321 int des = dy0(bv) + dimdown.descent();
322 dim.des = max(nd, des);
328 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
330 Changer dummy2 = pi.base.changeEnsureMath();
331 BufferView & bv = *pi.base.bv;
333 nuc().draw(pi, x + dxx(bv), y);
335 nuc().setXY(bv, x + dxx(bv), y);
337 pi.draw(x + dxx(bv), y, char_type('.'));
339 Changer dummy = pi.base.changeScript();
341 up().draw(pi, x + dx1(bv), y - dy1(bv));
343 down().draw(pi, x + dx0(bv), y + dy0(bv));
347 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
350 up().metricsT(mi, dim);
352 down().metricsT(mi, dim);
353 nuc().metricsT(mi, dim);
357 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
361 nuc().drawT(pain, x + 1, y);
363 up().drawT(pain, x + 1, y - 1 /*dy1()*/);
365 down().drawT(pain, x + 1, y + 1 /*dy0()*/);
369 bool InsetMathScript::hasLimits(FontInfo const & font) const
371 if (font.style() != DISPLAY_STYLE)
376 Limits const lim = nuc().back()->limits() == AUTO_LIMITS
377 ? nuc().back()->defaultLimits() : nuc().back()->limits();
378 LASSERT(lim != AUTO_LIMITS, return false);
379 return lim == LIMITS;
383 void InsetMathScript::removeScript(bool up)
386 if (up == cell_1_is_up_)
388 } else if (nargs() == 3) {
390 swap(cells_[1], cells_[2]);
391 cell_1_is_up_ = false;
393 cell_1_is_up_ = true;
400 bool InsetMathScript::has(bool up) const
402 return idxOfScript(up);
406 bool InsetMathScript::hasUp() const
408 //lyxerr << "1up: " << bool(cell_1_is_up_));
409 //lyxerr << "hasUp: " << bool(idxOfScript(true)));
410 return idxOfScript(true);
414 bool InsetMathScript::hasDown() const
416 //LYXERR0("1up: " << bool(cell_1_is_up_));
417 //LYXERR0("hasDown: " << bool(idxOfScript(false)));
418 return idxOfScript(false);
422 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
427 return (cell_1_is_up_ == up) ? 1 : 0;
430 LASSERT(false, return 0);
434 bool InsetMathScript::idxForward(Cursor &) const
440 bool InsetMathScript::idxBackward(Cursor &) const
446 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
449 if (cur.idx() == 0) {
450 // don't go up/down if there is no cell in this direction
453 // go up/down only if in the last position
454 // or in the first position of something with displayed limits
455 if (cur.pos() == cur.lastpos()
456 || (cur.pos() == 0 && has_limits_)) {
457 cur.idx() = idxOfScript(up);
465 if (cur.idx() == idxOfScript(true)) {
466 // can't go further up
469 // otherwise go to last position in the nucleus
471 cur.pos() = cur.lastpos();
476 if (cur.idx() == idxOfScript(false)) {
477 // can't go further down
480 // otherwise go to last position in the nucleus
482 cur.pos() = cur.lastpos();
490 void InsetMathScript::write(WriteStream & os) const
492 MathEnsurer ensurer(os);
496 else if (os.firstitem())
497 LYXERR(Debug::MATHED, "suppressing {} when writing");
501 if (hasDown() /*&& !down().empty()*/)
502 os << "_{" << down() << '}';
504 if (hasUp() /*&& !up().empty()*/) {
505 // insert space if up() is empty or an empty brace inset
507 if (os.latex() && (up().empty() ||
508 (up().size() == 1 && up().back()->asBraceInset() &&
509 up().back()->asBraceInset()->cell(0).empty())))
512 os << "^{" << up() << '}';
515 if (lock_ && !os.latex())
520 void InsetMathScript::normalize(NormalStream & os) const
522 bool d = hasDown() && !down().empty();
523 bool u = hasUp() && !up().empty();
538 os << down() << ' ' << up() << ']';
546 void InsetMathScript::maple(MapleStream & os) const
550 if (hasDown() && !down().empty())
551 os << '[' << down() << ']';
552 if (hasUp() && !up().empty())
553 os << "^(" << up() << ')';
557 void InsetMathScript::mathematica(MathematicaStream & os) const
559 bool d = hasDown() && !down().empty();
560 bool u = hasUp() && !up().empty();
562 if (!nuc().empty()) {
564 os << "Subscript[" << nuc();
570 os << "^(" << up() << ')';
572 if (!nuc().empty()) {
574 os << ',' << down() << ']';
579 void InsetMathScript::mathmlize(MathStream & ms) const
581 bool d = hasDown() && !down().empty();
582 bool u = hasUp() && !up().empty();
583 // FIXME: the MathStream should be able to give us this information
584 bool l = has_limits_;
587 ms << MTag(l ? "munderover" : "msubsup");
589 ms << MTag(l ? "mover" : "msup");
591 ms << MTag(l ? "munder" : "msub");
594 ms << MTag("mrow") << nuc() << ETag("mrow");
596 ms << "<" << from_ascii(ms.namespacedTag("mrow")) << " />";
599 ms << MTag("mrow") << down() << ETag("mrow")
600 << MTag("mrow") << up() << ETag("mrow")
601 << ETag(l ? "munderover" : "msubsup");
603 ms << MTag("mrow") << up() << ETag("mrow") << ETag(l ? "mover" : "msup");
605 ms << MTag("mrow") << down() << ETag("mrow") << ETag(l ? "munder" : "msub");
609 void InsetMathScript::htmlize(HtmlStream & os) const
611 bool d = hasDown() && !down().empty();
612 bool u = hasUp() && !up().empty();
618 os << MTag("span", "class='scripts'")
619 << MTag("span") << up() << ETag("span")
620 << MTag("span") << down() << ETag("span")
623 os << MTag("sup", "class='math'") << up() << ETag("sup");
625 os << MTag("sub", "class='math'") << down() << ETag("sub");
629 void InsetMathScript::octave(OctaveStream & os) const
633 if (hasDown() && !down().empty())
634 os << '[' << down() << ']';
635 if (hasUp() && !up().empty())
636 os << "^(" << up() << ')';
640 void InsetMathScript::infoize(odocstream & os) const
646 void InsetMathScript::infoize2(odocstream & os) const
648 os << from_ascii(has_limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
652 bool InsetMathScript::notifyCursorLeaves(Cursor const & old, Cursor & cur)
654 InsetMathNest::notifyCursorLeaves(old, cur);
656 //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
658 // Remove empty scripts if possible:
660 // The case of two scripts, but only one got empty (1 = super, 2 = sub).
661 // We keep the script inset, but remove the empty script.
662 if (nargs() > 2 && (!cell(1).empty() || !cell(2).empty())) {
663 if (cell(2).empty()) {
664 // must be a subscript...
665 old.recordUndoInset();
667 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
669 } else if (cell(1).empty()) {
670 // must be a superscript...
671 old.recordUndoInset();
673 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
677 // Now the two suicide cases:
678 // * we have only one script which is empty
679 // * we have two scripts which are both empty.
680 // The script inset is removed completely.
681 if ((nargs() == 2 && cell(1).empty())
682 || (nargs() == 3 && cell(1).empty() && cell(2).empty())) {
683 // Make undo step. We cannot use cur for this because
684 // it does not necessarily point to us anymore. But we
685 // should be on top of the cursor old.
686 Cursor insetCur = old;
687 int scriptSlice = insetCur.find(this);
688 LASSERT(scriptSlice != -1, /**/);
689 insetCur.cutOff(scriptSlice);
690 insetCur.recordUndoInset();
692 // Let the script inset commit suicide. This is
693 // modelled on Cursor.pullArg(), but tries not to
694 // invoke notifyCursorLeaves again and does not touch
695 // cur (since the top slice will be deleted
697 MathData ar = cell(0);
699 insetCur.cell().erase(insetCur.pos());
700 insetCur.cell().insert(insetCur.pos(), ar);
703 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
707 //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
712 // the idea for dual scripts came from the eLyXer code
713 void InsetMathScript::validate(LaTeXFeatures & features) const
715 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
716 features.addCSSSnippet(
717 "span.scripts{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
718 "span.scripts span {display: block;}\n"
719 "sub.math{font-size: 75%;}\n"
720 "sup.math{font-size: 75%;}");
721 InsetMathNest::validate(features);