Introduce new helpers ParagraphMetrics::top/bottom

This avoids code with position/ascent/descent that is difficult to follow.

No change in function intended.
This commit is contained in:
Jean-Marc Lasgouttes 2023-07-25 16:31:13 +02:00
parent 7f85024f80
commit 0b6105b924
4 changed files with 20 additions and 26 deletions

View File

@ -641,8 +641,8 @@ void BufferView::updateScrollbarParameters()
<< d->par_height_[pit]); << d->par_height_[pit]);
} }
int top_pos = first.second->position() - first.second->ascent(); int top_pos = first.second->top();
int bottom_pos = last.second->position() + last.second->descent(); int bottom_pos = last.second->bottom();
bool first_visible = first.first == 0 && top_pos >= 0; bool first_visible = first.first == 0 && top_pos >= 0;
bool last_visible = last.first + 1 == int(parsize) && bottom_pos <= height_; bool last_visible = last.first + 1 == int(parsize) && bottom_pos <= height_;
if (first_visible && last_visible) { if (first_visible && last_visible) {
@ -2726,7 +2726,7 @@ int BufferView::scrollDown(int pixels)
int const ymax = height_ + pixels; int const ymax = height_ + pixels;
while (true) { while (true) {
pair<pit_type, ParagraphMetrics const *> last = tm.last(); pair<pit_type, ParagraphMetrics const *> last = tm.last();
int bottom_pos = last.second->position() + last.second->descent(); int bottom_pos = last.second->bottom();
if (lyxrc.scroll_below_document) if (lyxrc.scroll_below_document)
bottom_pos += height_ - minVisiblePart(); bottom_pos += height_ - minVisiblePart();
if (last.first + 1 == int(text->paragraphs().size())) { if (last.first + 1 == int(text->paragraphs().size())) {
@ -2751,7 +2751,7 @@ int BufferView::scrollUp(int pixels)
int ymin = - pixels; int ymin = - pixels;
while (true) { while (true) {
pair<pit_type, ParagraphMetrics const *> first = tm.first(); pair<pit_type, ParagraphMetrics const *> first = tm.first();
int top_pos = first.second->position() - first.second->ascent(); int top_pos = first.second->top();
if (first.first == 0) { if (first.first == 0) {
if (top_pos >= 0) if (top_pos >= 0)
return 0; return 0;
@ -3102,10 +3102,8 @@ bool BufferView::singleParUpdate()
tm.updatePosCache(pit); tm.updatePosCache(pit);
LYXERR(Debug::PAINTING, "\ny1: " << pm.position() - pm.ascent() LYXERR(Debug::PAINTING, "\ny1: " << pm.top() << " y2: " << pm.bottom()
<< " y2: " << pm.position() + pm.descent() << " pit: " << pit << " singlepar: 1");
<< " pit: " << pit
<< " singlepar: 1");
return true; return true;
} }
@ -3653,7 +3651,7 @@ void BufferView::draw(frontend::Painter & pain, bool paint_caret)
// and possibly grey out below // and possibly grey out below
pair<pit_type, ParagraphMetrics const *> lastpm = tm.last(); pair<pit_type, ParagraphMetrics const *> lastpm = tm.last();
int const y2 = lastpm.second->position() + lastpm.second->descent(); int const y2 = lastpm.second->bottom();
if (y2 < height_) { if (y2 < height_) {
Color color = buffer().isInternal() Color color = buffer().isInternal()
@ -3674,7 +3672,7 @@ void BufferView::draw(frontend::Painter & pain, bool paint_caret)
pair<pit_type, ParagraphMetrics const *> lastpm = tm.last(); pair<pit_type, ParagraphMetrics const *> lastpm = tm.last();
for (pit_type pit = firstpm.first; pit <= lastpm.first; ++pit) { for (pit_type pit = firstpm.first; pit <= lastpm.first; ++pit) {
ParagraphMetrics const & pm = tm.parMetrics(pit); ParagraphMetrics const & pm = tm.parMetrics(pit);
if (pm.position() + pm.descent() > 0) { if (pm.bottom() > 0) {
if (d->anchor_pit_ != pit if (d->anchor_pit_ != pit
|| d->anchor_ypos_ != pm.position()) || d->anchor_ypos_ != pm.position())
LYXERR(Debug::PAINTING, "Found new anchor pit = " << d->anchor_pit_ LYXERR(Debug::PAINTING, "Found new anchor pit = " << d->anchor_pit_

View File

@ -69,9 +69,13 @@ public:
/// ///
bool hfillExpansion(Row const & row, pos_type pos) const; bool hfillExpansion(Row const & row, pos_type pos) const;
/// /// The vertical position of the baseline of the first line of the paragraph
int position() const { return position_; } int position() const { return position_; }
void setPosition(int position); void setPosition(int position);
/// The vertical position of the top of the paragraph
int top() const { return position_ - dim_.ascent(); }
/// The vertical position of the bottom of the paragraph
int bottom() const { return position_ + dim_.descent(); }
/// ///
int id() const { return id_; } int id() const { return id_; }

View File

@ -191,8 +191,7 @@ void TextMetrics::newParMetricsDown()
// do it and update its position. // do it and update its position.
redoParagraph(pit); redoParagraph(pit);
par_metrics_[pit].setPosition(last.second.position() par_metrics_[pit].setPosition(last.second.bottom() + par_metrics_[pit].ascent());
+ last.second.descent() + par_metrics_[pit].ascent());
updatePosCache(pit); updatePosCache(pit);
} }
@ -206,8 +205,7 @@ void TextMetrics::newParMetricsUp()
pit_type const pit = first.first - 1; pit_type const pit = first.first - 1;
// do it and update its position. // do it and update its position.
redoParagraph(pit); redoParagraph(pit);
par_metrics_[pit].setPosition(first.second.position() par_metrics_[pit].setPosition(first.second.top() - par_metrics_[pit].descent());
- first.second.ascent() - par_metrics_[pit].descent());
updatePosCache(pit); updatePosCache(pit);
} }
@ -1466,9 +1464,7 @@ pit_type TextMetrics::getPitNearY(int y)
ParMetricsCache::const_iterator last = et; ParMetricsCache::const_iterator last = et;
--last; --last;
ParagraphMetrics const & pm = it->second; if (y < it->second.top()) {
if (y < it->second.position() - pm.ascent()) {
// We are looking for a position that is before the first paragraph in // We are looking for a position that is before the first paragraph in
// the cache (which is in priciple off-screen, that is before the // the cache (which is in priciple off-screen, that is before the
// visible part. // visible part.
@ -1481,9 +1477,7 @@ pit_type TextMetrics::getPitNearY(int y)
return pit; return pit;
} }
ParagraphMetrics const & pm_last = par_metrics_[last->first]; if (y >= par_metrics_[last->first].bottom()) {
if (y >= last->second.position() + pm_last.descent()) {
// We are looking for a position that is after the last paragraph in // We are looking for a position that is after the last paragraph in
// the cache (which is in priciple off-screen), that is before the // the cache (which is in priciple off-screen), that is before the
// visible part. // visible part.
@ -1500,9 +1494,7 @@ pit_type TextMetrics::getPitNearY(int y)
LYXERR(Debug::PAINTING, "examining: pit: " << it->first LYXERR(Debug::PAINTING, "examining: pit: " << it->first
<< " y: " << it->second.position()); << " y: " << it->second.position());
ParagraphMetrics const & pm2 = par_metrics_[it->first]; if (it->first >= pit && it->second.top() <= y) {
if (it->first >= pit && it->second.position() - pm2.ascent() <= y) {
pit = it->first; pit = it->first;
yy = it->second.position(); yy = it->second.position();
} }
@ -1519,7 +1511,7 @@ Row const & TextMetrics::getPitAndRowNearY(int & y, pit_type & pit,
{ {
ParagraphMetrics const & pm = par_metrics_[pit]; ParagraphMetrics const & pm = par_metrics_[pit];
int yy = pm.position() - pm.ascent(); int yy = pm.top();
LBUFERR(!pm.rows().empty()); LBUFERR(!pm.rows().empty());
RowList::const_iterator rit = pm.rows().begin(); RowList::const_iterator rit = pm.rows().begin();
RowList::const_iterator rlast = pm.rows().end(); RowList::const_iterator rlast = pm.rows().end();

View File

@ -971,7 +971,7 @@ void GuiWorkArea::generateSyntheticMouseEvent()
// Find the row at which we set the cursor. // Find the row at which we set the cursor.
RowList::const_iterator rit = pm.rows().begin(); RowList::const_iterator rit = pm.rows().begin();
RowList::const_iterator rlast = pm.rows().end(); RowList::const_iterator rlast = pm.rows().end();
int yy = pm.position() - pm.ascent(); int yy = pm.top();
for (--rlast; rit != rlast; ++rit) { for (--rlast; rit != rlast; ++rit) {
int h = rit->height(); int h = rit->height();
if ((up && yy + h > 0) if ((up && yy + h > 0)