]> git.lyx.org Git - lyx.git/blob - src/rowpainter.C
ce48d1cd7400cb9fc527dac3d51480fd2760f167
[lyx.git] / src / rowpainter.C
1 /**
2  * \file rowpainter.C
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 "buffer.h"
17 #include "coordcache.h"
18 #include "cursor.h"
19 #include "debug.h"
20 #include "bufferparams.h"
21 #include "BufferView.h"
22 #include "encoding.h"
23 #include "gettext.h"
24 #include "language.h"
25 #include "LColor.h"
26 #include "lyxrc.h"
27 #include "lyxrow.h"
28 #include "lyxrow_funcs.h"
29 #include "metricsinfo.h"
30 #include "paragraph.h"
31 #include "paragraph_funcs.h"
32 #include "ParagraphParameters.h"
33 #include "vspace.h"
34
35 #include "frontends/FontMetrics.h"
36 #include "frontends/nullpainter.h"
37 #include "frontends/Painter.h"
38
39 #include "insets/insettext.h"
40
41 #include "support/textutils.h"
42
43 #include <boost/crc.hpp>
44
45
46 namespace lyx {
47
48 using frontend::Painter;
49 using frontend::NullPainter;
50 using frontend::FontMetrics;
51
52 using std::endl;
53 using std::max;
54 using std::min;
55 using std::string;
56
57
58 namespace {
59
60 /// Flag: do a full redraw of inside text of inset
61 /// Working variable indicating a full screen refresh
62 bool refreshInside;
63
64 /**
65  * A class used for painting an individual row of text.
66  */
67 class RowPainter {
68 public:
69         /// initialise and run painter
70         RowPainter(PainterInfo & pi, LyXText const & text,
71                 pit_type pit, Row const & row, int x, int y);
72
73         // paint various parts
74         void paintAppendix();
75         void paintDepthBar();
76         void paintChangeBar();
77         void paintFirst();
78         void paintLast();
79         void paintText();
80
81 private:
82         void paintForeignMark(double orig_x, LyXFont const & font, int desc = 0);
83         void paintHebrewComposeChar(pos_type & vpos, LyXFont const & font);
84         void paintArabicComposeChar(pos_type & vpos, LyXFont const & font);
85         void paintChars(pos_type & vpos, LyXFont font,
86                         bool hebrew, bool arabic);
87         int paintAppendixStart(int y);
88         void paintFromPos(pos_type & vpos);
89         void paintInset(pos_type const pos, LyXFont const & font);
90
91         /// return left margin
92         int leftMargin() const;
93
94         /// return the label font for this row
95         LyXFont const getLabelFont() const;
96
97         /// bufferview to paint on
98         BufferView & bv_;
99
100         /// Painter to use
101         Painter & pain_;
102
103         /// LyXText for the row
104         LyXText const & text_;
105         ParagraphList const & pars_;
106
107         /// The row to paint
108         Row const & row_;
109
110         /// Row's paragraph
111         pit_type const pit_;
112         Paragraph const & par_;
113
114         /// is row erased? (change tracking)
115         bool erased_;
116
117         // Looks ugly - is
118         double const xo_;
119         int const yo_;    // current baseline
120         double x_;
121         int width_;
122         double separator_;
123         double hfill_;
124         double label_hfill_;
125 };
126
127
128 RowPainter::RowPainter(PainterInfo & pi,
129         LyXText const & text, pit_type pit, Row const & row, int x, int y)
130         : bv_(*pi.base.bv), pain_(pi.pain), text_(text), pars_(text.paragraphs()),
131           row_(row), pit_(pit), par_(text.paragraphs()[pit]),
132           erased_(pi.erased_),
133           xo_(x), yo_(y), width_(text_.width())
134 {
135         RowMetrics m = text_.computeRowMetrics(pit, row_);
136         x_ = m.x + xo_;
137
138         //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
139         //row_.dump();
140
141         separator_ = m.separator;
142         hfill_ = m.hfill;
143         label_hfill_ = m.label_hfill;
144
145         BOOST_ASSERT(pit >= 0);
146         BOOST_ASSERT(pit < int(text.paragraphs().size()));
147 }
148
149
150 LyXFont const RowPainter::getLabelFont() const
151 {
152         return text_.getLabelFont(par_);
153 }
154
155
156 int RowPainter::leftMargin() const
157 {
158         return text_.leftMargin(pit_, row_.pos());
159 }
160
161
162 void RowPainter::paintInset(pos_type const pos, LyXFont const & font)
163 {
164         InsetBase const * inset = par_.getInset(pos);
165         BOOST_ASSERT(inset);
166         PainterInfo pi(const_cast<BufferView *>(&bv_), pain_);
167         // FIXME: We should always use font, see documentation of
168         // noFontChange() in insetbase.h.
169         pi.base.font = inset->noFontChange() ?
170                 bv_.buffer()->params().getFont() :
171                 font;
172         pi.ltr_pos = (text_.bidi.level(pos) % 2 == 0);
173         pi.erased_ = erased_ || par_.isDeleted(pos);
174         bv_.coordCache().insets().add(inset, int(x_), yo_);
175         InsetText const * const in = inset->asTextInset();
176         // non-wide insets are painted completely. Recursive
177         bool tmp = refreshInside;
178         if (!in || !in->Wide()) {
179                 refreshInside = true;
180                 lyxerr[Debug::PAINTING] << endl << "Paint inset fully" << endl;
181         }
182         if (refreshInside)
183                 inset->drawSelection(pi, int(x_), yo_);
184         inset->draw(pi, int(x_), yo_);
185         refreshInside = tmp;
186         x_ += inset->width();
187 }
188
189
190 void RowPainter::paintHebrewComposeChar(pos_type & vpos, LyXFont const & font)
191 {
192         pos_type pos = text_.bidi.vis2log(vpos);
193
194         docstring str;
195
196         // first char
197         char_type c = par_.getChar(pos);
198         str += c;
199         ++vpos;
200
201         int const width = theFontMetrics(font).width(c);
202         int dx = 0;
203
204         for (pos_type i = pos - 1; i >= 0; --i) {
205                 c = par_.getChar(i);
206                 if (!Encodings::isComposeChar_hebrew(c)) {
207                         if (isPrintableNonspace(c)) {
208                                 int const width2 =
209                                         text_.singleWidth(par_, i, c, text_.getFont(par_, i));
210                                 // FIXME UNICODE
211                                 // This does not work anymore, and non-ascii
212                                 // characters in source files are forbidden
213                                 // anyway.
214                                 // dalet / resh
215                                 dx = (c == 'ø' || c == 'ã')
216                                         ? width2 - width
217                                         : (width2 - width) / 2;
218                         }
219                         break;
220                 }
221         }
222
223         // Draw nikud
224         // FIXME UNICODE
225         pain_.text(int(x_) + dx, yo_, str, font);
226 }
227
228
229 void RowPainter::paintArabicComposeChar(pos_type & vpos, LyXFont const & font)
230 {
231         pos_type pos = text_.bidi.vis2log(vpos);
232         docstring str;
233
234         // first char
235         char_type c = par_.getChar(pos);
236         c = par_.transformChar(c, pos);
237         str += c;
238         ++vpos;
239
240         int const width = theFontMetrics(font).width(c);
241         int dx = 0;
242
243         for (pos_type i = pos - 1; i >= 0; --i) {
244                 c = par_.getChar(i);
245                 if (!Encodings::isComposeChar_arabic(c)) {
246                         if (isPrintableNonspace(c)) {
247                                 int const width2 =
248                                         text_.singleWidth(par_, i, c, text_.getFont(par_, i));
249                                 dx = (width2 - width) / 2;
250                         }
251                         break;
252                 }
253         }
254         // Draw nikud
255         pain_.text(int(x_) + dx, yo_, str, font);
256 }
257
258
259 void RowPainter::paintChars(pos_type & vpos, LyXFont font,
260                             bool hebrew, bool arabic)
261 {
262         pos_type pos = text_.bidi.vis2log(vpos);
263         pos_type const end = row_.endpos();
264         FontSpan const font_span = par_.fontSpan(pos);
265         Change::Type const prev_change = par_.lookupChange(pos).type;
266
267         // first character
268 #if 0
269         string str;
270         str += par_.getChar(pos);
271 #else
272         std::vector<char_type> str;
273         str.push_back(par_.getChar(pos));
274 #endif
275         if (arabic) {
276                 char_type c = str[0];
277                 str[0] = par_.transformChar(c, pos);
278         }
279
280         // collect as much similar chars as we can
281         for (++vpos ; vpos < end ; ++vpos) {
282                 pos = text_.bidi.vis2log(vpos);
283                 if (pos < font_span.first || pos > font_span.last)
284                         break;
285
286                 if (prev_change != par_.lookupChange(pos).type)
287                         break;
288
289                 char_type c = par_.getChar(pos);
290
291                 if (!isPrintableNonspace(c))
292                         break;
293
294                 if (arabic && Encodings::isComposeChar_arabic(c))
295                         break;
296
297                 if (hebrew && Encodings::isComposeChar_hebrew(c))
298                         break;
299
300                 if (arabic)
301                         c = par_.transformChar(c, pos);
302
303 #if 0
304                 str += c;
305 #else
306                 str.push_back(c);
307 #endif
308         }
309
310         if (prev_change == Change::DELETED)
311                 font.setColor(LColor::strikeout);
312         else if (prev_change == Change::INSERTED)
313                 font.setColor(LColor::newtext);
314
315         // Draw text and set the new x position
316         //lyxerr << "paint row: yo_ " << yo_ << "\n";
317 #if 0
318         int width = pain_.text(int(x_), yo_, str, font);
319 #else
320         int width = pain_.text(int(x_), yo_, &str[0], str.size(), font);
321 #endif
322         x_ += width;
323 }
324
325
326 void RowPainter::paintForeignMark(double orig_x, LyXFont const & font, int desc)
327 {
328         if (!lyxrc.mark_foreign_language)
329                 return;
330         if (font.language() == latex_language)
331                 return;
332         if (font.language() == bv_.buffer()->params().language)
333                 return;
334
335         int const y = yo_ + 1 + desc;
336         pain_.line(int(orig_x), y, int(x_), y, LColor::language);
337 }
338
339
340 void RowPainter::paintFromPos(pos_type & vpos)
341 {
342         pos_type const pos = text_.bidi.vis2log(vpos);
343         LyXFont orig_font = text_.getFont(par_, pos);
344
345         double const orig_x = x_;
346
347         if (par_.isInset(pos)) {
348                 paintInset(pos, orig_font);
349                 ++vpos;
350                 paintForeignMark(orig_x, orig_font,
351                         par_.getInset(pos)->descent());
352                 return;
353         }
354
355         // usual characters, no insets
356         char_type const c = par_.getChar(pos);
357
358         // special case languages
359         std::string const & lang = orig_font.language()->lang();
360         bool const hebrew = lang == "hebrew";
361         bool const arabic = lang == "arabic" &&
362                 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
363                 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
364
365         // draw as many chars as we can
366         if ((!hebrew && !arabic)
367                 || (hebrew && !Encodings::isComposeChar_hebrew(c))
368                 || (arabic && !Encodings::isComposeChar_arabic(c))) {
369                 paintChars(vpos, orig_font, hebrew, arabic);
370         } else if (hebrew) {
371                 paintHebrewComposeChar(vpos, orig_font);
372         } else if (arabic) {
373                 paintArabicComposeChar(vpos, orig_font);
374         }
375
376         paintForeignMark(orig_x, orig_font);
377 }
378
379
380 void RowPainter::paintChangeBar()
381 {
382         pos_type const start = row_.pos();
383         pos_type const end = row_.endpos();
384
385         if (start == end || !par_.isChanged(start, end))
386                 return;
387
388         int const height = text_.isLastRow(pit_, row_)
389                 ? row_.ascent()
390                 : row_.height();
391
392         pain_.fillRectangle(5, yo_ - row_.ascent(), 3, height, LColor::changebar);
393 }
394
395
396 void RowPainter::paintAppendix()
397 {
398         if (!par_.params().appendix())
399                 return;
400
401         int y = yo_ - row_.ascent();
402
403         if (par_.params().startOfAppendix())
404                 y += 2 * defaultRowHeight();
405
406         pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
407         pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), LColor::appendix);
408 }
409
410
411 void RowPainter::paintDepthBar()
412 {
413         depth_type const depth = par_.getDepth();
414
415         if (depth <= 0)
416                 return;
417
418         depth_type prev_depth = 0;
419         if (!text_.isFirstRow(pit_, row_)) {
420                 pit_type pit2 = pit_;
421                 if (row_.pos() == 0)
422                         --pit2;
423                 prev_depth = pars_[pit2].getDepth();
424         }
425
426         depth_type next_depth = 0;
427         if (!text_.isLastRow(pit_, row_)) {
428                 pit_type pit2 = pit_;
429                 if (row_.endpos() >= pars_[pit2].size())
430                         ++pit2;
431                 next_depth = pars_[pit2].getDepth();
432         }
433
434         for (depth_type i = 1; i <= depth; ++i) {
435                 int const w = nestMargin() / 5;
436                 int x = int(xo_) + w * i;
437                 // only consider the changebar space if we're drawing outermost text
438                 if (text_.isMainText())
439                         x += changebarMargin();
440
441                 int const starty = yo_ - row_.ascent();
442                 int const h =  row_.height() - 1 - (i - next_depth - 1) * 3;
443
444                 pain_.line(x, starty, x, starty + h, LColor::depthbar);
445
446                 if (i > prev_depth)
447                         pain_.fillRectangle(x, starty, w, 2, LColor::depthbar);
448                 if (i > next_depth)
449                         pain_.fillRectangle(x, starty + h, w, 2, LColor::depthbar);
450         }
451 }
452
453
454 int RowPainter::paintAppendixStart(int y)
455 {
456         LyXFont pb_font;
457         pb_font.setColor(LColor::appendix);
458         pb_font.decSize();
459
460         int w = 0;
461         int a = 0;
462         int d = 0;
463
464         docstring const label = _("Appendix");
465         theFontMetrics(pb_font).rectText(label, w, a, d);
466
467         int const text_start = int(xo_ + (width_ - w) / 2);
468         int const text_end = text_start + w;
469
470         pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
471
472         pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
473         pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
474
475         return 3 * defaultRowHeight();
476 }
477
478
479 void RowPainter::paintFirst()
480 {
481         ParagraphParameters const & parparams = par_.params();
482
483         int y_top = 0;
484
485         // start of appendix?
486         if (parparams.startOfAppendix())
487                 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
488
489         Buffer const & buffer = *bv_.buffer();
490
491         LyXLayout_ptr const & layout = par_.layout();
492
493         if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
494                 if (pit_ != 0) {
495                         if (layout->latextype == LATEX_PARAGRAPH
496                                 && !par_.getDepth()) {
497                                 y_top += buffer.params().getDefSkip().inPixels(bv_);
498                         } else {
499                                 LyXLayout_ptr const & playout = pars_[pit_ - 1].layout();
500                                 if (playout->latextype == LATEX_PARAGRAPH
501                                         && !pars_[pit_ - 1].getDepth()) {
502                                         // is it right to use defskip here, too? (AS)
503                                         y_top += buffer.params().getDefSkip().inPixels(bv_);
504                                 }
505                         }
506                 }
507         }
508
509         bool const is_rtl = text_.isRTL(par_);
510         bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
511         //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
512
513         // should we print a label?
514         if (layout->labeltype >= LABEL_STATIC
515             && (layout->labeltype != LABEL_STATIC
516                       || layout->latextype != LATEX_ENVIRONMENT
517                       || is_seq)) {
518
519                 LyXFont const font = getLabelFont();
520                 FontMetrics const & fm = theFontMetrics(font);
521
522                 docstring const str = par_.getLabelstring();
523                 if (!str.empty()) {
524                         double x = x_;
525
526                         // this is special code for the chapter layout. This is
527                         // printed in an extra row and has a pagebreak at
528                         // the top.
529                         if (layout->counter == "chapter") {
530                                 double spacing_val = 1.0;
531                                 if (!parparams.spacing().isDefault()) {
532                                         spacing_val = parparams.spacing().getValue();
533                                 } else {
534                                         spacing_val = buffer.params().spacing().getValue();
535                                 }
536
537                                 int const labeladdon = int(fm.maxHeight() * layout->spacing.getValue() * spacing_val);
538
539                                 int const maxdesc = int(fm.maxDescent() * layout->spacing.getValue() * spacing_val)
540                                         + int(layout->parsep) * defaultRowHeight();
541
542                                 if (is_rtl) {
543                                         x = width_ - leftMargin() -
544                                                 fm.width(str);
545                                 }
546
547                                 pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
548                         } else {
549                                 // FIXME UNICODE
550                                 docstring lab = from_utf8(layout->labelsep);
551                                 if (is_rtl) {
552                                         x = width_ - leftMargin()
553                                                 + fm.width(lab);
554                                 } else {
555                                         x = x_ - fm.width(lab)
556                                                 - fm.width(str);
557                                 }
558
559                                 pain_.text(int(x), yo_, str, font);
560                         }
561                 }
562
563         // the labels at the top of an environment.
564         // More or less for bibliography
565         } else if (is_seq &&
566                 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
567                 layout->labeltype == LABEL_BIBLIO ||
568                 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
569                 LyXFont font = getLabelFont();
570                 if (!par_.getLabelstring().empty()) {
571                         docstring const str = par_.getLabelstring();
572                         double spacing_val = 1.0;
573                         if (!parparams.spacing().isDefault())
574                                 spacing_val = parparams.spacing().getValue();
575                         else
576                                 spacing_val = buffer.params().spacing().getValue();
577
578                         FontMetrics const & fm = theFontMetrics(font);
579
580                         int const labeladdon = int(fm.maxHeight()
581                                 * layout->spacing.getValue() * spacing_val);
582
583                         int maxdesc =
584                                 int(fm.maxDescent() * layout->spacing.getValue() * spacing_val
585                                 + (layout->labelbottomsep * defaultRowHeight()));
586
587                         double x = x_;
588                         if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
589                                 if (is_rtl)
590                                         x = leftMargin();
591                                 x += (width_ - text_.rightMargin(par_) - leftMargin()) / 2;
592                                 x -= fm.width(str) / 2;
593                         } else if (is_rtl) {
594                                 x = width_ - leftMargin() -     fm.width(str);
595                         }
596                         pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
597                 }
598         }
599 }
600
601
602 void RowPainter::paintLast()
603 {
604         bool const is_rtl = text_.isRTL(par_);
605         int const endlabel = getEndLabel(pit_, text_.paragraphs());
606
607         // draw an endlabel
608         switch (endlabel) {
609         case END_LABEL_BOX:
610         case END_LABEL_FILLED_BOX: {
611                 LyXFont const font = getLabelFont();
612                 FontMetrics const & fm = theFontMetrics(font);
613                 int const size = int(0.75 * fm.maxAscent());
614                 int const y = yo_ - size;
615                 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
616
617                 if (width_ - int(row_.width()) <= size)
618                         x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
619
620                 if (endlabel == END_LABEL_BOX)
621                         pain_.rectangle(x, y, size, size, LColor::eolmarker);
622                 else
623                         pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
624                 break;
625         }
626
627         case END_LABEL_STATIC: {
628                 LyXFont font = getLabelFont();
629                 FontMetrics const & fm = theFontMetrics(font);
630                 docstring const & str = par_.layout()->endlabelstring();
631                 double const x = is_rtl ?
632                         x_ - fm.width(str)
633                         : - text_.rightMargin(par_) - row_.width();
634                 pain_.text(int(x), yo_, str, font);
635                 break;
636         }
637
638         case END_LABEL_NO_LABEL:
639                 break;
640         }
641 }
642
643
644 void RowPainter::paintText()
645 {
646         pos_type const end = row_.endpos();
647         pos_type body_pos = par_.beginOfBody();
648         if (body_pos > 0 &&
649                 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
650                 body_pos = 0;
651         }
652
653         LyXLayout_ptr const & layout = par_.layout();
654
655         bool running_strikeout = false;
656         bool is_struckout = false;
657         int last_strikeout_x = 0;
658
659         // Use font span to speed things up, see below
660         FontSpan font_span;
661         LyXFont font;
662
663         for (pos_type vpos = row_.pos(); vpos < end; ) {
664                 if (x_ > bv_.workWidth())
665                         break;
666
667                 pos_type const pos = text_.bidi.vis2log(vpos);
668
669                 if (pos >= par_.size()) {
670                         ++vpos;
671                         continue;
672                 }
673
674                 // Use font span to speed things up, see above
675                 if (vpos < font_span.first || vpos > font_span.last) {
676                         font_span = par_.fontSpan(vpos);
677                         font = text_.getFont(par_, vpos);
678                 }
679
680                 const int width_pos =
681                         text_.singleWidth(par_, pos, par_.getChar(pos), font);
682
683                 if (x_ + width_pos < 0) {
684                         x_ += width_pos;
685                         ++vpos;
686                         continue;
687                 }
688
689                 is_struckout = par_.isDeleted(pos);
690
691                 if (is_struckout && !running_strikeout) {
692                         running_strikeout = true;
693                         last_strikeout_x = int(x_);
694                 }
695
696                 bool const highly_editable_inset = par_.isInset(pos)
697                         && isHighlyEditableInset(par_.getInset(pos));
698
699                 // If we reach the end of a struck out range, paint it.
700                 // We also don't paint across things like tables
701                 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
702                         // Calculate 1/3 height of the buffer's default font
703                         FontMetrics const & fm 
704                                 = theFontMetrics(bv_.buffer()->params().getFont());
705                         int const middle = yo_ - fm.maxAscent() / 3;
706                         pain_.line(last_strikeout_x, middle, int(x_), middle,
707                                 LColor::strikeout, Painter::line_solid, Painter::line_thin);
708                         running_strikeout = false;
709                 }
710
711                 if (body_pos > 0 && pos == body_pos - 1) {
712                         // FIXME UNICODE
713                         int const lwidth = theFontMetrics(getLabelFont())
714                                 .width(from_utf8(layout->labelsep));
715
716                         x_ += label_hfill_ + lwidth - width_pos;
717                 }
718
719                 if (par_.isHfill(pos)) {
720                         x_ += 1;
721
722                         int const y0 = yo_;
723                         int const y1 = y0 - defaultRowHeight() / 2;
724
725                         pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
726
727                         if (hfillExpansion(par_, row_, pos)) {
728                                 int const y2 = (y0 + y1) / 2;
729
730                                 if (pos >= body_pos) {
731                                         pain_.line(int(x_), y2, int(x_ + hfill_), y2,
732                                                   LColor::added_space,
733                                                   Painter::line_onoffdash);
734                                         x_ += hfill_;
735                                 } else {
736                                         pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
737                                                   LColor::added_space,
738                                                   Painter::line_onoffdash);
739                                         x_ += label_hfill_;
740                                 }
741                                 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
742                         }
743                         x_ += 2;
744                         ++vpos;
745                 } else if (par_.isSeparator(pos)) {
746                         x_ += width_pos;
747                         if (pos >= body_pos)
748                                 x_ += separator_;
749                         ++vpos;
750                 } else {
751                         paintFromPos(vpos);
752                 }
753         }
754
755         // if we reach the end of a struck out range, paint it
756         if (running_strikeout) {
757                 // calculate 1/3 height of the buffer's default font
758                 FontMetrics const & fm 
759                         = theFontMetrics(bv_.buffer()->params().getFont());
760                 int const middle = yo_ - fm.maxAscent() / 3;
761                 pain_.line(last_strikeout_x, middle, int(x_), middle,
762                         LColor::strikeout, Painter::line_solid, Painter::line_thin);
763                 running_strikeout = false;
764         }
765 }
766
767
768 size_type calculateRowSignature(Row const & row, Paragraph const & par,
769         int x, int y)
770 {
771         boost::crc_32_type crc;
772         for (pos_type i = row.pos(); i < row.endpos(); ++i) {
773                 const unsigned char b[] = { par.getChar(i) };
774                 crc.process_bytes(b, 1);
775         }
776         const unsigned char b[] = { x, y, row.width() };
777         crc.process_bytes(b, 3);
778         return crc.checksum();
779 }
780
781
782 bool CursorOnRow(PainterInfo & pi, pit_type const pit,
783         RowList::const_iterator rit, LyXText const & text)
784 {
785         // Is there a cursor on this row (or inside inset on row)
786         LCursor & cur = pi.base.bv->cursor();
787         for (size_type d = 0; d < cur.depth(); ++d) {
788                 CursorSlice const & sl = cur[d];
789                 if (sl.text() == &text
790                     && sl.pit() == pit
791                     && sl.pos() >= rit->pos()
792                     && sl.pos() <= rit->endpos())
793                         return true;
794         }
795         return false;
796 }
797
798
799 bool innerCursorOnRow(PainterInfo & pi, pit_type pit,
800         RowList::const_iterator rit, LyXText const & text)
801 {
802         // Is there a cursor inside an inset on this row, and is this inset
803         // the only "character" on this row
804         LCursor & cur = pi.base.bv->cursor();
805         if (rit->pos() + 1 != rit->endpos())
806                 return false;
807         for (size_type d = 0; d < cur.depth(); d++) {
808                 CursorSlice const & sl = cur[d];
809                 if (sl.text() == &text
810                     && sl.pit() == pit
811                     && sl.pos() == rit->pos())
812                         return d < cur.depth() - 1;
813         }
814         return false;
815 }
816
817
818 void paintPar
819         (PainterInfo & pi, LyXText const & text, pit_type pit, int x, int y,
820          bool repaintAll)
821 {
822 //      lyxerr << "  paintPar: pit: " << pit << " at y: " << y << endl;
823         static NullPainter nop;
824         static PainterInfo nullpi(pi.base.bv, nop);
825         int const ww = pi.base.bv->workHeight();
826
827         pi.base.bv->coordCache().parPos()[&text][pit] = Point(x, y);
828
829         Paragraph const & par = text.paragraphs()[pit];
830         if (par.rows().empty())
831                 return;
832
833         RowList::const_iterator const rb = par.rows().begin();
834         RowList::const_iterator const re = par.rows().end();
835
836         y -= rb->ascent();
837         size_type rowno = 0;
838         for (RowList::const_iterator rit = rb; rit != re; ++rit, ++rowno) {
839                 y += rit->ascent();
840                 // Allow setting of refreshInside for nested insets in
841                 // this row only
842                 bool tmp = refreshInside;
843
844                 // Row signature; has row changed since last paint?
845                 size_type const row_sig = calculateRowSignature(*rit, par, x, y);
846                 bool row_has_changed = par.rowSignature()[rowno] != row_sig;
847
848                 bool cursor_on_row = CursorOnRow(pi, pit, rit, text);
849                 bool in_inset_alone_on_row = innerCursorOnRow(pi, pit, rit,
850                         text);
851
852                 // If this is the only object on the row, we can make it wide
853                 for (pos_type i = rit->pos() ; i != rit->endpos(); ++i) {
854                         InsetBase const * const in = par.getInset(i);
855                         if (in) {
856                                 InsetText const * const t = in->asTextInset();
857                                 if (t)
858                                         t->Wide() = in_inset_alone_on_row;
859                         }
860                 }
861
862                 // If selection is on, the current row signature differs
863                 // from cache, or cursor is inside an inset _on this row_,
864                 // then paint the row
865                 if (repaintAll || row_has_changed || cursor_on_row) {
866                         // Add to row signature cache
867                         par.rowSignature()[rowno] = row_sig;
868
869                         bool const inside = (y + rit->descent() >= 0
870                                        && y - rit->ascent() < ww);
871                         RowPainter rp(inside ? pi : nullpi, text, pit, *rit, x, y);
872                         // Clear background of this row
873                         // (if paragraph background was not cleared)
874                         if (!repaintAll &&
875                             (!in_inset_alone_on_row || row_has_changed)) {
876                                 pi.pain.fillRectangle(x, y - rit->ascent(),
877                                     text.maxwidth_, rit->height(),
878                                     text.backgroundColor());
879                                 // If outer row has changed, force nested
880                                 // insets to repaint completely
881                                 if (row_has_changed)
882                                         refreshInside = true;
883                         }
884
885                         // Instrumentation for testing row cache (see also
886                         // 12 lines lower):
887                         if (text.isMainText())
888                                 lyxerr[Debug::PAINTING] << "#";
889                         else
890                                 lyxerr[Debug::PAINTING] << "[" <<
891                                     repaintAll << row_has_changed <<
892                                     cursor_on_row << "]";
893                         rp.paintAppendix();
894                         rp.paintDepthBar();
895                         rp.paintChangeBar();
896                         if (rit == rb)
897                                 rp.paintFirst();
898                         if (rit + 1 == re)
899                                 rp.paintLast();
900                         rp.paintText();
901                 }
902                 y += rit->descent();
903                 // Restore, see above
904                 refreshInside = tmp;
905         }
906         lyxerr[Debug::PAINTING] << "." << endl;
907 }
908
909 } // namespace anon
910
911
912 void paintText(BufferView & bv, ViewMetricsInfo const & vi,
913                Painter & pain)
914 {
915         LyXText & text = bv.buffer()->text();
916         bool const select = bv.cursor().selection();
917
918         PainterInfo pi(const_cast<BufferView *>(&bv), pain);
919         // Should the whole screen, including insets, be refreshed?
920         bool repaintAll = select || !vi.singlepar;
921
922         if (repaintAll) {
923                 // Clear background (if not delegated to rows)
924                 pain.fillRectangle(0, vi.y1, bv.workWidth(), vi.y2 - vi.y1,
925                         text.backgroundColor());
926         }
927         if (select) {
928                 text.drawSelection(pi, 0, 0);
929         }
930
931         int yy = vi.y1;
932         // draw contents
933         for (pit_type pit = vi.p1; pit <= vi.p2; ++pit) {
934                 refreshInside = repaintAll;
935                 Paragraph const & par = text.getPar(pit);
936                 yy += par.ascent();
937                 paintPar(pi, text, pit, 0, yy, repaintAll);
938                 yy += par.descent();
939         }
940
941         // Cache one paragraph above and one below
942         // Note MV: this cannot be suppressed even for singlepar.
943         // Try viewing the User Guide Mobius figure
944
945         if (vi.p1 > 0) {
946                 text.redoParagraph(vi.p1 - 1);
947                 bv.coordCache().parPos()[&text][vi.p1 - 1] =
948                         Point(0, vi.y1 - text.getPar(vi.p1 - 1).descent());
949         }
950
951         if (vi.p2 < pit_type(text.paragraphs().size()) - 1) {
952                 text.redoParagraph(vi.p2 + 1);
953                 bv.coordCache().parPos()[&text][vi.p2 + 1] =
954                         Point(0, vi.y2 + text.getPar(vi.p2 + 1).ascent());
955         }
956
957         // and grey out above (should not happen later)
958 //      lyxerr << "par ascent: " << text.getPar(vi.p1).ascent() << endl;
959         if (vi.y1 > 0 && !vi.singlepar)
960                 pain.fillRectangle(0, 0, bv.workWidth(), vi.y1, LColor::bottomarea);
961
962         // and possibly grey out below
963 //      lyxerr << "par descent: " << text.getPar(vi.p1).ascent() << endl;
964         if (vi.y2 < bv.workHeight() && !vi.singlepar)
965                 pain.fillRectangle(0, vi.y2, bv.workWidth(), bv.workHeight() - vi.y2, LColor::bottomarea);
966 }
967
968
969 void paintTextInset(LyXText const & text, PainterInfo & pi, int x, int y)
970 {
971 //      lyxerr << "  paintTextInset: y: " << y << endl;
972
973         y -= text.getPar(0).ascent();
974         // This flag can not be set from within same inset:
975         bool repaintAll = refreshInside;
976         for (int pit = 0; pit < int(text.paragraphs().size()); ++pit) {
977                 y += text.getPar(pit).ascent();
978                 paintPar(pi, text, pit, x, y, repaintAll);
979                 y += text.getPar(pit).descent();
980         }
981 }
982
983
984 } // namespace lyx