]> 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 6842bb005192661d153efd2333ae3412fccbb19e..ffeee12e3a624518a5976db0933355b45f342996 100644 (file)
@@ -6,6 +6,7 @@
 
 #include "xarray.h"
 #include "math_inset.h"
+#include "math_scriptinset.h"
 #include "mathed/support.h"
 #include "math_defs.h"
 #include "Painter.h"
@@ -17,24 +18,33 @@ MathXArray::MathXArray()
 {}
 
 
-void MathXArray::metrics(MathStyles st) const
+void MathXArray::metrics(MathMetricsInfo const & st) const
 {
-       style_   = st;
-       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;
 
-       math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, ascent_, descent_); 
+       math_font_max_dim(LM_TC_TEXTRM, style_, ascent_, descent_);     
        width_   = 0;
 
        //lyxerr << "MathXArray::metrics(): '" << data_ << "'\n";
-       for (size_type pos = 0; pos < data_.size(); ++pos) {
-               MathAtom const * p = data_.at(pos);
-               p->metrics(st);
-               ascent_  = std::max(ascent_,  p->ascent());
-               descent_ = std::max(descent_, p->descent());
-               width_  += p->width();
+       
+       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 {
+                       p->metrics(st);
+                       ascent_  = std::max(ascent_,  p->ascent());
+                       descent_ = std::max(descent_, p->descent());
+                       width_  += p->width();  
+               }
        }
        //lyxerr << "MathXArray::metrics(): '" << ascent_ << " " 
        //      << descent_ << " " << width_ << "'\n";
@@ -51,10 +61,16 @@ void MathXArray::draw(Painter & pain, int x, int y) const
                return;
        }
 
-       for (size_type pos = 0; pos < data_.size(); ++pos) {
-               MathAtom const * p = data_.at(pos);
-               p->draw(pain, x, y);
-               x += p->width();
+       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();
+               }
        }
 }
 
@@ -62,32 +78,43 @@ void MathXArray::draw(Painter & pain, int x, int y) const
 int MathXArray::pos2x(size_type targetpos) const
 {
        int x = 0;
-       targetpos = std::min(targetpos, data_.size());
-       for (size_type pos = 0; pos < targetpos; ++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;
 }
 
 
 MathArray::size_type MathXArray::x2pos(int targetx) const
 {
-       size_type pos  = 0;
-       int lastx      = 0;
-       int currx      = 0;
-       for ( ; currx < targetx && pos < data_.size(); ++pos) {
+       const_iterator it = begin();
+       int lastx = 0;
+       int currx = 0;
+       for ( ; currx < targetx && it < end(); ++it) {
                lastx = currx;
-               currx += width(pos);
-       }
-       if (abs(lastx - targetx) < abs(currx - targetx) && pos > 0)
-               --pos;
-       return pos;
-}
 
+               int wid = 0;
+               MathInset const * p = it->nucleus();
+               if (MathScriptInset const * q = data_.asScript(it)) {
+                       wid = q->width(p);
+                       ++it;
+               } else
+                       wid = p->width();
 
-int MathXArray::width(size_type pos) const
-{
-       MathAtom const * t = data_.at(pos);
-       return t ? t->width() : 0;
+               currx += wid;
+       }
+       if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
+               --it;
+       return it - begin();
 }