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