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 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;
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::paintHebrewComposeChar(pos_type & vpos, FontInfo const & font)
165 pos_type pos = bidi_.vis2log(vpos);
170 char_type c = par_.getChar(pos);
174 int const width = theFontMetrics(font).width(c);
177 for (pos_type i = pos - 1; i >= 0; --i) {
179 if (!Encodings::isHebrewComposeChar(c)) {
180 if (isPrintableNonspace(c)) {
181 int const width2 = pm_.singleWidth(i,
182 text_metrics_.displayFont(pit_, i));
183 dx = (c == 0x05e8 || // resh
184 c == 0x05d3) // dalet
186 : (width2 - width) / 2;
193 pi_.pain.text(int(x_) + dx, yo_, str, font);
197 void RowPainter::paintArabicComposeChar(pos_type & vpos, FontInfo const & font)
199 pos_type pos = bidi_.vis2log(vpos);
203 char_type c = par_.getChar(pos);
204 c = par_.transformChar(c, pos);
208 int const width = theFontMetrics(font).width(c);
211 for (pos_type i = pos - 1; i >= 0; --i) {
213 if (!Encodings::isArabicComposeChar(c)) {
214 if (isPrintableNonspace(c)) {
215 int const width2 = pm_.singleWidth(i,
216 text_metrics_.displayFont(pit_, i));
217 dx = (width2 - width) / 2;
223 pi_.pain.text(int(x_) + dx, yo_, str, font);
227 void RowPainter::paintChars(pos_type & vpos, FontInfo const & font,
228 bool hebrew, bool arabic)
230 // This method takes up 70% of time when typing
231 pos_type pos = bidi_.vis2log(vpos);
233 char_type prev_char = par_.getChar(pos);
234 vector<char_type> str;
236 str.push_back(prev_char);
239 char_type c = str[0];
244 str[0] = par_.transformChar(c, pos);
247 pos_type const end = row_.endpos();
248 FontSpan const font_span = par_.fontSpan(pos);
249 // Track-change status.
250 Change const & change_running = par_.lookupChange(pos);
253 bool const selection = (pos >= row_.sel_beg && pos < row_.sel_end)
257 bool const spell_state =
258 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
260 // collect as much similar chars as we can
261 for (++vpos ; vpos < end ; ++vpos) {
262 // Work-around bug #6920
263 // The bug can be reproduced with DejaVu font under Linux.
264 // The issue is that we compute the metrics character by character
265 // in ParagraphMetrics::singleWidth(); but we paint word by word
266 // for performance reason.
267 // Maybe a more general fix would be draw character by character
268 // for some predefined fonts on some platform. In arabic and
269 // Hebrew we already do paint this way.
270 if (prev_char == 'f' || lyxrc.force_paint_single_char)
273 pos = bidi_.vis2log(vpos);
274 if (pos < font_span.first || pos > font_span.last)
277 bool const new_selection = pos >= row_.sel_beg && pos < row_.sel_end;
278 if (new_selection != selection)
279 // Selection ends or starts here.
282 bool const new_spell_state =
283 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
284 if (new_spell_state != spell_state)
285 // Spell checker state changed here.
288 Change const & change = par_.lookupChange(pos);
289 if (!change_running.isSimilarTo(change))
290 // Track change type or author has changed.
293 char_type c = par_.getChar(pos);
298 if (!isPrintableNonspace(c))
301 /* Because we do our own bidi, at this point the strings are
302 * already in visual order. However, Qt also applies its own
303 * bidi algorithm to strings that it paints to the screen.
304 * Therefore, if we were to paint Hebrew/Arabic words as a
305 * single string, the letters in the words would get reversed
306 * again. In order to avoid that, we don't collect Hebrew/
307 * Arabic characters, but rather paint them one at a time.
308 * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
313 /* FIXME: these checks are irrelevant, since 'arabic' and
314 * 'hebrew' alone are already going to trigger a break.
315 * However, this should not be removed completely, because
316 * if an alternative solution is found which allows grouping
317 * of arabic and hebrew characters, then these breaks may have
320 if (arabic && Encodings::isArabicComposeChar(c))
323 if (hebrew && Encodings::isHebrewComposeChar(c))
332 c = par_.transformChar(c, pos);
333 /* see comment in hebrew, explaining why we break */
341 docstring s(&str[0], str.size());
344 s.replace(0,1,from_ascii(" "));
346 if (!selection && !change_running.changed()) {
347 x_ += pi_.pain.text(int(x_), yo_, s, font);
351 FontInfo copy = font;
352 if (change_running.changed())
353 copy.setPaintColor(change_running.color());
355 copy.setPaintColor(Color_selectiontext);
357 x_ += pi_.pain.text(int(x_), yo_, s, copy);
361 void RowPainter::paintForeignMark(double orig_x, Language const * lang,
364 if (!lyxrc.mark_foreign_language)
366 if (lang == latex_language)
368 if (lang == pi_.base.bv->buffer().params().language)
371 int const y = yo_ + solid_line_offset_ + desc + int(solid_line_thickness_/2);
372 pi_.pain.line(int(orig_x), y, int(x_), y, Color_language,
373 Painter::line_solid, solid_line_thickness_);
377 void RowPainter::paintMisspelledMark(double orig_x, bool changed)
379 // if changed the misspelled marker gets placed slightly lower than normal
380 // to avoid drawing at the same vertical offset
381 float const y = yo_ + solid_line_offset_ + solid_line_thickness_
382 + (changed ? solid_line_thickness_ + 1 : 0)
383 + dotted_line_offset_;
384 pi_.pain.line(int(orig_x), int(y), int(x_), int(y), Color_error,
385 Painter::line_onoffdash, dotted_line_thickness_);
389 void RowPainter::paintFromPos(pos_type & vpos, bool changed)
391 pos_type const pos = bidi_.vis2log(vpos);
392 Font const orig_font = text_metrics_.displayFont(pit_, pos);
393 double const orig_x = x_;
395 // usual characters, no insets
396 char_type const c = par_.getChar(pos);
398 // special case languages
399 string const & lang = orig_font.language()->lang();
400 bool const hebrew = lang == "hebrew";
401 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" ||
405 bool const misspelled =
406 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
408 // draw as many chars as we can
409 if ((!hebrew && !arabic)
410 || (hebrew && !Encodings::isHebrewComposeChar(c))
411 || (arabic && !Encodings::isArabicComposeChar(c))) {
412 paintChars(vpos, orig_font.fontInfo(), hebrew, arabic);
414 paintHebrewComposeChar(vpos, orig_font.fontInfo());
416 paintArabicComposeChar(vpos, orig_font.fontInfo());
419 paintForeignMark(orig_x, orig_font.language());
421 if (lyxrc.spellcheck_continuously && misspelled) {
422 // check for cursor position
423 // don't draw misspelled marker for words at cursor position
424 // we don't want to disturb the process of text editing
425 BufferView const * bv = pi_.base.bv;
426 DocIterator const nw = bv->cursor().newWord();
427 bool new_word = false;
428 if (!nw.empty() && par_.id() == nw.paragraph().id()) {
429 pos_type cpos = nw.pos();
430 if (cpos > 0 && cpos == par_.size() && !par_.isWordSeparator(cpos-1))
432 else if (cpos > 0 && par_.isWordSeparator(cpos))
434 new_word = par_.isSameSpellRange(pos, cpos) ;
437 paintMisspelledMark(orig_x, changed);
442 void RowPainter::paintChangeBar()
444 pos_type const start = row_.pos();
445 pos_type end = row_.endpos();
447 if (par_.size() == end) {
448 // this is the last row of the paragraph;
449 // thus, we must also consider the imaginary end-of-par character
453 if (start == end || !par_.isChanged(start, end))
456 int const height = text_metrics_.isLastRow(pit_, row_)
460 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
464 void RowPainter::paintAppendix()
466 // only draw the appendix frame once (for the main text)
467 if (!par_.params().appendix() || !text_.isMainText())
470 int y = yo_ - row_.ascent();
472 if (par_.params().startOfAppendix())
473 y += 2 * defaultRowHeight();
475 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
476 pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
480 void RowPainter::paintDepthBar()
482 depth_type const depth = par_.getDepth();
487 depth_type prev_depth = 0;
488 if (!text_metrics_.isFirstRow(pit_, row_)) {
489 pit_type pit2 = pit_;
492 prev_depth = pars_[pit2].getDepth();
495 depth_type next_depth = 0;
496 if (!text_metrics_.isLastRow(pit_, row_)) {
497 pit_type pit2 = pit_;
498 if (row_.endpos() >= pars_[pit2].size())
500 next_depth = pars_[pit2].getDepth();
503 for (depth_type i = 1; i <= depth; ++i) {
504 int const w = nestMargin() / 5;
505 int x = int(xo_) + w * i;
506 // only consider the changebar space if we're drawing outermost text
507 if (text_.isMainText())
508 x += changebarMargin();
510 int const starty = yo_ - row_.ascent();
511 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
513 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
516 pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
518 pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
523 int RowPainter::paintAppendixStart(int y)
525 FontInfo pb_font = sane_font;
526 pb_font.setColor(Color_appendix);
533 docstring const label = _("Appendix");
534 theFontMetrics(pb_font).rectText(label, w, a, d);
536 int const text_start = int(xo_ + (width_ - w) / 2);
537 int const text_end = text_start + w;
539 pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
541 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
542 pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
544 return 3 * defaultRowHeight();
548 void RowPainter::paintFirst()
550 ParagraphParameters const & pparams = par_.params();
551 Buffer const & buffer = pi_.base.bv->buffer();
552 BufferParams const & bparams = buffer.params();
553 Layout const & layout = par_.layout();
557 // start of appendix?
558 if (pparams.startOfAppendix())
559 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
561 if (bparams.paragraph_separation == BufferParams::ParagraphSkipSeparation
563 if (layout.latextype == LATEX_PARAGRAPH
564 && !par_.getDepth()) {
565 y_top += bparams.getDefSkip().inPixels(*pi_.base.bv);
567 Layout const & playout = pars_[pit_ - 1].layout();
568 if (playout.latextype == LATEX_PARAGRAPH
569 && !pars_[pit_ - 1].getDepth()) {
570 // is it right to use defskip here, too? (AS)
571 y_top += bparams.getDefSkip().inPixels(*pi_.base.bv);
576 bool const is_rtl = text_.isRTL(par_);
577 bool const is_seq = text_.isFirstInSequence(pit_);
578 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
580 // should we print a label?
581 if (layout.labeltype >= LABEL_STATIC
582 && (layout.labeltype != LABEL_STATIC
583 || layout.latextype != LATEX_ENVIRONMENT
586 FontInfo const font = labelFont();
587 FontMetrics const & fm = theFontMetrics(font);
589 docstring const str = par_.labelString();
593 // this is special code for the chapter layout. This is
594 // printed in an extra row and has a pagebreak at
596 if (layout.counter == "chapter") {
597 double spacing_val = 1.0;
598 if (!pparams.spacing().isDefault()) {
599 spacing_val = pparams.spacing().getValue();
601 spacing_val = bparams.spacing().getValue();
604 int const labeladdon = int(fm.maxHeight() * layout.spacing.getValue() * spacing_val);
606 int const maxdesc = int(fm.maxDescent() * layout.spacing.getValue() * spacing_val)
607 + int(layout.parsep) * defaultRowHeight();
610 x = width_ - leftMargin() -
614 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
617 x = width_ - leftMargin()
618 + fm.width(layout.labelsep);
620 x = x_ - fm.width(layout.labelsep)
624 pi_.pain.text(int(x), yo_, str, font);
628 // the labels at the top of an environment.
629 // More or less for bibliography
631 (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
632 layout.labeltype == LABEL_BIBLIO ||
633 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
634 FontInfo const font = labelFont();
635 docstring const str = par_.labelString();
637 double spacing_val = 1.0;
638 if (!pparams.spacing().isDefault())
639 spacing_val = pparams.spacing().getValue();
641 spacing_val = bparams.spacing().getValue();
643 FontMetrics const & fm = theFontMetrics(font);
645 int const labeladdon = int(fm.maxHeight()
646 * layout.spacing.getValue() * spacing_val);
649 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
650 + (layout.labelbottomsep * defaultRowHeight()));
653 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
656 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
657 x -= fm.width(str) / 2;
659 x = width_ - leftMargin() - fm.width(str);
661 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
667 /** Check if the current paragraph is the last paragraph in a
669 static int getEndLabel(pit_type p, Text const & text)
671 ParagraphList const & pars = text.paragraphs();
673 depth_type par_depth = pars[p].getDepth();
674 while (pit != pit_type(pars.size())) {
675 Layout const & layout = pars[pit].layout();
676 int const endlabeltype = layout.endlabeltype;
678 if (endlabeltype != END_LABEL_NO_LABEL) {
679 if (p + 1 == pit_type(pars.size()))
682 depth_type const next_depth =
683 pars[p + 1].getDepth();
684 if (par_depth > next_depth ||
685 (par_depth == next_depth && layout != pars[p + 1].layout()))
691 pit = text.outerHook(pit);
692 if (pit != pit_type(pars.size()))
693 par_depth = pars[pit].getDepth();
695 return END_LABEL_NO_LABEL;
699 void RowPainter::paintLast()
701 bool const is_rtl = text_.isRTL(par_);
702 int const endlabel = getEndLabel(pit_, text_);
704 // paint imaginary end-of-paragraph character
706 Change const & change = par_.lookupChange(par_.size());
707 if (change.changed()) {
708 FontMetrics const & fm =
709 theFontMetrics(pi_.base.bv->buffer().params().getFont());
710 int const length = fm.maxAscent() / 2;
711 Color col = change.color();
713 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
714 Painter::line_solid, 3);
716 if (change.deleted()) {
717 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
718 yo_ + 2, col, Painter::line_solid, 3);
720 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
721 yo_ + 2, col, Painter::line_solid, 3);
729 case END_LABEL_FILLED_BOX: {
730 FontInfo const font = labelFont();
731 FontMetrics const & fm = theFontMetrics(font);
732 int const size = int(0.75 * fm.maxAscent());
733 int const y = yo_ - size;
734 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
735 int x = is_rtl ? nestMargin() + changebarMargin()
736 : max_row_width - text_metrics_.rightMargin(pm_);
738 // If needed, move the box a bit to avoid overlapping with text.
739 int const rem = max_row_width - row_.width();
741 x += is_rtl ? rem : - rem;
743 if (endlabel == END_LABEL_BOX)
744 pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
746 pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
750 case END_LABEL_STATIC: {
751 FontInfo const font = labelFont();
752 FontMetrics const & fm = theFontMetrics(font);
753 docstring const & str = par_.layout().endlabelstring();
754 double const x = is_rtl ? x_ - fm.width(str) : x_;
755 pi_.pain.text(int(x), yo_, str, font);
759 case END_LABEL_NO_LABEL:
760 if (lyxrc.paragraph_markers && size_type(pit_ + 1) < pars_.size()) {
761 docstring const s = docstring(1, char_type(0x00B6));
762 FontInfo f = FontInfo();
763 FontMetrics const & fm = theFontMetrics(f);
764 f.setColor(Color_paragraphmarker);
765 pi_.pain.text(int(x_), yo_, s, f);
773 void RowPainter::paintOnlyInsets()
775 CoordCache const & cache = pi_.base.bv->coordCache();
776 pos_type const end = row_.endpos();
777 for (pos_type pos = row_.pos(); pos != end; ++pos) {
778 // If outer row has changed, nested insets are repaint completely.
779 Inset const * inset = par_.getInset(pos);
780 bool const nested_inset = inset &&
781 ((inset->asInsetMath() &&
782 !inset->asInsetMath()->asMacroTemplate())
783 || inset->asInsetText()
784 || inset->asInsetTabular());
787 if (x_ > pi_.base.bv->workWidth()
788 || !cache.getInsets().has(inset))
790 x_ = cache.getInsets().x(inset);
792 bool const pi_selected = pi_.selected;
793 Cursor const & cur = pi_.base.bv->cursor();
794 if (cur.selection() && cur.text() == &text_
795 && cur.normalAnchor().text() == &text_)
796 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos;
797 paintInset(inset, pos);
798 pi_.selected = pi_selected;
803 void RowPainter::paintText()
805 pos_type const end = row_.endpos();
806 // Spaces at logical line breaks in bidi text must be skipped during
807 // painting. However, they may appear visually in the middle
808 // of a row; they must be skipped, wherever they are...
809 // * logically "abc_[HEBREW_\nHEBREW]"
810 // * visually "abc_[_WERBEH\nWERBEH]"
811 pos_type skipped_sep_vpos = -1;
812 pos_type body_pos = par_.beginOfBody();
814 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
818 Layout const & layout = par_.layout();
820 Change change_running;
821 int change_last_x = 0;
823 // check for possible inline completion
824 DocIterator const & inlineCompletionPos = pi_.base.bv->inlineCompletionPos();
825 pos_type inlineCompletionVPos = -1;
826 if (inlineCompletionPos.inTexted()
827 && inlineCompletionPos.text() == &text_
828 && inlineCompletionPos.pit() == pit_
829 && inlineCompletionPos.pos() - 1 >= row_.pos()
830 && inlineCompletionPos.pos() - 1 < row_.endpos()) {
831 // draw logically behind the previous character
832 inlineCompletionVPos = bidi_.log2vis(inlineCompletionPos.pos() - 1);
835 // Use font span to speed things up, see below
839 // If the last logical character is a separator, don't paint it, unless
840 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
841 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
842 skipped_sep_vpos = bidi_.log2vis(end - 1);
844 for (pos_type vpos = row_.pos(); vpos < end; ) {
845 if (x_ > pi_.base.bv->workWidth())
848 // Skip the separator at the logical end of the row
849 if (vpos == skipped_sep_vpos) {
854 pos_type const pos = bidi_.vis2log(vpos);
856 if (pos >= par_.size()) {
861 // Use font span to speed things up, see above
862 if (vpos < font_span.first || vpos > font_span.last) {
863 font_span = par_.fontSpan(vpos);
864 font = text_metrics_.displayFont(pit_, vpos);
866 // split font span if inline completion is inside
867 if (font_span.first <= inlineCompletionVPos
868 && font_span.last > inlineCompletionVPos)
869 font_span.last = inlineCompletionVPos;
872 const int width_pos = pm_.singleWidth(pos, font);
874 if (x_ + width_pos < 0) {
879 Change const & change = par_.lookupChange(pos);
880 if (change.changed() && !change_running.changed()) {
881 change_running = change;
882 change_last_x = int(x_);
885 Inset const * inset = par_.getInset(pos);
886 bool const highly_editable_inset = inset
887 && inset->editable();
889 // If we reach the end of a change or if the author changes, paint it.
890 // We also don't paint across things like tables
891 if (change_running.changed() && (highly_editable_inset
892 || !change.changed() || !change_running.isSimilarTo(change))) {
893 // Calculate 1/3 height of the buffer's default font
894 FontMetrics const & fm
895 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
896 float const y_bar = change_running.deleted() ?
897 yo_ - fm.maxAscent() / 3 : yo_ + 2 * solid_line_offset_ + solid_line_thickness_;
898 pi_.pain.line(change_last_x, int(y_bar), int(x_), int(y_bar),
899 change_running.color(), Painter::line_solid, solid_line_thickness_);
901 // Change might continue with a different author or type
902 if (change.changed() && !highly_editable_inset) {
903 change_running = change;
904 change_last_x = int(x_);
906 change_running.setUnchanged();
909 if (body_pos > 0 && pos == body_pos - 1) {
910 int const lwidth = theFontMetrics(labelFont())
911 .width(layout.labelsep);
913 x_ += row_.label_hfill + lwidth - width_pos;
916 // Is the inline completion in front of character?
917 if (font.isRightToLeft() && vpos == inlineCompletionVPos)
918 paintInlineCompletion(font);
920 if (par_.isSeparator(pos)) {
921 Font const orig_font = text_metrics_.displayFont(pit_, pos);
922 double const orig_x = x_;
925 x_ += row_.separator;
926 paintForeignMark(orig_x, orig_font.language());
930 // If outer row has changed, nested insets are repaint completely.
931 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
933 bool const pi_selected = pi_.selected;
934 Cursor const & cur = pi_.base.bv->cursor();
935 if (cur.selection() && cur.text() == &text_
936 && cur.normalAnchor().text() == &text_)
937 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos;
938 paintInset(inset, pos);
939 pi_.selected = pi_selected;
943 // paint as many characters as possible.
944 paintFromPos(vpos, change_running.changed());
947 // Is the inline completion after character?
948 if (!font.isRightToLeft() && vpos - 1 == inlineCompletionVPos)
949 paintInlineCompletion(font);
952 // if we reach the end of a struck out range, paint it
953 if (change_running.changed()) {
954 FontMetrics const & fm
955 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
956 float const y_bar = change_running.deleted() ?
957 yo_ - fm.maxAscent() / 3 : yo_ + 2 * solid_line_offset_ + solid_line_thickness_;
958 pi_.pain.line(change_last_x, int(y_bar), int(x_), int(y_bar),
959 change_running.color(), Painter::line_solid, solid_line_thickness_);
960 change_running.setUnchanged();
965 void RowPainter::paintSelection()
967 if (!row_.selection())
969 Cursor const & curs = pi_.base.bv->cursor();
970 DocIterator beg = curs.selectionBegin();
972 beg.pos() = row_.sel_beg;
974 DocIterator end = curs.selectionEnd();
976 end.pos() = row_.sel_end;
978 bool const begin_boundary = beg.pos() >= row_.endpos();
979 bool const end_boundary = row_.sel_end == row_.endpos();
981 DocIterator cur = beg;
982 cur.boundary(begin_boundary);
983 int x1 = text_metrics_.cursorX(beg.top(), begin_boundary);
984 int x2 = text_metrics_.cursorX(end.top(), end_boundary);
985 int const y1 = yo_ - row_.ascent();
986 int const y2 = y1 + row_.height();
988 int const rm = text_.isMainText() ? pi_.base.bv->rightMargin() : 0;
989 int const lm = text_.isMainText() ? pi_.base.bv->leftMargin() : 0;
992 if (row_.begin_margin_sel) {
993 if (text_.isRTL(beg.paragraph())) {
994 pi_.pain.fillRectangle(int(xo_ + x1), y1,
995 text_metrics_.width() - rm - x1, y2 - y1, Color_selection);
997 pi_.pain.fillRectangle(int(xo_ + lm), y1, x1 - lm, y2 - y1,
1002 if (row_.end_margin_sel) {
1003 if (text_.isRTL(beg.paragraph())) {
1004 pi_.pain.fillRectangle(int(xo_ + lm), y1, x2 - lm, y2 - y1,
1007 pi_.pain.fillRectangle(int(xo_ + x2), y1, text_metrics_.width() - rm - x2,
1008 y2 - y1, Color_selection);
1012 // if we are on a boundary from the beginning, it's probably
1013 // a RTL boundary and we jump to the other side directly as this
1014 // segement is 0-size and confuses the logic below
1016 cur.boundary(false);
1018 // go through row and draw from RTL boundary to RTL boundary
1020 bool draw_now = false;
1022 // simplified cursorForward code below which does not
1023 // descend into insets and which does not go into the
1024 // next line. Compare the logic with the original cursorForward
1026 // if left of boundary -> just jump to right side, but
1027 // for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
1028 if (cur.boundary()) {
1029 cur.boundary(false);
1030 } else if (text_metrics_.isRTLBoundary(cur.pit(), cur.pos() + 1)) {
1031 // in front of RTL boundary -> Stay on this side of the boundary
1032 // because: ab|cDDEEFFghi -> abc|DDEEFFghi
1041 if (cur.pos() == row_.endpos())
1046 // the previous segment was just drawn, now the next starts
1047 x1 = text_metrics_.cursorX(cur.top(), cur.boundary());
1050 if (!(cur < end) || draw_now) {
1051 x2 = text_metrics_.cursorX(cur.top(), cur.boundary());
1052 pi_.pain.fillRectangle(int(xo_ + min(x1, x2)), y1, abs(x2 - x1),
1053 y2 - y1, Color_selection);
1055 // reset x1, so it is set again next round (which will be on the
1056 // right side of a boundary or at the selection end)
1063 void RowPainter::paintInlineCompletion(Font const & font)
1065 docstring completion = pi_.base.bv->inlineCompletion();
1066 FontInfo f = font.fontInfo();
1067 bool rtl = font.isRightToLeft();
1069 // draw the unique and the non-unique completion part
1070 // Note: this is not time-critical as it is
1071 // only done once per screen.
1072 size_t uniqueTo = pi_.base.bv->inlineCompletionUniqueChars();
1073 docstring s1 = completion.substr(0, uniqueTo);
1074 docstring s2 = completion.substr(uniqueTo);
1075 ColorCode c1 = Color_inlinecompletion;
1076 ColorCode c2 = Color_nonunique_inlinecompletion;
1084 if (s1.size() > 0) {
1086 pi_.pain.text(int(x_), yo_, s1, f);
1087 x_ += theFontMetrics(font).width(s1);
1090 if (s2.size() > 0) {
1092 pi_.pain.text(int(x_), yo_, s2, f);
1093 x_ += theFontMetrics(font).width(s2);