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