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>
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 FontInfo 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_,
89 // If you want to debug inset metrics uncomment the following line:
90 //#define DEBUG_METRICS
91 // This draws green lines around each inset.
94 void RowPainter::paintInset(Inset const * inset, pos_type const pos)
96 Font const font = text_metrics_.getDisplayFont(pit_, pos);
99 // FIXME: We should always use font, see documentation of
100 // noFontChange() in Inset.h.
101 pi_.base.font = inset->noFontChange() ?
102 pi_.base.bv->buffer().params().getFont().fontInfo() :
104 pi_.ltr_pos = (bidi_.level(pos) % 2 == 0);
105 pi_.erased_ = erased_ || par_.isDeleted(pos);
106 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
107 // insets are painted completely. Recursive
108 inset->drawSelection(pi_, int(x_), yo_);
109 inset->draw(pi_, int(x_), yo_);
111 Dimension const & dim = pm_.insetDimension(inset);
113 paintForeignMark(x_, font.language(), dim.descent());
118 int const x1 = int(x_ - dim.width());
120 BOOST_ASSERT(max_witdh_ > 0);
121 int right_margin = text_metrics_.rightMargin(pm_);
122 int const w = max_witdh_ - leftMargin() - right_margin;
123 MetricsInfo mi(pi_.base.bv, font.fontInfo(), w);
124 inset->metrics(mi, dim2);
125 if (dim.wid != dim2.wid)
126 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
127 << " draw width " << dim.width()
128 << "> metrics width " << dim2.wid << "." << endl;
129 if (dim->asc != dim2.asc)
130 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
131 << " draw ascent " << dim.ascent()
132 << "> metrics ascent " << dim2.asc << "." << endl;
133 if (dim2.descent() != dim.des)
134 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
135 << " draw ascent " << dim.descent()
136 << "> metrics descent " << dim2.des << "." << endl;
137 BOOST_ASSERT(dim2.wid == dim.wid);
138 BOOST_ASSERT(dim2.asc == dim.asc);
139 BOOST_ASSERT(dim2.des == dim.des);
140 int const x2 = x1 + dim.wid;
141 int const y1 = yo_ + dim.des;
142 int const y2 = yo_ - dim.asc;
143 pi_.pain.line(x1, y1, x1, y2, Color_green);
144 pi_.pain.line(x1, y1, x2, y1, Color_green);
145 pi_.pain.line(x2, y1, x2, y2, Color_green);
146 pi_.pain.line(x1, y2, x2, y2, Color_green);
151 void RowPainter::paintHebrewComposeChar(pos_type & vpos, FontInfo const & font)
153 pos_type pos = bidi_.vis2log(vpos);
158 char_type c = par_.getChar(pos);
162 int const width = theFontMetrics(font).width(c);
165 for (pos_type i = pos - 1; i >= 0; --i) {
167 if (!Encodings::isComposeChar_hebrew(c)) {
168 if (isPrintableNonspace(c)) {
169 int const width2 = pm_.singleWidth(i,
170 text_metrics_.getDisplayFont(pit_, i));
171 dx = (c == 0x05e8 || // resh
172 c == 0x05d3) // dalet
174 : (width2 - width) / 2;
181 pi_.pain.text(int(x_) + dx, yo_, str, font);
185 void RowPainter::paintArabicComposeChar(pos_type & vpos, FontInfo const & font)
187 pos_type pos = bidi_.vis2log(vpos);
191 char_type c = par_.getChar(pos);
192 c = par_.transformChar(c, pos);
196 int const width = theFontMetrics(font).width(c);
199 for (pos_type i = pos - 1; i >= 0; --i) {
201 if (!Encodings::isComposeChar_arabic(c)) {
202 if (isPrintableNonspace(c)) {
203 int const width2 = pm_.singleWidth(i,
204 text_metrics_.getDisplayFont(pit_, i));
205 dx = (width2 - width) / 2;
211 pi_.pain.text(int(x_) + dx, yo_, str, font);
215 void RowPainter::paintChars(pos_type & vpos, FontInfo const & font,
216 bool hebrew, bool arabic)
218 // This method takes up 70% of time when typing
219 pos_type pos = bidi_.vis2log(vpos);
220 pos_type const end = row_.endpos();
221 FontSpan const font_span = par_.fontSpan(pos);
222 Change::Type const prev_change = par_.lookupChange(pos).type;
225 vector<char_type> str;
227 str.push_back(par_.getChar(pos));
230 char_type c = str[0];
235 str[0] = par_.transformChar(c, pos);
238 // collect as much similar chars as we can
239 for (++vpos ; vpos < end ; ++vpos) {
240 pos = bidi_.vis2log(vpos);
241 if (pos < font_span.first || pos > font_span.last)
244 if (prev_change != par_.lookupChange(pos).type)
247 char_type c = par_.getChar(pos);
249 if (!isPrintableNonspace(c))
252 /* Because we do our own bidi, at this point the strings are
253 * already in visual order. However, Qt also applies its own
254 * bidi algorithm to strings that it paints to the screen.
255 * Therefore, if we were to paint Hebrew/Arabic words as a
256 * single string, the letters in the words would get reversed
257 * again. In order to avoid that, we don't collect Hebrew/
258 * Arabic characters, but rather paint them one at a time.
259 * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
264 /* FIXME: these checks are irrelevant, since 'arabic' and
265 * 'hebrew' alone are already going to trigger a break.
266 * However, this should not be removed completely, because
267 * if an alternative solution is found which allows grouping
268 * of arabic and hebrew characters, then these breaks may have
271 if (arabic && Encodings::isComposeChar_arabic(c))
274 if (hebrew && Encodings::isComposeChar_hebrew(c))
283 c = par_.transformChar(c, pos);
284 /* see comment in hebrew, explaining why we break */
291 docstring s(&str[0], str.size());
293 if (prev_change != Change::UNCHANGED) {
294 FontInfo copy = font;
295 if (prev_change == Change::DELETED) {
296 copy.setColor(Color_deletedtext);
297 } else if (prev_change == Change::INSERTED) {
298 copy.setColor(Color_addedtext);
300 x_ += pi_.pain.text(int(x_), yo_, s, copy);
302 x_ += pi_.pain.text(int(x_), yo_, s, font);
307 void RowPainter::paintForeignMark(double orig_x, Language const * lang,
310 if (!lyxrc.mark_foreign_language)
312 if (lang == latex_language)
314 if (lang == pi_.base.bv->buffer().params().language)
317 int const y = yo_ + 1 + desc;
318 pi_.pain.line(int(orig_x), y, int(x_), y, Color_language);
322 void RowPainter::paintFromPos(pos_type & vpos)
324 pos_type const pos = bidi_.vis2log(vpos);
325 Font const orig_font = text_metrics_.getDisplayFont(pit_, pos);
326 double const orig_x = x_;
328 // usual characters, no insets
329 char_type const c = par_.getChar(pos);
331 // special case languages
332 string const & lang = orig_font.language()->lang();
333 bool const hebrew = lang == "hebrew";
334 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" ||
337 // draw as many chars as we can
338 if ((!hebrew && !arabic)
339 || (hebrew && !Encodings::isComposeChar_hebrew(c))
340 || (arabic && !Encodings::isComposeChar_arabic(c))) {
341 paintChars(vpos, orig_font.fontInfo(), hebrew, arabic);
343 paintHebrewComposeChar(vpos, orig_font.fontInfo());
345 paintArabicComposeChar(vpos, orig_font.fontInfo());
348 paintForeignMark(orig_x, orig_font.language());
352 void RowPainter::paintChangeBar()
354 pos_type const start = row_.pos();
355 pos_type end = row_.endpos();
357 if (par_.size() == end) {
358 // this is the last row of the paragraph;
359 // thus, we must also consider the imaginary end-of-par character
363 if (start == end || !par_.isChanged(start, end))
366 int const height = text_metrics_.isLastRow(pit_, row_)
370 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
374 void RowPainter::paintAppendix()
376 // only draw the appendix frame once (for the main text)
377 if (!par_.params().appendix() || !text_.isMainText(pi_.base.bv->buffer()))
380 int y = yo_ - row_.ascent();
382 if (par_.params().startOfAppendix())
383 y += 2 * defaultRowHeight();
385 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
386 pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
390 void RowPainter::paintDepthBar()
392 depth_type const depth = par_.getDepth();
397 depth_type prev_depth = 0;
398 if (!text_metrics_.isFirstRow(pit_, row_)) {
399 pit_type pit2 = pit_;
402 prev_depth = pars_[pit2].getDepth();
405 depth_type next_depth = 0;
406 if (!text_metrics_.isLastRow(pit_, row_)) {
407 pit_type pit2 = pit_;
408 if (row_.endpos() >= pars_[pit2].size())
410 next_depth = pars_[pit2].getDepth();
413 for (depth_type i = 1; i <= depth; ++i) {
414 int const w = nestMargin() / 5;
415 int x = int(xo_) + w * i;
416 // only consider the changebar space if we're drawing outermost text
417 if (text_.isMainText(pi_.base.bv->buffer()))
418 x += changebarMargin();
420 int const starty = yo_ - row_.ascent();
421 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
423 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
426 pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
428 pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
433 int RowPainter::paintAppendixStart(int y)
435 FontInfo pb_font = sane_font;
436 pb_font.setColor(Color_appendix);
443 docstring const label = _("Appendix");
444 theFontMetrics(pb_font).rectText(label, w, a, d);
446 int const text_start = int(xo_ + (width_ - w) / 2);
447 int const text_end = text_start + w;
449 pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
451 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
452 pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
454 return 3 * defaultRowHeight();
458 void RowPainter::paintFirst()
460 ParagraphParameters const & parparams = par_.params();
464 // start of appendix?
465 if (parparams.startOfAppendix())
466 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
468 Buffer const & buffer = pi_.base.bv->buffer();
470 LayoutPtr const & layout = par_.layout();
472 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
474 if (layout->latextype == LATEX_PARAGRAPH
475 && !par_.getDepth()) {
476 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
478 LayoutPtr const & playout = pars_[pit_ - 1].layout();
479 if (playout->latextype == LATEX_PARAGRAPH
480 && !pars_[pit_ - 1].getDepth()) {
481 // is it right to use defskip here, too? (AS)
482 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
488 bool const is_rtl = text_.isRTL(buffer, par_);
489 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
490 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
492 // should we print a label?
493 if (layout->labeltype >= LABEL_STATIC
494 && (layout->labeltype != LABEL_STATIC
495 || layout->latextype != LATEX_ENVIRONMENT
498 FontInfo const font = getLabelFont();
499 FontMetrics const & fm = theFontMetrics(font);
501 docstring const str = par_.getLabelstring();
505 // this is special code for the chapter layout. This is
506 // printed in an extra row and has a pagebreak at
508 if (layout->counter == "chapter") {
509 double spacing_val = 1.0;
510 if (!parparams.spacing().isDefault()) {
511 spacing_val = parparams.spacing().getValue();
513 spacing_val = buffer.params().spacing().getValue();
516 int const labeladdon = int(fm.maxHeight() * layout->spacing.getValue() * spacing_val);
518 int const maxdesc = int(fm.maxDescent() * layout->spacing.getValue() * spacing_val)
519 + int(layout->parsep) * defaultRowHeight();
522 x = width_ - leftMargin() -
526 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
529 x = width_ - leftMargin()
530 + fm.width(layout->labelsep);
532 x = x_ - fm.width(layout->labelsep)
536 pi_.pain.text(int(x), yo_, str, font);
540 // the labels at the top of an environment.
541 // More or less for bibliography
543 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
544 layout->labeltype == LABEL_BIBLIO ||
545 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
546 FontInfo const font = getLabelFont();
547 if (!par_.getLabelstring().empty()) {
548 docstring const str = par_.getLabelstring();
549 double spacing_val = 1.0;
550 if (!parparams.spacing().isDefault())
551 spacing_val = parparams.spacing().getValue();
553 spacing_val = buffer.params().spacing().getValue();
555 FontMetrics const & fm = theFontMetrics(font);
557 int const labeladdon = int(fm.maxHeight()
558 * layout->spacing.getValue() * spacing_val);
561 int(fm.maxDescent() * layout->spacing.getValue() * spacing_val
562 + (layout->labelbottomsep * defaultRowHeight()));
565 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
568 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
569 x -= fm.width(str) / 2;
571 x = width_ - leftMargin() - fm.width(str);
573 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
579 void RowPainter::paintLast()
581 bool const is_rtl = text_.isRTL(pi_.base.bv->buffer(), par_);
582 int const endlabel = getEndLabel(pit_, text_.paragraphs());
584 // paint imaginary end-of-paragraph character
586 if (par_.isInserted(par_.size()) || par_.isDeleted(par_.size())) {
587 FontMetrics const & fm = theFontMetrics(pi_.base.bv->buffer().params().getFont());
588 int const length = fm.maxAscent() / 2;
589 ColorCode col = par_.isInserted(par_.size()) ? Color_addedtext : Color_deletedtext;
591 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
592 Painter::line_solid, Painter::line_thick);
593 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1, yo_ + 2, col,
594 Painter::line_solid, Painter::line_thick);
601 case END_LABEL_FILLED_BOX: {
602 FontInfo const font = getLabelFont();
603 FontMetrics const & fm = theFontMetrics(font);
604 int const size = int(0.75 * fm.maxAscent());
605 int const y = yo_ - size;
606 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
608 if (width_ - int(row_.width()) <= size)
609 x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
611 if (endlabel == END_LABEL_BOX)
612 pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
614 pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
618 case END_LABEL_STATIC: {
619 FontInfo const font = getLabelFont();
620 FontMetrics const & fm = theFontMetrics(font);
621 docstring const & str = par_.layout()->endlabelstring();
622 double const x = is_rtl ?
624 : - text_metrics_.rightMargin(pm_) - row_.width();
625 pi_.pain.text(int(x), yo_, str, font);
629 case END_LABEL_NO_LABEL:
635 void RowPainter::paintOnlyInsets()
637 pos_type const end = row_.endpos();
638 for (pos_type pos = row_.pos(); pos != end; ++pos) {
639 if (!par_.isInset(pos))
642 // If outer row has changed, nested insets are repaint completely.
643 Inset const * inset = par_.getInset(pos);
645 if (x_ > pi_.base.bv->workWidth())
648 x_ = pi_.base.bv->coordCache().getInsets().x(inset);
649 paintInset(inset, pos);
654 void RowPainter::paintText()
656 pos_type const end = row_.endpos();
657 // Spaces at logical line breaks in bidi text must be skipped during
658 // painting. However, they may appear visually in the middle
659 // of a row; they must be skipped, wherever they are...
660 // * logically "abc_[HEBREW_\nHEBREW]"
661 // * visually "abc_[_WERBEH\nWERBEH]"
662 pos_type skipped_sep_vpos = -1;
663 pos_type body_pos = par_.beginOfBody();
665 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
669 LayoutPtr const & layout = par_.layout();
671 bool running_strikeout = false;
672 bool is_struckout = false;
673 int last_strikeout_x = 0;
675 // Use font span to speed things up, see below
679 // If the last logical character is a separator, don't paint it, unless
680 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
681 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
682 skipped_sep_vpos = bidi_.log2vis(end - 1);
684 for (pos_type vpos = row_.pos(); vpos < end; ) {
685 if (x_ > pi_.base.bv->workWidth())
688 // Skip the separator at the logical end of the row
689 if (vpos == skipped_sep_vpos) {
694 pos_type const pos = bidi_.vis2log(vpos);
696 if (pos >= par_.size()) {
701 // Use font span to speed things up, see above
702 if (vpos < font_span.first || vpos > font_span.last) {
703 font_span = par_.fontSpan(vpos);
704 font = text_metrics_.getDisplayFont(pit_, vpos);
707 const int width_pos = pm_.singleWidth(pos, font);
709 if (x_ + width_pos < 0) {
715 is_struckout = par_.isDeleted(pos);
717 if (is_struckout && !running_strikeout) {
718 running_strikeout = true;
719 last_strikeout_x = int(x_);
722 bool const highly_editable_inset = par_.isInset(pos)
723 && par_.getInset(pos)->editable() == Inset::HIGHLY_EDITABLE;
725 // If we reach the end of a struck out range, paint it.
726 // We also don't paint across things like tables
727 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
728 // Calculate 1/3 height of the buffer's default font
729 FontMetrics const & fm
730 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
731 int const middle = yo_ - fm.maxAscent() / 3;
732 pi_.pain.line(last_strikeout_x, middle, int(x_), middle,
733 Color_deletedtext, Painter::line_solid, Painter::line_thin);
734 running_strikeout = false;
737 if (body_pos > 0 && pos == body_pos - 1) {
738 int const lwidth = theFontMetrics(getLabelFont())
739 .width(layout->labelsep);
741 x_ += row_.label_hfill + lwidth - width_pos;
744 if (par_.isSeparator(pos)) {
745 Font const orig_font = text_metrics_.getDisplayFont(pit_, pos);
746 double const orig_x = x_;
749 x_ += row_.separator;
750 paintForeignMark(orig_x, orig_font.language());
753 } else if (par_.isInset(pos)) {
754 // If outer row has changed, nested insets are repaint completely.
755 Inset const * inset = par_.getInset(pos);
756 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
757 paintInset(inset, pos);
761 // paint as many characters as possible.
766 // if we reach the end of a struck out range, paint it
767 if (running_strikeout) {
768 // calculate 1/3 height of the buffer's default font
769 FontMetrics const & fm
770 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
771 int const middle = yo_ - fm.maxAscent() / 3;
772 pi_.pain.line(last_strikeout_x, middle, int(x_), middle,
773 Color_deletedtext, Painter::line_solid, Painter::line_thin);
774 running_strikeout = false;