]> git.lyx.org Git - features.git/blobdiff - src/mathed/MathData.cpp
Move Color::color enum to ColorCode.h
[features.git] / src / mathed / MathData.cpp
index 805f8b5495d85ded6b8ff6a3aeb10b0aabbb8572..e30fb3c4652455cde73add332851dd8f4476595d 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * \file MathArray.cpp
+ * \file MathData.cpp
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
 
 #include <config.h>
 
-#include "MathArray.h"
+#include "MathData.h"
 #include "InsetMathFont.h"
 #include "InsetMathScript.h"
 #include "MathMacro.h"
-#include "MathMacroTable.h"
+#include "MacroTable.h"
 #include "MathStream.h"
 #include "MathSupport.h"
 #include "ReplaceData.h"
 
+#include "Buffer.h"
 #include "BufferView.h"
-#include "buffer.h"
-#include "cursor.h"
+#include "CoordCache.h"
+#include "Cursor.h"
 #include "debug.h"
-#include "LColor.h"
 
 #include "frontends/FontMetrics.h"
 #include "frontends/Painter.h"
 
 #include <boost/assert.hpp>
+#include <boost/next_prior.hpp>
 
 
 namespace lyx {
@@ -41,70 +42,70 @@ using std::string;
 using std::vector;
 
 
-MathArray::MathArray(const_iterator from, const_iterator to)
+MathData::MathData(const_iterator from, const_iterator to)
        : base_type(from, to)
 {}
 
 
-MathAtom & MathArray::operator[](pos_type pos)
+MathAtom & MathData::operator[](pos_type pos)
 {
        BOOST_ASSERT(pos < size());
        return base_type::operator[](pos);
 }
 
 
-MathAtom const & MathArray::operator[](pos_type pos) const
+MathAtom const & MathData::operator[](pos_type pos) const
 {
        BOOST_ASSERT(pos < size());
        return base_type::operator[](pos);
 }
 
 
-void MathArray::insert(size_type pos, MathAtom const & t)
+void MathData::insert(size_type pos, MathAtom const & t)
 {
        base_type::insert(begin() + pos, t);
 }
 
 
-void MathArray::insert(size_type pos, MathArray const & ar)
+void MathData::insert(size_type pos, MathData const & ar)
 {
        BOOST_ASSERT(pos <= size());
        base_type::insert(begin() + pos, ar.begin(), ar.end());
 }
 
 
-void MathArray::append(MathArray const & ar)
+void MathData::append(MathData const & ar)
 {
        insert(size(), ar);
 }
 
 
-void MathArray::erase(size_type pos)
+void MathData::erase(size_type pos)
 {
        if (pos < size())
                erase(pos, pos + 1);
 }
 
 
-void MathArray::erase(iterator pos1, iterator pos2)
+void MathData::erase(iterator pos1, iterator pos2)
 {
        base_type::erase(pos1, pos2);
 }
 
 
-void MathArray::erase(iterator pos)
+void MathData::erase(iterator pos)
 {
        base_type::erase(pos);
 }
 
 
-void MathArray::erase(size_type pos1, size_type pos2)
+void MathData::erase(size_type pos1, size_type pos2)
 {
        base_type::erase(begin() + pos1, begin() + pos2);
 }
 
 
-void MathArray::dump2() const
+void MathData::dump2() const
 {
        odocstringstream os;
        NormalStream ns(os);
@@ -114,7 +115,7 @@ void MathArray::dump2() const
 }
 
 
-void MathArray::dump() const
+void MathData::dump() const
 {
        odocstringstream os;
        NormalStream ns(os);
@@ -124,20 +125,20 @@ void MathArray::dump() const
 }
 
 
-void MathArray::validate(LaTeXFeatures & features) const
+void MathData::validate(LaTeXFeatures & features) const
 {
        for (const_iterator it = begin(); it != end(); ++it)
                (*it)->validate(features);
 }
 
 
-bool MathArray::match(MathArray const & ar) const
+bool MathData::match(MathData const & ar) const
 {
        return size() == ar.size() && matchpart(ar, 0);
 }
 
 
-bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
+bool MathData::matchpart(MathData const & ar, pos_type pos) const
 {
        if (size() < ar.size() + pos)
                return false;
@@ -149,7 +150,7 @@ bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
 }
 
 
-void MathArray::replace(ReplaceData & rep)
+void MathData::replace(ReplaceData & rep)
 {
        for (size_type i = 0; i < size(); ++i) {
                if (find1(rep.from, i)) {
@@ -160,15 +161,13 @@ void MathArray::replace(ReplaceData & rep)
                }
        }
 
-#ifdef WITH_WARNINGS
-#warning temporarily disabled
+       // FIXME: temporarily disabled
        // for (const_iterator it = begin(); it != end(); ++it)
        //      it->nucleus()->replace(rep);
-#endif
 }
 
 
-bool MathArray::find1(MathArray const & ar, size_type pos) const
+bool MathData::find1(MathData const & ar, size_type pos) const
 {
        lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
        for (size_type i = 0, n = ar.size(); i < n; ++i)
@@ -178,7 +177,7 @@ bool MathArray::find1(MathArray const & ar, size_type pos) const
 }
 
 
-MathArray::size_type MathArray::find(MathArray const & ar) const
+MathData::size_type MathData::find(MathData const & ar) const
 {
        for (int i = 0, last = size() - ar.size(); i < last; ++i)
                if (find1(ar, i))
@@ -187,7 +186,7 @@ MathArray::size_type MathArray::find(MathArray const & ar) const
 }
 
 
-MathArray::size_type MathArray::find_last(MathArray const & ar) const
+MathData::size_type MathData::find_last(MathData const & ar) const
 {
        for (int i = size() - ar.size(); i >= 0; --i)
                if (find1(ar, i))
@@ -196,7 +195,7 @@ MathArray::size_type MathArray::find_last(MathArray const & ar) const
 }
 
 
-bool MathArray::contains(MathArray const & ar) const
+bool MathData::contains(MathData const & ar) const
 {
        if (find(ar) != size())
                return true;
@@ -207,25 +206,14 @@ bool MathArray::contains(MathArray const & ar) const
 }
 
 
-void MathArray::touch() const
-{
-}
-
-
-bool MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
+void MathData::touch() const
 {
-       dim = dim_;
-       metrics(mi);
-       if (dim_ == dim)
-               return false;
-       dim = dim_;
-       return true;
 }
 
 
 namespace {
 
-bool isInside(DocIterator const & it, MathArray const & ar,
+bool isInside(DocIterator const & it, MathData const & ar,
        pos_type p1, pos_type p2)
 {
        for (size_t i = 0; i != it.depth(); ++i) {
@@ -240,27 +228,31 @@ bool isInside(DocIterator const & it, MathArray const & ar,
 
 
 
-void MathArray::metrics(MetricsInfo & mi) const
+void MathData::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
-       dim_ = fm.dimension('I');
+       dim = fm.dimension('I');
        int xascent = fm.dimension('x').ascent();
-       if (xascent >= dim_.asc)
-               xascent = (2 * dim_.asc) / 3;
+       if (xascent >= dim.asc)
+               xascent = (2 * dim.asc) / 3;
        minasc_ = xascent;
        mindes_ = (3 * xascent) / 4;
        slevel_ = (4 * xascent) / 5;
        sshift_ = xascent / 4;
        kerning_ = 0;
 
-       if (empty())
+       if (empty()) {
+               // Cache the dimension.
+               mi.base.bv->coordCache().arrays().add(this, dim);
                return;
+       }
 
-       dim_.asc = 0;
-       dim_.wid = 0;
+       dim.asc = 0;
+       dim.wid = 0;
        Dimension d;
+       atom_dims_.clear();
        //BufferView & bv  = *mi.base.bv;
-       //Buffer const & buf = *bv.buffer();
+       //Buffer const & buf = bv.buffer();
        for (size_t i = 0, n = size(); i != n; ++i) {
                MathAtom const & at = operator[](i);
 #if 0
@@ -273,40 +265,45 @@ void MathArray::metrics(MetricsInfo & mi) const
                        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;
+                               MathData args(begin() + i + 1, begin() + i + numargs + 1);
+                               MathData exp;
                                tmpl.expand(args, exp);
                                mac->setExpansion(exp, args);
                                mac->metricsExpanded(mi, d);
-                               dim_.wid += mac->widthExpanded();
+                               dim.wid += mac->widthExpanded();
                                i += numargs;
                                continue;
                        }
                }
 #endif
                at->metrics(mi, d);
-               dim_ += d;
+               atom_dims_.push_back(d);
+               dim += d;
                if (i == n - 1)
                        kerning_ = at->kerning();
        }
+       // Cache the dimension.
+       mi.base.bv->coordCache().arrays().add(this, dim);
 }
 
 
-void MathArray::draw(PainterInfo & pi, int x, int y) const
+void MathData::draw(PainterInfo & pi, int x, int y) const
 {
-       //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
+       //lyxerr << "MathData::draw: x: " << x << " y: " << y << endl;
        BufferView & bv  = *pi.base.bv;
        setXY(bv, x, y);
 
+       Dimension const & dim = bv.coordCache().getArrays().dim(this);
+
        if (empty()) {
-               pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
+               pi.pain.rectangle(x, y - dim.ascent(), dim.width(), dim.height(), Color_mathline);
                return;
        }
 
        // don't draw outside the workarea
-       if (y + descent() <= 0
-               || y - ascent() >= bv.workHeight()
-               || x + width() <= 0
+       if (y + dim.descent() <= 0
+               || y - dim.ascent() >= bv.workHeight()
+               || x + dim.width() <= 0
                || x >= bv. workWidth())
                return;
 
@@ -332,12 +329,12 @@ void MathArray::draw(PainterInfo & pi, int x, int y) const
                bv.coordCache().insets().add(at.nucleus(), x, y);
                at->drawSelection(pi, x, y);
                at->draw(pi, x, y);
-               x += at->width();
+               x += atom_dims_[i].wid;
        }
 }
 
 
-void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
+void MathData::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
 {
        dim.clear();
        Dimension d;
@@ -348,7 +345,7 @@ void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
 }
 
 
-void MathArray::drawT(TextPainter & pain, int x, int y) const
+void MathData::drawT(TextPainter & pain, int x, int y) const
 {
        //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
 
@@ -363,13 +360,13 @@ void MathArray::drawT(TextPainter & pain, int x, int y) const
 }
 
 
-int MathArray::pos2x(size_type pos) const
+int MathData::pos2x(size_type pos) const
 {
        return pos2x(pos, 0);
 }
 
 
-int MathArray::pos2x(size_type pos, int glue) const
+int MathData::pos2x(size_type pos, int glue) const
 {
        int x = 0;
        size_type target = min(pos, size());
@@ -379,19 +376,19 @@ int MathArray::pos2x(size_type pos, int glue) const
                        x += glue;
                //lyxerr << "char: " << (*it)->getChar()
                //      << "width: " << (*it)->width() << std::endl;
-               x += (*it)->width();
+               x += atom_dims_[i].wid;
        }
        return x;
 }
 
 
-MathArray::size_type MathArray::x2pos(int targetx) const
+MathData::size_type MathData::x2pos(int targetx) const
 {
        return x2pos(targetx, 0);
 }
 
 
-MathArray::size_type MathArray::x2pos(int targetx, int glue) const
+MathData::size_type MathData::x2pos(int targetx, int glue) const
 {
        const_iterator it = begin();
        int lastx = 0;
@@ -401,7 +398,7 @@ MathArray::size_type MathArray::x2pos(int targetx, int glue) const
                lastx = currx;
                if ((*it)->getChar() == ' ')
                        currx += glue;
-               currx += (*it)->width();
+               currx += atom_dims_[it - begin()].wid;
        }
 
        /**
@@ -424,48 +421,54 @@ MathArray::size_type MathArray::x2pos(int targetx, int glue) const
 }
 
 
-int MathArray::dist(BufferView const & bv, int x, int y) const
+int MathData::dist(BufferView const & bv, int x, int y) const
 {
-       int xx = 0;
-       int yy = 0;
+       return bv.coordCache().getArrays().squareDistance(this, x, y);
+}
 
-       const int xo_ = xo(bv);
-       const int yo_ = yo(bv);
 
-       if (x < xo_)
-               xx = xo_ - x;
-       else if (x > xo_ + width())
-               xx = x - xo_ - width();
+void MathData::setXY(BufferView & bv, int x, int y) const
+{
+       //lyxerr << "setting position cache for MathData " << this << std::endl;
+       bv.coordCache().arrays().add(this, x, y);
+}
 
-       if (y < yo_ - ascent())
-               yy = yo_ - ascent() - y;
-       else if (y > yo_ + descent())
-               yy = y - yo_ - descent();
 
-       return xx + yy;
+Dimension const & MathData::dimension(BufferView const & bv) const
+{
+       return bv.coordCache().getArrays().dim(this);
 }
 
 
-void MathArray::setXY(BufferView & bv, int x, int y) const
+int MathData::xm(BufferView const & bv) const
 {
-       //lyxerr << "setting position cache for MathArray " << this << std::endl;
-       bv.coordCache().arrays().add(this, x, y);
+       Geometry const & g = bv.coordCache().getArrays().geometry(this);
+
+       return g.pos.x_ + g.dim.wid / 2;
+}
+
+
+int MathData::ym(BufferView const & bv) const
+{
+       Geometry const & g = bv.coordCache().getArrays().geometry(this);
+
+       return g.pos.y_ + (g.dim.des - g.dim.asc) / 2;
 }
 
 
-int MathArray::xo(BufferView const & bv) const
+int MathData::xo(BufferView const & bv) const
 {
        return bv.coordCache().getArrays().x(this);
 }
 
 
-int MathArray::yo(BufferView const & bv) const
+int MathData::yo(BufferView const & bv) const
 {
        return bv.coordCache().getArrays().y(this);
 }
 
 
-std::ostream & operator<<(std::ostream & os, MathArray const & ar)
+std::ostream & operator<<(std::ostream & os, MathData const & ar)
 {
        odocstringstream oss;
        NormalStream ns(oss);
@@ -474,7 +477,7 @@ std::ostream & operator<<(std::ostream & os, MathArray const & ar)
 }
 
 
-odocstream & operator<<(odocstream & os, MathArray const & ar)
+odocstream & operator<<(odocstream & os, MathData const & ar)
 {
        NormalStream ns(os);
        ns << ar;