]> 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 67be300ef987497120307f210b9cd5030ca98648..e30fb3c4652455cde73add332851dd8f4476595d 100644 (file)
 #include "MathSupport.h"
 #include "ReplaceData.h"
 
-#include "BufferView.h"
 #include "Buffer.h"
+#include "BufferView.h"
+#include "CoordCache.h"
 #include "Cursor.h"
 #include "debug.h"
-#include "Color.h"
 
 #include "frontends/FontMetrics.h"
 #include "frontends/Painter.h"
 
 #include <boost/assert.hpp>
+#include <boost/next_prior.hpp>
 
 
 namespace lyx {
@@ -210,17 +211,6 @@ void MathData::touch() const
 }
 
 
-bool MathData::metrics(MetricsInfo & mi, Dimension & dim) const
-{
-       dim = dim_;
-       metrics(mi);
-       if (dim_ == dim)
-               return false;
-       dim = dim_;
-       return true;
-}
-
-
 namespace {
 
 bool isInside(DocIterator const & it, MathData const & ar,
@@ -238,27 +228,31 @@ bool isInside(DocIterator const & it, MathData const & ar,
 
 
 
-void MathData::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
@@ -276,17 +270,20 @@ void MathData::metrics(MetricsInfo & mi) const
                                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);
 }
 
 
@@ -296,15 +293,17 @@ void MathData::draw(PainterInfo & pi, int x, int y) const
        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(), Color::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;
 
@@ -330,7 +329,7 @@ void MathData::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;
        }
 }
 
@@ -377,7 +376,7 @@ int MathData::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;
 }
@@ -399,7 +398,7 @@ MathData::size_type MathData::x2pos(int targetx, int glue) const
                lastx = currx;
                if ((*it)->getChar() == ' ')
                        currx += glue;
-               currx += (*it)->width();
+               currx += atom_dims_[it - begin()].wid;
        }
 
        /**
@@ -424,30 +423,36 @@ MathData::size_type MathData::x2pos(int targetx, int glue) 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);
+}
+
+
+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);
+}
+
 
-       const int xo_ = xo(bv);
-       const int yo_ = yo(bv);
+Dimension const & MathData::dimension(BufferView const & bv) const
+{
+       return bv.coordCache().getArrays().dim(this);
+}
 
-       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();
+int MathData::xm(BufferView const & bv) const
+{
+       Geometry const & g = bv.coordCache().getArrays().geometry(this);
 
-       return xx + yy;
+       return g.pos.x_ + g.dim.wid / 2;
 }
 
 
-void MathData::setXY(BufferView & bv, int x, int y) const
+int MathData::ym(BufferView const & bv) const
 {
-       //lyxerr << "setting position cache for MathData " << this << std::endl;
-       bv.coordCache().arrays().add(this, x, y);
+       Geometry const & g = bv.coordCache().getArrays().geometry(this);
+
+       return g.pos.y_ + (g.dim.des - g.dim.asc) / 2;
 }