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;
58 RowPainter::RowPainter(PainterInfo & pi,
59 Text const & text, pit_type pit, Row const & row, Bidi & bidi, int x, int y)
60 : pi_(pi), text_(text),
61 text_metrics_(pi_.base.bv->textMetrics(&text)),
62 pars_(text.paragraphs()),
63 row_(row), pit_(pit), par_(text.paragraphs()[pit]),
64 pm_(text_metrics_.parMetrics(pit)),
65 bidi_(bidi), change_(pi_.change_),
66 xo_(x), yo_(y), width_(text_metrics_.width()),
67 solid_line_thickness_(1.0), solid_line_offset_(1),
68 dotted_line_thickness_(1.0), dotted_line_offset_(2)
70 bidi_.computeTables(par_, pi_.base.bv->buffer(), row_);
72 if (lyxrc.zoom >= 200) {
73 // derive the line thickness from zoom factor
74 // the zoom is given in percent
75 // (increase thickness at 250%, 450% etc.)
76 solid_line_thickness_ = (float)(int((lyxrc.zoom + 50) / 200.0));
77 // adjust line_offset_ too
78 solid_line_offset_ = 1 + int(0.5 * solid_line_thickness_);
80 if (lyxrc.zoom >= 100) {
81 // derive the line thickness from zoom factor
82 // the zoom is given in percent
83 // (increase thickness at 150%, 250% etc.)
84 dotted_line_thickness_ = (float)(int((lyxrc.zoom + 50) / 100.0));
85 // adjust line_offset_ too
86 dotted_line_offset_ = int(0.5 * dotted_line_thickness_) + 1;
91 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
94 LASSERT(pit >= 0, /**/);
95 LASSERT(pit < int(text.paragraphs().size()), /**/);
99 FontInfo RowPainter::labelFont() const
101 FontInfo f = text_.labelFont(par_);
103 if (row_.begin_margin_sel || pi_.selected)
104 f.setPaintColor(Color_selectiontext);
109 int RowPainter::leftMargin() const
111 return text_metrics_.leftMargin(text_metrics_.width(), pit_,
115 // If you want to debug inset metrics uncomment the following line:
116 //#define DEBUG_METRICS
117 // This draws green lines around each inset.
120 void RowPainter::paintInset(Inset const * inset, pos_type const pos)
122 Font const font = text_metrics_.displayFont(pit_, pos);
124 LASSERT(inset, return);
125 // Backup full_repaint status because some insets (InsetTabular)
126 // requires a full repaint
127 bool pi_full_repaint = pi_.full_repaint;
129 pi_.base.font = inset->inheritFont() ? font.fontInfo() :
130 pi_.base.bv->buffer().params().getFont().fontInfo();
131 pi_.ltr_pos = (bidi_.level(pos) % 2 == 0);
132 Change prev_change = change_;
133 pi_.change_ = change_.changed() ? change_ : par_.lookupChange(pos);
135 int const x1 = int(x_);
136 pi_.base.bv->coordCache().insets().add(inset, x1, yo_);
137 // insets are painted completely. Recursive
138 // FIXME: it is wrong to completely paint the background
139 // if we want to do single row painting.
140 inset->drawBackground(pi_, x1, yo_);
141 inset->drawSelection(pi_, x1, yo_);
142 inset->draw(pi_, x1, yo_);
144 Dimension const & dim = pm_.insetDimension(inset);
146 paintForeignMark(x_, font.language(), dim.descent());
150 // Restore full_repaint status.
151 pi_.full_repaint = pi_full_repaint;
152 pi_.change_ = prev_change;
155 int const x2 = x1 + dim.wid;
156 int const y1 = yo_ + dim.des;
157 int const y2 = yo_ - dim.asc;
158 pi_.pain.line(x1, y1, x1, y2, Color_green);
159 pi_.pain.line(x1, y1, x2, y1, Color_green);
160 pi_.pain.line(x2, y1, x2, y2, Color_green);
161 pi_.pain.line(x1, y2, x2, y2, Color_green);
166 void RowPainter::paintHebrewComposeChar(pos_type & vpos, FontInfo const & font)
168 pos_type pos = bidi_.vis2log(vpos);
173 char_type c = par_.getChar(pos);
177 int const width = theFontMetrics(font).width(c);
180 for (pos_type i = pos - 1; i >= 0; --i) {
182 if (!Encodings::isHebrewComposeChar(c)) {
183 if (isPrintableNonspace(c)) {
184 int const width2 = pm_.singleWidth(i,
185 text_metrics_.displayFont(pit_, i));
186 dx = (c == 0x05e8 || // resh
187 c == 0x05d3) // dalet
189 : (width2 - width) / 2;
196 pi_.pain.text(int(x_) + dx, yo_, str, font);
200 void RowPainter::paintArabicComposeChar(pos_type & vpos, FontInfo const & font)
202 pos_type pos = bidi_.vis2log(vpos);
206 char_type c = par_.getChar(pos);
207 c = par_.transformChar(c, pos);
211 int const width = theFontMetrics(font).width(c);
214 for (pos_type i = pos - 1; i >= 0; --i) {
216 if (!Encodings::isArabicComposeChar(c)) {
217 if (isPrintableNonspace(c)) {
218 int const width2 = pm_.singleWidth(i,
219 text_metrics_.displayFont(pit_, i));
220 dx = (width2 - width) / 2;
226 pi_.pain.text(int(x_) + dx, yo_, str, font);
230 void RowPainter::paintChars(pos_type & vpos, FontInfo const & font,
231 bool hebrew, bool arabic)
233 // This method takes up 70% of time when typing
234 pos_type pos = bidi_.vis2log(vpos);
236 char_type prev_char = par_.getChar(pos);
237 vector<char_type> str;
239 str.push_back(prev_char);
241 // FIXME: Why only round brackets and why the difference to
242 // Hebrew? See also Paragraph::getUChar
244 char_type c = str[0];
249 str[0] = par_.transformChar(c, pos);
252 pos_type const end = row_.endpos();
253 FontSpan const font_span = par_.fontSpan(pos);
254 // Track-change status.
255 Change const & change_running = par_.lookupChange(pos);
258 bool const selection = (pos >= row_.sel_beg && pos < row_.sel_end)
262 bool const spell_state =
263 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
265 // collect as much similar chars as we can
266 for (++vpos ; vpos < end ; ++vpos) {
267 // Work-around bug #6920
268 // The bug can be reproduced with DejaVu font under Linux.
269 // The issue is that we compute the metrics character by character
270 // in ParagraphMetrics::singleWidth(); but we paint word by word
271 // for performance reason.
272 // Maybe a more general fix would be draw character by character
273 // for some predefined fonts on some platform. In arabic and
274 // Hebrew we already do paint this way.
275 if (prev_char == 'f' || lyxrc.force_paint_single_char)
278 pos = bidi_.vis2log(vpos);
279 if (pos < font_span.first || pos > font_span.last)
282 bool const new_selection = pos >= row_.sel_beg && pos < row_.sel_end;
283 if (new_selection != selection)
284 // Selection ends or starts here.
287 bool const new_spell_state =
288 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
289 if (new_spell_state != spell_state)
290 // Spell checker state changed here.
293 Change const & change = par_.lookupChange(pos);
294 if (!change_running.isSimilarTo(change))
295 // Track change type or author has changed.
298 char_type c = par_.getChar(pos);
303 if (!isPrintableNonspace(c))
306 /* Because we do our own bidi, at this point the strings are
307 * already in visual order. However, Qt also applies its own
308 * bidi algorithm to strings that it paints to the screen.
309 * Therefore, if we were to paint Hebrew/Arabic words as a
310 * single string, the letters in the words would get reversed
311 * again. In order to avoid that, we don't collect Hebrew/
312 * Arabic characters, but rather paint them one at a time.
313 * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
318 /* FIXME: these checks are irrelevant, since 'arabic' and
319 * 'hebrew' alone are already going to trigger a break.
320 * However, this should not be removed completely, because
321 * if an alternative solution is found which allows grouping
322 * of arabic and hebrew characters, then these breaks may have
325 if (arabic && Encodings::isArabicComposeChar(c))
328 if (hebrew && Encodings::isHebrewComposeChar(c))
332 // FIXME: Why only round brackets and why the difference to
333 // Hebrew? See also Paragraph::getUChar
339 c = par_.transformChar(c, pos);
340 /* see comment in hebrew, explaining why we break */
348 docstring s(&str[0], str.size());
351 s.replace(0,1,from_ascii(" "));
353 if (!selection && !change_running.changed()) {
354 x_ += pi_.pain.text(int(x_), yo_, s, font);
358 FontInfo copy = font;
359 if (change_running.changed())
360 copy.setPaintColor(change_running.color());
362 copy.setPaintColor(Color_selectiontext);
364 x_ += pi_.pain.text(int(x_), yo_, s, copy);
368 void RowPainter::paintSeparator(double orig_x, double width,
369 FontInfo const & font)
371 pi_.pain.textDecoration(font, int(orig_x), yo_, int(width));
376 void RowPainter::paintForeignMark(double orig_x, Language const * lang,
379 if (!lyxrc.mark_foreign_language)
381 if (lang == latex_language)
383 if (lang == pi_.base.bv->buffer().params().language)
386 int const y = yo_ + solid_line_offset_ + desc + int(solid_line_thickness_/2);
387 pi_.pain.line(int(orig_x), y, int(x_), y, Color_language,
388 Painter::line_solid, solid_line_thickness_);
392 void RowPainter::paintMisspelledMark(double orig_x, bool changed)
394 // if changed the misspelled marker gets placed slightly lower than normal
395 // to avoid drawing at the same vertical offset
396 float const y = yo_ + solid_line_offset_ + solid_line_thickness_
397 + (changed ? solid_line_thickness_ + 1 : 0)
398 + dotted_line_offset_;
399 pi_.pain.line(int(orig_x), int(y), int(x_), int(y), Color_error,
400 Painter::line_onoffdash, dotted_line_thickness_);
404 void RowPainter::paintFromPos(pos_type & vpos, bool changed)
406 pos_type const pos = bidi_.vis2log(vpos);
407 Font const orig_font = text_metrics_.displayFont(pit_, pos);
408 double const orig_x = x_;
410 // usual characters, no insets
411 char_type const c = par_.getChar(pos);
413 // special case languages
414 string const & lang = orig_font.language()->lang();
415 bool const hebrew = lang == "hebrew";
416 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" ||
420 bool const misspelled =
421 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
423 // draw as many chars as we can
424 if ((!hebrew && !arabic)
425 || (hebrew && !Encodings::isHebrewComposeChar(c))
426 || (arabic && !Encodings::isArabicComposeChar(c))) {
427 paintChars(vpos, orig_font.fontInfo(), hebrew, arabic);
429 paintHebrewComposeChar(vpos, orig_font.fontInfo());
431 paintArabicComposeChar(vpos, orig_font.fontInfo());
434 paintForeignMark(orig_x, orig_font.language());
436 if (lyxrc.spellcheck_continuously && misspelled) {
437 // check for cursor position
438 // don't draw misspelled marker for words at cursor position
439 // we don't want to disturb the process of text editing
440 BufferView const * bv = pi_.base.bv;
441 DocIterator const nw = bv->cursor().newWord();
442 bool new_word = false;
443 if (!nw.empty() && par_.id() == nw.paragraph().id()) {
444 pos_type cpos = nw.pos();
445 if (cpos > 0 && cpos == par_.size() && !par_.isWordSeparator(cpos-1))
447 else if (cpos > 0 && par_.isWordSeparator(cpos))
449 new_word = par_.isSameSpellRange(pos, cpos) ;
452 paintMisspelledMark(orig_x, changed);
457 void RowPainter::paintChangeBar()
459 pos_type const start = row_.pos();
460 pos_type end = row_.endpos();
462 if (par_.size() == end) {
463 // this is the last row of the paragraph;
464 // thus, we must also consider the imaginary end-of-par character
468 if (start == end || !par_.isChanged(start, end))
471 int const height = text_metrics_.isLastRow(pit_, row_)
475 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
479 void RowPainter::paintAppendix()
481 // only draw the appendix frame once (for the main text)
482 if (!par_.params().appendix() || !text_.isMainText())
485 int y = yo_ - row_.ascent();
487 if (par_.params().startOfAppendix())
488 y += 2 * defaultRowHeight();
490 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
491 pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
495 void RowPainter::paintDepthBar()
497 depth_type const depth = par_.getDepth();
502 depth_type prev_depth = 0;
503 if (!text_metrics_.isFirstRow(pit_, row_)) {
504 pit_type pit2 = pit_;
507 prev_depth = pars_[pit2].getDepth();
510 depth_type next_depth = 0;
511 if (!text_metrics_.isLastRow(pit_, row_)) {
512 pit_type pit2 = pit_;
513 if (row_.endpos() >= pars_[pit2].size())
515 next_depth = pars_[pit2].getDepth();
518 for (depth_type i = 1; i <= depth; ++i) {
519 int const w = nestMargin() / 5;
520 int x = int(xo_) + w * i;
521 // only consider the changebar space if we're drawing outermost text
522 if (text_.isMainText())
523 x += changebarMargin();
525 int const starty = yo_ - row_.ascent();
526 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
528 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
531 pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
533 pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
538 int RowPainter::paintAppendixStart(int y)
540 FontInfo pb_font = sane_font;
541 pb_font.setColor(Color_appendix);
548 docstring const label = _("Appendix");
549 theFontMetrics(pb_font).rectText(label, w, a, d);
551 int const text_start = int(xo_ + (width_ - w) / 2);
552 int const text_end = text_start + w;
554 pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
556 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
557 pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
559 return 3 * defaultRowHeight();
563 void RowPainter::paintFirst()
565 BufferParams const & bparams = pi_.base.bv->buffer().params();
566 Layout const & layout = par_.layout();
570 // start of appendix?
571 if (par_.params().startOfAppendix())
572 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
574 if (bparams.paragraph_separation == BufferParams::ParagraphSkipSeparation
576 if (layout.latextype == LATEX_PARAGRAPH
577 && !par_.getDepth()) {
578 y_top += bparams.getDefSkip().inPixels(*pi_.base.bv);
580 Layout const & playout = pars_[pit_ - 1].layout();
581 if (playout.latextype == LATEX_PARAGRAPH
582 && !pars_[pit_ - 1].getDepth()) {
583 // is it right to use defskip here, too? (AS)
584 y_top += bparams.getDefSkip().inPixels(*pi_.base.bv);
589 bool const is_seq = text_.isFirstInSequence(pit_);
590 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
592 if (layout.labeltype >= LABEL_STATIC
593 && (layout.labeltype != LABEL_STATIC
594 || layout.latextype != LATEX_ENVIRONMENT
598 && (layout.labeltype == LABEL_TOP_ENVIRONMENT
599 || layout.labeltype == LABEL_BIBLIO
600 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
601 // the labels at the top of an environment.
602 // More or less for bibliography
603 paintTopLevelLabel();
608 void RowPainter::paintLabel()
610 docstring const str = par_.labelString();
614 BufferParams const & bparams = pi_.base.bv->buffer().params();
615 bool const is_rtl = text_.isRTL(par_);
616 Layout const & layout = par_.layout();
617 ParagraphParameters const & pparams = par_.params();
618 FontInfo const font = labelFont();
619 FontMetrics const & fm = theFontMetrics(font);
623 // this is special code for the chapter layout. This is
624 // printed in an extra row and has a pagebreak at
626 if (layout.counter == "chapter") {
627 double spacing_val = 1.0;
628 if (!pparams.spacing().isDefault()) {
629 spacing_val = pparams.spacing().getValue();
631 spacing_val = bparams.spacing().getValue();
634 int const labeladdon = int(fm.maxHeight() * layout.spacing.getValue() * spacing_val);
636 int const maxdesc = int(fm.maxDescent() * layout.spacing.getValue() * spacing_val)
637 + int(layout.parsep) * defaultRowHeight();
640 x = width_ - leftMargin() -
644 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
647 x = width_ - leftMargin()
648 + fm.width(layout.labelsep);
650 x = x_ - fm.width(layout.labelsep)
654 pi_.pain.text(int(x), yo_, str, font);
659 void RowPainter::paintTopLevelLabel()
661 BufferParams const & bparams = pi_.base.bv->buffer().params();
662 bool const is_rtl = text_.isRTL(par_);
663 ParagraphParameters const & pparams = par_.params();
664 Layout const & layout = par_.layout();
665 FontInfo const font = labelFont();
666 docstring const str = par_.labelString();
670 double spacing_val = 1.0;
671 if (!pparams.spacing().isDefault())
672 spacing_val = pparams.spacing().getValue();
674 spacing_val = bparams.spacing().getValue();
676 FontMetrics const & fm = theFontMetrics(font);
678 int const labeladdon = int(fm.maxHeight()
679 * layout.spacing.getValue() * spacing_val);
682 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
683 + (layout.labelbottomsep * defaultRowHeight()));
686 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
689 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
690 x -= fm.width(str) / 2;
692 x = width_ - leftMargin() - fm.width(str);
694 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
698 /** Check if the current paragraph is the last paragraph in a
700 static int getEndLabel(pit_type p, Text const & text)
702 ParagraphList const & pars = text.paragraphs();
704 depth_type par_depth = pars[p].getDepth();
705 while (pit != pit_type(pars.size())) {
706 Layout const & layout = pars[pit].layout();
707 int const endlabeltype = layout.endlabeltype;
709 if (endlabeltype != END_LABEL_NO_LABEL) {
710 if (p + 1 == pit_type(pars.size()))
713 depth_type const next_depth =
714 pars[p + 1].getDepth();
715 if (par_depth > next_depth ||
716 (par_depth == next_depth && layout != pars[p + 1].layout()))
722 pit = text.outerHook(pit);
723 if (pit != pit_type(pars.size()))
724 par_depth = pars[pit].getDepth();
726 return END_LABEL_NO_LABEL;
730 void RowPainter::paintLast()
732 bool const is_rtl = text_.isRTL(par_);
733 int const endlabel = getEndLabel(pit_, text_);
735 // paint imaginary end-of-paragraph character
737 Change const & change = par_.lookupChange(par_.size());
738 if (change.changed()) {
739 FontMetrics const & fm =
740 theFontMetrics(pi_.base.bv->buffer().params().getFont());
741 int const length = fm.maxAscent() / 2;
742 Color col = change.color();
744 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
745 Painter::line_solid, 3);
747 if (change.deleted()) {
748 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
749 yo_ + 2, col, Painter::line_solid, 3);
751 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
752 yo_ + 2, col, Painter::line_solid, 3);
760 case END_LABEL_FILLED_BOX: {
761 FontInfo const font = labelFont();
762 FontMetrics const & fm = theFontMetrics(font);
763 int const size = int(0.75 * fm.maxAscent());
764 int const y = yo_ - size;
765 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
766 int x = is_rtl ? nestMargin() + changebarMargin()
767 : max_row_width - text_metrics_.rightMargin(pm_);
769 // If needed, move the box a bit to avoid overlapping with text.
770 int const rem = max_row_width - row_.width();
772 x += is_rtl ? rem : - rem;
774 if (endlabel == END_LABEL_BOX)
775 pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
777 pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
781 case END_LABEL_STATIC: {
782 FontInfo const font = labelFont();
783 FontMetrics const & fm = theFontMetrics(font);
784 docstring const & str = par_.layout().endlabelstring();
785 double const x = is_rtl ? x_ - fm.width(str) : x_;
786 pi_.pain.text(int(x), yo_, str, font);
790 case END_LABEL_NO_LABEL:
791 if (lyxrc.paragraph_markers && size_type(pit_ + 1) < pars_.size()) {
792 docstring const s = docstring(1, char_type(0x00B6));
793 FontInfo f = FontInfo();
794 FontMetrics const & fm = theFontMetrics(f);
795 f.setColor(Color_paragraphmarker);
796 pi_.pain.text(int(x_), yo_, s, f);
804 void RowPainter::paintOnlyInsets()
806 CoordCache const & cache = pi_.base.bv->coordCache();
807 pos_type const end = row_.endpos();
808 for (pos_type pos = row_.pos(); pos != end; ++pos) {
809 // If outer row has changed, nested insets are repaint completely.
810 Inset const * inset = par_.getInset(pos);
811 bool const nested_inset = inset &&
812 ((inset->asInsetMath() &&
813 !inset->asInsetMath()->asMacroTemplate())
814 || inset->asInsetText()
815 || inset->asInsetTabular());
818 if (x_ > pi_.base.bv->workWidth()
819 || !cache.getInsets().has(inset))
821 x_ = cache.getInsets().x(inset);
823 bool const pi_selected = pi_.selected;
824 Cursor const & cur = pi_.base.bv->cursor();
825 if (cur.selection() && cur.text() == &text_
826 && cur.normalAnchor().text() == &text_)
827 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos;
828 paintInset(inset, pos);
829 pi_.selected = pi_selected;
834 void RowPainter::paintText()
836 pos_type const end = row_.endpos();
837 // Spaces at logical line breaks in bidi text must be skipped during
838 // painting. However, they may appear visually in the middle
839 // of a row; they must be skipped, wherever they are...
840 // * logically "abc_[HEBREW_\nHEBREW]"
841 // * visually "abc_[_WERBEH\nWERBEH]"
842 pos_type skipped_sep_vpos = -1;
843 pos_type body_pos = par_.beginOfBody();
845 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
849 Layout const & layout = par_.layout();
851 Change change_running;
852 int change_last_x = 0;
854 // check for possible inline completion
855 DocIterator const & inlineCompletionPos = pi_.base.bv->inlineCompletionPos();
856 pos_type inlineCompletionVPos = -1;
857 if (inlineCompletionPos.inTexted()
858 && inlineCompletionPos.text() == &text_
859 && inlineCompletionPos.pit() == pit_
860 && inlineCompletionPos.pos() - 1 >= row_.pos()
861 && inlineCompletionPos.pos() - 1 < row_.endpos()) {
862 // draw logically behind the previous character
863 inlineCompletionVPos = bidi_.log2vis(inlineCompletionPos.pos() - 1);
866 // Use font span to speed things up, see below
870 // If the last logical character is a separator, don't paint it, unless
871 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
872 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
873 skipped_sep_vpos = bidi_.log2vis(end - 1);
875 for (pos_type vpos = row_.pos(); vpos < end; ) {
876 if (x_ > pi_.base.bv->workWidth())
879 // Skip the separator at the logical end of the row
880 if (vpos == skipped_sep_vpos) {
885 pos_type const pos = bidi_.vis2log(vpos);
887 if (pos >= par_.size()) {
892 // Use font span to speed things up, see above
893 if (vpos < font_span.first || vpos > font_span.last) {
894 font_span = par_.fontSpan(vpos);
895 font = text_metrics_.displayFont(pit_, vpos);
897 // split font span if inline completion is inside
898 if (font_span.first <= inlineCompletionVPos
899 && font_span.last > inlineCompletionVPos)
900 font_span.last = inlineCompletionVPos;
903 const int width_pos = pm_.singleWidth(pos, font);
905 if (x_ + width_pos < 0) {
910 Change const & change = par_.lookupChange(pos);
911 if (change.changed() && !change_running.changed()) {
912 change_running = change;
913 change_last_x = int(x_);
916 Inset const * inset = par_.getInset(pos);
917 bool const highly_editable_inset = inset
918 && inset->editable();
920 // If we reach the end of a change or if the author changes, paint it.
921 // We also don't paint across things like tables
922 if (change_running.changed() && (highly_editable_inset
923 || !change.changed() || !change_running.isSimilarTo(change))) {
924 // Calculate 1/3 height of the buffer's default font
925 FontMetrics const & fm
926 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
927 float const y_bar = change_running.deleted() ?
928 yo_ - fm.maxAscent() / 3 : yo_ + 2 * solid_line_offset_ + solid_line_thickness_;
929 pi_.pain.line(change_last_x, int(y_bar), int(x_), int(y_bar),
930 change_running.color(), Painter::line_solid, solid_line_thickness_);
932 // Change might continue with a different author or type
933 if (change.changed() && !highly_editable_inset) {
934 change_running = change;
935 change_last_x = int(x_);
937 change_running.setUnchanged();
940 if (body_pos > 0 && pos == body_pos - 1) {
941 int const lwidth = theFontMetrics(labelFont())
942 .width(layout.labelsep);
944 x_ += row_.label_hfill + lwidth - width_pos;
947 // Is the inline completion in front of character?
948 if (font.isRightToLeft() && vpos == inlineCompletionVPos)
949 paintInlineCompletion(font);
951 if (par_.isSeparator(pos)) {
952 Font const orig_font = text_metrics_.displayFont(pit_, pos);
953 double const orig_x = x_;
954 double separator_width = width_pos;
956 separator_width += row_.separator;
957 paintSeparator(orig_x, separator_width, orig_font.fontInfo());
958 paintForeignMark(orig_x, orig_font.language());
962 // If outer row has changed, nested insets are repaint completely.
963 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
965 bool const pi_selected = pi_.selected;
966 Cursor const & cur = pi_.base.bv->cursor();
967 if (cur.selection() && cur.text() == &text_
968 && cur.normalAnchor().text() == &text_)
969 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos;
970 paintInset(inset, pos);
971 pi_.selected = pi_selected;
975 // paint as many characters as possible.
976 paintFromPos(vpos, change_running.changed());
979 // Is the inline completion after character?
980 if (!font.isRightToLeft() && vpos - 1 == inlineCompletionVPos)
981 paintInlineCompletion(font);
984 // if we reach the end of a struck out range, paint it
985 if (change_running.changed()) {
986 FontMetrics const & fm
987 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
988 float const y_bar = change_running.deleted() ?
989 yo_ - fm.maxAscent() / 3 : yo_ + 2 * solid_line_offset_ + solid_line_thickness_;
990 pi_.pain.line(change_last_x, int(y_bar), int(x_), int(y_bar),
991 change_running.color(), Painter::line_solid, solid_line_thickness_);
992 change_running.setUnchanged();
997 void RowPainter::paintSelection()
999 if (!row_.selection())
1001 Cursor const & curs = pi_.base.bv->cursor();
1002 DocIterator beg = curs.selectionBegin();
1004 beg.pos() = row_.sel_beg;
1006 DocIterator end = curs.selectionEnd();
1008 end.pos() = row_.sel_end;
1010 bool const begin_boundary = beg.pos() >= row_.endpos();
1011 bool const end_boundary = row_.sel_end == row_.endpos();
1013 DocIterator cur = beg;
1014 cur.boundary(begin_boundary);
1015 int x1 = text_metrics_.cursorX(beg.top(), begin_boundary);
1016 int x2 = text_metrics_.cursorX(end.top(), end_boundary);
1017 int const y1 = yo_ - row_.ascent();
1018 int const y2 = y1 + row_.height();
1020 int const rm = text_.isMainText() ? pi_.base.bv->rightMargin() : 0;
1021 int const lm = text_.isMainText() ? pi_.base.bv->leftMargin() : 0;
1024 if (row_.begin_margin_sel) {
1025 if (text_.isRTL(beg.paragraph())) {
1026 pi_.pain.fillRectangle(int(xo_ + x1), y1,
1027 text_metrics_.width() - rm - x1, y2 - y1, Color_selection);
1029 pi_.pain.fillRectangle(int(xo_ + lm), y1, x1 - lm, y2 - y1,
1034 if (row_.end_margin_sel) {
1035 if (text_.isRTL(beg.paragraph())) {
1036 pi_.pain.fillRectangle(int(xo_ + lm), y1, x2 - lm, y2 - y1,
1039 pi_.pain.fillRectangle(int(xo_ + x2), y1, text_metrics_.width() - rm - x2,
1040 y2 - y1, Color_selection);
1044 // if we are on a boundary from the beginning, it's probably
1045 // a RTL boundary and we jump to the other side directly as this
1046 // segement is 0-size and confuses the logic below
1048 cur.boundary(false);
1050 // go through row and draw from RTL boundary to RTL boundary
1052 bool draw_now = false;
1054 // simplified cursorForward code below which does not
1055 // descend into insets and which does not go into the
1056 // next line. Compare the logic with the original cursorForward
1058 // if left of boundary -> just jump to right side, but
1059 // for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
1060 if (cur.boundary()) {
1061 cur.boundary(false);
1062 } else if (text_metrics_.isRTLBoundary(cur.pit(), cur.pos() + 1)) {
1063 // in front of RTL boundary -> Stay on this side of the boundary
1064 // because: ab|cDDEEFFghi -> abc|DDEEFFghi
1073 if (cur.pos() == row_.endpos())
1078 // the previous segment was just drawn, now the next starts
1079 x1 = text_metrics_.cursorX(cur.top(), cur.boundary());
1082 if (!(cur < end) || draw_now) {
1083 x2 = text_metrics_.cursorX(cur.top(), cur.boundary());
1084 pi_.pain.fillRectangle(int(xo_ + min(x1, x2)), y1, abs(x2 - x1),
1085 y2 - y1, Color_selection);
1087 // reset x1, so it is set again next round (which will be on the
1088 // right side of a boundary or at the selection end)
1095 void RowPainter::paintInlineCompletion(Font const & font)
1097 docstring completion = pi_.base.bv->inlineCompletion();
1098 FontInfo f = font.fontInfo();
1099 bool rtl = font.isRightToLeft();
1101 // draw the unique and the non-unique completion part
1102 // Note: this is not time-critical as it is
1103 // only done once per screen.
1104 size_t uniqueTo = pi_.base.bv->inlineCompletionUniqueChars();
1105 docstring s1 = completion.substr(0, uniqueTo);
1106 docstring s2 = completion.substr(uniqueTo);
1107 ColorCode c1 = Color_inlinecompletion;
1108 ColorCode c2 = Color_nonunique_inlinecompletion;
1118 pi_.pain.text(int(x_), yo_, s1, f);
1119 x_ += theFontMetrics(font).width(s1);
1124 pi_.pain.text(int(x_), yo_, s2, f);
1125 x_ += theFontMetrics(font).width(s2);