]> git.lyx.org Git - features.git/commitdiff
Change cell numbers so that 0 is the main cell
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Tue, 6 Nov 2018 07:29:47 +0000 (21:29 -1000)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Thu, 18 Jun 2020 12:39:51 +0000 (14:39 +0200)
This leads to code simplification in overset, root and underset.
Further simplification is possible.

src/mathed/InsetMathOverset.cpp
src/mathed/InsetMathOverset.h
src/mathed/InsetMathRoot.cpp
src/mathed/InsetMathRoot.h
src/mathed/InsetMathStackrel.cpp
src/mathed/InsetMathStackrel.h
src/mathed/InsetMathUnderset.cpp
src/mathed/InsetMathUnderset.h

index 1295685d782567dafc609e767650ca963a0a2ed2..b659a9eab4f2189dbf3c836a16deba1513e34239 100644 (file)
@@ -19,6 +19,8 @@
 #include "LaTeXFeatures.h"
 #include "MetricsInfo.h"
 
+#include "support/lassert.h"
+
 using namespace std;
 
 namespace lyx {
@@ -32,14 +34,14 @@ Inset * InsetMathOverset::clone() const
 void InsetMathOverset::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        Changer dummy2 = mi.base.changeEnsureMath();
-       Dimension dim1;
-       cell(1).metrics(mi, dim1);
-       Changer dummy = mi.base.changeFrac();
        Dimension dim0;
        cell(0).metrics(mi, dim0);
-       dim.wid = max(dim0.width(), dim1.wid) + 4;
-       dim.asc = dim1.asc + dim0.height() + 4;
-       dim.des = dim1.des;
+       Changer dummy = mi.base.changeFrac();
+       Dimension dim1;
+       cell(1).metrics(mi, dim1);
+       dim.wid = max(dim1.width(), dim0.wid) + 4;
+       dim.asc = dim0.asc + dim1.height() + 4;
+       dim.des = dim0.des;
 }
 
 
@@ -47,13 +49,24 @@ void InsetMathOverset::draw(PainterInfo & pi, int x, int y) const
 {
        Changer dummy2 = pi.base.changeEnsureMath();
        Dimension const dim = dimension(*pi.base.bv);
-       Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
        Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
+       Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
        int m  = x + dim.wid / 2;
-       int yo = y - dim1.asc - dim0.des - 1;
-       cell(1).draw(pi, m - dim1.wid / 2, y);
+       int yo = y - dim0.asc - dim1.des - 1;
+       cell(0).draw(pi, m - dim0.wid / 2, y);
        Changer dummy = pi.base.changeFrac();
-       cell(0).draw(pi, m - dim0.width() / 2, yo);
+       cell(1).draw(pi, m - dim1.width() / 2, yo);
+}
+
+
+bool InsetMathOverset::idxUpDown(Cursor & cur, bool up) const
+{
+       idx_type target = up; // up ? 1 : 0, since upper cell has idx 1
+       if (cur.idx() == target)
+               return false;
+       cur.idx() = target;
+       cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target());
+       return true;
 }
 
 
@@ -62,27 +75,27 @@ void InsetMathOverset::write(WriteStream & os) const
        MathEnsurer ensurer(os);
        if (os.fragile())
                os << "\\protect";
-       os << "\\overset{" << cell(0) << "}{" << cell(1) << '}';
+       os << "\\overset{" << cell(1) << "}{" << cell(0) << '}';
 }
 
 
 void InsetMathOverset::normalize(NormalStream & os) const
 {
-       os << "[overset " << cell(0) << ' ' << cell(1) << ']';
+       os << "[overset " << cell(1) << ' ' << cell(0) << ']';
 }
 
 
 void InsetMathOverset::mathmlize(MathStream & ms) const
 {
-       ms << "<mover accent='false'>" << cell(1) << cell(0) << "</mover>";
+       ms << "<mover accent='false'>" << cell(0) << cell(1) << "</mover>";
 }
 
 
 void InsetMathOverset::htmlize(HtmlStream & os) const
 {
        os << MTag("span", "class='overset'")
-                << MTag("span", "class='top'") << cell(0) << ETag("span")
-                << MTag("span") << cell(1) << ETag("span")
+                << MTag("span", "class='top'") << cell(1) << ETag("span")
+                << MTag("span") << cell(0) << ETag("span")
                 << ETag("span");
 }
 
