From af1b7b5fc2e809e86342b29644974be1c52f827c Mon Sep 17 00:00:00 2001 From: Jean-Marc Lasgouttes Date: Mon, 3 Jun 2019 16:22:44 +0200 Subject: [PATCH] Make caret visible inside math macros arguments The first step is to move the MathRow cache to BufferView, alongside coordCache. This was on the todo list anyway, since it allows to let go the math row information when the math equation is not on the screen anymore. With the old scheme, it would always remain in memory. Then, when computing caret size in MathData::metrics, make sure that the mathrow of the elements that are linearized in the MathRow object get their caret size information initialized too. Fixes bug #11587. --- src/BufferView.cpp | 23 +++++++++++++++++++++-- src/BufferView.h | 7 +++++++ src/mathed/MathData.cpp | 21 +++++++-------------- src/mathed/MathData.h | 9 --------- src/mathed/MathRow.h | 2 +- 5 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/BufferView.cpp b/src/BufferView.cpp index ac6aa6ba76..2068f050cf 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -252,6 +252,9 @@ struct BufferView::Private Update::flags update_flags_; /// CoordCache coord_cache_; + /// + typedef map MathRows; + MathRows math_rows_; /// Estimated average par height for scrollbar. int wh_; @@ -430,6 +433,20 @@ CoordCache const & BufferView::coordCache() const } +MathRow const & BufferView::mathRow(MathData const * cell) const +{ + auto it = d->math_rows_.find(cell); + LATTEST(it != d->math_rows_.end()); + return it->second; +} + + +void BufferView::setMathRow(MathData const * cell, MathRow const & mrow) +{ + d->math_rows_[cell] = mrow; +} + + Buffer & BufferView::buffer() { return buffer_; @@ -2771,6 +2788,7 @@ void BufferView::updateMetrics(Update::flags & update_flags) // Clear out the position cache in case of full screen redraw, d->coord_cache_.clear(); + d->math_rows_.clear(); // Clear out paragraph metrics to avoid having invalid metrics // in the cache from paragraphs not relayouted below @@ -3011,8 +3029,9 @@ void BufferView::caretPosAndHeight(Point & p, int & h) const int asc, des; Cursor const & cur = cursor(); if (cur.inMathed()) { - asc = cur.cell().caretAscent(this); - des = cur.cell().caretDescent(this); + MathRow const & mrow = mathRow(&cur.cell()); + asc = mrow.caret_ascent; + des = mrow.caret_descent; } else { Font const font = cur.real_current_font; frontend::FontMetrics const & fm = theFontMetrics(font); diff --git a/src/BufferView.h b/src/BufferView.h index 0d1a1bba31..45928f256e 100644 --- a/src/BufferView.h +++ b/src/BufferView.h @@ -42,6 +42,8 @@ class FuncStatus; class Intl; class Inset; class Length; +class MathData; +class MathRow; class ParIterator; class ParagraphMetrics; class Point; @@ -296,6 +298,11 @@ public: /// CoordCache const & coordCache() const; + /// + MathRow const & mathRow(MathData const * cell) const; + /// + void setMathRow(MathData const * cell, MathRow const & mrow); + /// Point getPos(DocIterator const & dit) const; /// is the paragraph of the cursor visible ? diff --git a/src/mathed/MathData.cpp b/src/mathed/MathData.cpp index c3b3faa239..0676dc732a 100644 --- a/src/mathed/MathData.cpp +++ b/src/mathed/MathData.cpp @@ -267,18 +267,6 @@ bool isInside(DocIterator const & it, MathData const & ar, #endif -int MathData::caretAscent(BufferView const * bv) const -{ - return mrow_cache_[bv].caret_ascent; -} - - -int MathData::caretDescent(BufferView const * bv) const -{ - return mrow_cache_[bv].caret_descent; -} - - void MathData::metrics(MetricsInfo & mi, Dimension & dim, bool tight) const { frontend::FontMetrics const & fm = theFontMetrics(mi.base.font); @@ -310,9 +298,14 @@ void MathData::metrics(MetricsInfo & mi, Dimension & dim, bool tight) const // so that we can set the caret vertical dimensions. mrow.caret_ascent = min(dim.asc, fm.maxAscent()); mrow.caret_descent = min(dim.des, fm.maxDescent()); + /// do the same for math cells linearized in the row + MathRow caret_row = MathRow(mrow.caret_ascent, mrow.caret_descent); + for (auto const & e : mrow) + if (e.type == MathRow::BEGIN && e.ar) + bv->setMathRow(e.ar, caret_row); // Cache row and dimension. - mrow_cache_[bv] = mrow; + bv->setMathRow(this, mrow); bv->coordCache().arrays().add(this, dim); } @@ -357,7 +350,7 @@ void MathData::draw(PainterInfo & pi, int const x, int const y) const setXY(*pi.base.bv, x, y); drawSelection(pi, x, y); - MathRow const & mrow = mrow_cache_[pi.base.bv]; + MathRow const & mrow = pi.base.bv->mathRow(this); mrow.draw(pi, x, y); } diff --git a/src/mathed/MathData.h b/src/mathed/MathData.h index 0e6c97747f..b118d6c41f 100644 --- a/src/mathed/MathData.h +++ b/src/mathed/MathData.h @@ -26,7 +26,6 @@ #include #include -#include namespace lyx { @@ -124,11 +123,6 @@ public: /// Add this array to a math row. Return true if contents got added bool addToMathRow(MathRow &, MetricsInfo & mi) const; - // ascent of caret in this cell - int caretAscent(BufferView const * bv) const; - /// descent of caret in this cell - int caretDescent(BufferView const * bv) const; - /// rebuild cached metrics information /** When \c tight is true, the height of the cell will be at least * the x height of the font. Otherwise, it will be the max height @@ -197,9 +191,6 @@ protected: mutable int kerning_; Buffer * buffer_; - /// cached object that describes typeset data - mutable std::map mrow_cache_; - private: /// is this an exact match at this position? bool find1(MathData const & ar, size_type pos) const; diff --git a/src/mathed/MathRow.h b/src/mathed/MathRow.h index 08f56128de..e009b90d56 100644 --- a/src/mathed/MathRow.h +++ b/src/mathed/MathRow.h @@ -86,7 +86,7 @@ public: }; /// - MathRow() : caret_ascent(0), caret_descent(0) {}; + MathRow(int asc = 0, int des = 0) : caret_ascent(asc), caret_descent(des) {}; /// typedef std::vector Elements; /// -- 2.39.5