]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_scriptinset.C
make \newcommand{\bb}[1]{\mathbf{#1}} work for read/write/display.
[lyx.git] / src / mathed / math_scriptinset.C
index 073593a48b694003e66266a9dc305d8a995d19f6..5c5aa5c7e3c79acc4379e31763da7e77b5df7bcb 100644 (file)
 #endif
 
 #include "math_scriptinset.h"
-#include "support/LOstream.h"
+#include "math_support.h"
+#include "math_symbolinset.h"
+#include "math_mathmlstream.h"
+#include "support/LAssert.h"
+#include "debug.h"
 
 
 MathScriptInset::MathScriptInset()
-       : MathNestInset(2), up_(false), down_(false), limits_(0), symbol_(0)
-{}
+       : MathNestInset(2), limits_(0)
+{
+       script_[0] = false;
+       script_[1] = false;
+}
 
 
-MathScriptInset::MathScriptInset(bool up, bool down, MathInset * symbol)
-       : MathNestInset(2), up_(up), down_(down), limits_(0), symbol_(symbol)
-{}
+MathScriptInset::MathScriptInset(bool up)
+       : MathNestInset(2), limits_(0)
+{
+       script_[0] = !up;
+       script_[1] = up;
+}
 
 
-MathScriptInset::MathScriptInset(MathScriptInset const & p)
-       : MathNestInset(p), up_(p.up_), down_(p.down_),
-               limits_(p.limits_), symbol_(p.symbol_ ? p.symbol_->clone() : 0)
-{}
+MathInset * MathScriptInset::clone() const
+{
+       return new MathScriptInset(*this);
+}
 
 
-MathScriptInset::~MathScriptInset()
+MathScriptInset const * MathScriptInset::asScriptInset() const
 {
-       delete symbol_;
+       return this;
 }
 
 
-MathInset * MathScriptInset::clone() const
-{   
-       return new MathScriptInset(*this);
+MathScriptInset * MathScriptInset::asScriptInset()
+{
+       return this;
 }
 
 
-bool MathScriptInset::up() const
+MathXArray const & MathScriptInset::up() const
 {
-       return up_;
+       return xcell(1);
 }
 
 
-bool MathScriptInset::down() const
+MathXArray const & MathScriptInset::down() const
 {
-       return down_;
+       return xcell(0);
 }
 
 
-void MathScriptInset::up(bool b)
+MathXArray & MathScriptInset::up()
 {
-       up_ = b;
+       return xcell(1);
 }
 
 
-void MathScriptInset::down(bool b)
+MathXArray & MathScriptInset::down()
 {
-       down_ = b;
+       return xcell(0);
 }
 
 
-bool MathScriptInset::idxRight(int &, int &) const
+void MathScriptInset::ensure(bool up)
 {
-       return false;
+       script_[up] = true;
 }
 
 
-bool MathScriptInset::idxLeft(int &, int &) const
+int MathScriptInset::dy0(MathInset const * nuc) const
 {
-       return false;
+       int nd = ndes(nuc);
+       if (!hasDown())
+               return nd;
+       int des = down().ascent();
+       if (hasLimits(nuc))
+               des += nd + 2;
+       else 
+               des = std::max(des, nd);
+       return des;
 }
 
 
-bool MathScriptInset::idxUp(int & idx, int & pos) const
+int MathScriptInset::dy1(MathInset const * nuc) const
 {
-       if (idx == 0 || !up()) 
-               return false;
-       idx = 0;
-       pos = 0;
-       return true;
+       int na = nasc(nuc);
+       if (!hasUp())
+               return na;
+       int asc = up().descent();
+       if (hasLimits(nuc))
+               asc += na + 2;
+       else 
+               asc = std::max(asc, na);
+       asc = std::max(asc, mathed_char_ascent(LM_TC_VAR, mi_, 'I'));
+       return asc;
 }
 
-bool MathScriptInset::idxDown(int & idx, int & pos) const
+
+int MathScriptInset::dx0(MathInset const * nuc) const
 {
-       if (idx == 1 || !down()) 
-               return false;
-       idx = 1;
-       pos = 0;
-       return true;
+       lyx::Assert(hasDown());
+       return hasLimits(nuc) ? (width2(nuc) - down().width()) / 2 : nwid(nuc);
 }
 
-bool MathScriptInset::idxFirst(int & idx, int & pos) const
+
+int MathScriptInset::dx1(MathInset const * nuc) const
 {
-       idx = up() ? 0 : 1;
-       pos = 0;
-       return true;
+       lyx::Assert(hasUp());
+       return hasLimits(nuc) ? (width2(nuc) - up().width()) / 2 : nwid(nuc);
 }
 
-bool MathScriptInset::idxLast(int & idx, int & pos) const
+
+int MathScriptInset::dxx(MathInset const * nuc) const
 {
-       idx = down() ? 1 : 0;
-       pos = cell(idx).size();
-       return true;
+       //lyx::Assert(nuc());
+       return hasLimits(nuc)  ?  (width2(nuc) - nwid(nuc)) / 2  :  0;
 }
 
 
-bool MathScriptInset::idxFirstUp(int & idx, int & pos) const
+int MathScriptInset::ascent2(MathInset const * nuc) const
 {
-       if (!up()) 
-               return false;
-       idx = 0;
-       pos = 0;
-       return true;
+       return dy1(nuc) + (hasUp() ? up().ascent() : 0);
 }
 
 
-bool MathScriptInset::idxFirstDown(int & idx, int & pos) const
+int MathScriptInset::descent2(MathInset const * nuc) const
 {
-       if (!down()) 
-               return false;
-       idx = 1;
-       pos = 0;
-       return true;
+       return dy0(nuc) + (hasDown() ? down().descent() : 0);
 }
 
 
-bool MathScriptInset::idxLastUp(int & idx, int & pos) const
+int MathScriptInset::width2(MathInset const * nuc) const
 {
-       if (!up()) 
-               return false;
-       idx = 0;
-       pos = cell(idx).size();
-       return true;
+       int wid = 0;
+       if (hasLimits(nuc)) {
+               wid = nwid(nuc);
+               if (hasUp())
+                       wid = std::max(wid, up().width());
+               if (hasDown())
+                       wid = std::max(wid, down().width());
+       } else {
+               if (hasUp())
+                       wid = std::max(wid, up().width());
+               if (hasDown())
+                       wid = std::max(wid, down().width());
+               wid += nwid(nuc);
+       }
+       return wid;
 }
 
 
-bool MathScriptInset::idxLastDown(int & idx, int & pos) const
+int MathScriptInset::nwid(MathInset const * nuc) const
 {
-       if (!down()) 
-               return false;
-       idx = 1;
-       pos = cell(idx).size();
-       return true;
+       return nuc ?
+               nuc->width() :
+               mathed_char_width(LM_TC_TEX, mi_, '.');
 }
 
 
-void MathScriptInset::write(std::ostream & os, bool fragile) const
+int MathScriptInset::nasc(MathInset const * nuc) const
 {
-       if (symbol_) {
-               symbol_->write(os, fragile);
-               if (limits())
-                       os << (limits() == 1 ? "\\limits" : "\\nolimits");
-       }
-       if (up()) {
-               os << "^{";
-               cell(0).write(os, fragile);
-               os << "}";
-       }
-       if (down()) {
-               os << "_{";
-               cell(1).write(os, fragile);
-               os << "}";
-       }
-       os << " ";
+       return nuc ? nuc->ascent()
+               : mathed_char_ascent(LM_TC_VAR, mi_, 'I');
 }
 
 
-void MathScriptInset::idxDelete(int & idx, bool & popit, bool & deleteit)
+int MathScriptInset::ndes(MathInset const * nuc) const
 {
-       if (idx == 0) 
-               up(false);
-       else
-               down(false);
-       popit = true;
-       deleteit = !(up() || down());
+       return nuc ? nuc->descent()
+               : mathed_char_descent(LM_TC_VAR, mi_, 'I');
 }
 
 
-int MathScriptInset::limits() const
+void MathScriptInset::metrics(MathMetricsInfo const & mi) const
+{      
+       metrics(0, mi);
+}
+
+
+void MathScriptInset::metrics(MathInset const * nuc,
+       MathMetricsInfo const & mi) const
+{      
+       MathNestInset::metrics(mi);
+       if (nuc)
+               nuc->metrics(mi);
+       ascent_  = ascent2(nuc);
+       descent_ = descent2(nuc);
+       width_   = width2(nuc);
+}
+
+
+void MathScriptInset::draw(Painter & pain, int x, int y) const
 {  
-       return limits_;
+       //lyxerr << "unexpected call to MathScriptInset::draw()\n";
+       draw(0, pain, x, y);
 }
 
 
-void MathScriptInset::limits(int limits) 
+void MathScriptInset::draw(MathInset const * nuc, Painter & pain,
+       int x, int y) const
 {  
-       limits_ = limits;
+       if (nuc)
+               nuc->draw(pain, x + dxx(nuc), y);
+       else
+               drawStr(pain, LM_TC_TEX, mi_, x + dxx(nuc), y, ".");
+
+       if (hasUp())
+               up().draw(pain, x + dx1(nuc), y - dy1(nuc));
+
+       if (hasDown())
+               down().draw(pain, x + dx0(nuc), y + dy0(nuc));
 }
 
 
-bool MathScriptInset::hasLimits() const
+bool MathScriptInset::hasLimits(MathInset const * nuc) const
 {
-       return
-               symbol_ && (limits_ == 1 || (limits_ == 0 && size() == LM_ST_DISPLAY));
+       // obviuos cases
+       if (limits_ == 1)
+               return true;
+       if (limits_ == -1)
+               return false;
+
+       // we can only display limits if the nucleus wants some 
+       if (!nuc)
+               return false;
+       if (!nuc->isScriptable())
+               return false;
+       
+       // per default \int has limits beside the \int even in displayed formulas
+       if (nuc->asSymbolInset())
+               if (nuc->asSymbolInset()->name().find("int") != string::npos)
+                       return false;
+
+       // assume "real" limits for everything else
+       return true;
 }
 
 
-void MathScriptInset::writeNormal(std::ostream & os) const
+void MathScriptInset::removeEmptyScripts()
 {
-       if (limits() && symbol_) 
-               os << "[" << (limits() ? "limits" : "nolimits") << "]";
-       if (up()) {
-               os << "[superscript ";
-               cell(0).writeNormal(os);
-               os << "] ";
-       }
-       if (down()) {
-               os << "[subscript ";
-               cell(1).writeNormal(os);
-               os << "] ";
-       }
+       for (int i = 0; i <= 1; ++i)
+               if (script_[i] && !cell(i).size())
+                       script_[i] = false;
 }
 
 
-void MathScriptInset::metrics(MathStyles st) const
+void MathScriptInset::removeScript(bool up)
 {
-       size_ = st;
-       MathStyles tt = smallerStyleScript(st);
-       
-       xcell(0).metrics(tt);
-       xcell(1).metrics(tt);
-
-       width_   = std::max(xcell(0).width(), xcell(1).width());
-
-       if (hasLimits()) {
-               symbol_->metrics(st);
-               int wid  = symbol_->width();
-               ascent_  = symbol_->ascent();
-               descent_ = symbol_->descent();
-               width_   = std::max(width_, wid);
-               if (up()) {
-                       ascent_  += xcell(0).height() + 2;
-                       dy0_     = - (ascent_ - xcell(0).ascent());
-               }
-               if (down()) {
-                       descent_ += xcell(1).height() + 2;
-                       dy1_     = descent_ - xcell(1).descent();
-               }
-               dxx_   = (width_ - wid) / 2;
-               dx0_   = (width_ - xcell(0).width()) / 2;
-               dx1_   = (width_ - xcell(1).width()) / 2;
-       } else {
-               int asc;
-               int des;
-               int wid = 0;
-               mathed_char_height(LM_TC_VAR, st, 'I', asc, des);
-               if (symbol_) {
-                       symbol_->metrics(st);
-                       wid  = symbol_->width();
-                       asc  = symbol_->ascent();
-                       des = symbol_->descent();
+       cell(up).clear();
+       script_[up] = false;
+}
+
+
+bool MathScriptInset::has(bool up) const
+{
+       return script_[up];
+}
+
+
+bool MathScriptInset::hasUp() const
+{
+       return script_[1];
+}
+
+
+bool MathScriptInset::hasDown() const
+{
+       return script_[0];
+}
+
+
+bool MathScriptInset::idxRight(MathInset::idx_type &,
+                                MathInset::pos_type &) const
+{
+       return false;
+}
+
+
+bool MathScriptInset::idxLeft(MathInset::idx_type &,
+                               MathInset::pos_type &) const
+{
+       return false;
+}
+
+
+void MathScriptInset::write(WriteStream & os) const
+{  
+       //lyxerr << "unexpected call to MathScriptInset::write()\n";
+       write2(0, os);
+}
+
+
+void MathScriptInset::write2(MathInset const * nuc, WriteStream & os) const
+{
+       if (nuc) {
+               os << nuc;
+               if (nuc->takesLimits()) {
+                       if (limits_ == -1)
+                               os << "\\nolimits ";
+                       if (limits_ == 1)
+                               os << "\\limits ";
                }
-               ascent_  = up()   ? xcell(0).height() + asc : 0;
-               descent_ = down() ? xcell(1).height() + des : 0;
-               width_  += wid;
-               dy0_     = - asc - xcell(0).descent();
-               dy1_     =   des + xcell(1).ascent();
-               dx0_     = wid;
-               dx1_     = wid;
-               dxx_     = 0;
-       }
+       } else
+                       if (os.firstitem())
+                               lyxerr << "suppressing {} \n";
+                       else
+                               os << "{}";
+
+       if (hasDown() && down().data_.size())
+               os << "_{" << down().data_ << '}';
+
+       if (hasUp() && up().data_.size())
+               os << "^{" << up().data_ << '}';
 }
 
 
-void MathScriptInset::draw(Painter & pain, int x, int y) const
+void MathScriptInset::normalize(NormalStream & os) const
 {  
-       xo(x);
-       yo(y);
+       //lyxerr << "unexpected call to MathScriptInset::normalize()\n";
+       normalize2(0, os);
+}
+
+
+void MathScriptInset::normalize2(MathInset const * nuc, NormalStream & os) const
+{
+       bool d = hasDown() && down().data_.size();
+       bool u = hasUp() && up().data_.size();
+
+       if (u) 
+               os << "[sup ";
+       if (d)
+               os << "[sub ";
+       
+       if (nuc)
+               os << nuc << ' ';
+       else
+               os << "[par]";
+
+       if (d)
+               os << down().data_ << ']';
+       if (u) 
+               os << up().data_ << ']';
+}
+
+
+void MathScriptInset::maplize2(MathInset const * nuc, MapleStream & os) const
+{
+       if (nuc)
+               os << nuc;
+       if (hasDown() && down().data_.size())
+               os << '[' << down().data_ << ']';
+       if (hasUp() && up().data_.size())
+               os << "^(" << up().data_ << ')';
+}
+
+
+void MathScriptInset::mathmlize2(MathInset const * nuc, MathMLStream & os) const
+{
+       bool d = hasDown() && down().data_.size();
+       bool u = hasUp() && up().data_.size();
+
+       if (u && d)
+               os << MTag("msubsup");
+       else if (u)
+               os << MTag("msup");
+       else if (d)
+               os << MTag("msub");
+
+       if (nuc)
+               os << nuc;
+       else
+               os << "<mrow/>";
+
+       if (u && d)
+               os << down().data_ << up().data_ << ETag("msubsup");
+       else if (u)
+               os << up().data_ << ETag("msup");
+       else if (d)
+               os << down().data_ << ETag("msub");
+}
+
 
-       if (symbol_)
-               symbol_->draw(pain, x + dxx_, y);
-       if (up())
-               xcell(0).draw(pain, x + dx0_, y + dy0_);
-       if (down())
-               xcell(1).draw(pain, x + dx1_, y + dy1_);
+void MathScriptInset::octavize2(MathInset const * nuc, OctaveStream & os) const
+{
+       if (nuc)
+               os << nuc;
+       if (hasDown() && down().data_.size())
+               os << '[' << down().data_ << ']';
+       if (hasUp() && up().data_.size())
+               os << "^(" << up().data_ << ')';
 }
+
+