From 629a692ce28db51a8d49ca17f61e311947de8437 Mon Sep 17 00:00:00 2001 From: Jean-Marc Lasgouttes Date: Fri, 29 Nov 2024 12:13:15 +0100 Subject: [PATCH] Cleanup Row::findElement and introduce Row::pos2x Remove the functionality of findElement of also computing a pixel position and introduce a function that only does that. --- src/Cursor.cpp | 3 +-- src/Row.cpp | 18 +++++++++++++++++- src/Row.h | 11 +++++++++-- src/TextMetrics.cpp | 7 +------ 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/Cursor.cpp b/src/Cursor.cpp index 11c04691f9..6c94716e66 100644 --- a/src/Cursor.cpp +++ b/src/Cursor.cpp @@ -1197,8 +1197,7 @@ void Cursor::getSurroundingPos(pos_type & left_pos, pos_type & right_pos) const right_pos = -1; Row const & row = textRow(); - double dummy = 0; - Row::const_iterator cit = row.findElement(pos(), boundary(), dummy); + Row::const_iterator cit = row.findElement(pos(), boundary()); // Handle the case of empty row if (cit == row.end()) { if (row.isRTL()) diff --git a/src/Row.cpp b/src/Row.cpp index d6c45c2ad6..7aa6d93447 100644 --- a/src/Row.cpp +++ b/src/Row.cpp @@ -753,8 +753,9 @@ void Row::reverseRTL() reverse(elements_.begin(), elements_.end()); } + Row::const_iterator const -Row::findElement(pos_type const pos, bool const boundary, double & x) const +Row::findElementHelper(pos_type const pos, bool const boundary, double & x) const { /** * When boundary is true, position i is in the row element (pos, endpos) @@ -803,4 +804,19 @@ Row::findElement(pos_type const pos, bool const boundary, double & x) const } +Row::const_iterator const Row::findElement(pos_type const pos, bool const boundary) const +{ + double dummy; + return findElementHelper(pos, boundary, dummy); +} + + +double Row::pos2x(pos_type const pos, bool const boundary) const +{ + double x; + findElementHelper(pos, boundary, x); + return x; +} + + } // namespace lyx diff --git a/src/Row.h b/src/Row.h index cfb7427269..07cffa7fa7 100644 --- a/src/Row.h +++ b/src/Row.h @@ -257,6 +257,10 @@ public: * adjusted to the actual pixel position. */ std::pair x2pos(int & x) const; + /** Return the pixel position that corresponds to the position and + * boundary. + */ + double pos2x(pos_type const pos, bool const boundary) const; /// void add(pos_type pos, Inset const * ins, Dimension const & dim, @@ -331,8 +335,8 @@ public: /// void needsChangeBar(bool ncb) { changebar_ = ncb; } - /// Find row element that contains \c pos, and compute x offset. - const_iterator const findElement(pos_type pos, bool boundary, double & x) const; + /// Find row element that contains \c pos. + const_iterator const findElement(pos_type pos, bool boundary) const; friend std::ostream & operator<<(std::ostream & os, Row const & row); @@ -372,6 +376,9 @@ private: */ bool sameString(Font const & f, Change const & ch) const; + /// Find row element that contains \c pos, and compute x offset. + const_iterator const findElementHelper(pos_type pos, bool boundary, double & x) const; + /// Elements elements_; diff --git a/src/TextMetrics.cpp b/src/TextMetrics.cpp index 1f56a46c76..09712faf6c 100644 --- a/src/TextMetrics.cpp +++ b/src/TextMetrics.cpp @@ -1691,12 +1691,7 @@ int TextMetrics::cursorX(CursorSlice const & sl, if (pm.rows().empty()) return 0; Row const & row = pm.getRow(sl.pos(), boundary); - pos_type const pos = sl.pos(); - - double x = 0; - row.findElement(pos, boundary, x); - return int(x); - + return row.pos2x(sl.pos(), boundary); }