-#include <config.h>
+/**
+ * \file math_data.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author André Pönitz
+ *
+ * Full author contact details are available in file CREDITS.
+ */
-#ifdef __GNUG__
-#pragma implementation
-#endif
+#include <config.h>
#include "math_data.h"
-#include "math_inset.h"
-#include "math_deliminset.h"
-#include "math_charinset.h"
+#include "math_fontinset.h"
#include "math_scriptinset.h"
-#include "math_stringinset.h"
-#include "math_matrixinset.h"
+#include "math_macro.h"
+#include "math_macrotable.h"
#include "math_mathmlstream.h"
#include "math_support.h"
#include "math_replace.h"
+
+#include "coordcache.h"
+#include "LColor.h"
+#include "BufferView.h"
+#include "buffer.h"
+#include "cursor.h"
#include "debug.h"
-#include "support/LAssert.h"
+#include "frontends/Painter.h"
-MathArray::MathArray()
-{}
+#include <boost/assert.hpp>
+using std::abs;
+using std::endl;
+using std::min;
+using std::ostringstream;
+using std::string;
+using std::vector;
-MathArray::MathArray(MathArray const & ar, size_type from, size_type to)
- : bf_(ar.begin() + from, ar.begin() + to)
+
+MathArray::MathArray()
{}
-MathArray::MathArray(iterator from, iterator to)
- : bf_(from, to)
+MathArray::MathArray(const_iterator from, const_iterator to)
+ : base_type(from, to)
{}
-void MathArray::substitute(MathMacro const & m)
+MathAtom & MathArray::operator[](pos_type pos)
{
- for (iterator it = begin(); it != end(); ++it)
- it->nucleus()->substitute(m);
+ BOOST_ASSERT(pos < size());
+ return base_type::operator[](pos);
}
-MathAtom & MathArray::at(size_type pos)
+MathAtom const & MathArray::operator[](pos_type pos) const
{
- lyx::Assert(pos < size());
- return bf_[pos];
+ BOOST_ASSERT(pos < size());
+ return base_type::operator[](pos);
}
-MathAtom const & MathArray::at(size_type pos) const
+void MathArray::insert(size_type pos, MathAtom const & t)
{
- lyx::Assert(pos < size());
- return bf_[pos];
+ base_type::insert(begin() + pos, t);
}
-void MathArray::insert(size_type pos, MathAtom const & t)
+void MathArray::insert(size_type pos, MathArray const & ar)
{
- bf_.insert(begin() + pos, t);
+ BOOST_ASSERT(pos <= size());
+ base_type::insert(begin() + pos, ar.begin(), ar.end());
}
-void MathArray::insert(size_type pos, MathArray const & ar)
+void MathArray::append(MathArray const & ar)
{
- bf_.insert(begin() + pos, ar.begin(), ar.end());
+ insert(size(), ar);
}
-void MathArray::push_back(MathAtom const & t)
+void MathArray::erase(size_type pos)
{
- bf_.push_back(t);
+ if (pos < size())
+ erase(pos, pos + 1);
}
-void MathArray::push_back(MathArray const & ar)
+void MathArray::erase(iterator pos1, iterator pos2)
{
- insert(size(), ar);
+ base_type::erase(pos1, pos2);
}
-void MathArray::clear()
+void MathArray::erase(iterator pos)
{
- erase();
+ base_type::erase(pos);
}
-void MathArray::swap(MathArray & ar)
+void MathArray::erase(size_type pos1, size_type pos2)
{
- if (this != &ar)
- bf_.swap(ar.bf_);
+ base_type::erase(begin() + pos1, begin() + pos2);
}
-bool MathArray::empty() const
+void MathArray::dump2() const
{
- return bf_.empty();
+ NormalStream ns(lyxerr);
+ for (const_iterator it = begin(); it != end(); ++it)
+ ns << *it << ' ';
}
-MathArray::size_type MathArray::size() const
+void MathArray::dump() const
{
- return bf_.size();
+ NormalStream ns(lyxerr);
+ for (const_iterator it = begin(); it != end(); ++it)
+ ns << '<' << *it << '>';
}
-void MathArray::erase()
+void MathArray::validate(LaTeXFeatures & features) const
{
- bf_.erase(begin(), end());
+ for (const_iterator it = begin(); it != end(); ++it)
+ (*it)->validate(features);
}
-void MathArray::erase(size_type pos)
+bool MathArray::match(MathArray const & ar) const
{
- if (pos < size())
- erase(pos, pos + 1);
+ return size() == ar.size() && matchpart(ar, 0);
}
-void MathArray::erase(iterator pos1, iterator pos2)
+bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
{
- bf_.erase(pos1, pos2);
+ if (size() < ar.size() + pos)
+ return false;
+ const_iterator it = begin() + pos;
+ for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
+ if (asString(*it) != asString(*jt))
+ return false;
+ return true;
}
-void MathArray::erase(iterator pos)
+void MathArray::replace(ReplaceData & rep)
{
- bf_.erase(pos);
+ for (size_type i = 0; i < size(); ++i) {
+ if (find1(rep.from, i)) {
+ // match found
+ lyxerr << "match found!" << endl;
+ erase(i, i + rep.from.size());
+ insert(i, rep.to);
+ }
+ }
+
+#ifdef WITH_WARNINGS
+#warning temporarily disabled
+ // for (const_iterator it = begin(); it != end(); ++it)
+ // it->nucleus()->replace(rep);
+#endif
}
-void MathArray::erase(size_type pos1, size_type pos2)
+bool MathArray::find1(MathArray const & ar, size_type pos) const
{
- bf_.erase(begin() + pos1, begin() + pos2);
+ lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
+ for (size_type i = 0, n = ar.size(); i < n; ++i)
+ if (asString(operator[](pos + i)) != asString(ar[i]))
+ return false;
+ return true;
}
-MathAtom & MathArray::back()
+MathArray::size_type MathArray::find(MathArray const & ar) const
{
- return bf_.back();
+ for (int i = 0, last = size() - ar.size(); i < last; ++i)
+ if (find1(ar, i))
+ return i;
+ return size();
}
-MathAtom & MathArray::front()
+MathArray::size_type MathArray::find_last(MathArray const & ar) const
{
- return bf_.front();
+ for (int i = size() - ar.size(); i >= 0; --i)
+ if (find1(ar, i))
+ return i;
+ return size();
}
-MathAtom const & MathArray::front() const
+bool MathArray::contains(MathArray const & ar) const
{
- return bf_.front();
+ if (find(ar) != size())
+ return true;
+ for (const_iterator it = begin(); it != end(); ++it)
+ if ((*it)->contains(ar))
+ return true;
+ return false;
}
-void MathArray::dump2() const
+void MathArray::touch() const
{
- NormalStream ns(lyxerr);
- for (const_iterator it = begin(); it != end(); ++it)
- ns << it->nucleus() << ' ';
}
-void MathArray::dump() const
+void MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
{
- NormalStream ns(lyxerr);
- for (const_iterator it = begin(); it != end(); ++it)
- ns << "<" << it->nucleus() << ">";
+ metrics(mi);
+ dim = dim_;
}
-void MathArray::validate(LaTeXFeatures & features) const
+namespace {
+
+bool isInside(DocIterator const & it, MathArray const & ar,
+ lyx::pos_type p1, lyx::pos_type p2)
{
- for (const_iterator it = begin(); it != end(); ++it)
- if (it->nucleus())
- it->nucleus()->validate(features);
+ for (size_t i = 0; i != it.depth(); ++i) {
+ CursorSlice const & sl = it[i];
+ if (sl.inset().inMathed() && &sl.cell() == &ar)
+ return p1 <= sl.pos() && sl.pos() < p2;
+ }
+ return false;
+}
+
}
-void MathArray::pop_back()
+
+void MathArray::metrics(MetricsInfo & mi) const
{
- if (!size()) {
- lyxerr << "pop_back from empty array!\n";
+ mathed_char_dim(mi.base.font, 'I', dim_);
+
+ if (empty())
return;
+
+ dim_.wid = 0;
+ Dimension d;
+ //BufferView & bv = *mi.base.bv;
+ //Buffer const & buf = *bv.buffer();
+ for (size_t i = 0, n = size(); i != n; ++i) {
+ MathAtom const & at = operator[](i);
+#if 0
+ MathMacro const * mac = at->asMacro();
+ if (mac && buf.hasMacro(mac->name())) {
+ MacroData const & tmpl = buf.getMacro(mac->name());
+ int numargs = tmpl.numargs();
+ if (i + numargs > n)
+ numargs = n - i - 1;
+ lyxerr << "metrics:found macro: " << mac->name()
+ << " numargs: " << numargs << endl;
+ if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
+ MathArray args(begin() + i + 1, begin() + i + numargs + 1);
+ MathArray exp;
+ tmpl.expand(args, exp);
+ mac->setExpansion(exp, args);
+ mac->metricsExpanded(mi, d);
+ dim_.wid += mac->widthExpanded();
+ i += numargs;
+ continue;
+ }
+ }
+#endif
+ at->metrics(mi, d);
+ dim_ += d;
}
- bf_.pop_back();
}
-MathArray::const_iterator MathArray::begin() const
+void MathArray::draw(PainterInfo & pi, int x, int y) const
{
- return bf_.begin();
+ //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
+ setXY(x, y);
+
+ if (empty()) {
+ pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
+ return;
+ }
+
+ // don't draw outside the workarea
+ if (y + descent() <= 0
+ || y - ascent() >= pi.pain.paperHeight()
+ || x + width() <= 0
+ || x >= pi.pain.paperWidth())
+ return;
+
+ //BufferView & bv = *pi.base.bv;
+ for (size_t i = 0, n = size(); i != n; ++i) {
+ MathAtom const & at = operator[](i);
+#if 0
+ Buffer const & buf = *bv.buffer();
+ // special macro handling
+ MathMacro const * mac = at->asMacro();
+ if (mac && buf.hasMacro(mac->name())) {
+ MacroData const & tmpl = buf.getMacro(mac->name());
+ int numargs = tmpl.numargs();
+ if (i + numargs > n)
+ numargs = n - i - 1;
+ if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
+ mac->drawExpanded(pi, x, y);
+ x += mac->widthExpanded();
+ i += numargs;
+ continue;
+ }
+ }
+#endif
+ theCoords.insets().add(at.nucleus(), x, y);
+ at->drawSelection(pi, x, y);
+ at->draw(pi, x, y);
+ x += at->width();
+ }
}
-MathArray::const_iterator MathArray::end() const
+void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
{
- return bf_.end();
+ dim.clear();
+ Dimension d;
+ for (const_iterator it = begin(); it != end(); ++it) {
+ (*it)->metricsT(mi, d);
+ dim += d;
+ }
}
-MathArray::iterator MathArray::begin()
+void MathArray::drawT(TextPainter & pain, int x, int y) const
{
- return bf_.begin();
+ //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
+ setXY(x, y);
+
+ for (const_iterator it = begin(), et = end(); it != et; ++it) {
+ (*it)->drawT(pain, x, y);
+ //x += (*it)->width_;
+ x += 2;
+ }
}
-MathArray::iterator MathArray::end()
+int MathArray::pos2x(size_type pos) const
{
- return bf_.end();
+ return pos2x(pos, 0);
}
-bool MathArray::match(MathArray const & ar) const
+int MathArray::pos2x(size_type pos, int glue) const
{
- return size() == ar.size() && matchpart(ar, 0);
+ int x = 0;
+ size_type target = min(pos, size());
+ for (size_type i = 0; i < target; ++i) {
+ const_iterator it = begin() + i;
+ if ((*it)->getChar() == ' ')
+ x += glue;
+ //lyxerr << "char: " << (*it)->getChar()
+ // << "width: " << (*it)->width() << std::endl;
+ x += (*it)->width();
+ }
+ return x;
}
-bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
+MathArray::size_type MathArray::x2pos(int targetx) const
{
- if (size() < ar.size() + pos)
- return false;
- const_iterator it = begin() + pos;
- for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
- if (!jt->nucleus()->match(it->nucleus()))
- return false;
- return true;
+ return x2pos(targetx, 0);
}
-void MathArray::replace(ReplaceData & rep)
+MathArray::size_type MathArray::x2pos(int targetx, int glue) const
{
- for (size_type i = 0; i < size(); ++i) {
- iterator it = begin() + i;
- const_iterator rt = rep.from.begin();
- const_iterator et = rep.from.end();
- for (const_iterator jt = it; jt != end() && rt != et; ++jt, ++rt)
- if (!jt->nucleus()->match(rt->nucleus()))
- break;
- if (rt == et) {
- // match found
- lyxerr << "match found!\n";
- erase(it, it + rep.from.size());
- insert(i, rep.to);
- }
+ const_iterator it = begin();
+ int lastx = 0;
+ int currx = 0;
+ for (; currx < targetx && it < end(); ++it) {
+ lastx = currx;
+ if ((*it)->getChar() == ' ')
+ currx += glue;
+ currx += (*it)->width();
}
- for (const_iterator it = begin(); it != end(); ++it)
- it->nucleus()->replace(rep);
+ if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
+ --it;
+ // The below code guarantees that in this slice, the cursor will
+ // never be on the right edge of an inset after a mouse click.
+#ifdef WITH_WARNINGS
+#warning A better solution has to be found here!
+ // FIXME: this is too brute! The position left to an inset should
+ // be reachable with the mouse in general.
+#endif
+ if (it != begin()) {
+ --it;
+ if (it < end() && (*it)->getChar())
+ ++it;
+ }
+
+ return it - begin();
}
-bool MathArray::contains(MathArray const & ar) const
+int MathArray::dist(int x, int y) const
{
- for (size_type i = 0; i + ar.size() <= size(); ++i) {
- const_iterator it = begin() + i;
- const_iterator rt = ar.begin();
- const_iterator et = ar.end();
- for (const_iterator jt = it; rt != et; ++jt, ++rt)
- if (!jt->nucleus()->match(rt->nucleus()))
- break;
- if (rt == et)
- return true;
- }
+ int xx = 0;
+ int yy = 0;
- for (const_iterator it = begin(); it != end(); ++it)
- if (it->nucleus()->contains(ar))
- return true;
+ const int xo_ = xo();
+ const int yo_ = yo();
- return false;
+ if (x < xo_)
+ xx = xo_ - x;
+ else if (x > xo_ + width())
+ xx = x - xo_ - width();
+
+ if (y < yo_ - ascent())
+ yy = yo_ - ascent() - y;
+ else if (y > yo_ + descent())
+ yy = y - yo_ - descent();
+
+ return xx + yy;
+}
+
+
+void MathArray::setXY(int x, int y) const
+{
+ //lyxerr << "setting position cache for MathArray " << this << std::endl;
+ theCoords.arrays().add(this, x, y);
+}
+
+
+int MathArray::xo() const
+{
+ return theCoords.getArrays().x(this);
+}
+
+
+int MathArray::yo() const
+{
+ return theCoords.getArrays().y(this);
}