Le 23/10/2015 21:47, Georg Baum a écrit :
Jean-Marc Lasgouttes wrote:

Actually fixing this for LR boxes (so-called makebox in the code) is not
difficult because we have the framework for table cells already. I
propose to put that in master. Would it be OK?

I did not know that mechanism. This looks indeed nice. However, I wonder
whether the condition for ignoring hor_pos should be the same as the one
used for LaTeX output. When looking at your patch I could not see whether
both places are equivalent. Anyway if this is sorted out I think that it
would be a very nice improvemrnt and should go in.

This new version of the patch is also able to handle 'stretch' alignment.

Concerning LaTeX output, hor_pos 'c' value is obtained by omitting alignment parameter. I guess one can specify 'c' explicitly, though. For screen layout, we always want to specify some alignment, so we always respect hor_pos.

Does this answer your question?

JMarc



>From 45e79e156075762f0e68b978c90fb6ae1765d935 Mon Sep 17 00:00:00 2001
From: Jean-Marc Lasgouttes <lasgout...@lyx.org>
Date: Fri, 23 Oct 2015 11:16:21 +0200
Subject: [PATCH] Implement on screen rendering of alignment in LR boxes.

This is done by implementing contentAlignment() for this inset.

In order to display properly 'stretch' alignment, the code for TextMetrics::getAlign is refactored to include some code that was in computeRowMetrics.
---
 src/TextMetrics.cpp     | 30 ++++++++++++++++++------------
 src/TextMetrics.h       |  4 ++--
 src/insets/InsetBox.cpp | 26 ++++++++++++++++++++++++++
 src/insets/InsetBox.h   |  2 ++
 4 files changed, 48 insertions(+), 14 deletions(-)

diff --git a/src/TextMetrics.cpp b/src/TextMetrics.cpp
index cd7beb2..50eba5b 100644
--- a/src/TextMetrics.cpp
+++ b/src/TextMetrics.cpp
@@ -486,7 +486,7 @@ bool TextMetrics::redoParagraph(pit_type const pit)
 }
 
 
