]> git.lyx.org Git - lyx.git/blobdiff - src/TextMetrics.cpp
Remove undisclosed imports (from xxx import *)
[lyx.git] / src / TextMetrics.cpp
index 837ad5766f31591a2e40c0e308e9e35e5ad23abc..766f0aae24427129a9185a3e0d5a76935a80de05 100644 (file)
@@ -118,6 +118,12 @@ bool TextMetrics::contains(pit_type pit) const
 }
 
 
+void TextMetrics::forget(pit_type pit)
+{
+       par_metrics_.erase(pit);
+}
+
+
 pair<pit_type, ParagraphMetrics const *> TextMetrics::first() const
 {
        ParMetricsCache::const_iterator it = par_metrics_.begin();
@@ -125,6 +131,17 @@ pair<pit_type, ParagraphMetrics const *> TextMetrics::first() const
 }
 
 
+pair<pit_type, ParagraphMetrics const *> TextMetrics::firstVisible() const
+{
+       // This only works in the main text, I think (bottom > 0)
+       LASSERT(text_->isMainText(), return first());
+       auto it = find_if(par_metrics_.begin(), par_metrics_.end(),
+                         [] (ParMetricsCache::value_type const & p) {
+                             return p.second.hasPosition() && p.second.bottom() > 0;
+                         });
+       return make_pair(it->first, &it->second);
+}
+
 pair<pit_type, ParagraphMetrics const *> TextMetrics::last() const
 {
        LBUFERR(!par_metrics_.empty());
@@ -210,6 +227,60 @@ void TextMetrics::newParMetricsUp()
 }
 
 
+void TextMetrics::updateMetrics(pit_type const anchor_pit, int const anchor_ypos,
+                                int const bv_height)
+{
+       LASSERT(text_->isMainText(), return);
+
+       // Forget existing positions
+       for (auto & pm_pair : par_metrics_)
+               pm_pair.second.resetPosition();
+
+       if (!contains(anchor_pit))
+               // Rebreak anchor paragraph.
+               redoParagraph(anchor_pit);
+       ParagraphMetrics & anchor_pm = parMetrics(anchor_pit);
+       anchor_pm.setPosition(anchor_ypos);
+
+       // Redo paragraphs above anchor if necessary.
+       int y1 = anchor_ypos - anchor_pm.ascent();
+       // We are now just above the anchor paragraph.
+       pit_type pit1 = anchor_pit - 1;
+       for (; pit1 >= 0 && y1 > 0; --pit1) {
+               if (!contains(pit1))
+                       redoParagraph(pit1);
+               ParagraphMetrics & pm = parMetrics(pit1);
+               y1 -= pm.descent();
+               // Save the paragraph position in the cache.
+               pm.setPosition(y1);
+               y1 -= pm.ascent();
+       }
+
+       // Redo paragraphs below the anchor if necessary.
+       int y2 = anchor_ypos + anchor_pm.descent();
+       // We are now just below the anchor paragraph.
+       pit_type pit2 = anchor_pit + 1;
+       pit_type const npit = pit_type(text_->paragraphs().size());
+       for (; pit2 < npit && y2 < bv_height; ++pit2) {
+               if (!contains(pit2))
+                       redoParagraph(pit2);
+               ParagraphMetrics & pm = parMetrics(pit2);
+               y2 += pm.ascent();
+               // Save the paragraph position in the cache.
+               pm.setPosition(y2);
+               y2 += pm.descent();
+       }
+
+       LYXERR(Debug::PAINTING, "TextMetrics::updateMetrics "
+               << " anchor pit = " << anchor_pit
+               << " anchor ypos = " << anchor_ypos
+               << " y1 = " << y1
+               << " y2 = " << y2
+               << " pit1 = " << pit1
+               << " pit2 = " << pit2);
+}
+
+
 bool TextMetrics::metrics(MetricsInfo const & mi, Dimension & dim, int min_width)
 {
        LBUFERR(mi.base.textwidth > 0);
@@ -1491,8 +1562,8 @@ pit_type TextMetrics::getPitNearY(int y)
        }
 
        for (; it != et; ++it) {
-               LYXERR(Debug::PAINTING, "examining: pit: " << it->first
-                       << " y: " << it->second.position());
+               // LYXERR(Debug::PAINTING, "examining: pit: " << it->first
+               //      << " y: " << it->second.position());
 
                if (it->first >= pit && it->second.top() <= y) {
                        pit = it->first;
@@ -1994,8 +2065,14 @@ void TextMetrics::drawParagraph(PainterInfo & pi, pit_type const pit, int const
        size_t const nrows = pm.rows().size();
        int const wh = bv_->workHeight();
        // Remember left and right margin for drawing math numbers
-       Changer changeleft = changeVar(pi.leftx, x + leftMargin(pit));
-       Changer changeright = changeVar(pi.rightx, x + width() - rightMargin(pit));
+       Changer changeleft, changeright;
+       if (text_->isRTL(pit)) {
+               changeleft = changeVar(pi.leftx, x + rightMargin(pit));
+               changeright = changeVar(pi.rightx, x + width() - leftMargin(pit));
+       } else {
+               changeleft = changeVar(pi.leftx, x + leftMargin(pit));
+               changeright = changeVar(pi.rightx, x + width() - rightMargin(pit));
+       }
 
        // Use fast lane in nodraw stage.
        if (pi.pain.isNull()) {
@@ -2044,9 +2121,6 @@ void TextMetrics::drawParagraph(PainterInfo & pi, pit_type const pit, int const
                }
        }
 
-       if (text_->isRTL(pit))
-               swap(pi.leftx, pi.rightx);
-
        BookmarksSection::BookmarkPosList bpl =
                theSession().bookmarks().bookmarksInPar(bv_->buffer().fileName(), pm.id());