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