]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_data.C
mathed uglyfication
[lyx.git] / src / mathed / math_data.C
index f97c93ec07318ad071dd5700fcae1d47e7d6b183..028d213e5ab8207fb3f7d5c0a4df908730dea981 100644 (file)
@@ -1,30 +1,33 @@
-#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_cursor.h"
+#include "math_fontinset.h"
 #include "math_scriptinset.h"
-#include "math_stringinset.h"
-#include "math_matrixinset.h"
 #include "math_mathmlstream.h"
 #include "math_support.h"
 #include "math_replace.h"
 #include "debug.h"
-#include "support/LAssert.h"
-#include "math_metricsinfo.h"
+#include "LColor.h"
+
 #include "frontends/Painter.h"
-#include "textpainter.h"
 
+#include <boost/assert.hpp>
 
-using std::max;
-using std::min;
 using std::abs;
-
+using std::endl;
+using std::min;
+using std::ostringstream;
 
 
 MathArray::MathArray()
@@ -44,16 +47,16 @@ void MathArray::substitute(MathMacro const & m)
 }
 
 
-MathAtom & MathArray::operator[](size_type pos)
+MathAtom & MathArray::operator[](pos_type pos)
 {
-       lyx::Assert(pos < size());
+       BOOST_ASSERT(pos < size());
        return base_type::operator[](pos);
 }
 
 
-MathAtom const & MathArray::operator[](size_type pos) const
+MathAtom const & MathArray::operator[](pos_type pos) const
 {
-       lyx::Assert(pos < size());
+       BOOST_ASSERT(pos < size());
        return base_type::operator[](pos);
 }
 
@@ -66,6 +69,7 @@ void MathArray::insert(size_type pos, MathAtom const & t)
 
 void MathArray::insert(size_type pos, MathArray const & ar)
 {
+       BOOST_ASSERT(pos <= size());
        base_type::insert(begin() + pos, ar.begin(), ar.end());
 }
 
@@ -113,7 +117,7 @@ void MathArray::dump() const
 {
        NormalStream ns(lyxerr);
        for (const_iterator it = begin(); it != end(); ++it)
-               ns << "<" << *it << ">";
+               ns << '<' << *it << '>';
 }
 
 
@@ -136,7 +140,7 @@ bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
                return false;
        const_iterator it = begin() + pos;
        for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
-               if (!(*jt)->match(*it))
+               if (asString(*it) != asString(*jt))
                        return false;
        return true;
 }
@@ -147,7 +151,7 @@ void MathArray::replace(ReplaceData & rep)
        for (size_type i = 0; i < size(); ++i) {
                if (find1(rep.from, i)) {
                        // match found
-                       lyxerr << "match found!\n";
+                       lyxerr << "match found!" << endl;
                        erase(i, i + rep.from.size());
                        insert(i, rep.to);
                }
@@ -163,9 +167,9 @@ void MathArray::replace(ReplaceData & rep)
 
 bool MathArray::find1(MathArray const & ar, size_type pos) const
 {
-       //lyxerr << "finding '" << ar << "' in '" << *this << "'\n";
+       lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
        for (size_type i = 0, n = ar.size(); i < n; ++i)
-               if (!operator[](pos + i)->match(ar[i]))
+               if (asString(operator[](pos + i)) != asString(ar[i]))
                        return false;
        return true;
 }
@@ -207,28 +211,35 @@ void MathArray::touch() const
 }
 
 
-Dimension const & MathArray::metrics(MathMetricsInfo & mi) const
+void MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
+{
+       metrics(mi);
+       dim = dim_;
+}
+
+
+void MathArray::metrics(MetricsInfo & mi) const
 {
        //if (clean_)
        //      return;
        clean_  = true;
        drawn_  = false;
 
-       if (empty()) {
-               mathed_char_dim(mi.base.font, 'I', dim_);
-               return dim_;
-       }
+       mathed_char_dim(mi.base.font, 'I', dim_);
 
-       dim_.clear();
-       for (const_iterator it = begin(), et = end(); it != et; ++it) {
-               (*it)->metrics(mi);
-               dim_ += (*it)->dimensions();
+       if (!empty()) {
+               dim_.wid = 0;
+               Dimension d;
+               for (const_iterator it = begin(), et = end(); it != et; ++it) {
+                       (*it)->metrics(mi, d);
+                       dim_ += d;
+                       it->width_ = d.wid;
+               }
        }
-       return dim_;
 }
 
 
-void MathArray::draw(MathPainterInfo & pi, int x, int y) const
+void MathArray::draw(PainterInfo & pi, int x, int y) const
 {
        //if (drawn_ && x == xo_ && y == yo_)
        //      return;
@@ -253,22 +264,23 @@ void MathArray::draw(MathPainterInfo & pi, int x, int y) const
        }
 
        for (const_iterator it = begin(), et = end(); it != et; ++it) {
+               pi.width = it->width_;
                (*it)->draw(pi, x, y);
-               x += (*it)->width();
+               x += it->width_;
        }
 }
 
 
