]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_data.C
small up/down tweaking
[lyx.git] / src / mathed / math_data.C
index 6e95cdcfceda9dad1212a4ca4c9a41558f784758..757b5d0a6fde06f6a0c199a390869d91bd8c0937 100644 (file)
@@ -1,32 +1,38 @@
+#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"
 #include "debug.h"
 #include "support/LAssert.h"
+#include "math_metricsinfo.h"
+#include "frontends/Painter.h"
+#include "textpainter.h"
 
 
-MathArray::MathArray()
-{}
+using std::max;
+using std::min;
+using std::abs;
+
 
 
-MathArray::MathArray(MathArray const & ar, size_type from, size_type to)
-       : bf_(ar.begin() + from, ar.begin() + to)
+MathArray::MathArray()
+       : xo_(0), yo_(0), clean_(false), drawn_(false)
 {}
 
 
-MathArray::MathArray(iterator from, iterator to)
-       : bf_(from, to)
+MathArray::MathArray(const_iterator from, const_iterator to)
+       : base_type(from, to), xo_(0), yo_(0), clean_(false), drawn_(false)
 {}
 
 
@@ -37,204 +43,373 @@ void MathArray::substitute(MathMacro const & m)
 }
 
 
-MathAtom & MathArray::at(size_type pos)
+MathAtom & MathArray::operator[](size_type pos)
 {
        lyx::Assert(pos < size());
-       return bf_[pos];
+       return base_type::operator[](pos);
 }
 
 
-MathAtom const & MathArray::at(size_type pos) const
+MathAtom const & MathArray::operator[](size_type pos) const
 {
        lyx::Assert(pos < size());
-       return bf_[pos];
+       return base_type::operator[](pos);
 }
 
 
 void MathArray::insert(size_type pos, MathAtom const & t)
 {
-       bf_.insert(begin() + pos, t);
+       base_type::insert(begin() + pos, t);
 }
 
 
 void MathArray::insert(size_type pos, MathArray const & ar)
 {
-       bf_.insert(begin() + pos, ar.begin(), ar.end());
+       lyx::Assert(pos <= size());
+       base_type::insert(begin() + pos, ar.begin(), ar.end());
 }
 
 
-void MathArray::push_back(MathAtom const & t)
-{      
-       bf_.push_back(t);
+void MathArray::append(MathArray const & ar)
+{
+       insert(size(), ar);
 }
 
 
-void MathArray::push_back(MathArray const & ar)
+void MathArray::erase(size_type pos)
 {
-       insert(size(), ar);
+       if (pos < size())
+               erase(pos, pos + 1);
 }
 
 
-void MathArray::clear()
+void MathArray::erase(iterator pos1, iterator pos2)
 {
-       erase();
+       base_type::erase(pos1, pos2);
 }
 
 
-void MathArray::swap(MathArray & ar)
+void MathArray::erase(iterator pos)
 {
-       if (this != &ar) 
-               bf_.swap(ar.bf_);
+       base_type::erase(pos);
 }
 
 
-bool MathArray::empty() const
+void MathArray::erase(size_type pos1, size_type pos2)
 {
-       return bf_.empty();
+       base_type::erase(begin() + pos1, begin() + pos2);
 }
 
 
-MathArray::size_type MathArray::size() const
+void MathArray::dump2() const
 {
-       return bf_.size();
+       NormalStream ns(lyxerr);
+       for (const_iterator it = begin(); it != end(); ++it)
+               ns << *it << ' ';
 }
 
 
-void MathArray::erase()
+void MathArray::dump() const
 {
-       bf_.erase(begin(), end());
+       NormalStream ns(lyxerr);
+       for (const_iterator it = begin(); it != end(); ++it)
+               ns << '<' << *it << '>';
 }
 
 
-void MathArray::erase(size_type pos)
+void MathArray::validate(LaTeXFeatures & features) const
 {
-       if (pos < size())
-               erase(pos, pos + 1);
+       for (const_iterator it = begin(); it != end(); ++it)
+               (*it)->validate(features);
 }
 
 
-void MathArray::erase(iterator pos1, iterator pos2)
+bool MathArray::match(MathArray const & ar) const
 {
-       bf_.erase(pos1, pos2);
+       return size() == ar.size() && matchpart(ar, 0);
 }
 
 
-void MathArray::erase(iterator pos)
+bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
 {
-       bf_.erase(pos);
+       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)->match(*it))
+                       return false;
+       return true;
 }
 
 
