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"
18 #include "CoordCache.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
28 #include "MetricsInfo.h"
29 #include "Paragraph.h"
30 #include "ParagraphMetrics.h"
31 #include "ParagraphParameters.h"
32 #include "TextMetrics.h"
35 #include "frontends/FontMetrics.h"
36 #include "frontends/Painter.h"
38 #include "insets/InsetText.h"
40 #include "mathed/InsetMath.h"
42 #include "support/debug.h"
43 #include "support/gettext.h"
44 #include "support/textutils.h"
46 #include "support/lassert.h"
47 #include <boost/crc.hpp>
53 using frontend::Painter;
54 using frontend::FontMetrics;
57 RowPainter::RowPainter(PainterInfo & pi,
58 Text const & text, pit_type pit, Row const & row, 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)), change_(pi_.change_),
64 xo_(x), yo_(y), width_(text_metrics_.width()),
65 solid_line_thickness_(1.0), solid_line_offset_(1),
66 dotted_line_thickness_(1.0), dotted_line_offset_(2)
68 bidi_.computeTables(par_, pi_.base.bv->buffer(), row_);
70 if (lyxrc.zoom >= 200) {
71 // derive the line thickness from zoom factor
72 // the zoom is given in percent
73 // (increase thickness at 250%, 450% etc.)
74 solid_line_thickness_ = (float)(int((lyxrc.zoom + 50) / 200.0));
75 // adjust line_offset_ too
76 solid_line_offset_ = 1 + int(0.5 * solid_line_thickness_);
78 if (lyxrc.zoom >= 100) {
79 // derive the line thickness from zoom factor
80 // the zoom is given in percent
81 // (increase thickness at 150%, 250% etc.)
82 dotted_line_thickness_ = (float)(int((lyxrc.zoom + 50) / 100.0));
83 // adjust line_offset_ too
84 dotted_line_offset_ = int(0.5 * dotted_line_thickness_) + 1;
89 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
93 LBUFERR(pit < int(text.paragraphs().size()));
97 FontInfo RowPainter::labelFont() const
99 FontInfo f = text_.labelFont(par_);
101 if (row_.begin_margin_sel || pi_.selected)
102 f.setPaintColor(Color_selectiontext);
107 int RowPainter::leftMargin() const
109 return text_metrics_.leftMargin(text_metrics_.width(), pit_,
113 // If you want to debug inset metrics uncomment the following line:
114 //#define DEBUG_METRICS
115 // This draws green lines around each inset.
118 void RowPainter::paintInset(Inset const * inset, pos_type const pos)
120 Font const font = text_metrics_.displayFont(pit_, pos);
122 LASSERT(inset, return);
123 // Backup full_repaint status because some insets (InsetTabular)
124 // requires a full repaint
125 bool pi_full_repaint = pi_.full_repaint;
127 pi_.base.font = inset->inheritFont() ? font.fontInfo() :
128 pi_.base.bv->buffer().params().getFont().fontInfo();
129 pi_.ltr_pos = (bidi_.level(pos) % 2 == 0);
130 Change prev_change = change_;
131 pi_.change_ = change_.changed() ? change_ : par_.lookupChange(pos);
133 int const x1 = int(x_);
134 pi_.base.bv->coordCache().insets().add(inset, x1, yo_);
135 // insets are painted completely. Recursive
136 // FIXME: it is wrong to completely paint the background
137 // if we want to do single row painting.
138 inset->drawBackground(pi_, x1, yo_);
139 inset->drawSelection(pi_, x1, yo_);
140 inset->draw(pi_, x1, yo_);
142 Dimension const & dim = pm_.insetDimension(inset);
144 paintForeignMark(x_, font.language(), dim.descent());
148 // Restore full_repaint status.
149 pi_.full_repaint = pi_full_repaint;
150 pi_.change_ = prev_change;
153 int const x2 = x1 + dim.wid;
154 int const y1 = yo_ + dim.des;
155 int const y2 = yo_ - dim.asc;
156 pi_.pain.line(x1, y1, x1, y2, Color_green);
157 pi_.pain.line(x1, y1, x2, y1, Color_green);
158 pi_.pain.line(x2, y1, x2, y2, Color_green);
159 pi_.pain.line(x1, y2, x2, y2, Color_green);
164 void RowPainter::paintHebrewComposeChar(pos_type & vpos, FontInfo const & font)
166 pos_type pos = bidi_.vis2log(vpos);
171 char_type c = par_.getChar(pos);
175 int const width = theFontMetrics(font).width(c);
178 for (pos_type i = pos - 1; i >= 0; --i) {
180 if (!Encodings::isHebrewComposeChar(c)) {
181 if (isPrintableNonspace(c)) {
182 int const width2 = pm_.singleWidth(i,
183 text_metrics_.displayFont(pit_, i));
184 dx = (c == 0x05e8 || // resh
185 c == 0x05d3) // dalet
187 : (width2 - width) / 2;
194 pi_.pain.text(int(x_) + dx, yo_, str, font);
198 void RowPainter::paintArabicComposeChar(pos_type & vpos, FontInfo const & font)
200 pos_type pos = bidi_.vis2log(vpos);
204 char_type c = par_.getChar(pos);
205 c = par_.transformChar(c, pos);
209 int const width = theFontMetrics(font).width(c);
212 for (pos_type i = pos - 1; i >= 0; --i) {
214 if (!Encodings::isArabicComposeChar(c)) {
215 if (isPrintableNonspace(c)) {
216 int const width2 = pm_.singleWidth(i,
217 text_metrics_.displayFont(pit_, i));
218 dx = (width2 - width) / 2;
224 pi_.pain.text(int(x_) + dx, yo_, str, font);
228 void RowPainter::paintChars(pos_type & vpos, FontInfo const & font,
229 bool hebrew, bool arabic)
231 // This method takes up 70% of time when typing
232 pos_type pos = bidi_.vis2log(vpos);
234 char_type prev_char = par_.getChar(pos);
235 vector<char_type> str;
237 str.push_back(prev_char);
239 // FIXME: Why only round brackets and why the difference to
240 // Hebrew? See also Paragraph::getUChar
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 if (lyxrc.force_paint_single_char)
268 pos = bidi_.vis2log(vpos);
269 if (pos < font_span.first || pos > font_span.last)
272 bool const new_selection = pos >= row_.sel_beg && pos < row_.sel_end;
273 if (new_selection != selection)
274 // Selection ends or starts here.
277 bool const new_spell_state =
278 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
279 if (new_spell_state != spell_state)
280 // Spell checker state changed here.
283 Change const & change = par_.lookupChange(pos);
284 if (!change_running.isSimilarTo(change))
285 // Track change type or author has changed.
288 char_type c = par_.getChar(pos);
293 if (!isPrintableNonspace(c))
296 /* Because we do our own bidi, at this point the strings are
297 * already in visual order. However, Qt also applies its own
298 * bidi algorithm to strings that it paints to the screen.
299 * Therefore, if we were to paint Hebrew/Arabic words as a
300 * single string, the letters in the words would get reversed
301 * again. In order to avoid that, we don't collect Hebrew/
302 * Arabic characters, but rather paint them one at a time.
303 * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
308 /* FIXME: these checks are irrelevant, since 'arabic' and
309 * 'hebrew' alone are already going to trigger a break.
310 * However, this should not be removed completely, because
311 * if an alternative solution is found which allows grouping
312 * of arabic and hebrew characters, then these breaks may have
315 if (arabic && Encodings::isArabicComposeChar(c))
318 if (hebrew && Encodings::isHebrewComposeChar(c))
322 // FIXME: Why only round brackets and why the difference to
323 // Hebrew? See also Paragraph::getUChar
329 c = par_.transformChar(c, pos);
330 /* see comment in hebrew, explaining why we break */
338 docstring s(&str[0], str.size());
341 s.replace(0,1,from_ascii(" "));
343 if (!selection && !change_running.changed()) {
344 x_ += pi_.pain.text(int(x_), yo_, s, font);
348 FontInfo copy = font;
349 if (change_running.changed())
350 copy.setPaintColor(change_running.color());
352 copy.setPaintColor(Color_selectiontext);
354 x_ += pi_.pain.text(int(x_), yo_, s, copy);
358 void RowPainter::paintSeparator(double orig_x, double width,
359 FontInfo const & font)
361 pi_.pain.textDecoration(font, int(orig_x), yo_, int(width));
366 void RowPainter::paintForeignMark(double orig_x, Language const * lang,
369 if (!lyxrc.mark_foreign_language)
371 if (lang == latex_language)
373 if (lang == pi_.base.bv->buffer().params().language)
376 int const y = yo_ + solid_line_offset_ + desc + int(solid_line_thickness_/2);
377 pi_.pain.line(int(orig_x), y, int(x_), y, Color_language,
378 Painter::line_solid, solid_line_thickness_);
382 void RowPainter::paintMisspelledMark(double orig_x, bool changed)
384 // if changed the misspelled marker gets placed slightly lower than normal
385 // to avoid drawing at the same vertical offset
386 float const y = yo_ + solid_line_offset_ + solid_line_thickness_
387 + (changed ? solid_line_thickness_ + 1 : 0)
388 + dotted_line_offset_;
389 pi_.pain.line(int(orig_x), int(y), int(x_), int(y), Color_error,
390 Painter::line_onoffdash, dotted_line_thickness_);
394 void RowPainter::paintFromPos(pos_type & vpos, bool changed)
396 pos_type const pos = bidi_.vis2log(vpos);
397 Font const orig_font = text_metrics_.displayFont(pit_, pos);
398 double const orig_x = x_;
400 // usual characters, no insets
401 char_type const c = par_.getChar(pos);
403 // special case languages
404 string const & lang = orig_font.language()->lang();
405 bool const hebrew = lang == "hebrew";
406 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" ||
410 bool const misspelled =
411 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
413 // draw as many chars as we can
414 if ((!hebrew && !arabic)
415 || (hebrew && !Encodings::isHebrewComposeChar(c))
416 || (arabic && !Encodings::isArabicComposeChar(c))) {
417 paintChars(vpos, orig_font.fontInfo(), hebrew, arabic);
419 paintHebrewComposeChar(vpos, orig_font.fontInfo());
421 paintArabicComposeChar(vpos, orig_font.fontInfo());
424 paintForeignMark(orig_x, orig_font.language());
426 if (lyxrc.spellcheck_continuously && misspelled) {
427 // check for cursor position
428 // don't draw misspelled marker for words at cursor position
429 // we don't want to disturb the process of text editing
430 BufferView const * bv = pi_.base.bv;
431 DocIterator const nw = bv->cursor().newWord();
432 bool new_word = false;
433 if (!nw.empty() && par_.id() == nw.paragraph().id()) {
434 pos_type cpos = nw.pos();
435 if (cpos > 0 && cpos == par_.size() && !par_.isWordSeparator(cpos-1))
437 else if (cpos > 0 && par_.isWordSeparator(cpos))
439 new_word = par_.isSameSpellRange(pos, cpos) ;
442 paintMisspelledMark(orig_x, changed);
447 void RowPainter::paintChangeBar()
449 pos_type const start = row_.pos();
450 pos_type end = row_.endpos();
452 if (par_.size() == end) {
453 // this is the last row of the paragraph;
454 // thus, we must also consider the imaginary end-of-par character
458 if (start == end || !par_.isChanged(start, end))
461 int const height = text_metrics_.isLastRow(pit_, row_)
465 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
469 void RowPainter::paintAppendix()
471 // only draw the appendix frame once (for the main text)
472 if (!par_.params().appendix() || !text_.isMainText())
475 int y = yo_ - row_.ascent();
477 if (par_.params().startOfAppendix())
478 y += 2 * defaultRowHeight();
480 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
481 pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
485 void RowPainter::paintDepthBar()
487 depth_type const depth = par_.getDepth();
492 depth_type prev_depth = 0;
493 if (!text_metrics_.isFirstRow(pit_, row_)) {
494 pit_type pit2 = pit_;
497 prev_depth = pars_[pit2].getDepth();
500 depth_type next_depth = 0;
501 if (!text_metrics_.isLastRow(pit_, row_)) {
502 pit_type pit2 = pit_;
503 if (row_.endpos() >= pars_[pit2].size())
505 next_depth = pars_[pit2].getDepth();
508 for (depth_type i = 1; i <= depth; ++i) {
509 int const w = nestMargin() / 5;
510 int x = int(xo_) + w * i;
511 // only consider the changebar space if we're drawing outermost text
512 if (text_.isMainText())
513 x += changebarMargin();
515 int const starty = yo_ - row_.ascent();
516 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
518 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
521 pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
523 pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
528 int RowPainter::paintAppendixStart(int y)
530 FontInfo pb_font = sane_font;
531 pb_font.setColor(Color_appendix);
538 docstring const label = _("Appendix");
539 theFontMetrics(pb_font).rectText(label, w, a, d);
541 int const text_start = int(xo_ + (width_ - w) / 2);
542 int const text_end = text_start + w;
544 pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
546 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
547 pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
549 return 3 * defaultRowHeight();
553 void RowPainter::paintFirst()
555 BufferParams const & bparams = pi_.base.bv->buffer().params();
556 Layout const & layout = par_.layout();
560 // start of appendix?
561 if (par_.params().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_first =
580 text_.isFirstInSequence(pit_) || !layout.isParagraphGroup();
581 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
583 if (layout.labelIsInline()
584 && (layout.labeltype != LABEL_STATIC || is_first)) {
586 } else if (is_first && layout.labelIsAbove()) {
587 paintTopLevelLabel();
592 void RowPainter::paintLabel()
594 docstring const str = par_.labelString();
598 bool const is_rtl = text_.isRTL(par_);
599 Layout const & layout = par_.layout();
600 FontInfo const font = labelFont();
601 FontMetrics const & fm = theFontMetrics(font);
605 x = width_ - leftMargin()
606 + fm.width(layout.labelsep);
608 x = x_ - fm.width(layout.labelsep)
612 pi_.pain.text(int(x), yo_, str, font);
616 void RowPainter::paintTopLevelLabel()
618 BufferParams const & bparams = pi_.base.bv->buffer().params();
619 bool const is_rtl = text_.isRTL(par_);
620 ParagraphParameters const & pparams = par_.params();
621 Layout const & layout = par_.layout();
622 FontInfo const font = labelFont();
623 docstring const str = par_.labelString();
627 double spacing_val = 1.0;
628 if (!pparams.spacing().isDefault())
629 spacing_val = pparams.spacing().getValue();
631 spacing_val = bparams.spacing().getValue();
633 FontMetrics const & fm = theFontMetrics(font);
635 int const labeladdon = int(fm.maxHeight()
636 * layout.spacing.getValue() * spacing_val);
639 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
640 + (layout.labelbottomsep * defaultRowHeight()));
643 if (layout.labeltype == LABEL_CENTERED) {
646 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
647 x -= fm.width(str) / 2;
649 x = width_ - leftMargin() - fm.width(str);
651 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
655 /** Check if the current paragraph is the last paragraph in a
657 static int getEndLabel(pit_type p, Text const & text)
659 ParagraphList const & pars = text.paragraphs();
661 depth_type par_depth = pars[p].getDepth();
662 while (pit != pit_type(pars.size())) {
663 Layout const & layout = pars[pit].layout();
664 int const endlabeltype = layout.endlabeltype;
666 if (endlabeltype != END_LABEL_NO_LABEL) {
667 if (p + 1 == pit_type(pars.size()))
670 depth_type const next_depth =
671 pars[p + 1].getDepth();
672 if (par_depth > next_depth ||
673 (par_depth == next_depth && layout != pars[p + 1].layout()))
679 pit = text.outerHook(pit);
680 if (pit != pit_type(pars.size()))
681 par_depth = pars[pit].getDepth();
683 return END_LABEL_NO_LABEL;
687 void RowPainter::paintLast()
689 bool const is_rtl = text_.isRTL(par_);
690 int const endlabel = getEndLabel(pit_, text_);
692 // paint imaginary end-of-paragraph character
694 Change const & change = par_.lookupChange(par_.size());
695 if (change.changed()) {
696 FontMetrics const & fm =
697 theFontMetrics(pi_.base.bv->buffer().params().getFont());
698 int const length = fm.maxAscent() / 2;
699 Color col = change.color();
701 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
702 Painter::line_solid, 3);
704 if (change.deleted()) {
705 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
706 yo_ + 2, col, Painter::line_solid, 3);
708 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
709 yo_ + 2, col, Painter::line_solid, 3);
717 case END_LABEL_FILLED_BOX: {
718 FontInfo const font = labelFont();
719 FontMetrics const & fm = theFontMetrics(font);
720 int const size = int(0.75 * fm.maxAscent());
721 int const y = yo_ - size;
722 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
723 int x = is_rtl ? nestMargin() + changebarMargin()
724 : max_row_width - text_metrics_.rightMargin(pm_);
726 // If needed, move the box a bit to avoid overlapping with text.
727 int const rem = max_row_width - row_.width();
729 x += is_rtl ? rem : - rem;
731 if (endlabel == END_LABEL_BOX)
732 pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
734 pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
738 case END_LABEL_STATIC: {
739 FontInfo const font = labelFont();
740 FontMetrics const & fm = theFontMetrics(font);
741 docstring const & str = par_.layout().endlabelstring();
742 double const x = is_rtl ? x_ - fm.width(str) : x_;
743 pi_.pain.text(int(x), yo_, str, font);
747 case END_LABEL_NO_LABEL:
748 if (lyxrc.paragraph_markers && size_type(pit_ + 1) < pars_.size()) {
749 docstring const s = docstring(1, char_type(0x00B6));
750 FontInfo f = FontInfo(text_.layoutFont(pit_));
751 f.setColor(Color_paragraphmarker);
752 pi_.pain.text(int(x_), yo_, s, f);
753 x_ += theFontMetrics(f).width(s);
760 void RowPainter::paintOnlyInsets()
762 CoordCache const & cache = pi_.base.bv->coordCache();
763 pos_type const end = row_.endpos();
764 for (pos_type pos = row_.pos(); pos != end; ++pos) {
765 // If outer row has changed, nested insets are repaint completely.
766 Inset const * inset = par_.getInset(pos);
767 bool const nested_inset = inset &&
768 ((inset->asInsetMath() &&
769 !inset->asInsetMath()->asMacroTemplate())
770 || inset->asInsetText()
771 || inset->asInsetTabular());
774 if (x_ > pi_.base.bv->workWidth()
775 || !cache.getInsets().has(inset))
777 x_ = cache.getInsets().x(inset);
779 bool const pi_selected = pi_.selected;
780 Cursor const & cur = pi_.base.bv->cursor();
781 if (cur.selection() && cur.text() == &text_
782 && cur.normalAnchor().text() == &text_)
783 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos;
784 paintInset(inset, pos);
785 pi_.selected = pi_selected;
790 void RowPainter::paintText()
792 pos_type const end = row_.endpos();
793 // Spaces at logical line breaks in bidi text must be skipped during
794 // painting. However, they may appear visually in the middle
795 // of a row; they must be skipped, wherever they are...
796 // * logically "abc_[HEBREW_\nHEBREW]"
797 // * visually "abc_[_WERBEH\nWERBEH]"
798 pos_type skipped_sep_vpos = -1;
799 pos_type body_pos = par_.beginOfBody();
801 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
805 Layout const & layout = par_.layout();
807 Change change_running;
808 int change_last_x = 0;
810 // check for possible inline completion
811 DocIterator const & inlineCompletionPos = pi_.base.bv->inlineCompletionPos();
812 pos_type inlineCompletionVPos = -1;
813 if (inlineCompletionPos.inTexted()
814 && inlineCompletionPos.text() == &text_
815 && inlineCompletionPos.pit() == pit_
816 && inlineCompletionPos.pos() - 1 >= row_.pos()
817 && inlineCompletionPos.pos() - 1 < row_.endpos()) {
818 // draw logically behind the previous character
819 inlineCompletionVPos = bidi_.log2vis(inlineCompletionPos.pos() - 1);
822 // Use font span to speed things up, see below
826 // If the last logical character is a separator, don't paint it, unless
827 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
828 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
829 skipped_sep_vpos = bidi_.log2vis(end - 1);
831 for (pos_type vpos = row_.pos(); vpos < end; ) {
832 if (x_ > pi_.base.bv->workWidth())
835 // Skip the separator at the logical end of the row
836 if (vpos == skipped_sep_vpos) {
841 pos_type const pos = bidi_.vis2log(vpos);
843 if (pos >= par_.size()) {
848 // Use font span to speed things up, see above
849 if (vpos < font_span.first || vpos > font_span.last) {
850 font_span = par_.fontSpan(vpos);
851 font = text_metrics_.displayFont(pit_, vpos);
853 // split font span if inline completion is inside
854 if (font_span.first <= inlineCompletionVPos
855 && font_span.last > inlineCompletionVPos)
856 font_span.last = inlineCompletionVPos;
859 const int width_pos = pm_.singleWidth(pos, font);
861 if (x_ + width_pos < 0) {
866 Change const & change = par_.lookupChange(pos);
867 if (change.changed() && !change_running.changed()) {
868 change_running = change;
869 change_last_x = int(x_);
872 Inset const * inset = par_.getInset(pos);
873 bool const highly_editable_inset = inset
874 && inset->editable();
876 // If we reach the end of a change or if the author changes, paint it.
877 // We also don't paint across things like tables
878 if (change_running.changed() && (highly_editable_inset
879 || !change.changed() || !change_running.isSimilarTo(change))) {
880 // Calculate 1/3 height of the buffer's default font
881 FontMetrics const & fm
882 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
883 float const y_bar = change_running.deleted() ?
884 yo_ - fm.maxAscent() / 3 : yo_ + 2 * solid_line_offset_ + solid_line_thickness_;
885 pi_.pain.line(change_last_x, int(y_bar), int(x_), int(y_bar),
886 change_running.color(), Painter::line_solid, solid_line_thickness_);
888 // Change might continue with a different author or type
889 if (change.changed() && !highly_editable_inset) {
890 change_running = change;
891 change_last_x = int(x_);
893 change_running.setUnchanged();
896 if (body_pos > 0 && pos == body_pos - 1) {
897 int const lwidth = theFontMetrics(labelFont())
898 .width(layout.labelsep);
900 x_ += row_.label_hfill + lwidth - width_pos;
903 // Is the inline completion in front of character?
904 if (font.isRightToLeft() && vpos == inlineCompletionVPos)
905 paintInlineCompletion(font);
907 if (par_.isSeparator(pos)) {
908 Font const orig_font = text_metrics_.displayFont(pit_, pos);
909 double const orig_x = x_;
910 double separator_width = width_pos;
912 separator_width += row_.separator;
913 paintSeparator(orig_x, separator_width, orig_font.fontInfo());
914 paintForeignMark(orig_x, orig_font.language());
918 // If outer row has changed, nested insets are repaint completely.
919 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
921 bool const pi_selected = pi_.selected;
922 Cursor const & cur = pi_.base.bv->cursor();
923 if (cur.selection() && cur.text() == &text_
924 && cur.normalAnchor().text() == &text_)
925 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos;
926 paintInset(inset, pos);
927 pi_.selected = pi_selected;
931 // paint as many characters as possible.
932 paintFromPos(vpos, change_running.changed());
935 // Is the inline completion after character?
936 if (!font.isRightToLeft() && vpos - 1 == inlineCompletionVPos)
937 paintInlineCompletion(font);
940 // if we reach the end of a struck out range, paint it
941 if (change_running.changed()) {
942 FontMetrics const & fm
943 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
944 float const y_bar = change_running.deleted() ?
945 yo_ - fm.maxAscent() / 3 : yo_ + 2 * solid_line_offset_ + solid_line_thickness_;
946 pi_.pain.line(change_last_x, int(y_bar), int(x_), int(y_bar),
947 change_running.color(), Painter::line_solid, solid_line_thickness_);
948 change_running.setUnchanged();
953 void RowPainter::paintSelection()
955 if (!row_.selection())
957 Cursor const & curs = pi_.base.bv->cursor();
958 DocIterator beg = curs.selectionBegin();
960 beg.pos() = row_.sel_beg;
962 DocIterator end = curs.selectionEnd();
964 end.pos() = row_.sel_end;
966 bool const begin_boundary = beg.pos() >= row_.endpos();
967 bool const end_boundary = row_.sel_end == row_.endpos();
969 DocIterator cur = beg;
970 cur.boundary(begin_boundary);
971 int x1 = text_metrics_.cursorX(beg.top(), begin_boundary);
972 int x2 = text_metrics_.cursorX(end.top(), end_boundary);
973 int const y1 = yo_ - row_.ascent();
974 int const y2 = y1 + row_.height();
976 int const rm = text_.isMainText() ? pi_.base.bv->rightMargin() : 0;
977 int const lm = text_.isMainText() ? pi_.base.bv->leftMargin() : 0;
980 if (row_.begin_margin_sel) {
981 if (text_.isRTL(beg.paragraph())) {
982 pi_.pain.fillRectangle(int(xo_ + x1), y1,
983 text_metrics_.width() - rm - x1, y2 - y1, Color_selection);
985 pi_.pain.fillRectangle(int(xo_ + lm), y1, x1 - lm, y2 - y1,
990 if (row_.end_margin_sel) {
991 if (text_.isRTL(beg.paragraph())) {
992 pi_.pain.fillRectangle(int(xo_ + lm), y1, x2 - lm, y2 - y1,
995 pi_.pain.fillRectangle(int(xo_ + x2), y1, text_metrics_.width() - rm - x2,
996 y2 - y1, Color_selection);
1000 // if we are on a boundary from the beginning, it's probably
1001 // a RTL boundary and we jump to the other side directly as this
1002 // segement is 0-size and confuses the logic below
1004 cur.boundary(false);
1006 // go through row and draw from RTL boundary to RTL boundary
1008 bool draw_now = false;
1010 // simplified cursorForward code below which does not
1011 // descend into insets and which does not go into the
1012 // next line. Compare the logic with the original cursorForward
1014 // if left of boundary -> just jump to right side, but
1015 // for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
1016 if (cur.boundary()) {
1017 cur.boundary(false);
1018 } else if (text_metrics_.isRTLBoundary(cur.pit(), cur.pos() + 1)) {
1019 // in front of RTL boundary -> Stay on this side of the boundary
1020 // because: ab|cDDEEFFghi -> abc|DDEEFFghi
1029 if (cur.pos() == row_.endpos())
1034 // the previous segment was just drawn, now the next starts
1035 x1 = text_metrics_.cursorX(cur.top(), cur.boundary());
1038 if (!(cur < end) || draw_now) {
1039 x2 = text_metrics_.cursorX(cur.top(), cur.boundary());
1040 pi_.pain.fillRectangle(int(xo_ + min(x1, x2)), y1, abs(x2 - x1),
1041 y2 - y1, Color_selection);
1043 // reset x1, so it is set again next round (which will be on the
1044 // right side of a boundary or at the selection end)
1051 void RowPainter::paintInlineCompletion(Font const & font)
1053 docstring completion = pi_.base.bv->inlineCompletion();
1054 FontInfo f = font.fontInfo();
1055 bool rtl = font.isRightToLeft();
1057 // draw the unique and the non-unique completion part
1058 // Note: this is not time-critical as it is
1059 // only done once per screen.
1060 size_t uniqueTo = pi_.base.bv->inlineCompletionUniqueChars();
1061 docstring s1 = completion.substr(0, uniqueTo);
1062 docstring s2 = completion.substr(uniqueTo);
1063 ColorCode c1 = Color_inlinecompletion;
1064 ColorCode c2 = Color_nonunique_inlinecompletion;
1074 pi_.pain.text(int(x_), yo_, s1, f);
1075 x_ += theFontMetrics(font).width(s1);
1080 pi_.pain.text(int(x_), yo_, s2, f);
1081 x_ += theFontMetrics(font).width(s2);