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