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