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 pi_.base.font = inset->inheritFont() ? font.fontInfo() :
129 pi_.base.bv->buffer().params().getFont().fontInfo();
130 pi_.ltr_pos = (bidi_.level(pos) % 2 == 0);
131 Change prev_change = change_;
132 pi_.change_ = change_.changed() ? change_ : par_.lookupChange(pos);
134 int const x1 = int(x_);
135 pi_.base.bv->coordCache().insets().add(inset, x1, yo_);
136 // insets are painted completely. Recursive
137 // FIXME: it is wrong to completely paint the background
138 // if we want to do single row painting.
139 inset->drawBackground(pi_, x1, yo_);
140 inset->drawSelection(pi_, x1, yo_);
141 inset->draw(pi_, x1, yo_);
143 Dimension const & dim = pm_.insetDimension(inset);
145 paintForeignMark(x_, font.language(), dim.descent());
149 // Restore full_repaint status.
150 pi_.full_repaint = pi_full_repaint;
151 pi_.change_ = prev_change;
154 int const x2 = x1 + dim.wid;
155 int const y1 = yo_ + dim.des;
156 int const y2 = yo_ - dim.asc;
157 pi_.pain.line(x1, y1, x1, y2, Color_green);
158 pi_.pain.line(x1, y1, x2, y1, Color_green);
159 pi_.pain.line(x2, y1, x2, y2, Color_green);
160 pi_.pain.line(x1, y2, x2, y2, Color_green);
165 void RowPainter::paintHebrewComposeChar(pos_type & vpos, FontInfo const & font)
167 pos_type pos = bidi_.vis2log(vpos);
172 char_type c = par_.getChar(pos);
176 int const width = theFontMetrics(font).width(c);
179 for (pos_type i = pos - 1; i >= 0; --i) {
181 if (!Encodings::isHebrewComposeChar(c)) {
182 if (isPrintableNonspace(c)) {
183 int const width2 = pm_.singleWidth(i,
184 text_metrics_.displayFont(pit_, i));
185 dx = (c == 0x05e8 || // resh
186 c == 0x05d3) // dalet
188 : (width2 - width) / 2;
195 pi_.pain.text(int(x_) + dx, yo_, str, font);
199 void RowPainter::paintArabicComposeChar(pos_type & vpos, FontInfo const & font)
201 pos_type pos = bidi_.vis2log(vpos);
205 char_type c = par_.getChar(pos);
206 c = par_.transformChar(c, pos);
210 int const width = theFontMetrics(font).width(c);
213 for (pos_type i = pos - 1; i >= 0; --i) {
215 if (!Encodings::isArabicComposeChar(c)) {
216 if (isPrintableNonspace(c)) {
217 int const width2 = pm_.singleWidth(i,
218 text_metrics_.displayFont(pit_, i));
219 dx = (width2 - width) / 2;
225 pi_.pain.text(int(x_) + dx, yo_, str, font);
229 void RowPainter::paintChars(pos_type & vpos, FontInfo const & font,
230 bool hebrew, bool arabic)
232 // This method takes up 70% of time when typing
233 pos_type pos = bidi_.vis2log(vpos);
235 char_type prev_char = par_.getChar(pos);
236 vector<char_type> str;
238 str.push_back(prev_char);
241 char_type c = str[0];
246 str[0] = par_.transformChar(c, pos);
249 pos_type const end = row_.endpos();
250 FontSpan const font_span = par_.fontSpan(pos);
251 // Track-change status.
252 Change const & change_running = par_.lookupChange(pos);
255 bool const selection = (pos >= row_.sel_beg && pos < row_.sel_end)
259 bool const spell_state =
260 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
262 // collect as much similar chars as we can
263 for (++vpos ; vpos < end ; ++vpos) {
264 // Work-around bug #6920
265 // The bug can be reproduced with DejaVu font under Linux.
266 // The issue is that we compute the metrics character by character
267 // in ParagraphMetrics::singleWidth(); but we paint word by word
268 // for performance reason.
269 // Maybe a more general fix would be draw character by character
270 // for some predefined fonts on some platform. In arabic and
271 // Hebrew we already do paint this way.
272 if (prev_char == 'f' || lyxrc.force_paint_single_char)
275 pos = bidi_.vis2log(vpos);
276 if (pos < font_span.first || pos > font_span.last)
279 bool const new_selection = pos >= row_.sel_beg && pos < row_.sel_end;
280 if (new_selection != selection)
281 // Selection ends or starts here.
284 bool const new_spell_state =
285 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
286 if (new_spell_state != spell_state)
287 // Spell checker state changed here.
290 Change const & change = par_.lookupChange(pos);
291 if (!change_running.isSimilarTo(change))
292 // Track change type or author has changed.
295 char_type c = par_.getChar(pos);
300 if (!isPrintableNonspace(c))
303 /* Because we do our own bidi, at this point the strings are
304 * already in visual order. However, Qt also applies its own
305 * bidi algorithm to strings that it paints to the screen.
306 * Therefore, if we were to paint Hebrew/Arabic words as a
307 * single string, the letters in the words would get reversed
308 * again. In order to avoid that, we don't collect Hebrew/
309 * Arabic characters, but rather paint them one at a time.
310 * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
315 /* FIXME: these checks are irrelevant, since 'arabic' and
316 * 'hebrew' alone are already going to trigger a break.
317 * However, this should not be removed completely, because
318 * if an alternative solution is found which allows grouping
319 * of arabic and hebrew characters, then these breaks may have
322 if (arabic && Encodings::isArabicComposeChar(c))
325 if (hebrew && Encodings::isHebrewComposeChar(c))
334 c = par_.transformChar(c, pos);
335 /* see comment in hebrew, explaining why we break */
343 docstring s(&str[0], str.size());
346 s.replace(0,1,from_ascii(" "));
348 if (!selection && !change_running.changed()) {
349 x_ += pi_.pain.text(int(x_), yo_, s, font);
353 FontInfo copy = font;
354 if (change_running.changed())
355 copy.setPaintColor(change_running.color());
357 copy.setPaintColor(Color_selectiontext);
359 x_ += pi_.pain.text(int(x_), yo_, s, copy);
363 void RowPainter::paintSeparator(double orig_x, double width,
364 FontInfo const & font)
366 pi_.pain.textDecoration(font, int(orig_x), yo_, int(width));
371 void RowPainter::paintForeignMark(double orig_x, Language const * lang,
374 if (!lyxrc.mark_foreign_language)
376 if (lang == latex_language)
378 if (lang == pi_.base.bv->buffer().params().language)
381 int const y = yo_ + solid_line_offset_ + desc + int(solid_line_thickness_/2);
382 pi_.pain.line(int(orig_x), y, int(x_), y, Color_language,
383 Painter::line_solid, solid_line_thickness_);
387 void RowPainter::paintMisspelledMark(double orig_x, bool changed)
389 // if changed the misspelled marker gets placed slightly lower than normal
390 // to avoid drawing at the same vertical offset
391 float const y = yo_ + solid_line_offset_ + solid_line_thickness_
392 + (changed ? solid_line_thickness_ + 1 : 0)
393 + dotted_line_offset_;
394 pi_.pain.line(int(orig_x), int(y), int(x_), int(y), Color_error,
395 Painter::line_onoffdash, dotted_line_thickness_);
399 void RowPainter::paintFromPos(pos_type & vpos, bool changed)
401 pos_type const pos = bidi_.vis2log(vpos);
402 Font const orig_font = text_metrics_.displayFont(pit_, pos);
403 double const orig_x = x_;
405 // usual characters, no insets
406 char_type const c = par_.getChar(pos);
408 // special case languages
409 string const & lang = orig_font.language()->lang();
410 bool const hebrew = lang == "hebrew";
411 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" ||
415 bool const misspelled =
416 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
418 // draw as many chars as we can
419 if ((!hebrew && !arabic)
420 || (hebrew && !Encodings::isHebrewComposeChar(c))
421 || (arabic && !Encodings::isArabicComposeChar(c))) {
422 paintChars(vpos, orig_font.fontInfo(), hebrew, arabic);
424 paintHebrewComposeChar(vpos, orig_font.fontInfo());
426 paintArabicComposeChar(vpos, orig_font.fontInfo());
429 paintForeignMark(orig_x, orig_font.language());
431 if (lyxrc.spellcheck_continuously && misspelled) {
432 // check for cursor position
433 // don't draw misspelled marker for words at cursor position
434 // we don't want to disturb the process of text editing
435 BufferView const * bv = pi_.base.bv;
436 DocIterator const nw = bv->cursor().newWord();
437 bool new_word = false;
438 if (!nw.empty() && par_.id() == nw.paragraph().id()) {
439 pos_type cpos = nw.pos();
440 if (cpos > 0 && cpos == par_.size() && !par_.isWordSeparator(cpos-1))
442 else if (cpos > 0 && par_.isWordSeparator(cpos))
444 new_word = par_.isSameSpellRange(pos, cpos) ;
447 paintMisspelledMark(orig_x, changed);
452 void RowPainter::paintChangeBar()
454 pos_type const start = row_.pos();
455 pos_type end = row_.endpos();
457 if (par_.size() == end) {
458 // this is the last row of the paragraph;
459 // thus, we must also consider the imaginary end-of-par character
463 if (start == end || !par_.isChanged(start, end))
466 int const height = text_metrics_.isLastRow(pit_, row_)
470 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
474 void RowPainter::paintAppendix()
476 // only draw the appendix frame once (for the main text)
477 if (!par_.params().appendix() || !text_.isMainText())
480 int y = yo_ - row_.ascent();
482 if (par_.params().startOfAppendix())
483 y += 2 * defaultRowHeight();
485 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
486 pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
490 void RowPainter::paintDepthBar()
492 depth_type const depth = par_.getDepth();
497 depth_type prev_depth = 0;
498 if (!text_metrics_.isFirstRow(pit_, row_)) {
499 pit_type pit2 = pit_;
502 prev_depth = pars_[pit2].getDepth();
505 depth_type next_depth = 0;
506 if (!text_metrics_.isLastRow(pit_, row_)) {
507 pit_type pit2 = pit_;
508 if (row_.endpos() >= pars_[pit2].size())
510 next_depth = pars_[pit2].getDepth();
513 for (depth_type i = 1; i <= depth; ++i) {
514 int const w = nestMargin() / 5;
515 int x = int(xo_) + w * i;
516 // only consider the changebar space if we're drawing outermost text
517 if (text_.isMainText())
518 x += changebarMargin();
520 int const starty = yo_ - row_.ascent();
521 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
523 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
526 pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
528 pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
533 int RowPainter::paintAppendixStart(int y)
535 FontInfo pb_font = sane_font;
536 pb_font.setColor(Color_appendix);
543 docstring const label = _("Appendix");
544 theFontMetrics(pb_font).rectText(label, w, a, d);
546 int const text_start = int(xo_ + (width_ - w) / 2);
547 int const text_end = text_start + w;
549 pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
551 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
552 pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
554 return 3 * defaultRowHeight();
558 void RowPainter::paintFirst()
560 BufferParams const & bparams = pi_.base.bv->buffer().params();
561 Layout const & layout = par_.layout();
565 // start of appendix?
566 if (par_.params().startOfAppendix())
567 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
569 if (bparams.paragraph_separation == BufferParams::ParagraphSkipSeparation
571 if (layout.latextype == LATEX_PARAGRAPH
572 && !par_.getDepth()) {
573 y_top += bparams.getDefSkip().inPixels(*pi_.base.bv);
575 Layout const & playout = pars_[pit_ - 1].layout();
576 if (playout.latextype == LATEX_PARAGRAPH
577 && !pars_[pit_ - 1].getDepth()) {
578 // is it right to use defskip here, too? (AS)
579 y_top += bparams.getDefSkip().inPixels(*pi_.base.bv);
584 bool const is_seq = text_.isFirstInSequence(pit_);
585 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
587 if (layout.labeltype >= LABEL_STATIC
588 && (layout.labeltype != LABEL_STATIC
589 || layout.latextype != LATEX_ENVIRONMENT
593 && (layout.labeltype == LABEL_TOP_ENVIRONMENT
594 || layout.labeltype == LABEL_BIBLIO
595 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
596 // the labels at the top of an environment.
597 // More or less for bibliography
598 paintTopLevelLabel();
603 void RowPainter::paintLabel()
605 docstring const str = par_.labelString();
609 BufferParams const & bparams = pi_.base.bv->buffer().params();
610 bool const is_rtl = text_.isRTL(par_);
611 Layout const & layout = par_.layout();
612 ParagraphParameters const & pparams = par_.params();
613 FontInfo const font = labelFont();
614 FontMetrics const & fm = theFontMetrics(font);
618 // this is special code for the chapter layout. This is
619 // printed in an extra row and has a pagebreak at
621 if (layout.counter == "chapter") {
622 double spacing_val = 1.0;
623 if (!pparams.spacing().isDefault()) {
624 spacing_val = pparams.spacing().getValue();
626 spacing_val = bparams.spacing().getValue();
629 int const labeladdon = int(fm.maxHeight() * layout.spacing.getValue() * spacing_val);
631 int const maxdesc = int(fm.maxDescent() * layout.spacing.getValue() * spacing_val)
632 + int(layout.parsep) * defaultRowHeight();
635 x = width_ - leftMargin() -
639 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
642 x = width_ - leftMargin()
643 + fm.width(layout.labelsep);
645 x = x_ - fm.width(layout.labelsep)
649 pi_.pain.text(int(x), yo_, str, font);
654 void RowPainter::paintTopLevelLabel()
656 BufferParams const & bparams = pi_.base.bv->buffer().params();
657 bool const is_rtl = text_.isRTL(par_);
658 ParagraphParameters const & pparams = par_.params();
659 Layout const & layout = par_.layout();
660 FontInfo const font = labelFont();
661 docstring const str = par_.labelString();
665 double spacing_val = 1.0;
666 if (!pparams.spacing().isDefault())
667 spacing_val = pparams.spacing().getValue();
669 spacing_val = bparams.spacing().getValue();
671 FontMetrics const & fm = theFontMetrics(font);
673 int const labeladdon = int(fm.maxHeight()
674 * layout.spacing.getValue() * spacing_val);
677 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
678 + (layout.labelbottomsep * defaultRowHeight()));
681 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
684 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
685 x -= fm.width(str) / 2;
687 x = width_ - leftMargin() - fm.width(str);
689 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
692 /** Check if the current paragraph is the last paragraph in a
694 static int getEndLabel(pit_type p, Text const & text)
696 ParagraphList const & pars = text.paragraphs();
698 depth_type par_depth = pars[p].getDepth();
699 while (pit != pit_type(pars.size())) {
700 Layout const & layout = pars[pit].layout();
701 int const endlabeltype = layout.endlabeltype;
703 if (endlabeltype != END_LABEL_NO_LABEL) {
704 if (p + 1 == pit_type(pars.size()))
707 depth_type const next_depth =
708 pars[p + 1].getDepth();
709 if (par_depth > next_depth ||
710 (par_depth == next_depth && layout != pars[p + 1].layout()))
716 pit = text.outerHook(pit);
717 if (pit != pit_type(pars.size()))
718 par_depth = pars[pit].getDepth();
720 return END_LABEL_NO_LABEL;
724 void RowPainter::paintLast()
726 bool const is_rtl = text_.isRTL(par_);
727 int const endlabel = getEndLabel(pit_, text_);
729 // paint imaginary end-of-paragraph character
731 Change const & change = par_.lookupChange(par_.size());
732 if (change.changed()) {
733 FontMetrics const & fm =
734 theFontMetrics(pi_.base.bv->buffer().params().getFont());
735 int const length = fm.maxAscent() / 2;
736 Color col = change.color();
738 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
739 Painter::line_solid, 3);
741 if (change.deleted()) {
742 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
743 yo_ + 2, col, Painter::line_solid, 3);
745 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
746 yo_ + 2, col, Painter::line_solid, 3);
754 case END_LABEL_FILLED_BOX: {
755 FontInfo const font = labelFont();
756 FontMetrics const & fm = theFontMetrics(font);
757 int const size = int(0.75 * fm.maxAscent());
758 int const y = yo_ - size;
759 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
760 int x = is_rtl ? nestMargin() + changebarMargin()
761 : max_row_width - text_metrics_.rightMargin(pm_);
763 // If needed, move the box a bit to avoid overlapping with text.
764 int const rem = max_row_width - row_.width();
766 x += is_rtl ? rem : - rem;
768 if (endlabel == END_LABEL_BOX)
769 pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
771 pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
775 case END_LABEL_STATIC: {
776 FontInfo const font = labelFont();
777 FontMetrics const & fm = theFontMetrics(font);
778 docstring const & str = par_.layout().endlabelstring();
779 double const x = is_rtl ? x_ - fm.width(str) : x_;
780 pi_.pain.text(int(x), yo_, str, font);
784 case END_LABEL_NO_LABEL:
785 if (lyxrc.paragraph_markers && size_type(pit_ + 1) < pars_.size()) {
786 docstring const s = docstring(1, char_type(0x00B6));
787 FontInfo f = FontInfo();
788 FontMetrics const & fm = theFontMetrics(f);
789 f.setColor(Color_paragraphmarker);
790 pi_.pain.text(int(x_), yo_, s, f);
798 void RowPainter::paintOnlyInsets()
800 CoordCache const & cache = pi_.base.bv->coordCache();
801 pos_type const end = row_.endpos();
802 for (pos_type pos = row_.pos(); pos != end; ++pos) {
803 // If outer row has changed, nested insets are repaint completely.
804 Inset const * inset = par_.getInset(pos);
805 bool const nested_inset = inset &&
806 ((inset->asInsetMath() &&
807 !inset->asInsetMath()->asMacroTemplate())
808 || inset->asInsetText()
809 || inset->asInsetTabular());
812 if (x_ > pi_.base.bv->workWidth()
813 || !cache.getInsets().has(inset))
815 x_ = cache.getInsets().x(inset);
817 bool const pi_selected = pi_.selected;
818 Cursor const & cur = pi_.base.bv->cursor();
819 if (cur.selection() && cur.text() == &text_
820 && cur.normalAnchor().text() == &text_)
821 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos;
822 paintInset(inset, pos);
823 pi_.selected = pi_selected;
828 void RowPainter::paintText()
830 pos_type const end = row_.endpos();
831 // Spaces at logical line breaks in bidi text must be skipped during
832 // painting. However, they may appear visually in the middle
833 // of a row; they must be skipped, wherever they are...
834 // * logically "abc_[HEBREW_\nHEBREW]"
835 // * visually "abc_[_WERBEH\nWERBEH]"
836 pos_type skipped_sep_vpos = -1;
837 pos_type body_pos = par_.beginOfBody();
839 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
843 Layout const & layout = par_.layout();
845 Change change_running;
846 int change_last_x = 0;
848 // check for possible inline completion
849 DocIterator const & inlineCompletionPos = pi_.base.bv->inlineCompletionPos();
850 pos_type inlineCompletionVPos = -1;
851 if (inlineCompletionPos.inTexted()
852 && inlineCompletionPos.text() == &text_
853 && inlineCompletionPos.pit() == pit_
854 && inlineCompletionPos.pos() - 1 >= row_.pos()
855 && inlineCompletionPos.pos() - 1 < row_.endpos()) {
856 // draw logically behind the previous character
857 inlineCompletionVPos = bidi_.log2vis(inlineCompletionPos.pos() - 1);
860 // Use font span to speed things up, see below
864 // If the last logical character is a separator, don't paint it, unless
865 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
866 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
867 skipped_sep_vpos = bidi_.log2vis(end - 1);
869 for (pos_type vpos = row_.pos(); vpos < end; ) {
870 if (x_ > pi_.base.bv->workWidth())
873 // Skip the separator at the logical end of the row
874 if (vpos == skipped_sep_vpos) {
879 pos_type const pos = bidi_.vis2log(vpos);
881 if (pos >= par_.size()) {
886 // Use font span to speed things up, see above
887 if (vpos < font_span.first || vpos > font_span.last) {
888 font_span = par_.fontSpan(vpos);
889 font = text_metrics_.displayFont(pit_, vpos);
891 // split font span if inline completion is inside
892 if (font_span.first <= inlineCompletionVPos
893 && font_span.last > inlineCompletionVPos)
894 font_span.last = inlineCompletionVPos;
897 const int width_pos = pm_.singleWidth(pos, font);
899 if (x_ + width_pos < 0) {
904 Change const & change = par_.lookupChange(pos);
905 if (change.changed() && !change_running.changed()) {
906 change_running = change;
907 change_last_x = int(x_);
910 Inset const * inset = par_.getInset(pos);
911 bool const highly_editable_inset = inset
912 && inset->editable();
914 // If we reach the end of a change or if the author changes, paint it.
915 // We also don't paint across things like tables
916 if (change_running.changed() && (highly_editable_inset
917 || !change.changed() || !change_running.isSimilarTo(change))) {
918 // Calculate 1/3 height of the buffer's default font
919 FontMetrics const & fm
920 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
921 float const y_bar = change_running.deleted() ?
922 yo_ - fm.maxAscent() / 3 : yo_ + 2 * solid_line_offset_ + solid_line_thickness_;
923 pi_.pain.line(change_last_x, int(y_bar), int(x_), int(y_bar),
924 change_running.color(), Painter::line_solid, solid_line_thickness_);
926 // Change might continue with a different author or type
927 if (change.changed() && !highly_editable_inset) {
928 change_running = change;
929 change_last_x = int(x_);
931 change_running.setUnchanged();
934 if (body_pos > 0 && pos == body_pos - 1) {
935 int const lwidth = theFontMetrics(labelFont())
936 .width(layout.labelsep);
938 x_ += row_.label_hfill + lwidth - width_pos;
941 // Is the inline completion in front of character?
942 if (font.isRightToLeft() && vpos == inlineCompletionVPos)
943 paintInlineCompletion(font);
945 if (par_.isSeparator(pos)) {
946 Font const orig_font = text_metrics_.displayFont(pit_, pos);
947 double const orig_x = x_;
948 double separator_width = width_pos;
950 separator_width += row_.separator;
951 paintSeparator(orig_x, separator_width, orig_font.fontInfo());
952 paintForeignMark(orig_x, orig_font.language());
956 // If outer row has changed, nested insets are repaint completely.
957 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
959 bool const pi_selected = pi_.selected;
960 Cursor const & cur = pi_.base.bv->cursor();
961 if (cur.selection() && cur.text() == &text_
962 && cur.normalAnchor().text() == &text_)
963 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos;
964 paintInset(inset, pos);
965 pi_.selected = pi_selected;
969 // paint as many characters as possible.
970 paintFromPos(vpos, change_running.changed());
973 // Is the inline completion after character?
974 if (!font.isRightToLeft() && vpos - 1 == inlineCompletionVPos)
975 paintInlineCompletion(font);
978 // if we reach the end of a struck out range, paint it
979 if (change_running.changed()) {
980 FontMetrics const & fm
981 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
982 float const y_bar = change_running.deleted() ?
983 yo_ - fm.maxAscent() / 3 : yo_ + 2 * solid_line_offset_ + solid_line_thickness_;
984 pi_.pain.line(change_last_x, int(y_bar), int(x_), int(y_bar),
985 change_running.color(), Painter::line_solid, solid_line_thickness_);
986 change_running.setUnchanged();
991 void RowPainter::paintSelection()
993 if (!row_.selection())
995 Cursor const & curs = pi_.base.bv->cursor();
996 DocIterator beg = curs.selectionBegin();
998 beg.pos() = row_.sel_beg;
1000 DocIterator end = curs.selectionEnd();
1002 end.pos() = row_.sel_end;
1004 bool const begin_boundary = beg.pos() >= row_.endpos();
1005 bool const end_boundary = row_.sel_end == row_.endpos();
1007 DocIterator cur = beg;
1008 cur.boundary(begin_boundary);
1009 int x1 = text_metrics_.cursorX(beg.top(), begin_boundary);
1010 int x2 = text_metrics_.cursorX(end.top(), end_boundary);
1011 int const y1 = yo_ - row_.ascent();
1012 int const y2 = y1 + row_.height();
1014 int const rm = text_.isMainText() ? pi_.base.bv->rightMargin() : 0;
1015 int const lm = text_.isMainText() ? pi_.base.bv->leftMargin() : 0;
1018 if (row_.begin_margin_sel) {
1019 if (text_.isRTL(beg.paragraph())) {
1020 pi_.pain.fillRectangle(int(xo_ + x1), y1,
1021 text_metrics_.width() - rm - x1, y2 - y1, Color_selection);
1023 pi_.pain.fillRectangle(int(xo_ + lm), y1, x1 - lm, y2 - y1,
1028 if (row_.end_margin_sel) {
1029 if (text_.isRTL(beg.paragraph())) {
1030 pi_.pain.fillRectangle(int(xo_ + lm), y1, x2 - lm, y2 - y1,
1033 pi_.pain.fillRectangle(int(xo_ + x2), y1, text_metrics_.width() - rm - x2,
1034 y2 - y1, Color_selection);
1038 // if we are on a boundary from the beginning, it's probably
1039 // a RTL boundary and we jump to the other side directly as this
1040 // segement is 0-size and confuses the logic below
1042 cur.boundary(false);
1044 // go through row and draw from RTL boundary to RTL boundary
1046 bool draw_now = false;
1048 // simplified cursorForward code below which does not
1049 // descend into insets and which does not go into the
1050 // next line. Compare the logic with the original cursorForward
1052 // if left of boundary -> just jump to right side, but
1053 // for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
1054 if (cur.boundary()) {
1055 cur.boundary(false);
1056 } else if (text_metrics_.isRTLBoundary(cur.pit(), cur.pos() + 1)) {
1057 // in front of RTL boundary -> Stay on this side of the boundary
1058 // because: ab|cDDEEFFghi -> abc|DDEEFFghi
1067 if (cur.pos() == row_.endpos())
1072 // the previous segment was just drawn, now the next starts
1073 x1 = text_metrics_.cursorX(cur.top(), cur.boundary());
1076 if (!(cur < end) || draw_now) {
1077 x2 = text_metrics_.cursorX(cur.top(), cur.boundary());
1078 pi_.pain.fillRectangle(int(xo_ + min(x1, x2)), y1, abs(x2 - x1),
1079 y2 - y1, Color_selection);
1081 // reset x1, so it is set again next round (which will be on the
1082 // right side of a boundary or at the selection end)
1089 void RowPainter::paintInlineCompletion(Font const & font)
1091 docstring completion = pi_.base.bv->inlineCompletion();
1092 FontInfo f = font.fontInfo();
1093 bool rtl = font.isRightToLeft();
1095 // draw the unique and the non-unique completion part
1096 // Note: this is not time-critical as it is
1097 // only done once per screen.
1098 size_t uniqueTo = pi_.base.bv->inlineCompletionUniqueChars();
1099 docstring s1 = completion.substr(0, uniqueTo);
1100 docstring s2 = completion.substr(uniqueTo);
1101 ColorCode c1 = Color_inlinecompletion;
1102 ColorCode c2 = Color_nonunique_inlinecompletion;
1110 if (s1.size() > 0) {
1112 pi_.pain.text(int(x_), yo_, s1, f);
1113 x_ += theFontMetrics(font).width(s1);
1116 if (s2.size() > 0) {
1118 pi_.pain.text(int(x_), yo_, s2, f);
1119 x_ += theFontMetrics(font).width(s2);