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