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(this)->parMetrics(pit, true); -} - - -ParagraphMetrics & TextMetrics::parMetrics(pit_type pit) -{ - return parMetrics(pit, true); + return const_cast(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;