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
274 // we store this, because it is much easier
275 has_limits_ = hasLimits(mi.base.font);
277 // Compute metrics of the available cells
282 // Keeps the changers local
283 Changer dummy2 = mi.base.changeEnsureMath();
284 cell(0).metrics(mi, dim0);
285 Changer dummy = mi.base.changeScript();
287 cell(1).metrics(mi, dim1, !has_limits_);
289 cell(2).metrics(mi, dim2, !has_limits_);
293 BufferView & bv = *mi.base.bv;
294 // FIXME: data copying... not very efficient.
298 dimup = up().dimension(bv);
300 dimdown = down().dimension(bv);
305 dim.wid = max(dim.wid, dimup.width());
307 dim.wid = max(dim.wid, dimdown.width());
310 dim.wid = max(dim.wid, max(nker(mi.base.bv), 0) + dimup.width());
312 dim.wid = max(dim.wid, min(nker(mi.base.bv), 0) + dimdown.width());
317 int asc = dy1(bv) + dimup.ascent();
318 dim.asc = max(na, asc);
323 int des = dy0(bv) + dimdown.descent();
324 dim.des = max(nd, des);
330 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
332 Changer dummy2 = pi.base.changeEnsureMath();
333 BufferView & bv = *pi.base.bv;
335 nuc().draw(pi, x + dxx(bv), y);
337 nuc().setXY(bv, x + dxx(bv), y);
339 pi.draw(x + dxx(bv), y, char_type('.'));
341 Changer dummy = pi.base.changeScript();
343 up().draw(pi, x + dx1(bv), y - dy1(bv));
345 down().draw(pi, x + dx0(bv), y + dy0(bv));
349 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
352 up().metricsT(mi, dim);
354 down().metricsT(mi, dim);
355 nuc().metricsT(mi, dim);
359 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
363 nuc().drawT(pain, x + 1, y);
365 up().drawT(pain, x + 1, y - 1 /*dy1()*/);
367 down().drawT(pain, x + 1, y + 1 /*dy0()*/);
371 bool InsetMathScript::hasLimits(FontInfo const & font) const
373 if (font.style() != DISPLAY_STYLE)
378 Limits const lim = nuc().back()->limits() == AUTO_LIMITS
379 ? nuc().back()->defaultLimits() : nuc().back()->limits();
380 LASSERT(lim != AUTO_LIMITS, return false);
381 return lim == LIMITS;
385 void InsetMathScript::removeScript(bool up)
388 if (up == cell_1_is_up_)
390 } else if (nargs() == 3) {
392 swap(cells_[1], cells_[2]);
393 cell_1_is_up_ = false;
395 cell_1_is_up_ = true;
402 bool InsetMathScript::has(bool up) const
404 return idxOfScript(up);
408 bool InsetMathScript::hasUp() const
410 //lyxerr << "1up: " << bool(cell_1_is_up_));
411 //lyxerr << "hasUp: " << bool(idxOfScript(true)));
412 return idxOfScript(true);
416 bool InsetMathScript::hasDown() const
418 //LYXERR0("1up: " << bool(cell_1_is_up_));
419 //LYXERR0("hasDown: " << bool(idxOfScript(false)));
420 return idxOfScript(false);
424 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
429 return (cell_1_is_up_ == up) ? 1 : 0;
432 LASSERT(false, return 0);
436 bool InsetMathScript::idxForward(Cursor &) const
442 bool InsetMathScript::idxBackward(Cursor &) const
448 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
451 if (cur.idx() == 0) {
452 // don't go up/down if there is no cell in this direction
455 // go up/down only if in the last position
456 // or in the first position of something with displayed limits
457 if (cur.pos() == cur.lastpos()
458 || (cur.pos() == 0 && has_limits_)) {
459 cur.idx() = idxOfScript(up);
467 if (cur.idx() == idxOfScript(true)) {
468 // can't go further up
471 // otherwise go to last position in the nucleus
473 cur.pos() = cur.lastpos();
478 if (cur.idx() == idxOfScript(false)) {
479 // can't go further down
482 // otherwise go to last position in the nucleus
484 cur.pos() = cur.lastpos();
492 void InsetMathScript::write(WriteStream & os) const
494 MathEnsurer ensurer(os);
498 else if (os.firstitem())
499 LYXERR(Debug::MATHED, "suppressing {} when writing");
503 if (hasDown() /*&& !down().empty()*/)
504 os << "_{" << down() << '}';
506 if (hasUp() /*&& !up().empty()*/) {
507 // insert space if up() is empty or an empty brace inset
509 if (os.latex() && (up().empty() ||
510 (up().size() == 1 && up().back()->asBraceInset() &&
511 up().back()->asBraceInset()->cell(0).empty())))
514 os << "^{" << up() << '}';
517 if (lock_ && !os.latex())
522 void InsetMathScript::normalize(NormalStream & os) const
524 bool d = hasDown() && !down().empty();
525 bool u = hasUp() && !up().empty();
540 os << down() << ' ' << up() << ']';
548 void InsetMathScript::maple(MapleStream & os) const
552 if (hasDown() && !down().empty())
553 os << '[' << down() << ']';
554 if (hasUp() && !up().empty())
555 os << "^(" << up() << ')';
559 void InsetMathScript::mathematica(MathematicaStream & os) const
561 bool d = hasDown() && !down().empty();
562 bool u = hasUp() && !up().empty();
564 if (!nuc().empty()) {
566 os << "Subscript[" << nuc();
572 os << "^(" << up() << ')';
574 if (!nuc().empty()) {
576 os << ',' << down() << ']';
581 void InsetMathScript::mathmlize(MathStream & ms) const
583 bool d = hasDown() && !down().empty();
584 bool u = hasUp() && !up().empty();
585 // FIXME: the MathStream should be able to give us this information
586 bool l = has_limits_;
589 ms << MTag(l ? "munderover" : "msubsup");
591 ms << MTag(l ? "mover" : "msup");
593 ms << MTag(l ? "munder" : "msub");
596 ms << MTag("mrow") << nuc() << ETag("mrow");
598 ms << "<" << from_ascii(ms.namespacedTag("mrow")) << " />";
601 ms << MTag("mrow") << down() << ETag("mrow")
602 << MTag("mrow") << up() << ETag("mrow")
603 << ETag(l ? "munderover" : "msubsup");
605 ms << MTag("mrow") << up() << ETag("mrow") << ETag(l ? "mover" : "msup");
607 ms << MTag("mrow") << down() << ETag("mrow") << ETag(l ? "munder" : "msub");
611 void InsetMathScript::htmlize(HtmlStream & os) const
613 bool d = hasDown() && !down().empty();
614 bool u = hasUp() && !up().empty();
620 os << MTag("span", "class='scripts'")
621 << MTag("span") << up() << ETag("span")
622 << MTag("span") << down() << ETag("span")
625 os << MTag("sup", "class='math'") << up() << ETag("sup");
627 os << MTag("sub", "class='math'") << down() << ETag("sub");
631 void InsetMathScript::octave(OctaveStream & os) const
635 if (hasDown() && !down().empty())
636 os << '[' << down() << ']';
637 if (hasUp() && !up().empty())
638 os << "^(" << up() << ')';
642 void InsetMathScript::infoize(odocstream & os) const
648 void InsetMathScript::infoize2(odocstream & os) const
650 os << from_ascii(has_limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
654 bool InsetMathScript::notifyCursorLeaves(Cursor const & old, Cursor & cur)
656 InsetMathNest::notifyCursorLeaves(old, cur);
658 //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
660 // Remove empty scripts if possible:
662 // The case of two scripts, but only one got empty (1 = super, 2 = sub).
663 // We keep the script inset, but remove the empty script.
664 if (nargs() > 2 && (!cell(1).empty() || !cell(2).empty())) {
665 if (cell(2).empty()) {
666 // must be a subscript...
667 old.recordUndoInset();
669 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
671 } else if (cell(1).empty()) {
672 // must be a superscript...
673 old.recordUndoInset();
675 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
679 // Now the two suicide cases:
680 // * we have only one script which is empty
681 // * we have two scripts which are both empty.
682 // The script inset is removed completely.
683 if ((nargs() == 2 && cell(1).empty())
684 || (nargs() == 3 && cell(1).empty() && cell(2).empty())) {
685 // Make undo step. We cannot use cur for this because
686 // it does not necessarily point to us anymore. But we
687 // should be on top of the cursor old.
688 Cursor insetCur = old;
689 int scriptSlice = insetCur.find(this);
690 LASSERT(scriptSlice != -1, /**/);
691 insetCur.cutOff(scriptSlice);
692 insetCur.recordUndoInset();
694 // Let the script inset commit suicide. This is
695 // modelled on Cursor.pullArg(), but tries not to
696 // invoke notifyCursorLeaves again and does not touch
697 // cur (since the top slice will be deleted
699 MathData ar = cell(0);
701 insetCur.cell().erase(insetCur.pos());
702 insetCur.cell().insert(insetCur.pos(), ar);
705 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
709 //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
714 // the idea for dual scripts came from the eLyXer code
715 void InsetMathScript::validate(LaTeXFeatures & features) const
717 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
718 features.addCSSSnippet(
719 "span.scripts{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
720 "span.scripts span {display: block;}\n"
721 "sub.math{font-size: 75%;}\n"
722 "sup.math{font-size: 75%;}");
723 InsetMathNest::validate(features);