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