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.
This commit is contained in:
Jean-Marc Lasgouttes 2025-01-09 15:46:44 +01:00
parent b1e4a11767
commit b4b27b4800
2 changed files with 27 additions and 20 deletions

View File

@ -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);

View File

@ -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;