]> git.lyx.org Git - lyx.git/blob - src/rowpainter.cpp
gcc compile fix: vector::insert() requires an iterator, not a const_iterator.
[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         // collect as much similar chars as we can
250         for (++vpos ; vpos < end ; ++vpos) {
251                 pos = bidi_.vis2log(vpos);
252                 if (pos < font_span.first || pos > font_span.last)
253                         break;
254
255                 bool const new_selection = pos >= row_.sel_beg && pos < row_.sel_end;
256                 if (new_selection != selection)
257                         // Selection ends or starts here.
258                         break;
259
260                 Change const & change = par_.lookupChange(pos);
261                 if (!change_running.isSimilarTo(change))
262                         // Track change type or author has changed.
263                         break;
264
265                 char_type c = par_.getChar(pos);
266
267                 if (!isPrintableNonspace(c))
268                         break;
269
270                 /* Because we do our own bidi, at this point the strings are
271                  * already in visual order. However, Qt also applies its own
272                  * bidi algorithm to strings that it paints to the screen.
273                  * Therefore, if we were to paint Hebrew/Arabic words as a
274                  * single string, the letters in the words would get reversed
275                  * again. In order to avoid that, we don't collect Hebrew/
276                  * Arabic characters, but rather paint them one at a time.
277                  * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
278                  */
279                 if (hebrew)
280                         break;
281
282                 /* FIXME: these checks are irrelevant, since 'arabic' and
283                  * 'hebrew' alone are already going to trigger a break.
284                  * However, this should not be removed completely, because
285                  * if an alternative solution is found which allows grouping
286                  * of arabic and hebrew characters, then these breaks may have
287                  * to be re-applied.
288
289                 if (arabic && Encodings::isArabicComposeChar(c))
290                         break;
291
292                 if (hebrew && Encodings::isHebrewComposeChar(c))
293                         break;
294                 */
295
296                 if (arabic) {
297                         if (c == '(')
298                                 c = ')';
299                         else if (c == ')')
300                                 c = '(';
301                         c = par_.transformChar(c, pos);
302                         /* see comment in hebrew, explaining why we break */
303                         break;
304                 }
305
306                 str.push_back(c);
307         }
308
309         docstring s(&str[0], str.size());
310
311         if (!selection && !change_running.changed()) {
312                 x_ += pi_.pain.text(int(x_), yo_, s, font);
313                 return;
314         }
315         
316         FontInfo copy = font;
317         if (change_running.changed())
318                 copy.setColor(change_running.color());
319         else if (selection)
320                 copy.setColor(Color_selectiontext);
321
322         x_ += pi_.pain.text(int(x_), yo_, s, copy);
323 }
324
325
326 void RowPainter::paintForeignMark(double orig_x, Language const * lang,
327                 int desc)
328 {
329         if (!lyxrc.mark_foreign_language)
330                 return;
331         if (lang == latex_language)
332                 return;
333         if (lang == pi_.base.bv->buffer().params().language)
334                 return;
335
336         int const y = yo_ + 1 + desc;
337         pi_.pain.line(int(orig_x), y, int(x_), y, Color_language);
338 }
339
340
341 void RowPainter::paintFromPos(pos_type & vpos)
342 {
343         pos_type const pos = bidi_.vis2log(vpos);
344         Font const orig_font = text_metrics_.displayFont(pit_, pos);
345         double const orig_x = x_;
346
347         // usual characters, no insets
348         char_type const c = par_.getChar(pos);
349
350         // special case languages
351         string const & lang = orig_font.language()->lang();
352         bool const hebrew = lang == "hebrew";
353         bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" || 
354                                                 lang == "farsi";
355
356         // draw as many chars as we can
357         if ((!hebrew && !arabic)
358                 || (hebrew && !Encodings::isHebrewComposeChar(c))
359                 || (arabic && !Encodings::isArabicComposeChar(c))) {
360                 paintChars(vpos, orig_font.fontInfo(), hebrew, arabic);
361         } else if (hebrew) {
362                 paintHebrewComposeChar(vpos, orig_font.fontInfo());
363         } else if (arabic) {
364                 paintArabicComposeChar(vpos, orig_font.fontInfo());
365         }
366
367         paintForeignMark(orig_x, orig_font.language());
368 }
369
370
371 void RowPainter::paintChangeBar()
372 {
373         pos_type const start = row_.pos();
374         pos_type end = row_.endpos();
375
376         if (par_.size() == end) {
377                 // this is the last row of the paragraph;
378                 // thus, we must also consider the imaginary end-of-par character
379                 end++;
380         }
381
382         if (start == end || !par_.isChanged(start, end))
383                 return;
384
385         int const height = text_metrics_.isLastRow(pit_, row_)
386                 ? row_.ascent()
387                 : row_.height();
388
389         pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
390 }
391
392
393 void RowPainter::paintAppendix()
394 {
395         // only draw the appendix frame once (for the main text)
396         if (!par_.params().appendix() || !text_.isMainText(pi_.base.bv->buffer()))
397                 return;
398
399         int y = yo_ - row_.ascent();
400
401         if (par_.params().startOfAppendix())
402                 y += 2 * defaultRowHeight();
403
404         pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
405         pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
406 }
407
408
409 void RowPainter::paintDepthBar()
410 {
411         depth_type const depth = par_.getDepth();
412
413         if (depth <= 0)
414                 return;
415
416         depth_type prev_depth = 0;
417         if (!text_metrics_.isFirstRow(pit_, row_)) {
418                 pit_type pit2 = pit_;
419                 if (row_.pos() == 0)
420                         --pit2;
421                 prev_depth = pars_[pit2].getDepth();
422         }
423
424         depth_type next_depth = 0;
425         if (!text_metrics_.isLastRow(pit_, row_)) {
426                 pit_type pit2 = pit_;
427                 if (row_.endpos() >= pars_[pit2].size())
428                         ++pit2;
429                 next_depth = pars_[pit2].getDepth();
430         }
431
432         for (depth_type i = 1; i <= depth; ++i) {
433                 int const w = nestMargin() / 5;
434                 int x = int(xo_) + w * i;
435                 // only consider the changebar space if we're drawing outermost text
436                 if (text_.isMainText(pi_.base.bv->buffer()))
437                         x += changebarMargin();
438
439                 int const starty = yo_ - row_.ascent();
440                 int const h =  row_.height() - 1 - (i - next_depth - 1) * 3;
441
442                 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
443
444                 if (i > prev_depth)
445                         pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
446                 if (i > next_depth)
447                         pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
448         }
449 }
450
451
452 int RowPainter::paintAppendixStart(int y)
453 {
454         FontInfo pb_font = sane_font;
455         pb_font.setColor(Color_appendix);
456         pb_font.decSize();
457
458         int w = 0;
459         int a = 0;
460         int d = 0;
461
462         docstring const label = _("Appendix");
463         theFontMetrics(pb_font).rectText(label, w, a, d);
464
465         int const text_start = int(xo_ + (width_ - w) / 2);
466         int const text_end = text_start + w;
467
468         pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
469
470         pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
471         pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
472
473         return 3 * defaultRowHeight();
474 }
475
476
477 void RowPainter::paintFirst()
478 {
479         ParagraphParameters const & parparams = par_.params();
480
481         int y_top = 0;
482
483         // start of appendix?
484         if (parparams.startOfAppendix())
485                 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
486
487         Buffer const & buffer = pi_.base.bv->buffer();
488         Layout const & layout = par_.layout();
489
490         if (buffer.params().paragraph_separation == BufferParams::ParagraphSkipSeparation) {
491                 if (pit_ != 0) {
492                         if (layout.latextype == LATEX_PARAGRAPH
493                                 && !par_.getDepth()) {
494                                 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
495                         } else {
496                                 Layout const & playout = pars_[pit_ - 1].layout();
497                                 if (playout.latextype == LATEX_PARAGRAPH
498                                         && !pars_[pit_ - 1].getDepth()) {
499                                         // is it right to use defskip here, too? (AS)
500                                         y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
501                                 }
502                         }
503                 }
504         }
505
506         bool const is_rtl = text_.isRTL(buffer, par_);
507         bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
508         //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
509
510         // should we print a label?
511         if (layout.labeltype >= LABEL_STATIC
512             && (layout.labeltype != LABEL_STATIC
513                       || layout.latextype != LATEX_ENVIRONMENT
514                       || is_seq)) {
515
516                 FontInfo const font = labelFont();
517                 FontMetrics const & fm = theFontMetrics(font);
518
519                 docstring const str = par_.labelString();
520                 if (!str.empty()) {
521                         double x = x_;
522
523                         // this is special code for the chapter layout. This is
524                         // printed in an extra row and has a pagebreak at
525                         // the top.
526                         if (layout.counter == "chapter") {
527                                 double spacing_val = 1.0;
528                                 if (!parparams.spacing().isDefault()) {
529                                         spacing_val = parparams.spacing().getValue();
530                                 } else {
531                                         spacing_val = buffer.params().spacing().getValue();
532                                 }
533
534                                 int const labeladdon = int(fm.maxHeight() * layout.spacing.getValue() * spacing_val);
535
536                                 int const maxdesc = int(fm.maxDescent() * layout.spacing.getValue() * spacing_val)
537                                         + int(layout.parsep) * defaultRowHeight();
538
539                                 if (is_rtl) {
540                                         x = width_ - leftMargin() -
541                                                 fm.width(str);
542                                 }
543
544                                 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
545                         } else {
546                                 if (is_rtl) {
547                                         x = width_ - leftMargin()
548                                                 + fm.width(layout.labelsep);
549                                 } else {
550                                         x = x_ - fm.width(layout.labelsep)
551                                                 - fm.width(str);
552                                 }
553
554                                 pi_.pain.text(int(x), yo_, str, font);
555                         }
556                 }
557
558         // the labels at the top of an environment.
559         // More or less for bibliography
560         } else if (is_seq &&
561                 (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
562                 layout.labeltype == LABEL_BIBLIO ||
563                 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
564                 FontInfo const font = labelFont();
565                 docstring const str = par_.labelString();
566                 if (!str.empty()) {
567                         double spacing_val = 1.0;
568                         if (!parparams.spacing().isDefault())
569                                 spacing_val = parparams.spacing().getValue();
570                         else
571                                 spacing_val = buffer.params().spacing().getValue();
572
573                         FontMetrics const & fm = theFontMetrics(font);
574
575                         int const labeladdon = int(fm.maxHeight()
576                                 * layout.spacing.getValue() * spacing_val);
577
578                         int maxdesc =
579                                 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
580                                 + (layout.labelbottomsep * defaultRowHeight()));
581
582                         double x = x_;
583                         if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
584                                 if (is_rtl)
585                                         x = leftMargin();
586                                 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
587                                 x -= fm.width(str) / 2;
588                         } else if (is_rtl) {
589                                 x = width_ - leftMargin() -     fm.width(str);
590                         }
591                         pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
592                 }
593         }
594 }
595
596
597 void RowPainter::paintLast()
598 {
599         bool const is_rtl = text_.isRTL(pi_.base.bv->buffer(), par_);
600         int const endlabel = getEndLabel(pit_, text_.paragraphs());
601
602         // paint imaginary end-of-paragraph character
603
604         Change const & change = par_.lookupChange(par_.size());
605         if (change.changed()) {
606                 FontMetrics const & fm =
607                         theFontMetrics(pi_.base.bv->buffer().params().getFont());
608                 int const length = fm.maxAscent() / 2;
609                 ColorCode col = change.color();
610
611                 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
612                            Painter::line_solid, Painter::line_thick);
613
614                 if (change.deleted()) {
615                         pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
616                                 yo_ + 2, col, Painter::line_solid, Painter::line_thick);
617                 } else {
618                         pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
619                                 yo_ + 2, col, Painter::line_solid, Painter::line_thick);
620                 }
621
622         }
623
624         // draw an endlabel
625
626         switch (endlabel) {
627         case END_LABEL_BOX:
628         case END_LABEL_FILLED_BOX: {
629                 FontInfo const font = labelFont();
630                 FontMetrics const & fm = theFontMetrics(font);
631                 int const size = int(0.75 * fm.maxAscent());
632                 int const y = yo_ - size;
633                 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
634                 int x = is_rtl ? nestMargin() + changebarMargin()
635                         : max_row_width - text_metrics_.rightMargin(pm_);
636
637                 // If needed, move the box a bit to avoid overlapping with text.
638                 int const rem = max_row_width - row_.width();
639                 if (rem <= 0)
640                         x += is_rtl ? rem : - rem;
641
642                 if (endlabel == END_LABEL_BOX)
643                         pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
644                 else
645                         pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
646                 break;
647         }
648
649         case END_LABEL_STATIC: {
650                 FontInfo const font = labelFont();
651                 FontMetrics const & fm = theFontMetrics(font);
652                 docstring const & str = par_.layout().endlabelstring();
653                 double const x = is_rtl ?
654                         x_ - fm.width(str)
655                         : - text_metrics_.rightMargin(pm_) - row_.width();
656                 pi_.pain.text(int(x), yo_, str, font);
657                 break;
658         }
659
660         case END_LABEL_NO_LABEL:
661                 break;
662         }
663 }
664
665
666 void RowPainter::paintOnlyInsets()
667 {
668         pos_type const end = row_.endpos();
669         for (pos_type pos = row_.pos(); pos != end; ++pos) {
670                 // If outer row has changed, nested insets are repaint completely.
671                 Inset const * inset = par_.getInset(pos);
672                 if (!inset)
673                         continue;
674                 if (x_ > pi_.base.bv->workWidth())
675                         continue;
676                 x_ = pi_.base.bv->coordCache().getInsets().x(inset);
677                 paintInset(inset, pos);
678         }
679 }
680
681
682 void RowPainter::paintText()
683 {
684         pos_type const end = row_.endpos();
685         // Spaces at logical line breaks in bidi text must be skipped during 
686         // painting. However, they may appear visually in the middle
687         // of a row; they must be skipped, wherever they are...
688         // * logically "abc_[HEBREW_\nHEBREW]"
689         // * visually "abc_[_WERBEH\nWERBEH]"
690         pos_type skipped_sep_vpos = -1;
691         pos_type body_pos = par_.beginOfBody();
692         if (body_pos > 0 &&
693                 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
694                 body_pos = 0;
695         }
696
697         Layout const & layout = par_.layout();
698
699         Change change_running;
700         int change_last_x = 0;
701
702         // check for possible inline completion
703         DocIterator const & inlineCompletionPos = pi_.base.bv->inlineCompletionPos();
704         pos_type inlineCompletionVPos = -1;
705         if (inlineCompletionPos.inTexted()
706             && inlineCompletionPos.text() == &text_
707             && inlineCompletionPos.pit() == pit_
708             && inlineCompletionPos.pos() - 1 >= row_.pos()
709             && inlineCompletionPos.pos() - 1 < row_.endpos()) {
710                 // draw logically behind the previous character
711                 inlineCompletionVPos = bidi_.log2vis(inlineCompletionPos.pos() - 1);
712         }
713
714         // Use font span to speed things up, see below
715         FontSpan font_span;
716         Font font;
717
718         // If the last logical character is a separator, don't paint it, unless
719         // it's in the last row of a paragraph; see skipped_sep_vpos declaration
720         if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
721                 skipped_sep_vpos = bidi_.log2vis(end - 1);
722
723         for (pos_type vpos = row_.pos(); vpos < end; ) {
724                 if (x_ > pi_.base.bv->workWidth())
725                         break;
726
727                 // Skip the separator at the logical end of the row
728                 if (vpos == skipped_sep_vpos) {
729                         ++vpos;
730                         continue;
731                 }
732
733                 pos_type const pos = bidi_.vis2log(vpos);
734
735                 if (pos >= par_.size()) {
736                         ++vpos;
737                         continue;
738                 }
739
740                 // Use font span to speed things up, see above
741                 if (vpos < font_span.first || vpos > font_span.last) {
742                         font_span = par_.fontSpan(vpos);
743                         font = text_metrics_.displayFont(pit_, vpos);
744
745                         // split font span if inline completion is inside
746                         if (font_span.first <= inlineCompletionVPos
747                             && font_span.last > inlineCompletionVPos)
748                                 font_span.last = inlineCompletionVPos;
749                 }
750
751                 const int width_pos = pm_.singleWidth(pos, font);
752
753                 if (x_ + width_pos < 0) {
754                         x_ += width_pos;
755                         ++vpos;
756                         continue;
757                 }
758                 Change const & change = par_.lookupChange(pos);
759                 if (change.changed() && !change_running.changed()) {
760                         change_running = change;
761                         change_last_x = int(x_);
762                 }
763
764                 Inset const * inset = par_.getInset(pos);
765                 bool const highly_editable_inset = inset
766                         && inset->editable() == Inset::HIGHLY_EDITABLE;
767
768                 // If we reach the end of a change or if the author changes, paint it.
769                 // We also don't paint across things like tables
770                 if (change_running.changed() && (highly_editable_inset
771                         || !change.changed() || !change_running.isSimilarTo(change))) {
772                         // Calculate 1/3 height of the buffer's default font
773                         FontMetrics const & fm
774                                 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
775                         int const y_bar = change_running.deleted() ?
776                                 yo_ - fm.maxAscent() / 3 : yo_ + fm.maxAscent() / 6;
777                         pi_.pain.line(change_last_x, y_bar, int(x_), y_bar,
778                                 change_running.color(), Painter::line_solid,
779                                 Painter::line_thin);
780
781                         // Change might continue with a different author or type
782                         if (change.changed() && !highly_editable_inset) {
783                                 change_running = change;
784                                 change_last_x = int(x_);
785                         } else
786                                 change_running.setUnchanged();
787                 }
788
789                 if (body_pos > 0 && pos == body_pos - 1) {
790                         int const lwidth = theFontMetrics(labelFont())
791                                 .width(layout.labelsep);
792
793                         x_ += row_.label_hfill + lwidth - width_pos;
794                 }
795                 
796                 // Is the inline completion in front of character?
797                 if (font.isRightToLeft() && vpos == inlineCompletionVPos)
798                         paintInlineCompletion(font);
799
800                 if (par_.isSeparator(pos)) {
801                         Font const orig_font = text_metrics_.displayFont(pit_, pos);
802                         double const orig_x = x_;
803                         x_ += width_pos;
804                         if (pos >= body_pos)
805                                 x_ += row_.separator;
806                         paintForeignMark(orig_x, orig_font.language());
807                         ++vpos;
808
809                 } else if (inset) {
810                         // If outer row has changed, nested insets are repaint completely.
811                         pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
812                         paintInset(inset, pos);
813                         ++vpos;
814
815                 } else {
816                         // paint as many characters as possible.
817                         paintFromPos(vpos);
818                 }
819
820                 // Is the inline completion after character?
821                 if (!font.isRightToLeft() && vpos - 1 == inlineCompletionVPos)
822                         paintInlineCompletion(font);
823         }
824
825         // if we reach the end of a struck out range, paint it
826         if (change_running.changed()) {
827                 FontMetrics const & fm
828                         = theFontMetrics(pi_.base.bv->buffer().params().getFont());
829                 int const y_bar = change_running.deleted() ?
830                                 yo_ - fm.maxAscent() / 3 : yo_ + fm.maxAscent() / 6;
831                 pi_.pain.line(change_last_x, y_bar, int(x_), y_bar,
832                         change_running.color(), Painter::line_solid, Painter::line_thin);
833                 change_running.setUnchanged();
834         }
835 }
836
837
838 void RowPainter::paintInlineCompletion(Font const & font)
839 {
840         docstring completion = pi_.base.bv->inlineCompletion();
841         FontInfo f = font.fontInfo();
842         bool rtl = font.isRightToLeft();
843         
844         // draw the unique and the non-unique completion part
845         // Note: this is not time-critical as it is
846         // only done once per screen.
847         size_t uniqueTo = pi_.base.bv->inlineCompletionUniqueChars();
848         docstring s1 = completion.substr(0, uniqueTo);
849         docstring s2 = completion.substr(uniqueTo);
850         ColorCode c1 = Color_inlinecompletion;
851         ColorCode c2 = Color_nonunique_inlinecompletion;
852         
853         // right to left?
854         if (rtl) {
855                 swap(s1, s2);
856                 swap(c1, c2);
857         }
858
859         if (s1.size() > 0) {
860                 f.setColor(c1);
861                 pi_.pain.text(int(x_), yo_, s1, f);
862                 x_ += theFontMetrics(font).width(s1);
863         }
864         
865         if (s2.size() > 0) {
866                 f.setColor(c2);
867                 pi_.pain.text(int(x_), yo_, s2, f);
868                 x_ += theFontMetrics(font).width(s2);
869         }
870 }
871
872 } // namespace lyx