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