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