]> git.lyx.org Git - features.git/commitdiff
math-extern: enable handling of simple sums
authorAndré Pönitz <poenitz@gmx.net>
Mon, 12 Nov 2001 14:37:43 +0000 (14:37 +0000)
committerAndré Pönitz <poenitz@gmx.net>
Mon, 12 Nov 2001 14:37:43 +0000 (14:37 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@3009 a592a061-630c-0410-9148-cb99ea01b6c8

src/mathed/math_exintinset.C
src/mathed/math_exintinset.h
src/mathed/math_extern.C

index b7de3206c6908968eb611c800e7bc39bbd292d6d..7bb6e8990472435a2a2b83b291d882a961882ca1 100644 (file)
@@ -5,8 +5,8 @@
 #include "math_symbolinset.h"
 
 
-MathExIntInset::MathExIntInset()
-       : int_(new MathSymbolInset("int"))
+MathExIntInset::MathExIntInset(string const & name)
+       : symbol_(name)
 {}
 
 
@@ -16,9 +16,9 @@ MathInset * MathExIntInset::clone() const
 }
 
 
-void MathExIntInset::differential(MathArray const & ar)
+void MathExIntInset::index(MathArray const & ar)
 {
-       diff_ = ar;
+       index_ = ar;
 }
 
 
@@ -34,9 +34,15 @@ void MathExIntInset::scripts(MathAtom const & at)
 }
 
 
-void MathExIntInset::symbol(MathAtom const & at)
+MathAtom & MathExIntInset::scripts()
 {
-       int_ = at;
+       return scripts_;
+}
+
+
+void MathExIntInset::symbol(string const & symbol)
+{
+       symbol_ = symbol;
 }
 
 
@@ -49,12 +55,12 @@ bool MathExIntInset::hasScripts() const
 
 void MathExIntInset::normalize(NormalStream & os) const
 {
-       os << "[int ";
+       os << '[' << symbol_.c_str() << ' ';
        if (hasScripts())
                os << scripts_.nucleus();
        else 
                os << "{}";
-       os << ' ' << core_ << ' ' << diff_ << ']';
+       os << ' ' << core_ << ' ' << index_ << ']';
 }
 
 
@@ -72,12 +78,12 @@ void MathExIntInset::draw(Painter &, int, int) const
 
 void MathExIntInset::maplize(MapleStream & os) const
 {
-       os << int_.nucleus() << '(';
+       os << symbol_.c_str() << '(';
        if (core_.size())
                os << core_;
        else 
                os << '1';
-       os << ',' << diff_;
+       os << ',' << index_;
        if (hasScripts()) {
                MathScriptInset * p = scripts_->asScriptInset();
                os << '=' << p->down().data_ << ".." << p->up().data_;
@@ -88,20 +94,20 @@ void MathExIntInset::maplize(MapleStream & os) const
 
 void MathExIntInset::mathmlize(MathMLStream & os) const
 {
+       MathSymbolInset * sym = new MathSymbolInset(symbol_.c_str());
        if (hasScripts())
-               scripts_->asScriptInset()->mathmlize(int_.nucleus(), os);
+               scripts_->asScriptInset()->mathmlize(sym, os);
        else 
-               int_->mathmlize(os);
+               sym->mathmlize(os);
+       delete sym;
        os << core_ << "<mo> &InvisibleTimes; </mo>"
           << MTag("mrow") << "<mo> &DifferentialD; </mo>"
-          << diff_ << ETag("mrow");
+          << index_ << ETag("mrow");
 }
 
 
-void MathExIntInset::write(WriteStream & os) const
+void MathExIntInset::write(WriteStream &) const
 {
-       if (hasScripts())
-               os << scripts_.nucleus();
-       os << core_ << "d" << diff_;
+       lyxerr << "should not happen\n";
 }
 
index 8e14e1c7a9b5f8a56a3f11d8ce434167e342fff2..5e7f51e057f03a9109845da4bee585334db53f31 100644 (file)
 class MathExIntInset : public MathInset {
 public:
        ///
-       MathExIntInset();
+       explicit MathExIntInset(string const & name_);
        ///
        MathInset * clone() const;
        ///
-       void differential(MathArray const &);
+       void index(MathArray const &);
        ///
        void core(MathArray const &);
        ///
        void scripts(MathAtom const &);
        ///
-       void symbol(MathAtom const &);
+       MathAtom & scripts();
        ///
-       bool hasScripts() const;
+       void symbol(string const &);
        ///
        void metrics(MathMetricsInfo const & st) const;
        ///
@@ -38,12 +38,16 @@ public:
        void write(WriteStream & os) const;
 private:
        ///
-       MathAtom int_;
+       bool hasScripts() const;
+
+       ///
+       string symbol_;
        ///
        MathAtom scripts_;
        ///
        MathArray core_;
        ///
-       MathArray diff_;
+       MathArray index_;
 };
+
 #endif
index 64eb18b3f3d04947df988ed0f3b186d0fc884530..e3064c107ee41ff4ef0deab3772d01e9d2f95117 100644 (file)
@@ -3,6 +3,7 @@
 // information" from the unstructered layout-oriented stuff in an
 // MathArray.
 
+#include <algorithm>
 
 #include "math_charinset.h"
 #include "math_deliminset.h"
@@ -111,9 +112,10 @@ string extractString(MathInset * p)
 // define a function for tests
 typedef bool TestItemFunc(MathInset *);
 
-// define a function for replacing pa
+// define a function for replacing subexpressions
 typedef MathInset * ReplaceArgumentFunc(const MathArray & ar);
 
+
 // search end of nested sequence
 MathArray::iterator endNestSearch(
        MathArray::iterator it,
@@ -293,7 +295,7 @@ void extractIntegrals(MathArray & ar)
                        continue;
 
                // create a proper inset as replacement
-               MathExIntInset * p = new MathExIntInset;
+               MathExIntInset * p = new MathExIntInset("int");
 
                // collect scripts
                MathArray::iterator st = it + 1;
@@ -305,26 +307,109 @@ void extractIntegrals(MathArray & ar)
                }
 
                // use the atom behind the 'd' as differential
-               MathArray diff;
+               MathArray ind;
                if (jt + 1 != ar.end()) {
-                       diff.push_back(*(jt + 1));
-                       ar.erase(it + 1, jt + 2);
-               } else {
-                       ar.erase(it + 1, jt + 1);
+                       ind.push_back(*(jt + 1));
+                       ++jt;
                }
-               p->differential(diff);
+               ar.erase(it + 1, jt + 1);
+
+               p->index(ind);
                (*it).reset(p);
        }
        lyxerr << "\nIntegrals to: " << ar << "\n";
 }
 
 