-void MathArray::erase(size_type pos1, size_type pos2)
+void MathArray::replace(ReplaceData & rep)
 {
-       bf_.erase(begin() + pos1, begin() + pos2);
+       for (size_type i = 0; i < size(); ++i) {
+               if (find1(rep.from, i)) {
+                       // match found
+                       lyxerr << "match found!\n";
+                       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
 }
 
 
-MathAtom & MathArray::back()
+bool MathArray::find1(MathArray const & ar, size_type pos) const
 {
-       return bf_.back();
+       //lyxerr << "finding '" << ar << "' in '" << *this << "'\n";
+       for (size_type i = 0, n = ar.size(); i < n; ++i)
+               if (!operator[](pos + i)->match(ar[i]))
+                       return false;
+       return true;
 }
 
 
-MathAtom & MathArray::front()
+MathArray::size_type MathArray::find(MathArray const & ar) const
 {
-       return bf_.front();
+       for (int i = 0, last = size() - ar.size(); i < last; ++i)
+               if (find1(ar, i))
+                       return i;
+       return size();
 }
 
 
-MathAtom const & MathArray::front() const
+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();
 }
 
 
-void MathArray::dump2() const
+bool MathArray::contains(MathArray const & ar) const
 {
-       NormalStream ns(lyxerr);
+       if (find(ar) != size())
+               return true;
        for (const_iterator it = begin(); it != end(); ++it)
-               ns << it->nucleus() << ' ';
+               if ((*it)->contains(ar))
+                       return true;
+       return false;
 }
 
 
-void MathArray::dump() const
+void MathArray::touch() const
 {
-       NormalStream ns(lyxerr);
-       for (const_iterator it = begin(); it != end(); ++it)
-               ns << "<" << it->nucleus() << ">";
+       clean_  = false;
+       drawn_  = false;
 }
 
 
-void MathArray::validate(LaTeXFeatures & features) const
+Dimension const & MathArray::metrics(MathMetricsInfo & mi) const
 {
-       for (const_iterator it = begin(); it != end(); ++it)
-               if (it->nucleus())
-                       it->nucleus()->validate(features);
+       //if (clean_)
+       //      return;
+       clean_  = true;
+       drawn_  = false;
+
+       mathed_char_dim(mi.base.font, 'I', dim_);
+       if (empty())
+               return dim_;
+
+       dim_.w = 0;
+       for (const_iterator it = begin(), et = end(); it != et; ++it) {
+               (*it)->metrics(mi);
+               dim_ += (*it)->dimensions();
+       }
+       return dim_;
 }
 
 
-void MathArray::pop_back()
-{      
-       if (!size()) {
-               lyxerr << "pop_back from empty array!\n";
+void MathArray::draw(MathPainterInfo & pi, int x, int y) const
+{
+       //if (drawn_ && x == xo_ && y == yo_)
+       //      return;
+       //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
+
+       xo_    = x;
+       yo_    = y;
+       drawn_ = true;
+
+       if (y + descent() <= 0)                   // don't draw above the workarea
+               return;
+       if (y - ascent() >= pi.pain.paperHeight())   // don't draw below the workarea
+               return;
+       if (x + width() <= 0)                     // don't draw left of workarea
+               return;
+       if (x >= pi.pain.paperWidth())              // don't draw right of workarea
                return;
+
+       if (empty()) {
+               pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
+               return;
+       }
+
+       for (const_iterator it = begin(), et = end(); it != et; ++it) {
+               (*it)->draw(pi, x, y);
+               x += (*it)->width();
        }
-       bf_.pop_back();
 }
 
 
-MathArray::const_iterator MathArray::begin() const
+Dimension const & MathArray::metricsT(TextMetricsInfo const & mi) const
 {
-       return bf_.begin();
+       //if (clean_)
+       //      return;
+       dim_.clear();
+       for (const_iterator it = begin(); it != end(); ++it) {
+               (*it)->metricsT(mi);
+               dim_ += (*it)->dimensions();
+       }
+       return dim_;
 }
 
 
-MathArray::const_iterator MathArray::end() const
+void MathArray::drawT(TextPainter & pain, int x, int y) const
 {
-       return bf_.end();
+       //if (drawn_ && x == xo_ && y == yo_)
+       //      return;
+       //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();
+       }
 }
 
 
-MathArray::iterator MathArray::begin()
+int MathArray::pos2x(size_type pos) const
 {
-       return bf_.begin();
+       return pos2x(0, pos, 0);
 }
 
 
-MathArray::iterator MathArray::end()
+int MathArray::pos2x(size_type pos1, size_type pos2, int glue) const
 {
-       return bf_.end();
+       int x = 0;
+       size_type target = min(pos2, size());
+       for (size_type i = pos1; i < target; ++i) {
+               const_iterator it = begin() + i;
+               if ((*it)->getChar() == ' ')
+                       x += glue;
+               x += (*it)->width();
+       }
+       return x;
 }
 
 
-bool MathArray::match(MathArray const & ar) const
+MathArray::size_type MathArray::x2pos(int targetx) const
 {
-       if (size() != ar.size())
-               return false;
-       for (const_iterator it = begin(), jt = ar.begin(); it != end(); ++it, ++jt)
-               if (!it->nucleus()->match(jt->nucleus()))
-                       return false;
-       return true;
+       return x2pos(0, targetx, 0);
 }
 
 
-void MathArray::replace(ReplaceData & rep)
+MathArray::size_type MathArray::x2pos(size_type startpos, 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() + startpos;
+       int lastx = 0;
+       int currx = 0;
+       for (; currx < targetx && it < end(); ++it) {
+               lastx = currx;
+               if ((*it)->getChar() == ' ')
+                       currx += glue;
+               currx += (*it)->width();
+       }
+       if (abs(lastx - targetx) < abs(currx - targetx) && it != begin() + startpos)
+               --it;
+       return it - begin();
+}
+
+
+int MathArray::dist(int x, int y) const
+{
+       int xx = 0;
+       int yy = 0;
+
+       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::boundingBox(int & x1, int & x2, int & y1, int & y2)
+{
+       x1 = xo_;
+       x2 = xo_ + width();
+       y1 = yo_ - ascent();
+       y2 = yo_ + descent();
+}
+
+void MathArray::center(int & x, int & y) const
+{
+       x = xo_ + width() / 2;
+       y = yo_ + (descent() - ascent()) / 2;
+}
+
+
+void MathArray::towards(int & x, int & y) const
+{
+       int cx = 0;
+       int cy = 0;
+       center(cx, cy);
+
+       double r = 1.0;
+       //int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
+
+       x = cx + int(r * (x - cx));
+       y = cy + int(r * (y - cy));
+}
+
+
+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);
                }
        }
 
-       for (const_iterator it = begin(); it != end(); ++it)
-               it->nucleus()->replace(rep);
 }