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