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