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 Limits InsetMathScript::limits() const
261 // only the limits status of the last element counts
262 return nuc().back()->limits();
266 void InsetMathScript::limits(Limits lim)
269 nuc().back()->limits(lim);
273 MathClass InsetMathScript::mathClass() const
275 // FIXME: this is a hack, since the class will not be correct if
276 // the nucleus has several elements or if the last element is a math macro
277 // or a macro argument proxy.
278 // The correct implementation would require to linearize the nucleus.
282 // return the class of last element since this is the one that counts.
283 MathClass mc = nuc().back()->mathClass();
284 return (mc == MC_UNKNOWN) ? MC_ORD : mc;
289 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
291 // we store this, because it is much easier
292 has_limits_ = hasLimits(mi.base.font);
294 // Compute metrics of the available cells
299 // Keeps the changers local
300 Changer dummy2 = mi.base.changeEnsureMath();
301 cell(0).metrics(mi, dim0);
302 Changer dummy = mi.base.changeScript();
304 cell(1).metrics(mi, dim1, !has_limits_);
306 cell(2).metrics(mi, dim2, !has_limits_);
310 BufferView & bv = *mi.base.bv;
311 // FIXME: data copying... not very efficient.
315 dimup = up().dimension(bv);
317 dimdown = down().dimension(bv);
322 dim.wid = max(dim.wid, dimup.width());
324 dim.wid = max(dim.wid, dimdown.width());
327 dim.wid = max(dim.wid, max(nker(mi.base.bv), 0) + dimup.width());
329 dim.wid = max(dim.wid, min(nker(mi.base.bv), 0) + dimdown.width());
334 int asc = dy1(bv) + dimup.ascent();
335 dim.asc = max(na, asc);
340 int des = dy0(bv) + dimdown.descent();
341 dim.des = max(nd, des);
347 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
349 Changer dummy2 = pi.base.changeEnsureMath();
350 BufferView & bv = *pi.base.bv;
352 nuc().draw(pi, x + dxx(bv), y);
354 nuc().setXY(bv, x + dxx(bv), y);
356 pi.draw(x + dxx(bv), y, char_type('.'));
358 Changer dummy = pi.base.changeScript();
360 up().draw(pi, x + dx1(bv), y - dy1(bv));
362 down().draw(pi, x + dx0(bv), y + dy0(bv));
366 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
369 up().metricsT(mi, dim);
371 down().metricsT(mi, dim);
372 nuc().metricsT(mi, dim);
376 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
380 nuc().drawT(pain, x + 1, y);
382 up().drawT(pain, x + 1, y - 1 /*dy1()*/);
384 down().drawT(pain, x + 1, y + 1 /*dy0()*/);
388 bool InsetMathScript::hasLimits(FontInfo const & font) const
393 Limits const lim = nuc().back()->limits() == AUTO_LIMITS
394 ? nuc().back()->defaultLimits(font.style() == DISPLAY_STYLE)
395 : nuc().back()->limits();
396 LASSERT(lim != AUTO_LIMITS, return false);
397 return lim == LIMITS;
401 void InsetMathScript::removeScript(bool up)
404 if (up == cell_1_is_up_)
406 } else if (nargs() == 3) {
408 swap(cells_[1], cells_[2]);
409 cell_1_is_up_ = false;
411 cell_1_is_up_ = true;
418 bool InsetMathScript::has(bool up) const
420 return idxOfScript(up);
424 bool InsetMathScript::hasUp() const
426 //lyxerr << "1up: " << bool(cell_1_is_up_));
427 //lyxerr << "hasUp: " << bool(idxOfScript(true)));
428 return idxOfScript(true);
432 bool InsetMathScript::hasDown() const
434 //LYXERR0("1up: " << bool(cell_1_is_up_));
435 //LYXERR0("hasDown: " << bool(idxOfScript(false)));
436 return idxOfScript(false);
440 idx_type InsetMathScript::idxOfScript(bool up) const
445 return (cell_1_is_up_ == up) ? 1 : 0;
448 LASSERT(false, return 0);
452 bool InsetMathScript::idxForward(Cursor &) const
458 bool InsetMathScript::idxBackward(Cursor &) const
464 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
467 if (cur.idx() == 0) {
468 // don't go up/down if there is no cell in this direction
471 // go up/down only if in the last position
472 // or in the first position of something with displayed limits
473 if (cur.pos() == cur.lastpos()
474 || (cur.pos() == 0 && has_limits_)) {
475 cur.idx() = idxOfScript(up);
483 if (cur.idx() == idxOfScript(true)) {
484 // can't go further up
487 // otherwise go to last position in the nucleus
489 cur.pos() = cur.lastpos();
494 if (cur.idx() == idxOfScript(false)) {
495 // can't go further down
498 // otherwise go to last position in the nucleus
500 cur.pos() = cur.lastpos();
508 void InsetMathScript::write(TeXMathStream & os) const
510 MathEnsurer ensurer(os);
514 else if (os.firstitem())
515 LYXERR(Debug::MATHED, "suppressing {} when writing");
519 if (hasDown() /*&& !down().empty()*/)
520 os << "_{" << down() << '}';
522 if (hasUp() /*&& !up().empty()*/) {
523 // insert space if up() is empty or an empty brace inset
525 if (os.latex() && (up().empty() ||
526 (up().size() == 1 && up().back()->asBraceInset() &&
527 up().back()->asBraceInset()->cell(0).empty())))
530 os << "^{" << up() << '}';
533 if (lock_ && !os.latex())
538 void InsetMathScript::normalize(NormalStream & os) const
540 bool d = hasDown() && !down().empty();
541 bool u = hasUp() && !up().empty();
556 os << down() << ' ' << up() << ']';
564 void InsetMathScript::maple(MapleStream & os) const
568 if (hasDown() && !down().empty())
569 os << '[' << down() << ']';
570 if (hasUp() && !up().empty())
571 os << "^(" << up() << ')';
575 void InsetMathScript::mathematica(MathematicaStream & os) const
577 bool d = hasDown() && !down().empty();
578 bool u = hasUp() && !up().empty();
580 if (!nuc().empty()) {
582 os << "Subscript[" << nuc();
588 os << "^(" << up() << ')';
590 if (!nuc().empty()) {
592 os << ',' << down() << ']';
597 void InsetMathScript::mathmlize(MathMLStream & ms) const
599 bool d = hasDown() && !down().empty();
600 bool u = hasUp() && !up().empty();
601 // FIXME: the MathMLStream should be able to give us this information
602 bool l = has_limits_;
605 ms << MTag(l ? "munderover" : "msubsup");
607 ms << MTag(l ? "mover" : "msup");
609 ms << MTag(l ? "munder" : "msub");
612 ms << MTag("mrow") << nuc() << ETag("mrow");
614 ms << "<" << from_ascii(ms.namespacedTag("mrow")) << " />";
617 ms << MTag("mrow") << down() << ETag("mrow")
618 << MTag("mrow") << up() << ETag("mrow")
619 << ETag(l ? "munderover" : "msubsup");
621 ms << MTag("mrow") << up() << ETag("mrow") << ETag(l ? "mover" : "msup");
623 ms << 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
666 os << from_ascii(has_limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
670 bool InsetMathScript::notifyCursorLeaves(Cursor const & old, Cursor & cur)
672 InsetMathNest::notifyCursorLeaves(old, cur);
674 //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
676 // Remove empty scripts if possible:
678 // The case of two scripts, but only one got empty (1 = super, 2 = sub).
679 // We keep the script inset, but remove the empty script.
680 if (nargs() > 2 && (!cell(1).empty() || !cell(2).empty())) {
681 if (cell(2).empty()) {
682 // must be a subscript...
683 old.recordUndoInset();
685 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
687 } else if (cell(1).empty()) {
688 // must be a superscript...
689 old.recordUndoInset();
691 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
695 // Now the two suicide cases:
696 // * we have only one script which is empty
697 // * we have two scripts which are both empty.
698 // The script inset is removed completely.
699 if ((nargs() == 2 && cell(1).empty())
700 || (nargs() == 3 && cell(1).empty() && cell(2).empty())) {
701 // Make undo step. We cannot use cur for this because
702 // it does not necessarily point to us anymore. But we
703 // should be on top of the cursor old.
704 Cursor insetCur = old;
705 int scriptSlice = insetCur.find(this);
706 LASSERT(scriptSlice != -1, /**/);
707 insetCur.cutOff(scriptSlice);
708 insetCur.recordUndoInset();
710 // Let the script inset commit suicide. This is
711 // modelled on Cursor.pullArg(), but tries not to
712 // invoke notifyCursorLeaves again and does not touch
713 // cur (since the top slice will be deleted
715 MathData ar = cell(0);
717 insetCur.cell().erase(insetCur.pos());
718 insetCur.cell().insert(insetCur.pos(), ar);
721 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
725 //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
730 // the idea for dual scripts came from the eLyXer code
731 void InsetMathScript::validate(LaTeXFeatures & features) const
733 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
734 features.addCSSSnippet(
735 "span.scripts{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
736 "span.scripts span {display: block;}\n"
737 "sub.math{font-size: 75%;}\n"
738 "sup.math{font-size: 75%;}");
739 InsetMathNest::validate(features);