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