]> git.lyx.org Git - lyx.git/blob - src/RowPainter.cpp
Cleanup Painter text() API
[lyx.git] / src / RowPainter.cpp
1 /**
2  * \file RowPainter.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author various
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13 #include <algorithm>
14
15 #include "RowPainter.h"
16
17 #include "Buffer.h"
18 #include "CoordCache.h"
19 #include "Cursor.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
22 #include "Changes.h"
23 #include "Language.h"
24 #include "Layout.h"
25 #include "LyXRC.h"
26 #include "Row.h"
27 #include "MetricsInfo.h"
28 #include "Paragraph.h"
29 #include "ParagraphMetrics.h"
30 #include "ParagraphParameters.h"
31 #include "TextMetrics.h"
32 #include "VSpace.h"
33
34 #include "frontends/FontMetrics.h"
35 #include "frontends/Painter.h"
36
37 #include "insets/InsetText.h"
38
39 #include "mathed/InsetMath.h"
40
41 #include "support/debug.h"
42 #include "support/gettext.h"
43 #include "support/textutils.h"
44
45 #include "support/lassert.h"
46 #include <boost/crc.hpp>
47
48 using namespace std;
49
50 namespace lyx {
51
52 using frontend::Painter;
53 using frontend::FontMetrics;
54
55
56 RowPainter::RowPainter(PainterInfo & pi,
57         Text const & text, pit_type pit, Row const & row, int x, int y)
58         : pi_(pi), text_(text),
59           text_metrics_(pi_.base.bv->textMetrics(&text)),
60           pars_(text.paragraphs()),
61           row_(row), pit_(pit), par_(text.paragraphs()[pit]),
62           pm_(text_metrics_.parMetrics(pit)), change_(pi_.change_),
63           xo_(x), yo_(y), width_(text_metrics_.width()),
64           solid_line_thickness_(1), solid_line_offset_(1),
65           dotted_line_thickness_(1)
66 {
67         if (lyxrc.zoom >= 200) {
68                 // derive the line thickness from zoom factor
69                 // the zoom is given in percent
70                 // (increase thickness at 250%, 450% etc.)
71                 solid_line_thickness_ = (lyxrc.zoom + 50) / 200;
72                 // adjust line_offset_ too
73                 solid_line_offset_ = 1 + solid_line_thickness_ / 2;
74         }
75         if (lyxrc.zoom >= 100) {
76                 // derive the line thickness from zoom factor
77                 // the zoom is given in percent
78                 // (increase thickness at 150%, 250% etc.)
79                 dotted_line_thickness_ = (lyxrc.zoom + 50) / 100;
80         }
81
82         x_ = row_.left_margin + xo_;
83
84         //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
85         //row_.dump();
86
87         LBUFERR(pit >= 0);
88         LBUFERR(pit < int(text.paragraphs().size()));
89 }
90
91
92 FontInfo RowPainter::labelFont() const
93 {
94         FontInfo f = text_.labelFont(par_);
95         // selected text?
96         if (row_.begin_margin_sel || pi_.selected)
97                 f.setPaintColor(Color_selectiontext);
98         return f;
99 }
100
101
102 // If you want to debug inset metrics uncomment the following line:
103 //#define DEBUG_METRICS
104 // This draws green lines around each inset.
105
106
107 void RowPainter::paintInset(Row::Element const & e)
108 {
109         // Handle selection
110         bool const pi_selected = pi_.selected;
111         Cursor const & cur = pi_.base.bv->cursor();
112         if (cur.selection() && cur.text() == &text_
113                 && cur.normalAnchor().text() == &text_)
114                 pi_.selected = row_.sel_beg <= e.pos && row_.sel_end > e.pos;
115
116         LASSERT(e.inset, return);
117         // Backup full_repaint status because some insets (InsetTabular)
118         // requires a full repaint
119         bool const pi_full_repaint = pi_.full_repaint;
120         bool const pi_do_spellcheck = pi_.do_spellcheck;
121         Change const pi_change = pi_.change_;
122
123         pi_.base.font = e.inset->inheritFont() ? e.font.fontInfo() :
124                 pi_.base.bv->buffer().params().getFont().fontInfo();
125         pi_.ltr_pos = !e.font.isVisibleRightToLeft();
126         pi_.change_ = change_.changed() ? change_ : e.change;
127         pi_.do_spellcheck &= e.inset->allowSpellCheck();
128
129         int const x1 = int(x_);
130         pi_.base.bv->coordCache().insets().add(e.inset, x1, yo_);
131         // insets are painted completely. Recursive
132         // FIXME: it is wrong to completely paint the background
133         // if we want to do single row painting.
134         e.inset->drawBackground(pi_, x1, yo_);
135         e.inset->drawSelection(pi_, x1, yo_);
136         e.inset->draw(pi_, x1, yo_);
137
138         Dimension const & dim = pi_.base.bv->coordCache().insets().dim(e.inset);
139
140         paintForeignMark(x_, e.font.language(), dim.descent());
141
142         x_ += dim.width();
143
144         // Restore full_repaint status.
145         pi_.full_repaint = pi_full_repaint;
146         pi_.change_ = pi_change;
147         pi_.do_spellcheck = pi_do_spellcheck;
148         pi_.selected = pi_selected;
149
150 #ifdef DEBUG_METRICS
151         int const x2 = x1 + dim.wid;
152         int const y1 = yo_ + dim.des;
153         int const y2 = yo_ - dim.asc;
154         pi_.pain.line(x1, y1, x1, y2, Color_green);
155         pi_.pain.line(x1, y1, x2, y1, Color_green);
156         pi_.pain.line(x2, y1, x2, y2, Color_green);
157         pi_.pain.line(x1, y2, x2, y2, Color_green);
158 #endif
159 }
160
161
162 void RowPainter::paintForeignMark(double orig_x, Language const * lang, int desc) const
163 {
164         if (!lyxrc.mark_foreign_language)
165                 return;
166         if (lang == latex_language)
167                 return;
168         if (lang == pi_.base.bv->buffer().params().language)
169                 return;
170
171         int const y = yo_ + solid_line_offset_ + desc + solid_line_thickness_ / 2;
172         pi_.pain.line(int(orig_x), y, int(x_), y, Color_language,
173                 Painter::line_solid, solid_line_thickness_);
174 }
175
176
177 void RowPainter::paintMisspelledMark(double const orig_x,
178                                      Row::Element const & e) const
179 {
180         // if changed the misspelled marker gets placed slightly lower than normal
181         // to avoid drawing at the same vertical offset
182         FontMetrics const & fm = theFontMetrics(e.font);
183         int const thickness = max(fm.lineWidth(), 2);
184         int const y = yo_ + solid_line_offset_ + solid_line_thickness_
185                 + (e.change.changed() ? solid_line_thickness_ + 1 : 0)
186                 + 1 + thickness / 2;
187
188         //FIXME: this could be computed only once, it is probably not costly.
189         // check for cursor position
190         // don't draw misspelled marker for words at cursor position
191         // we don't want to disturb the process of text editing
192         DocIterator const nw = pi_.base.bv->cursor().newWord();
193         pos_type cpos = -1;
194         if (!nw.empty() && par_.id() == nw.paragraph().id()) {
195                 cpos = nw.pos();
196                 if (cpos > 0 && cpos == par_.size() && !par_.isWordSeparator(cpos-1))
197                         --cpos;
198                 else if (cpos > 0 && par_.isWordSeparator(cpos))
199                         --cpos;
200         }
201
202         pos_type pos = e.pos;
203         while (pos < e.pos + pos_type(e.str.length())) {
204                 if (!par_.isMisspelled(pos)) {
205                         ++pos;
206                         continue;
207                 }
208
209                 FontSpan const & range = par_.getSpellRange(pos);
210
211                 // Skip element which are being edited
212                 if (range.contains(cpos)) {
213                         // the range includes the last element
214                         pos = range.last + 1;
215                         continue;
216                 }
217
218                 FontMetrics const & fm = theFontMetrics(e.font);
219                 int x1 = fm.pos2x(e.str, range.first - e.pos,
220                                   e.isRTL(), e.extra);
221                 int x2 = fm.pos2x(e.str, min(range.last - e.pos + 1,
222                                                                          pos_type(e.str.length())),
223                                                                          e.isRTL(), e.extra);
224                 if (x1 > x2)
225                         swap(x1, x2);
226
227                 pi_.pain.line(int(orig_x) + x1, y, int(orig_x) + x2, y,
228                               Color_error,
229                               Painter::line_onoffdash, thickness);
230                 pos = range.last + 1;
231         }
232 }
233
234
235 void RowPainter::paintStringAndSel(Row::Element const & e)
236 {
237         // at least part of text selected?
238         bool const some_sel = (e.endpos >= row_.sel_beg && e.pos < row_.sel_end)
239                 || pi_.selected;
240         // all the text selected?
241         bool const all_sel = (e.pos >= row_.sel_beg && e.endpos < row_.sel_end)
242                 || pi_.selected;
243
244         if (all_sel || e.change.changed()) {
245                 Font copy = e.font;
246                 Color const col = e.change.changed() ? e.change.color()
247                                                      : Color_selectiontext;
248                 copy.fontInfo().setPaintColor(col);
249                 pi_.pain.text(int(x_), yo_, e.str, copy, e.extra, e.full_width());
250         } else if (!some_sel) {
251                 pi_.pain.text(int(x_), yo_, e.str, e.font, e.extra, e.full_width());
252         } else {
253                 pi_.pain.text(int(x_), yo_, e.str, e.font, Color_selectiontext,
254                               max(row_.sel_beg, e.pos) - e.pos,
255                               min(row_.sel_end, e.endpos) - e.pos,
256                               e.extra, e.full_width());
257         }
258         x_ += e.full_width();
259 }
260
261
262 void RowPainter::paintChange(double orig_x, Font const & font,
263                              Change const & change) const
264 {
265         if (!change.changed())
266                 return;
267         // Calculate 1/3 height of font
268         FontMetrics const & fm = theFontMetrics(font);
269         int const y_bar = change.deleted() ? yo_ - fm.maxAscent() / 3
270                 : yo_ + 2 * solid_line_offset_ + solid_line_thickness_;
271         pi_.pain.line(int(orig_x), y_bar, int(x_), y_bar,
272                       change.color(), Painter::line_solid, solid_line_thickness_);
273 }
274
275
276 void RowPainter::paintChangeBar() const
277 {
278         pos_type const start = row_.pos();
279         pos_type end = row_.endpos();
280
281         if (par_.size() == end) {
282                 // this is the last row of the paragraph;
283                 // thus, we must also consider the imaginary end-of-par character
284                 end++;
285         }
286
287         if (start == end || !par_.isChanged(start, end))
288                 return;
289
290         int const height = text_metrics_.isLastRow(pit_, row_)
291                 ? row_.ascent()
292                 : row_.height();
293
294         pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
295 }
296
297
298 void RowPainter::paintAppendix() const
299 {
300         // only draw the appendix frame once (for the main text)
301         if (!par_.params().appendix() || !text_.isMainText())
302                 return;
303
304         int y = yo_ - row_.ascent();
305
306         if (par_.params().startOfAppendix())
307                 y += 2 * defaultRowHeight();
308
309         pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
310         pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
311 }
312
313
314 void RowPainter::paintDepthBar() const
315 {
316         depth_type const depth = par_.getDepth();
317
318         if (depth <= 0)
319                 return;
320
321         depth_type prev_depth = 0;
322         if (!text_metrics_.isFirstRow(pit_, row_)) {
323                 pit_type pit2 = pit_;
324                 if (row_.pos() == 0)
325                         --pit2;
326                 prev_depth = pars_[pit2].getDepth();
327         }
328
329         depth_type next_depth = 0;
330         if (!text_metrics_.isLastRow(pit_, row_)) {
331                 pit_type pit2 = pit_;
332                 if (row_.endpos() >= pars_[pit2].size())
333                         ++pit2;
334                 next_depth = pars_[pit2].getDepth();
335         }
336
337         for (depth_type i = 1; i <= depth; ++i) {
338                 int const w = nestMargin() / 5;
339                 int x = int(xo_) + w * i;
340                 // only consider the changebar space if we're drawing outermost text
341                 if (text_.isMainText())
342                         x += changebarMargin();
343
344                 int const starty = yo_ - row_.ascent();
345                 int const h =  row_.height() - 1 - (i - next_depth - 1) * 3;
346
347                 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
348
349                 if (i > prev_depth)
350                         pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
351                 if (i > next_depth)
352                         pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
353         }
354 }
355
356
357 void RowPainter::paintAppendixStart(int y) const
358 {
359         FontInfo pb_font = sane_font;
360         pb_font.setColor(Color_appendix);
361         pb_font.decSize();
362
363         int w = 0;
364         int a = 0;
365         int d = 0;
366
367         docstring const label = _("Appendix");
368         theFontMetrics(pb_font).rectText(label, w, a, d);
369
370         int const text_start = int(xo_ + (width_ - w) / 2);
371         int const text_end = text_start + w;
372
373         pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
374
375         pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
376         pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
377 }
378
379
380 void RowPainter::paintTooLargeMarks(bool const left, bool const right) const
381 {
382         if (left)
383                 pi_.pain.line(dotted_line_thickness_, yo_ - row_.ascent(),
384                                           dotted_line_thickness_, yo_ + row_.descent(),
385                                           Color_scroll,
386                                           Painter::line_onoffdash, dotted_line_thickness_);
387         if (right) {
388                 int const wwidth = pi_.base.bv->workWidth() - dotted_line_thickness_;
389                 pi_.pain.line(wwidth, yo_ - row_.ascent(),
390                                           wwidth, yo_ + row_.descent(),
391                                           Color_scroll,
392                                           Painter::line_onoffdash, dotted_line_thickness_);
393         }
394 }
395
396
397 void RowPainter::paintFirst() const
398 {
399         Layout const & layout = par_.layout();
400
401         // start of appendix?
402         if (par_.params().startOfAppendix())
403             paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
404
405         bool const is_first =
406                 text_.isFirstInSequence(pit_) || !layout.isParagraphGroup();
407         //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
408
409         if (layout.labelIsInline()
410             && (layout.labeltype != LABEL_STATIC || is_first))
411                 paintLabel();
412         else if (is_first && layout.labelIsAbove())
413                 paintTopLevelLabel();
414 }
415
416
417 void RowPainter::paintLabel() const
418 {
419         docstring const str = par_.labelString();
420         if (str.empty())
421                 return;
422
423         bool const is_rtl = text_.isRTL(par_);
424         Layout const & layout = par_.layout();
425         FontInfo const font = labelFont();
426         FontMetrics const & fm = theFontMetrics(font);
427         double x = x_;
428
429         if (is_rtl)
430                 x = width_ - row_.right_margin + fm.width(layout.labelsep);
431         else
432                 x = x_ - fm.width(layout.labelsep) - fm.width(str);
433
434         pi_.pain.text(int(x), yo_, str, font);
435 }
436
437
438 void RowPainter::paintTopLevelLabel() const
439 {
440         BufferParams const & bparams = pi_.base.bv->buffer().params();
441         bool const is_rtl = text_.isRTL(par_);
442         ParagraphParameters const & pparams = par_.params();
443         Layout const & layout = par_.layout();
444         FontInfo const font = labelFont();
445         docstring const str = par_.labelString();
446         if (str.empty())
447                 return;
448
449         double spacing_val = 1.0;
450         if (!pparams.spacing().isDefault())
451                 spacing_val = pparams.spacing().getValue();
452         else
453                 spacing_val = bparams.spacing().getValue();
454
455         FontMetrics const & fm = theFontMetrics(font);
456
457         int const labeladdon = int(fm.maxHeight()
458                 * layout.spacing.getValue() * spacing_val);
459
460         int maxdesc =
461                 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
462                 + (layout.labelbottomsep * defaultRowHeight()));
463
464         double x = x_;
465         if (layout.labeltype == LABEL_CENTERED) {
466                 x = row_.left_margin + (width_ - row_.left_margin - row_.right_margin) / 2;
467                 x -= fm.width(str) / 2;
468         } else if (is_rtl) {
469                 x = width_ - row_.right_margin - fm.width(str);
470         }
471         pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
472 }
473
474
475 /** Check if the current paragraph is the last paragraph in a
476     proof environment */
477 static int getEndLabel(pit_type p, Text const & text)
478 {
479         ParagraphList const & pars = text.paragraphs();
480         pit_type pit = p;
481         depth_type par_depth = pars[p].getDepth();
482         while (pit != pit_type(pars.size())) {
483                 Layout const & layout = pars[pit].layout();
484                 int const endlabeltype = layout.endlabeltype;
485
486                 if (endlabeltype != END_LABEL_NO_LABEL) {
487                         if (p + 1 == pit_type(pars.size()))
488                                 return endlabeltype;
489
490                         depth_type const next_depth =
491                                 pars[p + 1].getDepth();
492                         if (par_depth > next_depth ||
493                             (par_depth == next_depth && layout != pars[p + 1].layout()))
494                                 return endlabeltype;
495                         break;
496                 }
497                 if (par_depth == 0)
498                         break;
499                 pit = text.outerHook(pit);
500                 if (pit != pit_type(pars.size()))
501                         par_depth = pars[pit].getDepth();
502         }
503         return END_LABEL_NO_LABEL;
504 }
505
506
507 void RowPainter::paintLast()
508 {
509         bool const is_rtl = text_.isRTL(par_);
510         int const endlabel = getEndLabel(pit_, text_);
511
512         // paint imaginary end-of-paragraph character
513
514         Change const & change = par_.lookupChange(par_.size());
515         if (change.changed()) {
516                 FontMetrics const & fm =
517                         theFontMetrics(pi_.base.bv->buffer().params().getFont());
518                 int const length = fm.maxAscent() / 2;
519                 Color col = change.color();
520
521                 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
522                            Painter::line_solid, 3);
523
524                 if (change.deleted()) {
525                         pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
526                                 yo_ + 2, col, Painter::line_solid, 3);
527                 } else {
528                         pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
529                                 yo_ + 2, col, Painter::line_solid, 3);
530                 }
531         }
532
533         // draw an endlabel
534
535         switch (endlabel) {
536         case END_LABEL_BOX:
537         case END_LABEL_FILLED_BOX: {
538                 FontInfo const font = labelFont();
539                 FontMetrics const & fm = theFontMetrics(font);
540                 int const size = int(0.75 * fm.maxAscent());
541                 int const y = yo_ - size;
542                 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
543                 int x = is_rtl ? nestMargin() + changebarMargin()
544                         : max_row_width - row_.right_margin;
545
546                 // If needed, move the box a bit to avoid overlapping with text.
547                 int const rem = max_row_width - row_.width();
548                 if (rem <= 0)
549                         x += is_rtl ? rem : - rem;
550
551                 if (endlabel == END_LABEL_BOX)
552                         pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
553                 else
554                         pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
555                 break;
556         }
557
558         case END_LABEL_STATIC: {
559                 FontInfo const font = labelFont();
560                 FontMetrics const & fm = theFontMetrics(font);
561                 docstring const & str = par_.layout().endlabelstring();
562                 double const x = is_rtl ? x_ - fm.width(str) : x_;
563                 pi_.pain.text(int(x), yo_, str, font);
564                 break;
565         }
566
567         case END_LABEL_NO_LABEL:
568                 break;
569         }
570 }
571
572
573 void RowPainter::paintOnlyInsets()
574 {
575         Row::const_iterator cit = row_.begin();
576         Row::const_iterator const & end = row_.end();
577         for ( ; cit != end ; ++cit) {
578                 Row::Element const & e = *cit;
579                 if (e.type == Row::INSET) {
580                         // If outer row has changed, nested insets are repainted completely.
581                         bool const nested_inset =
582                                 (e.inset->asInsetMath() && !e.inset->asInsetMath()->asMacroTemplate())
583                                 || e.inset->asInsetText() || e.inset->asInsetTabular();
584                         if (!nested_inset) {
585                                 x_ += e.full_width();
586                                 continue;
587                         }
588                         paintInset(e);
589                 } else
590                         x_ += e.full_width();
591         }
592 }
593
594
595 void RowPainter::paintText()
596 {
597         Row::const_iterator cit = row_.begin();
598         Row::const_iterator const & end = row_.end();
599         for ( ; cit != end ; ++cit) {
600                 double const orig_x = x_;
601                 Row::Element const & e = *cit;
602                 int foreign_descent = 0;
603
604                 switch (e.type) {
605                 case Row::STRING:
606                 case Row::VIRTUAL:
607                         paintStringAndSel(e);
608
609                         // Paint the spelling marks if enabled.
610                         if (lyxrc.spellcheck_continuously && pi_.do_spellcheck && pi_.pain.isDrawingEnabled())
611                                 paintMisspelledMark(orig_x, e);
612                         break;
613                 case Row::INSET: {
614                         // If outer row has changed, nested insets are repainted completely.
615                         paintInset(e);
616                         foreign_descent = e.dim.descent();
617                 }
618                         break;
619                 case Row::SPACE:
620                         pi_.pain.textDecoration(e.font.fontInfo(), int(x_), yo_, int(e.full_width()));
621                         x_ += e.full_width();
622                 }
623
624                 // The line that indicates word in a different language
625                 paintForeignMark(orig_x, e.font.language(), foreign_descent);
626
627                 // change tracking (not for insets that track their own changes)
628                 if (e.type != Row::INSET || ! e.inset->canTrackChanges())
629                         paintChange(orig_x, e.font, e.change);
630         }
631 }
632
633
634 void RowPainter::paintSelection() const
635 {
636         if (!row_.selection())
637                 return;
638         Cursor const & curs = pi_.base.bv->cursor();
639         DocIterator beg = curs.selectionBegin();
640         beg.pit() = pit_;
641         beg.pos() = row_.sel_beg;
642
643         DocIterator end = curs.selectionEnd();
644         end.pit() = pit_;
645         end.pos() = row_.sel_end;
646
647         bool const begin_boundary = beg.pos() >= row_.endpos();
648         bool const end_boundary = row_.sel_end == row_.endpos();
649
650         DocIterator cur = beg;
651         cur.boundary(begin_boundary);
652         int x1 = text_metrics_.cursorX(beg.top(), begin_boundary);
653         int x2 = text_metrics_.cursorX(end.top(), end_boundary);
654         int const y1 = yo_ - row_.ascent();
655         int const y2 = y1 + row_.height();
656
657         int const rm = text_.isMainText() ? pi_.base.bv->rightMargin() : 0;
658         int const lm = text_.isMainText() ? pi_.base.bv->leftMargin() : 0;
659
660         // draw the margins
661         if (row_.begin_margin_sel) {
662                 if (text_.isRTL(beg.paragraph())) {
663                         pi_.pain.fillRectangle(int(xo_ + x1), y1,
664                                 text_metrics_.width() - rm - x1, y2 - y1, Color_selection);
665                 } else {
666                         pi_.pain.fillRectangle(int(xo_ + lm), y1, x1 - lm, y2 - y1,
667                                 Color_selection);
668                 }
669         }
670
671         if (row_.end_margin_sel) {
672                 if (text_.isRTL(beg.paragraph())) {
673                         pi_.pain.fillRectangle(int(xo_ + lm), y1, x2 - lm, y2 - y1,
674                                 Color_selection);
675                 } else {
676                         pi_.pain.fillRectangle(int(xo_ + x2), y1, text_metrics_.width() - rm - x2,
677                                 y2 - y1, Color_selection);
678                 }
679         }
680
681         // if we are on a boundary from the beginning, it's probably
682         // a RTL boundary and we jump to the other side directly as this
683         // segement is 0-size and confuses the logic below
684         if (cur.boundary())
685                 cur.boundary(false);
686
687         // go through row and draw from RTL boundary to RTL boundary
688         while (cur < end) {
689                 bool draw_now = false;
690
691                 // simplified cursorForward code below which does not
692                 // descend into insets and which does not go into the
693                 // next line. Compare the logic with the original cursorForward
694
695                 // if left of boundary -> just jump to right side, but
696                 // for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
697                 if (cur.boundary()) {
698                         cur.boundary(false);
699                 }       else if (text_metrics_.isRTLBoundary(cur.pit(), cur.pos() + 1)) {
700                         // in front of RTL boundary -> Stay on this side of the boundary
701                         // because:  ab|cDDEEFFghi -> abc|DDEEFFghi
702                         ++cur.pos();
703                         cur.boundary(true);
704                         draw_now = true;
705                 } else {
706                         // move right
707                         ++cur.pos();
708
709                         // line end?
710                         if (cur.pos() == row_.endpos())
711                                 cur.boundary(true);
712                 }
713
714                 if (x1 == -1) {
715                         // the previous segment was just drawn, now the next starts
716                         x1 = text_metrics_.cursorX(cur.top(), cur.boundary());
717                 }
718
719                 if (!(cur < end) || draw_now) {
720                         x2 = text_metrics_.cursorX(cur.top(), cur.boundary());
721                         pi_.pain.fillRectangle(int(xo_ + min(x1, x2)), y1, abs(x2 - x1),
722                                 y2 - y1, Color_selection);
723
724                         // reset x1, so it is set again next round (which will be on the
725                         // right side of a boundary or at the selection end)
726                         x1 = -1;
727                 }
728         }
729 }
730
731
732 } // namespace lyx