[LyX/master] Prevent insets in table cells from expanding artificially to max width

Jean-Marc Lasgouttes lasgouttes at lyx.org
Mon May 31 12:44:36 UTC 2021


commit 5e396c3f0cc16b65db6c1623b249b75002c5edbf
Author: Jean-Marc Lasgouttes <lasgouttes at lyx.org>
Date:   Tue Jul 14 23:28:43 2020 +0200

    Prevent insets in table cells from expanding artificially to max width
    
    This replaces ad-hoc hacks and does a better job by propagating the
    the tightness recursively.
    
    Fixes bug #9363.
---
 src/MetricsInfo.cpp         |    4 ++--
 src/MetricsInfo.h           |    4 +++-
 src/TextMetrics.cpp         |   23 +++++++++--------------
 src/TextMetrics.h           |    7 ++++---
 src/insets/InsetTabular.cpp |    8 +++++---
 5 files changed, 23 insertions(+), 23 deletions(-)

diff --git a/src/MetricsInfo.cpp b/src/MetricsInfo.cpp
index 9bab1a3..24656ed 100644
--- a/src/MetricsInfo.cpp
+++ b/src/MetricsInfo.cpp
@@ -120,8 +120,8 @@ int MetricsBase::inPixels(Length const & len) const
 /////////////////////////////////////////////////////////////////////////
 
 MetricsInfo::MetricsInfo(BufferView * bv, FontInfo font, int textwidth,
-                         MacroContext const & mc, bool vm)
-	: base(bv, font, textwidth), macrocontext(mc), vmode(vm)
+                         MacroContext const & mc, bool vm, bool tight)
+	: base(bv, font, textwidth), macrocontext(mc), vmode(vm), tight_insets(tight)
 {}
 
 
diff --git a/src/MetricsInfo.h b/src/MetricsInfo.h
index 14fd026..bfd9d07 100644
--- a/src/MetricsInfo.h
+++ b/src/MetricsInfo.h
@@ -95,7 +95,7 @@ public:
 	MetricsInfo();
 	///
 	MetricsInfo(BufferView * bv, FontInfo font, int textwidth,
-	            MacroContext const & mc, bool vm);
+	            MacroContext const & mc, bool vm, bool tight_insets);
 
 	///
 	MetricsBase base;
@@ -103,6 +103,8 @@ public:
 	MacroContext const & macrocontext;
 	/// Are we at the start of a paragraph (vertical mode)?
 	bool vmode;
+	/// if true, do not expand insets to max width artificially
+	bool tight_insets;
 };
 
 
diff --git a/src/TextMetrics.cpp b/src/TextMetrics.cpp
index 0b3bfc1..b0d1ff0 100644
--- a/src/TextMetrics.cpp
+++ b/src/TextMetrics.cpp
@@ -106,14 +106,9 @@ int numberOfHfills(Row const & row, ParagraphMetrics const & pm,
 
 
 TextMetrics::TextMetrics(BufferView * bv, Text * text)
-	: bv_(bv), text_(text)
-{
-	LBUFERR(bv_);
-	max_width_ = bv_->workWidth();
-	dim_.wid = max_width_;
-	dim_.asc = 10;
-	dim_.des = 10;
-}
+	: bv_(bv), text_(text), dim_(bv_->workWidth(), 10, 10),
+	  max_width_(dim_.wid), tight_(false)
+{}
 
 
 bool TextMetrics::contains(pit_type pit) const
@@ -216,18 +211,18 @@ void TextMetrics::newParMetricsUp()
 }
 
 
