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()),
64 line_thickness_(1.0), line_offset_(2)
66 bidi_.computeTables(par_, pi_.base.bv->buffer(), row_);
68 if (lyxrc.zoom >= 100) {
69 // derive the line thickness from zoom factor
70 // the zoom is given in percent
71 // (increase thickness at 150%, 250% etc.)
72 line_thickness_ = (float)(int((lyxrc.zoom + 50) / 100.0));
73 // adjust line_offset_ too
74 line_offset_ = int(2 * line_thickness_) + 1;
79 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
82 LASSERT(pit >= 0, /**/);
83 LASSERT(pit < int(text.paragraphs().size()), /**/);
87 FontInfo RowPainter::labelFont() const
89 FontInfo f = text_.labelFont(par_);
91 if (row_.begin_margin_sel || pi_.selected)
92 f.setPaintColor(Color_selectiontext);
97 int RowPainter::leftMargin() const
99 return text_metrics_.leftMargin(text_metrics_.width(), pit_,
103 // If you want to debug inset metrics uncomment the following line:
104 //#define DEBUG_METRICS
105 // This draws green lines around each inset.
108 void RowPainter::paintInset(Inset const * inset, pos_type const pos)
110 Font const font = text_metrics_.displayFont(pit_, pos);
112 LASSERT(inset, return);
113 // Backup full_repaint status because some insets (InsetTabular)
114 // requires a full repaint
115 bool pi_full_repaint = pi_.full_repaint;
117 // FIXME: We should always use font, see documentation of
118 // noFontChange() in Inset.h.
119 pi_.base.font = inset->noFontChange() ?
120 pi_.base.bv->buffer().params().getFont().fontInfo() :
122 pi_.ltr_pos = (bidi_.level(pos) % 2 == 0);
123 pi_.change_ = change_.changed() ? change_ : par_.lookupChange(pos);
125 int const x1 = int(x_);
126 pi_.base.bv->coordCache().insets().add(inset, x1, yo_);
127 // insets are painted completely. Recursive
128 // FIXME: it is wrong to completely paint the background
129 // if we want to do single row painting.
130 inset->drawBackground(pi_, x1, yo_);
131 inset->drawSelection(pi_, x1, yo_);
132 inset->draw(pi_, x1, yo_);
134 Dimension const & dim = pm_.insetDimension(inset);
136 paintForeignMark(x_, font.language(), dim.descent());
140 // Restore full_repaint status.
141 pi_.full_repaint = pi_full_repaint;
144 int const x2 = x1 + dim.wid;
145 int const y1 = yo_ + dim.des;
146 int const y2 = yo_ - dim.asc;
147 pi_.pain.line(x1, y1, x1, y2, Color_green);
148 pi_.pain.line(x1, y1, x2, y1, Color_green);
149 pi_.pain.line(x2, y1, x2, y2, Color_green);
150 pi_.pain.line(x1, y2, x2, y2, Color_green);
155 void RowPainter::paintHebrewComposeChar(pos_type & vpos, FontInfo const & font)
157 pos_type pos = bidi_.vis2log(vpos);
162 char_type c = par_.getChar(pos);
166 int const width = theFontMetrics(font).width(c);
169 for (pos_type i = pos - 1; i >= 0; --i) {
171 if (!Encodings::isHebrewComposeChar(c)) {
172 if (isPrintableNonspace(c)) {
173 int const width2 = pm_.singleWidth(i,
174 text_metrics_.displayFont(pit_, i));
175 dx = (c == 0x05e8 || // resh
176 c == 0x05d3) // dalet
178 : (width2 - width) / 2;
185 pi_.pain.text(int(x_) + dx, yo_, str, font);
189 void RowPainter::paintArabicComposeChar(pos_type & vpos, FontInfo const & font)
191 pos_type pos = bidi_.vis2log(vpos);
195 char_type c = par_.getChar(pos);
196 c = par_.transformChar(c, pos);
200 int const width = theFontMetrics(font).width(c);
203 for (pos_type i = pos - 1; i >= 0; --i) {
205 if (!Encodings::isArabicComposeChar(c)) {
206 if (isPrintableNonspace(c)) {
207 int const width2 = pm_.singleWidth(i,
208 text_metrics_.displayFont(pit_, i));
209 dx = (width2 - width) / 2;
215 pi_.pain.text(int(x_) + dx, yo_, str, font);
219 void RowPainter::paintChars(pos_type & vpos, FontInfo const & font,
220 bool hebrew, bool arabic)
222 // This method takes up 70% of time when typing
223 pos_type pos = bidi_.vis2log(vpos);
225 char_type prev_char = par_.getChar(pos);
226 vector<char_type> str;
228 str.push_back(prev_char);
231 char_type c = str[0];
236 str[0] = par_.transformChar(c, pos);
239 pos_type const end = row_.endpos();
240 FontSpan const font_span = par_.fontSpan(pos);
241 // Track-change status.
242 Change const & change_running = par_.lookupChange(pos);
245 bool const selection = (pos >= row_.sel_beg && pos < row_.sel_end)
249 bool const spell_state =
250 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
252 // collect as much similar chars as we can
253 for (++vpos ; vpos < end ; ++vpos) {
254 // Work-around bug #6920
255 // The bug can be reproduced with DejaVu font under Linux.
256 // The issue is that we compute the metrics character by character
257 // in ParagraphMetrics::singleWidth(); but we paint word by word
258 // for performance reason.
259 // Maybe a more general fix would be draw character by character
260 // for some predefined fonts on some platform. In arabic and
261 // Hebrew we already do paint this way.
262 if (prev_char == 'f')
265 pos = bidi_.vis2log(vpos);
266 if (pos < font_span.first || pos > font_span.last)
269 bool const new_selection = pos >= row_.sel_beg && pos < row_.sel_end;
270 if (new_selection != selection)
271 // Selection ends or starts here.
274 bool const new_spell_state =
275 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
276 if (new_spell_state != spell_state)
277 // Spell checker state changed here.
280 Change const & change = par_.lookupChange(pos);
281 if (!change_running.isSimilarTo(change))
282 // Track change type or author has changed.
285 char_type c = par_.getChar(pos);
290 if (!isPrintableNonspace(c))
293 /* Because we do our own bidi, at this point the strings are
294 * already in visual order. However, Qt also applies its own
295 * bidi algorithm to strings that it paints to the screen.
296 * Therefore, if we were to paint Hebrew/Arabic words as a
297 * single string, the letters in the words would get reversed
298 * again. In order to avoid that, we don't collect Hebrew/
299 * Arabic characters, but rather paint them one at a time.
300 * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
305 /* FIXME: these checks are irrelevant, since 'arabic' and
306 * 'hebrew' alone are already going to trigger a break.
307 * However, this should not be removed completely, because
308 * if an alternative solution is found which allows grouping
309 * of arabic and hebrew characters, then these breaks may have
312 if (arabic && Encodings::isArabicComposeChar(c))
315 if (hebrew && Encodings::isHebrewComposeChar(c))
324 c = par_.transformChar(c, pos);
325 /* see comment in hebrew, explaining why we break */
333 docstring s(&str[0], str.size());
336 s.replace(0,1,from_ascii(" "));
338 if (!selection && !change_running.changed()) {
339 x_ += pi_.pain.text(int(x_), yo_, s, font);
343 FontInfo copy = font;
344 if (change_running.changed())
345 copy.setPaintColor(change_running.color());
347 copy.setPaintColor(Color_selectiontext);
349 x_ += pi_.pain.text(int(x_), yo_, s, copy);
353 void RowPainter::paintForeignMark(double orig_x, Language const * lang,
356 if (!lyxrc.mark_foreign_language)
358 if (lang == latex_language)
360 if (lang == pi_.base.bv->buffer().params().language)
363 int const y = yo_ + 1 + desc + int(line_thickness_/2);
364 pi_.pain.line(int(orig_x), y, int(x_), y, Color_language,
365 Painter::line_solid, line_thickness_);
369 void RowPainter::paintMisspelledMark(double orig_x, bool changed)
371 // if changed the misspelled marker gets placed slightly lower than normal
372 // to avoid drawing at the same vertical offset
373 int const y = yo_ + (changed ? int(line_thickness_ + 1.0) : 0) + line_offset_;
374 pi_.pain.line(int(orig_x), y, int(x_), y, Color_error,
375 Painter::line_onoffdash, line_thickness_);
379 void RowPainter::paintFromPos(pos_type & vpos, bool changed)
381 pos_type const pos = bidi_.vis2log(vpos);
382 Font const orig_font = text_metrics_.displayFont(pit_, pos);
383 double const orig_x = x_;
385 // usual characters, no insets
386 char_type const c = par_.getChar(pos);
388 // special case languages
389 string const & lang = orig_font.language()->lang();
390 bool const hebrew = lang == "hebrew";
391 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" ||
395 bool const misspelled_ =
396 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
398 // draw as many chars as we can
399 if ((!hebrew && !arabic)
400 || (hebrew && !Encodings::isHebrewComposeChar(c))
401 || (arabic && !Encodings::isArabicComposeChar(c))) {
402 paintChars(vpos, orig_font.fontInfo(), hebrew, arabic);
404 paintHebrewComposeChar(vpos, orig_font.fontInfo());
406 paintArabicComposeChar(vpos, orig_font.fontInfo());
409 paintForeignMark(orig_x, orig_font.language());
411 if (lyxrc.spellcheck_continuously && misspelled_) {
412 paintMisspelledMark(orig_x, changed);
417 void RowPainter::paintChangeBar()
419 pos_type const start = row_.pos();
420 pos_type end = row_.endpos();
422 if (par_.size() == end) {
423 // this is the last row of the paragraph;
424 // thus, we must also consider the imaginary end-of-par character
428 if (start == end || !par_.isChanged(start, end))
431 int const height = text_metrics_.isLastRow(pit_, row_)
435 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
439 void RowPainter::paintAppendix()
441 // only draw the appendix frame once (for the main text)
442 if (!par_.params().appendix() || !text_.isMainText())
445 int y = yo_ - row_.ascent();
447 if (par_.params().startOfAppendix())
448 y += 2 * defaultRowHeight();
450 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
451 pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
455 void RowPainter::paintDepthBar()
457 depth_type const depth = par_.getDepth();
462 depth_type prev_depth = 0;
463 if (!text_metrics_.isFirstRow(pit_, row_)) {
464 pit_type pit2 = pit_;
467 prev_depth = pars_[pit2].getDepth();
470 depth_type next_depth = 0;
471 if (!text_metrics_.isLastRow(pit_, row_)) {
472 pit_type pit2 = pit_;
473 if (row_.endpos() >= pars_[pit2].size())
475 next_depth = pars_[pit2].getDepth();
478 for (depth_type i = 1; i <= depth; ++i) {
479 int const w = nestMargin() / 5;
480 int x = int(xo_) + w * i;
481 // only consider the changebar space if we're drawing outermost text
482 if (text_.isMainText())
483 x += changebarMargin();
485 int const starty = yo_ - row_.ascent();
486 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
488 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
491 pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
493 pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
498 int RowPainter::paintAppendixStart(int y)
500 FontInfo pb_font = sane_font;
501 pb_font.setColor(Color_appendix);
508 docstring const label = _("Appendix");
509 theFontMetrics(pb_font).rectText(label, w, a, d);
511 int const text_start = int(xo_ + (width_ - w) / 2);
512 int const text_end = text_start + w;
514 pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
516 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
517 pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
519 return 3 * defaultRowHeight();
523 void RowPainter::paintFirst()
525 ParagraphParameters const & pparams = par_.params();
526 Buffer const & buffer = pi_.base.bv->buffer();
527 BufferParams const & bparams = buffer.params();
528 Layout const & layout = par_.layout();
532 // start of appendix?
533 if (pparams.startOfAppendix())
534 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
536 if (bparams.paragraph_separation == BufferParams::ParagraphSkipSeparation
538 if (layout.latextype == LATEX_PARAGRAPH
539 && !par_.getDepth()) {
540 y_top += bparams.getDefSkip().inPixels(*pi_.base.bv);
542 Layout const & playout = pars_[pit_ - 1].layout();
543 if (playout.latextype == LATEX_PARAGRAPH
544 && !pars_[pit_ - 1].getDepth()) {
545 // is it right to use defskip here, too? (AS)
546 y_top += bparams.getDefSkip().inPixels(*pi_.base.bv);
551 bool const is_rtl = text_.isRTL(par_);
552 bool const is_seq = text_.isFirstInSequence(pit_);
553 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
555 // should we print a label?
556 if (layout.labeltype >= LABEL_STATIC
557 && (layout.labeltype != LABEL_STATIC
558 || layout.latextype != LATEX_ENVIRONMENT
561 FontInfo const font = labelFont();
562 FontMetrics const & fm = theFontMetrics(font);
564 docstring const str = par_.labelString();
568 // this is special code for the chapter layout. This is
569 // printed in an extra row and has a pagebreak at
571 if (layout.counter == "chapter") {
572 double spacing_val = 1.0;
573 if (!pparams.spacing().isDefault()) {
574 spacing_val = pparams.spacing().getValue();
576 spacing_val = bparams.spacing().getValue();
579 int const labeladdon = int(fm.maxHeight() * layout.spacing.getValue() * spacing_val);
581 int const maxdesc = int(fm.maxDescent() * layout.spacing.getValue() * spacing_val)
582 + int(layout.parsep) * defaultRowHeight();
585 x = width_ - leftMargin() -
589 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
592 x = width_ - leftMargin()
593 + fm.width(layout.labelsep);
595 x = x_ - fm.width(layout.labelsep)
599 pi_.pain.text(int(x), yo_, str, font);
603 // the labels at the top of an environment.
604 // More or less for bibliography
606 (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
607 layout.labeltype == LABEL_BIBLIO ||
608 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
609 FontInfo const font = labelFont();
610 docstring const str = par_.labelString();
612 double spacing_val = 1.0;
613 if (!pparams.spacing().isDefault())
614 spacing_val = pparams.spacing().getValue();
616 spacing_val = bparams.spacing().getValue();
618 FontMetrics const & fm = theFontMetrics(font);
620 int const labeladdon = int(fm.maxHeight()
621 * layout.spacing.getValue() * spacing_val);
624 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
625 + (layout.labelbottomsep * defaultRowHeight()));
628 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
631 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
632 x -= fm.width(str) / 2;
634 x = width_ - leftMargin() - fm.width(str);
636 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
642 /** Check if the current paragraph is the last paragraph in a
644 static int getEndLabel(pit_type p, Text const & text)
646 ParagraphList const & pars = text.paragraphs();
648 depth_type par_depth = pars[p].getDepth();
649 while (pit != pit_type(pars.size())) {
650 Layout const & layout = pars[pit].layout();
651 int const endlabeltype = layout.endlabeltype;
653 if (endlabeltype != END_LABEL_NO_LABEL) {
654 if (p + 1 == pit_type(pars.size()))
657 depth_type const next_depth =
658 pars[p + 1].getDepth();
659 if (par_depth > next_depth ||
660 (par_depth == next_depth && layout != pars[p + 1].layout()))
666 pit = text.outerHook(pit);
667 if (pit != pit_type(pars.size()))
668 par_depth = pars[pit].getDepth();
670 return END_LABEL_NO_LABEL;
674 void RowPainter::paintLast()
676 bool const is_rtl = text_.isRTL(par_);
677 int const endlabel = getEndLabel(pit_, text_);
679 // paint imaginary end-of-paragraph character
681 Change const & change = par_.lookupChange(par_.size());
682 if (change.changed()) {
683 FontMetrics const & fm =
684 theFontMetrics(pi_.base.bv->buffer().params().getFont());
685 int const length = fm.maxAscent() / 2;
686 Color col = change.color();
688 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
689 Painter::line_solid, 3);
691 if (change.deleted()) {
692 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
693 yo_ + 2, col, Painter::line_solid, 3);
695 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
696 yo_ + 2, col, Painter::line_solid, 3);
704 case END_LABEL_FILLED_BOX: {
705 FontInfo const font = labelFont();
706 FontMetrics const & fm = theFontMetrics(font);
707 int const size = int(0.75 * fm.maxAscent());
708 int const y = yo_ - size;
709 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
710 int x = is_rtl ? nestMargin() + changebarMargin()
711 : max_row_width - text_metrics_.rightMargin(pm_);
713 // If needed, move the box a bit to avoid overlapping with text.
714 int const rem = max_row_width - row_.width();
716 x += is_rtl ? rem : - rem;
718 if (endlabel == END_LABEL_BOX)
719 pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
721 pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
725 case END_LABEL_STATIC: {
726 FontInfo const font = labelFont();
727 FontMetrics const & fm = theFontMetrics(font);
728 docstring const & str = par_.layout().endlabelstring();
729 double const x = is_rtl ? x_ - fm.width(str) : x_;
730 pi_.pain.text(int(x), yo_, str, font);
734 case END_LABEL_NO_LABEL:
735 if (lyxrc.paragraph_markers && size_type(pit_ + 1) < pars_.size()) {
736 docstring const s = docstring(1, char_type(0x00B6));
737 FontInfo f = FontInfo();
738 FontMetrics const & fm = theFontMetrics(f);
739 f.setColor(Color_paragraphmarker);
740 pi_.pain.text(int(x_), yo_, s, f);
748 void RowPainter::paintOnlyInsets()
750 CoordCache const & cache = pi_.base.bv->coordCache();
751 pos_type const end = row_.endpos();
752 for (pos_type pos = row_.pos(); pos != end; ++pos) {
753 // If outer row has changed, nested insets are repaint completely.
754 Inset const * inset = par_.getInset(pos);
755 bool const nested_inset = inset &&
756 (inset->asInsetText() || inset->asInsetTabular());
759 if (x_ > pi_.base.bv->workWidth()
760 || !cache.getInsets().has(inset))
762 x_ = cache.getInsets().x(inset);
764 bool const pi_selected = pi_.selected;
765 Cursor const & cur = pi_.base.bv->cursor();
766 if (cur.selection() && cur.text() == &text_
767 && cur.normalAnchor().text() == &text_)
768 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos;
769 paintInset(inset, pos);
770 pi_.selected = pi_selected;
775 void RowPainter::paintText()
777 pos_type const end = row_.endpos();
778 // Spaces at logical line breaks in bidi text must be skipped during
779 // painting. However, they may appear visually in the middle
780 // of a row; they must be skipped, wherever they are...
781 // * logically "abc_[HEBREW_\nHEBREW]"
782 // * visually "abc_[_WERBEH\nWERBEH]"
783 pos_type skipped_sep_vpos = -1;
784 pos_type body_pos = par_.beginOfBody();
786 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
790 Layout const & layout = par_.layout();
792 Change change_running;
793 int change_last_x = 0;
795 // check for possible inline completion
796 DocIterator const & inlineCompletionPos = pi_.base.bv->inlineCompletionPos();
797 pos_type inlineCompletionVPos = -1;
798 if (inlineCompletionPos.inTexted()
799 && inlineCompletionPos.text() == &text_
800 && inlineCompletionPos.pit() == pit_
801 && inlineCompletionPos.pos() - 1 >= row_.pos()
802 && inlineCompletionPos.pos() - 1 < row_.endpos()) {
803 // draw logically behind the previous character
804 inlineCompletionVPos = bidi_.log2vis(inlineCompletionPos.pos() - 1);
807 // Use font span to speed things up, see below
811 // If the last logical character is a separator, don't paint it, unless
812 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
813 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
814 skipped_sep_vpos = bidi_.log2vis(end - 1);
816 for (pos_type vpos = row_.pos(); vpos < end; ) {
817 if (x_ > pi_.base.bv->workWidth())
820 // Skip the separator at the logical end of the row
821 if (vpos == skipped_sep_vpos) {
826 pos_type const pos = bidi_.vis2log(vpos);
828 if (pos >= par_.size()) {
833 // Use font span to speed things up, see above
834 if (vpos < font_span.first || vpos > font_span.last) {
835 font_span = par_.fontSpan(vpos);
836 font = text_metrics_.displayFont(pit_, vpos);
838 // split font span if inline completion is inside
839 if (font_span.first <= inlineCompletionVPos
840 && font_span.last > inlineCompletionVPos)
841 font_span.last = inlineCompletionVPos;
844 const int width_pos = pm_.singleWidth(pos, font);
846 if (x_ + width_pos < 0) {
851 Change const & change = par_.lookupChange(pos);
852 if (change.changed() && !change_running.changed()) {
853 change_running = change;
854 change_last_x = int(x_);
857 Inset const * inset = par_.getInset(pos);
858 bool const highly_editable_inset = inset
859 && inset->editable();
861 // If we reach the end of a change or if the author changes, paint it.
862 // We also don't paint across things like tables
863 if (change_running.changed() && (highly_editable_inset
864 || !change.changed() || !change_running.isSimilarTo(change))) {
865 // Calculate 1/3 height of the buffer's default font
866 FontMetrics const & fm
867 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
868 int const y_bar = change_running.deleted() ?
869 yo_ - fm.maxAscent() / 3 : yo_ + line_offset_;
870 pi_.pain.line(change_last_x, y_bar, int(x_), y_bar,
871 change_running.color(), Painter::line_solid, line_thickness_);
873 // Change might continue with a different author or type
874 if (change.changed() && !highly_editable_inset) {
875 change_running = change;
876 change_last_x = int(x_);
878 change_running.setUnchanged();
881 if (body_pos > 0 && pos == body_pos - 1) {
882 int const lwidth = theFontMetrics(labelFont())
883 .width(layout.labelsep);
885 x_ += row_.label_hfill + lwidth - width_pos;
888 // Is the inline completion in front of character?
889 if (font.isRightToLeft() && vpos == inlineCompletionVPos)
890 paintInlineCompletion(font);
892 if (par_.isSeparator(pos)) {
893 Font const orig_font = text_metrics_.displayFont(pit_, pos);
894 double const orig_x = x_;
897 x_ += row_.separator;
898 paintForeignMark(orig_x, orig_font.language());
902 // If outer row has changed, nested insets are repaint completely.
903 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
905 bool const pi_selected = pi_.selected;
906 Cursor const & cur = pi_.base.bv->cursor();
907 if (cur.selection() && cur.text() == &text_
908 && cur.normalAnchor().text() == &text_)
909 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos;
910 paintInset(inset, pos);
911 pi_.selected = pi_selected;
915 // paint as many characters as possible.
916 paintFromPos(vpos, change_running.changed());
919 // Is the inline completion after character?
920 if (!font.isRightToLeft() && vpos - 1 == inlineCompletionVPos)
921 paintInlineCompletion(font);
924 // if we reach the end of a struck out range, paint it
925 if (change_running.changed()) {
926 FontMetrics const & fm
927 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
928 int const y_bar = change_running.deleted() ?
929 yo_ - fm.maxAscent() / 3 : yo_ + line_offset_;
930 pi_.pain.line(change_last_x, y_bar, int(x_), y_bar,
931 change_running.color(), Painter::line_solid, line_thickness_);
932 change_running.setUnchanged();
937 void RowPainter::paintSelection()
939 if (!row_.selection())
941 Cursor const & curs = pi_.base.bv->cursor();
942 DocIterator beg = curs.selectionBegin();
944 beg.pos() = row_.sel_beg;
946 DocIterator end = curs.selectionEnd();
948 end.pos() = row_.sel_end;
950 bool const begin_boundary = beg.pos() >= row_.endpos();
951 bool const end_boundary = row_.sel_end == row_.endpos();
953 DocIterator cur = beg;
954 cur.boundary(begin_boundary);
955 int x1 = text_metrics_.cursorX(beg.top(), begin_boundary);
956 int x2 = text_metrics_.cursorX(end.top(), end_boundary);
957 int const y1 = yo_ - row_.ascent();
958 int const y2 = y1 + row_.height();
960 int const rm = text_.isMainText() ? pi_.base.bv->rightMargin() : 0;
961 int const lm = text_.isMainText() ? pi_.base.bv->leftMargin() : 0;
964 if (row_.begin_margin_sel) {
965 if (text_.isRTL(beg.paragraph())) {
966 pi_.pain.fillRectangle(int(xo_ + x1), y1,
967 text_metrics_.width() - rm - x1, y2 - y1, Color_selection);
969 pi_.pain.fillRectangle(int(xo_ + lm), y1, x1 - lm, y2 - y1,
974 if (row_.end_margin_sel) {
975 if (text_.isRTL(beg.paragraph())) {
976 pi_.pain.fillRectangle(int(xo_ + lm), y1, x2 - lm, y2 - y1,
979 pi_.pain.fillRectangle(int(xo_ + x2), y1, text_metrics_.width() - rm - x2,
980 y2 - y1, Color_selection);
984 // if we are on a boundary from the beginning, it's probably
985 // a RTL boundary and we jump to the other side directly as this
986 // segement is 0-size and confuses the logic below
990 // go through row and draw from RTL boundary to RTL boundary
992 bool draw_now = false;
994 // simplified cursorForward code below which does not
995 // descend into insets and which does not go into the
996 // next line. Compare the logic with the original cursorForward
998 // if left of boundary -> just jump to right side, but
999 // for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
1000 if (cur.boundary()) {
1001 cur.boundary(false);
1002 } else if (text_metrics_.isRTLBoundary(cur.pit(), cur.pos() + 1)) {
1003 // in front of RTL boundary -> Stay on this side of the boundary
1004 // because: ab|cDDEEFFghi -> abc|DDEEFFghi
1013 if (cur.pos() == row_.endpos())
1018 // the previous segment was just drawn, now the next starts
1019 x1 = text_metrics_.cursorX(cur.top(), cur.boundary());
1022 if (!(cur < end) || draw_now) {
1023 x2 = text_metrics_.cursorX(cur.top(), cur.boundary());
1024 pi_.pain.fillRectangle(int(xo_ + min(x1, x2)), y1, abs(x2 - x1),
1025 y2 - y1, Color_selection);
1027 // reset x1, so it is set again next round (which will be on the
1028 // right side of a boundary or at the selection end)
1035 void RowPainter::paintInlineCompletion(Font const & font)
1037 docstring completion = pi_.base.bv->inlineCompletion();
1038 FontInfo f = font.fontInfo();
1039 bool rtl = font.isRightToLeft();
1041 // draw the unique and the non-unique completion part
1042 // Note: this is not time-critical as it is
1043 // only done once per screen.
1044 size_t uniqueTo = pi_.base.bv->inlineCompletionUniqueChars();
1045 docstring s1 = completion.substr(0, uniqueTo);
1046 docstring s2 = completion.substr(uniqueTo);
1047 ColorCode c1 = Color_inlinecompletion;
1048 ColorCode c2 = Color_nonunique_inlinecompletion;
1056 if (s1.size() > 0) {
1058 pi_.pain.text(int(x_), yo_, s1, f);
1059 x_ += theFontMetrics(font).width(s1);
1062 if (s2.size() > 0) {
1064 pi_.pain.text(int(x_), yo_, s2, f);
1065 x_ += theFontMetrics(font).width(s2);