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