index 1202bbcfd42c1d81796477cc5c4e79629c6893f1..eb6b86b7988f34fcb49b8c45eacca467497ae8e7 100644 (file)
@@ -28,9 +28,7 @@ public:
        ///
        void draw(PainterInfo & pi, int x, int y) const;
        ///
-       idx_type firstIdx() const { return 1; }
-       ///
-       idx_type lastIdx() const { return 1; }
+       bool idxUpDown(Cursor & cur, bool up) const;
        ///
        void write(WriteStream & os) const;
        ///
index 3cc8e2e1d7851b0186a03ae87f29e6ec8d58c025..b3723faca83f11f808b3cf1860d66be57e0d47db 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "frontends/Painter.h"
 
+#include "support/lassert.h"
 
 using namespace std;
 
@@ -84,7 +85,7 @@ void mathed_root_metrics(MetricsInfo & mi, MathData const & nucleus,
 
 void InsetMathRoot::metrics(MetricsInfo & mi, Dimension & dim) const
 {
-       mathed_root_metrics(mi, cell(1), &cell(0), dim);
+       mathed_root_metrics(mi, cell(0), &cell(1), dim);
 }
 
 
@@ -127,26 +128,26 @@ void mathed_draw_root(PainterInfo & pi, int x, int y, MathData const & nucleus,
 
 void InsetMathRoot::draw(PainterInfo & pi, int x, int y) const
 {
-       mathed_draw_root(pi, x, y, cell(1), &cell(0), dimension(*pi.base.bv));
+       mathed_draw_root(pi, x, y, cell(0), &cell(1), dimension(*pi.base.bv));
 }
 
 
 void InsetMathRoot::write(WriteStream & os) const
 {
        MathEnsurer ensurer(os);
-       os << "\\sqrt[" << cell(0) << "]{" << cell(1) << '}';
+       os << "\\sqrt[" << cell(1) << "]{" << cell(0) << '}';
 }
 
 
 void InsetMathRoot::normalize(NormalStream & os) const
 {
-       os << "[root " << cell(0) << ' ' << cell(1) << ']';
+       os << "[root " << cell(1) << ' ' << cell(0) << ']';
 }
 
 
 bool InsetMathRoot::idxUpDown(Cursor & cur, bool up) const
 {
-       Cursor::idx_type const target = up ? 0 : 1;
+       Cursor::idx_type const target = up; //up ? 1 : 0;
        if (cur.idx() == target)
                return false;
        cur.idx() = target;
@@ -157,34 +158,34 @@ bool InsetMathRoot::idxUpDown(Cursor & cur, bool up) const
 
 void InsetMathRoot::maple(MapleStream & os) const
 {
-       os << '(' << cell(1) << ")^(1/(" << cell(0) <<"))";
+       os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
 }
 
 
 void InsetMathRoot::mathematica(MathematicaStream & os) const
 {
-       os << '(' << cell(1) << ")^(1/(" << cell(0) <<"))";
+       os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
 }
 
 
 void InsetMathRoot::octave(OctaveStream & os) const
 {
-       os << '(' << cell(1) << ")^(1/(" << cell(0) <<"))";
+       os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
 }
 
 
 void InsetMathRoot::mathmlize(MathStream & os) const
 {
-       os << MTag("mroot") << cell(1) << cell(0) << ETag("mroot");
+       os << MTag("mroot") << cell(0) << cell(1) << ETag("mroot");
 }
 
 
 void InsetMathRoot::htmlize(HtmlStream & os) const
 {
        os << MTag("span", "class='root'")
-          << MTag("sup") << cell(0) << ETag("sup")
+          << MTag("sup") << cell(1) << ETag("sup")
           << from_ascii("&radic;")
-          << MTag("span", "class='rootof'")    << cell(1) << ETag("span")
+          << MTag("span", "class='rootof'")    << cell(0) << ETag("span")
                 << ETag("span");
 }
 
index cb5c34ff2029d6890dc76dfd06721e15d54990d6..63c35b6425bc6ecbca49fc59354bd7707b59116f 100644 (file)
@@ -26,14 +26,11 @@ public:
        InsetMathRoot(Buffer * buf);
        ///
        bool idxUpDown(Cursor & cur, bool up) const;
+
        ///
        void metrics(MetricsInfo & mi, Dimension & dim) const;
        ///
        void draw(PainterInfo & pi, int x, int y) const;
-       ///
-       idx_type firstIdx() const { return 1; }
-       ///
-       idx_type lastIdx() const { return 1; }
 
        ///
        void write(WriteStream & os) const;
index dc01ffca90c20c6afa2fbffbe62a567f3cec6e37..4f9623e5e6d7d2e895f5762397bedfc5b0a17959 100644 (file)
@@ -36,15 +36,17 @@ Inset * InsetMathStackrel::clone() const
 
 bool InsetMathStackrel::idxUpDown(Cursor & cur, bool up) const
 {
+       idx_type const npos = 1234; // impossible number
+       idx_type target = npos;
        if (up) {
-               if (cur.idx() == 0)
-                       return false;
+               idx_type const targets[] = { 1, npos, 0 };
+               target = targets[cur.idx()];
        } else {
-               if (cur.idx() + 1 ==  nargs())
-                       return false;
+               idx_type const targets[] = { 2, 0, npos };
+               target = targets[cur.idx()];
        }
-       InsetMath::idx_type target = up ? cur.idx() - 1 : cur.idx() + 1;
-       if (cur.idx() == target)
+
+       if (target == npos || target == nargs())
                return false;
        cur.idx() = target;
        cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
@@ -62,21 +64,21 @@ MathClass InsetMathStackrel::mathClass() const
 void InsetMathStackrel::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        Changer dummy2 = mi.base.changeEnsureMath();
-       Dimension dim1;
-       cell(1).metrics(mi, dim1);
-       Changer dummy = mi.base.changeFrac();
        Dimension dim0;
        cell(0).metrics(mi, dim0);
+       Changer dummy = mi.base.changeFrac();
+       Dimension dim1;
+       cell(1).metrics(mi, dim1);
        if (nargs() > 2) {
                Dimension dim2;
                cell(2).metrics(mi, dim2);
-               dim.wid = max(max(dim0.width(), dim1.width()), dim2.width()) + 4;
-               dim.asc = dim1.ascent() + dim0.height() + 4;
-               dim.des = dim1.descent() + dim2.height() + dim2.descent() + 1;
+               dim.wid = max(max(dim1.width(), dim0.width()), dim2.width()) + 4;
+               dim.asc = dim0.ascent() + dim1.height() + 4;
+               dim.des = dim0.descent() + dim2.height() + dim2.descent() + 1;
        } else {
-               dim.wid = max(dim0.width(), dim1.width()) + 4;
-               dim.asc = dim1.ascent() + dim0.height() + 4;
-               dim.des = dim1.descent();
+               dim.wid = max(dim1.width(), dim0.width()) + 4;
+               dim.asc = dim0.ascent() + dim1.height() + 4;
+               dim.des = dim0.descent();
        }
 }
 
@@ -88,13 +90,13 @@ void InsetMathStackrel::draw(PainterInfo & pi, int x, int y) const
        Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
        Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
        int m  = x + dim.width() / 2;
-       int yo = y - dim1.ascent() - dim0.descent() - 1;
-       cell(1).draw(pi, m - dim1.width() / 2, y);
+       int yo = y - dim0.ascent() - dim1.descent() - 1;
+       cell(0).draw(pi, m - dim0.width() / 2, y);
        Changer dummy = pi.base.changeFrac();
-       cell(0).draw(pi, m - dim0.width() / 2, yo);
+       cell(1).draw(pi, m - dim1.width() / 2, yo);
        if (nargs() > 2) {
                Dimension const & dim2 = cell(2).dimension(*pi.base.bv);
-               int y2 = y + dim1.descent() + dim2.ascent() + 1;
+               int y2 = y + dim0.descent() + dim2.ascent() + 1;
                cell(2).draw(pi, m - dim2.width() / 2, y2);
        }
 }
@@ -106,13 +108,13 @@ void InsetMathStackrel::write(WriteStream & os) const
        os << "\\stackrel";
        if (nargs() > 2)
                os << '[' << cell(2) << ']';
-       os << '{' << cell(0) << "}{" << cell(1) << '}';
+       os << '{' << cell(1) << "}{" << cell(0) << '}';
 }
 
 
 void InsetMathStackrel::normalize(NormalStream & os) const
 {
-       os << "[stackrel " << cell(0) << ' ' << cell(1);
+       os << "[stackrel " << cell(1) << ' ' << cell(0);
        if (nargs() > 2)
                os << ' ' << cell(2);
        os << ']';
@@ -122,9 +124,9 @@ void InsetMathStackrel::normalize(NormalStream & os) const
 void InsetMathStackrel::mathmlize(MathStream & ms) const
 {
        if (nargs() > 2)
-               ms << "<munderover>" << cell(1) << cell(2) << cell(0) << "</munderover>";
+               ms << "<munderover>" << cell(0) << cell(2) << cell(1) << "</munderover>";
        else
-               ms << "<mover accent='false'>" << cell(1) << cell(0) << "</mover>";
+               ms << "<mover accent='false'>" << cell(0) << cell(1) << "</mover>";
 }
 
 
@@ -132,14 +134,14 @@ void InsetMathStackrel::htmlize(HtmlStream & os) const
 {
        if (nargs() > 2) {
                os << MTag("span", "class='underoverset'")
-                  << MTag("span", "class='top'") << cell(0) << ETag("span")
-                  << MTag("span") << cell(1) << ETag("span")
+                  << MTag("span", "class='top'") << cell(1) << ETag("span")
+                  << MTag("span") << cell(0) << ETag("span")
                   << MTag("span", "class='bottom'") << cell(2) << ETag("span");
        } else {
                // at the moment, this is exactly the same as overset
                os << MTag("span", "class='overset'")
-                  << MTag("span", "class='top'") << cell(0) << ETag("span")
-                  << MTag("span") << cell(1) << ETag("span");
+                  << MTag("span", "class='top'") << cell(1) << ETag("span")
+                  << MTag("span") << cell(0) << ETag("span");
        }
        os << ETag("span");
 }
index 2dc7a4975c5043230ace25d3a0a5ef8dcd1cb9d9..d6ac81518231201cc2608ea2c681b63d82e1f0d0 100644 (file)
@@ -30,10 +30,6 @@ public:
        ///
        void draw(PainterInfo & pi, int x, int y) const;
        ///
-       idx_type firstIdx() const { return 1; }
-       ///
-       idx_type lastIdx() const { return 1; }
-       ///
        void write(WriteStream & os) const;
        ///
        void normalize(NormalStream &) const;
index 4df7f59a5bbaa2db73ba7eee1bfd42bac64f8f73..559889073b9811944d86debdfa8f77c5bec37787 100644 (file)
@@ -33,14 +33,14 @@ Inset * InsetMathUnderset::clone() const
 void InsetMathUnderset::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        Changer dummy2 = mi.base.changeEnsureMath();
-       Dimension dim1;
-       cell(1).metrics(mi, dim1);
-       Changer dummy = mi.base.changeFrac();
        Dimension dim0;
        cell(0).metrics(mi, dim0);
-       dim.wid = max(dim0.width(), dim1.width()) + 4;
-       dim.asc = dim1.ascent();
-       dim.des = dim1.descent() + dim0.height() + 4;
+       Changer dummy = mi.base.changeFrac();
+       Dimension dim1;
+       cell(1).metrics(mi, dim1);
+       dim.wid = max(dim1.width(), dim0.width()) + 4;
+       dim.asc = dim0.ascent();
+       dim.des = dim0.descent() + dim1.height() + 4;
 }
 
 
@@ -48,24 +48,13 @@ void InsetMathUnderset::draw(PainterInfo & pi, int x, int y) const
 {
        Changer dummy2 = pi.base.changeEnsureMath();
        Dimension const dim = dimension(*pi.base.bv);
-       Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
        Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
+       Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
        int m  = x + dim.wid / 2;
-       int yo = y + dim1.descent() + dim0.ascent() + 1;
-       cell(1).draw(pi, m - dim1.width() / 2, y);
+       int yo = y + dim0.descent() + dim1.ascent() + 1;
+       cell(0).draw(pi, m - dim0.width() / 2, y);
        Changer dummy = pi.base.changeFrac();
-       cell(0).draw(pi, m - dim0.width() / 2, yo);
-}
-
-
-bool InsetMathUnderset::idxUpDown(Cursor & cur, bool up) const
-{
-       idx_type target = up; // up ? 1 : 0, since upper cell has idx 1
-       if (cur.idx() == target)
-               return false;
-       cur.idx() = target;
-       cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target());
-       return true;
+       cell(1).draw(pi, m - dim1.width() / 2, yo);
 }
 
 
