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