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"
17 #include "coordcache.h"
20 #include "bufferparams.h"
21 #include "BufferView.h"
28 #include "lyxrow_funcs.h"
29 #include "metricsinfo.h"
30 #include "paragraph.h"
31 #include "paragraph_funcs.h"
32 #include "ParagraphParameters.h"
35 #include "frontends/font_metrics.h"
36 #include "frontends/nullpainter.h"
37 #include "frontends/Painter.h"
39 #include "insets/insettext.h"
41 #include "support/textutils.h"
43 #include <boost/crc.hpp>
57 * A class used for painting an individual row of text.
61 /// initialise and run painter
62 RowPainter(PainterInfo & pi, LyXText const & text,
63 pit_type pit, Row const & row, int x, int y);
65 // paint various parts
68 void paintChangeBar();
74 void paintForeignMark(double orig_x, LyXFont const & font, int desc = 0);
75 void paintHebrewComposeChar(lyx::pos_type & vpos, LyXFont const & font);
76 void paintArabicComposeChar(lyx::pos_type & vpos, LyXFont const & font);
77 void paintChars(lyx::pos_type & vpos, LyXFont font,
78 bool hebrew, bool arabic);
79 int paintAppendixStart(int y);
80 void paintFromPos(lyx::pos_type & vpos);
81 void paintInset(lyx::pos_type const pos, LyXFont const & font);
83 /// return left margin
84 int leftMargin() const;
86 /// return the label font for this row
87 LyXFont const getLabelFont() const;
89 /// bufferview to paint on
90 BufferView const & bv_;
95 /// LyXText for the row
96 LyXText const & text_;
97 ParagraphList const & pars_;
104 Paragraph const & par_;
106 /// is row erased? (change tracking)
111 int const yo_; // current baseline
120 RowPainter::RowPainter(PainterInfo & pi,
121 LyXText const & text, pit_type pit, Row const & row, int x, int y)
122 : bv_(*pi.base.bv), pain_(pi.pain), text_(text), pars_(text.paragraphs()),
123 row_(row), pit_(pit), par_(text.paragraphs()[pit]),
125 xo_(x), yo_(y), width_(text_.width())
127 RowMetrics m = text_.computeRowMetrics(pit, row_);
130 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
133 separator_ = m.separator;
135 label_hfill_ = m.label_hfill;
137 BOOST_ASSERT(pit >= 0);
138 BOOST_ASSERT(pit < int(text.paragraphs().size()));
142 LyXFont const RowPainter::getLabelFont() const
144 return text_.getLabelFont(par_);
148 int RowPainter::leftMargin() const
150 return text_.leftMargin(pit_, row_.pos());
154 void RowPainter::paintInset(pos_type const pos, LyXFont const & font)
156 InsetBase const * inset = par_.getInset(pos);
158 PainterInfo pi(const_cast<BufferView *>(&bv_), pain_);
159 // FIXME: We should always use font, see documentation of
160 // noFontChange() in insetbase.h.
161 pi.base.font = inset->noFontChange() ?
162 bv_.buffer()->params().getFont() :
164 pi.ltr_pos = (text_.bidi.level(pos) % 2 == 0);
165 pi.erased_ = erased_ || isDeletedText(par_, pos);
166 theCoords.insets().add(inset, int(x_), yo_);
167 inset->drawSelection(pi, int(x_), yo_);
168 inset->draw(pi, int(x_), yo_);
169 x_ += inset->width();
173 void RowPainter::paintHebrewComposeChar(pos_type & vpos, LyXFont const & font)
175 pos_type pos = text_.bidi.vis2log(vpos);
180 char c = par_.getChar(pos);
184 int const width = font_metrics::width(c, font);
187 for (pos_type i = pos - 1; i >= 0; --i) {
189 if (!Encodings::IsComposeChar_hebrew(c)) {
190 if (IsPrintableNonspace(c)) {
192 text_.singleWidth(par_, i, c, text_.getFont(par_, i));
194 dx = (c == 'ø' || c == 'ã')
196 : (width2 - width) / 2;
203 pain_.text(int(x_) + dx, yo_, str, font);
207 void RowPainter::paintArabicComposeChar(pos_type & vpos, LyXFont const & font)
209 pos_type pos = text_.bidi.vis2log(vpos);
213 char c = par_.getChar(pos);
214 c = par_.transformChar(c, pos);
218 int const width = font_metrics::width(c, font);
221 for (pos_type i = pos - 1; i >= 0; --i) {
223 if (!Encodings::IsComposeChar_arabic(c)) {
224 if (IsPrintableNonspace(c)) {
226 text_.singleWidth(par_, i, c, text_.getFont(par_, i));
227 dx = (width2 - width) / 2;
233 pain_.text(int(x_) + dx, yo_, str, font);
237 void RowPainter::paintChars(pos_type & vpos, LyXFont font,
238 bool hebrew, bool arabic)
240 pos_type pos = text_.bidi.vis2log(vpos);
241 pos_type const end = row_.endpos();
242 FontSpan const font_span = par_.fontSpan(pos);
243 Change::Type const prev_change = par_.lookupChange(pos);
247 str += par_.getChar(pos);
249 unsigned char c = str[0];
250 str[0] = par_.transformChar(c, pos);
253 // collect as much similar chars as we can
254 for (++vpos ; vpos < end ; ++vpos) {
255 pos = text_.bidi.vis2log(vpos);
256 if (pos < font_span.first || pos > font_span.last)
259 if (prev_change != par_.lookupChange(pos))
262 char c = par_.getChar(pos);
264 if (!IsPrintableNonspace(c))
267 if (arabic && Encodings::IsComposeChar_arabic(c))
270 if (hebrew && Encodings::IsComposeChar_hebrew(c))
274 c = par_.transformChar(c, pos);
279 if (prev_change == Change::DELETED)
280 font.setColor(LColor::strikeout);
281 else if (prev_change == Change::INSERTED)
282 font.setColor(LColor::newtext);
284 // Draw text and set the new x position
285 //lyxerr << "paint row: yo_ " << yo_ << "\n";
286 pain_.text(int(x_), yo_, str, font);
287 x_ += font_metrics::width(str, font);
291 void RowPainter::paintForeignMark(double orig_x, LyXFont const & font, int desc)
293 if (!lyxrc.mark_foreign_language)
295 if (font.language() == latex_language)
297 if (font.language() == bv_.buffer()->params().language)
300 int const y = yo_ + 1 + desc;
301 pain_.line(int(orig_x), y, int(x_), y, LColor::language);
305 void RowPainter::paintFromPos(pos_type & vpos)
307 pos_type const pos = text_.bidi.vis2log(vpos);
308 LyXFont orig_font = text_.getFont(par_, pos);
309 text_.applyOuterFont(orig_font);
311 double const orig_x = x_;
313 if (par_.isInset(pos)) {
314 paintInset(pos, orig_font);
316 paintForeignMark(orig_x, orig_font,
317 par_.getInset(pos)->descent());
321 // usual characters, no insets
322 char const c = par_.getChar(pos);
324 // special case languages
325 std::string const & lang = orig_font.language()->lang();
326 bool const hebrew = lang == "hebrew";
327 bool const arabic = lang == "arabic" &&
328 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
329 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
331 // draw as many chars as we can
332 if ((!hebrew && !arabic)
333 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
334 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
335 paintChars(vpos, orig_font, hebrew, arabic);
337 paintHebrewComposeChar(vpos, orig_font);
339 paintArabicComposeChar(vpos, orig_font);
342 paintForeignMark(orig_x, orig_font);
346 void RowPainter::paintChangeBar()
348 pos_type const start = row_.pos();
349 pos_type const end = row_.endpos();
351 if (start == end || !par_.isChanged(start, end - 1))
354 int const height = text_.isLastRow(pit_, row_)
358 pain_.fillRectangle(4, yo_ - row_.ascent(), 5, height, LColor::changebar);
362 void RowPainter::paintAppendix()
364 if (!par_.params().appendix())
367 int y = yo_ - row_.ascent();
369 if (par_.params().startOfAppendix())
370 y += 2 * defaultRowHeight();
372 pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
373 pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), LColor::appendix);
377 void RowPainter::paintDepthBar()
379 Paragraph::depth_type const depth = par_.getDepth();
384 Paragraph::depth_type prev_depth = 0;
385 if (!text_.isFirstRow(pit_, row_)) {
386 pit_type pit2 = pit_;
389 prev_depth = pars_[pit2].getDepth();
392 Paragraph::depth_type next_depth = 0;
393 if (!text_.isLastRow(pit_, row_)) {
394 pit_type pit2 = pit_;
395 if (row_.endpos() >= pars_[pit2].size())
397 next_depth = pars_[pit2].getDepth();
400 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
401 int const w = nestMargin() / 5;
402 int x = int(xo_) + w * i;
403 // only consider the changebar space if we're drawing outermost text
404 if (text_.isMainText())
405 x += changebarMargin();
407 int const starty = yo_ - row_.ascent();
408 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
410 pain_.line(x, starty, x, starty + h, LColor::depthbar);
413 pain_.fillRectangle(x, starty, w, 2, LColor::depthbar);
415 pain_.fillRectangle(x, starty + h, w, 2, LColor::depthbar);
420 int RowPainter::paintAppendixStart(int y)
423 pb_font.setColor(LColor::appendix);
426 string const label = _("Appendix");
430 font_metrics::rectText(label, pb_font, w, a, d);
432 int const text_start = int(xo_ + (width_ - w) / 2);
433 int const text_end = text_start + w;
435 pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
437 pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
438 pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
440 return 3 * defaultRowHeight();
444 void RowPainter::paintFirst()
446 ParagraphParameters const & parparams = par_.params();
450 // start of appendix?
451 if (parparams.startOfAppendix())
452 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
454 Buffer const & buffer = *bv_.buffer();
456 LyXLayout_ptr const & layout = par_.layout();
458 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
460 if (layout->latextype == LATEX_PARAGRAPH
461 && !par_.getDepth()) {
462 y_top += buffer.params().getDefSkip().inPixels(bv_);
464 LyXLayout_ptr const & playout = pars_[pit_ - 1].layout();
465 if (playout->latextype == LATEX_PARAGRAPH
466 && !pars_[pit_ - 1].getDepth()) {
467 // is it right to use defskip here, too? (AS)
468 y_top += buffer.params().getDefSkip().inPixels(bv_);
474 bool const is_rtl = text_.isRTL(par_);
475 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
476 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
478 // should we print a label?
479 if (layout->labeltype >= LABEL_STATIC
480 && (layout->labeltype != LABEL_STATIC
481 || layout->latextype != LATEX_ENVIRONMENT
484 LyXFont const font = getLabelFont();
485 string const str = par_.getLabelstring();
489 // this is special code for the chapter layout. This is
490 // printed in an extra row and has a pagebreak at
492 if (layout->counter == "chapter") {
493 double spacing_val = 1.0;
494 if (!parparams.spacing().isDefault()) {
495 spacing_val = parparams.spacing().getValue();
497 spacing_val = buffer.params().spacing().getValue();
500 int const labeladdon = int(font_metrics::maxHeight(font) * layout->spacing.getValue() * spacing_val);
502 int const maxdesc = int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
503 + int(layout->parsep) * defaultRowHeight();
506 x = width_ - leftMargin() -
507 font_metrics::width(str, font);
510 pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
513 x = width_ - leftMargin()
514 + font_metrics::width(layout->labelsep, font);
516 x = x_ - font_metrics::width(layout->labelsep, font)
517 - font_metrics::width(str, font);
520 pain_.text(int(x), yo_, str, font);
524 // the labels at the top of an environment.
525 // More or less for bibliography
527 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
528 layout->labeltype == LABEL_BIBLIO ||
529 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
530 LyXFont font = getLabelFont();
531 if (!par_.getLabelstring().empty()) {
532 string const str = par_.getLabelstring();
533 double spacing_val = 1.0;
534 if (!parparams.spacing().isDefault())
535 spacing_val = parparams.spacing().getValue();
537 spacing_val = buffer.params().spacing().getValue();
539 int const labeladdon = int(font_metrics::maxHeight(font) * layout->spacing.getValue() * spacing_val);
542 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
543 + (layout->labelbottomsep * defaultRowHeight()));
546 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
549 x += (width_ - text_.rightMargin(par_) - leftMargin()) / 2;
550 x -= font_metrics::width(str, font) / 2;
552 x = width_ - leftMargin() -
553 font_metrics::width(str, font);
555 pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
561 void RowPainter::paintLast()
563 bool const is_rtl = text_.isRTL(par_);
564 int const endlabel = getEndLabel(pit_, text_.paragraphs());
569 case END_LABEL_FILLED_BOX: {
570 LyXFont const font = getLabelFont();
571 int const size = int(0.75 * font_metrics::maxAscent(font));
572 int const y = yo_ - size;
573 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
575 if (width_ - int(row_.width()) <= size)
576 x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
578 if (endlabel == END_LABEL_BOX)
579 pain_.rectangle(x, y, size, size, LColor::eolmarker);
581 pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
585 case END_LABEL_STATIC: {
586 LyXFont font = getLabelFont();
587 string const & str = par_.layout()->endlabelstring();
588 double const x = is_rtl ?
589 x_ - font_metrics::width(str, font)
590 : - text_.rightMargin(par_) - row_.width();
591 pain_.text(int(x), yo_, str, font);
595 case END_LABEL_NO_LABEL:
601 void RowPainter::paintText()
603 pos_type const end = row_.endpos();
604 pos_type body_pos = par_.beginOfBody();
606 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
610 LyXLayout_ptr const & layout = par_.layout();
612 bool running_strikeout = false;
613 bool is_struckout = false;
614 int last_strikeout_x = 0;
616 // Use font span to speed things up, see below
620 for (pos_type vpos = row_.pos(); vpos < end; ) {
621 if (x_ > bv_.workWidth())
624 pos_type const pos = text_.bidi.vis2log(vpos);
626 if (pos >= par_.size()) {
631 // Use font span to speed things up, see above
632 if (vpos < font_span.first || vpos > font_span.last) {
633 font_span = par_.fontSpan(vpos);
634 font = text_.getFont(par_, vpos);
637 const int width_pos =
638 text_.singleWidth(par_, pos, par_.getChar(pos), font);
640 if (x_ + width_pos < 0) {
646 is_struckout = isDeletedText(par_, pos);
648 if (is_struckout && !running_strikeout) {
649 running_strikeout = true;
650 last_strikeout_x = int(x_);
653 bool const highly_editable_inset = par_.isInset(pos)
654 && isHighlyEditableInset(par_.getInset(pos));
656 // If we reach the end of a struck out range, paint it.
657 // We also don't paint across things like tables
658 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
659 // Calculate 1/3 height of the buffer's default font
662 font_metrics::maxAscent(bv_.buffer()->params().getFont()) / 3;
663 pain_.line(last_strikeout_x, middle, int(x_), middle,
664 LColor::strikeout, Painter::line_solid, Painter::line_thin);
665 running_strikeout = false;
668 if (body_pos > 0 && pos == body_pos - 1) {
669 int const lwidth = font_metrics::width(layout->labelsep,
672 x_ += label_hfill_ + lwidth - width_pos;
675 if (par_.isHfill(pos)) {
679 int const y1 = y0 - defaultRowHeight() / 2;
681 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
683 if (hfillExpansion(par_, row_, pos)) {
684 int const y2 = (y0 + y1) / 2;
686 if (pos >= body_pos) {
687 pain_.line(int(x_), y2, int(x_ + hfill_), y2,
689 Painter::line_onoffdash);
692 pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
694 Painter::line_onoffdash);
697 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
701 } else if (par_.isSeparator(pos)) {
711 // if we reach the end of a struck out range, paint it
712 if (running_strikeout) {
713 // calculate 1/3 height of the buffer's default font
716 font_metrics::maxAscent(bv_.buffer()->params().getFont()) / 3;
717 pain_.line(last_strikeout_x, middle, int(x_), middle,
718 LColor::strikeout, Painter::line_solid, Painter::line_thin);
719 running_strikeout = false;
724 lyx::size_type calculateRowSignature(Row const & row, Paragraph const & par)
726 boost::crc_32_type crc;
727 for (lyx::pos_type i = row.pos(); i < row.endpos(); ++i) {
728 const unsigned char b[] = { par.getChar(i) };
729 crc.process_bytes(b, 1);
731 return crc.checksum();
735 bool isCursorOnRow(PainterInfo & pi, pit_type pit, RowList::const_iterator rit)
737 LCursor & cur = pi.base.bv->cursor();
738 for (lyx::size_type d = 0; d < cur.depth(); d++)
739 if (cur[d].pit() == pit
740 && cur[d].pos() >= rit->pos()
741 && cur[d].pos() <= rit->endpos())
748 (PainterInfo & pi, LyXText const & text, pit_type pit, int x, int y,
751 // lyxerr << " paintPar: pit: " << pit << " at y: " << y << endl;
752 static NullPainter nop;
753 static PainterInfo nullpi(pi.base.bv, nop);
754 int const ww = pi.base.bv->workHeight();
756 Paragraph const & par = text.paragraphs()[pit];
758 RowList::const_iterator const rb = par.rows().begin();
759 RowList::const_iterator const re = par.rows().end();
760 theCoords.parPos()[&text][pit] = Point(x, y);
763 lyx::size_type rowno(0);
764 for (RowList::const_iterator rit = rb; rit != re; ++rit, ++rowno) {
767 // Row signature; has row changed since last paint?
768 lyx::size_type const row_sig = calculateRowSignature(*rit, par);
770 bool cursor_on_row = isCursorOnRow(pi, pit, rit);
772 // If selection is on, the current row signature differs from
773 // from cache, or cursor is inside an inset _on this row_,
774 // then paint the row
775 if (repaintAll || par.rowSignature()[rowno] != row_sig
777 // Add to row signature cache
778 par.rowSignature()[rowno] = row_sig;
780 bool const inside = (y + rit->descent() >= 0
781 && y - rit->ascent() < ww);
782 RowPainter rp(inside ? pi : nullpi, text, pit, *rit, x, y);
783 // Clear background of this row
784 // (if paragraph background was not cleared)
786 pi.pain.fillRectangle(x, y - rit->ascent(),
787 pi.base.bv->workWidth(), rit->height(),
788 text.backgroundColor());
791 // Instrumentation for testing row cache (see also
805 //lyxerr << "." << endl;
811 void paintText(BufferView const & bv, ViewMetricsInfo const & vi)
813 Painter & pain = bv.painter();
814 LyXText * const text = bv.text();
815 bool const select = bv.cursor().selection();
817 PainterInfo pi(const_cast<BufferView *>(&bv), pain);
818 if (select || !vi.singlepar) {
819 // Clear background (Delegated to rows if no selection)
820 pain.fillRectangle(0, vi.y1, bv.workWidth(), vi.y2 - vi.y1,
821 text->backgroundColor());
824 text->drawSelection(pi, 0, 0);
829 for (pit_type pit = vi.p1; pit <= vi.p2; ++pit) {
830 Paragraph const & par = text->getPar(pit);
832 paintPar(pi, *bv.text(), pit, 0, yy, select || !vi.singlepar);
836 // Cache one paragraph above and one below
837 // Note MV: this cannot be suppressed even for singlepar.
838 // Try viewing the User Guide Mobius figure
841 text->redoParagraph(vi.p1 - 1);
842 theCoords.parPos()[bv.text()][vi.p1 - 1] =
843 Point(0, vi.y1 - text->getPar(vi.p1 - 1).descent());
846 if (vi.p2 < lyx::pit_type(text->paragraphs().size()) - 1) {
847 text->redoParagraph(vi.p2 + 1);
848 theCoords.parPos()[bv.text()][vi.p2 + 1] =
849 Point(0, vi.y2 + text->getPar(vi.p2 + 1).ascent());
852 // and grey out above (should not happen later)
853 // lyxerr << "par ascent: " << text->getPar(vi.p1).ascent() << endl;
854 if (vi.y1 > 0 && !vi.singlepar)
855 pain.fillRectangle(0, 0, bv.workWidth(), vi.y1, LColor::bottomarea);
857 // and possibly grey out below
858 // lyxerr << "par descent: " << text->getPar(vi.p1).ascent() << endl;
859 if (vi.y2 < bv.workHeight() && !vi.singlepar)
860 pain.fillRectangle(0, vi.y2, bv.workWidth(), bv.workHeight() - vi.y2, LColor::bottomarea);
864 void paintTextInset(LyXText const & text, PainterInfo & pi, int x, int y)
866 // lyxerr << " paintTextInset: y: " << y << endl;
868 y -= text.getPar(0).ascent();
869 for (int pit = 0; pit < int(text.paragraphs().size()); ++pit) {
870 y += text.getPar(pit).ascent();
871 paintPar(pi, text, pit, x, y, true);
872 y += text.getPar(pit).descent();