]> git.lyx.org Git - lyx.git/blob - src/rowpainter.cpp
Replace the PainterInfo::erased_ member by a proper Change object and remove the...
[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 "Bidi.h"
18 #include "Buffer.h"
19 #include "CoordCache.h"
20 #include "Cursor.h"
21 #include "BufferParams.h"
22 #include "BufferView.h"
23 #include "Changes.h"
24 #include "Encoding.h"
25 #include "support/gettext.h"
26 #include "Language.h"
27 #include "Layout.h"
28 #include "LyXRC.h"
29 #include "Row.h"
30 #include "MetricsInfo.h"
31 #include "Paragraph.h"
32 #include "ParagraphMetrics.h"
33 #include "paragraph_funcs.h"
34 #include "ParagraphParameters.h"
35 #include "TextMetrics.h"
36 #include "VSpace.h"
37
38 #include "frontends/FontMetrics.h"
39 #include "frontends/Painter.h"
40
41 #include "insets/InsetText.h"
42
43 #include "support/debug.h"
44 #include "support/textutils.h"
45
46 #include "support/lassert.h"
47 #include <boost/crc.hpp>
48
49 using namespace std;
50
51 namespace lyx {
52
53 using frontend::Painter;
54 using frontend::FontMetrics;
55
56 RowPainter::RowPainter(PainterInfo & pi,
57         Text const & text, pit_type pit, Row const & row, Bidi & bidi, 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)),
63           bidi_(bidi), change_(pi_.change_),
64           xo_(x), yo_(y), width_(text_metrics_.width())
65 {
66         bidi_.computeTables(par_, pi_.base.bv->buffer(), row_);
67         x_ = row_.x + xo_;
68
69         //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
70         //row_.dump();
71
72         LASSERT(pit >= 0, /**/);
73         LASSERT(pit < int(text.paragraphs().size()), /**/);
74 }
75
76
77 FontInfo RowPainter::labelFont() const
78 {
79         return text_.labelFont(pi_.base.bv->buffer(), par_);
80 }
81
82
83 int RowPainter::leftMargin() const
84 {
85         return text_metrics_.leftMargin(text_metrics_.width(), pit_,
86                 row_.pos());
87 }
88
89 // If you want to debug inset metrics uncomment the following line:
90 //#define DEBUG_METRICS
91 // This draws green lines around each inset.
92
93
94 void RowPainter::paintInset(Inset const * inset, pos_type const pos)
95 {
96         Font const font = text_metrics_.displayFont(pit_, pos);
97
98         LASSERT(inset, return);
99         // Backup full_repaint status because some insets (InsetTabular)
100         // requires a full repaint
101         bool pi_full_repaint = pi_.full_repaint;
102
103         // FIXME: We should always use font, see documentation of
104         // noFontChange() in Inset.h.
105         pi_.base.font = inset->noFontChange() ?
106                 pi_.base.bv->buffer().params().getFont().fontInfo() :
107                 font.fontInfo();
108         pi_.ltr_pos = (bidi_.level(pos) % 2 == 0);
109         pi_.change_ = change_.changed() ? change_ : par_.lookupChange(pos);
110
111         int const x1 = int(x_);
112         pi_.base.bv->coordCache().insets().add(inset, x1, yo_);
113         // insets are painted completely. Recursive
114         inset->drawBackground(pi_, x1, yo_);
115         inset->drawSelection(pi_, x1, yo_);
116         inset->draw(pi_, x1, yo_);
117
118         Dimension const & dim = pm_.insetDimension(inset);
119
120         paintForeignMark(x_, font.language(), dim.descent());
121
122         x_ += dim.width();
123
124         // Restore full_repaint status.
125         pi_.full_repaint = pi_full_repaint;
126
127 #ifdef DEBUG_METRICS
128         int const x2 = x1 + dim.wid;
129         int const y1 = yo_ + dim.des;
130         int const y2 = yo_ - dim.asc;
131         pi_.pain.line(x1, y1, x1, y2, Color_green);
132         pi_.pain.line(x1, y1, x2, y1, Color_green);
133         pi_.pain.line(x2, y1, x2, y2, Color_green);
134         pi_.pain.line(x1, y2, x2, y2, Color_green);
135 #endif
136 }
137
138
139 void RowPainter::paintHebrewComposeChar(pos_type & vpos, FontInfo const & font)
140 {
141         pos_type pos = bidi_.vis2log(vpos);
142
143         docstring str;
144
145         // first char
146         char_type c = par_.getChar(pos);
147         str += c;
148         ++vpos;
149
150         int const width = theFontMetrics(font).width(c);
151         int dx = 0;
152
153         for (pos_type i = pos - 1; i >= 0; --i) {
154                 c = par_.getChar(i);
155                 if (!Encodings::isHebrewComposeChar(c)) {
156                         if (isPrintableNonspace(c)) {
157                                 int const width2 = pm_.singleWidth(i,
158                                         text_metrics_.displayFont(pit_, i));
159                                 dx = (c == 0x05e8 || // resh
160                                       c == 0x05d3)   // dalet
161                                         ? width2 - width
162                                         : (width2 - width) / 2;
163                         }
164                         break;
165                 }
166         }
167
168         // Draw nikud
169         pi_.pain.text(int(x_) + dx, yo_, str, font);
170 }
171
172
173 void RowPainter::paintArabicComposeChar(pos_type & vpos, FontInfo const & font)
174 {
175         pos_type pos = bidi_.vis2log(vpos);
176         docstring str;
177
178         // first char
179         char_type c = par_.getChar(pos);
180         c = par_.transformChar(c, pos);
181         str += c;
182         ++vpos;
183
184         int const width = theFontMetrics(font).width(c);
185         int dx = 0;
186
187         for (pos_type i = pos - 1; i >= 0; --i) {
188                 c = par_.getChar(i);
189                 if (!Encodings::isArabicComposeChar(c)) {
190                         if (isPrintableNonspace(c)) {
191                                 int const width2 = pm_.singleWidth(i,
192                                                 text_metrics_.displayFont(pit_, i));
193                                 dx = (width2 - width) / 2;
194                         }
195                         break;
196                 }
197         }
198         // Draw nikud
199         pi_.pain.text(int(x_) + dx, yo_, str, font);
200 }
201
202
203 void RowPainter::paintChars(pos_type & vpos, FontInfo const & font,
204                             bool hebrew, bool arabic)
205 {
206         // This method takes up 70% of time when typing
207         pos_type pos = bidi_.vis2log(vpos);
208         // first character
209         vector<char_type> str;
210         str.reserve(100);
211         str.push_back(par_.getChar(pos));
212
213         if (arabic) {
214                 char_type c = str[0];
215                 if (c == '(')
216                         c = ')';
217                 else if (c == ')')
218                         c = '(';
219                 str[0] = par_.transformChar(c, pos);
220         }
221
222         pos_type const end = row_.endpos();
223         FontSpan const font_span = par_.fontSpan(pos);
224         // Track-change status.
225         Change const & change_running = par_.lookupChange(pos);
226
227         // selected text?
228         bool const selection = pos >= row_.sel_beg && pos < row_.sel_end;
229
230         char_type prev_char = ' ';
231         // collect as much similar chars as we can
232         for (++vpos ; vpos < end ; ++vpos) {
233                 pos = bidi_.vis2log(vpos);
234                 if (pos < font_span.first || pos > font_span.last)
235                         break;
236
237                 bool const new_selection = pos >= row_.sel_beg && pos < row_.sel_end;
238                 if (new_selection != selection)
239                         // Selection ends or starts here.
240                         break;
241
242                 Change const & change = par_.lookupChange(pos);
243                 if (!change_running.isSimilarTo(change))
244                         // Track change type or author has changed.
245                         break;
246
247                 char_type c = par_.getChar(pos);
248
249                 if (c == '\t' || prev_char == '\t') {
250                         prev_char = c;
251                         break;
252                 }
253
254                 if (!isPrintableNonspace(c))
255                         break;
256
257                 /* Because we do our own bidi, at this point the strings are
258                  * already in visual order. However, Qt also applies its own
259                  * bidi algorithm to strings that it paints to the screen.
260                  * Therefore, if we were to paint Hebrew/Arabic words as a
261                  * single string, the letters in the words would get reversed
262                  * again. In order to avoid that, we don't collect Hebrew/
263                  * Arabic characters, but rather paint them one at a time.
264                  * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
265                  */
266                 if (hebrew)
267                         break;
268
269                 /* FIXME: these checks are irrelevant, since 'arabic' and
270                  * 'hebrew' alone are already going to trigger a break.
271                  * However, this should not be removed completely, because
272                  * if an alternative solution is found which allows grouping
273                  * of arabic and hebrew characters, then these breaks may have
274                  * to be re-applied.
275
276                 if (arabic && Encodings::isArabicComposeChar(c))
277                         break;
278
279                 if (hebrew && Encodings::isHebrewComposeChar(c))
280                         break;
281                 */
282
283                 if (arabic) {
284                         if (c == '(')
285                                 c = ')';
286                         else if (c == ')')
287                                 c = '(';
288                         c = par_.transformChar(c, pos);
289                         /* see comment in hebrew, explaining why we break */
290                         break;
291                 }
292
293                 str.push_back(c);
294         }
295
296         docstring s(&str[0], str.size());
297
298         if (s[0] == '\t')
299                 s.replace(0,1,from_ascii("    "));
300
301         if (!selection && !change_running.changed()) {
302                 x_ += pi_.pain.text(int(x_), yo_, s, font);
303                 return;
304         }
305         
306         FontInfo copy = font;
307         if (change_running.changed())
308                 copy.setPaintColor(change_running.color());
309         else if (selection)
310                 copy.setPaintColor(Color_selectiontext);
311
312         x_ += pi_.pain.text(int(x_), yo_, s, copy);
313 }
314
315
316 void RowPainter::paintForeignMark(double orig_x, Language const * lang,
317                 int desc)
318 {
319         if (!lyxrc.mark_foreign_language)
320                 return;
321         if (lang == latex_language)
322                 return;
323         if (lang == pi_.base.bv->buffer().params().language)
324                 return;
325
326         int const y = yo_ + 1 + desc;
327         pi_.pain.line(int(orig_x), y, int(x_), y, Color_language);
328 }
329
330
331 void RowPainter::paintFromPos(pos_type & vpos)
332 {
333         pos_type const pos = bidi_.vis2log(vpos);
334         Font const orig_font = text_metrics_.displayFont(pit_, pos);
335         double const orig_x = x_;
336
337         // usual characters, no insets
338         char_type const c = par_.getChar(pos);
339
340         // special case languages
341         string const & lang = orig_font.language()->lang();
342         bool const hebrew = lang == "hebrew";
343         bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" || 
344                                                 lang == "farsi";
345
346         // draw as many chars as we can
347         if ((!hebrew && !arabic)
348                 || (hebrew && !Encodings::isHebrewComposeChar(c))
349                 || (arabic && !Encodings::isArabicComposeChar(c))) {
350                 paintChars(vpos, orig_font.fontInfo(), hebrew, arabic);
351         } else if (hebrew) {
352                 paintHebrewComposeChar(vpos, orig_font.fontInfo());
353         } else if (arabic) {
354                 paintArabicComposeChar(vpos, orig_font.fontInfo());
355         }
356
357         paintForeignMark(orig_x, orig_font.language());
358 }
359
360
361 void RowPainter::paintChangeBar()
362 {
363         pos_type const start = row_.pos();
364         pos_type end = row_.endpos();
365
366         if (par_.size() == end) {
367                 // this is the last row of the paragraph;
368                 // thus, we must also consider the imaginary end-of-par character
369                 end++;
370         }
371
372         if (start == end || !par_.isChanged(start, end))
373                 return;
374
375         int const height = text_metrics_.isLastRow(pit_, row_)
376                 ? row_.ascent()
377                 : row_.height();
378
379         pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
380 }
381
382
383 void RowPainter::paintAppendix()
384 {
385         // only draw the appendix frame once (for the main text)
386         if (!par_.params().appendix() || !text_.isMainText(pi_.base.bv->buffer()))
387                 return;
388
389         int y = yo_ - row_.ascent();
390
391         if (par_.params().startOfAppendix())
392                 y += 2 * defaultRowHeight();
393
394         pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
395         pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
396 }
397
398
399 void RowPainter::paintDepthBar()
400 {
401         depth_type const depth = par_.getDepth();
402
403         if (depth <= 0)
404                 return;
405
406         depth_type prev_depth = 0;
407         if (!text_metrics_.isFirstRow(pit_, row_)) {
408                 pit_type pit2 = pit_;
409                 if (row_.pos() == 0)
410                         --pit2;
411                 prev_depth = pars_[pit2].getDepth();
412         }
413
414         depth_type next_depth = 0;
415         if (!text_metrics_.isLastRow(pit_, row_)) {
416                 pit_type pit2 = pit_;
417                 if (row_.endpos() >= pars_[pit2].size())
418                         ++pit2;
419                 next_depth = pars_[pit2].getDepth();
420         }
421
422         for (depth_type i = 1; i <= depth; ++i) {
423                 int const w = nestMargin() / 5;
424                 int x = int(xo_) + w * i;
425                 // only consider the changebar space if we're drawing outermost text
426                 if (text_.isMainText(pi_.base.bv->buffer()))
427                         x += changebarMargin();
428
429                 int const starty = yo_ - row_.ascent();
430                 int const h =  row_.height() - 1 - (i - next_depth - 1) * 3;
431
432                 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
433
434                 if (i > prev_depth)
435                         pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
436                 if (i > next_depth)
437                         pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
438         }
439 }
440
441
442 int RowPainter::paintAppendixStart(int y)
443 {
444         FontInfo pb_font = sane_font;
445         pb_font.setColor(Color_appendix);
446         pb_font.decSize();
447
448         int w = 0;
449         int a = 0;
450         int d = 0;
451
452         docstring const label = _("Appendix");
453         theFontMetrics(pb_font).rectText(label, w, a, d);
454
455         int const text_start = int(xo_ + (width_ - w) / 2);
456         int const text_end = text_start + w;
457
458         pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
459
460         pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
461         pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
462
463         return 3 * defaultRowHeight();
464 }
465
466
467 void RowPainter::paintFirst()
468 {
469         ParagraphParameters const & parparams = par_.params();
470
471         int y_top = 0;
472
473         // start of appendix?
474         if (parparams.startOfAppendix())
475                 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
476
477         Buffer const & buffer = pi_.base.bv->buffer();
478         Layout const & layout = par_.layout();
479
480         if (buffer.params().paragraph_separation == BufferParams::ParagraphSkipSeparation) {
481                 if (pit_ != 0) {
482                         if (layout.latextype == LATEX_PARAGRAPH
483                                 && !par_.getDepth()) {
484                                 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
485                         } else {
486                                 Layout const & playout = pars_[pit_ - 1].layout();
487                                 if (playout.latextype == LATEX_PARAGRAPH
488                                         && !pars_[pit_ - 1].getDepth()) {
489                                         // is it right to use defskip here, too? (AS)
490                                         y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
491                                 }
492                         }
493                 }
494         }
495
496         bool const is_rtl = text_.isRTL(buffer, par_);
497         bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
498         //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
499
500         // should we print a label?
501         if (layout.labeltype >= LABEL_STATIC
502             && (layout.labeltype != LABEL_STATIC
503                       || layout.latextype != LATEX_ENVIRONMENT
504                       || is_seq)) {
505
506                 FontInfo const font = labelFont();
507                 FontMetrics const & fm = theFontMetrics(font);
508
509                 docstring const str = par_.labelString();
510                 if (!str.empty()) {
511                         double x = x_;
512
513                         // this is special code for the chapter layout. This is
514                         // printed in an extra row and has a pagebreak at
515                         // the top.
516                         if (layout.counter == "chapter") {
517                                 double spacing_val = 1.0;
518                                 if (!parparams.spacing().isDefault()) {
519                                         spacing_val = parparams.spacing().getValue();
520                                 } else {
521                                         spacing_val = buffer.params().spacing().getValue();
522                                 }
523
524                                 int const labeladdon = int(fm.maxHeight() * layout.spacing.getValue() * spacing_val);
525
526                                 int const maxdesc = int(fm.maxDescent() * layout.spacing.getValue() * spacing_val)
527                                         + int(layout.parsep) * defaultRowHeight();
528
529                                 if (is_rtl) {
530                                         x = width_ - leftMargin() -
531                                                 fm.width(str);
532                                 }
533
534                                 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
535                         } else {
536                                 if (is_rtl) {
537                                         x = width_ - leftMargin()
538                                                 + fm.width(layout.labelsep);
539                                 } else {
540                                         x = x_ - fm.width(layout.labelsep)
541                                                 - fm.width(str);
542                                 }
543
544                                 pi_.pain.text(int(x), yo_, str, font);
545                         }
546                 }
547
548         // the labels at the top of an environment.
549         // More or less for bibliography
550         } else if (is_seq &&
551                 (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
552                 layout.labeltype == LABEL_BIBLIO ||
553                 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
554                 FontInfo const font = labelFont();
555                 docstring const str = par_.labelString();
556                 if (!str.empty()) {
557                         double spacing_val = 1.0;
558                         if (!parparams.spacing().isDefault())
559                                 spacing_val = parparams.spacing().getValue();
560                         else
561                                 spacing_val = buffer.params().spacing().getValue();
562
563                         FontMetrics const & fm = theFontMetrics(font);
564
565                         int const labeladdon = int(fm.maxHeight()
566                                 * layout.spacing.getValue() * spacing_val);
567
568                         int maxdesc =
569                                 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
570                                 + (layout.labelbottomsep * defaultRowHeight()));
571
572                         double x = x_;
573                         if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
574                                 if (is_rtl)
575                                         x = leftMargin();
576                                 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
577                                 x -= fm.width(str) / 2;
578                         } else if (is_rtl) {
579                                 x = width_ - leftMargin() -     fm.width(str);
580                         }
581                         pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
582                 }
583         }
584 }
585
586
587 void RowPainter::paintLast()
588 {
589         bool const is_rtl = text_.isRTL(pi_.base.bv->buffer(), par_);
590         int const endlabel = getEndLabel(pit_, text_.paragraphs());
591
592         // paint imaginary end-of-paragraph character
593
594         Change const & change = par_.lookupChange(par_.size());
595         if (change.changed()) {
596                 FontMetrics const & fm =
597                         theFontMetrics(pi_.base.bv->buffer().params().getFont());
598                 int const length = fm.maxAscent() / 2;
599                 Color col = change.color();
600
601                 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
602                            Painter::line_solid, Painter::line_thick);
603
604                 if (change.deleted()) {
605                         pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
606                                 yo_ + 2, col, Painter::line_solid, Painter::line_thick);
607                 } else {
608                         pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
609                                 yo_ + 2, col, Painter::line_solid, Painter::line_thick);
610                 }
611
612         }
613
614         // draw an endlabel
615
616         switch (endlabel) {
617         case END_LABEL_BOX:
618         case END_LABEL_FILLED_BOX: {
619                 FontInfo const font = labelFont();
620                 FontMetrics const & fm = theFontMetrics(font);
621                 int const size = int(0.75 * fm.maxAscent());
622                 int const y = yo_ - size;
623                 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
624                 int x = is_rtl ? nestMargin() + changebarMargin()
625                         : max_row_width - text_metrics_.rightMargin(pm_);
626
627                 // If needed, move the box a bit to avoid overlapping with text.
628                 int const rem = max_row_width - row_.width();
629                 if (rem <= 0)
630                         x += is_rtl ? rem : - rem;
631
632                 if (endlabel == END_LABEL_BOX)
633                         pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
634                 else
635                         pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
636                 break;
637         }
638
639         case END_LABEL_STATIC: {
640                 FontInfo const font = labelFont();
641                 FontMetrics const & fm = theFontMetrics(font);
642                 docstring const & str = par_.layout().endlabelstring();
643                 double const x = is_rtl ?
644                         x_ - fm.width(str)
645                         : - text_metrics_.rightMargin(pm_) - row_.width();
646                 pi_.pain.text(int(x), yo_, str, font);
647                 break;
648         }
649
650         case END_LABEL_NO_LABEL:
651                 break;
652         }
653 }
654
655
656 void RowPainter::paintOnlyInsets()
657 {
658         pos_type const end = row_.endpos();
659         for (pos_type pos = row_.pos(); pos != end; ++pos) {
660                 // If outer row has changed, nested insets are repaint completely.
661                 Inset const * inset = par_.getInset(pos);
662                 if (!inset)
663                         continue;
664                 if (x_ > pi_.base.bv->workWidth())
665                         continue;
666                 x_ = pi_.base.bv->coordCache().getInsets().x(inset);
667
668                 bool const pi_selected = pi_.selected;
669                 Cursor const & cur = pi_.base.bv->cursor();
670                 if (cur.selection() && cur.text() == &text_ 
671                           && cur.anchor().text() == &text_)
672                         pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos; 
673                 paintInset(inset, pos);
674                 pi_.selected = pi_selected;
675         }
676 }
677
678
679 void RowPainter::paintText()
680 {
681         pos_type const end = row_.endpos();
682         // Spaces at logical line breaks in bidi text must be skipped during 
683         // painting. However, they may appear visually in the middle
684         // of a row; they must be skipped, wherever they are...
685         // * logically "abc_[HEBREW_\nHEBREW]"
686         // * visually "abc_[_WERBEH\nWERBEH]"
687         pos_type skipped_sep_vpos = -1;
688         pos_type body_pos = par_.beginOfBody();
689         if (body_pos > 0 &&
690                 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
691                 body_pos = 0;
692         }
693
694         Layout const & layout = par_.layout();
695
696         Change change_running;
697         int change_last_x = 0;
698
699         // check for possible inline completion
700         DocIterator const & inlineCompletionPos = pi_.base.bv->inlineCompletionPos();
701         pos_type inlineCompletionVPos = -1;
702         if (inlineCompletionPos.inTexted()
703             && inlineCompletionPos.text() == &text_
704             && inlineCompletionPos.pit() == pit_
705             && inlineCompletionPos.pos() - 1 >= row_.pos()
706             && inlineCompletionPos.pos() - 1 < row_.endpos()) {
707                 // draw logically behind the previous character
708                 inlineCompletionVPos = bidi_.log2vis(inlineCompletionPos.pos() - 1);
709         }
710
711         // Use font span to speed things up, see below
712         FontSpan font_span;
713         Font font;
714
715         // If the last logical character is a separator, don't paint it, unless
716         // it's in the last row of a paragraph; see skipped_sep_vpos declaration
717         if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
718                 skipped_sep_vpos = bidi_.log2vis(end - 1);
719
720         for (pos_type vpos = row_.pos(); vpos < end; ) {
721                 if (x_ > pi_.base.bv->workWidth())
722                         break;
723
724                 // Skip the separator at the logical end of the row
725                 if (vpos == skipped_sep_vpos) {
726                         ++vpos;
727                         continue;
728                 }
729
730                 pos_type const pos = bidi_.vis2log(vpos);
731
732                 if (pos >= par_.size()) {
733                         ++vpos;
734                         continue;
735                 }
736
737                 // Use font span to speed things up, see above
738                 if (vpos < font_span.first || vpos > font_span.last) {
739                         font_span = par_.fontSpan(vpos);
740                         font = text_metrics_.displayFont(pit_, vpos);
741
742                         // split font span if inline completion is inside
743                         if (font_span.first <= inlineCompletionVPos
744                             && font_span.last > inlineCompletionVPos)
745                                 font_span.last = inlineCompletionVPos;
746                 }
747
748                 const int width_pos = pm_.singleWidth(pos, font);
749
750                 if (x_ + width_pos < 0) {
751                         x_ += width_pos;
752                         ++vpos;
753                         continue;
754                 }
755                 Change const & change = par_.lookupChange(pos);
756                 if (change.changed() && !change_running.changed()) {
757                         change_running = change;
758                         change_last_x = int(x_);
759                 }
760
761                 Inset const * inset = par_.getInset(pos);
762                 bool const highly_editable_inset = inset
763                         && inset->editable() == Inset::HIGHLY_EDITABLE;
764
765                 // If we reach the end of a change or if the author changes, paint it.
766                 // We also don't paint across things like tables
767                 if (change_running.changed() && (highly_editable_inset
768                         || !change.changed() || !change_running.isSimilarTo(change))) {
769                         // Calculate 1/3 height of the buffer's default font
770                         FontMetrics const & fm
771                                 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
772                         int const y_bar = change_running.deleted() ?
773                                 yo_ - fm.maxAscent() / 3 : yo_ + fm.maxAscent() / 6;
774                         pi_.pain.line(change_last_x, y_bar, int(x_), y_bar,
775                                 change_running.color(), Painter::line_solid,
776                                 Painter::line_thin);
777
778                         // Change might continue with a different author or type
779                         if (change.changed() && !highly_editable_inset) {
780                                 change_running = change;
781                                 change_last_x = int(x_);
782                         } else
783                                 change_running.setUnchanged();
784                 }
785
786                 if (body_pos > 0 && pos == body_pos - 1) {
787                         int const lwidth = theFontMetrics(labelFont())
788                                 .width(layout.labelsep);
789
790                         x_ += row_.label_hfill + lwidth - width_pos;
791                 }
792                 
793                 // Is the inline completion in front of character?
794                 if (font.isRightToLeft() && vpos == inlineCompletionVPos)
795                         paintInlineCompletion(font);
796
797                 if (par_.isSeparator(pos)) {
798                         Font const orig_font = text_metrics_.displayFont(pit_, pos);
799                         double const orig_x = x_;
800                         x_ += width_pos;
801                         if (pos >= body_pos)
802                                 x_ += row_.separator;
803                         paintForeignMark(orig_x, orig_font.language());
804                         ++vpos;
805
806                 } else if (inset) {
807                         // If outer row has changed, nested insets are repaint completely.
808                         pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
809                         
810                         bool const pi_selected = pi_.selected;
811                         Cursor const & cur = pi_.base.bv->cursor();
812                         if (cur.selection() && cur.text() == &text_ 
813                                   && cur.anchor().text() == &text_)
814                                 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos; 
815                         paintInset(inset, pos);
816                         pi_.selected = pi_selected;
817                         ++vpos;
818
819                 } else {
820                         // paint as many characters as possible.
821                         paintFromPos(vpos);
822                 }
823
824                 // Is the inline completion after character?
825                 if (!font.isRightToLeft() && vpos - 1 == inlineCompletionVPos)
826                         paintInlineCompletion(font);
827         }
828
829         // if we reach the end of a struck out range, paint it
830         if (change_running.changed()) {
831                 FontMetrics const & fm
832                         = theFontMetrics(pi_.base.bv->buffer().params().getFont());
833                 int const y_bar = change_running.deleted() ?
834                                 yo_ - fm.maxAscent() / 3 : yo_ + fm.maxAscent() / 6;
835                 pi_.pain.line(change_last_x, y_bar, int(x_), y_bar,
836                         change_running.color(), Painter::line_solid, Painter::line_thin);
837                 change_running.setUnchanged();
838         }
839 }
840
841
842 void RowPainter::paintInlineCompletion(Font const & font)
843 {
844         docstring completion = pi_.base.bv->inlineCompletion();
845         FontInfo f = font.fontInfo();
846         bool rtl = font.isRightToLeft();
847         
848         // draw the unique and the non-unique completion part
849         // Note: this is not time-critical as it is
850         // only done once per screen.
851         size_t uniqueTo = pi_.base.bv->inlineCompletionUniqueChars();
852         docstring s1 = completion.substr(0, uniqueTo);
853         docstring s2 = completion.substr(uniqueTo);
854         ColorCode c1 = Color_inlinecompletion;
855         ColorCode c2 = Color_nonunique_inlinecompletion;
856         
857         // right to left?
858         if (rtl) {
859                 swap(s1, s2);
860                 swap(c1, c2);
861         }
862
863         if (s1.size() > 0) {
864                 f.setColor(c1);
865                 pi_.pain.text(int(x_), yo_, s1, f);
866                 x_ += theFontMetrics(font).width(s1);
867         }
868         
869         if (s2.size() > 0) {
870                 f.setColor(c2);
871                 pi_.pain.text(int(x_), yo_, s2, f);
872                 x_ += theFontMetrics(font).width(s2);
873         }
874 }
875
876 } // namespace lyx