commit d8719ade592599a2b2ec6c4d54a76206773584cd
Author: Jean-Marc Lasgouttes <[email protected]>
Date:   Fri Oct 23 11:16:21 2015 +0200

    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 rewritten to include some code that was in 
computeRowMetrics.

diff --git a/src/TextMetrics.cpp b/src/TextMetrics.cpp
index cd7beb2..1a033d5 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,14 @@ 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:
+               // In general block align is the default state, but here it is
+               // an explicit choice. Therefore it should not be overridden
+               // later.
+               forced_block = true;
+               // fall through
        case LYX_ALIGN_CENTER:
        case LYX_ALIGN_LEFT:
        case LYX_ALIGN_RIGHT:
@@ -511,7 +518,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 +535,17 @@ 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.
+               // A forced block alignment can only be overridden the 'no
+               // justification on screen' setting.
+               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 +595,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;

Reply via email to