]> git.lyx.org Git - features.git/blobdiff - src/rowpainter.cpp
Merge remote-tracking branch 'features/str-metrics'
[features.git] / src / rowpainter.cpp
index 6dc975f8a96b19ee4ddbc034eec34c1ad0df5cc5..bb2535f66c810dedaa4a7600ca962012e0b7a08f 100644 (file)
@@ -164,49 +164,26 @@ void RowPainter::paintChars(pos_type & vpos, Font const & font)
 {
        // This method takes up 70% of time when typing
        pos_type pos = bidi_.vis2log(vpos);
+       pos_type start_pos = pos;
        // first character
-       char_type c = par_.getChar(pos);
        docstring str;
        str.reserve(100);
-
-       // special case for arabic
-       string const & lang = font.language()->lang();
-       bool const swap_paren = lang == "arabic_arabtex"
-               || lang == "arabic_arabi"
-               || lang == "farsi";
-
-       // FIXME: Why only round brackets and why the difference to
-       // Hebrew? See also Paragraph::getUChar
-       if (swap_paren) {
-               if (c == '(')
-                       c = ')';
-               else if (c == ')')
-                       c = '(';
-       }
+       char_type const c = par_.getChar(pos);
        str.push_back(c);
 
-       pos_type const end = row_.endpos();
        FontSpan const font_span = par_.fontSpan(pos);
        // Track-change status.
        Change const & change_running = par_.lookupChange(pos);
-
-       // selected text?
-       bool const selection = (pos >= row_.sel_beg && pos < row_.sel_end)
-               || pi_.selected;
-
        // spelling correct?
        bool const spell_state =
                lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
 
        // collect as much similar chars as we can
+       pos_type const end = row_.endpos();
        for (++vpos ; vpos < end ; ++vpos) {
                pos = bidi_.vis2log(vpos);
-               if (pos < font_span.first || pos > font_span.last)
-                       break;
 
-               bool const new_selection = pos >= row_.sel_beg && pos < row_.sel_end;
-               if (new_selection != selection)
-                       // Selection ends or starts here.
+               if (!font_span.inside(pos))
                        break;
 
                bool const new_spell_state =
@@ -220,27 +197,30 @@ void RowPainter::paintChars(pos_type & vpos, Font const & font)
                        // Track change type or author has changed.
                        break;
 
-               char_type c = par_.getChar(pos);
+               char_type const c = par_.getChar(pos);
 
                if (c == '\t')
                        break;
 
-               if (!isPrintableNonspace(c)
-                   && (c != ' ' || row_.separator > 0))
+               if (!isPrintableNonspace(c))
                        break;
 
-               // FIXME: Why only round brackets and why the difference to
-               // Hebrew? See also Paragraph::getUChar
-               if (swap_paren) {
-                       if (c == '(')
-                               c = ')';
-                       else if (c == ')')
-                               c = '(';
-               }
-
                str.push_back(c);
        }
 
+       // Make pos point to the last character in the string.
+       // Using "pos = bidi_.vis2log(vpos)" does not work for some reason.
+       if (vpos < end)
+               pos = bidi_.vis2log(vpos - 1);
+
+       // Now make pos point to the position _after_ the string.
+       // Using vis2log for that is not a good idea in general, we
+       // want logical ordering.
+       if (font.isVisibleRightToLeft())
+               --pos;
+       else
+               ++pos;
+
        if (str[0] == '\t')
                str.replace(0,1,from_ascii("    "));
 
@@ -253,30 +233,36 @@ void RowPainter::paintChars(pos_type & vpos, Font const & font)
         * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
         * for an earlier thread on the subject
         */
-       // Left-to-right override: forces to draw text left-to-right
-       char_type const LRO = 0x202D;
-       // Right-to-left override: forces to draw text right-to-left
-       char_type const RLO = 0x202E;
-       // Pop directional formatting: return to previous state
-       char_type const PDF = 0x202C;
        if (font.isVisibleRightToLeft()) {
                reverse(str.begin(), str.end());
-               str = RLO + str + PDF;
-       } else
-               str = LRO + str + PDF;
+               // If the string is reversed, the positions need to be adjusted
+               ++pos;
+               ++start_pos;
+               swap(start_pos, pos);
+       } 
+
+       // at least part of text selected?
+       bool const some_sel = (pos >= row_.sel_beg && start_pos < row_.sel_end)
+               || pi_.selected;
+       // all the text selected?
+       bool const all_sel = (start_pos >= row_.sel_beg && pos < row_.sel_end)
+               || pi_.selected;
 
