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"
22 #include "BufferParams.h"
23 #include "BufferView.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>
54 using frontend::Painter;
55 using frontend::FontMetrics;
57 RowPainter::RowPainter(PainterInfo & pi,
58 Text const & text, pit_type pit, Row const & row, Bidi & bidi, int x, int y)
59 : pi_(pi), text_(text),
60 text_metrics_(pi_.base.bv->textMetrics(&text)),
61 pars_(text.paragraphs()),
62 row_(row), pit_(pit), par_(text.paragraphs()[pit]),
63 pm_(text_metrics_.parMetrics(pit)),
64 bidi_(bidi), erased_(pi_.erased_),
65 xo_(x), yo_(y), width_(text_metrics_.width())
67 bidi_.computeTables(par_, pi_.base.bv->buffer(), row_);
70 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
73 BOOST_ASSERT(pit >= 0);
74 BOOST_ASSERT(pit < int(text.paragraphs().size()));
78 Font const RowPainter::getLabelFont() const
80 return text_.getLabelFont(pi_.base.bv->buffer(), par_);
84 int RowPainter::leftMargin() const
86 return text_metrics_.leftMargin(text_metrics_.width(), pit_,
91 void RowPainter::paintHfill(pos_type const pos, pos_type const body_pos)
96 int const y1 = y0 - defaultRowHeight() / 2;
98 pi_.pain.line(int(x_), y1, int(x_), y0, Color::added_space);
100 if (pm_.hfillExpansion(row_, pos)) {
101 int const y2 = (y0 + y1) / 2;
103 if (pos >= body_pos) {
104 pi_.pain.line(int(x_), y2, int(x_ + row_.hfill), y2,
106 Painter::line_onoffdash);
109 pi_.pain.line(int(x_), y2, int(x_ + row_.label_hfill), y2,
111 Painter::line_onoffdash);
112 x_ += row_.label_hfill;
114 pi_.pain.line(int(x_), y1, int(x_), y0, Color::added_space);
120 // If you want to debug inset metrics uncomment the following line:
121 //#define DEBUG_METRICS
122 // This draws green lines around each inset.
125 void RowPainter::paintInset(Inset const * inset, pos_type const pos)
127 Font font = text_metrics_.getDisplayFont(pit_, pos);
130 // FIXME: We should always use font, see documentation of
131 // noFontChange() in Inset.h.
132 pi_.base.font = inset->noFontChange() ?
133 pi_.base.bv->buffer().params().getFont() :
135 pi_.ltr_pos = (bidi_.level(pos) % 2 == 0);
136 pi_.erased_ = erased_ || par_.isDeleted(pos);
137 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
138 // insets are painted completely. Recursive
139 inset->drawSelection(pi_, int(x_), yo_);
140 inset->draw(pi_, int(x_), yo_);
142 Dimension const & dim = pm_.insetDimension(inset);
144 paintForeignMark(x_, font, dim.descent());
149 int const x1 = int(x_ - dim.width());
151 BOOST_ASSERT(max_witdh_ > 0);
152 int right_margin = text_metrics_.rightMargin(pm_);
153 int const w = max_witdh_ - leftMargin() - right_margin;
154 MetricsInfo mi(pi_.base.bv, font, w);
155 inset->metrics(mi, dim2);
156 if (dim.wid != dim2.wid)
157 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
158 << " draw width " << dim.width()
159 << "> metrics width " << dim2.wid << "." << std::endl;
160 if (dim->asc != dim2.asc)
161 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
162 << " draw ascent " << dim.ascent()
163 << "> metrics ascent " << dim2.asc << "." << std::endl;
164 if (dim2.descent() != dim.des)
165 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
166 << " draw ascent " << dim.descent()
167 << "> metrics descent " << dim2.des << "." << std::endl;
168 BOOST_ASSERT(dim2.wid == dim.wid);
169 BOOST_ASSERT(dim2.asc == dim.asc);
170 BOOST_ASSERT(dim2.des == dim.des);
171 int const x2 = x1 + dim.wid;
172 int const y1 = yo_ + dim.des;
173 int const y2 = yo_ - dim.asc;
174 pi_.pain.line(x1, y1, x1, y2, Color::green);
175 pi_.pain.line(x1, y1, x2, y1, Color::green);
176 pi_.pain.line(x2, y1, x2, y2, Color::green);
177 pi_.pain.line(x1, y2, x2, y2, Color::green);
182 void RowPainter::paintHebrewComposeChar(pos_type & vpos, Font const & font)
184 pos_type pos = bidi_.vis2log(vpos);
189 char_type c = par_.getChar(pos);
193 int const width = theFontMetrics(font).width(c);
196 for (pos_type i = pos - 1; i >= 0; --i) {
198 if (!Encodings::isComposeChar_hebrew(c)) {
199 if (isPrintableNonspace(c)) {
200 int const width2 = pm_.singleWidth(i,
201 text_metrics_.getDisplayFont(pit_, i));
202 dx = (c == 0x05e8 || // resh
203 c == 0x05d3) // dalet
205 : (width2 - width) / 2;
212 pi_.pain.text(int(x_) + dx, yo_, str, font);
216 void RowPainter::paintArabicComposeChar(pos_type & vpos, Font const & font)
218 pos_type pos = bidi_.vis2log(vpos);
222 char_type c = par_.getChar(pos);
223 c = par_.transformChar(c, pos);
227 int const width = theFontMetrics(font).width(c);
230 for (pos_type i = pos - 1; i >= 0; --i) {
232 if (!Encodings::isComposeChar_arabic(c)) {
233 if (isPrintableNonspace(c)) {
234 int const width2 = pm_.singleWidth(i,
235 text_metrics_.getDisplayFont(pit_, i));
236 dx = (width2 - width) / 2;
242 pi_.pain.text(int(x_) + dx, yo_, str, font);
246 void RowPainter::paintChars(pos_type & vpos, Font const & font,
247 bool hebrew, bool arabic)
249 // This method takes up 70% of time when typing
250 pos_type pos = bidi_.vis2log(vpos);
251 pos_type const end = row_.endpos();
252 FontSpan const font_span = par_.fontSpan(pos);
253 Change::Type const prev_change = par_.lookupChange(pos).type;
256 std::vector<char_type> str;
258 str.push_back(par_.getChar(pos));
261 char_type c = str[0];
266 str[0] = par_.transformChar(c, pos);
269 // collect as much similar chars as we can
270 for (++vpos ; vpos < end ; ++vpos) {
271 pos = bidi_.vis2log(vpos);
272 if (pos < font_span.first || pos > font_span.last)
275 if (prev_change != par_.lookupChange(pos).type)
278 char_type c = par_.getChar(pos);
280 if (!isPrintableNonspace(c))
283 /* Because we do our own bidi, at this point the strings are
284 * already in visual order. However, Qt also applies its own
285 * bidi algorithm to strings that it paints to the screen.
286 * Therefore, if we were to paint Hebrew/Arabic words as a
287 * single string, the letters in the words would get reversed
288 * again. In order to avoid that, we don't collect Hebrew/
289 * Arabic characters, but rather paint them one at a time.
290 * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
295 /* FIXME: these checks are irrelevant, since 'arabic' and
296 * 'hebrew' alone are already going to trigger a break.
297 * However, this should not be removed completely, because
298 * if an alternative solution is found which allows grouping
299 * of arabic and hebrew characters, then these breaks may have
302 if (arabic && Encodings::isComposeChar_arabic(c))
305 if (hebrew && Encodings::isComposeChar_hebrew(c))
314 c = par_.transformChar(c, pos);
315 /* see comment in hebrew, explaining why we break */
322 docstring s(&str[0], str.size());
324 if (prev_change != Change::UNCHANGED) {
326 if (prev_change == Change::DELETED) {
327 copy.setColor(Color::deletedtext);
328 } else if (prev_change == Change::INSERTED) {
329 copy.setColor(Color::addedtext);
331 x_ += pi_.pain.text(int(x_), yo_, s, copy);
333 x_ += pi_.pain.text(int(x_), yo_, s, font);
338 void RowPainter::paintForeignMark(double orig_x, Font const & font, int desc)
340 if (!lyxrc.mark_foreign_language)
342 if (font.language() == latex_language)
344 if (font.language() == pi_.base.bv->buffer().params().language)
347 int const y = yo_ + 1 + desc;
348 pi_.pain.line(int(orig_x), y, int(x_), y, Color::language);
352 void RowPainter::paintFromPos(pos_type & vpos)
354 pos_type const pos = bidi_.vis2log(vpos);
355 Font orig_font = text_metrics_.getDisplayFont(pit_, pos);
356 double const orig_x = x_;
358 // usual characters, no insets
359 char_type const c = par_.getChar(pos);
361 // special case languages
362 std::string const & lang = orig_font.language()->lang();
363 bool const hebrew = lang == "hebrew";
364 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" ||
367 // draw as many chars as we can
368 if ((!hebrew && !arabic)
369 || (hebrew && !Encodings::isComposeChar_hebrew(c))
370 || (arabic && !Encodings::isComposeChar_arabic(c))) {
371 paintChars(vpos, orig_font, hebrew, arabic);
373 paintHebrewComposeChar(vpos, orig_font);
375 paintArabicComposeChar(vpos, orig_font);
378 paintForeignMark(orig_x, orig_font);
382 void RowPainter::paintChangeBar()
384 pos_type const start = row_.pos();
385 pos_type end = row_.endpos();
387 if (par_.size() == end) {
388 // this is the last row of the paragraph;
389 // thus, we must also consider the imaginary end-of-par character
393 if (start == end || !par_.isChanged(start, end))
396 int const height = text_metrics_.isLastRow(pit_, row_)
400 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color::changebar);
404 void RowPainter::paintAppendix()
406 // only draw the appendix frame once (for the main text)
407 if (!par_.params().appendix() || !text_.isMainText(pi_.base.bv->buffer()))
410 int y = yo_ - row_.ascent();
412 if (par_.params().startOfAppendix())
413 y += 2 * defaultRowHeight();
415 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color::appendix);
416 pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color::appendix);
420 void RowPainter::paintDepthBar()
422 depth_type const depth = par_.getDepth();
427 depth_type prev_depth = 0;
428 if (!text_metrics_.isFirstRow(pit_, row_)) {
429 pit_type pit2 = pit_;
432 prev_depth = pars_[pit2].getDepth();
435 depth_type next_depth = 0;
436 if (!text_metrics_.isLastRow(pit_, row_)) {
437 pit_type pit2 = pit_;
438 if (row_.endpos() >= pars_[pit2].size())
440 next_depth = pars_[pit2].getDepth();
443 for (depth_type i = 1; i <= depth; ++i) {
444 int const w = nestMargin() / 5;
445 int x = int(xo_) + w * i;
446 // only consider the changebar space if we're drawing outermost text
447 if (text_.isMainText(pi_.base.bv->buffer()))
448 x += changebarMargin();
450 int const starty = yo_ - row_.ascent();
451 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
453 pi_.pain.line(x, starty, x, starty + h, Color::depthbar);
456 pi_.pain.fillRectangle(x, starty, w, 2, Color::depthbar);
458 pi_.pain.fillRectangle(x, starty + h, w, 2, Color::depthbar);
463 int RowPainter::paintAppendixStart(int y)
466 pb_font.setColor(Color::appendix);
473 docstring const label = _("Appendix");
474 theFontMetrics(pb_font).rectText(label, w, a, d);
476 int const text_start = int(xo_ + (width_ - w) / 2);
477 int const text_end = text_start + w;
479 pi_.pain.rectText(text_start, y + d, label, pb_font, Color::none, Color::none);
481 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color::appendix);
482 pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color::appendix);
484 return 3 * defaultRowHeight();
488 void RowPainter::paintFirst()
490 ParagraphParameters const & parparams = par_.params();
494 // start of appendix?
495 if (parparams.startOfAppendix())
496 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
498 Buffer const & buffer = pi_.base.bv->buffer();
500 LayoutPtr const & layout = par_.layout();
502 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
504 if (layout->latextype == LATEX_PARAGRAPH
505 && !par_.getDepth()) {
506 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
508 LayoutPtr const & playout = pars_[pit_ - 1].layout();
509 if (playout->latextype == LATEX_PARAGRAPH
510 && !pars_[pit_ - 1].getDepth()) {
511 // is it right to use defskip here, too? (AS)
512 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
518 bool const is_rtl = text_.isRTL(buffer, par_);
519 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
520 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
522 // should we print a label?
523 if (layout->labeltype >= LABEL_STATIC
524 && (layout->labeltype != LABEL_STATIC
525 || layout->latextype != LATEX_ENVIRONMENT
528 Font const font = getLabelFont();
529 FontMetrics const & fm = theFontMetrics(font);
531 docstring const str = par_.getLabelstring();
535 // this is special code for the chapter layout. This is
536 // printed in an extra row and has a pagebreak at
538 if (layout->counter == "chapter") {
539 double spacing_val = 1.0;
540 if (!parparams.spacing().isDefault()) {
541 spacing_val = parparams.spacing().getValue();
543 spacing_val = buffer.params().spacing().getValue();
546 int const labeladdon = int(fm.maxHeight() * layout->spacing.getValue() * spacing_val);
548 int const maxdesc = int(fm.maxDescent() * layout->spacing.getValue() * spacing_val)
549 + int(layout->parsep) * defaultRowHeight();
552 x = width_ - leftMargin() -
556 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
559 x = width_ - leftMargin()
560 + fm.width(layout->labelsep);
562 x = x_ - fm.width(layout->labelsep)
566 pi_.pain.text(int(x), yo_, str, font);
570 // the labels at the top of an environment.
571 // More or less for bibliography
573 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
574 layout->labeltype == LABEL_BIBLIO ||
575 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
576 Font font = getLabelFont();
577 if (!par_.getLabelstring().empty()) {
578 docstring const str = par_.getLabelstring();
579 double spacing_val = 1.0;
580 if (!parparams.spacing().isDefault())
581 spacing_val = parparams.spacing().getValue();
583 spacing_val = buffer.params().spacing().getValue();
585 FontMetrics const & fm = theFontMetrics(font);
587 int const labeladdon = int(fm.maxHeight()
588 * layout->spacing.getValue() * spacing_val);
591 int(fm.maxDescent() * layout->spacing.getValue() * spacing_val
592 + (layout->labelbottomsep * defaultRowHeight()));
595 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
598 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
599 x -= fm.width(str) / 2;
601 x = width_ - leftMargin() - fm.width(str);
603 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
609 void RowPainter::paintLast()
611 bool const is_rtl = text_.isRTL(pi_.base.bv->buffer(), par_);
612 int const endlabel = getEndLabel(pit_, text_.paragraphs());
614 // paint imaginary end-of-paragraph character
616 if (par_.isInserted(par_.size()) || par_.isDeleted(par_.size())) {
617 FontMetrics const & fm = theFontMetrics(pi_.base.bv->buffer().params().getFont());
618 int const length = fm.maxAscent() / 2;
619 Color::color col = par_.isInserted(par_.size()) ? Color::addedtext : Color::deletedtext;
621 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
622 Painter::line_solid, Painter::line_thick);
623 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1, yo_ + 2, col,
624 Painter::line_solid, Painter::line_thick);
631 case END_LABEL_FILLED_BOX: {
632 Font const font = getLabelFont();
633 FontMetrics const & fm = theFontMetrics(font);
634 int const size = int(0.75 * fm.maxAscent());
635 int const y = yo_ - size;
636 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
638 if (width_ - int(row_.width()) <= size)
639 x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
641 if (endlabel == END_LABEL_BOX)
642 pi_.pain.rectangle(x, y, size, size, Color::eolmarker);
644 pi_.pain.fillRectangle(x, y, size, size, Color::eolmarker);
648 case END_LABEL_STATIC: {
649 Font font = getLabelFont();
650 FontMetrics const & fm = theFontMetrics(font);
651 docstring const & str = par_.layout()->endlabelstring();
652 double const x = is_rtl ?
654 : - text_metrics_.rightMargin(pm_) - row_.width();
655 pi_.pain.text(int(x), yo_, str, font);
659 case END_LABEL_NO_LABEL:
665 void RowPainter::paintOnlyInsets()
667 pos_type const end = row_.endpos();
668 for (pos_type pos = row_.pos(); pos != end; ++pos) {
669 if (!par_.isInset(pos))
672 // If outer row has changed, nested insets are repaint completely.
673 Inset const * inset = par_.getInset(pos);
675 if (x_ > pi_.base.bv->workWidth())
678 x_ = pi_.base.bv->coordCache().getInsets().x(inset);
679 paintInset(inset, pos);
684 void RowPainter::paintText()
686 pos_type const end = row_.endpos();
687 // Spaces at logical line breaks in bidi text must be skipped during
688 // painting. However, they may appear visually in the middle
689 // of a row; they must be skipped, wherever they are...
690 // * logically "abc_[HEBREW_\nHEBREW]"
691 // * visually "abc_[_WERBEH\nWERBEH]"
692 pos_type skipped_sep_vpos = -1;
693 pos_type body_pos = par_.beginOfBody();
695 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
699 LayoutPtr const & layout = par_.layout();
701 bool running_strikeout = false;
702 bool is_struckout = false;
703 int last_strikeout_x = 0;
705 // Use font span to speed things up, see below
709 // If the last logical character is a separator, don't paint it, unless
710 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
711 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
712 skipped_sep_vpos = bidi_.log2vis(end - 1);
714 for (pos_type vpos = row_.pos(); vpos < end; ) {
715 if (x_ > pi_.base.bv->workWidth())
718 // Skip the separator at the logical end of the row
719 if (vpos == skipped_sep_vpos) {
724 pos_type const pos = bidi_.vis2log(vpos);
726 if (pos >= par_.size()) {
731 // Use font span to speed things up, see above
732 if (vpos < font_span.first || vpos > font_span.last) {
733 font_span = par_.fontSpan(vpos);
734 font = text_metrics_.getDisplayFont(pit_, vpos);
737 const int width_pos = pm_.singleWidth(pos, font);
739 if (x_ + width_pos < 0) {
745 is_struckout = par_.isDeleted(pos);
747 if (is_struckout && !running_strikeout) {
748 running_strikeout = true;
749 last_strikeout_x = int(x_);
752 bool const highly_editable_inset = par_.isInset(pos)
753 && par_.getInset(pos)->editable() == Inset::HIGHLY_EDITABLE;
755 // If we reach the end of a struck out range, paint it.
756 // We also don't paint across things like tables
757 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
758 // Calculate 1/3 height of the buffer's default font
759 FontMetrics const & fm
760 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
761 int const middle = yo_ - fm.maxAscent() / 3;
762 pi_.pain.line(last_strikeout_x, middle, int(x_), middle,
763 Color::deletedtext, Painter::line_solid, Painter::line_thin);
764 running_strikeout = false;
767 if (body_pos > 0 && pos == body_pos - 1) {
768 int const lwidth = theFontMetrics(getLabelFont())
769 .width(layout->labelsep);
771 x_ += row_.label_hfill + lwidth - width_pos;
774 if (par_.isHfill(pos)) {
775 paintHfill(pos, body_pos);
778 } else if (par_.isSeparator(pos)) {
779 Font orig_font = text_metrics_.getDisplayFont(pit_, pos);
780 double const orig_x = x_;
783 x_ += row_.separator;
784 paintForeignMark(orig_x, orig_font);
787 } else if (par_.isInset(pos)) {
788 // If outer row has changed, nested insets are repaint completely.
789 Inset const * inset = par_.getInset(pos);
790 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
791 paintInset(inset, pos);
795 // paint as many characters as possible.
800 // if we reach the end of a struck out range, paint it
801 if (running_strikeout) {
802 // calculate 1/3 height of the buffer's default font
803 FontMetrics const & fm
804 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
805 int const middle = yo_ - fm.maxAscent() / 3;
806 pi_.pain.line(last_strikeout_x, middle, int(x_), middle,
807 Color::deletedtext, Painter::line_solid, Painter::line_thin);
808 running_strikeout = false;