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 "mathed/InsetMath.h"
43 #include "support/debug.h"
44 #include "support/gettext.h"
45 #include "support/textutils.h"
47 #include "support/lassert.h"
48 #include <boost/crc.hpp>
54 using frontend::Painter;
55 using frontend::FontMetrics;
57 RowPainter::RowPainter(PainterInfo & pi,
58 Text const & text, pit_type pit, Row const & row, Bidi & bidi, int x, int y)
59 : pi_(pi), text_(text),
60 text_metrics_(pi_.base.bv->textMetrics(&text)),
61 pars_(text.paragraphs()),
62 row_(row), pit_(pit), par_(text.paragraphs()[pit]),
63 pm_(text_metrics_.parMetrics(pit)),
64 bidi_(bidi), change_(pi_.change_),
65 xo_(x), yo_(y), width_(text_metrics_.width()),
66 solid_line_thickness_(1.0), solid_line_offset_(1),
67 dotted_line_thickness_(1.0), dotted_line_offset_(2)
69 bidi_.computeTables(par_, pi_.base.bv->buffer(), row_);
71 if (lyxrc.zoom >= 200) {
72 // derive the line thickness from zoom factor
73 // the zoom is given in percent
74 // (increase thickness at 250%, 450% etc.)
75 solid_line_thickness_ = (float)(int((lyxrc.zoom + 50) / 200.0));
76 // adjust line_offset_ too
77 solid_line_offset_ = 1 + int(0.5 * solid_line_thickness_);
79 if (lyxrc.zoom >= 100) {
80 // derive the line thickness from zoom factor
81 // the zoom is given in percent
82 // (increase thickness at 150%, 250% etc.)
83 dotted_line_thickness_ = (float)(int((lyxrc.zoom + 50) / 100.0));
84 // adjust line_offset_ too
85 dotted_line_offset_ = int(0.5 * dotted_line_thickness_) + 1;
90 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
93 LASSERT(pit >= 0, /**/);
94 LASSERT(pit < int(text.paragraphs().size()), /**/);
98 FontInfo RowPainter::labelFont() const
100 FontInfo f = text_.labelFont(par_);
102 if (row_.begin_margin_sel || pi_.selected)
103 f.setPaintColor(Color_selectiontext);
108 int RowPainter::leftMargin() const
110 return text_metrics_.leftMargin(text_metrics_.width(), pit_,
114 // If you want to debug inset metrics uncomment the following line:
115 //#define DEBUG_METRICS
116 // This draws green lines around each inset.
119 void RowPainter::paintInset(Inset const * inset, pos_type const pos)
121 Font const font = text_metrics_.displayFont(pit_, pos);
123 LASSERT(inset, return);
124 // Backup full_repaint status because some insets (InsetTabular)
125 // requires a full repaint
126 bool pi_full_repaint = pi_.full_repaint;
128 // FIXME: We should always use font, see documentation of
129 // noFontChange() in Inset.h.
130 pi_.base.font = inset->noFontChange() ?
131 pi_.base.bv->buffer().params().getFont().fontInfo() :
133 pi_.ltr_pos = (bidi_.level(pos) % 2 == 0);
134 pi_.change_ = change_.changed() ? change_ : par_.lookupChange(pos);
136 int const x1 = int(x_);
137 pi_.base.bv->coordCache().insets().add(inset, x1, yo_);
138 // insets are painted completely. Recursive
139 // FIXME: it is wrong to completely paint the background
140 // if we want to do single row painting.
141 inset->drawBackground(pi_, x1, yo_);
142 inset->drawSelection(pi_, x1, yo_);
143 inset->draw(pi_, x1, yo_);
145 Dimension const & dim = pm_.insetDimension(inset);
147 paintForeignMark(x_, font.language(), dim.descent());
151 // Restore full_repaint status.
152 pi_.full_repaint = pi_full_repaint;
155 int const x2 = x1 + dim.wid;
156 int const y1 = yo_ + dim.des;
157 int const y2 = yo_ - dim.asc;
158 pi_.pain.line(x1, y1, x1, y2, Color_green);
159 pi_.pain.line(x1, y1, x2, y1, Color_green);
160 pi_.pain.line(x2, y1, x2, y2, Color_green);
161 pi_.pain.line(x1, y2, x2, y2, Color_green);
166 void RowPainter::paintHebrewComposeChar(pos_type & vpos, FontInfo const & font)
168 pos_type pos = bidi_.vis2log(vpos);
173 char_type c = par_.getChar(pos);
177 int const width = theFontMetrics(font).width(c);
180 for (pos_type i = pos - 1; i >= 0; --i) {
182 if (!Encodings::isHebrewComposeChar(c)) {
183 if (isPrintableNonspace(c)) {
184 int const width2 = pm_.singleWidth(i,
185 text_metrics_.displayFont(pit_, i));
186 dx = (c == 0x05e8 || // resh
187 c == 0x05d3) // dalet
189 : (width2 - width) / 2;
196 pi_.pain.text(int(x_) + dx, yo_, str, font);
200 void RowPainter::paintArabicComposeChar(pos_type & vpos, FontInfo const & font)
202 pos_type pos = bidi_.vis2log(vpos);
206 char_type c = par_.getChar(pos);
207 c = par_.transformChar(c, pos);
211 int const width = theFontMetrics(font).width(c);
214 for (pos_type i = pos - 1; i >= 0; --i) {
216 if (!Encodings::isArabicComposeChar(c)) {
217 if (isPrintableNonspace(c)) {
218 int const width2 = pm_.singleWidth(i,
219 text_metrics_.displayFont(pit_, i));
220 dx = (width2 - width) / 2;
226 pi_.pain.text(int(x_) + dx, yo_, str, font);
230 void RowPainter::paintChars(pos_type & vpos, FontInfo const & font,
231 bool hebrew, bool arabic)
233 // This method takes up 70% of time when typing
234 pos_type pos = bidi_.vis2log(vpos);
236 char_type prev_char = par_.getChar(pos);
237 vector<char_type> str;
239 str.push_back(prev_char);
242 char_type c = str[0];
247 str[0] = par_.transformChar(c, pos);
250 pos_type const end = row_.endpos();
251 FontSpan const font_span = par_.fontSpan(pos);
252 // Track-change status.
253 Change const & change_running = par_.lookupChange(pos);
256 bool const selection = (pos >= row_.sel_beg && pos < row_.sel_end)
260 bool const spell_state =
261 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
263 // collect as much similar chars as we can
264 for (++vpos ; vpos < end ; ++vpos) {
265 // Work-around bug #6920
266 // The bug can be reproduced with DejaVu font under Linux.
267 // The issue is that we compute the metrics character by character
268 // in ParagraphMetrics::singleWidth(); but we paint word by word
269 // for performance reason.
270 // Maybe a more general fix would be draw character by character
271 // for some predefined fonts on some platform. In arabic and
272 // Hebrew we already do paint this way.
273 if (prev_char == 'f')
276 pos = bidi_.vis2log(vpos);
277 if (pos < font_span.first || pos > font_span.last)
280 bool const new_selection = pos >= row_.sel_beg && pos < row_.sel_end;
281 if (new_selection != selection)
282 // Selection ends or starts here.
285 bool const new_spell_state =
286 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
287 if (new_spell_state != spell_state)
288 // Spell checker state changed here.
291 Change const & change = par_.lookupChange(pos);
292 if (!change_running.isSimilarTo(change))
293 // Track change type or author has changed.
296 char_type c = par_.getChar(pos);
301 if (!isPrintableNonspace(c))
304 /* Because we do our own bidi, at this point the strings are
305 * already in visual order. However, Qt also applies its own
306 * bidi algorithm to strings that it paints to the screen.
307 * Therefore, if we were to paint Hebrew/Arabic words as a
308 * single string, the letters in the words would get reversed
309 * again. In order to avoid that, we don't collect Hebrew/
310 * Arabic characters, but rather paint them one at a time.
311 * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
316 /* FIXME: these checks are irrelevant, since 'arabic' and
317 * 'hebrew' alone are already going to trigger a break.
318 * However, this should not be removed completely, because
319 * if an alternative solution is found which allows grouping
320 * of arabic and hebrew characters, then these breaks may have
323 if (arabic && Encodings::isArabicComposeChar(c))
326 if (hebrew && Encodings::isHebrewComposeChar(c))
335 c = par_.transformChar(c, pos);
336 /* see comment in hebrew, explaining why we break */
344 docstring s(&str[0], str.size());
347 s.replace(0,1,from_ascii(" "));
349 if (!selection && !change_running.changed()) {
350 x_ += pi_.pain.text(int(x_), yo_, s, font);
354 FontInfo copy = font;
355 if (change_running.changed())
356 copy.setPaintColor(change_running.color());
358 copy.setPaintColor(Color_selectiontext);
360 x_ += pi_.pain.text(int(x_), yo_, s, copy);
364 void RowPainter::paintForeignMark(double orig_x, Language const * lang,
367 if (!lyxrc.mark_foreign_language)
369 if (lang == latex_language)
371 if (lang == pi_.base.bv->buffer().params().language)
374 int const y = yo_ + solid_line_offset_ + desc + int(solid_line_thickness_/2);
375 pi_.pain.line(int(orig_x), y, int(x_), y, Color_language,
376 Painter::line_solid, solid_line_thickness_);
380 void RowPainter::paintMisspelledMark(double orig_x, bool changed)
382 // if changed the misspelled marker gets placed slightly lower than normal
383 // to avoid drawing at the same vertical offset
384 int const y = yo_ + solid_line_offset_ + solid_line_thickness_
385 + (changed ? solid_line_thickness_ + 1 : 0)
386 + dotted_line_offset_;
387 pi_.pain.line(int(orig_x), y, int(x_), y, Color_error,
388 Painter::line_onoffdash, dotted_line_thickness_);
392 void RowPainter::paintFromPos(pos_type & vpos, bool changed)
394 pos_type const pos = bidi_.vis2log(vpos);
395 Font const orig_font = text_metrics_.displayFont(pit_, pos);
396 double const orig_x = x_;
398 // usual characters, no insets
399 char_type const c = par_.getChar(pos);
401 // special case languages
402 string const & lang = orig_font.language()->lang();
403 bool const hebrew = lang == "hebrew";
404 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" ||
408 bool const misspelled =
409 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
411 // draw as many chars as we can
412 if ((!hebrew && !arabic)
413 || (hebrew && !Encodings::isHebrewComposeChar(c))
414 || (arabic && !Encodings::isArabicComposeChar(c))) {
415 paintChars(vpos, orig_font.fontInfo(), hebrew, arabic);
417 paintHebrewComposeChar(vpos, orig_font.fontInfo());
419 paintArabicComposeChar(vpos, orig_font.fontInfo());
422 paintForeignMark(orig_x, orig_font.language());
424 if (lyxrc.spellcheck_continuously && misspelled) {
425 // check for cursor position
426 // don't draw misspelled marker for words at cursor position
427 // we don't want to disturb the process of text editing
428 BufferView const * bv = pi_.base.bv;
429 DocIterator const nw = bv->cursor().newWord();
430 bool new_word = false;
431 if (!nw.empty() && par_.id() == nw.paragraph().id()) {
432 pos_type cpos = nw.pos();
433 if (cpos > 0 && cpos == par_.size() && !par_.isWordSeparator(cpos-1))
435 else if (cpos > 0 && par_.isWordSeparator(cpos))
437 new_word = par_.isSameSpellRange(pos, cpos) ;
440 paintMisspelledMark(orig_x, changed);
445 void RowPainter::paintChangeBar()
447 pos_type const start = row_.pos();
448 pos_type end = row_.endpos();
450 if (par_.size() == end) {
451 // this is the last row of the paragraph;
452 // thus, we must also consider the imaginary end-of-par character
456 if (start == end || !par_.isChanged(start, end))
459 int const height = text_metrics_.isLastRow(pit_, row_)
463 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
467 void RowPainter::paintAppendix()
469 // only draw the appendix frame once (for the main text)
470 if (!par_.params().appendix() || !text_.isMainText())
473 int y = yo_ - row_.ascent();
475 if (par_.params().startOfAppendix())
476 y += 2 * defaultRowHeight();
478 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
479 pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
483 void RowPainter::paintDepthBar()
485 depth_type const depth = par_.getDepth();
490 depth_type prev_depth = 0;
491 if (!text_metrics_.isFirstRow(pit_, row_)) {
492 pit_type pit2 = pit_;
495 prev_depth = pars_[pit2].getDepth();
498 depth_type next_depth = 0;
499 if (!text_metrics_.isLastRow(pit_, row_)) {
500 pit_type pit2 = pit_;
501 if (row_.endpos() >= pars_[pit2].size())
503 next_depth = pars_[pit2].getDepth();
506 for (depth_type i = 1; i <= depth; ++i) {
507 int const w = nestMargin() / 5;
508 int x = int(xo_) + w * i;
509 // only consider the changebar space if we're drawing outermost text
510 if (text_.isMainText())
511 x += changebarMargin();
513 int const starty = yo_ - row_.ascent();
514 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
516 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
519 pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
521 pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
526 int RowPainter::paintAppendixStart(int y)
528 FontInfo pb_font = sane_font;
529 pb_font.setColor(Color_appendix);
536 docstring const label = _("Appendix");
537 theFontMetrics(pb_font).rectText(label, w, a, d);
539 int const text_start = int(xo_ + (width_ - w) / 2);
540 int const text_end = text_start + w;
542 pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
544 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
545 pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
547 return 3 * defaultRowHeight();
551 void RowPainter::paintFirst()
553 ParagraphParameters const & pparams = par_.params();
554 Buffer const & buffer = pi_.base.bv->buffer();
555 BufferParams const & bparams = buffer.params();
556 Layout const & layout = par_.layout();
560 // start of appendix?
561 if (pparams.startOfAppendix())
562 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
564 if (bparams.paragraph_separation == BufferParams::ParagraphSkipSeparation
566 if (layout.latextype == LATEX_PARAGRAPH
567 && !par_.getDepth()) {
568 y_top += bparams.getDefSkip().inPixels(*pi_.base.bv);
570 Layout const & playout = pars_[pit_ - 1].layout();
571 if (playout.latextype == LATEX_PARAGRAPH
572 && !pars_[pit_ - 1].getDepth()) {
573 // is it right to use defskip here, too? (AS)
574 y_top += bparams.getDefSkip().inPixels(*pi_.base.bv);
579 bool const is_rtl = text_.isRTL(par_);
580 bool const is_seq = text_.isFirstInSequence(pit_);
581 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
583 // should we print a label?
584 if (layout.labeltype >= LABEL_STATIC
585 && (layout.labeltype != LABEL_STATIC
586 || layout.latextype != LATEX_ENVIRONMENT
589 FontInfo const font = labelFont();
590 FontMetrics const & fm = theFontMetrics(font);
592 docstring const str = par_.labelString();
596 // this is special code for the chapter layout. This is
597 // printed in an extra row and has a pagebreak at
599 if (layout.counter == "chapter") {
600 double spacing_val = 1.0;
601 if (!pparams.spacing().isDefault()) {
602 spacing_val = pparams.spacing().getValue();
604 spacing_val = bparams.spacing().getValue();
607 int const labeladdon = int(fm.maxHeight() * layout.spacing.getValue() * spacing_val);
609 int const maxdesc = int(fm.maxDescent() * layout.spacing.getValue() * spacing_val)
610 + int(layout.parsep) * defaultRowHeight();
613 x = width_ - leftMargin() -
617 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
620 x = width_ - leftMargin()
621 + fm.width(layout.labelsep);
623 x = x_ - fm.width(layout.labelsep)
627 pi_.pain.text(int(x), yo_, str, font);
631 // the labels at the top of an environment.
632 // More or less for bibliography
634 (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
635 layout.labeltype == LABEL_BIBLIO ||
636 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
637 FontInfo const font = labelFont();
638 docstring const str = par_.labelString();
640 double spacing_val = 1.0;
641 if (!pparams.spacing().isDefault())
642 spacing_val = pparams.spacing().getValue();
644 spacing_val = bparams.spacing().getValue();
646 FontMetrics const & fm = theFontMetrics(font);
648 int const labeladdon = int(fm.maxHeight()
649 * layout.spacing.getValue() * spacing_val);
652 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
653 + (layout.labelbottomsep * defaultRowHeight()));
656 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
659 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
660 x -= fm.width(str) / 2;
662 x = width_ - leftMargin() - fm.width(str);
664 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
670 /** Check if the current paragraph is the last paragraph in a
672 static int getEndLabel(pit_type p, Text const & text)
674 ParagraphList const & pars = text.paragraphs();
676 depth_type par_depth = pars[p].getDepth();
677 while (pit != pit_type(pars.size())) {
678 Layout const & layout = pars[pit].layout();
679 int const endlabeltype = layout.endlabeltype;
681 if (endlabeltype != END_LABEL_NO_LABEL) {
682 if (p + 1 == pit_type(pars.size()))
685 depth_type const next_depth =
686 pars[p + 1].getDepth();
687 if (par_depth > next_depth ||
688 (par_depth == next_depth && layout != pars[p + 1].layout()))
694 pit = text.outerHook(pit);
695 if (pit != pit_type(pars.size()))
696 par_depth = pars[pit].getDepth();
698 return END_LABEL_NO_LABEL;
702 void RowPainter::paintLast()
704 bool const is_rtl = text_.isRTL(par_);
705 int const endlabel = getEndLabel(pit_, text_);
707 // paint imaginary end-of-paragraph character
709 Change const & change = par_.lookupChange(par_.size());
710 if (change.changed()) {
711 FontMetrics const & fm =
712 theFontMetrics(pi_.base.bv->buffer().params().getFont());
713 int const length = fm.maxAscent() / 2;
714 Color col = change.color();
716 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
717 Painter::line_solid, 3);
719 if (change.deleted()) {
720 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
721 yo_ + 2, col, Painter::line_solid, 3);
723 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
724 yo_ + 2, col, Painter::line_solid, 3);
732 case END_LABEL_FILLED_BOX: {
733 FontInfo const font = labelFont();
734 FontMetrics const & fm = theFontMetrics(font);
735 int const size = int(0.75 * fm.maxAscent());
736 int const y = yo_ - size;
737 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
738 int x = is_rtl ? nestMargin() + changebarMargin()
739 : max_row_width - text_metrics_.rightMargin(pm_);
741 // If needed, move the box a bit to avoid overlapping with text.
742 int const rem = max_row_width - row_.width();
744 x += is_rtl ? rem : - rem;
746 if (endlabel == END_LABEL_BOX)
747 pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
749 pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
753 case END_LABEL_STATIC: {
754 FontInfo const font = labelFont();
755 FontMetrics const & fm = theFontMetrics(font);
756 docstring const & str = par_.layout().endlabelstring();
757 double const x = is_rtl ? x_ - fm.width(str) : x_;
758 pi_.pain.text(int(x), yo_, str, font);
762 case END_LABEL_NO_LABEL:
763 if (lyxrc.paragraph_markers && size_type(pit_ + 1) < pars_.size()) {
764 docstring const s = docstring(1, char_type(0x00B6));
765 FontInfo f = FontInfo();
766 FontMetrics const & fm = theFontMetrics(f);
767 f.setColor(Color_paragraphmarker);
768 pi_.pain.text(int(x_), yo_, s, f);
776 void RowPainter::paintOnlyInsets()
778 CoordCache const & cache = pi_.base.bv->coordCache();
779 pos_type const end = row_.endpos();
780 for (pos_type pos = row_.pos(); pos != end; ++pos) {
781 // If outer row has changed, nested insets are repaint completely.
782 Inset const * inset = par_.getInset(pos);
783 bool const nested_inset = inset &&
784 ((inset->asInsetMath() &&
785 !inset->asInsetMath()->asMacroTemplate())
786 || inset->asInsetText()
787 || inset->asInsetTabular());
790 if (x_ > pi_.base.bv->workWidth()
791 || !cache.getInsets().has(inset))
793 x_ = cache.getInsets().x(inset);
795 bool const pi_selected = pi_.selected;
796 Cursor const & cur = pi_.base.bv->cursor();
797 if (cur.selection() && cur.text() == &text_
798 && cur.normalAnchor().text() == &text_)
799 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos;
800 paintInset(inset, pos);
801 pi_.selected = pi_selected;
806 void RowPainter::paintText()
808 pos_type const end = row_.endpos();
809 // Spaces at logical line breaks in bidi text must be skipped during
810 // painting. However, they may appear visually in the middle
811 // of a row; they must be skipped, wherever they are...
812 // * logically "abc_[HEBREW_\nHEBREW]"
813 // * visually "abc_[_WERBEH\nWERBEH]"
814 pos_type skipped_sep_vpos = -1;
815 pos_type body_pos = par_.beginOfBody();
817 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
821 Layout const & layout = par_.layout();
823 Change change_running;
824 int change_last_x = 0;
826 // check for possible inline completion
827 DocIterator const & inlineCompletionPos = pi_.base.bv->inlineCompletionPos();
828 pos_type inlineCompletionVPos = -1;
829 if (inlineCompletionPos.inTexted()
830 && inlineCompletionPos.text() == &text_
831 && inlineCompletionPos.pit() == pit_
832 && inlineCompletionPos.pos() - 1 >= row_.pos()
833 && inlineCompletionPos.pos() - 1 < row_.endpos()) {
834 // draw logically behind the previous character
835 inlineCompletionVPos = bidi_.log2vis(inlineCompletionPos.pos() - 1);
838 // Use font span to speed things up, see below
842 // If the last logical character is a separator, don't paint it, unless
843 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
844 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
845 skipped_sep_vpos = bidi_.log2vis(end - 1);
847 for (pos_type vpos = row_.pos(); vpos < end; ) {
848 if (x_ > pi_.base.bv->workWidth())
851 // Skip the separator at the logical end of the row
852 if (vpos == skipped_sep_vpos) {
857 pos_type const pos = bidi_.vis2log(vpos);
859 if (pos >= par_.size()) {
864 // Use font span to speed things up, see above
865 if (vpos < font_span.first || vpos > font_span.last) {
866 font_span = par_.fontSpan(vpos);
867 font = text_metrics_.displayFont(pit_, vpos);
869 // split font span if inline completion is inside
870 if (font_span.first <= inlineCompletionVPos
871 && font_span.last > inlineCompletionVPos)
872 font_span.last = inlineCompletionVPos;
875 const int width_pos = pm_.singleWidth(pos, font);
877 if (x_ + width_pos < 0) {
882 Change const & change = par_.lookupChange(pos);
883 if (change.changed() && !change_running.changed()) {
884 change_running = change;
885 change_last_x = int(x_);
888 Inset const * inset = par_.getInset(pos);
889 bool const highly_editable_inset = inset
890 && inset->editable();
892 // If we reach the end of a change or if the author changes, paint it.
893 // We also don't paint across things like tables
894 if (change_running.changed() && (highly_editable_inset
895 || !change.changed() || !change_running.isSimilarTo(change))) {
896 // Calculate 1/3 height of the buffer's default font
897 FontMetrics const & fm
898 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
899 int const y_bar = change_running.deleted() ?
900 yo_ - fm.maxAscent() / 3 : yo_ + 2 * solid_line_offset_ + solid_line_thickness_;
901 pi_.pain.line(change_last_x, y_bar, int(x_), y_bar,
902 change_running.color(), Painter::line_solid, solid_line_thickness_);
904 // Change might continue with a different author or type
905 if (change.changed() && !highly_editable_inset) {
906 change_running = change;
907 change_last_x = int(x_);
909 change_running.setUnchanged();
912 if (body_pos > 0 && pos == body_pos - 1) {
913 int const lwidth = theFontMetrics(labelFont())
914 .width(layout.labelsep);
916 x_ += row_.label_hfill + lwidth - width_pos;
919 // Is the inline completion in front of character?
920 if (font.isRightToLeft() && vpos == inlineCompletionVPos)
921 paintInlineCompletion(font);
923 if (par_.isSeparator(pos)) {
924 Font const orig_font = text_metrics_.displayFont(pit_, pos);
925 double const orig_x = x_;
928 x_ += row_.separator;
929 paintForeignMark(orig_x, orig_font.language());
933 // If outer row has changed, nested insets are repaint completely.
934 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
936 bool const pi_selected = pi_.selected;
937 Cursor const & cur = pi_.base.bv->cursor();
938 if (cur.selection() && cur.text() == &text_
939 && cur.normalAnchor().text() == &text_)
940 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos;
941 paintInset(inset, pos);
942 pi_.selected = pi_selected;
946 // paint as many characters as possible.
947 paintFromPos(vpos, change_running.changed());
950 // Is the inline completion after character?
951 if (!font.isRightToLeft() && vpos - 1 == inlineCompletionVPos)
952 paintInlineCompletion(font);
955 // if we reach the end of a struck out range, paint it
956 if (change_running.changed()) {
957 FontMetrics const & fm
958 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
959 int const y_bar = change_running.deleted() ?
960 yo_ - fm.maxAscent() / 3 : yo_ + 2 * solid_line_offset_ + solid_line_thickness_;
961 pi_.pain.line(change_last_x, y_bar, int(x_), y_bar,
962 change_running.color(), Painter::line_solid, solid_line_thickness_);
963 change_running.setUnchanged();
968 void RowPainter::paintSelection()
970 if (!row_.selection())
972 Cursor const & curs = pi_.base.bv->cursor();
973 DocIterator beg = curs.selectionBegin();
975 beg.pos() = row_.sel_beg;
977 DocIterator end = curs.selectionEnd();
979 end.pos() = row_.sel_end;
981 bool const begin_boundary = beg.pos() >= row_.endpos();
982 bool const end_boundary = row_.sel_end == row_.endpos();
984 DocIterator cur = beg;
985 cur.boundary(begin_boundary);
986 int x1 = text_metrics_.cursorX(beg.top(), begin_boundary);
987 int x2 = text_metrics_.cursorX(end.top(), end_boundary);
988 int const y1 = yo_ - row_.ascent();
989 int const y2 = y1 + row_.height();
991 int const rm = text_.isMainText() ? pi_.base.bv->rightMargin() : 0;
992 int const lm = text_.isMainText() ? pi_.base.bv->leftMargin() : 0;
995 if (row_.begin_margin_sel) {
996 if (text_.isRTL(beg.paragraph())) {
997 pi_.pain.fillRectangle(int(xo_ + x1), y1,
998 text_metrics_.width() - rm - x1, y2 - y1, Color_selection);
1000 pi_.pain.fillRectangle(int(xo_ + lm), y1, x1 - lm, y2 - y1,
1005 if (row_.end_margin_sel) {
1006 if (text_.isRTL(beg.paragraph())) {
1007 pi_.pain.fillRectangle(int(xo_ + lm), y1, x2 - lm, y2 - y1,
1010 pi_.pain.fillRectangle(int(xo_ + x2), y1, text_metrics_.width() - rm - x2,
1011 y2 - y1, Color_selection);
1015 // if we are on a boundary from the beginning, it's probably
1016 // a RTL boundary and we jump to the other side directly as this
1017 // segement is 0-size and confuses the logic below
1019 cur.boundary(false);
1021 // go through row and draw from RTL boundary to RTL boundary
1023 bool draw_now = false;
1025 // simplified cursorForward code below which does not
1026 // descend into insets and which does not go into the
1027 // next line. Compare the logic with the original cursorForward
1029 // if left of boundary -> just jump to right side, but
1030 // for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
1031 if (cur.boundary()) {
1032 cur.boundary(false);
1033 } else if (text_metrics_.isRTLBoundary(cur.pit(), cur.pos() + 1)) {
1034 // in front of RTL boundary -> Stay on this side of the boundary
1035 // because: ab|cDDEEFFghi -> abc|DDEEFFghi
1044 if (cur.pos() == row_.endpos())
1049 // the previous segment was just drawn, now the next starts
1050 x1 = text_metrics_.cursorX(cur.top(), cur.boundary());
1053 if (!(cur < end) || draw_now) {
1054 x2 = text_metrics_.cursorX(cur.top(), cur.boundary());
1055 pi_.pain.fillRectangle(int(xo_ + min(x1, x2)), y1, abs(x2 - x1),
1056 y2 - y1, Color_selection);
1058 // reset x1, so it is set again next round (which will be on the
1059 // right side of a boundary or at the selection end)
1066 void RowPainter::paintInlineCompletion(Font const & font)
1068 docstring completion = pi_.base.bv->inlineCompletion();
1069 FontInfo f = font.fontInfo();
1070 bool rtl = font.isRightToLeft();
1072 // draw the unique and the non-unique completion part
1073 // Note: this is not time-critical as it is
1074 // only done once per screen.
1075 size_t uniqueTo = pi_.base.bv->inlineCompletionUniqueChars();
1076 docstring s1 = completion.substr(0, uniqueTo);
1077 docstring s2 = completion.substr(uniqueTo);
1078 ColorCode c1 = Color_inlinecompletion;
1079 ColorCode c2 = Color_nonunique_inlinecompletion;
1087 if (s1.size() > 0) {
1089 pi_.pain.text(int(x_), yo_, s1, f);
1090 x_ += theFontMetrics(font).width(s1);
1093 if (s2.size() > 0) {
1095 pi_.pain.text(int(x_), yo_, s2, f);
1096 x_ += theFontMetrics(font).width(s2);