Move TextMetrics::findRowElement to Row class

This commit is contained in:
Jean-Marc Lasgouttes 2017-02-02 15:23:20 +01:00
parent 57c3a94730
commit 1fca708c7f
5 changed files with 53 additions and 59 deletions

View File

@ -785,9 +785,8 @@ void Cursor::getSurroundingPos(pos_type & left_pos, pos_type & right_pos) const
right_pos = -1;
Row const & row = textRow();
TextMetrics const & tm = bv_->textMetrics(text());
double dummy = 0;
Row::const_iterator cit = tm.findRowElement(row, pos(), boundary(), dummy);
Row::const_iterator cit = row.findElement(pos(), boundary(), dummy);
// Handle the case of empty row
if (cit == row.end()) {
if (row.isRTL())

View File

@ -569,4 +569,53 @@ void Row::reverseRTL(bool const rtl_par)
rtl_ = rtl_par;
}
Row::const_iterator const
Row::findElement(pos_type const pos, bool const boundary, double & x) const
{
/**
* When boundary is true, position i is in the row element (pos, endpos)
* if
* pos < i <= endpos
* whereas, when boundary is false, the test is
* pos <= i < endpos
* The correction below allows to handle both cases.
*/
int const boundary_corr = (boundary && pos) ? -1 : 0;
x = left_margin;
/** Early return in trivial cases
* 1) the row is empty
* 2) the position is the left-most position of the row; there
* is a quirk here however: if the first element is virtual
* (end-of-par marker for example), then we have to look
* closer
*/
if (empty()
|| (pos == begin()->left_pos() && !boundary
&& !begin()->isVirtual()))
return begin();
Row::const_iterator cit = begin();
for ( ; cit != end() ; ++cit) {
/** Look whether the cursor is inside the element's
* span. Note that it is necessary to take the
* boundary into account, and to accept virtual
* elements, which have pos == endpos.
*/
if (pos + boundary_corr >= cit->pos
&& (pos + boundary_corr < cit->endpos || cit->isVirtual())) {
x += cit->pos2x(pos);
break;
}
x += cit->full_width();
}
if (cit == end())
--cit;
return cit;
}
} // namespace lyx

View File

@ -266,6 +266,8 @@ public:
void reverseRTL(bool rtl_par);
///
bool isRTL() const { return rtl_; }
/// Find row element that contains \c pos, and compute x offset.
const_iterator const findElement(pos_type pos, bool boundary, double & x) const;
friend std::ostream & operator<<(std::ostream & os, Row const & row);

View File

@ -1471,56 +1471,6 @@ Inset * TextMetrics::checkInsetHit(int x, int y)
}
Row::const_iterator const
TextMetrics::findRowElement(Row const & row, pos_type const pos,
bool const boundary, double & x) const
{
/**
* When boundary is true, position i is in the row element (pos, endpos)
* if
* pos < i <= endpos
* whereas, when boundary is false, the test is
* pos <= i < endpos
* The correction below allows to handle both cases.
*/
int const boundary_corr = (boundary && pos) ? -1 : 0;
x = row.left_margin;
/** Early return in trivial cases
* 1) the row is empty
* 2) the position is the left-most position of the row; there
* is a quirk here however: if the first element is virtual
* (end-of-par marker for example), then we have to look
* closer
*/
if (row.empty()
|| (pos == row.begin()->left_pos() && !boundary
&& !row.begin()->isVirtual()))
return row.begin();
Row::const_iterator cit = row.begin();
for ( ; cit != row.end() ; ++cit) {
/** Look whether the cursor is inside the element's
* span. Note that it is necessary to take the
* boundary into account, and to accept virtual
* elements, which have pos == endpos.
*/
if (pos + boundary_corr >= cit->pos
&& (pos + boundary_corr < cit->endpos || cit->isVirtual())) {
x += cit->pos2x(pos);
break;
}
x += cit->full_width();
}
if (cit == row.end())
--cit;
return cit;
}
int TextMetrics::cursorX(CursorSlice const & sl,
bool boundary) const
{
@ -1533,7 +1483,7 @@ int TextMetrics::cursorX(CursorSlice const & sl,
pos_type const pos = sl.pos();
double x = 0;
findRowElement(row, pos, boundary, x);
row.findElement(pos, boundary, x);
return int(x);
}

View File

@ -190,12 +190,6 @@ public:
/// x,y are screen coordinates
void setCursorFromCoordinates(Cursor & cur, int x, int y);
/// Helper function: find row element that contains pos, and
/// compute x offset.
Row::const_iterator const
findRowElement(Row const & row, pos_type const pos,
bool const boundary, double & x) const;
///
int cursorX(CursorSlice const & cursor, bool boundary) const;
///