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"
27 #include "MetricsInfo.h"
28 #include "Paragraph.h"
29 #include "ParagraphMetrics.h"
30 #include "ParagraphParameters.h"
31 #include "TextMetrics.h"
34 #include "frontends/FontMetrics.h"
35 #include "frontends/Painter.h"
37 #include "insets/InsetText.h"
39 #include "mathed/InsetMath.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;
56 RowPainter::RowPainter(PainterInfo & pi,
57 Text const & text, pit_type pit, Row const & row, int x, int y)
58 : pi_(pi), text_(text),
59 text_metrics_(pi_.base.bv->textMetrics(&text)),
60 pars_(text.paragraphs()),
61 row_(row), pit_(pit), par_(text.paragraphs()[pit]),
62 pm_(text_metrics_.parMetrics(pit)), change_(pi_.change_),
63 xo_(x), yo_(y), width_(text_metrics_.width()),
64 solid_line_thickness_(1.0), solid_line_offset_(1),
65 dotted_line_thickness_(1.0), dotted_line_offset_(2)
67 bidi_.computeTables(par_, pi_.base.bv->buffer(), row_);
69 if (lyxrc.zoom >= 200) {
70 // derive the line thickness from zoom factor
71 // the zoom is given in percent
72 // (increase thickness at 250%, 450% etc.)
73 solid_line_thickness_ = (float)(int((lyxrc.zoom + 50) / 200.0));
74 // adjust line_offset_ too
75 solid_line_offset_ = 1 + int(0.5 * solid_line_thickness_);
77 if (lyxrc.zoom >= 100) {
78 // derive the line thickness from zoom factor
79 // the zoom is given in percent
80 // (increase thickness at 150%, 250% etc.)
81 dotted_line_thickness_ = (float)(int((lyxrc.zoom + 50) / 100.0));
82 // adjust line_offset_ too
83 dotted_line_offset_ = int(0.5 * dotted_line_thickness_) + 1;
88 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
92 LBUFERR(pit < int(text.paragraphs().size()));
96 FontInfo RowPainter::labelFont() const
98 FontInfo f = text_.labelFont(par_);
100 if (row_.begin_margin_sel || pi_.selected)
101 f.setPaintColor(Color_selectiontext);
106 int RowPainter::leftMargin() const
108 return text_metrics_.leftMargin(text_metrics_.width(), pit_,
112 // If you want to debug inset metrics uncomment the following line:
113 //#define DEBUG_METRICS
114 // This draws green lines around each inset.
117 void RowPainter::paintInset(Inset const * inset, pos_type const pos)
119 Font const font = text_metrics_.displayFont(pit_, pos);
121 LASSERT(inset, return);
122 // Backup full_repaint status because some insets (InsetTabular)
123 // requires a full repaint
124 bool pi_full_repaint = pi_.full_repaint;
126 pi_.base.font = inset->inheritFont() ? font.fontInfo() :
127 pi_.base.bv->buffer().params().getFont().fontInfo();
128 pi_.ltr_pos = (bidi_.level(pos) % 2 == 0);
129 Change prev_change = change_;
130 pi_.change_ = change_.changed() ? change_ : par_.lookupChange(pos);
132 int const x1 = int(x_);
133 pi_.base.bv->coordCache().insets().add(inset, x1, yo_);
134 // insets are painted completely. Recursive
135 // FIXME: it is wrong to completely paint the background
136 // if we want to do single row painting.
137 inset->drawBackground(pi_, x1, yo_);
138 inset->drawSelection(pi_, x1, yo_);
139 inset->draw(pi_, x1, yo_);
141 Dimension const & dim = pm_.insetDimension(inset);
143 paintForeignMark(x_, font.language(), dim.descent());
147 // Restore full_repaint status.
148 pi_.full_repaint = pi_full_repaint;
149 pi_.change_ = prev_change;
152 int const x2 = x1 + dim.wid;
153 int const y1 = yo_ + dim.des;
154 int const y2 = yo_ - dim.asc;
155 pi_.pain.line(x1, y1, x1, y2, Color_green);
156 pi_.pain.line(x1, y1, x2, y1, Color_green);
157 pi_.pain.line(x2, y1, x2, y2, Color_green);
158 pi_.pain.line(x1, y2, x2, y2, Color_green);
163 void RowPainter::paintChars(pos_type & vpos, Font const & font)
165 // This method takes up 70% of time when typing
166 pos_type pos = bidi_.vis2log(vpos);
167 pos_type start_pos = pos;
169 char_type c = par_.getChar(pos);
173 // special case for arabic
174 string const & lang = font.language()->lang();
175 bool const swap_paren = lang == "arabic_arabtex"
176 || lang == "arabic_arabi"
179 // FIXME: Why only round brackets and why the difference to
180 // Hebrew? See also Paragraph::getUChar
189 pos_type const end = row_.endpos();
190 FontSpan const font_span = par_.fontSpan(pos);
191 // Track-change status.
192 Change const & change_running = par_.lookupChange(pos);
195 bool const spell_state =
196 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
198 // collect as much similar chars as we can
199 for (++vpos ; vpos < end ; ++vpos) {
200 pos = bidi_.vis2log(vpos);
202 if (!font_span.inside(pos))
205 bool const new_spell_state =
206 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
207 if (new_spell_state != spell_state)
208 // Spell checker state changed here.
211 Change const & change = par_.lookupChange(pos);
212 if (!change_running.isSimilarTo(change))
213 // Track change type or author has changed.
216 char_type c = par_.getChar(pos);
221 // When row_.separator == 0, it is possible to print a
222 // string longer than a word in one fell swoop.
223 // Therefore there is no need to break at spaces.
224 if (!isPrintableNonspace(c)
225 && (c != ' ' || row_.separator > 0))
228 // FIXME: Why only round brackets and why the difference to
229 // Hebrew? See also Paragraph::getUChar
240 // Make pos point to the last character in the string.
241 // Using "pos = bidi_.vis2log(vpos)" does not work for some reason.
243 pos = bidi_.vis2log(vpos - 1);
245 // Now make pos point to the position _after_ the string.
246 // Using vis2log for that is not a good idea in general, we
247 // want logical ordering.
248 if (font.isVisibleRightToLeft())
254 str.replace(0,1,from_ascii(" "));
256 /* Because we do our own bidi, at this point the strings are
257 * already in visual order. However, Qt also applies its own
258 * bidi algorithm to strings that it paints to the screen.
259 * Therefore, if we were to paint Hebrew/Arabic words as a
260 * single string, the letters in the words would get reversed
261 * again. In order to avoid that, we force LTR drawing.
262 * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
263 * for an earlier thread on the subject
265 if (font.isVisibleRightToLeft()) {
266 reverse(str.begin(), str.end());
267 // If the string is reversed, the positions need to be adjusted
270 swap(start_pos, pos);
273 // at least part of text selected?
274 bool const some_sel = (pos >= row_.sel_beg && start_pos < row_.sel_end)
276 // all the text selected?
277 bool const all_sel = (start_pos >= row_.sel_beg && pos < row_.sel_end)
282 copy.fontInfo().setPaintColor(Color_selectiontext);
283 x_ += pi_.pain.text(int(x_), yo_, str, copy);
284 } else if (change_running.changed()) {
286 copy.fontInfo().setPaintColor(change_running.color());
287 x_ += pi_.pain.text(int(x_), yo_, str, copy);
288 } else if (!some_sel) {
289 x_ += pi_.pain.text(int(x_), yo_, str, font);
291 x_ += pi_.pain.text(int(x_), yo_, str, font, Color_selectiontext,
292 max(row_.sel_beg, start_pos) - start_pos,
293 min(row_.sel_end, pos) - start_pos);
298 void RowPainter::paintSeparator(double orig_x, double width,
299 FontInfo const & font)
301 pi_.pain.textDecoration(font, int(orig_x), yo_, int(width));
306 void RowPainter::paintForeignMark(double orig_x, Language const * lang,
309 if (!lyxrc.mark_foreign_language)
311 if (lang == latex_language)
313 if (lang == pi_.base.bv->buffer().params().language)
316 int const y = yo_ + solid_line_offset_ + desc + int(solid_line_thickness_/2);
317 pi_.pain.line(int(orig_x), y, int(x_), y, Color_language,
318 Painter::line_solid, solid_line_thickness_);
322 void RowPainter::paintMisspelledMark(double orig_x, bool changed)
324 // if changed the misspelled marker gets placed slightly lower than normal
325 // to avoid drawing at the same vertical offset
326 float const y = yo_ + solid_line_offset_ + solid_line_thickness_
327 + (changed ? solid_line_thickness_ + 1 : 0)
328 + dotted_line_offset_;
329 pi_.pain.line(int(orig_x), int(y), int(x_), int(y), Color_error,
330 Painter::line_onoffdash, dotted_line_thickness_);
334 void RowPainter::paintFromPos(pos_type & vpos, bool changed)
336 pos_type const pos = bidi_.vis2log(vpos);
337 Font const font = text_metrics_.displayFont(pit_, pos);
338 double const orig_x = x_;
340 paintChars(vpos, font);
341 paintForeignMark(orig_x, font.language());
343 // Paint the spelling mark if needed.
344 if (lyxrc.spellcheck_continuously && par_.isMisspelled(pos)) {
345 // check for cursor position
346 // don't draw misspelled marker for words at cursor position
347 // we don't want to disturb the process of text editing
348 BufferView const * bv = pi_.base.bv;
349 DocIterator const nw = bv->cursor().newWord();
350 bool new_word = false;
351 if (!nw.empty() && par_.id() == nw.paragraph().id()) {
352 pos_type cpos = nw.pos();
353 if (cpos > 0 && cpos == par_.size() && !par_.isWordSeparator(cpos-1))
355 else if (cpos > 0 && par_.isWordSeparator(cpos))
357 new_word = par_.isSameSpellRange(pos, cpos) ;
360 paintMisspelledMark(orig_x, changed);
365 void RowPainter::paintChangeBar()
367 pos_type const start = row_.pos();
368 pos_type end = row_.endpos();
370 if (par_.size() == end) {
371 // this is the last row of the paragraph;
372 // thus, we must also consider the imaginary end-of-par character
376 if (start == end || !par_.isChanged(start, end))
379 int const height = text_metrics_.isLastRow(pit_, row_)
383 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
387 void RowPainter::paintAppendix()
389 // only draw the appendix frame once (for the main text)
390 if (!par_.params().appendix() || !text_.isMainText())
393 int y = yo_ - row_.ascent();
395 if (par_.params().startOfAppendix())
396 y += 2 * defaultRowHeight();
398 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
399 pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
403 void RowPainter::paintDepthBar()
405 depth_type const depth = par_.getDepth();
410 depth_type prev_depth = 0;
411 if (!text_metrics_.isFirstRow(pit_, row_)) {
412 pit_type pit2 = pit_;
415 prev_depth = pars_[pit2].getDepth();
418 depth_type next_depth = 0;
419 if (!text_metrics_.isLastRow(pit_, row_)) {
420 pit_type pit2 = pit_;
421 if (row_.endpos() >= pars_[pit2].size())
423 next_depth = pars_[pit2].getDepth();
426 for (depth_type i = 1; i <= depth; ++i) {
427 int const w = nestMargin() / 5;
428 int x = int(xo_) + w * i;
429 // only consider the changebar space if we're drawing outermost text
430 if (text_.isMainText())
431 x += changebarMargin();
433 int const starty = yo_ - row_.ascent();
434 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
436 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
439 pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
441 pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
446 int RowPainter::paintAppendixStart(int y)
448 FontInfo pb_font = sane_font;
449 pb_font.setColor(Color_appendix);
456 docstring const label = _("Appendix");
457 theFontMetrics(pb_font).rectText(label, w, a, d);
459 int const text_start = int(xo_ + (width_ - w) / 2);
460 int const text_end = text_start + w;
462 pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
464 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
465 pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
467 return 3 * defaultRowHeight();
471 void RowPainter::paintFirst()
473 BufferParams const & bparams = pi_.base.bv->buffer().params();
474 Layout const & layout = par_.layout();
478 // start of appendix?
479 if (par_.params().startOfAppendix())
480 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
482 if (bparams.paragraph_separation == BufferParams::ParagraphSkipSeparation
484 if (layout.latextype == LATEX_PARAGRAPH
485 && !par_.getDepth()) {
486 y_top += bparams.getDefSkip().inPixels(*pi_.base.bv);
488 Layout const & playout = pars_[pit_ - 1].layout();
489 if (playout.latextype == LATEX_PARAGRAPH
490 && !pars_[pit_ - 1].getDepth()) {
491 // is it right to use defskip here, too? (AS)
492 y_top += bparams.getDefSkip().inPixels(*pi_.base.bv);
497 bool const is_first =
498 text_.isFirstInSequence(pit_) || !layout.isParagraphGroup();
499 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
501 if (layout.labelIsInline()
502 && (layout.labeltype != LABEL_STATIC || is_first)) {
504 } else if (is_first && layout.labelIsAbove()) {
505 paintTopLevelLabel();
510 void RowPainter::paintLabel()
512 docstring const str = par_.labelString();
516 bool const is_rtl = text_.isRTL(par_);
517 Layout const & layout = par_.layout();
518 FontInfo const font = labelFont();
519 FontMetrics const & fm = theFontMetrics(font);
523 x = width_ - leftMargin()
524 + fm.width(layout.labelsep);
526 x = x_ - fm.width(layout.labelsep)
530 pi_.pain.text(int(x), yo_, str, font);
534 void RowPainter::paintTopLevelLabel()
536 BufferParams const & bparams = pi_.base.bv->buffer().params();
537 bool const is_rtl = text_.isRTL(par_);
538 ParagraphParameters const & pparams = par_.params();
539 Layout const & layout = par_.layout();
540 FontInfo const font = labelFont();
541 docstring const str = par_.labelString();
545 double spacing_val = 1.0;
546 if (!pparams.spacing().isDefault())
547 spacing_val = pparams.spacing().getValue();
549 spacing_val = bparams.spacing().getValue();
551 FontMetrics const & fm = theFontMetrics(font);
553 int const labeladdon = int(fm.maxHeight()
554 * layout.spacing.getValue() * spacing_val);
557 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
558 + (layout.labelbottomsep * defaultRowHeight()));
561 if (layout.labeltype == LABEL_CENTERED) {
564 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
565 x -= fm.width(str) / 2;
567 x = width_ - leftMargin() - fm.width(str);
569 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
573 /** Check if the current paragraph is the last paragraph in a
575 static int getEndLabel(pit_type p, Text const & text)
577 ParagraphList const & pars = text.paragraphs();
579 depth_type par_depth = pars[p].getDepth();
580 while (pit != pit_type(pars.size())) {
581 Layout const & layout = pars[pit].layout();
582 int const endlabeltype = layout.endlabeltype;
584 if (endlabeltype != END_LABEL_NO_LABEL) {
585 if (p + 1 == pit_type(pars.size()))
588 depth_type const next_depth =
589 pars[p + 1].getDepth();
590 if (par_depth > next_depth ||
591 (par_depth == next_depth && layout != pars[p + 1].layout()))
597 pit = text.outerHook(pit);
598 if (pit != pit_type(pars.size()))
599 par_depth = pars[pit].getDepth();
601 return END_LABEL_NO_LABEL;
605 void RowPainter::paintLast()
607 bool const is_rtl = text_.isRTL(par_);
608 int const endlabel = getEndLabel(pit_, text_);
610 // paint imaginary end-of-paragraph character
612 Change const & change = par_.lookupChange(par_.size());
613 if (change.changed()) {
614 FontMetrics const & fm =
615 theFontMetrics(pi_.base.bv->buffer().params().getFont());
616 int const length = fm.maxAscent() / 2;
617 Color col = change.color();
619 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
620 Painter::line_solid, 3);
622 if (change.deleted()) {
623 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
624 yo_ + 2, col, Painter::line_solid, 3);
626 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
627 yo_ + 2, col, Painter::line_solid, 3);
635 case END_LABEL_FILLED_BOX: {
636 FontInfo const font = labelFont();
637 FontMetrics const & fm = theFontMetrics(font);
638 int const size = int(0.75 * fm.maxAscent());
639 int const y = yo_ - size;
640 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
641 int x = is_rtl ? nestMargin() + changebarMargin()
642 : max_row_width - text_metrics_.rightMargin(pm_);
644 // If needed, move the box a bit to avoid overlapping with text.
645 int const rem = max_row_width - row_.width();
647 x += is_rtl ? rem : - rem;
649 if (endlabel == END_LABEL_BOX)
650 pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
652 pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
656 case END_LABEL_STATIC: {
657 FontInfo const font = labelFont();
658 FontMetrics const & fm = theFontMetrics(font);
659 docstring const & str = par_.layout().endlabelstring();
660 double const x = is_rtl ? x_ - fm.width(str) : x_;
661 pi_.pain.text(int(x), yo_, str, font);
665 case END_LABEL_NO_LABEL:
666 if (lyxrc.paragraph_markers && size_type(pit_ + 1) < pars_.size()) {
667 docstring const s = docstring(1, char_type(0x00B6));
668 FontInfo f = FontInfo(text_.layoutFont(pit_));
669 f.setColor(Color_paragraphmarker);
670 pi_.pain.text(int(x_), yo_, s, f);
671 x_ += theFontMetrics(f).width(s);
678 void RowPainter::paintOnlyInsets()
680 CoordCache const & cache = pi_.base.bv->coordCache();
681 pos_type const end = row_.endpos();
682 for (pos_type pos = row_.pos(); pos != end; ++pos) {
683 // If outer row has changed, nested insets are repaint completely.
684 Inset const * inset = par_.getInset(pos);
685 bool const nested_inset = inset &&
686 ((inset->asInsetMath() &&
687 !inset->asInsetMath()->asMacroTemplate())
688 || inset->asInsetText()
689 || inset->asInsetTabular());
692 if (x_ > pi_.base.bv->workWidth()
693 || !cache.getInsets().has(inset))
695 x_ = cache.getInsets().x(inset);
697 bool const pi_selected = pi_.selected;
698 Cursor const & cur = pi_.base.bv->cursor();
699 if (cur.selection() && cur.text() == &text_
700 && cur.normalAnchor().text() == &text_)
701 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos;
702 paintInset(inset, pos);
703 pi_.selected = pi_selected;
708 void RowPainter::paintText()
710 pos_type const end = row_.endpos();
711 // Spaces at logical line breaks in bidi text must be skipped during
712 // painting. However, they may appear visually in the middle
713 // of a row; they must be skipped, wherever they are...
714 // * logically "abc_[HEBREW_\nHEBREW]"
715 // * visually "abc_[_WERBEH\nWERBEH]"
716 pos_type skipped_sep_vpos = -1;
717 pos_type body_pos = par_.beginOfBody();
719 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
723 Layout const & layout = par_.layout();
725 Change change_running;
726 int change_last_x = 0;
728 // check for possible inline completion
729 DocIterator const & inlineCompletionPos = pi_.base.bv->inlineCompletionPos();
730 pos_type inlineCompletionVPos = -1;
731 if (inlineCompletionPos.inTexted()
732 && inlineCompletionPos.text() == &text_
733 && inlineCompletionPos.pit() == pit_
734 && inlineCompletionPos.pos() - 1 >= row_.pos()
735 && inlineCompletionPos.pos() - 1 < row_.endpos()) {
736 // draw logically behind the previous character
737 inlineCompletionVPos = bidi_.log2vis(inlineCompletionPos.pos() - 1);
740 // Use font span to speed things up, see below
744 // If the last logical character is a separator, don't paint it, unless
745 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
746 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
747 skipped_sep_vpos = bidi_.log2vis(end - 1);
749 for (pos_type vpos = row_.pos(); vpos < end; ) {
750 if (x_ > pi_.base.bv->workWidth())
753 // Skip the separator at the logical end of the row
754 if (vpos == skipped_sep_vpos) {
759 pos_type const pos = bidi_.vis2log(vpos);
761 if (pos >= par_.size()) {
766 // Use font span to speed things up, see above
767 if (vpos < font_span.first || vpos > font_span.last) {
768 font_span = par_.fontSpan(vpos);
769 font = text_metrics_.displayFont(pit_, vpos);
771 // split font span if inline completion is inside
772 if (font_span.first <= inlineCompletionVPos
773 && font_span.last > inlineCompletionVPos)
774 font_span.last = inlineCompletionVPos;
777 const int width_pos = pm_.singleWidth(pos, font);
779 if (x_ + width_pos < 0) {
784 Change const & change = par_.lookupChange(pos);
785 if (change.changed() && !change_running.changed()) {
786 change_running = change;
787 change_last_x = int(x_);
790 Inset const * inset = par_.getInset(pos);
791 bool const highly_editable_inset = inset
792 && inset->editable();
794 // If we reach the end of a change or if the author changes, paint it.
795 // We also don't paint across things like tables
796 if (change_running.changed() && (highly_editable_inset
797 || !change.changed() || !change_running.isSimilarTo(change))) {
798 // Calculate 1/3 height of the buffer's default font
799 FontMetrics const & fm
800 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
801 float const y_bar = change_running.deleted() ?
802 yo_ - fm.maxAscent() / 3 : yo_ + 2 * solid_line_offset_ + solid_line_thickness_;
803 pi_.pain.line(change_last_x, int(y_bar), int(x_), int(y_bar),
804 change_running.color(), Painter::line_solid, solid_line_thickness_);
806 // Change might continue with a different author or type
807 if (change.changed() && !highly_editable_inset) {
808 change_running = change;
809 change_last_x = int(x_);
811 change_running.setUnchanged();
814 if (body_pos > 0 && pos == body_pos - 1) {
815 int const lwidth = theFontMetrics(labelFont())
816 .width(layout.labelsep);
818 x_ += row_.label_hfill + lwidth - width_pos;
821 // Is the inline completion in front of character?
822 if (font.isRightToLeft() && vpos == inlineCompletionVPos)
823 paintInlineCompletion(font);
825 if (par_.isSeparator(pos)) {
826 Font const orig_font = text_metrics_.displayFont(pit_, pos);
827 double const orig_x = x_;
828 double separator_width = width_pos;
830 separator_width += row_.separator;
831 paintSeparator(orig_x, separator_width, orig_font.fontInfo());
832 paintForeignMark(orig_x, orig_font.language());
836 // If outer row has changed, nested insets are repaint completely.
837 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
839 bool const pi_selected = pi_.selected;
840 Cursor const & cur = pi_.base.bv->cursor();
841 if (cur.selection() && cur.text() == &text_
842 && cur.normalAnchor().text() == &text_)
843 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos;
844 paintInset(inset, pos);
845 pi_.selected = pi_selected;
849 // paint as many characters as possible.
850 paintFromPos(vpos, change_running.changed());
853 // Is the inline completion after character?
854 if (!font.isRightToLeft() && vpos - 1 == inlineCompletionVPos)
855 paintInlineCompletion(font);
858 // if we reach the end of a struck out range, paint it
859 if (change_running.changed()) {
860 FontMetrics const & fm
861 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
862 float const y_bar = change_running.deleted() ?
863 yo_ - fm.maxAscent() / 3 : yo_ + 2 * solid_line_offset_ + solid_line_thickness_;
864 pi_.pain.line(change_last_x, int(y_bar), int(x_), int(y_bar),
865 change_running.color(), Painter::line_solid, solid_line_thickness_);
866 change_running.setUnchanged();
871 void RowPainter::paintSelection()
873 if (!row_.selection())
875 Cursor const & curs = pi_.base.bv->cursor();
876 DocIterator beg = curs.selectionBegin();
878 beg.pos() = row_.sel_beg;
880 DocIterator end = curs.selectionEnd();
882 end.pos() = row_.sel_end;
884 bool const begin_boundary = beg.pos() >= row_.endpos();
885 bool const end_boundary = row_.sel_end == row_.endpos();
887 DocIterator cur = beg;
888 cur.boundary(begin_boundary);
889 int x1 = text_metrics_.cursorX(beg.top(), begin_boundary);
890 int x2 = text_metrics_.cursorX(end.top(), end_boundary);
891 int const y1 = yo_ - row_.ascent();
892 int const y2 = y1 + row_.height();
894 int const rm = text_.isMainText() ? pi_.base.bv->rightMargin() : 0;
895 int const lm = text_.isMainText() ? pi_.base.bv->leftMargin() : 0;
898 if (row_.begin_margin_sel) {
899 if (text_.isRTL(beg.paragraph())) {
900 pi_.pain.fillRectangle(int(xo_ + x1), y1,
901 text_metrics_.width() - rm - x1, y2 - y1, Color_selection);
903 pi_.pain.fillRectangle(int(xo_ + lm), y1, x1 - lm, y2 - y1,
908 if (row_.end_margin_sel) {
909 if (text_.isRTL(beg.paragraph())) {
910 pi_.pain.fillRectangle(int(xo_ + lm), y1, x2 - lm, y2 - y1,
913 pi_.pain.fillRectangle(int(xo_ + x2), y1, text_metrics_.width() - rm - x2,
914 y2 - y1, Color_selection);
918 // if we are on a boundary from the beginning, it's probably
919 // a RTL boundary and we jump to the other side directly as this
920 // segement is 0-size and confuses the logic below
924 // go through row and draw from RTL boundary to RTL boundary
926 bool draw_now = false;
928 // simplified cursorForward code below which does not
929 // descend into insets and which does not go into the
930 // next line. Compare the logic with the original cursorForward
932 // if left of boundary -> just jump to right side, but
933 // for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
934 if (cur.boundary()) {
936 } else if (text_metrics_.isRTLBoundary(cur.pit(), cur.pos() + 1)) {
937 // in front of RTL boundary -> Stay on this side of the boundary
938 // because: ab|cDDEEFFghi -> abc|DDEEFFghi
947 if (cur.pos() == row_.endpos())
952 // the previous segment was just drawn, now the next starts
953 x1 = text_metrics_.cursorX(cur.top(), cur.boundary());
956 if (!(cur < end) || draw_now) {
957 x2 = text_metrics_.cursorX(cur.top(), cur.boundary());
958 pi_.pain.fillRectangle(int(xo_ + min(x1, x2)), y1, abs(x2 - x1),
959 y2 - y1, Color_selection);
961 // reset x1, so it is set again next round (which will be on the
962 // right side of a boundary or at the selection end)
969 void RowPainter::paintInlineCompletion(Font const & font)
971 docstring completion = pi_.base.bv->inlineCompletion();
972 FontInfo f = font.fontInfo();
973 bool rtl = font.isRightToLeft();
975 // draw the unique and the non-unique completion part
976 // Note: this is not time-critical as it is
977 // only done once per screen.
978 size_t uniqueTo = pi_.base.bv->inlineCompletionUniqueChars();
979 docstring s1 = completion.substr(0, uniqueTo);
980 docstring s2 = completion.substr(uniqueTo);
981 ColorCode c1 = Color_inlinecompletion;
982 ColorCode c2 = Color_nonunique_inlinecompletion;
992 pi_.pain.text(int(x_), yo_, s1, f);
993 x_ += theFontMetrics(font).width(s1);
998 pi_.pain.text(int(x_), yo_, s2, f);
999 x_ += theFontMetrics(font).width(s2);