]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_data.C
Jean-Marc's fix for wrong descent
[lyx.git] / src / mathed / math_data.C
index 8ee1a3418eff31f7ddaa59d07be6a1af071ee622..1f65cd705a856566805f3e6dae92b72cabdd5179 100644 (file)
@@ -1,16 +1,12 @@
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
 
 #include "math_data.h"
 #include "math_inset.h"
+#include "math_cursor.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_mathmlstream.h"
 #include "math_support.h"
 #include "math_replace.h"
@@ -66,6 +62,7 @@ void MathArray::insert(size_type pos, MathAtom const & t)
 
 void MathArray::insert(size_type pos, MathArray const & ar)
 {
+       lyx::Assert(pos <= size());
        base_type::insert(begin() + pos, ar.begin(), ar.end());
 }
 
@@ -113,7 +110,7 @@ void MathArray::dump() const
 {
        NormalStream ns(lyxerr);
        for (const_iterator it = begin(); it != end(); ++it)
-               ns << "<" << *it << ">";
+               ns << '<' << *it << '>';
 }
 
 
@@ -136,7 +133,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->nucleus()))
+               if (!(*jt)->match(*it))
                        return false;
        return true;
 }
@@ -165,7 +162,7 @@ bool MathArray::find1(MathArray const & ar, size_type pos) const
 {
        //lyxerr << "finding '" << ar << "' in '" << *this << "'\n";
        for (size_type i = 0, n = ar.size(); i < n; ++i)
-               if (!operator[](pos + i)->match(ar[i].nucleus()))
+               if (!operator[](pos + i)->match(ar[i]))
                        return false;
        return true;
 }
@@ -182,7 +179,7 @@ MathArray::size_type MathArray::find(MathArray const & ar) const
 
 MathArray::size_type MathArray::find_last(MathArray const & ar) const
 {
-       for (int i = size() - ar.size(); i >= 0; --i) 
+       for (int i = size() - ar.size(); i >= 0; --i)
                if (find1(ar, i))
                        return i;
        return size();
@@ -194,7 +191,7 @@ bool MathArray::contains(MathArray const & ar) const
        if (find(ar) != size())
                return true;
        for (const_iterator it = begin(); it != end(); ++it)
-               if (it->nucleus()->contains(ar))
+               if ((*it)->contains(ar))
                        return true;
        return false;
 }
@@ -214,12 +211,11 @@ Dimension const & MathArray::metrics(MathMetricsInfo & mi) const
        clean_  = true;
        drawn_  = false;
 
-       if (empty()) {
-               mathed_char_dim(mi.base.font, 'I', dim_);
+       mathed_char_dim(mi.base.font, 'I', dim_);
+       if (empty())
                return dim_;
-       }
 
-       dim_.clear();
+       dim_.w = 0;
        for (const_iterator it = begin(), et = end(); it != et; ++it) {
                (*it)->metrics(mi);
                dim_ += (*it)->dimensions();
@@ -276,7 +272,7 @@ 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;
@@ -300,10 +296,9 @@ int MathArray::pos2x(size_type pos1, size_type pos2, int glue) const
        size_type target = min(pos2, size());
        for (size_type i = pos1; i < target; ++i) {
                const_iterator it = begin() + i;
-               MathInset const * p = it->nucleus();
-               if (p->getChar() == ' ')
+               if ((*it)->getChar() == ' ')
                        x += glue;
-               x += p->width();
+               x += (*it)->width();
        }
        return x;
 }
@@ -323,10 +318,9 @@ MathArray::size_type MathArray::x2pos(size_type startpos, int targetx,
        int currx = 0;
        for (; currx < targetx && it < end(); ++it) {
                lastx = currx;
-               MathInset const * p = it->nucleus();
-               if (p->getChar() == ' ')
+               if ((*it)->getChar() == ' ')
                        currx += glue;
-               currx += p->width();
+               currx += (*it)->width();
        }
        if (abs(lastx - targetx) < abs(currx - targetx) && it != begin() + startpos)
                --it;
@@ -361,6 +355,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;
@@ -382,9 +377,37 @@ 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);
+               }
+       }
+
+}