-bool TextMetrics::metrics(MetricsInfo const & mi, Dimension & dim, int min_width,
-			  bool const expand_on_multipars)
+bool TextMetrics::metrics(MetricsInfo const & mi, Dimension & dim, int min_width)
 {
 	LBUFERR(mi.base.textwidth > 0);
 	max_width_ = mi.base.textwidth;
+	tight_ = mi.tight_insets;
 	// backup old dimension.
 	Dimension const old_dim = dim_;
 	// reset dimension.
 	dim_ = Dimension();
 	dim_.wid = min_width;
 	pit_type const npar = text_->paragraphs().size();
-	if (npar > 1 && expand_on_multipars)
+	if (npar > 1 && !tight_)
 		// If there is more than one row, expand the text to
 		// the full allowable width.
 		dim_.wid = max_width_;
@@ -512,7 +507,7 @@ bool TextMetrics::redoParagraph(pit_type const pit, bool const align_rows)
 		Font const & font = e.inset->inheritFont() ?
 			displayFont(pit, e.pos) : bufferfont;
 		MacroContext mc(&buffer, parPos);
-		MetricsInfo mi(bv_, font.fontInfo(), w, mc, e.pos == 0);
+		MetricsInfo mi(bv_, font.fontInfo(), w, mc, e.pos == 0, tight_);
 		e.inset->metrics(mi, dim);
 		if (!insetCache.has(e.inset) || insetCache.dim(e.inset) != dim) {
 			insetCache.add(e.inset, dim);
@@ -536,12 +531,12 @@ bool TextMetrics::redoParagraph(pit_type const pit, bool const align_rows)
 		setRowHeight(row);
 		row.changed(true);
 		if ((row_index || row.endpos() < par.size() || row.right_boundary())
-		    && par.inInset().lyxCode() != CELL_CODE) {
+		    && !tight_) {
 			/* If there is more than one row or the row has been
 			 * broken by a display inset or a newline, expand the text
 			 * to the full allowable width. This setting here is
 			 * needed for the setRowAlignment() below.
-			 * We do nothing when inside a table cell.
+			 * We do nothing when tight insets are requested.
 			 */
 			if (dim_.wid < max_width_)
 				dim_.wid = max_width_;
diff --git a/src/TextMetrics.h b/src/TextMetrics.h
index 0ae802a..1501250 100644
--- a/src/TextMetrics.h
+++ b/src/TextMetrics.h
@@ -39,7 +39,7 @@ class TextMetrics
 	void operator=(TextMetrics const &);
 public:
 	/// Default constructor (only here for STL containers).
-	TextMetrics() : bv_(0), text_(0), max_width_(0) {}
+	TextMetrics() : bv_(0), text_(0), max_width_(0), tight_(false) {}
 	/// The only useful constructor.
 	TextMetrics(BufferView *, Text *);
 
@@ -72,8 +72,7 @@ public:
 	void newParMetricsUp();
 
 	/// compute text metrics.
-	bool metrics(MetricsInfo const & mi, Dimension & dim, int min_width = 0,
-		     bool const expand_on_multipars = true);
+	bool metrics(MetricsInfo const & mi, Dimension & dim, int min_width = 0);
 
 	/// The "nodraw" drawing stage for one single paragraph: set the
 	/// positions of the insets contained in this paragraph in metrics
@@ -257,6 +256,8 @@ private:
 	mutable ParMetricsCache par_metrics_;
 	Dimension dim_;
 	int max_width_;
+	/// if true, do not expand insets to max width artificially
+	bool tight_;
 	mutable Point origin_;
 
 // temporary public:
diff --git a/src/insets/InsetTabular.cpp b/src/insets/InsetTabular.cpp
index 647f6d6..d6d9c57 100644
--- a/src/insets/InsetTabular.cpp
+++ b/src/insets/InsetTabular.cpp
@@ -57,6 +57,7 @@
 #include "frontends/Painter.h"
 #include "frontends/Selection.h"
 
+#include "support/Changer.h"
 #include "support/convert.h"
 #include "support/debug.h"
 #include "support/docstream.h"
@@ -4341,10 +4342,11 @@ void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
 
 	// We tell metrics here not to expand on multiple pars
 	// This is the difference to InsetText::Metrics
-	if (hasFixedWidth() || isVarwidth)
-		tm.metrics(mi, dim, mi.base.textwidth, false);
+	Changer changetight = changeVar(mi.tight_insets, true);
+	if (hasFixedWidth())
+		tm.metrics(mi, dim, mi.base.textwidth);
 	else
-		tm.metrics(mi, dim, 0, false);
+		tm.metrics(mi, dim, 0);
 	mi.base.textwidth += horiz_offset;
 	dim.asc += topOffset(mi.base.bv);
 	dim.des += bottomOffset(mi.base.bv);


More information about the lyx-cvs mailing list