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"
21 #include "BufferParams.h"
22 #include "BufferView.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/textutils.h"
44 #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 Font 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_,
90 void RowPainter::paintHfill(pos_type const pos, pos_type const body_pos)
95 int const y1 = y0 - defaultRowHeight() / 2;
97 pi_.pain.line(int(x_), y1, int(x_), y0, Color::added_space);
99 if (pm_.hfillExpansion(row_, pos)) {
100 int const y2 = (y0 + y1) / 2;
102 if (pos >= body_pos) {
103 pi_.pain.line(int(x_), y2, int(x_ + row_.hfill), y2,
105 Painter::line_onoffdash);
108 pi_.pain.line(int(x_), y2, int(x_ + row_.label_hfill), y2,
110 Painter::line_onoffdash);
111 x_ += row_.label_hfill;
113 pi_.pain.line(int(x_), y1, int(x_), y0, Color::added_space);
119 // If you want to debug inset metrics uncomment the following line:
120 //#define DEBUG_METRICS
121 // This draws green lines around each inset.
124 void RowPainter::paintInset(Inset const * inset, pos_type const pos)
126 Font font = text_metrics_.getDisplayFont(pit_, pos);
129 // FIXME: We should always use font, see documentation of
130 // noFontChange() in Inset.h.
131 pi_.base.font = inset->noFontChange() ?
132 pi_.base.bv->buffer().params().getFont() :
134 pi_.ltr_pos = (bidi_.level(pos) % 2 == 0);
135 pi_.erased_ = erased_ || par_.isDeleted(pos);
136 // insets are painted completely. Recursive
137 inset->drawSelection(pi_, int(x_), yo_);
138 inset->draw(pi_, int(x_), yo_);
140 paintForeignMark(x_, font, inset->descent());
142 x_ += inset->width();
145 int const x1 = int(x_ - inset->width());
147 BOOST_ASSERT(max_witdh_ > 0);
148 int right_margin = text_metrics_.rightMargin(pm_);
149 int const w = max_witdh_ - leftMargin() - right_margin;
150 MetricsInfo mi(pi_.base.bv, font, w);
151 inset->metrics(mi, dim);
152 if (inset->width() > dim.wid)
153 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
154 << " draw width " << inset->width()
155 << "> metrics width " << dim.wid << "." << std::endl;
156 if (inset->ascent() > dim.asc)
157 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
158 << " draw ascent " << inset->ascent()
159 << "> metrics ascent " << dim.asc << "." << std::endl;
160 if (inset->descent() > dim.des)
161 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
162 << " draw ascent " << inset->descent()
163 << "> metrics descent " << dim.des << "." << std::endl;
164 BOOST_ASSERT(inset->width() <= dim.wid);
165 BOOST_ASSERT(inset->ascent() <= dim.asc);
166 BOOST_ASSERT(inset->descent() <= dim.des);
167 int const x2 = x1 + dim.wid;
168 int const y1 = yo_ + dim.des;
169 int const y2 = yo_ - dim.asc;
170 pi_.pain.line(x1, y1, x1, y2, Color::green);
171 pi_.pain.line(x1, y1, x2, y1, Color::green);
172 pi_.pain.line(x2, y1, x2, y2, Color::green);
173 pi_.pain.line(x1, y2, x2, y2, Color::green);
178 void RowPainter::paintHebrewComposeChar(pos_type & vpos, Font const & font)
180 pos_type pos = bidi_.vis2log(vpos);
185 char_type c = par_.getChar(pos);
189 int const width = theFontMetrics(font).width(c);
192 for (pos_type i = pos - 1; i >= 0; --i) {
194 if (!Encodings::isComposeChar_hebrew(c)) {
195 if (isPrintableNonspace(c)) {
196 int const width2 = pm_.singleWidth(i,
197 text_metrics_.getDisplayFont(pit_, i));
198 dx = (c == 0x05e8 || // resh
199 c == 0x05d3) // dalet
201 : (width2 - width) / 2;
208 pi_.pain.text(int(x_) + dx, yo_, str, font);
212 void RowPainter::paintArabicComposeChar(pos_type & vpos, Font const & font)
214 pos_type pos = bidi_.vis2log(vpos);
218 char_type c = par_.getChar(pos);
219 c = par_.transformChar(c, pos);
223 int const width = theFontMetrics(font).width(c);
226 for (pos_type i = pos - 1; i >= 0; --i) {
228 if (!Encodings::isComposeChar_arabic(c)) {
229 if (isPrintableNonspace(c)) {
230 int const width2 = pm_.singleWidth(i,
231 text_metrics_.getDisplayFont(pit_, i));
232 dx = (width2 - width) / 2;
238 pi_.pain.text(int(x_) + dx, yo_, str, font);
242 void RowPainter::paintChars(pos_type & vpos, Font const & font,
243 bool hebrew, bool arabic)
245 // This method takes up 70% of time when typing
246 pos_type pos = bidi_.vis2log(vpos);
247 pos_type const end = row_.endpos();
248 FontSpan const font_span = par_.fontSpan(pos);
249 Change::Type const prev_change = par_.lookupChange(pos).type;
252 std::vector<char_type> str;
254 str.push_back(par_.getChar(pos));
257 char_type c = str[0];
262 str[0] = par_.transformChar(c, pos);
265 // collect as much similar chars as we can
266 for (++vpos ; vpos < end ; ++vpos) {
267 pos = bidi_.vis2log(vpos);
268 if (pos < font_span.first || pos > font_span.last)
271 if (prev_change != par_.lookupChange(pos).type)
274 char_type c = par_.getChar(pos);
276 if (!isPrintableNonspace(c))
279 /* Because we do our own bidi, at this point the strings are
280 * already in visual order. However, Qt also applies its own
281 * bidi algorithm to strings that it paints to the screen.
282 * Therefore, if we were to paint Hebrew/Arabic words as a
283 * single string, the letters in the words would get reversed
284 * again. In order to avoid that, we don't collect Hebrew/
285 * Arabic characters, but rather paint them one at a time.
286 * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
291 /* FIXME: these checks are irrelevant, since 'arabic' and
292 * 'hebrew' alone are already going to trigger a break.
293 * However, this should not be removed completely, because
294 * if an alternative solution is found which allows grouping
295 * of arabic and hebrew characters, then these breaks may have
298 if (arabic && Encodings::isComposeChar_arabic(c))
301 if (hebrew && Encodings::isComposeChar_hebrew(c))
310 c = par_.transformChar(c, pos);
311 /* see comment in hebrew, explaining why we break */
318 docstring s(&str[0], str.size());
320 if (prev_change != Change::UNCHANGED) {
322 if (prev_change == Change::DELETED) {
323 copy.setColor(Color::deletedtext);
324 } else if (prev_change == Change::INSERTED) {
325 copy.setColor(Color::addedtext);
327 x_ += pi_.pain.text(int(x_), yo_, s, copy);
329 x_ += pi_.pain.text(int(x_), yo_, s, font);
334 void RowPainter::paintForeignMark(double orig_x, Font const & font, int desc)
336 if (!lyxrc.mark_foreign_language)
338 if (font.language() == latex_language)
340 if (font.language() == pi_.base.bv->buffer().params().language)
343 int const y = yo_ + 1 + desc;
344 pi_.pain.line(int(orig_x), y, int(x_), y, Color::language);
348 void RowPainter::paintFromPos(pos_type & vpos)
350 pos_type const pos = bidi_.vis2log(vpos);
351 Font orig_font = text_metrics_.getDisplayFont(pit_, pos);
352 double const orig_x = x_;
354 // usual characters, no insets
355 char_type const c = par_.getChar(pos);
357 // special case languages
358 std::string const & lang = orig_font.language()->lang();
359 bool const hebrew = lang == "hebrew";
360 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" ||
363 // draw as many chars as we can
364 if ((!hebrew && !arabic)
365 || (hebrew && !Encodings::isComposeChar_hebrew(c))
366 || (arabic && !Encodings::isComposeChar_arabic(c))) {
367 paintChars(vpos, orig_font, hebrew, arabic);
369 paintHebrewComposeChar(vpos, orig_font);
371 paintArabicComposeChar(vpos, orig_font);
374 paintForeignMark(orig_x, orig_font);
378 void RowPainter::paintChangeBar()
380 pos_type const start = row_.pos();
381 pos_type end = row_.endpos();
383 if (par_.size() == end) {
384 // this is the last row of the paragraph;
385 // thus, we must also consider the imaginary end-of-par character
389 if (start == end || !par_.isChanged(start, end))
392 int const height = text_metrics_.isLastRow(pit_, row_)
396 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color::changebar);
400 void RowPainter::paintAppendix()
402 // only draw the appendix frame once (for the main text)
403 if (!par_.params().appendix() || !text_.isMainText(pi_.base.bv->buffer()))
406 int y = yo_ - row_.ascent();
408 if (par_.params().startOfAppendix())
409 y += 2 * defaultRowHeight();
411 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color::appendix);
412 pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color::appendix);
416 void RowPainter::paintDepthBar()
418 depth_type const depth = par_.getDepth();
423 depth_type prev_depth = 0;
424 if (!text_metrics_.isFirstRow(pit_, row_)) {
425 pit_type pit2 = pit_;
428 prev_depth = pars_[pit2].getDepth();
431 depth_type next_depth = 0;
432 if (!text_metrics_.isLastRow(pit_, row_)) {
433 pit_type pit2 = pit_;
434 if (row_.endpos() >= pars_[pit2].size())
436 next_depth = pars_[pit2].getDepth();
439 for (depth_type i = 1; i <= depth; ++i) {
440 int const w = nestMargin() / 5;
441 int x = int(xo_) + w * i;
442 // only consider the changebar space if we're drawing outermost text
443 if (text_.isMainText(pi_.base.bv->buffer()))
444 x += changebarMargin();
446 int const starty = yo_ - row_.ascent();
447 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
449 pi_.pain.line(x, starty, x, starty + h, Color::depthbar);
452 pi_.pain.fillRectangle(x, starty, w, 2, Color::depthbar);
454 pi_.pain.fillRectangle(x, starty + h, w, 2, Color::depthbar);
459 int RowPainter::paintAppendixStart(int y)
462 pb_font.setColor(Color::appendix);
469 docstring const label = _("Appendix");
470 theFontMetrics(pb_font).rectText(label, w, a, d);
472 int const text_start = int(xo_ + (width_ - w) / 2);
473 int const text_end = text_start + w;
475 pi_.pain.rectText(text_start, y + d, label, pb_font, Color::none, Color::none);
477 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color::appendix);
478 pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color::appendix);
480 return 3 * defaultRowHeight();
484 void RowPainter::paintFirst()
486 ParagraphParameters const & parparams = par_.params();
490 // start of appendix?
491 if (parparams.startOfAppendix())
492 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
494 Buffer const & buffer = pi_.base.bv->buffer();
496 LayoutPtr const & layout = par_.layout();
498 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
500 if (layout->latextype == LATEX_PARAGRAPH
501 && !par_.getDepth()) {
502 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
504 LayoutPtr const & playout = pars_[pit_ - 1].layout();
505 if (playout->latextype == LATEX_PARAGRAPH
506 && !pars_[pit_ - 1].getDepth()) {
507 // is it right to use defskip here, too? (AS)
508 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
514 bool const is_rtl = text_.isRTL(buffer, par_);
515 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
516 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
518 // should we print a label?
519 if (layout->labeltype >= LABEL_STATIC
520 && (layout->labeltype != LABEL_STATIC
521 || layout->latextype != LATEX_ENVIRONMENT
524 Font const font = getLabelFont();
525 FontMetrics const & fm = theFontMetrics(font);
527 docstring const str = par_.getLabelstring();
531 // this is special code for the chapter layout. This is
532 // printed in an extra row and has a pagebreak at
534 if (layout->counter == "chapter") {
535 double spacing_val = 1.0;
536 if (!parparams.spacing().isDefault()) {
537 spacing_val = parparams.spacing().getValue();
539 spacing_val = buffer.params().spacing().getValue();
542 int const labeladdon = int(fm.maxHeight() * layout->spacing.getValue() * spacing_val);
544 int const maxdesc = int(fm.maxDescent() * layout->spacing.getValue() * spacing_val)
545 + int(layout->parsep) * defaultRowHeight();
548 x = width_ - leftMargin() -
552 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
555 x = width_ - leftMargin()
556 + fm.width(layout->labelsep);
558 x = x_ - fm.width(layout->labelsep)
562 pi_.pain.text(int(x), yo_, str, font);
566 // the labels at the top of an environment.
567 // More or less for bibliography
569 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
570 layout->labeltype == LABEL_BIBLIO ||
571 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
572 Font font = getLabelFont();
573 if (!par_.getLabelstring().empty()) {
574 docstring const str = par_.getLabelstring();
575 double spacing_val = 1.0;
576 if (!parparams.spacing().isDefault())
577 spacing_val = parparams.spacing().getValue();
579 spacing_val = buffer.params().spacing().getValue();
581 FontMetrics const & fm = theFontMetrics(font);
583 int const labeladdon = int(fm.maxHeight()
584 * layout->spacing.getValue() * spacing_val);
587 int(fm.maxDescent() * layout->spacing.getValue() * spacing_val
588 + (layout->labelbottomsep * defaultRowHeight()));
591 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
594 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
595 x -= fm.width(str) / 2;
597 x = width_ - leftMargin() - fm.width(str);
599 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
605 void RowPainter::paintLast()
607 bool const is_rtl = text_.isRTL(pi_.base.bv->buffer(), par_);
608 int const endlabel = getEndLabel(pit_, text_.paragraphs());
610 // paint imaginary end-of-paragraph character
612 if (par_.isInserted(par_.size()) || par_.isDeleted(par_.size())) {
613 FontMetrics const & fm = theFontMetrics(pi_.base.bv->buffer().params().getFont());
614 int const length = fm.maxAscent() / 2;
615 Color::color col = par_.isInserted(par_.size()) ? Color::addedtext : Color::deletedtext;
617 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
618 Painter::line_solid, Painter::line_thick);
619 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1, yo_ + 2, col,
620 Painter::line_solid, Painter::line_thick);
627 case END_LABEL_FILLED_BOX: {
628 Font const font = getLabelFont();
629 FontMetrics const & fm = theFontMetrics(font);
630 int const size = int(0.75 * fm.maxAscent());
631 int const y = yo_ - size;
632 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
634 if (width_ - int(row_.width()) <= size)
635 x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
637 if (endlabel == END_LABEL_BOX)
638 pi_.pain.rectangle(x, y, size, size, Color::eolmarker);
640 pi_.pain.fillRectangle(x, y, size, size, Color::eolmarker);
644 case END_LABEL_STATIC: {
645 Font font = getLabelFont();
646 FontMetrics const & fm = theFontMetrics(font);
647 docstring const & str = par_.layout()->endlabelstring();
648 double const x = is_rtl ?
650 : - text_metrics_.rightMargin(pm_) - row_.width();
651 pi_.pain.text(int(x), yo_, str, font);
655 case END_LABEL_NO_LABEL:
661 void RowPainter::paintOnlyInsets()
663 pos_type const end = row_.endpos();
664 for (pos_type pos = row_.pos(); pos != end; ++pos) {
665 if (!par_.isInset(pos))
668 // If outer row has changed, nested insets are repaint completely.
669 Inset const * inset = par_.getInset(pos);
671 if (x_ > pi_.base.bv->workWidth())
674 x_ = pi_.base.bv->coordCache().getInsets().x(inset);
675 paintInset(inset, pos);
680 void RowPainter::paintText()
682 pos_type const end = row_.endpos();
683 // Spaces at logical line breaks in bidi text must be skipped during
684 // painting. However, they may appear visually in the middle
685 // of a row; they must be skipped, wherever they are...
686 // * logically "abc_[HEBREW_\nHEBREW]"
687 // * visually "abc_[_WERBEH\nWERBEH]"
688 pos_type skipped_sep_vpos = -1;
689 pos_type body_pos = par_.beginOfBody();
691 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
695 LayoutPtr const & layout = par_.layout();
697 bool running_strikeout = false;
698 bool is_struckout = false;
699 int last_strikeout_x = 0;
701 // Use font span to speed things up, see below
705 // If the last logical character is a separator, don't paint it, unless
706 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
707 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
708 skipped_sep_vpos = bidi_.log2vis(end - 1);
710 for (pos_type vpos = row_.pos(); vpos < end; ) {
711 if (x_ > pi_.base.bv->workWidth())
714 // Skip the separator at the logical end of the row
715 if (vpos == skipped_sep_vpos) {
720 pos_type const pos = bidi_.vis2log(vpos);
722 if (pos >= par_.size()) {
727 // Use font span to speed things up, see above
728 if (vpos < font_span.first || vpos > font_span.last) {
729 font_span = par_.fontSpan(vpos);
730 font = text_metrics_.getDisplayFont(pit_, vpos);
733 const int width_pos = pm_.singleWidth(pos, font);
735 if (x_ + width_pos < 0) {
741 is_struckout = par_.isDeleted(pos);
743 if (is_struckout && !running_strikeout) {
744 running_strikeout = true;
745 last_strikeout_x = int(x_);
748 bool const highly_editable_inset = par_.isInset(pos)
749 && isHighlyEditableInset(par_.getInset(pos));
751 // If we reach the end of a struck out range, paint it.
752 // We also don't paint across things like tables
753 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
754 // Calculate 1/3 height of the buffer's default font
755 FontMetrics const & fm
756 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
757 int const middle = yo_ - fm.maxAscent() / 3;
758 pi_.pain.line(last_strikeout_x, middle, int(x_), middle,
759 Color::deletedtext, Painter::line_solid, Painter::line_thin);
760 running_strikeout = false;
763 if (body_pos > 0 && pos == body_pos - 1) {
764 int const lwidth = theFontMetrics(getLabelFont())
765 .width(layout->labelsep);
767 x_ += row_.label_hfill + lwidth - width_pos;
770 if (par_.isHfill(pos)) {
771 paintHfill(pos, body_pos);
774 } else if (par_.isSeparator(pos)) {
775 Font orig_font = text_metrics_.getDisplayFont(pit_, pos);
776 double const orig_x = x_;
779 x_ += row_.separator;
780 paintForeignMark(orig_x, orig_font);
783 } else if (par_.isInset(pos)) {
784 // If outer row has changed, nested insets are repaint completely.
785 Inset const * inset = par_.getInset(pos);
786 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
787 paintInset(inset, pos);
791 // paint as many characters as possible.
796 // if we reach the end of a struck out range, paint it
797 if (running_strikeout) {
798 // calculate 1/3 height of the buffer's default font
799 FontMetrics const & fm
800 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
801 int const middle = yo_ - fm.maxAscent() / 3;
802 pi_.pain.line(last_strikeout_x, middle, int(x_), middle,
803 Color::deletedtext, Painter::line_solid, Painter::line_thin);
804 running_strikeout = false;