* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
- * \author André Pönitz
+ * \author André Pönitz
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
+#include "BufferView.h"
+#include "Cursor.h"
+#include "DispatchResult.h"
+#include "FuncRequest.h"
+#include "InsetMathFont.h"
#include "InsetMathScript.h"
-#include "MathArray.h"
+#include "InsetMathSymbol.h"
+#include "MathData.h"
#include "MathStream.h"
#include "MathSupport.h"
-#include "InsetMathSymbol.h"
-#include "InsetMathFont.h"
-#include "DispatchResult.h"
-#include "Cursor.h"
-#include "debug.h"
-#include "FuncRequest.h"
-#include "Undo.h"
-#include <boost/assert.hpp>
+#include "support/debug.h"
+#include "support/lassert.h"
-namespace lyx {
+#include <ostream>
-using std::string;
-using std::max;
-using std::auto_ptr;
-using std::endl;
+using namespace std;
+namespace lyx {
-InsetMathScript::InsetMathScript()
- : InsetMathNest(1), cell_1_is_up_(false), limits_(0)
+InsetMathScript::InsetMathScript(Buffer * buf)
+ : InsetMathNest(buf, 1), cell_1_is_up_(false), limits_(0)
{}
-InsetMathScript::InsetMathScript(bool up)
- : InsetMathNest(2), cell_1_is_up_(up), limits_(0)
+InsetMathScript::InsetMathScript(Buffer * buf, bool up)
+ : InsetMathNest(buf, 2), cell_1_is_up_(up), limits_(0)
{}
-InsetMathScript::InsetMathScript(MathAtom const & at, bool up)
- : InsetMathNest(2), cell_1_is_up_(up), limits_(0)
+InsetMathScript::InsetMathScript(Buffer * buf, MathAtom const & at, bool up)
+ : InsetMathNest(buf, 2), cell_1_is_up_(up), limits_(0)
{
- BOOST_ASSERT(nargs() >= 1);
+ LASSERT(nargs() >= 1, /**/);
cell(0).push_back(at);
}
-auto_ptr<InsetBase> InsetMathScript::doClone() const
+Inset * InsetMathScript::clone() const
{
- return auto_ptr<InsetBase>(new InsetMathScript(*this));
+ return new InsetMathScript(*this);
}
}
-MathArray const & InsetMathScript::down() const
+MathData const & InsetMathScript::down() const
{
if (nargs() == 3)
return cell(2);
- BOOST_ASSERT(nargs() > 1);
+ LASSERT(nargs() > 1, /**/);
return cell(1);
}
-MathArray & InsetMathScript::down()
+MathData & InsetMathScript::down()
{
if (nargs() == 3)
return cell(2);
- BOOST_ASSERT(nargs() > 1);
+ LASSERT(nargs() > 1, /**/);
return cell(1);
}
-MathArray const & InsetMathScript::up() const
+MathData const & InsetMathScript::up() const
{
- BOOST_ASSERT(nargs() > 1);
+ LASSERT(nargs() > 1, /**/);
return cell(1);
}
-MathArray & InsetMathScript::up()
+MathData & InsetMathScript::up()
{
- BOOST_ASSERT(nargs() > 1);
+ LASSERT(nargs() > 1, /**/);
return cell(1);
}
{
if (nargs() == 1) {
// just nucleus so far
- cells_.push_back(MathArray());
+ cells_.push_back(MathData());
cell_1_is_up_ = up;
} else if (nargs() == 2 && !has(up)) {
if (up) {
cells_.push_back(cell(1));
cell(1).clear();
} else {
- cells_.push_back(MathArray());
+ cells_.push_back(MathData());
}
}
}
-MathArray const & InsetMathScript::nuc() const
+MathData const & InsetMathScript::nuc() const
{
return cell(0);
}
-MathArray & InsetMathScript::nuc()
+MathData & InsetMathScript::nuc()
{
return cell(0);
}
return true;
if (at->asFontInset()) {
- MathArray const & ar = at->asFontInset()->cell(0);
+ MathData const & ar = at->asFontInset()->cell(0);
for (size_t i = 0; i < ar.size(); ++i) {
if (!(ar[i]->asCharInset() ||
(ar[i]->asSymbolInset() &&
} // namespace anon
-int InsetMathScript::dy01(int asc, int des, int what) const
+int InsetMathScript::dy01(BufferView const & bv, int asc, int des, int what) const
{
int dasc = 0;
int slevel = 0;
bool isCharBox = nuc().size() ? isAlphaSymbol(nuc().back()) : false;
if (hasDown()) {
- dasc = down().ascent();
+ Dimension const & dimdown = down().dimension(bv);
+ dasc = dimdown.ascent();
slevel = nuc().slevel();
int ascdrop = dasc - slevel;
int desdrop = isCharBox ? 0 : des + nuc().sshift();
des = max(mindes, des);
}
if (hasUp()) {
+ Dimension const & dimup = up().dimension(bv);
int minasc = nuc().minasc();
int ascdrop = isCharBox ? 0 : asc - up().mindes();
- int udes = up().descent();
+ int udes = dimup.descent();
asc = udes + nuc().sshift();
asc = max(ascdrop, asc);
asc = max(minasc, asc);
}
-int InsetMathScript::dy0() const
+int InsetMathScript::dy0(BufferView const & bv) const
{
- int nd = ndes();
+ int nd = ndes(bv);
if (!hasDown())
return nd;
- int des = down().ascent();
+ int des = down().dimension(bv).ascent();
if (hasLimits())
des += nd + 2;
else {
- int na = nasc();
- des = dy01(na, nd, 0);
+ int na = nasc(bv);
+ des = dy01(bv, na, nd, 0);
}
return des;
}
-int InsetMathScript::dy1() const
+int InsetMathScript::dy1(BufferView const & bv) const
{
- int na = nasc();
+ int na = nasc(bv);
if (!hasUp())
return na;
- int asc = up().descent();
+ int asc = up().dimension(bv).descent();
if (hasLimits())
asc += na + 2;
else {
- int nd = ndes();
- asc = dy01(na, nd, 1);
+ int nd = ndes(bv);
+ asc = dy01(bv, na, nd, 1);
}
asc = max(asc, 5);
return asc;
}
-int InsetMathScript::dx0() const
+int InsetMathScript::dx0(BufferView const & bv) const
{
- BOOST_ASSERT(hasDown());
- return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
+ LASSERT(hasDown(), /**/);
+ Dimension const dim = dimension(bv);
+ return hasLimits() ? (dim.wid - down().dimension(bv).width()) / 2 : nwid(bv);
}
-int InsetMathScript::dx1() const
+int InsetMathScript::dx1(BufferView const & bv) const
{
- BOOST_ASSERT(hasUp());
- return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid() + nker();
+ LASSERT(hasUp(), /**/);
+ Dimension const dim = dimension(bv);
+ return hasLimits() ? (dim.wid - up().dimension(bv).width()) / 2 : nwid(bv) + nker(&bv);
}
-int InsetMathScript::dxx() const
+int InsetMathScript::dxx(BufferView const & bv) const
{
- return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
+ Dimension const dim = dimension(bv);
+ return hasLimits() ? (dim.wid - nwid(bv)) / 2 : 0;
}
-int InsetMathScript::nwid() const
+int InsetMathScript::nwid(BufferView const & bv) const
{
- return nuc().size() ? nuc().width() : 2;
+ return nuc().size() ? nuc().dimension(bv).width() : 2;
}
-int InsetMathScript::nasc() const
+int InsetMathScript::nasc(BufferView const & bv) const
{
- return nuc().size() ? nuc().ascent() : 5;
+ return nuc().size() ? nuc().dimension(bv).ascent() : 5;
}
-int InsetMathScript::ndes() const
+int InsetMathScript::ndes(BufferView const & bv) const
{
- return nuc().size() ? nuc().descent() : 0;
+ return nuc().size() ? nuc().dimension(bv).descent() : 0;
}
-int InsetMathScript::nker() const
+int InsetMathScript::nker(BufferView const * bv) const
{
if (nuc().size()) {
- int kerning = nuc().kerning();
+ int kerning = nuc().kerning(bv);
return kerning > 0 ? kerning : 0;
}
return 0;
}
-bool InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
+void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
{
- cell(0).metrics(mi);
+ Dimension dim0;
+ Dimension dim1;
+ Dimension dim2;
+ cell(0).metrics(mi, dim0);
ScriptChanger dummy(mi.base);
if (nargs() > 1)
- cell(1).metrics(mi);
+ cell(1).metrics(mi, dim1);
if (nargs() > 2)
- cell(2).metrics(mi);
+ cell(2).metrics(mi, dim2);
+
dim.wid = 0;
+ BufferView & bv = *mi.base.bv;
+ // FIXME: data copying... not very efficient.
+ Dimension dimup;
+ Dimension dimdown;
+ if (hasUp())
+ dimup = up().dimension(bv);
+ if (hasDown())
+ dimdown = down().dimension(bv);
+
if (hasLimits()) {
- dim.wid = nwid();
+ dim.wid = nwid(bv);
if (hasUp())
- dim.wid = max(dim.wid, up().width());
+ dim.wid = max(dim.wid, dimup.width());
if (hasDown())
- dim.wid = max(dim.wid, down().width());
+ dim.wid = max(dim.wid, dimdown.width());
} else {
if (hasUp())
- dim.wid = max(dim.wid, nker() + up().width());
+ dim.wid = max(dim.wid, nker(mi.base.bv) + dimup.width());
if (hasDown())
- dim.wid = max(dim.wid, down().width());
- dim.wid += nwid();
+ dim.wid = max(dim.wid, dimdown.width());
+ dim.wid += nwid(bv);
}
- int na = nasc();
+ int na = nasc(bv);
if (hasUp()) {
- int asc = dy1() + up().ascent();
+ int asc = dy1(bv) + dimup.ascent();
dim.asc = max(na, asc);
} else
dim.asc = na;
- int nd = ndes();
+ int nd = ndes(bv);
if (hasDown()) {
- int des = dy0() + down().descent();
+ int des = dy0(bv) + dimdown.descent();
dim.des = max(nd, des);
} else
dim.des = nd;
metricsMarkers(dim);
- if (dim_ == dim)
- return false;
- dim_ = dim;
- return true;
}
void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
{
+ BufferView & bv = *pi.base.bv;
if (nuc().size())
- nuc().draw(pi, x + dxx(), y);
+ nuc().draw(pi, x + dxx(bv), y);
else {
- nuc().setXY(*pi.base.bv, x + dxx(), y);
- if (editing(pi.base.bv))
- pi.draw(x + dxx(), y, char_type('.'));
+ nuc().setXY(bv, x + dxx(bv), y);
+ if (editing(&bv))
+ pi.draw(x + dxx(bv), y, char_type('.'));
}
ScriptChanger dummy(pi.base);
if (hasUp())
- up().draw(pi, x + dx1(), y - dy1());
+ up().draw(pi, x + dx1(bv), y - dy1(bv));
if (hasDown())
- down().draw(pi, x + dx0(), y + dy0());
+ down().draw(pi, x + dx0(bv), y + dy0(bv));
drawMarkers(pi, x, y);
}
void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
{
+ // FIXME: BROKEN
if (nuc().size())
- nuc().drawT(pain, x + dxx(), y);
+ nuc().drawT(pain, x + 1, y);
if (hasUp())
- up().drawT(pain, x + dx1(), y - dy1());
+ up().drawT(pain, x + 1, y - 1 /*dy1()*/);
if (hasDown())
- down().drawT(pain, x + dx0(), y + dy0());
+ down().drawT(pain, x + 1, y + 1 /*dy0()*/);
}
bool InsetMathScript::hasUp() const
{
- //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
- //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl;
+ //lyxerr << "1up: " << bool(cell_1_is_up_));
+ //lyxerr << "hasUp: " << bool(idxOfScript(true)));
return idxOfScript(true);
}
bool InsetMathScript::hasDown() const
{
- //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
- //lyxerr << "hasDown: " << bool(idxOfScript(false)) << endl;
+ //LYXERR0("1up: " << bool(cell_1_is_up_));
+ //LYXERR0("hasDown: " << bool(idxOfScript(false)));
return idxOfScript(false);
}
-InsetBase::idx_type InsetMathScript::idxOfScript(bool up) const
+Inset::idx_type InsetMathScript::idxOfScript(bool up) const
{
if (nargs() == 1)
return 0;
return (cell_1_is_up_ == up) ? 1 : 0;
if (nargs() == 3)
return up ? 1 : 2;
- BOOST_ASSERT(false);
+ LASSERT(false, /**/);
// Silence compiler
return 0;
}
-bool InsetMathScript::idxRight(Cursor &) const
+bool InsetMathScript::idxForward(Cursor &) const
{
return false;
}
-bool InsetMathScript::idxLeft(Cursor &) const
+bool InsetMathScript::idxBackward(Cursor &) const
{
return false;
}
}
// Are we 'up'?
- if (has(up) && cur.idx() == idxOfScript(true)) {
+ if (cur.idx() == idxOfScript(true)) {
// can't go further up
if (up)
return false;
}
// Are we 'down'?
- if (has(up) && cur.idx() == idxOfScript(false)) {
+ if (cur.idx() == idxOfScript(false)) {
// can't go further down
if (!up)
return false;
void InsetMathScript::write(WriteStream & os) const
{
+ MathEnsurer ensurer(os);
+
if (nuc().size()) {
os << nuc();
//if (nuc().back()->takesLimits()) {
//}
} else {
if (os.firstitem())
- LYXERR(Debug::MATHED) << "suppressing {} when writing"
- << endl;
+ LYXERR(Debug::MATHED, "suppressing {} when writing");
else
os << "{}";
}
}
+// FIXME XHTML
+// It may be worth trying to output munder, mover, and munderover
+// in certain cases, e.g., for display formulas. But then we would
+// need to know if we're in a display formula.
void InsetMathScript::mathmlize(MathStream & os) const
{
bool d = hasDown() && down().size();
os << MTag("msub");
if (nuc().size())
- os << nuc();
+ os << MTag("mrow") << nuc() << ETag("mrow");
else
- os << "<mrow/>";
+ os << "<mrow />";
if (u && d)
- os << down() << up() << ETag("msubsup");
+ os << MTag("mrow") << down() << ETag("mrow")
+ << MTag("mrow") << up() << ETag("mrow")
+ << ETag("msubsup");
else if (u)
- os << up() << ETag("msup");
+ os << MTag("mrow") << up() << ETag("mrow") << ETag("msup");
else if (d)
- os << down() << ETag("msub");
+ os << MTag("mrow") << down() << ETag("mrow") << ETag("msub");
}
void InsetMathScript::infoize2(odocstream & os) const
{
if (limits_)
- os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
+ os << from_ascii(limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
}
-bool InsetMathScript::notifyCursorLeaves(Cursor & cur)
+bool InsetMathScript::notifyCursorLeaves(Cursor const & old, Cursor & cur)
{
- InsetMathNest::notifyCursorLeaves(cur);
+ InsetMathNest::notifyCursorLeaves(old, cur);
+
+ //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
- //lyxerr << "InsetMathScript::notifyCursorLeaves: 1 " << cur << endl;
+ // Remove empty scripts if possible:
- // remove empty scripts if possible
- if (nargs() > 2) {
- // Case of two scripts. In this case, 1 = super, 2 = sub
- if (cur.idx() == 2 && cell(2).empty()) {
+ // The case of two scripts, but only one got empty (1 = super, 2 = sub).
+ // We keep the script inset, but remove the empty script.
+ if (nargs() > 2 && (!cell(1).empty() || !cell(2).empty())) {
+ if (cell(2).empty()) {
// must be a subscript...
- recordUndoInset(cur);
+ old.recordUndoInset();
removeScript(false);
+ cur.updateFlags(cur.disp_.update() | Update::SinglePar);
return true;
- } else if (cur.idx() == 1 && cell(1).empty()) {
+ } else if (cell(1).empty()) {
// must be a superscript...
- recordUndoInset(cur);
+ old.recordUndoInset();
removeScript(true);
+ cur.updateFlags(cur.disp_.update() | Update::SinglePar);
return true;
}
- } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
- // could be either subscript or super script
- recordUndoInset(cur);
- removeScript(cell_1_is_up_);
+ }
+ // Now the two suicide cases:
+ // * we have only one script which is empty
+ // * we have two scripts which are both empty.
+ // The script inset is removed completely.
+ if ((nargs() == 2 && cell(1).empty())
+ || (nargs() == 3 && cell(1).empty() && cell(2).empty())) {
+ // Make undo step. We cannot use cur for this because
+ // it does not necessarily point to us anymore. But we
+ // should be on top of the cursor old.
+ Cursor insetCur = old;
+ int scriptSlice = insetCur.find(this);
+ LASSERT(scriptSlice != -1, /**/);
+ insetCur.cutOff(scriptSlice);
+ insetCur.recordUndoInset();
+
// Let the script inset commit suicide. This is
// modelled on Cursor.pullArg(), but tries not to
// invoke notifyCursorLeaves again and does not touch
// cur (since the top slice will be deleted
- // afterwards))
- MathArray ar = cell(0);
- Cursor tmpcur = cur;
- tmpcur.pop();
- tmpcur.cell().erase(tmpcur.pos());
- tmpcur.cell().insert(tmpcur.pos(), ar);
+ // afterwards)
+ MathData ar = cell(0);
+ insetCur.pop();
+ insetCur.cell().erase(insetCur.pos());
+ insetCur.cell().insert(insetCur.pos(), ar);
+
+ // redraw
+ cur.updateFlags(cur.disp_.update() | Update::SinglePar);
return true;
}
- //lyxerr << "InsetMathScript::notifyCursorLeaves: 2 " << cur << endl;
+ //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
return false;
}
void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
{
- //lyxerr << "InsetMathScript: request: " << cmd << std::endl;
+ //LYXERR("InsetMathScript: request: " << cmd);
if (cmd.action == LFUN_MATH_LIMITS) {
if (!cmd.argument().empty()) {