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 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
137 // insets are painted completely. Recursive
138 inset->drawSelection(pi_, int(x_), yo_);
139 inset->draw(pi_, int(x_), yo_);
141 Dimension const & dim = pm_.insetDimension(inset);
143 paintForeignMark(x_, font, dim.descent());
148 int const x1 = int(x_ - dim.width());
150 BOOST_ASSERT(max_witdh_ > 0);
151 int right_margin = text_metrics_.rightMargin(pm_);
152 int const w = max_witdh_ - leftMargin() - right_margin;
153 MetricsInfo mi(pi_.base.bv, font, w);
154 inset->metrics(mi, dim2);
155 if (dim.wid != dim2.wid)
156 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
157 << " draw width " << dim.width()
158 << "> metrics width " << dim2.wid << "." << std::endl;
159 if (dim->asc != dim2.asc)
160 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
161 << " draw ascent " << dim.ascent()
162 << "> metrics ascent " << dim2.asc << "." << std::endl;
163 if (dim2.descent() != dim.des)
164 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
165 << " draw ascent " << dim.descent()
166 << "> metrics descent " << dim2.des << "." << std::endl;
167 BOOST_ASSERT(dim2.wid == dim.wid);
168 BOOST_ASSERT(dim2.asc == dim.asc);
169 BOOST_ASSERT(dim2.des == dim.des);
170 int const x2 = x1 + dim.wid;
171 int const y1 = yo_ + dim.des;
172 int const y2 = yo_ - dim.asc;
173 pi_.pain.line(x1, y1, x1, y2, Color::green);
174 pi_.pain.line(x1, y1, x2, y1, Color::green);
175 pi_.pain.line(x2, y1, x2, y2, Color::green);
176 pi_.pain.line(x1, y2, x2, y2, Color::green);
181 void RowPainter::paintHebrewComposeChar(pos_type & vpos, Font const & font)
183 pos_type pos = bidi_.vis2log(vpos);
188 char_type c = par_.getChar(pos);
192 int const width = theFontMetrics(font).width(c);
195 for (pos_type i = pos - 1; i >= 0; --i) {
197 if (!Encodings::isComposeChar_hebrew(c)) {
198 if (isPrintableNonspace(c)) {
199 int const width2 = pm_.singleWidth(i,
200 text_metrics_.getDisplayFont(pit_, i));
201 dx = (c == 0x05e8 || // resh
202 c == 0x05d3) // dalet
204 : (width2 - width) / 2;
211 pi_.pain.text(int(x_) + dx, yo_, str, font);
215 void RowPainter::paintArabicComposeChar(pos_type & vpos, Font const & font)
217 pos_type pos = bidi_.vis2log(vpos);
221 char_type c = par_.getChar(pos);
222 c = par_.transformChar(c, pos);
226 int const width = theFontMetrics(font).width(c);
229 for (pos_type i = pos - 1; i >= 0; --i) {
231 if (!Encodings::isComposeChar_arabic(c)) {
232 if (isPrintableNonspace(c)) {
233 int const width2 = pm_.singleWidth(i,
234 text_metrics_.getDisplayFont(pit_, i));
235 dx = (width2 - width) / 2;
241 pi_.pain.text(int(x_) + dx, yo_, str, font);
245 void RowPainter::paintChars(pos_type & vpos, Font const & font,
246 bool hebrew, bool arabic)
248 // This method takes up 70% of time when typing
249 pos_type pos = bidi_.vis2log(vpos);
250 pos_type const end = row_.endpos();
251 FontSpan const font_span = par_.fontSpan(pos);
252 Change::Type const prev_change = par_.lookupChange(pos).type;
255 std::vector<char_type> str;
257 str.push_back(par_.getChar(pos));
260 char_type c = str[0];
265 str[0] = par_.transformChar(c, pos);
268 // collect as much similar chars as we can
269 for (++vpos ; vpos < end ; ++vpos) {
270 pos = bidi_.vis2log(vpos);
271 if (pos < font_span.first || pos > font_span.last)
274 if (prev_change != par_.lookupChange(pos).type)
277 char_type c = par_.getChar(pos);
279 if (!isPrintableNonspace(c))
282 /* Because we do our own bidi, at this point the strings are
283 * already in visual order. However, Qt also applies its own
284 * bidi algorithm to strings that it paints to the screen.
285 * Therefore, if we were to paint Hebrew/Arabic words as a
286 * single string, the letters in the words would get reversed
287 * again. In order to avoid that, we don't collect Hebrew/
288 * Arabic characters, but rather paint them one at a time.
289 * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
294 /* FIXME: these checks are irrelevant, since 'arabic' and
295 * 'hebrew' alone are already going to trigger a break.
296 * However, this should not be removed completely, because
297 * if an alternative solution is found which allows grouping
298 * of arabic and hebrew characters, then these breaks may have
301 if (arabic && Encodings::isComposeChar_arabic(c))
304 if (hebrew && Encodings::isComposeChar_hebrew(c))
313 c = par_.transformChar(c, pos);
314 /* see comment in hebrew, explaining why we break */
321 docstring s(&str[0], str.size());
323 if (prev_change != Change::UNCHANGED) {
325 if (prev_change == Change::DELETED) {
326 copy.setColor(Color::deletedtext);
327 } else if (prev_change == Change::INSERTED) {
328 copy.setColor(Color::addedtext);
330 x_ += pi_.pain.text(int(x_), yo_, s, copy);
332 x_ += pi_.pain.text(int(x_), yo_, s, font);
337 void RowPainter::paintForeignMark(double orig_x, Font const & font, int desc)
339 if (!lyxrc.mark_foreign_language)
341 if (font.language() == latex_language)
343 if (font.language() == pi_.base.bv->buffer().params().language)
346 int const y = yo_ + 1 + desc;
347 pi_.pain.line(int(orig_x), y, int(x_), y, Color::language);
351 void RowPainter::paintFromPos(pos_type & vpos)
353 pos_type const pos = bidi_.vis2log(vpos);
354 Font orig_font = text_metrics_.getDisplayFont(pit_, pos);
355 double const orig_x = x_;
357 // usual characters, no insets
358 char_type const c = par_.getChar(pos);
360 // special case languages
361 std::string const & lang = orig_font.language()->lang();
362 bool const hebrew = lang == "hebrew";
363 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" ||
366 // draw as many chars as we can
367 if ((!hebrew && !arabic)
368 || (hebrew && !Encodings::isComposeChar_hebrew(c))
369 || (arabic && !Encodings::isComposeChar_arabic(c))) {
370 paintChars(vpos, orig_font, hebrew, arabic);
372 paintHebrewComposeChar(vpos, orig_font);
374 paintArabicComposeChar(vpos, orig_font);
377 paintForeignMark(orig_x, orig_font);
381 void RowPainter::paintChangeBar()
383 pos_type const start = row_.pos();
384 pos_type end = row_.endpos();
386 if (par_.size() == end) {
387 // this is the last row of the paragraph;
388 // thus, we must also consider the imaginary end-of-par character
392 if (start == end || !par_.isChanged(start, end))
395 int const height = text_metrics_.isLastRow(pit_, row_)
399 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color::changebar);
403 void RowPainter::paintAppendix()
405 // only draw the appendix frame once (for the main text)
406 if (!par_.params().appendix() || !text_.isMainText(pi_.base.bv->buffer()))
409 int y = yo_ - row_.ascent();
411 if (par_.params().startOfAppendix())
412 y += 2 * defaultRowHeight();
414 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color::appendix);
415 pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color::appendix);
419 void RowPainter::paintDepthBar()
421 depth_type const depth = par_.getDepth();
426 depth_type prev_depth = 0;
427 if (!text_metrics_.isFirstRow(pit_, row_)) {
428 pit_type pit2 = pit_;
431 prev_depth = pars_[pit2].getDepth();
434 depth_type next_depth = 0;
435 if (!text_metrics_.isLastRow(pit_, row_)) {
436 pit_type pit2 = pit_;
437 if (row_.endpos() >= pars_[pit2].size())
439 next_depth = pars_[pit2].getDepth();
442 for (depth_type i = 1; i <= depth; ++i) {
443 int const w = nestMargin() / 5;
444 int x = int(xo_) + w * i;
445 // only consider the changebar space if we're drawing outermost text
446 if (text_.isMainText(pi_.base.bv->buffer()))
447 x += changebarMargin();
449 int const starty = yo_ - row_.ascent();
450 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
452 pi_.pain.line(x, starty, x, starty + h, Color::depthbar);
455 pi_.pain.fillRectangle(x, starty, w, 2, Color::depthbar);
457 pi_.pain.fillRectangle(x, starty + h, w, 2, Color::depthbar);
462 int RowPainter::paintAppendixStart(int y)
465 pb_font.setColor(Color::appendix);
472 docstring const label = _("Appendix");
473 theFontMetrics(pb_font).rectText(label, w, a, d);
475 int const text_start = int(xo_ + (width_ - w) / 2);
476 int const text_end = text_start + w;
478 pi_.pain.rectText(text_start, y + d, label, pb_font, Color::none, Color::none);
480 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color::appendix);
481 pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color::appendix);
483 return 3 * defaultRowHeight();
487 void RowPainter::paintFirst()
489 ParagraphParameters const & parparams = par_.params();
493 // start of appendix?
494 if (parparams.startOfAppendix())
495 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
497 Buffer const & buffer = pi_.base.bv->buffer();
499 LayoutPtr const & layout = par_.layout();
501 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
503 if (layout->latextype == LATEX_PARAGRAPH
504 && !par_.getDepth()) {
505 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
507 LayoutPtr const & playout = pars_[pit_ - 1].layout();
508 if (playout->latextype == LATEX_PARAGRAPH
509 && !pars_[pit_ - 1].getDepth()) {
510 // is it right to use defskip here, too? (AS)
511 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
517 bool const is_rtl = text_.isRTL(buffer, par_);
518 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
519 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
521 // should we print a label?
522 if (layout->labeltype >= LABEL_STATIC
523 && (layout->labeltype != LABEL_STATIC
524 || layout->latextype != LATEX_ENVIRONMENT
527 Font const font = getLabelFont();
528 FontMetrics const & fm = theFontMetrics(font);
530 docstring const str = par_.getLabelstring();
534 // this is special code for the chapter layout. This is
535 // printed in an extra row and has a pagebreak at
537 if (layout->counter == "chapter") {
538 double spacing_val = 1.0;
539 if (!parparams.spacing().isDefault()) {
540 spacing_val = parparams.spacing().getValue();
542 spacing_val = buffer.params().spacing().getValue();
545 int const labeladdon = int(fm.maxHeight() * layout->spacing.getValue() * spacing_val);
547 int const maxdesc = int(fm.maxDescent() * layout->spacing.getValue() * spacing_val)
548 + int(layout->parsep) * defaultRowHeight();
551 x = width_ - leftMargin() -
555 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
558 x = width_ - leftMargin()
559 + fm.width(layout->labelsep);
561 x = x_ - fm.width(layout->labelsep)
565 pi_.pain.text(int(x), yo_, str, font);
569 // the labels at the top of an environment.
570 // More or less for bibliography
572 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
573 layout->labeltype == LABEL_BIBLIO ||
574 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
575 Font font = getLabelFont();
576 if (!par_.getLabelstring().empty()) {
577 docstring const str = par_.getLabelstring();
578 double spacing_val = 1.0;
579 if (!parparams.spacing().isDefault())
580 spacing_val = parparams.spacing().getValue();
582 spacing_val = buffer.params().spacing().getValue();
584 FontMetrics const & fm = theFontMetrics(font);
586 int const labeladdon = int(fm.maxHeight()
587 * layout->spacing.getValue() * spacing_val);
590 int(fm.maxDescent() * layout->spacing.getValue() * spacing_val
591 + (layout->labelbottomsep * defaultRowHeight()));
594 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
597 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
598 x -= fm.width(str) / 2;
600 x = width_ - leftMargin() - fm.width(str);
602 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
608 void RowPainter::paintLast()
610 bool const is_rtl = text_.isRTL(pi_.base.bv->buffer(), par_);
611 int const endlabel = getEndLabel(pit_, text_.paragraphs());
613 // paint imaginary end-of-paragraph character
615 if (par_.isInserted(par_.size()) || par_.isDeleted(par_.size())) {
616 FontMetrics const & fm = theFontMetrics(pi_.base.bv->buffer().params().getFont());
617 int const length = fm.maxAscent() / 2;
618 Color::color col = par_.isInserted(par_.size()) ? Color::addedtext : Color::deletedtext;
620 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
621 Painter::line_solid, Painter::line_thick);
622 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1, yo_ + 2, col,
623 Painter::line_solid, Painter::line_thick);
630 case END_LABEL_FILLED_BOX: {
631 Font const font = getLabelFont();
632 FontMetrics const & fm = theFontMetrics(font);
633 int const size = int(0.75 * fm.maxAscent());
634 int const y = yo_ - size;
635 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
637 if (width_ - int(row_.width()) <= size)
638 x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
640 if (endlabel == END_LABEL_BOX)
641 pi_.pain.rectangle(x, y, size, size, Color::eolmarker);
643 pi_.pain.fillRectangle(x, y, size, size, Color::eolmarker);
647 case END_LABEL_STATIC: {
648 Font font = getLabelFont();
649 FontMetrics const & fm = theFontMetrics(font);
650 docstring const & str = par_.layout()->endlabelstring();
651 double const x = is_rtl ?
653 : - text_metrics_.rightMargin(pm_) - row_.width();
654 pi_.pain.text(int(x), yo_, str, font);
658 case END_LABEL_NO_LABEL:
664 void RowPainter::paintOnlyInsets()
666 pos_type const end = row_.endpos();
667 for (pos_type pos = row_.pos(); pos != end; ++pos) {
668 if (!par_.isInset(pos))
671 // If outer row has changed, nested insets are repaint completely.
672 Inset const * inset = par_.getInset(pos);
674 if (x_ > pi_.base.bv->workWidth())
677 x_ = pi_.base.bv->coordCache().getInsets().x(inset);
678 paintInset(inset, pos);
683 void RowPainter::paintText()
685 pos_type const end = row_.endpos();
686 // Spaces at logical line breaks in bidi text must be skipped during
687 // painting. However, they may appear visually in the middle
688 // of a row; they must be skipped, wherever they are...
689 // * logically "abc_[HEBREW_\nHEBREW]"
690 // * visually "abc_[_WERBEH\nWERBEH]"
691 pos_type skipped_sep_vpos = -1;
692 pos_type body_pos = par_.beginOfBody();
694 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
698 LayoutPtr const & layout = par_.layout();
700 bool running_strikeout = false;
701 bool is_struckout = false;
702 int last_strikeout_x = 0;
704 // Use font span to speed things up, see below
708 // If the last logical character is a separator, don't paint it, unless
709 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
710 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
711 skipped_sep_vpos = bidi_.log2vis(end - 1);
713 for (pos_type vpos = row_.pos(); vpos < end; ) {
714 if (x_ > pi_.base.bv->workWidth())
717 // Skip the separator at the logical end of the row
718 if (vpos == skipped_sep_vpos) {
723 pos_type const pos = bidi_.vis2log(vpos);
725 if (pos >= par_.size()) {
730 // Use font span to speed things up, see above
731 if (vpos < font_span.first || vpos > font_span.last) {
732 font_span = par_.fontSpan(vpos);
733 font = text_metrics_.getDisplayFont(pit_, vpos);
736 const int width_pos = pm_.singleWidth(pos, font);
738 if (x_ + width_pos < 0) {
744 is_struckout = par_.isDeleted(pos);
746 if (is_struckout && !running_strikeout) {
747 running_strikeout = true;
748 last_strikeout_x = int(x_);
751 bool const highly_editable_inset = par_.isInset(pos)
752 && isHighlyEditableInset(par_.getInset(pos));
754 // If we reach the end of a struck out range, paint it.
755 // We also don't paint across things like tables
756 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
757 // Calculate 1/3 height of the buffer's default font
758 FontMetrics const & fm
759 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
760 int const middle = yo_ - fm.maxAscent() / 3;
761 pi_.pain.line(last_strikeout_x, middle, int(x_), middle,
762 Color::deletedtext, Painter::line_solid, Painter::line_thin);
763 running_strikeout = false;
766 if (body_pos > 0 && pos == body_pos - 1) {
767 int const lwidth = theFontMetrics(getLabelFont())
768 .width(layout->labelsep);
770 x_ += row_.label_hfill + lwidth - width_pos;
773 if (par_.isHfill(pos)) {
774 paintHfill(pos, body_pos);
777 } else if (par_.isSeparator(pos)) {
778 Font orig_font = text_metrics_.getDisplayFont(pit_, pos);
779 double const orig_x = x_;
782 x_ += row_.separator;
783 paintForeignMark(orig_x, orig_font);
786 } else if (par_.isInset(pos)) {
787 // If outer row has changed, nested insets are repaint completely.
788 Inset const * inset = par_.getInset(pos);
789 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
790 paintInset(inset, pos);
794 // paint as many characters as possible.
799 // if we reach the end of a struck out range, paint it
800 if (running_strikeout) {
801 // calculate 1/3 height of the buffer's default font
802 FontMetrics const & fm
803 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
804 int const middle = yo_ - fm.maxAscent() / 3;
805 pi_.pain.line(last_strikeout_x, middle, int(x_), middle,
806 Color::deletedtext, Painter::line_solid, Painter::line_thin);
807 running_strikeout = false;