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