-Dimension const & MathArray::metricsT(TextMetricsInfo const & mi) const
+void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
 {
        //if (clean_)
        //      return;
-       dim_.clear();
+       dim.clear();
+       Dimension d;
        for (const_iterator it = begin(); it != end(); ++it) {
-               (*it)->metricsT(mi);
-               dim_ += (*it)->dimensions();
+               (*it)->metricsT(mi, d);
+               dim += d;
        }
-       return dim_;
 }
 
 
@@ -276,33 +288,33 @@ void MathArray::drawT(TextPainter & pain, int x, int y) const
 {
        //if (drawn_ && x == xo_ && y == yo_)
        //      return;
-       //lyxerr << "x: " << x << " y: " << y << " " << pain.workAreaHeight() << endl;
+       //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
        xo_    = x;
        yo_    = y;
        drawn_ = true;
 
        for (const_iterator it = begin(), et = end(); it != et; ++it) {
                (*it)->drawT(pain, x, y);
-               x += (*it)->width();
+               x += it->width_;
        }
 }
 
 
 int MathArray::pos2x(size_type pos) const
 {
-       return pos2x(0, pos, 0);
+       return pos2x(pos, 0);
 }
 
 
-int MathArray::pos2x(size_type pos1, size_type pos2, int glue) const
+int MathArray::pos2x(size_type pos, int glue) const
 {
        int x = 0;
-       size_type target = min(pos2, size());
-       for (size_type i = pos1; i < target; ++i) {
+       size_type target = min(pos, size());
+       for (size_type i = 0; i < target; ++i) {
                const_iterator it = begin() + i;
                if ((*it)->getChar() == ' ')
                        x += glue;
-               x += (*it)->width();
+               x += it->width_;
        }
        return x;
 }
@@ -310,23 +322,22 @@ int MathArray::pos2x(size_type pos1, size_type pos2, int glue) const
 
 MathArray::size_type MathArray::x2pos(int targetx) const
 {
-       return x2pos(0, targetx, 0);
+       return x2pos(targetx, 0);
 }
 
 
-MathArray::size_type MathArray::x2pos(size_type startpos, int targetx,
-       int glue) const
+MathArray::size_type MathArray::x2pos(int targetx, int glue) const
 {
-       const_iterator it = begin() + startpos;
+       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();
+               currx += it->width_;
        }
-       if (abs(lastx - targetx) < abs(currx - targetx) && it != begin() + startpos)
+       if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
                --it;
        return it - begin();
 }
@@ -359,6 +370,7 @@ void MathArray::boundingBox(int & x1, int & x2, int & y1, int & y2)
        y2 = yo_ + descent();
 }
 
+
 void MathArray::center(int & x, int & y) const
 {
        x = xo_ + width() / 2;
@@ -380,8 +392,38 @@ void MathArray::towards(int & x, int & y) const
 }
 
 
-void MathArray::setXY(int x, int y)
+void MathArray::setXY(int x, int y) const
 {
        xo_ = x;
        yo_ = y;
 }
+
+
+void MathArray::notifyCursorLeaves()
+{
+       // do not recurse!
+
+/*
+       // remove base-only "scripts"
+       for (pos_type i = 0; i + 1 < size(); ++i) {
+               MathScriptInset * p = operator[](i).nucleus()->asScriptInset();
+               if (p && p->cell(0).empty() && p->cell(1).empty()) {
+                       MathArray ar = p->nuc();
+                       erase(i);
+                       insert(i, ar);
+                       mathcursor->adjust(i, ar.size() - 1);
+               }
+       }
+
+       // glue adjacent font insets of the same kind
+       for (pos_type i = 0; i + 1 < size(); ++i) {
+               MathFontInset * p = operator[](i).nucleus()->asFontInset();
+               MathFontInset const * q = operator[](i + 1)->asFontInset();
+               if (p && q && p->name() == q->name()) {
+                       p->cell(0).append(q->cell(0));
+                       erase(i + 1);
+                       mathcursor->adjust(i, -1);
+               }
+       }
+*/
+}