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