-LyXAlignment TextMetrics::getAlign(Paragraph const & par, pos_type const pos) const
+LyXAlignment TextMetrics::getAlign(Paragraph const & par, Row const & row) const
 {
 	Layout const & layout = par.layout();
 
@@ -498,7 +498,11 @@ LyXAlignment TextMetrics::getAlign(Paragraph const & par, pos_type const pos) co
 
 	// handle alignment inside tabular cells
 	Inset const & owner = text_->inset();
+	bool forced_block = false;
 	switch (owner.contentAlignment()) {
+	case LYX_ALIGN_BLOCK:
+		forced_block = true;
+		// fall through
 	case LYX_ALIGN_CENTER:
 	case LYX_ALIGN_LEFT:
 	case LYX_ALIGN_RIGHT:
@@ -511,7 +515,7 @@ LyXAlignment TextMetrics::getAlign(Paragraph const & par, pos_type const pos) co
 	}
 
 	// Display-style insets should always be on a centered row
-	if (Inset const * inset = par.getInset(pos)) {
+	if (Inset const * inset = par.getInset(row.pos())) {
 		switch (inset->display()) {
 		case Inset::AlignLeft:
 			align = LYX_ALIGN_BLOCK;
@@ -528,10 +532,15 @@ LyXAlignment TextMetrics::getAlign(Paragraph const & par, pos_type const pos) co
 		}
 	}
 
-	// Has the user requested we not justify stuff?
-	if (!bv_->buffer().params().justification
-	    && align == LYX_ALIGN_BLOCK)
-		align = LYX_ALIGN_LEFT;
+	if (align == LYX_ALIGN_BLOCK) {
+		// If this row has been broken abruptly by a display inset, or
+		// it is the end of the paragraph, or the user requested we
+		// not justify stuff, then don't stretch.
+		if (((row.right_boundary() || row.endpos() == par.size())
+		     && !forced_block)
+		    || !bv_->buffer().params().justification)
+			align = text_->isRTL(par) ? LYX_ALIGN_RIGHT : LYX_ALIGN_LEFT;
+	}
 
 	return align;
 }
@@ -581,14 +590,11 @@ void TextMetrics::computeRowMetrics(pit_type const pit,
 	} else if (int(row.width()) < max_width_) {
 		// is it block, flushleft or flushright?
 		// set x how you need it
-		switch (getAlign(par, row.pos())) {
+		switch (getAlign(par, row)) {
 		case LYX_ALIGN_BLOCK: {
 			int const ns = row.countSeparators();
-			/** If we have separators, and this row has
-			 * not be broken abruptly by a display inset
-			 * or newline, then stretch it */
-			if (ns && !row.right_boundary()
-			    && row.endpos() != par.size()) {
+			// If we have separators, then stretch the row
+			if (ns) {
 				row.setSeparatorExtraWidth(double(w) / ns);
 				row.dimension().wid += w;
 			} else if (text_->isRTL(par)) {
diff --git a/src/TextMetrics.h b/src/TextMetrics.h
index f100cb8..f0abcb5 100644
--- a/src/TextMetrics.h
+++ b/src/TextMetrics.h
@@ -136,8 +136,8 @@ private:
 	/// for example, the pos after which isNewLine(pos) == true
 	void breakRow(Row & row, int right_margin, pit_type const pit) const;
 
-	// Expand the alignment of paragraph \param par at position \param pos
-	LyXAlignment getAlign(Paragraph const & par, pos_type pos) const;
+	// Expand the alignment of row \param row in paragraph \param par
+	LyXAlignment getAlign(Paragraph const & par, Row const & row) const;
 	/** this calculates the specified parameters. needed when setting
 	 * the cursor and when creating a visible row */
 	void computeRowMetrics(pit_type pit, Row & row, int width) const;
diff --git a/src/insets/InsetBox.cpp b/src/insets/InsetBox.cpp
index bfa188e..fd926f4 100644
--- a/src/insets/InsetBox.cpp
+++ b/src/insets/InsetBox.cpp
@@ -223,6 +223,31 @@ ColorCode InsetBox::backgroundColor(PainterInfo const &) const
 }
 
 
+LyXAlignment InsetBox::contentAlignment() const
+{
+	if (!params_.use_makebox)
+		return LYX_ALIGN_NONE;
+
+	// The default value below is actually irrelevant
+	LyXAlignment align = LYX_ALIGN_NONE;
+	switch (params_.hor_pos) {
+	case 'l':
+		align = LYX_ALIGN_LEFT;
+		break;
+	case 'c':
+		align = LYX_ALIGN_CENTER;
+		break;
+	case 'r':
+		align = LYX_ALIGN_RIGHT;
+		break;
+	case 's':
+		align = LYX_ALIGN_BLOCK;
+		break;
+	}
+	return align;
+}
+
+
 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
 	switch (cmd.action()) {
@@ -380,6 +405,7 @@ void InsetBox::latex(otexstream & os, OutputParams const & runparams) const
 			} else
 				os << '[' << from_ascii(width_string)
 				   << ']';
+			// default horizontal alignment is 'c'
 			if (params_.hor_pos != 'c')
 				os << "[" << params_.hor_pos << "]";
 		} else {
diff --git a/src/insets/InsetBox.h b/src/insets/InsetBox.h
index 877dd60..151d622 100644
--- a/src/insets/InsetBox.h
+++ b/src/insets/InsetBox.h
@@ -115,6 +115,8 @@ public:
 	///
 	ColorCode backgroundColor(PainterInfo const &) const;
 	///
+	LyXAlignment contentAlignment() const;
+	///
 	bool allowParagraphCustomization(idx_type = 0) const { return !forcePlainLayout(); }
 	///
 	bool allowMultiPar() const;
-- 
2.5.0

Reply via email to