]> git.lyx.org Git - lyx.git/blobdiff - src/TextMetrics.cpp
Remove hardcoded values
[lyx.git] / src / TextMetrics.cpp
index 66e100b2d90202684ed5317f2cdd3eaaf24be116..17ee2e410afe56b0c3325953cef3a82cb1d251ef 100644 (file)
@@ -48,6 +48,7 @@
 #include "support/debug.h"
 #include "support/lassert.h"
 
+#include <stdlib.h>
 #include <cmath>
 
 using namespace std;
@@ -401,6 +402,7 @@ bool TextMetrics::redoParagraph(pit_type const pit)
        }
 
        // redo insets
+       par.setBeginOfBody();
        Font const bufferfont = buffer.params().getFont();
        CoordCache::Insets & insetCache = bv_->coordCache().insets();
        InsetList::const_iterator ii = par.insetList().begin();
@@ -434,7 +436,6 @@ bool TextMetrics::redoParagraph(pit_type const pit)
                }
        }
 
-       par.setBeginOfBody();
        pos_type first = 0;
        size_t row_index = 0;
        bool need_new_row = false;
@@ -481,6 +482,11 @@ bool TextMetrics::redoParagraph(pit_type const pit)
        if (text_->isMainText()) {
                if (pit == 0) {
                        pm.rows().front().dimension().asc += 20;
+                       /* coverity[copy_paste_error]: coverity thinks that we
+                        * should update pm.dim().asc below, but all the rows
+                        * heights are actually counted as part of the paragraph metric
+                        * descent see loop above).
+                        */
                        pm.dim().des += 20;
                }
                ParagraphList const & pars = text_->paragraphs();
@@ -619,18 +625,14 @@ void TextMetrics::computeRowMetrics(Row & row, int width) const
                // Common case : there is no hfill, and the alignment will be
                // meaningful
                switch (getAlign(par, row)) {
-               case LYX_ALIGN_BLOCK: {
-                       int const ns = row.countSeparators();
-                       // If we have separators, then stretch the row
-                       if (ns) {
-                               row.setSeparatorExtraWidth(double(w) / ns);
-                               row.dimension().wid += w;
-                       } else if (text_->isRTL(par)) {
+               case LYX_ALIGN_BLOCK:
+                       // Expand expanding characters by a total of w
+                       if (!row.setExtraWidth(w) && text_->isRTL(par)) {
+                               // Justification failed and the text is RTL: align to the right
                                row.left_margin += w;
                                row.dimension().wid += w;
                        }
                        break;
-               }
                case LYX_ALIGN_RIGHT:
                        row.left_margin += w;
                        row.dimension().wid += w;
@@ -649,6 +651,7 @@ void TextMetrics::computeRowMetrics(Row & row, int width) const
                return;
        }
 
+       // Case nh > 0. There are hfill separators.
        hfill = w / nh;
        hfill_rem = w % nh;
        row.dimension().wid += w;
@@ -861,7 +864,23 @@ bool TextMetrics::breakRow(Row & row, int const right_margin) const
                } else if (c == '\t')
                        row.addSpace(i, theFontMetrics(*fi).width(from_ascii("    ")),
                                     *fi, par.lookupChange(i));
-               else {
+               else if (c == 0x2028 || c == 0x2029) {
+                       /**
+                        * U+2028 LINE SEPARATOR
+                        * U+2029 PARAGRAPH SEPARATOR
+
+                        * These are special unicode characters that break
+                        * lines/pragraphs. Not handling them lead to trouble wrt
+                        * Qt QTextLayout formatting. We add a visible character
+                        * on screen so that the user can see that something is
+                        * happening.
+                       */
+                       row.finalizeLast();
+                       // ⤶ U+2936 ARROW POINTING DOWNWARDS THEN CURVING LEFTWARDS
+                       // ¶ U+00B6 PILCROW SIGN
+                       char_type const screen_char = (c == 0x2028) ? 0x2936 : 0x00B6;
+                       row.add(i, screen_char, *fi, par.lookupChange(i));
+               } else {
                        // FIXME: please someone fix the Hebrew/Arabic parenthesis mess!
                        // see also Paragraph::getUChar.
                        if (fi->language()->lang() == "hebrew") {
@@ -922,13 +941,14 @@ bool TextMetrics::breakRow(Row & row, int const right_margin) const
                BufferParams const & bparams
                        = text_->inset().buffer().params();
                f.setLanguage(par.getParLanguage(bparams));
+               // ¶ U+00B6 PILCROW SIGN
                row.addVirtual(end, docstring(1, char_type(0x00B6)), f, Change());
        }
 
        // if the row is too large, try to cut at last separator. In case
        // of success, reset indication that the row was broken abruptly.
        if (row.shortenIfNeeded(body_pos, width))
-               row.right_boundary(false);
+               row.right_boundary(!row.empty() && row.back().endpos == row.endpos());
 
        // make sure that the RTL elements are in reverse ordering
        row.reverseRTL(is_rtl);