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.
14 #include "rowpainter.h"
18 #include "CoordCache.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
24 #include "support/gettext.h"
29 #include "MetricsInfo.h"
30 #include "Paragraph.h"
31 #include "ParagraphMetrics.h"
32 #include "paragraph_funcs.h"
33 #include "ParagraphParameters.h"
34 #include "TextMetrics.h"
37 #include "frontends/FontMetrics.h"
38 #include "frontends/Painter.h"
40 #include "insets/InsetText.h"
42 #include "support/debug.h"
43 #include "support/textutils.h"
45 #include <boost/crc.hpp>
53 using frontend::Painter;
54 using frontend::FontMetrics;
56 RowPainter::RowPainter(PainterInfo & pi,
57 Text const & text, pit_type pit, Row const & row, Bidi & bidi, 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)),
63 bidi_(bidi), erased_(pi_.erased_),
64 xo_(x), yo_(y), width_(text_metrics_.width())
66 bidi_.computeTables(par_, pi_.base.bv->buffer(), row_);
69 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
72 BOOST_ASSERT(pit >= 0);
73 BOOST_ASSERT(pit < int(text.paragraphs().size()));
75 // check for possible inline completion
76 DocIterator const & inlineCompletionPos = pi_.base.bv->inlineCompletionPos();
77 inlineCompletionVPos_ = -1;
78 if (inlineCompletionPos.inTexted()
79 && inlineCompletionPos.text() == &text_
80 && inlineCompletionPos.pit() == pit_) {
81 // draw visually behind the previous character
82 // FIXME: probably special RTL handling needed here
83 inlineCompletionVPos_ = bidi_.log2vis(inlineCompletionPos.pos() - 1);
88 FontInfo const RowPainter::getLabelFont() const
90 return text_.getLabelFont(pi_.base.bv->buffer(), par_);
94 int RowPainter::leftMargin() const
96 return text_metrics_.leftMargin(text_metrics_.width(), pit_,
100 // If you want to debug inset metrics uncomment the following line:
101 //#define DEBUG_METRICS
102 // This draws green lines around each inset.
105 void RowPainter::paintInset(Inset const * inset, pos_type const pos)
107 Font const font = text_metrics_.getDisplayFont(pit_, pos);
110 // Backup full_repaint status because some insets (InsetTabular)
111 // requires a full repaint
112 bool pi_full_repaint = pi_.full_repaint;
114 // FIXME: We should always use font, see documentation of
115 // noFontChange() in Inset.h.
116 pi_.base.font = inset->noFontChange() ?
117 pi_.base.bv->buffer().params().getFont().fontInfo() :
119 pi_.ltr_pos = (bidi_.level(pos) % 2 == 0);
120 pi_.erased_ = erased_ || par_.isDeleted(pos);
121 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
122 // insets are painted completely. Recursive
123 inset->drawSelection(pi_, int(x_), yo_);
124 inset->draw(pi_, int(x_), yo_);
126 Dimension const & dim = pm_.insetDimension(inset);
128 paintForeignMark(x_, font.language(), dim.descent());
132 // Restore full_repaint status.
133 pi_.full_repaint = pi_full_repaint;
136 int const x1 = int(x_ - dim.width());
138 BOOST_ASSERT(max_witdh_ > 0);
139 int right_margin = text_metrics_.rightMargin(pm_);
140 int const w = max_witdh_ - leftMargin() - right_margin;
141 MetricsInfo mi(pi_.base.bv, font.fontInfo(), w);
142 inset->metrics(mi, dim2);
143 if (dim.wid != dim2.wid)
144 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
145 << " draw width " << dim.width()
146 << "> metrics width " << dim2.wid << "." << endl;
147 if (dim->asc != dim2.asc)
148 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
149 << " draw ascent " << dim.ascent()
150 << "> metrics ascent " << dim2.asc << "." << endl;
151 if (dim2.descent() != dim.des)
152 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
153 << " draw ascent " << dim.descent()
154 << "> metrics descent " << dim2.des << "." << endl;
155 BOOST_ASSERT(dim2.wid == dim.wid);
156 BOOST_ASSERT(dim2.asc == dim.asc);
157 BOOST_ASSERT(dim2.des == dim.des);
158 int const x2 = x1 + dim.wid;
159 int const y1 = yo_ + dim.des;
160 int const y2 = yo_ - dim.asc;
161 pi_.pain.line(x1, y1, x1, y2, Color_green);
162 pi_.pain.line(x1, y1, x2, y1, Color_green);
163 pi_.pain.line(x2, y1, x2, y2, Color_green);
164 pi_.pain.line(x1, y2, x2, y2, Color_green);
169 void RowPainter::paintHebrewComposeChar(pos_type & vpos, FontInfo const & font)
171 pos_type pos = bidi_.vis2log(vpos);
176 char_type c = par_.getChar(pos);
180 int const width = theFontMetrics(font).width(c);
183 for (pos_type i = pos - 1; i >= 0; --i) {
185 if (!Encodings::isComposeChar_hebrew(c)) {
186 if (isPrintableNonspace(c)) {
187 int const width2 = pm_.singleWidth(i,
188 text_metrics_.getDisplayFont(pit_, i));
189 dx = (c == 0x05e8 || // resh
190 c == 0x05d3) // dalet
192 : (width2 - width) / 2;
199 pi_.pain.text(int(x_) + dx, yo_, str, font);
203 void RowPainter::paintArabicComposeChar(pos_type & vpos, FontInfo const & font)
205 pos_type pos = bidi_.vis2log(vpos);
209 char_type c = par_.getChar(pos);
210 c = par_.transformChar(c, pos);
214 int const width = theFontMetrics(font).width(c);
217 for (pos_type i = pos - 1; i >= 0; --i) {
219 if (!Encodings::isComposeChar_arabic(c)) {
220 if (isPrintableNonspace(c)) {
221 int const width2 = pm_.singleWidth(i,
222 text_metrics_.getDisplayFont(pit_, i));
223 dx = (width2 - width) / 2;
229 pi_.pain.text(int(x_) + dx, yo_, str, font);
233 void RowPainter::paintChars(pos_type & vpos, FontInfo const & font,
234 bool hebrew, bool arabic)
236 // This method takes up 70% of time when typing
237 pos_type pos = bidi_.vis2log(vpos);
238 pos_type const end = row_.endpos();
239 FontSpan const font_span = par_.fontSpan(pos);
240 Change::Type const prev_change = par_.lookupChange(pos).type;
243 vector<char_type> str;
245 str.push_back(par_.getChar(pos));
248 char_type c = str[0];
253 str[0] = par_.transformChar(c, pos);
256 // collect as much similar chars as we can
257 for (++vpos ; vpos < end ; ++vpos) {
258 pos = bidi_.vis2log(vpos);
259 if (pos < font_span.first || pos > font_span.last)
262 if (prev_change != par_.lookupChange(pos).type)
265 char_type c = par_.getChar(pos);
267 if (!isPrintableNonspace(c))
270 /* Because we do our own bidi, at this point the strings are
271 * already in visual order. However, Qt also applies its own
272 * bidi algorithm to strings that it paints to the screen.
273 * Therefore, if we were to paint Hebrew/Arabic words as a
274 * single string, the letters in the words would get reversed
275 * again. In order to avoid that, we don't collect Hebrew/
276 * Arabic characters, but rather paint them one at a time.
277 * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
282 /* FIXME: these checks are irrelevant, since 'arabic' and
283 * 'hebrew' alone are already going to trigger a break.
284 * However, this should not be removed completely, because
285 * if an alternative solution is found which allows grouping
286 * of arabic and hebrew characters, then these breaks may have
289 if (arabic && Encodings::isComposeChar_arabic(c))
292 if (hebrew && Encodings::isComposeChar_hebrew(c))
301 c = par_.transformChar(c, pos);
302 /* see comment in hebrew, explaining why we break */
309 docstring s(&str[0], str.size());
311 if (prev_change != Change::UNCHANGED) {
312 FontInfo copy = font;
313 if (prev_change == Change::DELETED) {
314 copy.setColor(Color_deletedtext);
315 } else if (prev_change == Change::INSERTED) {
316 copy.setColor(Color_addedtext);
318 x_ += pi_.pain.text(int(x_), yo_, s, copy);
320 x_ += pi_.pain.text(int(x_), yo_, s, font);
325 void RowPainter::paintForeignMark(double orig_x, Language const * lang,
328 if (!lyxrc.mark_foreign_language)
330 if (lang == latex_language)
332 if (lang == pi_.base.bv->buffer().params().language)
335 int const y = yo_ + 1 + desc;
336 pi_.pain.line(int(orig_x), y, int(x_), y, Color_language);
340 void RowPainter::paintFromPos(pos_type & vpos)
342 pos_type const pos = bidi_.vis2log(vpos);
343 Font const orig_font = text_metrics_.getDisplayFont(pit_, pos);
344 double const orig_x = x_;
346 // usual characters, no insets
347 char_type const c = par_.getChar(pos);
349 // special case languages
350 string const & lang = orig_font.language()->lang();
351 bool const hebrew = lang == "hebrew";
352 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" ||
355 // draw as many chars as we can
356 if ((!hebrew && !arabic)
357 || (hebrew && !Encodings::isComposeChar_hebrew(c))
358 || (arabic && !Encodings::isComposeChar_arabic(c))) {
359 paintChars(vpos, orig_font.fontInfo(), hebrew, arabic);
361 paintHebrewComposeChar(vpos, orig_font.fontInfo());
363 paintArabicComposeChar(vpos, orig_font.fontInfo());
366 paintForeignMark(orig_x, orig_font.language());
370 void RowPainter::paintChangeBar()
372 pos_type const start = row_.pos();
373 pos_type end = row_.endpos();
375 if (par_.size() == end) {
376 // this is the last row of the paragraph;
377 // thus, we must also consider the imaginary end-of-par character
381 if (start == end || !par_.isChanged(start, end))
384 int const height = text_metrics_.isLastRow(pit_, row_)
388 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
392 void RowPainter::paintAppendix()
394 // only draw the appendix frame once (for the main text)
395 if (!par_.params().appendix() || !text_.isMainText(pi_.base.bv->buffer()))
398 int y = yo_ - row_.ascent();
400 if (par_.params().startOfAppendix())
401 y += 2 * defaultRowHeight();
403 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
404 pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
408 void RowPainter::paintDepthBar()
410 depth_type const depth = par_.getDepth();
415 depth_type prev_depth = 0;
416 if (!text_metrics_.isFirstRow(pit_, row_)) {
417 pit_type pit2 = pit_;
420 prev_depth = pars_[pit2].getDepth();
423 depth_type next_depth = 0;
424 if (!text_metrics_.isLastRow(pit_, row_)) {
425 pit_type pit2 = pit_;
426 if (row_.endpos() >= pars_[pit2].size())
428 next_depth = pars_[pit2].getDepth();
431 for (depth_type i = 1; i <= depth; ++i) {
432 int const w = nestMargin() / 5;
433 int x = int(xo_) + w * i;
434 // only consider the changebar space if we're drawing outermost text
435 if (text_.isMainText(pi_.base.bv->buffer()))
436 x += changebarMargin();
438 int const starty = yo_ - row_.ascent();
439 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
441 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
444 pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
446 pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
451 int RowPainter::paintAppendixStart(int y)
453 FontInfo pb_font = sane_font;
454 pb_font.setColor(Color_appendix);
461 docstring const label = _("Appendix");
462 theFontMetrics(pb_font).rectText(label, w, a, d);
464 int const text_start = int(xo_ + (width_ - w) / 2);
465 int const text_end = text_start + w;
467 pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
469 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
470 pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
472 return 3 * defaultRowHeight();
476 void RowPainter::paintFirst()
478 ParagraphParameters const & parparams = par_.params();
482 // start of appendix?
483 if (parparams.startOfAppendix())
484 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
486 Buffer const & buffer = pi_.base.bv->buffer();
488 LayoutPtr const & layout = par_.layout();
490 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
492 if (layout->latextype == LATEX_PARAGRAPH
493 && !par_.getDepth()) {
494 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
496 LayoutPtr const & playout = pars_[pit_ - 1].layout();
497 if (playout->latextype == LATEX_PARAGRAPH
498 && !pars_[pit_ - 1].getDepth()) {
499 // is it right to use defskip here, too? (AS)
500 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
506 bool const is_rtl = text_.isRTL(buffer, par_);
507 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
508 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
510 // should we print a label?
511 if (layout->labeltype >= LABEL_STATIC
512 && (layout->labeltype != LABEL_STATIC
513 || layout->latextype != LATEX_ENVIRONMENT
516 FontInfo const font = getLabelFont();
517 FontMetrics const & fm = theFontMetrics(font);
519 docstring const str = par_.getLabelstring();
523 // this is special code for the chapter layout. This is
524 // printed in an extra row and has a pagebreak at
526 if (layout->counter == "chapter") {
527 double spacing_val = 1.0;
528 if (!parparams.spacing().isDefault()) {
529 spacing_val = parparams.spacing().getValue();
531 spacing_val = buffer.params().spacing().getValue();
534 int const labeladdon = int(fm.maxHeight() * layout->spacing.getValue() * spacing_val);
536 int const maxdesc = int(fm.maxDescent() * layout->spacing.getValue() * spacing_val)
537 + int(layout->parsep) * defaultRowHeight();
540 x = width_ - leftMargin() -
544 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
547 x = width_ - leftMargin()
548 + fm.width(layout->labelsep);
550 x = x_ - fm.width(layout->labelsep)
554 pi_.pain.text(int(x), yo_, str, font);
558 // the labels at the top of an environment.
559 // More or less for bibliography
561 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
562 layout->labeltype == LABEL_BIBLIO ||
563 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
564 FontInfo const font = getLabelFont();
565 if (!par_.getLabelstring().empty()) {
566 docstring const str = par_.getLabelstring();
567 double spacing_val = 1.0;
568 if (!parparams.spacing().isDefault())
569 spacing_val = parparams.spacing().getValue();
571 spacing_val = buffer.params().spacing().getValue();
573 FontMetrics const & fm = theFontMetrics(font);
575 int const labeladdon = int(fm.maxHeight()
576 * layout->spacing.getValue() * spacing_val);
579 int(fm.maxDescent() * layout->spacing.getValue() * spacing_val
580 + (layout->labelbottomsep * defaultRowHeight()));
583 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
586 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
587 x -= fm.width(str) / 2;
589 x = width_ - leftMargin() - fm.width(str);
591 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
597 void RowPainter::paintLast()
599 bool const is_rtl = text_.isRTL(pi_.base.bv->buffer(), par_);
600 int const endlabel = getEndLabel(pit_, text_.paragraphs());
602 // paint imaginary end-of-paragraph character
604 if (par_.isInserted(par_.size()) || par_.isDeleted(par_.size())) {
605 FontMetrics const & fm = theFontMetrics(pi_.base.bv->buffer().params().getFont());
606 int const length = fm.maxAscent() / 2;
607 ColorCode col = par_.isInserted(par_.size()) ? Color_addedtext : Color_deletedtext;
609 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
610 Painter::line_solid, Painter::line_thick);
611 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1, yo_ + 2, col,
612 Painter::line_solid, Painter::line_thick);
619 case END_LABEL_FILLED_BOX: {
620 FontInfo const font = getLabelFont();
621 FontMetrics const & fm = theFontMetrics(font);
622 int const size = int(0.75 * fm.maxAscent());
623 int const y = yo_ - size;
624 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
625 int x = is_rtl ? nestMargin() + changebarMargin()
626 : max_row_width - text_metrics_.rightMargin(pm_);
628 // If needed, move the box a bit to avoid overlapping with text.
629 int const rem = max_row_width - row_.width();
631 x += is_rtl ? rem : - rem;
633 if (endlabel == END_LABEL_BOX)
634 pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
636 pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
640 case END_LABEL_STATIC: {
641 FontInfo const font = getLabelFont();
642 FontMetrics const & fm = theFontMetrics(font);
643 docstring const & str = par_.layout()->endlabelstring();
644 double const x = is_rtl ?
646 : - text_metrics_.rightMargin(pm_) - row_.width();
647 pi_.pain.text(int(x), yo_, str, font);
651 case END_LABEL_NO_LABEL:
657 void RowPainter::paintOnlyInsets()
659 pos_type const end = row_.endpos();
660 for (pos_type pos = row_.pos(); pos != end; ++pos) {
661 // If outer row has changed, nested insets are repaint completely.
662 Inset const * inset = par_.getInset(pos);
665 if (x_ > pi_.base.bv->workWidth())
667 x_ = pi_.base.bv->coordCache().getInsets().x(inset);
668 paintInset(inset, pos);
673 void RowPainter::paintText()
675 pos_type const end = row_.endpos();
676 // Spaces at logical line breaks in bidi text must be skipped during
677 // painting. However, they may appear visually in the middle
678 // of a row; they must be skipped, wherever they are...
679 // * logically "abc_[HEBREW_\nHEBREW]"
680 // * visually "abc_[_WERBEH\nWERBEH]"
681 pos_type skipped_sep_vpos = -1;
682 pos_type body_pos = par_.beginOfBody();
684 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
688 LayoutPtr const & layout = par_.layout();
690 bool running_strikeout = false;
691 bool is_struckout = false;
692 int last_strikeout_x = 0;
694 // Use font span to speed things up, see below
698 // If the last logical character is a separator, don't paint it, unless
699 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
700 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
701 skipped_sep_vpos = bidi_.log2vis(end - 1);
703 for (pos_type vpos = row_.pos(); vpos < end; ) {
704 if (x_ > pi_.base.bv->workWidth())
707 // Skip the separator at the logical end of the row
708 if (vpos == skipped_sep_vpos) {
713 pos_type const pos = bidi_.vis2log(vpos);
715 if (pos >= par_.size()) {
720 // Use font span to speed things up, see above
721 if (vpos < font_span.first || vpos > font_span.last) {
722 font_span = par_.fontSpan(vpos);
723 font = text_metrics_.getDisplayFont(pit_, vpos);
725 // split font span if inline completion is inside
726 if (font_span.first <= inlineCompletionVPos_
727 && font_span.last > inlineCompletionVPos_)
728 font_span.last = inlineCompletionVPos_;
731 const int width_pos = pm_.singleWidth(pos, font);
733 if (x_ + width_pos < 0) {
739 is_struckout = par_.isDeleted(pos);
741 if (is_struckout && !running_strikeout) {
742 running_strikeout = true;
743 last_strikeout_x = int(x_);
746 Inset const * inset = par_.getInset(pos);
747 bool const highly_editable_inset = inset
748 && inset->editable() == Inset::HIGHLY_EDITABLE;
750 // If we reach the end of a struck out range, paint it.
751 // We also don't paint across things like tables
752 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
753 // Calculate 1/3 height of the buffer's default font
754 FontMetrics const & fm
755 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
756 int const middle = yo_ - fm.maxAscent() / 3;
757 pi_.pain.line(last_strikeout_x, middle, int(x_), middle,
758 Color_deletedtext, Painter::line_solid, Painter::line_thin);
759 running_strikeout = false;
762 if (body_pos > 0 && pos == body_pos - 1) {
763 int const lwidth = theFontMetrics(getLabelFont())
764 .width(layout->labelsep);
766 x_ += row_.label_hfill + lwidth - width_pos;
769 if (par_.isSeparator(pos)) {
770 Font const orig_font = text_metrics_.getDisplayFont(pit_, pos);
771 double const orig_x = x_;
774 x_ += row_.separator;
775 paintForeignMark(orig_x, orig_font.language());
779 // If outer row has changed, nested insets are repaint completely.
780 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
781 paintInset(inset, pos);
785 // paint as many characters as possible.
789 // Is the inline completion here?
790 // FIXME: RTL support needed here
791 if (vpos - 1 == inlineCompletionVPos_) {
792 docstring const & completion = pi_.base.bv->inlineCompletion();
793 FontInfo f = font.fontInfo();
795 // draw the unique and the non-unique completion part
796 // Note: this is not time-critical as it is
797 // only done once per screen.
798 size_t uniqueTo = pi_.base.bv->inlineCompletionUniqueChars();
799 docstring s1 = completion.substr(0, uniqueTo);
800 docstring s2 = completion.substr(uniqueTo);
803 f.setColor(Color_inlinecompletion);
804 pi_.pain.text(x_, yo_, s1, f);
805 x_ += theFontMetrics(font).width(s1);
809 f.setColor(Color_nonunique_inlinecompletion);
810 pi_.pain.text(x_, yo_, s2, f);
811 x_ += theFontMetrics(font).width(s2);
816 // if we reach the end of a struck out range, paint it
817 if (running_strikeout) {
818 // calculate 1/3 height of the buffer's default font
819 FontMetrics const & fm
820 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
821 int const middle = yo_ - fm.maxAscent() / 3;
822 pi_.pain.line(last_strikeout_x, middle, int(x_), middle,
823 Color_deletedtext, Painter::line_solid, Painter::line_thin);
824 running_strikeout = false;