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 : bv_(*pi.base.bv), pain_(pi.pain), 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_, 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(bv_.buffer(), par_);
83 int RowPainter::leftMargin() const
85 return text_.leftMargin(bv_.buffer(), text_metrics_.width(), pit_,
90 // If you want to debug inset metrics uncomment the following line:
91 //#define DEBUG_METRICS
92 // This draws green lines around each inset.
95 void RowPainter::paintInset(pos_type & vpos)
97 pos_type const pos = bidi_.vis2log(vpos);
98 Font font = text_.getFont(bv_.buffer(), par_, pos);
100 Inset const * inset = par_.getInset(pos);
102 PainterInfo pi(const_cast<BufferView *>(&bv_), pain_);
103 // FIXME: We should always use font, see documentation of
104 // noFontChange() in Inset.h.
105 pi.base.font = inset->noFontChange() ?
106 bv_.buffer().params().getFont() :
108 pi.ltr_pos = (bidi_.level(pos) % 2 == 0);
109 pi.erased_ = erased_ || par_.isDeleted(pos);
110 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_);
116 paintForeignMark(x_, font, inset->descent());
118 x_ += inset->width();
121 int const x1 = int(x_ - inset->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(&bv_, font, w);
127 inset->metrics(mi, dim);
128 if (inset->width() > dim.wid)
129 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
130 << " draw width " << inset->width()
131 << "> metrics width " << dim.wid << "." << std::endl;
132 if (inset->ascent() > dim.asc)
133 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
134 << " draw ascent " << inset->ascent()
135 << "> metrics ascent " << dim.asc << "." << std::endl;
136 if (inset->descent() > dim.des)
137 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
138 << " draw ascent " << inset->descent()
139 << "> metrics descent " << dim.des << "." << std::endl;
140 BOOST_ASSERT(inset->width() <= dim.wid);
141 BOOST_ASSERT(inset->ascent() <= dim.asc);
142 BOOST_ASSERT(inset->descent() <= 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, Font 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_.getFont(bv_.buffer(), par_, i));
174 dx = (c == 0x05e8 || // resh
175 c == 0x05d3) // dalet
177 : (width2 - width) / 2;
184 pain_.text(int(x_) + dx, yo_, str, font);
188 void RowPainter::paintArabicComposeChar(pos_type & vpos, Font 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_.getFont(bv_.buffer(), par_, i));
208 dx = (width2 - width) / 2;
214 pain_.text(int(x_) + dx, yo_, str, font);
218 void RowPainter::paintChars(pos_type & vpos, Font 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) {
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_ += pain_.text(int(x_), yo_, s, copy);
305 x_ += pain_.text(int(x_), yo_, s, font);
310 void RowPainter::paintForeignMark(double orig_x, Font const & font, int desc)
312 if (!lyxrc.mark_foreign_language)
314 if (font.language() == latex_language)
316 if (font.language() == bv_.buffer().params().language)
319 int const y = yo_ + 1 + desc;
320 pain_.line(int(orig_x), y, int(x_), y, Color::language);
324 void RowPainter::paintFromPos(pos_type & vpos)
326 pos_type const pos = bidi_.vis2log(vpos);
327 Font orig_font = text_.getFont(bv_.buffer(), par_, pos);
328 double const orig_x = x_;
330 // usual characters, no insets
331 char_type const c = par_.getChar(pos);
333 // special case languages
334 std::string const & lang = orig_font.language()->lang();
335 bool const hebrew = lang == "hebrew";
336 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" ||
339 // draw as many chars as we can
340 if ((!hebrew && !arabic)
341 || (hebrew && !Encodings::isComposeChar_hebrew(c))
342 || (arabic && !Encodings::isComposeChar_arabic(c))) {
343 paintChars(vpos, orig_font, hebrew, arabic);
345 paintHebrewComposeChar(vpos, orig_font);
347 paintArabicComposeChar(vpos, orig_font);
350 paintForeignMark(orig_x, orig_font);
354 void RowPainter::paintChangeBar()
356 pos_type const start = row_.pos();
357 pos_type end = row_.endpos();
359 if (par_.size() == end) {
360 // this is the last row of the paragraph;
361 // thus, we must also consider the imaginary end-of-par character
365 if (start == end || !par_.isChanged(start, end))
368 int const height = text_.isLastRow(pit_, row_)
372 pain_.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color::changebar);
376 void RowPainter::paintAppendix()
378 // only draw the appendix frame once (for the main text)
379 if (!par_.params().appendix() || !text_.isMainText(bv_.buffer()))
382 int y = yo_ - row_.ascent();
384 if (par_.params().startOfAppendix())
385 y += 2 * defaultRowHeight();
387 pain_.line(1, y, 1, yo_ + row_.height(), Color::appendix);
388 pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color::appendix);
392 void RowPainter::paintDepthBar()
394 depth_type const depth = par_.getDepth();
399 depth_type prev_depth = 0;
400 if (!text_.isFirstRow(pit_, row_)) {
401 pit_type pit2 = pit_;
404 prev_depth = pars_[pit2].getDepth();
407 depth_type next_depth = 0;
408 if (!text_.isLastRow(pit_, row_)) {
409 pit_type pit2 = pit_;
410 if (row_.endpos() >= pars_[pit2].size())
412 next_depth = pars_[pit2].getDepth();
415 for (depth_type i = 1; i <= depth; ++i) {
416 int const w = nestMargin() / 5;
417 int x = int(xo_) + w * i;
418 // only consider the changebar space if we're drawing outermost text
419 if (text_.isMainText(bv_.buffer()))
420 x += changebarMargin();
422 int const starty = yo_ - row_.ascent();
423 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
425 pain_.line(x, starty, x, starty + h, Color::depthbar);
428 pain_.fillRectangle(x, starty, w, 2, Color::depthbar);
430 pain_.fillRectangle(x, starty + h, w, 2, Color::depthbar);
435 int RowPainter::paintAppendixStart(int y)
438 pb_font.setColor(Color::appendix);
445 docstring const label = _("Appendix");
446 theFontMetrics(pb_font).rectText(label, w, a, d);
448 int const text_start = int(xo_ + (width_ - w) / 2);
449 int const text_end = text_start + w;
451 pain_.rectText(text_start, y + d, label, pb_font, Color::none, Color::none);
453 pain_.line(int(xo_ + 1), y, text_start, y, Color::appendix);
454 pain_.line(text_end, y, int(xo_ + width_ - 2), y, Color::appendix);
456 return 3 * defaultRowHeight();
460 void RowPainter::paintFirst()
462 ParagraphParameters const & parparams = par_.params();
466 // start of appendix?
467 if (parparams.startOfAppendix())
468 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
470 Buffer const & buffer = bv_.buffer();
472 LayoutPtr const & layout = par_.layout();
474 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
476 if (layout->latextype == LATEX_PARAGRAPH
477 && !par_.getDepth()) {
478 y_top += buffer.params().getDefSkip().inPixels(bv_);
480 LayoutPtr const & playout = pars_[pit_ - 1].layout();
481 if (playout->latextype == LATEX_PARAGRAPH
482 && !pars_[pit_ - 1].getDepth()) {
483 // is it right to use defskip here, too? (AS)
484 y_top += buffer.params().getDefSkip().inPixels(bv_);
490 bool const is_rtl = text_.isRTL(buffer, par_);
491 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
492 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
494 // should we print a label?
495 if (layout->labeltype >= LABEL_STATIC
496 && (layout->labeltype != LABEL_STATIC
497 || layout->latextype != LATEX_ENVIRONMENT
500 Font const font = getLabelFont();
501 FontMetrics const & fm = theFontMetrics(font);
503 docstring const str = par_.getLabelstring();
507 // this is special code for the chapter layout. This is
508 // printed in an extra row and has a pagebreak at
510 if (layout->counter == "chapter") {
511 double spacing_val = 1.0;
512 if (!parparams.spacing().isDefault()) {
513 spacing_val = parparams.spacing().getValue();
515 spacing_val = buffer.params().spacing().getValue();
518 int const labeladdon = int(fm.maxHeight() * layout->spacing.getValue() * spacing_val);
520 int const maxdesc = int(fm.maxDescent() * layout->spacing.getValue() * spacing_val)
521 + int(layout->parsep) * defaultRowHeight();
524 x = width_ - leftMargin() -
528 pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
531 x = width_ - leftMargin()
532 + fm.width(layout->labelsep);
534 x = x_ - fm.width(layout->labelsep)
538 pain_.text(int(x), yo_, str, font);
542 // the labels at the top of an environment.
543 // More or less for bibliography
545 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
546 layout->labeltype == LABEL_BIBLIO ||
547 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
548 Font font = getLabelFont();
549 if (!par_.getLabelstring().empty()) {
550 docstring const str = par_.getLabelstring();
551 double spacing_val = 1.0;
552 if (!parparams.spacing().isDefault())
553 spacing_val = parparams.spacing().getValue();
555 spacing_val = buffer.params().spacing().getValue();
557 FontMetrics const & fm = theFontMetrics(font);
559 int const labeladdon = int(fm.maxHeight()
560 * layout->spacing.getValue() * spacing_val);
563 int(fm.maxDescent() * layout->spacing.getValue() * spacing_val
564 + (layout->labelbottomsep * defaultRowHeight()));
567 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
570 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
571 x -= fm.width(str) / 2;
573 x = width_ - leftMargin() - fm.width(str);
575 pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
581 void RowPainter::paintLast()
583 bool const is_rtl = text_.isRTL(bv_.buffer(), par_);
584 int const endlabel = getEndLabel(pit_, text_.paragraphs());
586 // paint imaginary end-of-paragraph character
588 if (par_.isInserted(par_.size()) || par_.isDeleted(par_.size())) {
589 FontMetrics const & fm = theFontMetrics(bv_.buffer().params().getFont());
590 int const length = fm.maxAscent() / 2;
591 Color::color col = par_.isInserted(par_.size()) ? Color::addedtext : Color::deletedtext;
593 pain_.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
594 Painter::line_solid, Painter::line_thick);
595 pain_.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1, yo_ + 2, col,
596 Painter::line_solid, Painter::line_thick);
603 case END_LABEL_FILLED_BOX: {
604 Font const font = getLabelFont();
605 FontMetrics const & fm = theFontMetrics(font);
606 int const size = int(0.75 * fm.maxAscent());
607 int const y = yo_ - size;
608 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
610 if (width_ - int(row_.width()) <= size)
611 x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
613 if (endlabel == END_LABEL_BOX)
614 pain_.rectangle(x, y, size, size, Color::eolmarker);
616 pain_.fillRectangle(x, y, size, size, Color::eolmarker);
620 case END_LABEL_STATIC: {
621 Font font = getLabelFont();
622 FontMetrics const & fm = theFontMetrics(font);
623 docstring const & str = par_.layout()->endlabelstring();
624 double const x = is_rtl ?
626 : - text_metrics_.rightMargin(pm_) - row_.width();
627 pain_.text(int(x), yo_, str, font);
631 case END_LABEL_NO_LABEL:
637 void RowPainter::paintText()
639 pos_type const end = row_.endpos();
640 // Spaces at logical line breaks in bidi text must be skipped during
641 // painting. However, they may appear visually in the middle
642 // of a row; they must be skipped, wherever they are...
643 // * logically "abc_[HEBREW_\nHEBREW]"
644 // * visually "abc_[_WERBEH\nWERBEH]"
645 pos_type skipped_sep_vpos = -1;
646 pos_type body_pos = par_.beginOfBody();
648 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
652 LayoutPtr const & layout = par_.layout();
654 bool running_strikeout = false;
655 bool is_struckout = false;
656 int last_strikeout_x = 0;
658 // Use font span to speed things up, see below
661 Buffer const & buffer = bv_.buffer();
663 // If the last logical character is a separator, don't paint it, unless
664 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
665 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
666 skipped_sep_vpos = bidi_.log2vis(end - 1);
668 for (pos_type vpos = row_.pos(); vpos < end; ) {
669 if (x_ > bv_.workWidth())
672 // Skip the separator at the logical end of the row
673 if (vpos == skipped_sep_vpos) {
678 pos_type const pos = bidi_.vis2log(vpos);
680 if (pos >= par_.size()) {
685 // Use font span to speed things up, see above
686 if (vpos < font_span.first || vpos > font_span.last) {
687 font_span = par_.fontSpan(vpos);
688 font = text_.getFont(buffer, par_, vpos);
691 const int width_pos = pm_.singleWidth(pos, font);
693 if (x_ + width_pos < 0) {
699 is_struckout = par_.isDeleted(pos);
701 if (is_struckout && !running_strikeout) {
702 running_strikeout = true;
703 last_strikeout_x = int(x_);
706 bool const highly_editable_inset = par_.isInset(pos)
707 && isHighlyEditableInset(par_.getInset(pos));
709 // If we reach the end of a struck out range, paint it.
710 // We also don't paint across things like tables
711 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
712 // Calculate 1/3 height of the buffer's default font
713 FontMetrics const & fm
714 = theFontMetrics(bv_.buffer().params().getFont());
715 int const middle = yo_ - fm.maxAscent() / 3;
716 pain_.line(last_strikeout_x, middle, int(x_), middle,
717 Color::deletedtext, Painter::line_solid, Painter::line_thin);
718 running_strikeout = false;
721 if (body_pos > 0 && pos == body_pos - 1) {
722 int const lwidth = theFontMetrics(getLabelFont())
723 .width(layout->labelsep);
725 x_ += row_.label_hfill + lwidth - width_pos;
728 if (par_.isHfill(pos)) {
732 int const y1 = y0 - defaultRowHeight() / 2;
734 pain_.line(int(x_), y1, int(x_), y0, Color::added_space);
736 if (par_.hfillExpansion(row_, pos)) {
737 int const y2 = (y0 + y1) / 2;
739 if (pos >= body_pos) {
740 pain_.line(int(x_), y2, int(x_ + row_.hfill), y2,
742 Painter::line_onoffdash);
745 pain_.line(int(x_), y2, int(x_ + row_.label_hfill), y2,
747 Painter::line_onoffdash);
748 x_ += row_.label_hfill;
750 pain_.line(int(x_), y1, int(x_), y0, Color::added_space);
755 } else if (par_.isSeparator(pos)) {
756 Font orig_font = text_.getFont(bv_.buffer(), par_, pos);
757 double const orig_x = x_;
760 x_ += row_.separator;
762 paintForeignMark(orig_x, orig_font);
764 } else if (par_.isInset(pos)) {
765 // If outer row has changed, nested insets are repaint completely.
773 // if we reach the end of a struck out range, paint it
774 if (running_strikeout) {
775 // calculate 1/3 height of the buffer's default font
776 FontMetrics const & fm
777 = theFontMetrics(bv_.buffer().params().getFont());
778 int const middle = yo_ - fm.maxAscent() / 3;
779 pain_.line(last_strikeout_x, middle, int(x_), middle,
780 Color::deletedtext, Painter::line_solid, Painter::line_thin);
781 running_strikeout = false;