]> git.lyx.org Git - lyx.git/blob - src/rowpainter.cpp
Paragraph:
[lyx.git] / src / rowpainter.cpp
1 /**
2  * \file rowpainter.cpp
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 #include <algorithm>
14
15 #include "rowpainter.h"
16
17 #include "Bidi.h"
18 #include "Buffer.h"
19 #include "CoordCache.h"
20 #include "Cursor.h"
21 #include "BufferParams.h"
22 #include "BufferView.h"
23 #include "Changes.h"
24 #include "Encoding.h"
25 #include "support/gettext.h"
26 #include "Language.h"
27 #include "Layout.h"
28 #include "LyXRC.h"
29 #include "Row.h"
30 #include "MetricsInfo.h"
31 #include "Paragraph.h"
32 #include "ParagraphMetrics.h"
33 #include "paragraph_funcs.h"
34 #include "ParagraphParameters.h"
35 #include "TextMetrics.h"
36 #include "VSpace.h"
37
38 #include "frontends/FontMetrics.h"
39 #include "frontends/Painter.h"
40
41 #include "insets/InsetText.h"
42
43 #include "support/debug.h"
44 #include "support/textutils.h"
45
46 #include "support/lassert.h"
47 #include <boost/crc.hpp>
48
49 using namespace std;
50
51 namespace lyx {
52
53 using frontend::Painter;
54 using frontend::FontMetrics;
55
56 RowPainter::RowPainter(PainterInfo & pi,
57         Text const & text, pit_type pit, Row const & row, Bidi & bidi, int x, int y)
58         : pi_(pi), text_(text),
59           text_metrics_(pi_.base.bv->textMetrics(&text)),
60           pars_(text.paragraphs()),
61           row_(row), pit_(pit), par_(text.paragraphs()[pit]),
62           pm_(text_metrics_.parMetrics(pit)),
63           bidi_(bidi), erased_(pi_.erased_),
64           xo_(x), yo_(y), width_(text_metrics_.width())
65 {
66         bidi_.computeTables(par_, pi_.base.bv->buffer(), row_);
67         x_ = row_.x + xo_;
68
69         //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
70         //row_.dump();
71
72         LASSERT(pit >= 0, /**/);
73         LASSERT(pit < int(text.paragraphs().size()), /**/);
74 }
75
76
77 FontInfo RowPainter::labelFont() const
78 {
79         return text_.labelFont(pi_.base.bv->buffer(), par_);
80 }
81
82
83 int RowPainter::leftMargin() const
84 {
85         return text_metrics_.leftMargin(text_metrics_.width(), pit_,
86                 row_.pos());
87 }
88
89 // If you want to debug inset metrics uncomment the following line:
90 //#define DEBUG_METRICS
91 // This draws green lines around each inset.
92
93
94 void RowPainter::paintInset(Inset const * inset, pos_type const pos)
95 {
96         Font const font = text_metrics_.displayFont(pit_, pos);
97
98         LASSERT(inset, return);
99         // Backup full_repaint status because some insets (InsetTabular)
100         // requires a full repaint
101         bool pi_full_repaint = pi_.full_repaint;
102
103         // FIXME: We should always use font, see documentation of
104         // noFontChange() in Inset.h.
105         pi_.base.font = inset->noFontChange() ?
106                 pi_.base.bv->buffer().params().getFont().fontInfo() :
107                 font.fontInfo();
108         pi_.ltr_pos = (bidi_.level(pos) % 2 == 0);
109         pi_.erased_ = erased_ || par_.isDeleted(pos);
110         pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
111         // insets are painted completely. Recursive
112         inset->drawSelection(pi_, int(x_), yo_);
113         inset->draw(pi_, int(x_), yo_);
114
115         Dimension const & dim = pm_.insetDimension(inset);
116
117         paintForeignMark(x_, font.language(), dim.descent());
118
119         x_ += dim.width();
120
121         // Restore full_repaint status.
122         pi_.full_repaint = pi_full_repaint;
123
124 #ifdef DEBUG_METRICS
125         int const x1 = int(x_ - dim.width());
126         Dimension dim2;
127         LASSERT(max_witdh_ > 0, /**/);
128         int right_margin = text_metrics_.rightMargin(pm_);
129         int const w = max_witdh_ - leftMargin() - right_margin;
130         MetricsInfo mi(pi_.base.bv, font.fontInfo(), w);
131         inset->metrics(mi, dim2);
132         if (dim.wid != dim2.wid)
133                 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
134                        << " draw width " << dim.width()
135                        << "> metrics width " << dim2.wid << "." << endl;
136         if (dim->asc != dim2.asc)
137                 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
138                        << " draw ascent " << dim.ascent()
139                        << "> metrics ascent " << dim2.asc << "." << endl;
140         if (dim2.descent() != dim.des)
141                 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
142                        << " draw ascent " << dim.descent()
143                        << "> metrics descent " << dim2.des << "." << endl;
144         LASSERT(dim2.wid == dim.wid, /**/);
145         LASSERT(dim2.asc == dim.asc, /**/);
146         LASSERT(dim2.des == dim.des, /**/);
147         int const x2 = x1 + dim.wid;
148         int const y1 = yo_ + dim.des;
149         int const y2 = yo_ - dim.asc;
150         pi_.pain.line(x1, y1, x1, y2, Color_green);
151         pi_.pain.line(x1, y1, x2, y1, Color_green);
152         pi_.pain.line(x2, y1, x2, y2, Color_green);
153         pi_.pain.line(x1, y2, x2, y2, Color_green);
154 #endif
155 }
156
157
158 void RowPainter::paintHebrewComposeChar(pos_type & vpos, FontInfo const & font)
159 {
160         pos_type pos = bidi_.vis2log(vpos);
161
162         docstring str;
163
164         // first char
165         char_type c = par_.getChar(pos);
166         str += c;
167         ++vpos;
168
169         int const width = theFontMetrics(font).width(c);
170         int dx = 0;
171
172         for (pos_type i = pos - 1; i >= 0; --i) {
173                 c = par_.getChar(i);
174                 if (!Encodings::isHebrewComposeChar(c)) {
175                         if (isPrintableNonspace(c)) {
176                                 int const width2 = pm_.singleWidth(i,
177                                         text_metrics_.displayFont(pit_, i));
178                                 dx = (c == 0x05e8 || // resh
179                                       c == 0x05d3)   // dalet
180                                         ? width2 - width
181                                         : (width2 - width) / 2;
182                         }
183                         break;
184                 }
185         }
186
187         // Draw nikud
188         pi_.pain.text(int(x_) + dx, yo_, str, font);
189 }
190
191
192 void RowPainter::paintArabicComposeChar(pos_type & vpos, FontInfo const & font)
193 {
194         pos_type pos = bidi_.vis2log(vpos);
195         docstring str;
196
197         // first char
198         char_type c = par_.getChar(pos);
199         c = par_.transformChar(c, pos);
200         str += c;
201         ++vpos;
202
203         int const width = theFontMetrics(font).width(c);
204         int dx = 0;
205
206         for (pos_type i = pos - 1; i >= 0; --i) {
207                 c = par_.getChar(i);
208                 if (!Encodings::isArabicComposeChar(c)) {
209                         if (isPrintableNonspace(c)) {
210                                 int const width2 = pm_.singleWidth(i,
211                                                 text_metrics_.displayFont(pit_, i));
212                                 dx = (width2 - width) / 2;
213                         }
214                         break;
215                 }
216         }
217         // Draw nikud
218         pi_.pain.text(int(x_) + dx, yo_, str, font);
219 }
220
221
222 void RowPainter::paintChars(pos_type & vpos, FontInfo const & font,
223                             bool hebrew, bool arabic)
224 {
225         // This method takes up 70% of time when typing
226         pos_type pos = bidi_.vis2log(vpos);
227         // first character
228         vector<char_type> str;
229         str.reserve(100);
230         str.push_back(par_.getChar(pos));
231
232         if (arabic) {
233                 char_type c = str[0];
234                 if (c == '(')
235                         c = ')';
236                 else if (c == ')')
237                         c = '(';
238                 str[0] = par_.transformChar(c, pos);
239         }
240
241         pos_type const end = row_.endpos();
242         FontSpan const font_span = par_.fontSpan(pos);
243         // Track-change status.
244         Change const & change_running = par_.lookupChange(pos);
245
246         // selected text?
247         bool const selection = pos >= row_.sel_beg && pos < row_.sel_end;
248
249         char_type prev_char = ' ';
250         // collect as much similar chars as we can
251         for (++vpos ; vpos < end ; ++vpos) {
252                 pos = bidi_.vis2log(vpos);
253                 if (pos < font_span.first || pos > font_span.last)
254                         break;
255
256                 bool const new_selection = pos >= row_.sel_beg && pos < row_.sel_end;
257                 if (new_selection != selection)
258                         // Selection ends or starts here.
259                         break;
260
261                 Change const & change = par_.lookupChange(pos);
262                 if (!change_running.isSimilarTo(change))
263                         // Track change type or author has changed.
264                         break;
265
266                 char_type c = par_.getChar(pos);
267
268                 if (c == '\t' || prev_char == '\t') {
269                         prev_char = c;
270                         break;
271                 }
272
273                 if (!isPrintableNonspace(c))
274                         break;
275
276                 /* Because we do our own bidi, at this point the strings are
277                  * already in visual order. However, Qt also applies its own
278                  * bidi algorithm to strings that it paints to the screen.
279                  * Therefore, if we were to paint Hebrew/Arabic words as a
280                  * single string, the letters in the words would get reversed
281                  * again. In order to avoid that, we don't collect Hebrew/
282                  * Arabic characters, but rather paint them one at a time.
283                  * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
284                  */
285                 if (hebrew)
286                         break;
287
288                 /* FIXME: these checks are irrelevant, since 'arabic' and
289                  * 'hebrew' alone are already going to trigger a break.
290                  * However, this should not be removed completely, because
291                  * if an alternative solution is found which allows grouping
292                  * of arabic and hebrew characters, then these breaks may have
293                  * to be re-applied.
294
295                 if (arabic && Encodings::isArabicComposeChar(c))
296                         break;
297
298                 if (hebrew && Encodings::isHebrewComposeChar(c))
299                         break;
300                 */
301
302                 if (arabic) {
303                         if (c == '(')
304                                 c = ')';
305                         else if (c == ')')
306                                 c = '(';
307                         c = par_.transformChar(c, pos);
308                         /* see comment in hebrew, explaining why we break */
309                         break;
310                 }
311
312                 str.push_back(c);
313         }
314
315         docstring s(&str[0], str.size());
316
317         if (s[0] == '\t')
318                 s.replace(0,1,from_ascii("    "));
319
320         if (!selection && !change_running.changed()) {
321                 x_ += pi_.pain.text(int(x_), yo_, s, font);
322                 return;
323         }
324         
325         FontInfo copy = font;
326         if (change_running.changed())
327                 copy.setColor(change_running.color());
328         else if (selection)
329                 copy.setColor(Color_selectiontext);
330
331         x_ += pi_.pain.text(int(x_), yo_, s, copy);
332 }
333
334
335 void RowPainter::paintForeignMark(double orig_x, Language const * lang,
336                 int desc)
337 {
338         if (!lyxrc.mark_foreign_language)
339                 return;
340         if (lang == latex_language)
341                 return;
342         if (lang == pi_.base.bv->buffer().params().language)
343                 return;
344
345         int const y = yo_ + 1 + desc;
346         pi_.pain.line(int(orig_x), y, int(x_), y, Color_language);
347 }
348
349
350 void RowPainter::paintFromPos(pos_type & vpos)
351 {
352         pos_type const pos = bidi_.vis2log(vpos);
353         Font const orig_font = text_metrics_.displayFont(pit_, pos);
354         double const orig_x = x_;
355
356         // usual characters, no insets
357         char_type const c = par_.getChar(pos);
358
359         // special case languages
360         string const & lang = orig_font.language()->lang();
361         bool const hebrew = lang == "hebrew";
362         bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" || 
363                                                 lang == "farsi";
364
365         // draw as many chars as we can
366         if ((!hebrew && !arabic)
367                 || (hebrew && !Encodings::isHebrewComposeChar(c))
368                 || (arabic && !Encodings::isArabicComposeChar(c))) {
369                 paintChars(vpos, orig_font.fontInfo(), hebrew, arabic);
370         } else if (hebrew) {
371                 paintHebrewComposeChar(vpos, orig_font.fontInfo());
372         } else if (arabic) {
373                 paintArabicComposeChar(vpos, orig_font.fontInfo());
374         }
375
376         paintForeignMark(orig_x, orig_font.language());
377 }
378
379
380 void RowPainter::paintChangeBar()
381 {
382         pos_type const start = row_.pos();
383         pos_type end = row_.endpos();
384
385         if (par_.size() == end) {
386                 // this is the last row of the paragraph;
387                 // thus, we must also consider the imaginary end-of-par character
388                 end++;
389         }
390
391         if (start == end || !par_.isChanged(start, end))
392                 return;
393
394         int const height = text_metrics_.isLastRow(pit_, row_)
395                 ? row_.ascent()
396                 : row_.height();
397
398         pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
399 }
400
401
402 void RowPainter::paintAppendix()
403 {
404         // only draw the appendix frame once (for the main text)
405         if (!par_.params().appendix() || !text_.isMainText(pi_.base.bv->buffer()))
406                 return;
407
408         int y = yo_ - row_.ascent();
409
410         if (par_.params().startOfAppendix())
411                 y += 2 * defaultRowHeight();
412
413         pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
414         pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
415 }
416
417
418 void RowPainter::paintDepthBar()
419 {
420         depth_type const depth = par_.getDepth();
421
422         if (depth <= 0)
423                 return;
424
425         depth_type prev_depth = 0;
426         if (!text_metrics_.isFirstRow(pit_, row_)) {
427                 pit_type pit2 = pit_;
428                 if (row_.pos() == 0)
429                         --pit2;
430                 prev_depth = pars_[pit2].getDepth();
431         }
432
433         depth_type next_depth = 0;
434         if (!text_metrics_.isLastRow(pit_, row_)) {
435                 pit_type pit2 = pit_;
436                 if (row_.endpos() >= pars_[pit2].size())
437                         ++pit2;
438                 next_depth = pars_[pit2].getDepth();
439         }
440
441         for (depth_type i = 1; i <= depth; ++i) {
442                 int const w = nestMargin() / 5;
443                 int x = int(xo_) + w * i;
444                 // only consider the changebar space if we're drawing outermost text
445                 if (text_.isMainText(pi_.base.bv->buffer()))
446                         x += changebarMargin();
447
448                 int const starty = yo_ - row_.ascent();
449                 int const h =  row_.height() - 1 - (i - next_depth - 1) * 3;
450
451                 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
452
453                 if (i > prev_depth)
454                         pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
455                 if (i > next_depth)
456                         pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
457         }
458 }
459
460
461 int RowPainter::paintAppendixStart(int y)
462 {
463         FontInfo pb_font = sane_font;
464         pb_font.setColor(Color_appendix);
465         pb_font.decSize();
466
467         int w = 0;
468         int a = 0;
469         int d = 0;
470
471         docstring const label = _("Appendix");
472         theFontMetrics(pb_font).rectText(label, w, a, d);
473
474         int const text_start = int(xo_ + (width_ - w) / 2);
475         int const text_end = text_start + w;
476
477         pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
478
479         pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
480         pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
481
482         return 3 * defaultRowHeight();
483 }
484
485
486 void RowPainter::paintFirst()
487 {
488         ParagraphParameters const & parparams = par_.params();
489
490         int y_top = 0;
491
492         // start of appendix?
493         if (parparams.startOfAppendix())
494                 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
495
496         Buffer const & buffer = pi_.base.bv->buffer();
497         Layout const & layout = par_.layout();
498
499         if (buffer.params().paragraph_separation == BufferParams::ParagraphSkipSeparation) {
500                 if (pit_ != 0) {
501                         if (layout.latextype == LATEX_PARAGRAPH
502                                 && !par_.getDepth()) {
503                                 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
504                         } else {
505                                 Layout const & playout = pars_[pit_ - 1].layout();
506                                 if (playout.latextype == LATEX_PARAGRAPH
507                                         && !pars_[pit_ - 1].getDepth()) {
508                                         // is it right to use defskip here, too? (AS)
509                                         y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
510                                 }
511                         }
512                 }
513         }
514
515         bool const is_rtl = text_.isRTL(buffer, par_);
516         bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
517         //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
518
519         // should we print a label?
520         if (layout.labeltype >= LABEL_STATIC
521             && (layout.labeltype != LABEL_STATIC
522                       || layout.latextype != LATEX_ENVIRONMENT
523                       || is_seq)) {
524
525                 FontInfo const font = labelFont();
526                 FontMetrics const & fm = theFontMetrics(font);
527
528                 docstring const str = par_.labelString();
529                 if (!str.empty()) {
530                         double x = x_;
531
532                         // this is special code for the chapter layout. This is
533                         // printed in an extra row and has a pagebreak at
534                         // the top.
535                         if (layout.counter == "chapter") {
536                                 double spacing_val = 1.0;
537                                 if (!parparams.spacing().isDefault()) {
538                                         spacing_val = parparams.spacing().getValue();
539                                 } else {
540                                         spacing_val = buffer.params().spacing().getValue();
541                                 }
542
543                                 int const labeladdon = int(fm.maxHeight() * layout.spacing.getValue() * spacing_val);
544
545                                 int const maxdesc = int(fm.maxDescent() * layout.spacing.getValue() * spacing_val)
546                                         + int(layout.parsep) * defaultRowHeight();
547
548                                 if (is_rtl) {
549                                         x = width_ - leftMargin() -
550                                                 fm.width(str);
551                                 }
552
553                                 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
554                         } else {
555                                 if (is_rtl) {
556                                         x = width_ - leftMargin()
557                                                 + fm.width(layout.labelsep);
558                                 } else {
559                                         x = x_ - fm.width(layout.labelsep)
560                                                 - fm.width(str);
561                                 }
562
563                                 pi_.pain.text(int(x), yo_, str, 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                 FontInfo const font = labelFont();
574                 docstring const str = par_.labelString();
575                 if (!str.empty()) {
576                         double spacing_val = 1.0;
577                         if (!parparams.spacing().isDefault())
578                                 spacing_val = parparams.spacing().getValue();
579                         else
580                                 spacing_val = buffer.params().spacing().getValue();
581
582                         FontMetrics const & fm = theFontMetrics(font);
583
584                         int const labeladdon = int(fm.maxHeight()
585                                 * layout.spacing.getValue() * spacing_val);
586
587                         int maxdesc =
588                                 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
589                                 + (layout.labelbottomsep * defaultRowHeight()));
590
591                         double x = x_;
592                         if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
593                                 if (is_rtl)
594                                         x = leftMargin();
595                                 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
596                                 x -= fm.width(str) / 2;
597                         } else if (is_rtl) {
598                                 x = width_ - leftMargin() -     fm.width(str);
599                         }
600                         pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
601                 }
602         }
603 }
604
605
606 void RowPainter::paintLast()
607 {
608         bool const is_rtl = text_.isRTL(pi_.base.bv->buffer(), par_);
609         int const endlabel = getEndLabel(pit_, text_.paragraphs());
610
611         // paint imaginary end-of-paragraph character
612
613         Change const & change = par_.lookupChange(par_.size());
614         if (change.changed()) {
615                 FontMetrics const & fm =
616                         theFontMetrics(pi_.base.bv->buffer().params().getFont());
617                 int const length = fm.maxAscent() / 2;
618                 ColorCode col = change.color();
619
620                 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
621                            Painter::line_solid, Painter::line_thick);
622
623                 if (change.deleted()) {
624                         pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
625                                 yo_ + 2, col, Painter::line_solid, Painter::line_thick);
626                 } else {
627                         pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
628                                 yo_ + 2, col, Painter::line_solid, Painter::line_thick);
629                 }
630
631         }
632
633         // draw an endlabel
634
635         switch (endlabel) {
636         case END_LABEL_BOX:
637         case END_LABEL_FILLED_BOX: {
638                 FontInfo const font = labelFont();
639                 FontMetrics const & fm = theFontMetrics(font);
640                 int const size = int(0.75 * fm.maxAscent());
641                 int const y = yo_ - size;
642                 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
643                 int x = is_rtl ? nestMargin() + changebarMargin()
644                         : max_row_width - text_metrics_.rightMargin(pm_);
645
646                 // If needed, move the box a bit to avoid overlapping with text.
647                 int const rem = max_row_width - row_.width();
648                 if (rem <= 0)
649                         x += is_rtl ? rem : - rem;
650
651                 if (endlabel == END_LABEL_BOX)
652                         pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
653                 else
654                         pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
655                 break;
656         }
657
658         case END_LABEL_STATIC: {
659                 FontInfo const font = labelFont();
660                 FontMetrics const & fm = theFontMetrics(font);
661                 docstring const & str = par_.layout().endlabelstring();
662                 double const x = is_rtl ?
663                         x_ - fm.width(str)
664                         : - text_metrics_.rightMargin(pm_) - row_.width();
665                 pi_.pain.text(int(x), yo_, str, font);
666                 break;
667         }
668
669         case END_LABEL_NO_LABEL:
670                 break;
671         }
672 }
673
674
675 void RowPainter::paintOnlyInsets()
676 {
677         pos_type const end = row_.endpos();
678         for (pos_type pos = row_.pos(); pos != end; ++pos) {
679                 // If outer row has changed, nested insets are repaint completely.
680                 Inset const * inset = par_.getInset(pos);
681                 if (!inset)
682                         continue;
683                 if (x_ > pi_.base.bv->workWidth())
684                         continue;
685                 x_ = pi_.base.bv->coordCache().getInsets().x(inset);
686                 paintInset(inset, pos);
687         }
688 }
689
690
691 void RowPainter::paintText()
692 {
693         pos_type const end = row_.endpos();
694         // Spaces at logical line breaks in bidi text must be skipped during 
695         // painting. However, they may appear visually in the middle
696         // of a row; they must be skipped, wherever they are...
697         // * logically "abc_[HEBREW_\nHEBREW]"
698         // * visually "abc_[_WERBEH\nWERBEH]"
699         pos_type skipped_sep_vpos = -1;
700         pos_type body_pos = par_.beginOfBody();
701         if (body_pos > 0 &&
702                 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
703                 body_pos = 0;
704         }
705
706         Layout const & layout = par_.layout();
707
708         Change change_running;
709         int change_last_x = 0;
710
711         // check for possible inline completion
712         DocIterator const & inlineCompletionPos = pi_.base.bv->inlineCompletionPos();
713         pos_type inlineCompletionVPos = -1;
714         if (inlineCompletionPos.inTexted()
715             && inlineCompletionPos.text() == &text_
716             && inlineCompletionPos.pit() == pit_
717             && inlineCompletionPos.pos() - 1 >= row_.pos()
718             && inlineCompletionPos.pos() - 1 < row_.endpos()) {
719                 // draw logically behind the previous character
720                 inlineCompletionVPos = bidi_.log2vis(inlineCompletionPos.pos() - 1);
721         }
722
723         // Use font span to speed things up, see below
724         FontSpan font_span;
725         Font font;
726
727         // If the last logical character is a separator, don't paint it, unless
728         // it's in the last row of a paragraph; see skipped_sep_vpos declaration
729         if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
730                 skipped_sep_vpos = bidi_.log2vis(end - 1);
731
732         for (pos_type vpos = row_.pos(); vpos < end; ) {
733                 if (x_ > pi_.base.bv->workWidth())
734                         break;
735
736                 // Skip the separator at the logical end of the row
737                 if (vpos == skipped_sep_vpos) {
738                         ++vpos;
739                         continue;
740                 }
741
742                 pos_type const pos = bidi_.vis2log(vpos);
743
744                 if (pos >= par_.size()) {
745                         ++vpos;
746                         continue;
747                 }
748
749                 // Use font span to speed things up, see above
750                 if (vpos < font_span.first || vpos > font_span.last) {
751                         font_span = par_.fontSpan(vpos);
752                         font = text_metrics_.displayFont(pit_, vpos);
753
754                         // split font span if inline completion is inside
755                         if (font_span.first <= inlineCompletionVPos
756                             && font_span.last > inlineCompletionVPos)
757                                 font_span.last = inlineCompletionVPos;
758                 }
759
760                 const int width_pos = pm_.singleWidth(pos, font);
761
762                 if (x_ + width_pos < 0) {
763                         x_ += width_pos;
764                         ++vpos;
765                         continue;
766                 }
767                 Change const & change = par_.lookupChange(pos);
768                 if (change.changed() && !change_running.changed()) {
769                         change_running = change;
770                         change_last_x = int(x_);
771                 }
772
773                 Inset const * inset = par_.getInset(pos);
774                 bool const highly_editable_inset = inset
775                         && inset->editable() == Inset::HIGHLY_EDITABLE;
776
777                 // If we reach the end of a change or if the author changes, paint it.
778                 // We also don't paint across things like tables
779                 if (change_running.changed() && (highly_editable_inset
780                         || !change.changed() || !change_running.isSimilarTo(change))) {
781                         // Calculate 1/3 height of the buffer's default font
782                         FontMetrics const & fm
783                                 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
784                         int const y_bar = change_running.deleted() ?
785                                 yo_ - fm.maxAscent() / 3 : yo_ + fm.maxAscent() / 6;
786                         pi_.pain.line(change_last_x, y_bar, int(x_), y_bar,
787                                 change_running.color(), Painter::line_solid,
788                                 Painter::line_thin);
789
790                         // Change might continue with a different author or type
791                         if (change.changed() && !highly_editable_inset) {
792                                 change_running = change;
793                                 change_last_x = int(x_);
794                         } else
795                                 change_running.setUnchanged();
796                 }
797
798                 if (body_pos > 0 && pos == body_pos - 1) {
799                         int const lwidth = theFontMetrics(labelFont())
800                                 .width(layout.labelsep);
801
802                         x_ += row_.label_hfill + lwidth - width_pos;
803                 }
804                 
805                 // Is the inline completion in front of character?
806                 if (font.isRightToLeft() && vpos == inlineCompletionVPos)
807                         paintInlineCompletion(font);
808
809                 if (par_.isSeparator(pos)) {
810                         Font const orig_font = text_metrics_.displayFont(pit_, pos);
811                         double const orig_x = x_;
812                         x_ += width_pos;
813                         if (pos >= body_pos)
814                                 x_ += row_.separator;
815                         paintForeignMark(orig_x, orig_font.language());
816                         ++vpos;
817
818                 } else if (inset) {
819                         // If outer row has changed, nested insets are repaint completely.
820                         pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
821                         paintInset(inset, pos);
822                         ++vpos;
823
824                 } else {
825                         // paint as many characters as possible.
826                         paintFromPos(vpos);
827                 }
828
829                 // Is the inline completion after character?
830                 if (!font.isRightToLeft() && vpos - 1 == inlineCompletionVPos)
831                         paintInlineCompletion(font);
832         }
833
834         // if we reach the end of a struck out range, paint it
835         if (change_running.changed()) {
836                 FontMetrics const & fm
837                         = theFontMetrics(pi_.base.bv->buffer().params().getFont());
838                 int const y_bar = change_running.deleted() ?
839                                 yo_ - fm.maxAscent() / 3 : yo_ + fm.maxAscent() / 6;
840                 pi_.pain.line(change_last_x, y_bar, int(x_), y_bar,
841                         change_running.color(), Painter::line_solid, Painter::line_thin);
842                 change_running.setUnchanged();
843         }
844 }
845
846
847 void RowPainter::paintInlineCompletion(Font const & font)
848 {
849         docstring completion = pi_.base.bv->inlineCompletion();
850         FontInfo f = font.fontInfo();
851         bool rtl = font.isRightToLeft();
852         
853         // draw the unique and the non-unique completion part
854         // Note: this is not time-critical as it is
855         // only done once per screen.
856         size_t uniqueTo = pi_.base.bv->inlineCompletionUniqueChars();
857         docstring s1 = completion.substr(0, uniqueTo);
858         docstring s2 = completion.substr(uniqueTo);
859         ColorCode c1 = Color_inlinecompletion;
860         ColorCode c2 = Color_nonunique_inlinecompletion;
861         
862         // right to left?
863         if (rtl) {
864                 swap(s1, s2);
865                 swap(c1, c2);
866         }
867
868         if (s1.size() > 0) {
869                 f.setColor(c1);
870                 pi_.pain.text(int(x_), yo_, s1, f);
871                 x_ += theFontMetrics(font).width(s1);
872         }
873         
874         if (s2.size() > 0) {
875                 f.setColor(c2);
876                 pi_.pain.text(int(x_), yo_, s2, f);
877                 x_ += theFontMetrics(font).width(s2);
878         }
879 }
880
881 } // namespace lyx