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