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