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