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 "support/debug.h"
21 #include "BufferParams.h"
22 #include "BufferView.h"
25 #include "support/gettext.h"
30 #include "MetricsInfo.h"
31 #include "Paragraph.h"
32 #include "ParagraphMetrics.h"
33 #include "paragraph_funcs.h"
34 #include "ParagraphParameters.h"
35 #include "TextMetrics.h"
38 #include "frontends/FontMetrics.h"
39 #include "frontends/Painter.h"
41 #include "insets/InsetText.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()));
77 FontInfo const RowPainter::getLabelFont() const
79 return text_.getLabelFont(pi_.base.bv->buffer(), par_);
83 int RowPainter::leftMargin() const
85 return text_metrics_.leftMargin(text_metrics_.width(), pit_,
89 // If you want to debug inset metrics uncomment the following line:
90 //#define DEBUG_METRICS
91 // This draws green lines around each inset.
94 void RowPainter::paintInset(Inset const * inset, pos_type const pos)
96 Font const font = text_metrics_.getDisplayFont(pit_, pos);
99 // Backup full_repaint status because some insets (InsetTabular)
100 // requires a full repaint
101 bool pi_full_repaint = pi_.full_repaint;
103 // FIXME: We should always use font, see documentation of
104 // noFontChange() in Inset.h.
105 pi_.base.font = inset->noFontChange() ?
106 pi_.base.bv->buffer().params().getFont().fontInfo() :
108 pi_.ltr_pos = (bidi_.level(pos) % 2 == 0);
109 pi_.erased_ = erased_ || par_.isDeleted(pos);
110 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
111 // insets are painted completely. Recursive
112 inset->drawSelection(pi_, int(x_), yo_);
113 inset->draw(pi_, int(x_), yo_);
115 Dimension const & dim = pm_.insetDimension(inset);
117 paintForeignMark(x_, font.language(), dim.descent());
121 // Restore full_repaint status.
122 pi_.full_repaint = pi_full_repaint;
125 int const x1 = int(x_ - dim.width());
127 BOOST_ASSERT(max_witdh_ > 0);
128 int right_margin = text_metrics_.rightMargin(pm_);
129 int const w = max_witdh_ - leftMargin() - right_margin;
130 MetricsInfo mi(pi_.base.bv, font.fontInfo(), w);
131 inset->metrics(mi, dim2);
132 if (dim.wid != dim2.wid)
133 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
134 << " draw width " << dim.width()
135 << "> metrics width " << dim2.wid << "." << endl;
136 if (dim->asc != dim2.asc)
137 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
138 << " draw ascent " << dim.ascent()
139 << "> metrics ascent " << dim2.asc << "." << endl;
140 if (dim2.descent() != dim.des)
141 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
142 << " draw ascent " << dim.descent()
143 << "> metrics descent " << dim2.des << "." << endl;
144 BOOST_ASSERT(dim2.wid == dim.wid);
145 BOOST_ASSERT(dim2.asc == dim.asc);
146 BOOST_ASSERT(dim2.des == dim.des);
147 int const x2 = x1 + dim.wid;
148 int const y1 = yo_ + dim.des;
149 int const y2 = yo_ - dim.asc;
150 pi_.pain.line(x1, y1, x1, y2, Color_green);
151 pi_.pain.line(x1, y1, x2, y1, Color_green);
152 pi_.pain.line(x2, y1, x2, y2, Color_green);
153 pi_.pain.line(x1, y2, x2, y2, Color_green);
158 void RowPainter::paintHebrewComposeChar(pos_type & vpos, FontInfo const & font)
160 pos_type pos = bidi_.vis2log(vpos);
165 char_type c = par_.getChar(pos);
169 int const width = theFontMetrics(font).width(c);
172 for (pos_type i = pos - 1; i >= 0; --i) {
174 if (!Encodings::isComposeChar_hebrew(c)) {
175 if (isPrintableNonspace(c)) {
176 int const width2 = pm_.singleWidth(i,
177 text_metrics_.getDisplayFont(pit_, i));
178 dx = (c == 0x05e8 || // resh
179 c == 0x05d3) // dalet
181 : (width2 - width) / 2;
188 pi_.pain.text(int(x_) + dx, yo_, str, font);
192 void RowPainter::paintArabicComposeChar(pos_type & vpos, FontInfo const & font)
194 pos_type pos = bidi_.vis2log(vpos);
198 char_type c = par_.getChar(pos);
199 c = par_.transformChar(c, pos);
203 int const width = theFontMetrics(font).width(c);
206 for (pos_type i = pos - 1; i >= 0; --i) {
208 if (!Encodings::isComposeChar_arabic(c)) {
209 if (isPrintableNonspace(c)) {
210 int const width2 = pm_.singleWidth(i,
211 text_metrics_.getDisplayFont(pit_, i));
212 dx = (width2 - width) / 2;
218 pi_.pain.text(int(x_) + dx, yo_, str, font);
222 void RowPainter::paintChars(pos_type & vpos, FontInfo const & font,
223 bool hebrew, bool arabic)
225 // This method takes up 70% of time when typing
226 pos_type pos = bidi_.vis2log(vpos);
227 pos_type const end = row_.endpos();
228 FontSpan const font_span = par_.fontSpan(pos);
229 Change::Type const prev_change = par_.lookupChange(pos).type;
232 vector<char_type> str;
234 str.push_back(par_.getChar(pos));
237 char_type c = str[0];
242 str[0] = par_.transformChar(c, pos);
245 // collect as much similar chars as we can
246 for (++vpos ; vpos < end ; ++vpos) {
247 pos = bidi_.vis2log(vpos);
248 if (pos < font_span.first || pos > font_span.last)
251 if (prev_change != par_.lookupChange(pos).type)
254 char_type c = par_.getChar(pos);
256 if (!isPrintableNonspace(c))
259 /* Because we do our own bidi, at this point the strings are
260 * already in visual order. However, Qt also applies its own
261 * bidi algorithm to strings that it paints to the screen.
262 * Therefore, if we were to paint Hebrew/Arabic words as a
263 * single string, the letters in the words would get reversed
264 * again. In order to avoid that, we don't collect Hebrew/
265 * Arabic characters, but rather paint them one at a time.
266 * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
271 /* FIXME: these checks are irrelevant, since 'arabic' and
272 * 'hebrew' alone are already going to trigger a break.
273 * However, this should not be removed completely, because
274 * if an alternative solution is found which allows grouping
275 * of arabic and hebrew characters, then these breaks may have
278 if (arabic && Encodings::isComposeChar_arabic(c))
281 if (hebrew && Encodings::isComposeChar_hebrew(c))
290 c = par_.transformChar(c, pos);
291 /* see comment in hebrew, explaining why we break */
298 docstring s(&str[0], str.size());
300 if (prev_change != Change::UNCHANGED) {
301 FontInfo copy = font;
302 if (prev_change == Change::DELETED) {
303 copy.setColor(Color_deletedtext);
304 } else if (prev_change == Change::INSERTED) {
305 copy.setColor(Color_addedtext);
307 x_ += pi_.pain.text(int(x_), yo_, s, copy);
309 x_ += pi_.pain.text(int(x_), yo_, s, font);
314 void RowPainter::paintForeignMark(double orig_x, Language const * lang,
317 if (!lyxrc.mark_foreign_language)
319 if (lang == latex_language)
321 if (lang == pi_.base.bv->buffer().params().language)
324 int const y = yo_ + 1 + desc;
325 pi_.pain.line(int(orig_x), y, int(x_), y, Color_language);
329 void RowPainter::paintFromPos(pos_type & vpos)
331 pos_type const pos = bidi_.vis2log(vpos);
332 Font const orig_font = text_metrics_.getDisplayFont(pit_, pos);
333 double const orig_x = x_;
335 // usual characters, no insets
336 char_type const c = par_.getChar(pos);
338 // special case languages
339 string const & lang = orig_font.language()->lang();
340 bool const hebrew = lang == "hebrew";
341 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" ||
344 // draw as many chars as we can
345 if ((!hebrew && !arabic)
346 || (hebrew && !Encodings::isComposeChar_hebrew(c))
347 || (arabic && !Encodings::isComposeChar_arabic(c))) {
348 paintChars(vpos, orig_font.fontInfo(), hebrew, arabic);
350 paintHebrewComposeChar(vpos, orig_font.fontInfo());
352 paintArabicComposeChar(vpos, orig_font.fontInfo());
355 paintForeignMark(orig_x, orig_font.language());
359 void RowPainter::paintChangeBar()
361 pos_type const start = row_.pos();
362 pos_type end = row_.endpos();
364 if (par_.size() == end) {
365 // this is the last row of the paragraph;
366 // thus, we must also consider the imaginary end-of-par character
370 if (start == end || !par_.isChanged(start, end))
373 int const height = text_metrics_.isLastRow(pit_, row_)
377 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
381 void RowPainter::paintAppendix()
383 // only draw the appendix frame once (for the main text)
384 if (!par_.params().appendix() || !text_.isMainText(pi_.base.bv->buffer()))
387 int y = yo_ - row_.ascent();
389 if (par_.params().startOfAppendix())
390 y += 2 * defaultRowHeight();
392 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
393 pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
397 void RowPainter::paintDepthBar()
399 depth_type const depth = par_.getDepth();
404 depth_type prev_depth = 0;
405 if (!text_metrics_.isFirstRow(pit_, row_)) {
406 pit_type pit2 = pit_;
409 prev_depth = pars_[pit2].getDepth();
412 depth_type next_depth = 0;
413 if (!text_metrics_.isLastRow(pit_, row_)) {
414 pit_type pit2 = pit_;
415 if (row_.endpos() >= pars_[pit2].size())
417 next_depth = pars_[pit2].getDepth();
420 for (depth_type i = 1; i <= depth; ++i) {
421 int const w = nestMargin() / 5;
422 int x = int(xo_) + w * i;
423 // only consider the changebar space if we're drawing outermost text
424 if (text_.isMainText(pi_.base.bv->buffer()))
425 x += changebarMargin();
427 int const starty = yo_ - row_.ascent();
428 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
430 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
433 pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
435 pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
440 int RowPainter::paintAppendixStart(int y)
442 FontInfo pb_font = sane_font;
443 pb_font.setColor(Color_appendix);
450 docstring const label = _("Appendix");
451 theFontMetrics(pb_font).rectText(label, w, a, d);
453 int const text_start = int(xo_ + (width_ - w) / 2);
454 int const text_end = text_start + w;
456 pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
458 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
459 pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
461 return 3 * defaultRowHeight();
465 void RowPainter::paintFirst()
467 ParagraphParameters const & parparams = par_.params();
471 // start of appendix?
472 if (parparams.startOfAppendix())
473 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
475 Buffer const & buffer = pi_.base.bv->buffer();
477 LayoutPtr const & layout = par_.layout();
479 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
481 if (layout->latextype == LATEX_PARAGRAPH
482 && !par_.getDepth()) {
483 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
485 LayoutPtr const & playout = pars_[pit_ - 1].layout();
486 if (playout->latextype == LATEX_PARAGRAPH
487 && !pars_[pit_ - 1].getDepth()) {
488 // is it right to use defskip here, too? (AS)
489 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
495 bool const is_rtl = text_.isRTL(buffer, par_);
496 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
497 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
499 // should we print a label?
500 if (layout->labeltype >= LABEL_STATIC
501 && (layout->labeltype != LABEL_STATIC
502 || layout->latextype != LATEX_ENVIRONMENT
505 FontInfo const font = getLabelFont();
506 FontMetrics const & fm = theFontMetrics(font);
508 docstring const str = par_.getLabelstring();
512 // this is special code for the chapter layout. This is
513 // printed in an extra row and has a pagebreak at
515 if (layout->counter == "chapter") {
516 double spacing_val = 1.0;
517 if (!parparams.spacing().isDefault()) {
518 spacing_val = parparams.spacing().getValue();
520 spacing_val = buffer.params().spacing().getValue();
523 int const labeladdon = int(fm.maxHeight() * layout->spacing.getValue() * spacing_val);
525 int const maxdesc = int(fm.maxDescent() * layout->spacing.getValue() * spacing_val)
526 + int(layout->parsep) * defaultRowHeight();
529 x = width_ - leftMargin() -
533 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
536 x = width_ - leftMargin()
537 + fm.width(layout->labelsep);
539 x = x_ - fm.width(layout->labelsep)
543 pi_.pain.text(int(x), yo_, str, font);
547 // the labels at the top of an environment.
548 // More or less for bibliography
550 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
551 layout->labeltype == LABEL_BIBLIO ||
552 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
553 FontInfo const font = getLabelFont();
554 if (!par_.getLabelstring().empty()) {
555 docstring const str = par_.getLabelstring();
556 double spacing_val = 1.0;
557 if (!parparams.spacing().isDefault())
558 spacing_val = parparams.spacing().getValue();
560 spacing_val = buffer.params().spacing().getValue();
562 FontMetrics const & fm = theFontMetrics(font);
564 int const labeladdon = int(fm.maxHeight()
565 * layout->spacing.getValue() * spacing_val);
568 int(fm.maxDescent() * layout->spacing.getValue() * spacing_val
569 + (layout->labelbottomsep * defaultRowHeight()));
572 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
575 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
576 x -= fm.width(str) / 2;
578 x = width_ - leftMargin() - fm.width(str);
580 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
586 void RowPainter::paintLast()
588 bool const is_rtl = text_.isRTL(pi_.base.bv->buffer(), par_);
589 int const endlabel = getEndLabel(pit_, text_.paragraphs());
591 // paint imaginary end-of-paragraph character
593 if (par_.isInserted(par_.size()) || par_.isDeleted(par_.size())) {
594 FontMetrics const & fm = theFontMetrics(pi_.base.bv->buffer().params().getFont());
595 int const length = fm.maxAscent() / 2;
596 ColorCode col = par_.isInserted(par_.size()) ? Color_addedtext : Color_deletedtext;
598 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
599 Painter::line_solid, Painter::line_thick);
600 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1, yo_ + 2, col,
601 Painter::line_solid, Painter::line_thick);
608 case END_LABEL_FILLED_BOX: {
609 FontInfo const font = getLabelFont();
610 FontMetrics const & fm = theFontMetrics(font);
611 int const size = int(0.75 * fm.maxAscent());
612 int const y = yo_ - size;
613 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
614 int x = is_rtl ? nestMargin() + changebarMargin()
615 : max_row_width - text_metrics_.rightMargin(pm_);
617 // If needed, move the box a bit to avoid overlapping with text.
618 int const rem = max_row_width - row_.width();
620 x += is_rtl ? rem : - rem;
622 if (endlabel == END_LABEL_BOX)
623 pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
625 pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
629 case END_LABEL_STATIC: {
630 FontInfo const font = getLabelFont();
631 FontMetrics const & fm = theFontMetrics(font);
632 docstring const & str = par_.layout()->endlabelstring();
633 double const x = is_rtl ?
635 : - text_metrics_.rightMargin(pm_) - row_.width();
636 pi_.pain.text(int(x), yo_, str, font);
640 case END_LABEL_NO_LABEL:
646 void RowPainter::paintOnlyInsets()
648 pos_type const end = row_.endpos();
649 for (pos_type pos = row_.pos(); pos != end; ++pos) {
650 // If outer row has changed, nested insets are repaint completely.
651 Inset const * inset = par_.getInset(pos);
654 if (x_ > pi_.base.bv->workWidth())
656 x_ = pi_.base.bv->coordCache().getInsets().x(inset);
657 paintInset(inset, pos);
662 void RowPainter::paintText()
664 pos_type const end = row_.endpos();
665 // Spaces at logical line breaks in bidi text must be skipped during
666 // painting. However, they may appear visually in the middle
667 // of a row; they must be skipped, wherever they are...
668 // * logically "abc_[HEBREW_\nHEBREW]"
669 // * visually "abc_[_WERBEH\nWERBEH]"
670 pos_type skipped_sep_vpos = -1;
671 pos_type body_pos = par_.beginOfBody();
673 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
677 LayoutPtr const & layout = par_.layout();
679 bool running_strikeout = false;
680 bool is_struckout = false;
681 int last_strikeout_x = 0;
683 // Use font span to speed things up, see below
687 // If the last logical character is a separator, don't paint it, unless
688 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
689 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
690 skipped_sep_vpos = bidi_.log2vis(end - 1);
692 for (pos_type vpos = row_.pos(); vpos < end; ) {
693 if (x_ > pi_.base.bv->workWidth())
696 // Skip the separator at the logical end of the row
697 if (vpos == skipped_sep_vpos) {
702 pos_type const pos = bidi_.vis2log(vpos);
704 if (pos >= par_.size()) {
709 // Use font span to speed things up, see above
710 if (vpos < font_span.first || vpos > font_span.last) {
711 font_span = par_.fontSpan(vpos);
712 font = text_metrics_.getDisplayFont(pit_, vpos);
715 const int width_pos = pm_.singleWidth(pos, font);
717 if (x_ + width_pos < 0) {
723 is_struckout = par_.isDeleted(pos);
725 if (is_struckout && !running_strikeout) {
726 running_strikeout = true;
727 last_strikeout_x = int(x_);
730 Inset const * inset = par_.getInset(pos);
731 bool const highly_editable_inset = inset
732 && inset->editable() == Inset::HIGHLY_EDITABLE;
734 // If we reach the end of a struck out range, paint it.
735 // We also don't paint across things like tables
736 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
737 // Calculate 1/3 height of the buffer's default font
738 FontMetrics const & fm
739 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
740 int const middle = yo_ - fm.maxAscent() / 3;
741 pi_.pain.line(last_strikeout_x, middle, int(x_), middle,
742 Color_deletedtext, Painter::line_solid, Painter::line_thin);
743 running_strikeout = false;
746 if (body_pos > 0 && pos == body_pos - 1) {
747 int const lwidth = theFontMetrics(getLabelFont())
748 .width(layout->labelsep);
750 x_ += row_.label_hfill + lwidth - width_pos;
753 if (par_.isSeparator(pos)) {
754 Font const orig_font = text_metrics_.getDisplayFont(pit_, pos);
755 double const orig_x = x_;
758 x_ += row_.separator;
759 paintForeignMark(orig_x, orig_font.language());
763 // If outer row has changed, nested insets are repaint completely.
764 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
765 paintInset(inset, pos);
769 // paint as many characters as possible.
774 // if we reach the end of a struck out range, paint it
775 if (running_strikeout) {
776 // calculate 1/3 height of the buffer's default font
777 FontMetrics const & fm
778 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
779 int const middle = yo_ - fm.maxAscent() / 3;
780 pi_.pain.line(last_strikeout_x, middle, int(x_), middle,
781 Color_deletedtext, Painter::line_solid, Painter::line_thin);
782 running_strikeout = false;