commit b4b27b48001ff0ce66f4232f2f421fea2ac36bb6
Author: Jean-Marc Lasgouttes <[email protected]>
Date:   Thu Jan 9 15:46:44 2025 +0100

    Remove parameter to TextMetrics::parMetrics, introduce dim(pit)
    
    Now, parMetrics always recomputes the metrics if they are not yet
    here. The same is (still) true for the const version, which is
    annoying.
    
    Introduce dim(pit) that allows to get the dimension, without
    recomputing if has not yet been done.
---
 src/TextMetrics.cpp | 30 +++++++++++++++++-------------
 src/TextMetrics.h   | 17 ++++++++++-------
 2 files changed, 27 insertions(+), 20 deletions(-)

diff --git a/src/TextMetrics.cpp b/src/TextMetrics.cpp
index 09712faf6c..ea9efe776d 100644
--- a/src/TextMetrics.cpp
+++ b/src/TextMetrics.cpp
@@ -163,14 +163,25 @@ void TextMetrics::setRowChanged(pit_type pit, pos_type 
pos)
 }
 
 
-ParagraphMetrics & TextMetrics::parMetrics(pit_type pit, bool redo)
+Dimension const & TextMetrics::dim(pit_type pit) const
 {
-       ParMetricsCache::iterator pmc_it = par_metrics_.find(pit);
+       auto pmc_it = par_metrics_.find(pit);
+       if (pmc_it == par_metrics_.end()) {
+               static Dimension empty_dim;
+               return empty_dim;
+       } else
+               return pmc_it->second.dim();
+}
+
+
+ParagraphMetrics & TextMetrics::parMetrics(pit_type pit)
+{
+       auto pmc_it = par_metrics_.find(pit);
        if (pmc_it == par_metrics_.end()) {
                pmc_it = par_metrics_.insert(
                        make_pair(pit, 
ParagraphMetrics(text_->getPar(pit)))).first;
        }
-       if (pmc_it->second.rows().empty() && redo)
+       if (pmc_it->second.rows().empty())
                redoParagraph(pit);
        return pmc_it->second;
 }
@@ -178,13 +189,7 @@ ParagraphMetrics & TextMetrics::parMetrics(pit_type pit, 
bool redo)
 
 ParagraphMetrics const & TextMetrics::parMetrics(pit_type pit) const
 {
-       return const_cast<TextMetrics *>(this)->parMetrics(pit, true);
-}
-
-
-ParagraphMetrics & TextMetrics::parMetrics(pit_type pit)
-{
-       return parMetrics(pit, true);
+       return const_cast<TextMetrics *>(this)->parMetrics(pit);
 }
 
 
@@ -484,9 +489,8 @@ bool TextMetrics::isRTLBoundary(pit_type pit, pos_type pos,
 bool TextMetrics::redoParagraph(pit_type const pit, bool const align_rows)
 {
        Paragraph & par = text_->getPar(pit);
-       // IMPORTANT NOTE: We pass 'false' explicitly in order to not call
-       // redoParagraph() recursively inside parMetrics.
-       Dimension old_dim = parMetrics(pit, false).dim();
+       // This gets the dimension if it exists and an empty one otherwise.
+       Dimension old_dim = dim(pit);
        ParagraphMetrics & pm = par_metrics_[pit];
        pm.reset(par);
 
diff --git a/src/TextMetrics.h b/src/TextMetrics.h
index 4f49f0fa7d..216b7014be 100644
--- a/src/TextMetrics.h
+++ b/src/TextMetrics.h
@@ -58,15 +58,20 @@ public:
        ///
        void setRowChanged(pit_type pit, pos_type pos);
 
-       ///
+       /// Dimension of the entire text.
        Dimension const & dim() const { return dim_; }
+       /// Dimension of paragraph \c pit if it exists, empty dimension
+       /// otherwise.
+       Dimension const & dim(pit_type pit) const;
        ///
        Point const & origin() const { return origin_; }
 
-       ///
-       ParagraphMetrics const & parMetrics(pit_type) const;
-       ///
-       ParagraphMetrics & parMetrics(pit_type);
+       /// Metrics of paragraph \c pit. If no metrics exist or if they
+       /// are empty, recompute metrics for this paragraph.
+       ParagraphMetrics & parMetrics(pit_type pit);
+       /// Identical to the non-const version
+       /// FIXME: a const method should not modify the object!
+       ParagraphMetrics const & parMetrics(pit_type pit) const;
 
        ///
        void newParMetricsDown();
@@ -156,8 +161,6 @@ public:
        void drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const;
 
 private:
-       ///
-       ParagraphMetrics & parMetrics(pit_type, bool redo_paragraph);
 
        /// the minimum space a manual label needs on the screen in pixels
        int labelFill(Row const & row) const;
-- 
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to