]> git.lyx.org Git - features.git/commitdiff
Make MathRow tokens completely generic
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Wed, 1 Feb 2017 16:46:53 +0000 (17:46 +0100)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Wed, 1 Feb 2017 16:46:53 +0000 (17:46 +0100)
The enum values BEG_MACRO/END_MACRO and BEG_AR/END_AR are replaced by a
single BEGIN/END pair.

The MathRow code now only knows about insets and math arrays.

src/mathed/MathMacro.cpp
src/mathed/MathRow.cpp
src/mathed/MathRow.h

index 4ea986a847827113f12c661ce8d7102f83a051b1..4dae0035eb8625f067a3d3ca3ce01972d59ae86c 100644 (file)
@@ -87,7 +87,7 @@ public:
                Changer chg = make_change(mi.base.macro_nesting,
                                          mathMacro_->nesting() == 1 ? 0 : mathMacro_->nesting());
 
-               MathRow::Element e_beg(mi, MathRow::BEG_ARG);
+               MathRow::Element e_beg(mi, MathRow::BEGIN);
                e_beg.inset = this;
                e_beg.ar = &mathMacro_->cell(idx_);
                mrow.push_back(e_beg);
@@ -106,10 +106,9 @@ public:
                        has_contents = true;
                }
 
-               MathRow::Element e_end(mi, MathRow::END_ARG);
+               MathRow::Element e_end(mi, MathRow::END);
                e_end.inset = this;
                e_end.ar = &mathMacro_->cell(idx_);
-
                mrow.push_back(e_end);
 
                return has_contents;
@@ -338,7 +337,7 @@ bool MathMacro::addToMathRow(MathRow & mrow, MetricsInfo & mi) const
        /// The macro nesting can change display of insets. Change it locally.
        Changer chg = make_change(mi.base.macro_nesting, d->nesting_);
 
-       MathRow::Element e_beg(mi, MathRow::BEG_MACRO);
+       MathRow::Element e_beg(mi, MathRow::BEGIN);
        e_beg.inset = this;
        e_beg.marker = (d->nesting_ == 1 && nargs()) ? marker() : NO_MARKER;
        mrow.push_back(e_beg);
@@ -357,7 +356,7 @@ bool MathMacro::addToMathRow(MathRow & mrow, MetricsInfo & mi) const
                has_contents = true;
        }
 
-       MathRow::Element e_end(mi, MathRow::END_MACRO);
+       MathRow::Element e_end(mi, MathRow::END);
        e_end.inset = this;
        mrow.push_back(e_end);
 
