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