3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
15 #include "rowpainter.h"
19 #include "CoordCache.h"
21 #include "BufferParams.h"
22 #include "BufferView.h"
29 #include "MetricsInfo.h"
30 #include "Paragraph.h"
31 #include "ParagraphMetrics.h"
32 #include "ParagraphParameters.h"
33 #include "TextMetrics.h"
36 #include "frontends/FontMetrics.h"
37 #include "frontends/Painter.h"
39 #include "insets/InsetText.h"
41 #include "support/debug.h"
42 #include "support/gettext.h"
43 #include "support/textutils.h"
45 #include "support/lassert.h"
46 #include <boost/crc.hpp>
52 using frontend::Painter;
53 using frontend::FontMetrics;
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())
65 bidi_.computeTables(par_, pi_.base.bv->buffer(), row_);
68 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
71 LASSERT(pit >= 0, /**/);
72 LASSERT(pit < int(text.paragraphs().size()), /**/);
76 FontInfo RowPainter::labelFont() const
78 FontInfo f = text_.labelFont(par_);
80 if (row_.begin_margin_sel || pi_.selected)
81 f.setPaintColor(Color_selectiontext);
86 int RowPainter::leftMargin() const
88 return text_metrics_.leftMargin(text_metrics_.width(), pit_,
92 // If you want to debug inset metrics uncomment the following line:
93 //#define DEBUG_METRICS
94 // This draws green lines around each inset.
97 void RowPainter::paintInset(Inset const * inset, pos_type const pos)
99 Font const font = text_metrics_.displayFont(pit_, pos);
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;
106 // FIXME: We should always use font, see documentation of
107 // noFontChange() in Inset.h.
108 pi_.base.font = inset->noFontChange() ?
109 pi_.base.bv->buffer().params().getFont().fontInfo() :
111 pi_.ltr_pos = (bidi_.level(pos) % 2 == 0);
112 pi_.change_ = change_.changed() ? change_ : par_.lookupChange(pos);
114 int const x1 = int(x_);
115 pi_.base.bv->coordCache().insets().add(inset, x1, yo_);
116 // insets are painted completely. Recursive
117 // FIXME: it is wrong to completely paint the background
118 // if we want to do single row painting.
119 inset->drawBackground(pi_, x1, yo_);
120 inset->drawSelection(pi_, x1, yo_);
121 inset->draw(pi_, x1, yo_);
123 Dimension const & dim = pm_.insetDimension(inset);
125 paintForeignMark(x_, font.language(), dim.descent());
129 // Restore full_repaint status.
130 pi_.full_repaint = pi_full_repaint;
133 int const x2 = x1 + dim.wid;
134 int const y1 = yo_ + dim.des;
135 int const y2 = yo_ - dim.asc;
136 pi_.pain.line(x1, y1, x1, y2, Color_green);
137 pi_.pain.line(x1, y1, x2, y1, Color_green);
138 pi_.pain.line(x2, y1, x2, y2, Color_green);
139 pi_.pain.line(x1, y2, x2, y2, Color_green);
144 void RowPainter::paintHebrewComposeChar(pos_type & vpos, FontInfo const & font)
146 pos_type pos = bidi_.vis2log(vpos);
151 char_type c = par_.getChar(pos);
155 int const width = theFontMetrics(font).width(c);
158 for (pos_type i = pos - 1; i >= 0; --i) {
160 if (!Encodings::isHebrewComposeChar(c)) {
161 if (isPrintableNonspace(c)) {
162 int const width2 = pm_.singleWidth(i,
163 text_metrics_.displayFont(pit_, i));
164 dx = (c == 0x05e8 || // resh
165 c == 0x05d3) // dalet
167 : (width2 - width) / 2;
174 pi_.pain.text(int(x_) + dx, yo_, str, font);
178 void RowPainter::paintArabicComposeChar(pos_type & vpos, FontInfo const & font)
180 pos_type pos = bidi_.vis2log(vpos);
184 char_type c = par_.getChar(pos);
185 c = par_.transformChar(c, pos);
189 int const width = theFontMetrics(font).width(c);
192 for (pos_type i = pos - 1; i >= 0; --i) {
194 if (!Encodings::isArabicComposeChar(c)) {
195 if (isPrintableNonspace(c)) {
196 int const width2 = pm_.singleWidth(i,
197 text_metrics_.displayFont(pit_, i));
198 dx = (width2 - width) / 2;
204 pi_.pain.text(int(x_) + dx, yo_, str, font);
208 void RowPainter::paintChars(pos_type & vpos, FontInfo const & font,
209 bool hebrew, bool arabic)
211 // This method takes up 70% of time when typing
212 pos_type pos = bidi_.vis2log(vpos);
214 vector<char_type> str;
216 str.push_back(par_.getChar(pos));
219 char_type c = str[0];
224 str[0] = par_.transformChar(c, pos);
227 pos_type const end = row_.endpos();
228 FontSpan const font_span = par_.fontSpan(pos);
229 // Track-change status.
230 Change const & change_running = par_.lookupChange(pos);
233 bool const selection = (pos >= row_.sel_beg && pos < row_.sel_end)
237 bool const spell_state =
238 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
240 char_type prev_char = 0;
241 // collect as much similar chars as we can
242 for (++vpos ; vpos < end ; ++vpos) {
243 pos = bidi_.vis2log(vpos);
244 if (pos < font_span.first || pos > font_span.last)
247 bool const new_selection = pos >= row_.sel_beg && pos < row_.sel_end;
248 if (new_selection != selection)
249 // Selection ends or starts here.
252 bool const new_spell_state =
253 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
254 if (new_spell_state != spell_state)
255 // Spell checker state changed here.
258 Change const & change = par_.lookupChange(pos);
259 if (!change_running.isSimilarTo(change))
260 // Track change type or author has changed.
263 char_type c = par_.getChar(pos);
268 if (!isPrintableNonspace(c))
271 // Work-around bug #6920
272 // The bug can be reproduced with DejaVu font under Linux.
273 // The issue is that we compute the metrics character by character
274 // in ParagraphMetrics::singleWidth(); but we paint word by word
275 // for performance reason.
276 // Maybe a more general fix would be draw character by character
277 // for some predefined fonts on some platform. In arabic and
278 // Hebrew we already do paint this way.
279 if (prev_char == 'f')
282 /* Because we do our own bidi, at this point the strings are
283 * already in visual order. However, Qt also applies its own
284 * bidi algorithm to strings that it paints to the screen.
285 * Therefore, if we were to paint Hebrew/Arabic words as a
286 * single string, the letters in the words would get reversed
287 * again. In order to avoid that, we don't collect Hebrew/
288 * Arabic characters, but rather paint them one at a time.
289 * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
294 /* FIXME: these checks are irrelevant, since 'arabic' and
295 * 'hebrew' alone are already going to trigger a break.
296 * However, this should not be removed completely, because
297 * if an alternative solution is found which allows grouping
298 * of arabic and hebrew characters, then these breaks may have
301 if (arabic && Encodings::isArabicComposeChar(c))
304 if (hebrew && Encodings::isHebrewComposeChar(c))
313 c = par_.transformChar(c, pos);
314 /* see comment in hebrew, explaining why we break */
322 docstring s(&str[0], str.size());
325 s.replace(0,1,from_ascii(" "));
327 if (!selection && !change_running.changed()) {
328 x_ += pi_.pain.text(int(x_), yo_, s, font);
332 FontInfo copy = font;
333 if (change_running.changed())
334 copy.setPaintColor(change_running.color());
336 copy.setPaintColor(Color_selectiontext);
338 x_ += pi_.pain.text(int(x_), yo_, s, copy);
342 void RowPainter::paintForeignMark(double orig_x, Language const * lang,
345 if (!lyxrc.mark_foreign_language)
347 if (lang == latex_language)
349 if (lang == pi_.base.bv->buffer().params().language)
352 int const y = yo_ + 1 + desc;
353 pi_.pain.line(int(orig_x), y, int(x_), y, Color_language);
357 void RowPainter::paintMisspelledMark(double orig_x, int desc, bool changed)
359 // derive the offset from zoom factor specified by user in percent
360 // if changed the misspelled marker gets placed slightly lower than normal
361 // to avoid drawing at the same vertical offset
362 int const offset = int(1.5 * lyxrc.zoom / 100.0); // [percent]
363 int const y = yo_ + desc + (changed ? offset : 0);
364 pi_.pain.line(int(orig_x), y, int(x_), y, Color_misspelled,
365 Painter::line_onoffdash, 1.0);
369 void RowPainter::paintFromPos(pos_type & vpos, bool changed)
371 pos_type const pos = bidi_.vis2log(vpos);
372 Font const orig_font = text_metrics_.displayFont(pit_, pos);
373 double const orig_x = x_;
375 // usual characters, no insets
376 char_type const c = par_.getChar(pos);
378 // special case languages
379 string const & lang = orig_font.language()->lang();
380 bool const hebrew = lang == "hebrew";
381 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" ||
385 bool const misspelled_ =
386 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
388 // draw as many chars as we can
389 if ((!hebrew && !arabic)
390 || (hebrew && !Encodings::isHebrewComposeChar(c))
391 || (arabic && !Encodings::isArabicComposeChar(c))) {
392 paintChars(vpos, orig_font.fontInfo(), hebrew, arabic);
394 paintHebrewComposeChar(vpos, orig_font.fontInfo());
396 paintArabicComposeChar(vpos, orig_font.fontInfo());
399 paintForeignMark(orig_x, orig_font.language());
401 if (lyxrc.spellcheck_continuously && misspelled_) {
402 paintMisspelledMark(orig_x, 2, changed);
407 void RowPainter::paintChangeBar()
409 pos_type const start = row_.pos();
410 pos_type end = row_.endpos();
412 if (par_.size() == end) {
413 // this is the last row of the paragraph;
414 // thus, we must also consider the imaginary end-of-par character
418 if (start == end || !par_.isChanged(start, end))
421 int const height = text_metrics_.isLastRow(pit_, row_)
425 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
429 void RowPainter::paintAppendix()
431 // only draw the appendix frame once (for the main text)
432 if (!par_.params().appendix() || !text_.isMainText())
435 int y = yo_ - row_.ascent();
437 if (par_.params().startOfAppendix())
438 y += 2 * defaultRowHeight();
440 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
441 pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
445 void RowPainter::paintDepthBar()
447 depth_type const depth = par_.getDepth();
452 depth_type prev_depth = 0;
453 if (!text_metrics_.isFirstRow(pit_, row_)) {
454 pit_type pit2 = pit_;
457 prev_depth = pars_[pit2].getDepth();
460 depth_type next_depth = 0;
461 if (!text_metrics_.isLastRow(pit_, row_)) {
462 pit_type pit2 = pit_;
463 if (row_.endpos() >= pars_[pit2].size())
465 next_depth = pars_[pit2].getDepth();
468 for (depth_type i = 1; i <= depth; ++i) {
469 int const w = nestMargin() / 5;
470 int x = int(xo_) + w * i;
471 // only consider the changebar space if we're drawing outermost text
472 if (text_.isMainText())
473 x += changebarMargin();
475 int const starty = yo_ - row_.ascent();
476 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
478 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
481 pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
483 pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
488 int RowPainter::paintAppendixStart(int y)
490 FontInfo pb_font = sane_font;
491 pb_font.setColor(Color_appendix);
498 docstring const label = _("Appendix");
499 theFontMetrics(pb_font).rectText(label, w, a, d);
501 int const text_start = int(xo_ + (width_ - w) / 2);
502 int const text_end = text_start + w;
504 pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
506 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
507 pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
509 return 3 * defaultRowHeight();
513 void RowPainter::paintFirst()
515 ParagraphParameters const & pparams = par_.params();
516 Buffer const & buffer = pi_.base.bv->buffer();
517 BufferParams const & bparams = buffer.params();
518 Layout const & layout = par_.layout();
522 // start of appendix?
523 if (pparams.startOfAppendix())
524 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
526 if (bparams.paragraph_separation == BufferParams::ParagraphSkipSeparation
528 if (layout.latextype == LATEX_PARAGRAPH
529 && !par_.getDepth()) {
530 y_top += bparams.getDefSkip().inPixels(*pi_.base.bv);
532 Layout const & playout = pars_[pit_ - 1].layout();
533 if (playout.latextype == LATEX_PARAGRAPH
534 && !pars_[pit_ - 1].getDepth()) {
535 // is it right to use defskip here, too? (AS)
536 y_top += bparams.getDefSkip().inPixels(*pi_.base.bv);
541 bool const is_rtl = text_.isRTL(par_);
542 bool const is_seq = text_.isFirstInSequence(pit_);
543 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
545 // should we print a label?
546 if (layout.labeltype >= LABEL_STATIC
547 && (layout.labeltype != LABEL_STATIC
548 || layout.latextype != LATEX_ENVIRONMENT
551 FontInfo const font = labelFont();
552 FontMetrics const & fm = theFontMetrics(font);
554 docstring const str = par_.labelString();
558 // this is special code for the chapter layout. This is
559 // printed in an extra row and has a pagebreak at
561 if (layout.counter == "chapter") {
562 double spacing_val = 1.0;
563 if (!pparams.spacing().isDefault()) {
564 spacing_val = pparams.spacing().getValue();
566 spacing_val = bparams.spacing().getValue();
569 int const labeladdon = int(fm.maxHeight() * layout.spacing.getValue() * spacing_val);
571 int const maxdesc = int(fm.maxDescent() * layout.spacing.getValue() * spacing_val)
572 + int(layout.parsep) * defaultRowHeight();
575 x = width_ - leftMargin() -
579 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
582 x = width_ - leftMargin()
583 + fm.width(layout.labelsep);
585 x = x_ - fm.width(layout.labelsep)
589 pi_.pain.text(int(x), yo_, str, font);
593 // the labels at the top of an environment.
594 // More or less for bibliography
596 (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
597 layout.labeltype == LABEL_BIBLIO ||
598 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
599 FontInfo const font = labelFont();
600 docstring const str = par_.labelString();
602 double spacing_val = 1.0;
603 if (!pparams.spacing().isDefault())
604 spacing_val = pparams.spacing().getValue();
606 spacing_val = bparams.spacing().getValue();
608 FontMetrics const & fm = theFontMetrics(font);
610 int const labeladdon = int(fm.maxHeight()
611 * layout.spacing.getValue() * spacing_val);
614 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
615 + (layout.labelbottomsep * defaultRowHeight()));
618 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
621 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
622 x -= fm.width(str) / 2;
624 x = width_ - leftMargin() - fm.width(str);
626 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
632 /** Check if the current paragraph is the last paragraph in a
634 static int getEndLabel(pit_type p, Text const & text)
636 ParagraphList const & pars = text.paragraphs();
638 depth_type par_depth = pars[p].getDepth();
639 while (pit != pit_type(pars.size())) {
640 Layout const & layout = pars[pit].layout();
641 int const endlabeltype = layout.endlabeltype;
643 if (endlabeltype != END_LABEL_NO_LABEL) {
644 if (p + 1 == pit_type(pars.size()))
647 depth_type const next_depth =
648 pars[p + 1].getDepth();
649 if (par_depth > next_depth ||
650 (par_depth == next_depth && layout != pars[p + 1].layout()))
656 pit = text.outerHook(pit);
657 if (pit != pit_type(pars.size()))
658 par_depth = pars[pit].getDepth();
660 return END_LABEL_NO_LABEL;
664 void RowPainter::paintLast()
666 bool const is_rtl = text_.isRTL(par_);
667 int const endlabel = getEndLabel(pit_, text_);
669 // paint imaginary end-of-paragraph character
671 Change const & change = par_.lookupChange(par_.size());
672 if (change.changed()) {
673 FontMetrics const & fm =
674 theFontMetrics(pi_.base.bv->buffer().params().getFont());
675 int const length = fm.maxAscent() / 2;
676 Color col = change.color();
678 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
679 Painter::line_solid, 3);
681 if (change.deleted()) {
682 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
683 yo_ + 2, col, Painter::line_solid, 3);
685 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
686 yo_ + 2, col, Painter::line_solid, 3);
694 case END_LABEL_FILLED_BOX: {
695 FontInfo const font = labelFont();
696 FontMetrics const & fm = theFontMetrics(font);
697 int const size = int(0.75 * fm.maxAscent());
698 int const y = yo_ - size;
699 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
700 int x = is_rtl ? nestMargin() + changebarMargin()
701 : max_row_width - text_metrics_.rightMargin(pm_);
703 // If needed, move the box a bit to avoid overlapping with text.
704 int const rem = max_row_width - row_.width();
706 x += is_rtl ? rem : - rem;
708 if (endlabel == END_LABEL_BOX)
709 pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
711 pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
715 case END_LABEL_STATIC: {
716 FontInfo const font = labelFont();
717 FontMetrics const & fm = theFontMetrics(font);
718 docstring const & str = par_.layout().endlabelstring();
719 double const x = is_rtl ? x_ - fm.width(str) : x_;
720 pi_.pain.text(int(x), yo_, str, font);
724 case END_LABEL_NO_LABEL:
725 if (lyxrc.paragraph_markers && size_type(pit_ + 1) < pars_.size()) {
726 docstring const s = docstring(1, char_type(0x00B6));
727 FontInfo f = FontInfo();
728 FontMetrics const & fm = theFontMetrics(f);
729 f.setColor(Color_paragraphmarker);
730 pi_.pain.text(int(x_), yo_, s, f);
738 void RowPainter::paintOnlyInsets()
740 CoordCache const & cache = pi_.base.bv->coordCache();
741 pos_type const end = row_.endpos();
742 for (pos_type pos = row_.pos(); pos != end; ++pos) {
743 // If outer row has changed, nested insets are repaint completely.
744 Inset const * inset = par_.getInset(pos);
747 if (x_ > pi_.base.bv->workWidth()
748 || !cache.getInsets().has(inset))
750 x_ = cache.getInsets().x(inset);
752 bool const pi_selected = pi_.selected;
753 Cursor const & cur = pi_.base.bv->cursor();
754 if (cur.selection() && cur.text() == &text_
755 && cur.normalAnchor().text() == &text_)
756 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos;
757 paintInset(inset, pos);
758 pi_.selected = pi_selected;
763 void RowPainter::paintText()
765 pos_type const end = row_.endpos();
766 // Spaces at logical line breaks in bidi text must be skipped during
767 // painting. However, they may appear visually in the middle
768 // of a row; they must be skipped, wherever they are...
769 // * logically "abc_[HEBREW_\nHEBREW]"
770 // * visually "abc_[_WERBEH\nWERBEH]"
771 pos_type skipped_sep_vpos = -1;
772 pos_type body_pos = par_.beginOfBody();
774 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
778 Layout const & layout = par_.layout();
780 Change change_running;
781 int change_last_x = 0;
783 // check for possible inline completion
784 DocIterator const & inlineCompletionPos = pi_.base.bv->inlineCompletionPos();
785 pos_type inlineCompletionVPos = -1;
786 if (inlineCompletionPos.inTexted()
787 && inlineCompletionPos.text() == &text_
788 && inlineCompletionPos.pit() == pit_
789 && inlineCompletionPos.pos() - 1 >= row_.pos()
790 && inlineCompletionPos.pos() - 1 < row_.endpos()) {
791 // draw logically behind the previous character
792 inlineCompletionVPos = bidi_.log2vis(inlineCompletionPos.pos() - 1);
795 // Use font span to speed things up, see below
799 // If the last logical character is a separator, don't paint it, unless
800 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
801 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
802 skipped_sep_vpos = bidi_.log2vis(end - 1);
804 for (pos_type vpos = row_.pos(); vpos < end; ) {
805 if (x_ > pi_.base.bv->workWidth())
808 // Skip the separator at the logical end of the row
809 if (vpos == skipped_sep_vpos) {
814 pos_type const pos = bidi_.vis2log(vpos);
816 if (pos >= par_.size()) {
821 // Use font span to speed things up, see above
822 if (vpos < font_span.first || vpos > font_span.last) {
823 font_span = par_.fontSpan(vpos);
824 font = text_metrics_.displayFont(pit_, vpos);
826 // split font span if inline completion is inside
827 if (font_span.first <= inlineCompletionVPos
828 && font_span.last > inlineCompletionVPos)
829 font_span.last = inlineCompletionVPos;
832 const int width_pos = pm_.singleWidth(pos, font);
834 if (x_ + width_pos < 0) {
839 Change const & change = par_.lookupChange(pos);
840 if (change.changed() && !change_running.changed()) {
841 change_running = change;
842 change_last_x = int(x_);
845 Inset const * inset = par_.getInset(pos);
846 bool const highly_editable_inset = inset
847 && inset->editable();
849 // If we reach the end of a change or if the author changes, paint it.
850 // We also don't paint across things like tables
851 if (change_running.changed() && (highly_editable_inset
852 || !change.changed() || !change_running.isSimilarTo(change))) {
853 // Calculate 1/3 height of the buffer's default font
854 FontMetrics const & fm
855 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
856 int const y_bar = change_running.deleted() ?
857 yo_ - fm.maxAscent() / 3 : yo_ + fm.maxAscent() / 6;
858 pi_.pain.line(change_last_x, y_bar, int(x_), y_bar,
859 change_running.color(), Painter::line_solid, 0.5);
861 // Change might continue with a different author or type
862 if (change.changed() && !highly_editable_inset) {
863 change_running = change;
864 change_last_x = int(x_);
866 change_running.setUnchanged();
869 if (body_pos > 0 && pos == body_pos - 1) {
870 int const lwidth = theFontMetrics(labelFont())
871 .width(layout.labelsep);
873 x_ += row_.label_hfill + lwidth - width_pos;
876 // Is the inline completion in front of character?
877 if (font.isRightToLeft() && vpos == inlineCompletionVPos)
878 paintInlineCompletion(font);
880 if (par_.isSeparator(pos)) {
881 Font const orig_font = text_metrics_.displayFont(pit_, pos);
882 double const orig_x = x_;
885 x_ += row_.separator;
886 paintForeignMark(orig_x, orig_font.language());
890 // If outer row has changed, nested insets are repaint completely.
891 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
893 bool const pi_selected = pi_.selected;
894 Cursor const & cur = pi_.base.bv->cursor();
895 if (cur.selection() && cur.text() == &text_
896 && cur.normalAnchor().text() == &text_)
897 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos;
898 paintInset(inset, pos);
899 pi_.selected = pi_selected;
903 // paint as many characters as possible.
904 paintFromPos(vpos, change_running.changed());
907 // Is the inline completion after character?
908 if (!font.isRightToLeft() && vpos - 1 == inlineCompletionVPos)
909 paintInlineCompletion(font);
912 // if we reach the end of a struck out range, paint it
913 if (change_running.changed()) {
914 FontMetrics const & fm
915 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
916 int const y_bar = change_running.deleted() ?
917 yo_ - fm.maxAscent() / 3 : yo_ + fm.maxAscent() / 6;
918 pi_.pain.line(change_last_x, y_bar, int(x_), y_bar,
919 change_running.color(), Painter::line_solid, 0.5);
920 change_running.setUnchanged();
925 void RowPainter::paintSelection()
927 if (!row_.selection())
929 Cursor const & curs = pi_.base.bv->cursor();
930 DocIterator beg = curs.selectionBegin();
932 beg.pos() = row_.sel_beg;
934 DocIterator end = curs.selectionEnd();
936 end.pos() = row_.sel_end;
938 bool const begin_boundary = beg.pos() >= row_.endpos();
939 bool const end_boundary = row_.sel_end == row_.endpos();
941 DocIterator cur = beg;
942 cur.boundary(begin_boundary);
943 int x1 = text_metrics_.cursorX(beg.top(), begin_boundary);
944 int x2 = text_metrics_.cursorX(end.top(), end_boundary);
945 int const y1 = yo_ - row_.ascent();
946 int const y2 = y1 + row_.height();
948 int const rm = text_.isMainText() ? pi_.base.bv->rightMargin() : 0;
949 int const lm = text_.isMainText() ? pi_.base.bv->leftMargin() : 0;
952 if (row_.begin_margin_sel) {
953 if (text_.isRTL(beg.paragraph())) {
954 pi_.pain.fillRectangle(xo_ + x1, y1, text_metrics_.width() - rm - x1, y2 - y1,
957 pi_.pain.fillRectangle(xo_ + lm, y1, x1 - lm, y2 - y1,
962 if (row_.end_margin_sel) {
963 if (text_.isRTL(beg.paragraph())) {
964 pi_.pain.fillRectangle(xo_ + lm, y1, x2 - lm, y2 - y1,
967 pi_.pain.fillRectangle(xo_ + x2, y1, text_metrics_.width() - rm - x2, y2 - y1,
972 // if we are on a boundary from the beginning, it's probably
973 // a RTL boundary and we jump to the other side directly as this
974 // segement is 0-size and confuses the logic below
978 // go through row and draw from RTL boundary to RTL boundary
980 bool draw_now = false;
982 // simplified cursorForward code below which does not
983 // descend into insets and which does not go into the
984 // next line. Compare the logic with the original cursorForward
986 // if left of boundary -> just jump to right side, but
987 // for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
988 if (cur.boundary()) {
990 } else if (text_metrics_.isRTLBoundary(cur.pit(), cur.pos() + 1)) {
991 // in front of RTL boundary -> Stay on this side of the boundary
992 // because: ab|cDDEEFFghi -> abc|DDEEFFghi
1001 if (cur.pos() == row_.endpos())
1006 // the previous segment was just drawn, now the next starts
1007 x1 = text_metrics_.cursorX(cur.top(), cur.boundary());
1010 if (!(cur < end) || draw_now) {
1011 x2 = text_metrics_.cursorX(cur.top(), cur.boundary());
1012 pi_.pain.fillRectangle(xo_ + min(x1,x2), y1, abs(x2 - x1), y2 - y1,
1015 // reset x1, so it is set again next round (which will be on the
1016 // right side of a boundary or at the selection end)
1023 void RowPainter::paintInlineCompletion(Font const & font)
1025 docstring completion = pi_.base.bv->inlineCompletion();
1026 FontInfo f = font.fontInfo();
1027 bool rtl = font.isRightToLeft();
1029 // draw the unique and the non-unique completion part
1030 // Note: this is not time-critical as it is
1031 // only done once per screen.
1032 size_t uniqueTo = pi_.base.bv->inlineCompletionUniqueChars();
1033 docstring s1 = completion.substr(0, uniqueTo);
1034 docstring s2 = completion.substr(uniqueTo);
1035 ColorCode c1 = Color_inlinecompletion;
1036 ColorCode c2 = Color_nonunique_inlinecompletion;
1044 if (s1.size() > 0) {
1046 pi_.pain.text(int(x_), yo_, s1, f);
1047 x_ += theFontMetrics(font).width(s1);
1050 if (s2.size() > 0) {
1052 pi_.pain.text(int(x_), yo_, s2, f);
1053 x_ += theFontMetrics(font).width(s2);