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>
56 using frontend::Painter;
57 using frontend::FontMetrics;
59 RowPainter::RowPainter(PainterInfo & pi,
60 Text const & text, pit_type pit, Row const & row, Bidi & bidi, int x, int y)
61 : pi_(pi), text_(text),
62 text_metrics_(pi_.base.bv->textMetrics(&text)),
63 pars_(text.paragraphs()),
64 row_(row), pit_(pit), par_(text.paragraphs()[pit]),
65 pm_(text_metrics_.parMetrics(pit)),
66 bidi_(bidi), erased_(pi_.erased_),
67 xo_(x), yo_(y), width_(text_metrics_.width())
69 bidi_.computeTables(par_, pi_.base.bv->buffer(), row_);
72 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
75 BOOST_ASSERT(pit >= 0);
76 BOOST_ASSERT(pit < int(text.paragraphs().size()));
80 FontInfo const RowPainter::getLabelFont() const
82 return text_.getLabelFont(pi_.base.bv->buffer(), par_);
86 int RowPainter::leftMargin() const
88 return text_metrics_.leftMargin(text_metrics_.width(), pit_,
92 // If you want to debug inset metrics uncomment the following line:
93 //#define DEBUG_METRICS
94 // This draws green lines around each inset.
97 void RowPainter::paintInset(Inset const * inset, pos_type const pos)
99 Font const font = text_metrics_.getDisplayFont(pit_, pos);
102 // FIXME: We should always use font, see documentation of
103 // noFontChange() in Inset.h.
104 pi_.base.font = inset->noFontChange() ?
105 pi_.base.bv->buffer().params().getFont().fontInfo() :
107 pi_.ltr_pos = (bidi_.level(pos) % 2 == 0);
108 pi_.erased_ = erased_ || par_.isDeleted(pos);
109 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
110 // insets are painted completely. Recursive
111 inset->drawSelection(pi_, int(x_), yo_);
112 inset->draw(pi_, int(x_), yo_);
114 Dimension const & dim = pm_.insetDimension(inset);
116 paintForeignMark(x_, font.language(), dim.descent());
121 int const x1 = int(x_ - dim.width());
123 BOOST_ASSERT(max_witdh_ > 0);
124 int right_margin = text_metrics_.rightMargin(pm_);
125 int const w = max_witdh_ - leftMargin() - right_margin;
126 MetricsInfo mi(pi_.base.bv, font.fontInfo(), w);
127 inset->metrics(mi, dim2);
128 if (dim.wid != dim2.wid)
129 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
130 << " draw width " << dim.width()
131 << "> metrics width " << dim2.wid << "." << std::endl;
132 if (dim->asc != dim2.asc)
133 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
134 << " draw ascent " << dim.ascent()
135 << "> metrics ascent " << dim2.asc << "." << std::endl;
136 if (dim2.descent() != dim.des)
137 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
138 << " draw ascent " << dim.descent()
139 << "> metrics descent " << dim2.des << "." << std::endl;
140 BOOST_ASSERT(dim2.wid == dim.wid);
141 BOOST_ASSERT(dim2.asc == dim.asc);
142 BOOST_ASSERT(dim2.des == dim.des);
143 int const x2 = x1 + dim.wid;
144 int const y1 = yo_ + dim.des;
145 int const y2 = yo_ - dim.asc;
146 pi_.pain.line(x1, y1, x1, y2, Color_green);
147 pi_.pain.line(x1, y1, x2, y1, Color_green);
148 pi_.pain.line(x2, y1, x2, y2, Color_green);
149 pi_.pain.line(x1, y2, x2, y2, Color_green);
154 void RowPainter::paintHebrewComposeChar(pos_type & vpos, FontInfo const & font)
156 pos_type pos = bidi_.vis2log(vpos);
161 char_type c = par_.getChar(pos);
165 int const width = theFontMetrics(font).width(c);
168 for (pos_type i = pos - 1; i >= 0; --i) {
170 if (!Encodings::isComposeChar_hebrew(c)) {
171 if (isPrintableNonspace(c)) {
172 int const width2 = pm_.singleWidth(i,
173 text_metrics_.getDisplayFont(pit_, i));
174 dx = (c == 0x05e8 || // resh
175 c == 0x05d3) // dalet
177 : (width2 - width) / 2;
184 pi_.pain.text(int(x_) + dx, yo_, str, font);
188 void RowPainter::paintArabicComposeChar(pos_type & vpos, FontInfo const & font)
190 pos_type pos = bidi_.vis2log(vpos);
194 char_type c = par_.getChar(pos);
195 c = par_.transformChar(c, pos);
199 int const width = theFontMetrics(font).width(c);
202 for (pos_type i = pos - 1; i >= 0; --i) {
204 if (!Encodings::isComposeChar_arabic(c)) {
205 if (isPrintableNonspace(c)) {
206 int const width2 = pm_.singleWidth(i,
207 text_metrics_.getDisplayFont(pit_, i));
208 dx = (width2 - width) / 2;
214 pi_.pain.text(int(x_) + dx, yo_, str, font);
218 void RowPainter::paintChars(pos_type & vpos, FontInfo const & font,
219 bool hebrew, bool arabic)
221 // This method takes up 70% of time when typing
222 pos_type pos = bidi_.vis2log(vpos);
223 pos_type const end = row_.endpos();
224 FontSpan const font_span = par_.fontSpan(pos);
225 Change::Type const prev_change = par_.lookupChange(pos).type;
228 std::vector<char_type> str;
230 str.push_back(par_.getChar(pos));
233 char_type c = str[0];
238 str[0] = par_.transformChar(c, pos);
241 // collect as much similar chars as we can
242 for (++vpos ; vpos < end ; ++vpos) {
243 pos = bidi_.vis2log(vpos);
244 if (pos < font_span.first || pos > font_span.last)
247 if (prev_change != par_.lookupChange(pos).type)
250 char_type c = par_.getChar(pos);
252 if (!isPrintableNonspace(c))
255 /* Because we do our own bidi, at this point the strings are
256 * already in visual order. However, Qt also applies its own
257 * bidi algorithm to strings that it paints to the screen.
258 * Therefore, if we were to paint Hebrew/Arabic words as a
259 * single string, the letters in the words would get reversed
260 * again. In order to avoid that, we don't collect Hebrew/
261 * Arabic characters, but rather paint them one at a time.
262 * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
267 /* FIXME: these checks are irrelevant, since 'arabic' and
268 * 'hebrew' alone are already going to trigger a break.
269 * However, this should not be removed completely, because
270 * if an alternative solution is found which allows grouping
271 * of arabic and hebrew characters, then these breaks may have
274 if (arabic && Encodings::isComposeChar_arabic(c))
277 if (hebrew && Encodings::isComposeChar_hebrew(c))
286 c = par_.transformChar(c, pos);
287 /* see comment in hebrew, explaining why we break */
294 docstring s(&str[0], str.size());
296 if (prev_change != Change::UNCHANGED) {
297 FontInfo copy = font;
298 if (prev_change == Change::DELETED) {
299 copy.setColor(Color_deletedtext);
300 } else if (prev_change == Change::INSERTED) {
301 copy.setColor(Color_addedtext);
303 x_ += pi_.pain.text(int(x_), yo_, s, copy);
305 x_ += pi_.pain.text(int(x_), yo_, s, font);
310 void RowPainter::paintForeignMark(double orig_x, Language const * lang,
313 if (!lyxrc.mark_foreign_language)
315 if (lang == latex_language)
317 if (lang == pi_.base.bv->buffer().params().language)
320 int const y = yo_ + 1 + desc;
321 pi_.pain.line(int(orig_x), y, int(x_), y, Color_language);
325 void RowPainter::paintFromPos(pos_type & vpos)
327 pos_type const pos = bidi_.vis2log(vpos);
328 Font const orig_font = text_metrics_.getDisplayFont(pit_, pos);
329 double const orig_x = x_;
331 // usual characters, no insets
332 char_type const c = par_.getChar(pos);
334 // special case languages
335 std::string const & lang = orig_font.language()->lang();
336 bool const hebrew = lang == "hebrew";
337 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" ||
340 // draw as many chars as we can
341 if ((!hebrew && !arabic)
342 || (hebrew && !Encodings::isComposeChar_hebrew(c))
343 || (arabic && !Encodings::isComposeChar_arabic(c))) {
344 paintChars(vpos, orig_font.fontInfo(), hebrew, arabic);
346 paintHebrewComposeChar(vpos, orig_font.fontInfo());
348 paintArabicComposeChar(vpos, orig_font.fontInfo());
351 paintForeignMark(orig_x, orig_font.language());
355 void RowPainter::paintChangeBar()
357 pos_type const start = row_.pos();
358 pos_type end = row_.endpos();
360 if (par_.size() == end) {
361 // this is the last row of the paragraph;
362 // thus, we must also consider the imaginary end-of-par character
366 if (start == end || !par_.isChanged(start, end))
369 int const height = text_metrics_.isLastRow(pit_, row_)
373 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
377 void RowPainter::paintAppendix()
379 // only draw the appendix frame once (for the main text)
380 if (!par_.params().appendix() || !text_.isMainText(pi_.base.bv->buffer()))
383 int y = yo_ - row_.ascent();
385 if (par_.params().startOfAppendix())
386 y += 2 * defaultRowHeight();
388 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
389 pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
393 void RowPainter::paintDepthBar()
395 depth_type const depth = par_.getDepth();
400 depth_type prev_depth = 0;
401 if (!text_metrics_.isFirstRow(pit_, row_)) {
402 pit_type pit2 = pit_;
405 prev_depth = pars_[pit2].getDepth();
408 depth_type next_depth = 0;
409 if (!text_metrics_.isLastRow(pit_, row_)) {
410 pit_type pit2 = pit_;
411 if (row_.endpos() >= pars_[pit2].size())
413 next_depth = pars_[pit2].getDepth();
416 for (depth_type i = 1; i <= depth; ++i) {
417 int const w = nestMargin() / 5;
418 int x = int(xo_) + w * i;
419 // only consider the changebar space if we're drawing outermost text
420 if (text_.isMainText(pi_.base.bv->buffer()))
421 x += changebarMargin();
423 int const starty = yo_ - row_.ascent();
424 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
426 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
429 pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
431 pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
436 int RowPainter::paintAppendixStart(int y)
438 FontInfo pb_font = sane_font;
439 pb_font.setColor(Color_appendix);
446 docstring const label = _("Appendix");
447 theFontMetrics(pb_font).rectText(label, w, a, d);
449 int const text_start = int(xo_ + (width_ - w) / 2);
450 int const text_end = text_start + w;
452 pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
454 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
455 pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
457 return 3 * defaultRowHeight();
461 void RowPainter::paintFirst()
463 ParagraphParameters const & parparams = par_.params();
467 // start of appendix?
468 if (parparams.startOfAppendix())
469 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
471 Buffer const & buffer = pi_.base.bv->buffer();
473 LayoutPtr const & layout = par_.layout();
475 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
477 if (layout->latextype == LATEX_PARAGRAPH
478 && !par_.getDepth()) {
479 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
481 LayoutPtr const & playout = pars_[pit_ - 1].layout();
482 if (playout->latextype == LATEX_PARAGRAPH
483 && !pars_[pit_ - 1].getDepth()) {
484 // is it right to use defskip here, too? (AS)
485 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
491 bool const is_rtl = text_.isRTL(buffer, par_);
492 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
493 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
495 // should we print a label?
496 if (layout->labeltype >= LABEL_STATIC
497 && (layout->labeltype != LABEL_STATIC
498 || layout->latextype != LATEX_ENVIRONMENT
501 FontInfo const font = getLabelFont();
502 FontMetrics const & fm = theFontMetrics(font);
504 docstring const str = par_.getLabelstring();
508 // this is special code for the chapter layout. This is
509 // printed in an extra row and has a pagebreak at
511 if (layout->counter == "chapter") {
512 double spacing_val = 1.0;
513 if (!parparams.spacing().isDefault()) {
514 spacing_val = parparams.spacing().getValue();
516 spacing_val = buffer.params().spacing().getValue();
519 int const labeladdon = int(fm.maxHeight() * layout->spacing.getValue() * spacing_val);
521 int const maxdesc = int(fm.maxDescent() * layout->spacing.getValue() * spacing_val)
522 + int(layout->parsep) * defaultRowHeight();
525 x = width_ - leftMargin() -
529 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
532 x = width_ - leftMargin()
533 + fm.width(layout->labelsep);
535 x = x_ - fm.width(layout->labelsep)
539 pi_.pain.text(int(x), yo_, str, font);
543 // the labels at the top of an environment.
544 // More or less for bibliography
546 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
547 layout->labeltype == LABEL_BIBLIO ||
548 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
549 FontInfo const font = getLabelFont();
550 if (!par_.getLabelstring().empty()) {
551 docstring const str = par_.getLabelstring();
552 double spacing_val = 1.0;
553 if (!parparams.spacing().isDefault())
554 spacing_val = parparams.spacing().getValue();
556 spacing_val = buffer.params().spacing().getValue();
558 FontMetrics const & fm = theFontMetrics(font);
560 int const labeladdon = int(fm.maxHeight()
561 * layout->spacing.getValue() * spacing_val);
564 int(fm.maxDescent() * layout->spacing.getValue() * spacing_val
565 + (layout->labelbottomsep * defaultRowHeight()));
568 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
571 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
572 x -= fm.width(str) / 2;
574 x = width_ - leftMargin() - fm.width(str);
576 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
582 void RowPainter::paintLast()
584 bool const is_rtl = text_.isRTL(pi_.base.bv->buffer(), par_);
585 int const endlabel = getEndLabel(pit_, text_.paragraphs());
587 // paint imaginary end-of-paragraph character
589 if (par_.isInserted(par_.size()) || par_.isDeleted(par_.size())) {
590 FontMetrics const & fm = theFontMetrics(pi_.base.bv->buffer().params().getFont());
591 int const length = fm.maxAscent() / 2;
592 ColorCode col = par_.isInserted(par_.size()) ? Color_addedtext : Color_deletedtext;
594 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
595 Painter::line_solid, Painter::line_thick);
596 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1, yo_ + 2, col,
597 Painter::line_solid, Painter::line_thick);
604 case END_LABEL_FILLED_BOX: {
605 FontInfo const font = getLabelFont();
606 FontMetrics const & fm = theFontMetrics(font);
607 int const size = int(0.75 * fm.maxAscent());
608 int const y = yo_ - size;
609 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
611 if (width_ - int(row_.width()) <= size)
612 x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
614 if (endlabel == END_LABEL_BOX)
615 pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
617 pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
621 case END_LABEL_STATIC: {
622 FontInfo const font = getLabelFont();
623 FontMetrics const & fm = theFontMetrics(font);
624 docstring const & str = par_.layout()->endlabelstring();
625 double const x = is_rtl ?
627 : - text_metrics_.rightMargin(pm_) - row_.width();
628 pi_.pain.text(int(x), yo_, str, font);
632 case END_LABEL_NO_LABEL:
638 void RowPainter::paintOnlyInsets()
640 pos_type const end = row_.endpos();
641 for (pos_type pos = row_.pos(); pos != end; ++pos) {
642 if (!par_.isInset(pos))
645 // If outer row has changed, nested insets are repaint completely.
646 Inset const * inset = par_.getInset(pos);
648 if (x_ > pi_.base.bv->workWidth())
651 x_ = pi_.base.bv->coordCache().getInsets().x(inset);
652 paintInset(inset, pos);
657 void RowPainter::paintText()
659 pos_type const end = row_.endpos();
660 // Spaces at logical line breaks in bidi text must be skipped during
661 // painting. However, they may appear visually in the middle
662 // of a row; they must be skipped, wherever they are...
663 // * logically "abc_[HEBREW_\nHEBREW]"
664 // * visually "abc_[_WERBEH\nWERBEH]"
665 pos_type skipped_sep_vpos = -1;
666 pos_type body_pos = par_.beginOfBody();
668 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
672 LayoutPtr const & layout = par_.layout();
674 bool running_strikeout = false;
675 bool is_struckout = false;
676 int last_strikeout_x = 0;
678 // Use font span to speed things up, see below
682 // If the last logical character is a separator, don't paint it, unless
683 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
684 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
685 skipped_sep_vpos = bidi_.log2vis(end - 1);
687 for (pos_type vpos = row_.pos(); vpos < end; ) {
688 if (x_ > pi_.base.bv->workWidth())
691 // Skip the separator at the logical end of the row
692 if (vpos == skipped_sep_vpos) {
697 pos_type const pos = bidi_.vis2log(vpos);
699 if (pos >= par_.size()) {
704 // Use font span to speed things up, see above
705 if (vpos < font_span.first || vpos > font_span.last) {
706 font_span = par_.fontSpan(vpos);
707 font = text_metrics_.getDisplayFont(pit_, vpos);
710 const int width_pos = pm_.singleWidth(pos, font);
712 if (x_ + width_pos < 0) {
718 is_struckout = par_.isDeleted(pos);
720 if (is_struckout && !running_strikeout) {
721 running_strikeout = true;
722 last_strikeout_x = int(x_);
725 bool const highly_editable_inset = par_.isInset(pos)
726 && par_.getInset(pos)->editable() == Inset::HIGHLY_EDITABLE;
728 // If we reach the end of a struck out range, paint it.
729 // We also don't paint across things like tables
730 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
731 // Calculate 1/3 height of the buffer's default font
732 FontMetrics const & fm
733 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
734 int const middle = yo_ - fm.maxAscent() / 3;
735 pi_.pain.line(last_strikeout_x, middle, int(x_), middle,
736 Color_deletedtext, Painter::line_solid, Painter::line_thin);
737 running_strikeout = false;
740 if (body_pos > 0 && pos == body_pos - 1) {
741 int const lwidth = theFontMetrics(getLabelFont())
742 .width(layout->labelsep);
744 x_ += row_.label_hfill + lwidth - width_pos;
747 if (par_.isSeparator(pos)) {
748 Font const orig_font = text_metrics_.getDisplayFont(pit_, pos);
749 double const orig_x = x_;
752 x_ += row_.separator;
753 paintForeignMark(orig_x, orig_font.language());
756 } else if (par_.isInset(pos)) {
757 // If outer row has changed, nested insets are repaint completely.
758 Inset const * inset = par_.getInset(pos);
759 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
760 paintInset(inset, pos);
764 // paint as many characters as possible.
769 // if we reach the end of a struck out range, paint it
770 if (running_strikeout) {
771 // calculate 1/3 height of the buffer's default font
772 FontMetrics const & fm
773 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
774 int const middle = yo_ - fm.maxAscent() / 3;
775 pi_.pain.line(last_strikeout_x, middle, int(x_), middle,
776 Color_deletedtext, Painter::line_solid, Painter::line_thin);
777 running_strikeout = false;