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