Code factorization around getRow()

Rename ParagraphMetrics::pos2row to getRowIndex and add a 'boundary'
parameter. Simplify code that handles boundaries.

No change intended.
This commit is contained in:
Jean-Marc Lasgouttes 2020-09-03 08:49:56 +02:00
parent 6cc9638dc2
commit 6727022b05
5 changed files with 16 additions and 46 deletions

View File

@ -3429,20 +3429,10 @@ Point BufferView::coordOffset(DocIterator const & dit) const
LBUFERR(!pm.rows().empty()); LBUFERR(!pm.rows().empty());
y -= pm.rows()[0].ascent(); y -= pm.rows()[0].ascent();
#if 1
// FIXME: document this mess // Take boundary into account if cursor is in main text.
size_t rend; bool const has_boundary = dit.depth() == 1 && dit.boundary();
if (sl.pos() > 0 && dit.depth() == 1) { size_t const rend = pm.getRowIndex(sl.pos(), has_boundary);
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
for (size_t rit = 0; rit != rend; ++rit) for (size_t rit = 0; rit != rend; ++rit)
y += pm.rows()[rit].height(); y += pm.rows()[rit].height();
y += pm.rows()[rend].ascent(); y += pm.rows()[rend].ascent();
@ -3450,13 +3440,12 @@ Point BufferView::coordOffset(DocIterator const & dit) const
TextMetrics const & bottom_tm = textMetrics(dit.bottom().text()); TextMetrics const & bottom_tm = textMetrics(dit.bottom().text());
// Make relative position from the nested inset now bufferview absolute. // 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; x += xx;
// In the RTL case place the nested inset at the left of the cursor in // In the RTL case place the nested inset at the left of the cursor in
// the outer paragraph // the outer paragraph
bool boundary_1 = dit.boundary() && 1 == dit.depth(); bool rtl = bottom_tm.isRTL(dit.bottom(), has_boundary);
bool rtl = bottom_tm.isRTL(dit.bottom(), boundary_1);
if (rtl) if (rtl)
x -= lastw; x -= lastw;

View File

@ -1412,11 +1412,7 @@ bool Cursor::atFirstOrLastRow(bool up)
TextMetrics const & tm = bv_->textMetrics(text()); TextMetrics const & tm = bv_->textMetrics(text());
ParagraphMetrics const & pm = tm.parMetrics(pit()); ParagraphMetrics const & pm = tm.parMetrics(pit());
int row; int const row = pm.getRowIndex(pos(), boundary());
if (pos() && boundary())
row = pm.pos2row(pos() - 1);
else
row = pm.pos2row(pos());
if (up) { if (up) {
if (pit() == 0 && row == 0) if (pit() == 0 && row == 0)
@ -2196,11 +2192,7 @@ bool Cursor::upDownInText(bool up)
// first get the current line // first get the current line
TextMetrics & tm = bv_->textMetrics(text()); TextMetrics & tm = bv_->textMetrics(text());
ParagraphMetrics const & pm = tm.parMetrics(pit()); ParagraphMetrics const & pm = tm.parMetrics(pit());
int row; int const row = pm.getRowIndex(pos(), boundary());
if (pos() && boundary())
row = pm.pos2row(pos() - 1);
else
row = pm.pos2row(pos());
if (atFirstOrLastRow(up)) { if (atFirstOrLastRow(up)) {
// Is there a place for the cursor to go ? If yes, we // Is there a place for the cursor to go ? If yes, we

View File

@ -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()); 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) 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()); return *(rows_.begin() + getRowIndex(pos, boundary));
RowList::const_iterator rit = rows_.end();
RowList::const_iterator const begin = rows_.begin();
for (--rit; rit != begin && rit->pos() > pos; --rit)
;
return rit - begin;
} }

View File

@ -41,7 +41,7 @@ public:
/// ///
Row const & getRow(pos_type pos, bool boundary) const; 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 /// TextMetrics::redoParagraph updates this
Dimension const & dim() const { return dim_; } Dimension const & dim() const { return dim_; }

View File

@ -1762,13 +1762,10 @@ int TextMetrics::cursorY(CursorSlice const & sl, bool boundary) const
int h = 0; int h = 0;
h -= parMetrics(0).rows()[0].ascent(); 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(); h += parMetrics(pit).height();
}
int pos = sl.pos(); size_t const rend = pm.getRowIndex(sl.pos(), boundary);
if (pos && boundary)
--pos;
size_t const rend = pm.pos2row(pos);
for (size_t rit = 0; rit != rend; ++rit) for (size_t rit = 0; rit != rend; ++rit)
h += pm.rows()[rit].height(); h += pm.rows()[rit].height();
h += pm.rows()[rend].ascent(); h += pm.rows()[rend].ascent();