+//
+// search sums
+//
+
+bool sumSymbolTest(MathInset * p)
+{
+       return p->asSymbolInset() && p->asSymbolInset()->name() == "sum";
+}
+
+
+bool equalSign(MathInset * p)
+{
+       return extractString(p) == "=";
+}
+
+
+bool equalSign1(MathAtom const & at)
+{
+       return equalSign(at.nucleus());
+}
+
+
+
+// replace '\sum' ['_^'] f(x) sequences by a real MathExIntInset
+// assume 'extractDelims' ran before
+void extractSums(MathArray & ar)
+{
+       // we need at least two items...
+       if (ar.size() <= 1)
+               return;
+
+       lyxerr << "\nSums from: " << ar << "\n";
+       for (MathArray::size_type i = 0; i < ar.size() - 1; ++i) {
+               MathArray::iterator it = ar.begin() + i;
+
+               // is this a sum name?
+               if (!sumSymbolTest(it->nucleus()))
+                       continue;
+
+               // create a proper inset as replacement
+               MathExIntInset * p = new MathExIntInset("sum");
+
+               // collect scripts
+               MathArray::iterator st = it + 1;
+               if (st != ar.end() && (*st)->asScriptInset()) {
+                       p->scripts(*st);
+                       ++st;
+
+                       // try to figure out the summation index from the subscript
+                       MathScriptInset * script = p->scripts()->asScriptInset();
+                       if (script->hasDown()) {
+                               MathArray & ar = script->down().data_;
+                               MathArray::iterator it =
+                                       std::find_if(ar.begin(), ar.end(), &equalSign1);
+                               if (it != ar.end()) {
+                                       // we found a '=', use everything in front of that as index,
+                                       // and everything behind as start value
+                                       p->index(MathArray(ar.begin(), it));
+                                       ar.erase(ar.begin(), it + 1);
+                               } else {
+                                       // use everything as summation index
+                                       p->index(ar);
+                                       p->scripts().reset(0);
+                               }
+                       }
+               }
+
+               // use the atom behind the script as core
+               MathArray ind;
+               if (st != ar.end()) {
+                       MathArray core;
+                       core.push_back(*st);
+                       p->core(core);
+                       ++st;
+               }
+               ar.erase(it + 1, st);
+               (*it).reset(p);
+       }
+       lyxerr << "\nSums to: " << ar << "\n";
+}
+
+
 void extractStructure(MathArray & ar)
 {
        extractMatrices(ar);
        extractDelims(ar);
        extractFunctions(ar);
        extractIntegrals(ar);
+       extractSums(ar);
        extractStrings(ar);
 }