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