]> git.lyx.org Git - lyx.git/blob - src/rowpainter.C
This commit is a big rework of the FontLoader/FontMetrics interaction. Only Qt4 for...
[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/Application.h"
36 #include "frontends/FontLoader.h"
37 #include "frontends/FontMetrics.h"
38 #include "frontends/nullpainter.h"
39 #include "frontends/Painter.h"
40
41 #include "insets/insettext.h"
42
43 #include "support/textutils.h"
44
45 #include <boost/crc.hpp>
46
47 using lyx::docstring;
48 using lyx::frontend::Painter;
49 using lyx::frontend::NullPainter;
50 using lyx::frontend::FontMetrics;
51
52 using lyx::char_type;
53 using lyx::pos_type;
54 using lyx::pit_type;
55
56 using std::endl;
57 using std::max;
58 using std::min;
59 using std::string;
60
61
62 namespace {
63
64 /// Flag: do a full redraw of inside text of inset
65 /// Working variable indicating a full screen refresh
66 bool refreshInside;
67
68 /**
69  * A class used for painting an individual row of text.
70  */
71 class RowPainter {
72 public:
73         /// initialise and run painter
74         RowPainter(PainterInfo & pi, LyXText const & text,
75                 pit_type pit, Row const & row, int x, int y);
76
77         // paint various parts
78         void paintAppendix();
79         void paintDepthBar();
80         void paintChangeBar();
81         void paintFirst();
82         void paintLast();
83         void paintText();
84
85 private:
86         void paintForeignMark(double orig_x, LyXFont const & font, int desc = 0);
87         void paintHebrewComposeChar(lyx::pos_type & vpos, LyXFont const & font);
88         void paintArabicComposeChar(lyx::pos_type & vpos, LyXFont const & font);
89         void paintChars(lyx::pos_type & vpos, LyXFont font,
90                         bool hebrew, bool arabic);
91         int paintAppendixStart(int y);
92         void paintFromPos(lyx::pos_type & vpos);
93         void paintInset(lyx::pos_type const pos, LyXFont const & font);
94
95         /// return left margin
96         int leftMargin() const;
97
98         /// return the label font for this row
99         LyXFont const getLabelFont() const;
100
101         /// bufferview to paint on
102         BufferView const & bv_;
103
104         /// Painter to use
105         Painter & pain_;
106
107         /// LyXText for the row
108         LyXText const & text_;
109         ParagraphList const & pars_;
110
111         /// The row to paint
112         Row const & row_;
113
114         /// Row's paragraph
115         pit_type const pit_;
116         Paragraph const & par_;
117
118         /// is row erased? (change tracking)
119         bool erased_;
120
121         // Looks ugly - is
122         double const xo_;
123         int const yo_;    // current baseline
124         double x_;
125         int width_;
126         double separator_;
127         double hfill_;
128         double label_hfill_;
129 };
130
131
132 RowPainter::RowPainter(PainterInfo & pi,
133         LyXText const & text, pit_type pit, Row const & row, int x, int y)
134         : bv_(*pi.base.bv), pain_(pi.pain), text_(text), pars_(text.paragraphs()),
135           row_(row), pit_(pit), par_(text.paragraphs()[pit]),
136           erased_(pi.erased_),
137           xo_(x), yo_(y), width_(text_.width())
138 {
139         RowMetrics m = text_.computeRowMetrics(pit, row_);
140         x_ = m.x + xo_;
141
142         //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
143         //row_.dump();
144
145         separator_ = m.separator;
146         hfill_ = m.hfill;
147         label_hfill_ = m.label_hfill;
148
149         BOOST_ASSERT(pit >= 0);
150         BOOST_ASSERT(pit < int(text.paragraphs().size()));
151 }
152
153
154 LyXFont const RowPainter::getLabelFont() const
155 {
156         return text_.getLabelFont(par_);
157 }
158
159
160 int RowPainter::leftMargin() const
161 {
162         return text_.leftMargin(pit_, row_.pos());
163 }
164
165
166 void RowPainter::paintInset(pos_type const pos, LyXFont const & font)
167 {
168         InsetBase const * inset = par_.getInset(pos);
169         BOOST_ASSERT(inset);
170         PainterInfo pi(const_cast<BufferView *>(&bv_), pain_);
171         // FIXME: We should always use font, see documentation of
172         // noFontChange() in insetbase.h.
173         pi.base.font = inset->noFontChange() ?
174                 bv_.buffer()->params().getFont() :
175                 font;
176         pi.ltr_pos = (text_.bidi.level(pos) % 2 == 0);
177         pi.erased_ = erased_ || isDeletedText(par_, pos);
178         theCoords.insets().add(inset, int(x_), yo_);
179         InsetText const * const in = inset->asTextInset();
180         // non-wide insets are painted completely. Recursive
181         bool tmp = refreshInside;
182         if (!in || !in->Wide()) {
183                 refreshInside = true;
184                 lyxerr[Debug::PAINTING] << endl << "Paint inset fully" << endl;
185         }
186         if (refreshInside)
187                 inset->drawSelection(pi, int(x_), yo_);
188         inset->draw(pi, int(x_), yo_);
189         refreshInside = tmp;
190         x_ += inset->width();
191 }
192
193
194 void RowPainter::paintHebrewComposeChar(pos_type & vpos, LyXFont const & font)
195 {
196         pos_type pos = text_.bidi.vis2log(vpos);
197
198         string str;
199
200         // first char
201         char_type c = par_.getChar(pos);
202         str += c;
203         ++vpos;
204
205         int const width = theApp->fontLoader().metrics(font).width(c);
206         int dx = 0;
207
208         for (pos_type i = pos - 1; i >= 0; --i) {
209                 c = par_.getChar(i);
210                 if (!Encodings::isComposeChar_hebrew(c)) {
211                         if (isPrintableNonspace(c)) {
212                                 int const width2 =
213                                         text_.singleWidth(par_, i, c, text_.getFont(par_, i));
214                                 // dalet / resh
215                                 dx = (c == 'ø' || c == 'ã')
216                                         ? width2 - width
217                                         : (width2 - width) / 2;
218                         }
219                         break;
220                 }
221         }
222
223         // Draw nikud
224         docstring dstr(str.begin(), str.end());
225         pain_.text(int(x_) + dx, yo_, dstr, font);
226 }
227
228
229 void RowPainter::paintArabicComposeChar(pos_type & vpos, LyXFont const & font)
230 {
231         pos_type pos = text_.bidi.vis2log(vpos);
232         string str;
233
234         // first char
235         char_type c = par_.getChar(pos);
236         c = par_.transformChar(c, pos);
237         str += c;
238         ++vpos;
239
240         int const width = theApp->fontLoader().metrics(font).width(c);
241         int dx = 0;
242
243         for (pos_type i = pos - 1; i >= 0; --i) {
244                 c = par_.getChar(i);
245                 if (!Encodings::isComposeChar_arabic(c)) {
246                         if (isPrintableNonspace(c)) {
247                                 int const width2 =
248                                         text_.singleWidth(par_, i, c, text_.getFont(par_, i));
249                                 dx = (width2 - width) / 2;
250                         }
251                         break;
252                 }
253         }
254         // Draw nikud
255         docstring dstr(str.begin(), str.end());
256         pain_.text(int(x_) + dx, yo_, dstr, font);
257 }
258
259
260 void RowPainter::paintChars(pos_type & vpos, LyXFont font,
261                             bool hebrew, bool arabic)
262 {
263         pos_type pos = text_.bidi.vis2log(vpos);
264         pos_type const end = row_.endpos();
265         FontSpan const font_span = par_.fontSpan(pos);
266         Change::Type const prev_change = par_.lookupChange(pos).type;
267
268         // first character
269 #if 0
270         string str;
271         str += par_.getChar(pos);
272 #else
273         std::vector<char_type> str;
274         str.push_back(par_.getChar(pos));
275 #endif
276         if (arabic) {
277                 char_type c = str[0];
278                 str[0] = par_.transformChar(c, pos);
279         }
280
281         // collect as much similar chars as we can
282         for (++vpos ; vpos < end ; ++vpos) {
283                 pos = text_.bidi.vis2log(vpos);
284                 if (pos < font_span.first || pos > font_span.last)
285                         break;
286
287                 if (prev_change != par_.lookupChange(pos))
288                         break;
289
290                 char_type c = par_.getChar(pos);
291
292                 if (!isPrintableNonspace(c))
293                         break;
294
295                 if (arabic && Encodings::isComposeChar_arabic(c))
296                         break;
297
298                 if (hebrew && Encodings::isComposeChar_hebrew(c))
299                         break;
300
301                 if (arabic)
302                         c = par_.transformChar(c, pos);
303
304 #if 0
305                 str += c;
306 #else
307                 str.push_back(c);
308 #endif
309         }
310
311         if (prev_change == Change::DELETED)
312                 font.setColor(LColor::strikeout);
313         else if (prev_change == Change::INSERTED)
314                 font.setColor(LColor::newtext);
315
316         // Draw text and set the new x position
317         //lyxerr << "paint row: yo_ " << yo_ << "\n";
318 #if 0
319         pain_.text(int(x_), yo_, str, font);
320         x_ += theApp->fontLoader().metrics(font).width(str);
321 #else
322         pain_.text(int(x_), yo_, &str[0], str.size(), font);
323         x_ += theApp->fontLoader().metrics(font).width(&str[0], str.size());
324 #endif
325 }
326
327
328 void RowPainter::paintForeignMark(double orig_x, LyXFont const & font, int desc)
329 {
330         if (!lyxrc.mark_foreign_language)
331                 return;
332         if (font.language() == latex_language)
333                 return;
334         if (font.language() == bv_.buffer()->params().language)
335                 return;
336
337         int const y = yo_ + 1 + desc;
338         pain_.line(int(orig_x), y, int(x_), y, LColor::language);
339 }
340
341
342 void RowPainter::paintFromPos(pos_type & vpos)
343 {
344         pos_type const pos = text_.bidi.vis2log(vpos);
345         LyXFont orig_font = text_.getFont(par_, pos);
346
347         double const orig_x = x_;
348
349         if (par_.isInset(pos)) {
350                 paintInset(pos, orig_font);
351                 ++vpos;
352                 paintForeignMark(orig_x, orig_font,
353                         par_.getInset(pos)->descent());
354                 return;
355         }
356
357         // usual characters, no insets
358         char_type const c = par_.getChar(pos);
359
360         // special case languages
361         std::string const & lang = orig_font.language()->lang();
362         bool const hebrew = lang == "hebrew";
363         bool const arabic = lang == "arabic" &&
364                 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
365                 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
366
367         // draw as many chars as we can
368         if ((!hebrew && !arabic)
369                 || (hebrew && !Encodings::isComposeChar_hebrew(c))
370                 || (arabic && !Encodings::isComposeChar_arabic(c))) {
371                 paintChars(vpos, orig_font, hebrew, arabic);
372         } else if (hebrew) {
373                 paintHebrewComposeChar(vpos, orig_font);
374         } else if (arabic) {
375                 paintArabicComposeChar(vpos, orig_font);
376         }
377
378         paintForeignMark(orig_x, orig_font);
379 }
380
381
382 void RowPainter::paintChangeBar()
383 {
384         pos_type const start = row_.pos();
385         pos_type const end = row_.endpos();
386
387         if (start == end || !par_.isChanged(start, end))
388                 return;
389
390         int const height = text_.isLastRow(pit_, row_)
391                 ? row_.ascent()
392                 : row_.height();
393
394         pain_.fillRectangle(5, yo_ - row_.ascent(), 3, height, LColor::changebar);
395 }
396
397
398 void RowPainter::paintAppendix()
399 {
400         if (!par_.params().appendix())
401                 return;
402
403         int y = yo_ - row_.ascent();
404
405         if (par_.params().startOfAppendix())
406                 y += 2 * defaultRowHeight();
407
408         pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
409         pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), LColor::appendix);
410 }
411
412
413 void RowPainter::paintDepthBar()
414 {
415         Paragraph::depth_type const depth = par_.getDepth();
416
417         if (depth <= 0)
418                 return;
419
420         Paragraph::depth_type prev_depth = 0;
421         if (!text_.isFirstRow(pit_, row_)) {
422                 pit_type pit2 = pit_;
423                 if (row_.pos() == 0)
424                         --pit2;
425                 prev_depth = pars_[pit2].getDepth();
426         }
427
428         Paragraph::depth_type next_depth = 0;
429         if (!text_.isLastRow(pit_, row_)) {
430                 pit_type pit2 = pit_;
431                 if (row_.endpos() >= pars_[pit2].size())
432                         ++pit2;
433                 next_depth = pars_[pit2].getDepth();
434         }
435
436         for (Paragraph::depth_type i = 1; i <= depth; ++i) {
437                 int const w = nestMargin() / 5;
438                 int x = int(xo_) + w * i;
439                 // only consider the changebar space if we're drawing outermost text
440                 if (text_.isMainText())
441                         x += changebarMargin();
442
443                 int const starty = yo_ - row_.ascent();
444                 int const h =  row_.height() - 1 - (i - next_depth - 1) * 3;
445
446                 pain_.line(x, starty, x, starty + h, LColor::depthbar);
447
448                 if (i > prev_depth)
449                         pain_.fillRectangle(x, starty, w, 2, LColor::depthbar);
450                 if (i > next_depth)
451                         pain_.fillRectangle(x, starty + h, w, 2, LColor::depthbar);
452         }
453 }
454
455
456 int RowPainter::paintAppendixStart(int y)
457 {
458         LyXFont pb_font;
459         pb_font.setColor(LColor::appendix);
460         pb_font.decSize();
461
462         string const label = lyx::to_utf8(_("Appendix"));
463         int w = 0;
464         int a = 0;
465         int d = 0;
466         docstring dlab(label.begin(), label.end());
467         theApp->fontLoader().metrics(pb_font).rectText(dlab, w, a, d);
468
469         int const text_start = int(xo_ + (width_ - w) / 2);
470         int const text_end = text_start + w;
471
472         pain_.rectText(text_start, y + d, dlab, pb_font, LColor::none, LColor::none);
473
474         pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
475         pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
476
477         return 3 * defaultRowHeight();
478 }
479
480
481 void RowPainter::paintFirst()
482 {
483         ParagraphParameters const & parparams = par_.params();
484
485         int y_top = 0;
486
487         // start of appendix?
488         if (parparams.startOfAppendix())
489                 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
490
491         Buffer const & buffer = *bv_.buffer();
492
493         LyXLayout_ptr const & layout = par_.layout();
494
495         if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
496                 if (pit_ != 0) {
497                         if (layout->latextype == LATEX_PARAGRAPH
498                                 && !par_.getDepth()) {
499                                 y_top += buffer.params().getDefSkip().inPixels(bv_);
500                         } else {
501                                 LyXLayout_ptr const & playout = pars_[pit_ - 1].layout();
502                                 if (playout->latextype == LATEX_PARAGRAPH
503                                         && !pars_[pit_ - 1].getDepth()) {
504                                         // is it right to use defskip here, too? (AS)
505                                         y_top += buffer.params().getDefSkip().inPixels(bv_);
506                                 }
507                         }
508                 }
509         }
510
511         bool const is_rtl = text_.isRTL(par_);
512         bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
513         //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
514
515         // should we print a label?
516         if (layout->labeltype >= LABEL_STATIC
517             && (layout->labeltype != LABEL_STATIC
518                       || layout->latextype != LATEX_ENVIRONMENT
519                       || is_seq)) {
520
521                 LyXFont const font = getLabelFont();
522                 FontMetrics const & fm =
523                         theApp->fontLoader().metrics(font);
524
525                 string const str = par_.getLabelstring();
526                 if (!str.empty()) {
527                         double x = x_;
528                         docstring dstr(str.begin(), str.end());
529
530                         // this is special code for the chapter layout. This is
531                         // printed in an extra row and has a pagebreak at
532                         // the top.
533                         if (layout->counter == "chapter") {
534                                 double spacing_val = 1.0;
535                                 if (!parparams.spacing().isDefault()) {
536                                         spacing_val = parparams.spacing().getValue();
537                                 } else {
538                                         spacing_val = buffer.params().spacing().getValue();
539                                 }
540
541                                 int const labeladdon = int(fm.maxHeight() * layout->spacing.getValue() * spacing_val);
542
543                                 int const maxdesc = int(fm.maxDescent() * layout->spacing.getValue() * spacing_val)
544                                         + int(layout->parsep) * defaultRowHeight();
545
546                                 if (is_rtl) {
547                                         x = width_ - leftMargin() -
548                                                 fm.width(dstr);
549                                 }
550
551                                 pain_.text(int(x), yo_ - maxdesc - labeladdon, dstr, font);
552                         } else {
553                                 string lab = layout->labelsep;
554                                 docstring dlab(lab.begin(), lab.end());
555                                 if (is_rtl) {
556                                         x = width_ - leftMargin()
557                                                 + fm.width(dlab);
558                                 } else {
559                                         x = x_ - fm.width(dlab)
560                                                 - fm.width(dstr);
561                                 }
562
563                                 pain_.text(int(x), yo_, dstr, font);
564                         }
565                 }
566
567         // the labels at the top of an environment.
568         // More or less for bibliography
569         } else if (is_seq &&
570                 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
571                 layout->labeltype == LABEL_BIBLIO ||
572                 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
573                 LyXFont font = getLabelFont();
574                 if (!par_.getLabelstring().empty()) {
575                         string const str = par_.getLabelstring();
576                         docstring dstr(str.begin(), str.end());
577                         double spacing_val = 1.0;
578                         if (!parparams.spacing().isDefault())
579                                 spacing_val = parparams.spacing().getValue();
580                         else
581                                 spacing_val = buffer.params().spacing().getValue();
582
583                         FontMetrics const & fm =
584                                 theApp->fontLoader().metrics(font);
585
586                         int const labeladdon = int(fm.maxHeight()
587                                 * layout->spacing.getValue() * spacing_val);
588
589                         int maxdesc =
590                                 int(fm.maxDescent() * layout->spacing.getValue() * spacing_val
591                                 + (layout->labelbottomsep * defaultRowHeight()));
592
593                         double x = x_;
594                         if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
595                                 if (is_rtl)
596                                         x = leftMargin();
597                                 x += (width_ - text_.rightMargin(par_) - leftMargin()) / 2;
598                                 x -= fm.width(dstr) / 2;
599                         } else if (is_rtl) {
600                                 x = width_ - leftMargin() -     fm.width(dstr);
601                         }
602                         pain_.text(int(x), yo_ - maxdesc - labeladdon, dstr, font);
603                 }
604         }
605 }
606
607
608 void RowPainter::paintLast()
609 {
610         bool const is_rtl = text_.isRTL(par_);
611         int const endlabel = getEndLabel(pit_, text_.paragraphs());
612
613         // draw an endlabel
614         switch (endlabel) {
615         case END_LABEL_BOX:
616         case END_LABEL_FILLED_BOX: {
617                 LyXFont const font = getLabelFont();
618                 FontMetrics const & fm = theApp->fontLoader().metrics(font);
619                 int const size = int(0.75 * fm.maxAscent());
620                 int const y = yo_ - size;
621                 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
622
623                 if (width_ - int(row_.width()) <= size)
624                         x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
625
626                 if (endlabel == END_LABEL_BOX)
627                         pain_.rectangle(x, y, size, size, LColor::eolmarker);
628                 else
629                         pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
630                 break;
631         }
632
633         case END_LABEL_STATIC: {
634                 LyXFont font = getLabelFont();
635                 FontMetrics const & fm = theApp->fontLoader().metrics(font);
636                 string const & str = par_.layout()->endlabelstring();
637                 docstring dstr(str.begin(), str.end());
638                 double const x = is_rtl ?
639                         x_ - fm.width(dstr)
640                         : - text_.rightMargin(par_) - row_.width();
641                 pain_.text(int(x), yo_, dstr, font);
642                 break;
643         }
644
645         case END_LABEL_NO_LABEL:
646                 break;
647         }
648 }
649
650
651 void RowPainter::paintText()
652 {
653         pos_type const end = row_.endpos();
654         pos_type body_pos = par_.beginOfBody();
655         if (body_pos > 0 &&
656                 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
657                 body_pos = 0;
658         }
659
660         LyXLayout_ptr const & layout = par_.layout();
661
662         bool running_strikeout = false;
663         bool is_struckout = false;
664         int last_strikeout_x = 0;
665
666         // Use font span to speed things up, see below
667         FontSpan font_span;
668         LyXFont font;
669
670         for (pos_type vpos = row_.pos(); vpos < end; ) {
671                 if (x_ > bv_.workWidth())
672                         break;
673
674                 pos_type const pos = text_.bidi.vis2log(vpos);
675
676                 if (pos >= par_.size()) {
677                         ++vpos;
678                         continue;
679                 }
680
681                 // Use font span to speed things up, see above
682                 if (vpos < font_span.first || vpos > font_span.last) {
683                         font_span = par_.fontSpan(vpos);
684                         font = text_.getFont(par_, vpos);
685                 }
686
687                 const int width_pos =
688                         text_.singleWidth(par_, pos, par_.getChar(pos), font);
689
690                 if (x_ + width_pos < 0) {
691                         x_ += width_pos;
692                         ++vpos;
693                         continue;
694                 }
695
696                 is_struckout = isDeletedText(par_, pos);
697
698                 if (is_struckout && !running_strikeout) {
699                         running_strikeout = true;
700                         last_strikeout_x = int(x_);
701                 }
702
703                 bool const highly_editable_inset = par_.isInset(pos)
704                         && isHighlyEditableInset(par_.getInset(pos));
705
706                 // If we reach the end of a struck out range, paint it.
707                 // We also don't paint across things like tables
708                 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
709                         // Calculate 1/3 height of the buffer's default font
710                         FontMetrics const & fm = theApp->fontLoader().metrics(
711                                 bv_.buffer()->params().getFont());
712                         int const middle = yo_ - fm.maxAscent() / 3;
713                         pain_.line(last_strikeout_x, middle, int(x_), middle,
714                                 LColor::strikeout, Painter::line_solid, Painter::line_thin);
715                         running_strikeout = false;
716                 }
717
718                 if (body_pos > 0 && pos == body_pos - 1) {
719                         string lab = layout->labelsep;
720                         docstring dlab(lab.begin(), lab.end());
721                         int const lwidth =
722                                 theApp->fontLoader().metrics(getLabelFont()).width(dlab);
723
724                         x_ += label_hfill_ + lwidth - width_pos;
725                 }
726
727                 if (par_.isHfill(pos)) {
728                         x_ += 1;
729
730                         int const y0 = yo_;
731                         int const y1 = y0 - defaultRowHeight() / 2;
732
733                         pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
734
735                         if (hfillExpansion(par_, row_, pos)) {
736                                 int const y2 = (y0 + y1) / 2;
737
738                                 if (pos >= body_pos) {
739                                         pain_.line(int(x_), y2, int(x_ + hfill_), y2,
740                                                   LColor::added_space,
741                                                   Painter::line_onoffdash);
742                                         x_ += hfill_;
743                                 } else {
744                                         pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
745                                                   LColor::added_space,
746                                                   Painter::line_onoffdash);
747                                         x_ += label_hfill_;
748                                 }
749                                 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
750                         }
751                         x_ += 2;
752                         ++vpos;
753                 } else if (par_.isSeparator(pos)) {
754                         x_ += width_pos;
755                         if (pos >= body_pos)
756                                 x_ += separator_;
757                         ++vpos;
758                 } else {
759                         paintFromPos(vpos);
760                 }
761         }
762
763         // if we reach the end of a struck out range, paint it
764         if (running_strikeout) {
765                 // calculate 1/3 height of the buffer's default font
766                 FontMetrics const & fm = theApp->fontLoader().metrics(
767                         bv_.buffer()->params().getFont());
768                 int const middle = yo_ - fm.maxAscent() / 3;
769                 pain_.line(last_strikeout_x, middle, int(x_), middle,
770                         LColor::strikeout, Painter::line_solid, Painter::line_thin);
771                 running_strikeout = false;
772         }
773 }
774
775
776 lyx::size_type calculateRowSignature(Row const & row, Paragraph const & par,
777         int x, int y)
778 {
779         boost::crc_32_type crc;
780         for (lyx::pos_type i = row.pos(); i < row.endpos(); ++i) {
781                 const unsigned char b[] = { par.getChar(i) };
782                 crc.process_bytes(b, 1);
783         }
784         const unsigned char b[] = { x, y, row.width() };
785         crc.process_bytes(b, 3);
786         return crc.checksum();
787 }
788
789
790 bool CursorOnRow(PainterInfo & pi, pit_type const pit,
791         RowList::const_iterator rit, LyXText const & text)
792 {
793         // Is there a cursor on this row (or inside inset on row)
794         LCursor & cur = pi.base.bv->cursor();
795         for (lyx::size_type d = 0; d < cur.depth(); d++) {
796                 CursorSlice const & sl = cur[d];
797                 if (sl.text() == &text
798                     && sl.pit() == pit
799                     && sl.pos() >= rit->pos()
800                     && sl.pos() <= rit->endpos())
801                         return true;
802         }
803         return false;
804 }
805
806
807 bool innerCursorOnRow(PainterInfo & pi, pit_type pit,
808         RowList::const_iterator rit, LyXText const & text)
809 {
810         // Is there a cursor inside an inset on this row, and is this inset
811         // the only "character" on this row
812         LCursor & cur = pi.base.bv->cursor();
813         if (rit->pos() + 1 != rit->endpos())
814                 return false;
815         for (lyx::size_type d = 0; d < cur.depth(); d++) {
816                 CursorSlice const & sl = cur[d];
817                 if (sl.text() == &text
818                     && sl.pit() == pit
819                     && sl.pos() == rit->pos())
820                         return d < cur.depth() - 1;
821         }
822         return false;
823 }
824
825
826 void paintPar
827         (PainterInfo & pi, LyXText const & text, pit_type pit, int x, int y,
828          bool repaintAll)
829 {
830 //      lyxerr << "  paintPar: pit: " << pit << " at y: " << y << endl;
831         static NullPainter nop;
832         static PainterInfo nullpi(pi.base.bv, nop);
833         int const ww = pi.base.bv->workHeight();
834
835         theCoords.parPos()[&text][pit] = Point(x, y);
836
837         Paragraph const & par = text.paragraphs()[pit];
838         if (par.rows().empty())
839                 return;
840
841         RowList::const_iterator const rb = par.rows().begin();
842         RowList::const_iterator const re = par.rows().end();
843
844         y -= rb->ascent();
845         lyx::size_type rowno(0);
846         for (RowList::const_iterator rit = rb; rit != re; ++rit, ++rowno) {
847                 y += rit->ascent();
848                 // Allow setting of refreshInside for nested insets in
849                 // this row only
850                 bool tmp = refreshInside;
851
852                 // Row signature; has row changed since last paint?
853                 lyx::size_type const row_sig = calculateRowSignature(*rit, par, x, y);
854                 bool row_has_changed = par.rowSignature()[rowno] != row_sig;
855
856                 bool cursor_on_row = CursorOnRow(pi, pit, rit, text);
857                 bool in_inset_alone_on_row = innerCursorOnRow(pi, pit, rit,
858                         text);
859
860                 // If this is the only object on the row, we can make it wide
861                 for (pos_type i = rit->pos() ; i != rit->endpos(); ++i) {
862                         InsetBase const * const in = par.getInset(i);
863                         if (in) {
864                                 InsetText const * const t = in->asTextInset();
865                                 if (t)
866                                         t->Wide() = in_inset_alone_on_row;
867                         }
868                 }
869
870                 // If selection is on, the current row signature differs
871                 // from cache, or cursor is inside an inset _on this row_,
872                 // then paint the row
873                 if (repaintAll || row_has_changed || cursor_on_row) {
874                         // Add to row signature cache
875                         par.rowSignature()[rowno] = row_sig;
876
877                         bool const inside = (y + rit->descent() >= 0
878                                        && y - rit->ascent() < ww);
879                         RowPainter rp(inside ? pi : nullpi, text, pit, *rit, x, y);
880                         // Clear background of this row
881                         // (if paragraph background was not cleared)
882                         if (!repaintAll &&
883                             (!in_inset_alone_on_row || row_has_changed)) {
884                                 pi.pain.fillRectangle(x, y - rit->ascent(),
885                                     text.maxwidth_, rit->height(),
886                                     text.backgroundColor());
887                                 // If outer row has changed, force nested
888                                 // insets to repaint completely
889                                 if (row_has_changed)
890                                         refreshInside = true;
891                         }
892
893                         // Instrumentation for testing row cache (see also
894                         // 12 lines lower):
895                         if (text.isMainText())
896                                 lyxerr[Debug::PAINTING] << "#";
897                         else
898                                 lyxerr[Debug::PAINTING] << "[" <<
899                                     repaintAll << row_has_changed <<
900                                     cursor_on_row << "]";
901                         rp.paintAppendix();
902                         rp.paintDepthBar();
903                         rp.paintChangeBar();
904                         if (rit == rb)
905                                 rp.paintFirst();
906                         if (rit + 1 == re)
907                                 rp.paintLast();
908                         rp.paintText();
909                 }
910                 y += rit->descent();
911                 // Restore, see above
912                 refreshInside = tmp;
913         }
914         lyxerr[Debug::PAINTING] << "." << endl;
915 }
916
917 } // namespace anon
918
919
920 void paintText(BufferView const & bv, ViewMetricsInfo const & vi,
921                Painter & pain)
922 {
923         LyXText & text = bv.buffer()->text();
924         bool const select = bv.cursor().selection();
925
926         PainterInfo pi(const_cast<BufferView *>(&bv), pain);
927         // Should the whole screen, including insets, be refreshed?
928         bool repaintAll = select || !vi.singlepar;
929
930         if (repaintAll) {
931                 // Clear background (if not delegated to rows)
932                 pain.fillRectangle(0, vi.y1, bv.workWidth(), vi.y2 - vi.y1,
933                         text.backgroundColor());
934         }
935         if (select) {
936                 text.drawSelection(pi, 0, 0);
937         }
938
939         int yy = vi.y1;
940         // draw contents
941         for (pit_type pit = vi.p1; pit <= vi.p2; ++pit) {
942                 refreshInside = repaintAll;
943                 Paragraph const & par = text.getPar(pit);
944                 yy += par.ascent();
945                 paintPar(pi, text, pit, 0, yy, repaintAll);
946                 yy += par.descent();
947         }
948
949         // Cache one paragraph above and one below
950         // Note MV: this cannot be suppressed even for singlepar.
951         // Try viewing the User Guide Mobius figure
952
953         if (vi.p1 > 0) {
954                 text.redoParagraph(vi.p1 - 1);
955                 theCoords.parPos()[&text][vi.p1 - 1] =
956                         Point(0, vi.y1 - text.getPar(vi.p1 - 1).descent());
957         }
958
959         if (vi.p2 < lyx::pit_type(text.paragraphs().size()) - 1) {
960                 text.redoParagraph(vi.p2 + 1);
961                 theCoords.parPos()[&text][vi.p2 + 1] =
962                         Point(0, vi.y2 + text.getPar(vi.p2 + 1).ascent());
963         }
964
965         // and grey out above (should not happen later)
966 //      lyxerr << "par ascent: " << text.getPar(vi.p1).ascent() << endl;
967         if (vi.y1 > 0 && !vi.singlepar)
968                 pain.fillRectangle(0, 0, bv.workWidth(), vi.y1, LColor::bottomarea);
969
970         // and possibly grey out below
971 //      lyxerr << "par descent: " << text.getPar(vi.p1).ascent() << endl;
972         if (vi.y2 < bv.workHeight() && !vi.singlepar)
973                 pain.fillRectangle(0, vi.y2, bv.workWidth(), bv.workHeight() - vi.y2, LColor::bottomarea);
974 }
975
976
977 void paintTextInset(LyXText const & text, PainterInfo & pi, int x, int y)
978 {
979 //      lyxerr << "  paintTextInset: y: " << y << endl;
980
981         y -= text.getPar(0).ascent();
982         // This flag can not be set from within same inset:
983         bool repaintAll = refreshInside;
984         for (int pit = 0; pit < int(text.paragraphs().size()); ++pit) {
985                 y += text.getPar(pit).ascent();
986                 paintPar(pi, text, pit, x, y, repaintAll);
987                 y += text.getPar(pit).descent();
988         }
989 }