]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/xarray.C
use stream-like syntax for LaTeX output
[lyx.git] / src / mathed / xarray.C
index 269bfafefaa8c33474a020fab0c3cf1a72566bbd..ffeee12e3a624518a5976db0933355b45f342996 100644 (file)
@@ -6,12 +6,11 @@
 
 #include "xarray.h"
 #include "math_inset.h"
+#include "math_scriptinset.h"
 #include "mathed/support.h"
 #include "math_defs.h"
 #include "Painter.h"
-
-using std::max;
-using std::min;
+#include "debug.h"
 
 
 MathXArray::MathXArray()
@@ -19,45 +18,40 @@ MathXArray::MathXArray()
 {}
 
 
-void MathXArray::Metrics(MathStyles st, int, int)
+void MathXArray::metrics(MathMetricsInfo const & st) const
 {
-       if (data_.empty()) {
-               mathed_char_dim(LM_TC_VAR, st, 'I', ascent_, descent_, width_); 
+       style_ = st.size;
+       mathed_char_dim(LM_TC_VAR, style_, 'I', ascent_, descent_, width_);
+
+       if (data_.empty()) 
                return;
-       }
-       
-       ascent_  = 0;
-       descent_ = 0;
+
+       math_font_max_dim(LM_TC_TEXTRM, style_, ascent_, descent_);     
        width_   = 0;
-       style_    = st;
-
-       // keep last values for scriptInset's need to look back
-       int asc = 0;
-       int des = 0;
-       int wid = 0;
-       mathed_char_height(LM_TC_VAR, st, 'I', asc, des);
-
-       for (int pos = 0; pos < data_.size(); data_.next(pos)) {
-               MathInset * p = data_.nextInset(pos);
-               if (p) {
-                       // only MathUpDownInsets will use the asc/des information...
-                       p->Metrics(st, asc, des);
-                       asc = p->ascent();
-                       des = p->descent();
-                       wid = p->width();
+
+       //lyxerr << "MathXArray::metrics(): '" << data_ << "'\n";
+       
+       for (const_iterator it = begin(); it != end(); ++it) {
+               MathInset const * p = it->nucleus();
+               if (MathScriptInset const * q = data_.asScript(it)) {
+                       q->metrics(p, st);
+                       ascent_  = std::max(ascent_,  q->ascent(p));
+                       descent_ = std::max(descent_, q->descent(p));
+                       width_  += q->width(p); 
+                       ++it;
                } else {
-                       char cx = data_.GetChar(pos); 
-                       MathTextCodes fc = data_.GetCode(pos); 
-                       mathed_char_dim(fc, style_, cx, asc, des, wid);
+                       p->metrics(st);
+                       ascent_  = std::max(ascent_,  p->ascent());
+                       descent_ = std::max(descent_, p->descent());
+                       width_  += p->width();  
                }
-               ascent_  = max(ascent_, asc);
-               descent_ = max(descent_, des);
-               width_   += wid;
        }
+       //lyxerr << "MathXArray::metrics(): '" << ascent_ << " " 
+       //      << descent_ << " " << width_ << "'\n";
 }
 
 
-void MathXArray::draw(Painter & pain, int x, int y)
+void MathXArray::draw(Painter & pain, int x, int y) const
 {
        xo_ = x;
        yo_ = y;
@@ -67,62 +61,65 @@ void MathXArray::draw(Painter & pain, int x, int y)
                return;
        }
 
-       for (int pos = 0; pos < data_.size(); data_.next(pos)) {
-               MathInset * p = data_.nextInset(pos);
-               if (p) {
+       for (const_iterator it = begin(); it != end(); ++it) {
+               MathInset const * p = it->nucleus();
+               if (MathScriptInset const * q = data_.asScript(it)) {
+                       q->draw(p, pain, x, y);
+                       x += q->width(p);
+                       ++it;
+               } else {
                        p->draw(pain, x, y);
                        x += p->width();
-               } else {
-                       char cx = data_.GetChar(pos);
-                       MathTextCodes fc = data_.GetCode(pos);
-                       string s;
-                       s += cx;
-                       drawStr(pain, fc, style_, x, y, s);
-                       x += mathed_char_width(fc, style_, cx);
                }
        }
 }
 
 
-int MathXArray::pos2x(int targetpos) const
+int MathXArray::pos2x(size_type targetpos) const
 {
        int x = 0;
-       targetpos = min(targetpos, data_.size());
-       for (int pos = 0; pos < targetpos; data_.next(pos)) 
-               x += width(pos);
+       const_iterator target = std::min(begin() + targetpos, end());
+       for (const_iterator it = begin(); it < target; ++it) {
+               MathInset const * p = it->nucleus();
+               if (MathScriptInset const * q = data_.asScript(it)) {
+                       ++it;
+                       if (it < target)
+                               x += q->width(p);
+                       else  // "half" position
+                               x += q->dxx(p) + q->nwid(p);
+               } else
+                       x += p->width();
+       }
        return x;
 }
 
 
-int MathXArray::x2pos(int targetx) const
+MathArray::size_type MathXArray::x2pos(int targetx) const
 {
-       int pos   = 0;
+       const_iterator it = begin();
        int lastx = 0;
        int currx = 0;
-       while (currx < targetx && pos < data_.size()) {
+       for ( ; currx < targetx && it < end(); ++it) {
                lastx = currx;
-               currx += width(pos);
-               data_.next(pos);
-       }
-       if (abs(lastx - targetx) < abs(currx - targetx))
-               data_.prev(pos);
-       return pos;
-}
 
-int MathXArray::width(int pos) const
-{
-       if (pos >= data_.size())
-               return 0;
+               int wid = 0;
+               MathInset const * p = it->nucleus();
+               if (MathScriptInset const * q = data_.asScript(it)) {
+                       wid = q->width(p);
+                       ++it;
+               } else
+                       wid = p->width();
 
-       if (data_.isInset(pos)) 
-               return data_.nextInset(pos)->width();
-       else 
-               return mathed_char_width(data_.GetCode(pos), style_, data_.GetChar(pos));
+               currx += wid;
+       }
+       if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
+               --it;
+       return it - begin();
 }
 
+
 std::ostream & operator<<(std::ostream & os, MathXArray const & ar)
 {
        os << ar.data_;
        return os;
 }
-