index 0f9436dbb89324dd22f502322d7780c61b85940b..3263d1c48315f373bd04cd5c642a236839de0e40 100644 (file)
@@ -37,7 +37,7 @@ namespace lyx {
 
 MathRow::Element::Element(MetricsInfo const & mi, Type t, MathClass mc)
        : type(t), mclass(mc), before(0), after(0), macro_nesting(mi.base.macro_nesting),
-         marker(InsetMath::NO_MARKER), inset(0), compl_unique_to(0),
+         marker(InsetMath::NO_MARKER), inset(0), compl_unique_to(0), ar(0),
          color(Color_red)
 {}
 
@@ -132,8 +132,8 @@ void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const
        dim.wid = 0;
        // In order to compute the dimension of macros and their
        // arguments, it is necessary to keep track of them.
-       map<InsetMath const *, Dimension> dim_insets;
-       map<MathData const *, Dimension> dim_arrays;
+       vector<pair<InsetMath const *, Dimension>> dim_insets;
+       vector<pair<MathData const *, Dimension>> dim_arrays;
        CoordCache & coords = mi.base.bv->coordCache();
        for (Element const & e : elements_) {
                mi.base.macro_nesting = e.macro_nesting;
@@ -146,29 +146,26 @@ void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const
                        d.wid += e.before + e.after;
                        coords.insets().add(e.inset, d);
                        break;
-               case BEG_MACRO:
-                       e.inset->beforeMetrics();
-                       // Add a macro to current list
-                       dim_insets[e.inset] = Dimension();
-                       break;
-               case END_MACRO:
-                       LATTEST(dim_insets.find(e.inset) != dim_insets.end());
-                       e.inset->afterMetrics();
-                       // Cache the dimension of the macro and remove it from
-                       // tracking map.
-                       coords.insets().add(e.inset, dim_insets[e.inset]);
-                       dim_insets.erase(e.inset);
-                       break;
-                       // This is basically like macros
-               case BEG_ARG:
-                       e.inset->beforeMetrics();
-                       dim_arrays[e.ar] = Dimension();
+               case BEGIN:
+                       if (e.inset) {
+                               dim_insets.push_back(make_pair(e.inset, Dimension()));
+                               e.inset->beforeMetrics();
+                       }
+                       if (e.ar)
+                               dim_arrays.push_back(make_pair(e.ar, Dimension()));
                        break;
-               case END_ARG:
-                       LATTEST(dim_arrays.find(e.ar) != dim_arrays.end());
-                       e.inset->afterMetrics();
-                       coords.arrays().add(e.ar, dim_arrays[e.ar]);
-                       dim_arrays.erase(e.ar);
+               case END:
+                       if (e.inset) {
+                               e.inset->afterMetrics();
+                               LATTEST(dim_insets.back().first == e.inset);
+                               coords.insets().add(e.inset, dim_insets.back().second);
+                               dim_insets.pop_back();
+                       }
+                       if (e.ar) {
+                               LATTEST(dim_arrays.back().first == e.ar);
+                               coords.arrays().add(e.ar, dim_arrays.back().second);
+                               dim_arrays.pop_back();
+                       }
                        break;
                case BOX:
                        d = theFontMetrics(mi.base.font).dimension('I');
@@ -271,20 +268,18 @@ void MathRow::draw(PainterInfo & pi, int x, int const y) const
                        x += d.wid;
                        break;
                }
-               case BEG_MACRO:
-                       coords.insets().add(e.inset, x, y);
-                       drawMarkers(pi, e, x, y);
-                       e.inset->beforeDraw(pi);
-                       break;
-               case END_MACRO:
-                       e.inset->afterDraw(pi);
-                       break;
-               case BEG_ARG:
-                       coords.arrays().add(e.ar, x, y);
-                       e.inset->beforeDraw(pi);
+               case BEGIN:
+                       if (e.inset) {
+                               coords.insets().add(e.inset, x, y);
+                               drawMarkers(pi, e, x, y);
+                               e.inset->beforeDraw(pi);
+                       }
+                       if (e.ar)
+                               coords.arrays().add(e.ar, x, y);
                        break;
-               case END_ARG:
-                       e.inset->afterDraw(pi);
+               case END:
+                       if (e.inset)
+                               e.inset->afterDraw(pi);
                        break;
                case BOX: {
                        if (e.color == Color_none)
@@ -344,18 +339,18 @@ ostream & operator<<(ostream & os, MathRow::Element const & e)
                   << to_utf8(class_to_string(e.mclass))
                   << "-" << e.after << ">";
                break;
-       case MathRow::BEG_MACRO:
-               os << "\\" << to_utf8(e.inset->name())
-                  << "^" << e.macro_nesting << "[";
-               break;
-       case MathRow::END_MACRO:
-               os << "]";
-               break;
-       case MathRow::BEG_ARG:
-               os << "#(";
+       case MathRow::BEGIN:
+               if (e.inset)
+                       os << "\\" << to_utf8(e.inset->name())
+                          << "^" << e.macro_nesting << "[";
+               if (e.ar)
+                       os << "(";
                break;
-       case MathRow::END_ARG:
-               os << ")";
+       case MathRow::END:
+               if (e.ar)
+                       os << ")";
+               if (e.inset)
+                       os << "]";
                break;
        case MathRow::BOX:
                os << "<" << e.before << "-[]-" << e.after << ">";
index 692c1e4ae546dea743e698bdc9789dff7b8fdafb..797248c3a5512901cf284baf7011c8c42d63c3e1 100644 (file)
@@ -48,10 +48,8 @@ public:
        enum Type {
                INSET, // this element is a plain inset
                BOX, // an empty box
-               BEG_MACRO, // a macro begins here
-               END_MACRO, // a macro ends here
-               BEG_ARG, // a macro argument begins here
-               END_ARG, // a macro argument ends here
+               BEGIN, // an inset and/or a math array begins here
+               END, // an inset and/or a math array ends here
                DUMMY // a dummy element (used before or after row)
        };
 
@@ -73,14 +71,14 @@ public:
                InsetMath::marker_type marker;
 
                /// When type is INSET
-               /// the math inset
+               /// the math inset (also for BEGIN and END)
                InsetMath const * inset;
                // Non empty when there is a completion to draw
                docstring compl_text;
                // the number of characters forming the unique part.
                size_t compl_unique_to;
 
-               // type is BEG_ARG, END_ARG
+               // type is BEGIN, END
                MathData const * ar;
 
                // type is BOX