commit 6727022b052814b9981d3c2e18cffd99464d504f
Author: Jean-Marc Lasgouttes <[email protected]>
Date:   Thu Sep 3 08:49:56 2020 +0200

    Code factorization around getRow()
    
    Rename ParagraphMetrics::pos2row to getRowIndex and add a 'boundary'
    parameter. Simplify code that handles boundaries.
    
    No change intended.
---
 src/BufferView.cpp       | 23 ++++++-----------------
 src/Cursor.cpp           | 12 ++----------
 src/ParagraphMetrics.cpp | 16 ++++------------
 src/ParagraphMetrics.h   |  2 +-
 src/TextMetrics.cpp      |  9 +++------
 5 files changed, 16 insertions(+), 46 deletions(-)

diff --git a/src/BufferView.cpp b/src/BufferView.cpp
index d0fbeb59f4..e7aa2826c9 100644
--- a/src/BufferView.cpp
+++ b/src/BufferView.cpp
@@ -3429,20 +3429,10 @@ Point BufferView::coordOffset(DocIterator const & dit) 
const
 
        LBUFERR(!pm.rows().empty());
        y -= pm.rows()[0].ascent();
-#if 1
-       // FIXME: document this mess
-       size_t rend;
-       if (sl.pos() > 0 && dit.depth() == 1) {
-               int pos = sl.pos();
-               if (pos && dit.boundary())
-                       --pos;
-//             lyxerr << "coordOffset: boundary:" << dit.boundary() << " 
depth:" << dit.depth() << " pos:" << pos << " sl.pos:" << sl.pos() << endl;
-               rend = pm.pos2row(pos);
-       } else
-               rend = pm.pos2row(sl.pos());
-#else
-       size_t rend = pm.pos2row(sl.pos());
-#endif
+
+       // Take boundary into account if cursor is in main text.
+       bool const has_boundary = dit.depth() == 1 && dit.boundary();
+       size_t const rend = pm.getRowIndex(sl.pos(), has_boundary);
        for (size_t rit = 0; rit != rend; ++rit)
                y += pm.rows()[rit].height();
        y += pm.rows()[rend].ascent();
@@ -3450,13 +3440,12 @@ Point BufferView::coordOffset(DocIterator const & dit) 
const
        TextMetrics const & bottom_tm = textMetrics(dit.bottom().text());
 
        // Make relative position from the nested inset now bufferview absolute.
-       int xx = bottom_tm.cursorX(dit.bottom(), dit.boundary() && dit.depth() 
== 1);
+       int xx = bottom_tm.cursorX(dit.bottom(), has_boundary);
        x += xx;
 
        // In the RTL case place the nested inset at the left of the cursor in
        // the outer paragraph
-       bool boundary_1 = dit.boundary() && 1 == dit.depth();
-       bool rtl = bottom_tm.isRTL(dit.bottom(), boundary_1);
+       bool rtl = bottom_tm.isRTL(dit.bottom(), has_boundary);
        if (rtl)
                x -= lastw;
 
diff --git a/src/Cursor.cpp b/src/Cursor.cpp
index 76f4be407e..225e4a685c 100644
--- a/src/Cursor.cpp
+++ b/src/Cursor.cpp
@@ -1412,11 +1412,7 @@ bool Cursor::atFirstOrLastRow(bool up)
        TextMetrics const & tm = bv_->textMetrics(text());
        ParagraphMetrics const & pm = tm.parMetrics(pit());
 
-       int row;
-       if (pos() && boundary())
-               row = pm.pos2row(pos() - 1);
-       else
-               row = pm.pos2row(pos());
+       int const row = pm.getRowIndex(pos(), boundary());
 
        if (up) {
                if (pit() == 0 && row == 0)
@@ -2196,11 +2192,7 @@ bool Cursor::upDownInText(bool up)
        // first get the current line
        TextMetrics & tm = bv_->textMetrics(text());
        ParagraphMetrics const & pm = tm.parMetrics(pit());
-       int row;
-       if (pos() && boundary())
-               row = pm.pos2row(pos() - 1);
-       else
-               row = pm.pos2row(pos());
+       int const row = pm.getRowIndex(pos(), boundary());
 
        if (atFirstOrLastRow(up)) {
                // Is there a place for the cursor to go ? If yes, we
diff --git a/src/ParagraphMetrics.cpp b/src/ParagraphMetrics.cpp
index e1284b0e80..470c765350 100644
--- a/src/ParagraphMetrics.cpp
+++ b/src/ParagraphMetrics.cpp
@@ -91,7 +91,7 @@ bool ParagraphMetrics::hasPosition() const
 }
 
 
-Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
+size_t ParagraphMetrics::getRowIndex(pos_type pos, bool boundary) const
 {
        LBUFERR(!rows().empty());
 
@@ -106,21 +106,13 @@ Row const & ParagraphMetrics::getRow(pos_type pos, bool 
boundary) const
        for (--rit; rit != begin && rit->pos() > pos; --rit)
                ;
 
-       return *rit;
+       return rit - begin;
 }
 
 
-size_t ParagraphMetrics::pos2row(pos_type pos) const
+Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
 {
-       LBUFERR(!rows().empty());
-
-       RowList::const_iterator rit = rows_.end();
-       RowList::const_iterator const begin = rows_.begin();
-
-       for (--rit; rit != begin && rit->pos() > pos; --rit)
-               ;
-
-       return rit - begin;
+       return *(rows_.begin() + getRowIndex(pos, boundary));
 }
 
 
diff --git a/src/ParagraphMetrics.h b/src/ParagraphMetrics.h
index b572f122b5..15f2d61749 100644
--- a/src/ParagraphMetrics.h
+++ b/src/ParagraphMetrics.h
@@ -41,7 +41,7 @@ public:
        ///
        Row const & getRow(pos_type pos, bool boundary) const;
        ///
-       size_t pos2row(pos_type pos) const;
+       size_t getRowIndex(pos_type pos, bool boundary) const;
 
        /// TextMetrics::redoParagraph updates this
        Dimension const & dim() const { return dim_; }
diff --git a/src/TextMetrics.cpp b/src/TextMetrics.cpp
index 6ba15b0251..c5f2bf498a 100644
--- a/src/TextMetrics.cpp
+++ b/src/TextMetrics.cpp
@@ -1762,13 +1762,10 @@ int TextMetrics::cursorY(CursorSlice const & sl, bool 
boundary) const
 
        int h = 0;
        h -= parMetrics(0).rows()[0].ascent();
-       for (pit_type pit = 0; pit < sl.pit(); ++pit) {
+       for (pit_type pit = 0; pit < sl.pit(); ++pit)
                h += parMetrics(pit).height();
-       }
-       int pos = sl.pos();
-       if (pos && boundary)
-               --pos;
-       size_t const rend = pm.pos2row(pos);
+
+       size_t const rend = pm.getRowIndex(sl.pos(), boundary);
        for (size_t rit = 0; rit != rend; ++rit)
                h += pm.rows()[rit].height();
        h += pm.rows()[rend].ascent();
-- 
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to