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