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"
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>
56 using frontend::Painter;
57 using frontend::FontMetrics;
59 RowPainter::RowPainter(PainterInfo & pi,
60 Text const & text, pit_type pit, Row const & row, Bidi & bidi, int x, int y)
61 : pi_(pi), text_(text),
62 text_metrics_(pi_.base.bv->textMetrics(&text)),
63 pars_(text.paragraphs()),
64 row_(row), pit_(pit), par_(text.paragraphs()[pit]),
65 pm_(text_metrics_.parMetrics(pit)),
66 bidi_(bidi), erased_(pi_.erased_),
67 xo_(x), yo_(y), width_(text_metrics_.width())
69 bidi_.computeTables(par_, pi_.base.bv->buffer(), row_);
72 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
75 BOOST_ASSERT(pit >= 0);
76 BOOST_ASSERT(pit < int(text.paragraphs().size()));
80 FontInfo const RowPainter::getLabelFont() const
82 return text_.getLabelFont(pi_.base.bv->buffer(), par_);
86 int RowPainter::leftMargin() const
88 return text_metrics_.leftMargin(text_metrics_.width(), pit_,
93 void RowPainter::paintHfill(pos_type const pos, pos_type const body_pos)
98 int const y1 = y0 - defaultRowHeight() / 2;
100 pi_.pain.line(int(x_), y1, int(x_), y0, Color_added_space);
102 if (pm_.hfillExpansion(row_, pos)) {
103 int const y2 = (y0 + y1) / 2;
105 if (pos >= body_pos) {
106 pi_.pain.line(int(x_), y2, int(x_ + row_.hfill), y2,
108 Painter::line_onoffdash);
111 pi_.pain.line(int(x_), y2, int(x_ + row_.label_hfill), y2,
113 Painter::line_onoffdash);
114 x_ += row_.label_hfill;
116 pi_.pain.line(int(x_), y1, int(x_), y0, Color_added_space);
122 // If you want to debug inset metrics uncomment the following line:
123 //#define DEBUG_METRICS
124 // This draws green lines around each inset.
127 void RowPainter::paintInset(Inset const * inset, pos_type const pos)
129 Font const font = text_metrics_.getDisplayFont(pit_, pos);
132 // FIXME: We should always use font, see documentation of
133 // noFontChange() in Inset.h.
134 pi_.base.font = inset->noFontChange() ?
135 pi_.base.bv->buffer().params().getFont().fontInfo() :
137 pi_.ltr_pos = (bidi_.level(pos) % 2 == 0);
138 pi_.erased_ = erased_ || par_.isDeleted(pos);
139 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
140 // insets are painted completely. Recursive
141 inset->drawSelection(pi_, int(x_), yo_);
142 inset->draw(pi_, int(x_), yo_);
144 Dimension const & dim = pm_.insetDimension(inset);
146 paintForeignMark(x_, font.language(), dim.descent());
151 int const x1 = int(x_ - dim.width());
153 BOOST_ASSERT(max_witdh_ > 0);
154 int right_margin = text_metrics_.rightMargin(pm_);
155 int const w = max_witdh_ - leftMargin() - right_margin;
156 MetricsInfo mi(pi_.base.bv, font.fontInfo(), w);
157 inset->metrics(mi, dim2);
158 if (dim.wid != dim2.wid)
159 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
160 << " draw width " << dim.width()
161 << "> metrics width " << dim2.wid << "." << std::endl;
162 if (dim->asc != dim2.asc)
163 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
164 << " draw ascent " << dim.ascent()
165 << "> metrics ascent " << dim2.asc << "." << std::endl;
166 if (dim2.descent() != dim.des)
167 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
168 << " draw ascent " << dim.descent()
169 << "> metrics descent " << dim2.des << "." << std::endl;
170 BOOST_ASSERT(dim2.wid == dim.wid);
171 BOOST_ASSERT(dim2.asc == dim.asc);
172 BOOST_ASSERT(dim2.des == dim.des);
173 int const x2 = x1 + dim.wid;
174 int const y1 = yo_ + dim.des;
175 int const y2 = yo_ - dim.asc;
176 pi_.pain.line(x1, y1, x1, y2, Color_green);
177 pi_.pain.line(x1, y1, x2, y1, Color_green);
178 pi_.pain.line(x2, y1, x2, y2, Color_green);
179 pi_.pain.line(x1, y2, x2, y2, Color_green);
184 void RowPainter::paintHebrewComposeChar(pos_type & vpos, FontInfo const & font)
186 pos_type pos = bidi_.vis2log(vpos);
191 char_type c = par_.getChar(pos);
195 int const width = theFontMetrics(font).width(c);
198 for (pos_type i = pos - 1; i >= 0; --i) {
200 if (!Encodings::isComposeChar_hebrew(c)) {
201 if (isPrintableNonspace(c)) {
202 int const width2 = pm_.singleWidth(i,
203 text_metrics_.getDisplayFont(pit_, i));
204 dx = (c == 0x05e8 || // resh
205 c == 0x05d3) // dalet
207 : (width2 - width) / 2;
214 pi_.pain.text(int(x_) + dx, yo_, str, font);
218 void RowPainter::paintArabicComposeChar(pos_type & vpos, FontInfo const & font)
220 pos_type pos = bidi_.vis2log(vpos);
224 char_type c = par_.getChar(pos);
225 c = par_.transformChar(c, pos);
229 int const width = theFontMetrics(font).width(c);
232 for (pos_type i = pos - 1; i >= 0; --i) {
234 if (!Encodings::isComposeChar_arabic(c)) {
235 if (isPrintableNonspace(c)) {
236 int const width2 = pm_.singleWidth(i,
237 text_metrics_.getDisplayFont(pit_, i));
238 dx = (width2 - width) / 2;
244 pi_.pain.text(int(x_) + dx, yo_, str, font);
248 void RowPainter::paintChars(pos_type & vpos, FontInfo const & font,
249 bool hebrew, bool arabic)
251 // This method takes up 70% of time when typing
252 pos_type pos = bidi_.vis2log(vpos);
253 pos_type const end = row_.endpos();
254 FontSpan const font_span = par_.fontSpan(pos);
255 Change::Type const prev_change = par_.lookupChange(pos).type;
258 std::vector<char_type> str;
260 str.push_back(par_.getChar(pos));
263 char_type c = str[0];
268 str[0] = par_.transformChar(c, pos);
271 // collect as much similar chars as we can
272 for (++vpos ; vpos < end ; ++vpos) {
273 pos = bidi_.vis2log(vpos);
274 if (pos < font_span.first || pos > font_span.last)
277 if (prev_change != par_.lookupChange(pos).type)
280 char_type c = par_.getChar(pos);
282 if (!isPrintableNonspace(c))
285 /* Because we do our own bidi, at this point the strings are
286 * already in visual order. However, Qt also applies its own
287 * bidi algorithm to strings that it paints to the screen.
288 * Therefore, if we were to paint Hebrew/Arabic words as a
289 * single string, the letters in the words would get reversed
290 * again. In order to avoid that, we don't collect Hebrew/
291 * Arabic characters, but rather paint them one at a time.
292 * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
297 /* FIXME: these checks are irrelevant, since 'arabic' and
298 * 'hebrew' alone are already going to trigger a break.
299 * However, this should not be removed completely, because
300 * if an alternative solution is found which allows grouping
301 * of arabic and hebrew characters, then these breaks may have
304 if (arabic && Encodings::isComposeChar_arabic(c))
307 if (hebrew && Encodings::isComposeChar_hebrew(c))
316 c = par_.transformChar(c, pos);
317 /* see comment in hebrew, explaining why we break */
324 docstring s(&str[0], str.size());
326 if (prev_change != Change::UNCHANGED) {
327 FontInfo copy = font;
328 if (prev_change == Change::DELETED) {
329 copy.setColor(Color_deletedtext);
330 } else if (prev_change == Change::INSERTED) {
331 copy.setColor(Color_addedtext);
333 x_ += pi_.pain.text(int(x_), yo_, s, copy);
335 x_ += pi_.pain.text(int(x_), yo_, s, font);
340 void RowPainter::paintForeignMark(double orig_x, Language const * lang,
343 if (!lyxrc.mark_foreign_language)
345 if (lang == latex_language)
347 if (lang == pi_.base.bv->buffer().params().language)
350 int const y = yo_ + 1 + desc;
351 pi_.pain.line(int(orig_x), y, int(x_), y, Color_language);
355 void RowPainter::paintFromPos(pos_type & vpos)
357 pos_type const pos = bidi_.vis2log(vpos);
358 Font const orig_font = text_metrics_.getDisplayFont(pit_, pos);
359 double const orig_x = x_;
361 // usual characters, no insets
362 char_type const c = par_.getChar(pos);
364 // special case languages
365 std::string const & lang = orig_font.language()->lang();
366 bool const hebrew = lang == "hebrew";
367 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" ||
370 // draw as many chars as we can
371 if ((!hebrew && !arabic)
372 || (hebrew && !Encodings::isComposeChar_hebrew(c))
373 || (arabic && !Encodings::isComposeChar_arabic(c))) {
374 paintChars(vpos, orig_font.fontInfo(), hebrew, arabic);
376 paintHebrewComposeChar(vpos, orig_font.fontInfo());
378 paintArabicComposeChar(vpos, orig_font.fontInfo());
381 paintForeignMark(orig_x, orig_font.language());
385 void RowPainter::paintChangeBar()
387 pos_type const start = row_.pos();
388 pos_type end = row_.endpos();
390 if (par_.size() == end) {
391 // this is the last row of the paragraph;
392 // thus, we must also consider the imaginary end-of-par character
396 if (start == end || !par_.isChanged(start, end))
399 int const height = text_metrics_.isLastRow(pit_, row_)
403 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
407 void RowPainter::paintAppendix()
409 // only draw the appendix frame once (for the main text)
410 if (!par_.params().appendix() || !text_.isMainText(pi_.base.bv->buffer()))
413 int y = yo_ - row_.ascent();
415 if (par_.params().startOfAppendix())
416 y += 2 * defaultRowHeight();
418 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
419 pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
423 void RowPainter::paintDepthBar()
425 depth_type const depth = par_.getDepth();
430 depth_type prev_depth = 0;
431 if (!text_metrics_.isFirstRow(pit_, row_)) {
432 pit_type pit2 = pit_;
435 prev_depth = pars_[pit2].getDepth();
438 depth_type next_depth = 0;
439 if (!text_metrics_.isLastRow(pit_, row_)) {
440 pit_type pit2 = pit_;
441 if (row_.endpos() >= pars_[pit2].size())
443 next_depth = pars_[pit2].getDepth();
446 for (depth_type i = 1; i <= depth; ++i) {
447 int const w = nestMargin() / 5;
448 int x = int(xo_) + w * i;
449 // only consider the changebar space if we're drawing outermost text
450 if (text_.isMainText(pi_.base.bv->buffer()))
451 x += changebarMargin();
453 int const starty = yo_ - row_.ascent();
454 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
456 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
459 pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
461 pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
466 int RowPainter::paintAppendixStart(int y)
468 FontInfo pb_font = sane_font;
469 pb_font.setColor(Color_appendix);
476 docstring const label = _("Appendix");
477 theFontMetrics(pb_font).rectText(label, w, a, d);
479 int const text_start = int(xo_ + (width_ - w) / 2);
480 int const text_end = text_start + w;
482 pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
484 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
485 pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
487 return 3 * defaultRowHeight();
491 void RowPainter::paintFirst()
493 ParagraphParameters const & parparams = par_.params();
497 // start of appendix?
498 if (parparams.startOfAppendix())
499 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
501 Buffer const & buffer = pi_.base.bv->buffer();
503 LayoutPtr const & layout = par_.layout();
505 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
507 if (layout->latextype == LATEX_PARAGRAPH
508 && !par_.getDepth()) {
509 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
511 LayoutPtr const & playout = pars_[pit_ - 1].layout();
512 if (playout->latextype == LATEX_PARAGRAPH
513 && !pars_[pit_ - 1].getDepth()) {
514 // is it right to use defskip here, too? (AS)
515 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
521 bool const is_rtl = text_.isRTL(buffer, par_);
522 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
523 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
525 // should we print a label?
526 if (layout->labeltype >= LABEL_STATIC
527 && (layout->labeltype != LABEL_STATIC
528 || layout->latextype != LATEX_ENVIRONMENT
531 FontInfo const font = getLabelFont();
532 FontMetrics const & fm = theFontMetrics(font);
534 docstring const str = par_.getLabelstring();
538 // this is special code for the chapter layout. This is
539 // printed in an extra row and has a pagebreak at
541 if (layout->counter == "chapter") {
542 double spacing_val = 1.0;
543 if (!parparams.spacing().isDefault()) {
544 spacing_val = parparams.spacing().getValue();
546 spacing_val = buffer.params().spacing().getValue();
549 int const labeladdon = int(fm.maxHeight() * layout->spacing.getValue() * spacing_val);
551 int const maxdesc = int(fm.maxDescent() * layout->spacing.getValue() * spacing_val)
552 + int(layout->parsep) * defaultRowHeight();
555 x = width_ - leftMargin() -
559 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
562 x = width_ - leftMargin()
563 + fm.width(layout->labelsep);
565 x = x_ - fm.width(layout->labelsep)
569 pi_.pain.text(int(x), yo_, str, font);
573 // the labels at the top of an environment.
574 // More or less for bibliography
576 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
577 layout->labeltype == LABEL_BIBLIO ||
578 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
579 FontInfo const font = getLabelFont();
580 if (!par_.getLabelstring().empty()) {
581 docstring const str = par_.getLabelstring();
582 double spacing_val = 1.0;
583 if (!parparams.spacing().isDefault())
584 spacing_val = parparams.spacing().getValue();
586 spacing_val = buffer.params().spacing().getValue();
588 FontMetrics const & fm = theFontMetrics(font);
590 int const labeladdon = int(fm.maxHeight()
591 * layout->spacing.getValue() * spacing_val);
594 int(fm.maxDescent() * layout->spacing.getValue() * spacing_val
595 + (layout->labelbottomsep * defaultRowHeight()));
598 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
601 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
602 x -= fm.width(str) / 2;
604 x = width_ - leftMargin() - fm.width(str);
606 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
612 void RowPainter::paintLast()
614 bool const is_rtl = text_.isRTL(pi_.base.bv->buffer(), par_);
615 int const endlabel = getEndLabel(pit_, text_.paragraphs());
617 // paint imaginary end-of-paragraph character
619 if (par_.isInserted(par_.size()) || par_.isDeleted(par_.size())) {
620 FontMetrics const & fm = theFontMetrics(pi_.base.bv->buffer().params().getFont());
621 int const length = fm.maxAscent() / 2;
622 ColorCode col = par_.isInserted(par_.size()) ? Color_addedtext : Color_deletedtext;
624 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
625 Painter::line_solid, Painter::line_thick);
626 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1, yo_ + 2, col,
627 Painter::line_solid, Painter::line_thick);
634 case END_LABEL_FILLED_BOX: {
635 FontInfo const font = getLabelFont();
636 FontMetrics const & fm = theFontMetrics(font);
637 int const size = int(0.75 * fm.maxAscent());
638 int const y = yo_ - size;
639 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
641 if (width_ - int(row_.width()) <= size)
642 x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
644 if (endlabel == END_LABEL_BOX)
645 pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
647 pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
651 case END_LABEL_STATIC: {
652 FontInfo const font = getLabelFont();
653 FontMetrics const & fm = theFontMetrics(font);
654 docstring const & str = par_.layout()->endlabelstring();
655 double const x = is_rtl ?
657 : - text_metrics_.rightMargin(pm_) - row_.width();
658 pi_.pain.text(int(x), yo_, str, font);
662 case END_LABEL_NO_LABEL:
668 void RowPainter::paintOnlyInsets()
670 pos_type const end = row_.endpos();
671 for (pos_type pos = row_.pos(); pos != end; ++pos) {
672 if (!par_.isInset(pos))
675 // If outer row has changed, nested insets are repaint completely.
676 Inset const * inset = par_.getInset(pos);
678 if (x_ > pi_.base.bv->workWidth())
681 x_ = pi_.base.bv->coordCache().getInsets().x(inset);
682 paintInset(inset, pos);
687 void RowPainter::paintText()
689 pos_type const end = row_.endpos();
690 // Spaces at logical line breaks in bidi text must be skipped during
691 // painting. However, they may appear visually in the middle
692 // of a row; they must be skipped, wherever they are...
693 // * logically "abc_[HEBREW_\nHEBREW]"
694 // * visually "abc_[_WERBEH\nWERBEH]"
695 pos_type skipped_sep_vpos = -1;
696 pos_type body_pos = par_.beginOfBody();
698 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
702 LayoutPtr const & layout = par_.layout();
704 bool running_strikeout = false;
705 bool is_struckout = false;
706 int last_strikeout_x = 0;
708 // Use font span to speed things up, see below
712 // If the last logical character is a separator, don't paint it, unless
713 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
714 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
715 skipped_sep_vpos = bidi_.log2vis(end - 1);
717 for (pos_type vpos = row_.pos(); vpos < end; ) {
718 if (x_ > pi_.base.bv->workWidth())
721 // Skip the separator at the logical end of the row
722 if (vpos == skipped_sep_vpos) {
727 pos_type const pos = bidi_.vis2log(vpos);
729 if (pos >= par_.size()) {
734 // Use font span to speed things up, see above
735 if (vpos < font_span.first || vpos > font_span.last) {
736 font_span = par_.fontSpan(vpos);
737 font = text_metrics_.getDisplayFont(pit_, vpos);
740 const int width_pos = pm_.singleWidth(pos, font);
742 if (x_ + width_pos < 0) {
748 is_struckout = par_.isDeleted(pos);
750 if (is_struckout && !running_strikeout) {
751 running_strikeout = true;
752 last_strikeout_x = int(x_);
755 bool const highly_editable_inset = par_.isInset(pos)
756 && par_.getInset(pos)->editable() == Inset::HIGHLY_EDITABLE;
758 // If we reach the end of a struck out range, paint it.
759 // We also don't paint across things like tables
760 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
761 // Calculate 1/3 height of the buffer's default font
762 FontMetrics const & fm
763 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
764 int const middle = yo_ - fm.maxAscent() / 3;
765 pi_.pain.line(last_strikeout_x, middle, int(x_), middle,
766 Color_deletedtext, Painter::line_solid, Painter::line_thin);
767 running_strikeout = false;
770 if (body_pos > 0 && pos == body_pos - 1) {
771 int const lwidth = theFontMetrics(getLabelFont())
772 .width(layout->labelsep);
774 x_ += row_.label_hfill + lwidth - width_pos;
777 if (par_.isHfill(pos)) {
778 Inset const * inset = par_.getInset(pos);
779 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
780 paintHfill(pos, body_pos);
783 } else if (par_.isSeparator(pos)) {
784 Font const orig_font = text_metrics_.getDisplayFont(pit_, pos);
785 double const orig_x = x_;
788 x_ += row_.separator;
789 paintForeignMark(orig_x, orig_font.language());
792 } else if (par_.isInset(pos)) {
793 // If outer row has changed, nested insets are repaint completely.
794 Inset const * inset = par_.getInset(pos);
795 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
796 paintInset(inset, pos);
800 // paint as many characters as possible.
805 // if we reach the end of a struck out range, paint it
806 if (running_strikeout) {
807 // calculate 1/3 height of the buffer's default font
808 FontMetrics const & fm
809 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
810 int const middle = yo_ - fm.maxAscent() / 3;
811 pi_.pain.line(last_strikeout_x, middle, int(x_), middle,
812 Color_deletedtext, Painter::line_solid, Painter::line_thin);
813 running_strikeout = false;