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