@@ -74,27 +63,27 @@ void InsetMathUnderset::write(WriteStream & os) const
        MathEnsurer ensurer(os);
        if (os.fragile())
                os << "\\protect";
-       os << "\\underset{" << cell(0) << "}{" << cell(1) << '}';
+       os << "\\underset{" << cell(1) << "}{" << cell(0) << '}';
 }
 
 
 void InsetMathUnderset::normalize(NormalStream & os) const
 {
-       os << "[underset " << cell(0) << ' ' << cell(1) << ']';
+       os << "[underset " << cell(1) << ' ' << cell(0) << ']';
 }
 
 
 void InsetMathUnderset::mathmlize(MathStream & ms) const
 {
-       ms << "<munder accent='false'>" << cell(1) << cell(0) << "</munder>";
+       ms << "<munder accent='false'>" << cell(0) << cell(1) << "</munder>";
 }
 
 
 void InsetMathUnderset::htmlize(HtmlStream & os) const
 {
        os << MTag("span", "class='underset'")
-                << MTag("span") << cell(0) << ETag("span")
-                << MTag("span", "class='bottom'") << cell(1) << ETag("span")
+                << MTag("span") << cell(1) << ETag("span")
+                << MTag("span", "class='bottom'") << cell(0) << ETag("span")
                 << ETag("span");
 }
 
index ff671adccba7c77a44a0cd4b87010d022fb07301..fbc3e779deaf6f1553bb9bbecb8da628270fa44c 100644 (file)
@@ -28,12 +28,6 @@ public:
        ///
        void draw(PainterInfo & pi, int x, int y) const;
        ///
-       idx_type firstIdx() const { return 1; }
-       ///
-       idx_type lastIdx() const { return 1; }
-       ///
-       bool idxUpDown(Cursor & cur, bool up) const;
-       ///
        void write(WriteStream & ws) const;
        ///
        void normalize(NormalStream & ns) const;