-       if (!selection && !change_running.changed()) {
-               x_ += pi_.pain.text(int(x_), yo_, str, font.fontInfo());
-               return;
+       if (all_sel) {
+               Font copy = font;
+               copy.fontInfo().setPaintColor(Color_selectiontext);
+               x_ += pi_.pain.text(int(x_), yo_, str, copy);
+       } else if (change_running.changed()) {
+               Font copy = font;
+               copy.fontInfo().setPaintColor(change_running.color());
+               x_ += pi_.pain.text(int(x_), yo_, str, copy);
+       } else if (!some_sel) {
+               x_ += pi_.pain.text(int(x_), yo_, str, font);
+       } else {
+               x_ += pi_.pain.text(int(x_), yo_, str, font, Color_selectiontext,
+                                   max(row_.sel_beg, start_pos) - start_pos,
+                                   min(row_.sel_end, pos) - start_pos);
        }
-
-       FontInfo copy = font.fontInfo();
-       if (change_running.changed())
-               copy.setPaintColor(change_running.color());
-       else if (selection)
-               copy.setPaintColor(Color_selectiontext);
-
-       x_ += pi_.pain.text(int(x_), yo_, str, copy);
 }
 
 
@@ -692,6 +678,7 @@ void RowPainter::paintOnlyInsets()
 
 void RowPainter::paintText()
 {
+       //LYXERR0("-------------------------------------------------------");
        pos_type const end = row_.endpos();
        // Spaces at logical line breaks in bidi text must be skipped during
        // painting. However, they may appear visually in the middle
@@ -749,23 +736,21 @@ void RowPainter::paintText()
                }
 
                // Use font span to speed things up, see above
-               if (vpos < font_span.first || vpos > font_span.last) {
-                       font_span = par_.fontSpan(vpos);
-                       font = text_metrics_.displayFont(pit_, vpos);
+               if (!font_span.inside(pos)) {
+                       font_span = par_.fontSpan(pos);
+                       font = text_metrics_.displayFont(pit_, pos);
 
                        // split font span if inline completion is inside
-                       if (font_span.first <= inlineCompletionVPos
-                           && font_span.last > inlineCompletionVPos)
-                               font_span.last = inlineCompletionVPos;
+                       if (inlineCompletionVPos != -1
+                           && font_span.inside(inlineCompletionPos.pos()))
+                               font_span.last = inlineCompletionPos.pos();
                }
 
+               // Note that this value will only be used in
+               // situations where no ligature of composition of
+               // characters is needed. (see comments in uses of width_pos).
                const int width_pos = pm_.singleWidth(pos, font);
 
-               if (x_ + width_pos < 0) {
-                       x_ += width_pos;
-                       ++vpos;
-                       continue;
-               }
                Change const & change = par_.lookupChange(pos);
                if (change.changed() && !change_running.changed()) {
                        change_running = change;
@@ -800,6 +785,7 @@ void RowPainter::paintText()
                        int const lwidth = theFontMetrics(labelFont())
                                .width(layout.labelsep);
 
+                       // width_pos is either the width of a space or an inset
                        x_ += row_.label_hfill + lwidth - width_pos;
                }
 
@@ -810,6 +796,7 @@ void RowPainter::paintText()
                if (par_.isSeparator(pos)) {
                        Font const orig_font = text_metrics_.displayFont(pit_, pos);
                        double const orig_x = x_;
+                       // width_pos is the width of a space
                        double separator_width = width_pos;
                        if (pos >= body_pos)
                                separator_width += row_.separator;