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