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);
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().getLyXTextClass().defaultfont() :
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)
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;
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);
320 // usual characters, no insets
321 char const c = par_.getChar(pos);
323 // special case languages
324 std::string const & lang = orig_font.language()->lang();
325 bool const hebrew = lang == "hebrew";
326 bool const arabic = lang == "arabic" &&
327 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
328 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
330 // draw as many chars as we can
331 if ((!hebrew && !arabic)
332 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
333 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
334 paintChars(vpos, orig_font, hebrew, arabic);
336 paintHebrewComposeChar(vpos, orig_font);
338 paintArabicComposeChar(vpos, orig_font);
341 paintForeignMark(orig_x, orig_font);
345 void RowPainter::paintChangeBar()
347 pos_type const start = row_.pos();
348 pos_type const end = row_.endpos();
350 if (start == end || !par_.isChanged(start, end - 1))
353 int const height = text_.isLastRow(pit_, row_)
357 pain_.fillRectangle(4, yo_ - row_.ascent(), 5, height, LColor::changebar);
361 void RowPainter::paintAppendix()
363 if (!par_.params().appendix())
366 int y = yo_ - row_.ascent();
368 if (par_.params().startOfAppendix())
369 y += 2 * defaultRowHeight();
371 pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
372 pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), LColor::appendix);
376 void RowPainter::paintDepthBar()
378 Paragraph::depth_type const depth = par_.getDepth();
383 Paragraph::depth_type prev_depth = 0;
384 if (!text_.isFirstRow(pit_, row_)) {
385 pit_type pit2 = pit_;
388 prev_depth = pars_[pit2].getDepth();
391 Paragraph::depth_type next_depth = 0;
392 if (!text_.isLastRow(pit_, row_)) {
393 pit_type pit2 = pit_;
394 if (row_.endpos() >= pars_[pit2].size())
396 next_depth = pars_[pit2].getDepth();
399 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
400 int const w = nestMargin() / 5;
401 int x = int(xo_) + w * i;
402 // only consider the changebar space if we're drawing outermost text
403 if (text_.isMainText())
404 x += changebarMargin();
406 int const starty = yo_ - row_.ascent();
407 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
409 pain_.line(x, starty, x, starty + h, LColor::depthbar);
412 pain_.fillRectangle(x, starty, w, 2, LColor::depthbar);
414 pain_.fillRectangle(x, starty + h, w, 2, LColor::depthbar);
419 int RowPainter::paintAppendixStart(int y)
422 pb_font.setColor(LColor::appendix);
425 string const label = _("Appendix");
429 font_metrics::rectText(label, pb_font, w, a, d);
431 int const text_start = int(xo_ + (width_ - w) / 2);
432 int const text_end = text_start + w;
434 pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
436 pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
437 pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
439 return 3 * defaultRowHeight();
443 void RowPainter::paintFirst()
445 ParagraphParameters const & parparams = par_.params();
449 // start of appendix?
450 if (parparams.startOfAppendix())
451 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
453 Buffer const & buffer = *bv_.buffer();
455 LyXLayout_ptr const & layout = par_.layout();
457 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
459 if (layout->latextype == LATEX_PARAGRAPH
460 && !par_.getDepth()) {
461 y_top += buffer.params().getDefSkip().inPixels(bv_);
463 LyXLayout_ptr const & playout = pars_[pit_ - 1].layout();
464 if (playout->latextype == LATEX_PARAGRAPH
465 && !pars_[pit_ - 1].getDepth()) {
466 // is it right to use defskip here, too? (AS)
467 y_top += buffer.params().getDefSkip().inPixels(bv_);
473 bool const is_rtl = text_.isRTL(par_);
474 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
475 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
477 // should we print a label?
478 if (layout->labeltype >= LABEL_STATIC
479 && (layout->labeltype != LABEL_STATIC
480 || layout->latextype != LATEX_ENVIRONMENT
483 LyXFont const font = getLabelFont();
484 string const str = par_.getLabelstring();
488 // this is special code for the chapter layout. This is
489 // printed in an extra row and has a pagebreak at
491 if (layout->counter == "chapter") {
492 double spacing_val = 1.0;
493 if (!parparams.spacing().isDefault()) {
494 spacing_val = parparams.spacing().getValue();
496 spacing_val = buffer.params().spacing().getValue();
499 int const labeladdon = int(font_metrics::maxHeight(font) * layout->spacing.getValue() * spacing_val);
501 int const maxdesc = int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
502 + int(layout->parsep) * defaultRowHeight();
505 x = width_ - leftMargin() -
506 font_metrics::width(str, font);
509 pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
512 x = width_ - leftMargin()
513 + font_metrics::width(layout->labelsep, font);
515 x = x_ - font_metrics::width(layout->labelsep, font)
516 - font_metrics::width(str, font);
519 pain_.text(int(x), yo_, str, font);
523 // the labels at the top of an environment.
524 // More or less for bibliography
526 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
527 layout->labeltype == LABEL_BIBLIO ||
528 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
529 LyXFont font = getLabelFont();
530 if (!par_.getLabelstring().empty()) {
531 string const str = par_.getLabelstring();
532 double spacing_val = 1.0;
533 if (!parparams.spacing().isDefault())
534 spacing_val = parparams.spacing().getValue();
536 spacing_val = buffer.params().spacing().getValue();
538 int const labeladdon = int(font_metrics::maxHeight(font) * layout->spacing.getValue() * spacing_val);
541 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
542 + (layout->labelbottomsep * defaultRowHeight()));
545 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
548 x += (width_ - text_.rightMargin(par_) - leftMargin()) / 2;
549 x -= font_metrics::width(str, font) / 2;
551 x = width_ - leftMargin() -
552 font_metrics::width(str, font);
554 pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
560 void RowPainter::paintLast()
562 bool const is_rtl = text_.isRTL(par_);
563 int const endlabel = getEndLabel(pit_, text_.paragraphs());
568 case END_LABEL_FILLED_BOX: {
569 LyXFont const font = getLabelFont();
570 int const size = int(0.75 * font_metrics::maxAscent(font));
571 int const y = yo_ - size;
572 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
574 if (width_ - int(row_.width()) <= size)
575 x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
577 if (endlabel == END_LABEL_BOX)
578 pain_.rectangle(x, y, size, size, LColor::eolmarker);
580 pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
584 case END_LABEL_STATIC: {
585 LyXFont font = getLabelFont();
586 string const & str = par_.layout()->endlabelstring();
587 double const x = is_rtl ?
588 x_ - font_metrics::width(str, font)
589 : - text_.rightMargin(par_) - row_.width();
590 pain_.text(int(x), yo_, str, font);
594 case END_LABEL_NO_LABEL:
600 void RowPainter::paintText()
602 pos_type const end = row_.endpos();
603 pos_type body_pos = par_.beginOfBody();
605 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
609 LyXLayout_ptr const & layout = par_.layout();
611 bool running_strikeout = false;
612 bool is_struckout = false;
613 int last_strikeout_x = 0;
615 // Use font span to speed things up, see below
619 for (pos_type vpos = row_.pos(); vpos < end; ) {
620 if (x_ > bv_.workWidth())
623 pos_type const pos = text_.bidi.vis2log(vpos);
625 if (pos >= par_.size()) {
630 // Use font span to speed things up, see above
631 if (vpos < font_span.first || vpos > font_span.last) {
632 font_span = par_.fontSpan(vpos);
633 font = text_.getFont(par_, vpos);
636 const int width_pos =
637 text_.singleWidth(par_, pos, par_.getChar(pos), font);
639 if (x_ + width_pos < 0) {
645 is_struckout = isDeletedText(par_, pos);
647 if (is_struckout && !running_strikeout) {
648 running_strikeout = true;
649 last_strikeout_x = int(x_);
652 bool const highly_editable_inset = par_.isInset(pos)
653 && isHighlyEditableInset(par_.getInset(pos));
655 // If we reach the end of a struck out range, paint it.
656 // We also don't paint across things like tables
657 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
658 // Calculate 1/3 height of the buffer's default font
660 yo_ - font_metrics::maxAscent(text_.defaultfont_) / 3;
661 pain_.line(last_strikeout_x, middle, int(x_), middle,
662 LColor::strikeout, Painter::line_solid, Painter::line_thin);
663 running_strikeout = false;
666 if (body_pos > 0 && pos == body_pos - 1) {
667 int const lwidth = font_metrics::width(layout->labelsep,
670 x_ += label_hfill_ + lwidth - width_pos;
673 if (par_.isHfill(pos)) {
677 int const y1 = y0 - defaultRowHeight() / 2;
679 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
681 if (hfillExpansion(par_, row_, pos)) {
682 int const y2 = (y0 + y1) / 2;
684 if (pos >= body_pos) {
685 pain_.line(int(x_), y2, int(x_ + hfill_), y2,
687 Painter::line_onoffdash);
690 pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
692 Painter::line_onoffdash);
695 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
699 } else if (par_.isSeparator(pos)) {
709 // if we reach the end of a struck out range, paint it
710 if (running_strikeout) {
711 // calculate 1/3 height of the buffer's default font
713 yo_ - font_metrics::maxAscent(text_.defaultfont_) / 3;
714 pain_.line(last_strikeout_x, middle, int(x_), middle,
715 LColor::strikeout, Painter::line_solid, Painter::line_thin);
716 running_strikeout = false;
721 lyx::size_type calculateRowSignature(Row const & row, Paragraph const & par)
723 boost::crc_32_type crc;
724 for (lyx::pos_type i = row.pos(); i < row.endpos(); ++i) {
725 const unsigned char b[] = { par.getChar(i) };
726 crc.process_bytes(b, 1);
728 return crc.checksum();
732 bool isCursorOnRow(PainterInfo & pi, pit_type pit, RowList::const_iterator rit)
734 LCursor & cur = pi.base.bv->cursor();
735 for (lyx::size_type d = 0; d < cur.depth(); d++)
736 if (cur[d].pit() == pit
737 && cur[d].pos() >= rit->pos()
738 && cur[d].pos() < rit->endpos())
745 (PainterInfo & pi, LyXText const & text, pit_type pit, int x, int y,
748 // lyxerr << " paintPar: pit: " << pit << " at y: " << y << endl;
749 static NullPainter nop;
750 static PainterInfo nullpi(pi.base.bv, nop);
751 int const ww = pi.base.bv->workHeight();
753 Paragraph const & par = text.paragraphs()[pit];
755 RowList::const_iterator const rb = par.rows().begin();
756 RowList::const_iterator const re = par.rows().end();
757 theCoords.parPos()[&text][pit] = Point(x, y);
760 lyx::size_type rowno(0);
761 for (RowList::const_iterator rit = rb; rit != re; ++rit, ++rowno) {
764 // Row signature; has row changed since last paint?
765 lyx::size_type const row_sig = calculateRowSignature(*rit, par);
767 bool cursor_on_row = isCursorOnRow(pi, pit, rit);
769 // If selection is on, the current row signature differs from
770 // from cache, or cursor is inside an inset _on this row_,
771 // then paint the row
772 if (repaintAll || par.rowSignature()[rowno] != row_sig
774 // Add to row signature cache
775 par.rowSignature()[rowno] = row_sig;
777 bool const inside = (y + rit->descent() >= 0
778 && y - rit->ascent() < ww);
779 RowPainter rp(inside ? pi : nullpi, text, pit, *rit, x, y);
780 // Clear background of this row
781 // (if paragraph background was not cleared)
783 pi.pain.fillRectangle(x, y - rit->ascent(),
784 pi.base.bv->workWidth(), rit->height(),
785 text.backgroundColor());
788 // Instrumentation for testing row cache (see also
802 //lyxerr << "." << endl;
808 void paintText(BufferView const & bv, ViewMetricsInfo const & vi)
810 Painter & pain = bv.painter();
811 LyXText * const text = bv.text();
812 bool const select = bv.cursor().selection();
814 PainterInfo pi(const_cast<BufferView *>(&bv), pain);
815 if (select || !vi.singlepar) {
816 // Clear background (Delegated to rows if no selection)
817 pain.fillRectangle(0, vi.y1, bv.workWidth(), vi.y2 - vi.y1,
818 text->backgroundColor());
821 text->drawSelection(pi, 0, 0);
826 for (pit_type pit = vi.p1; pit <= vi.p2; ++pit) {
827 Paragraph const & par = text->getPar(pit);
829 paintPar(pi, *bv.text(), pit, 0, yy, select || !vi.singlepar);
833 // Cache one paragraph above and one below
834 // Note MV: this cannot be suppressed even for singlepar.
835 // Try viewing the User Guide Mobius figure
838 text->redoParagraph(vi.p1 - 1);
839 theCoords.parPos()[bv.text()][vi.p1 - 1] =
840 Point(0, vi.y1 - text->getPar(vi.p1 - 1).descent());
843 if (vi.p2 < lyx::pit_type(text->paragraphs().size()) - 1) {
844 text->redoParagraph(vi.p2 + 1);
845 theCoords.parPos()[bv.text()][vi.p2 + 1] =
846 Point(0, vi.y2 + text->getPar(vi.p2 + 1).ascent());
849 // and grey out above (should not happen later)
850 // lyxerr << "par ascent: " << text->getPar(vi.p1).ascent() << endl;
851 if (vi.y1 > 0 && !vi.singlepar)
852 pain.fillRectangle(0, 0, bv.workWidth(), vi.y1, LColor::bottomarea);
854 // and possibly grey out below
855 // lyxerr << "par descent: " << text->getPar(vi.p1).ascent() << endl;
856 if (vi.y2 < bv.workHeight() && !vi.singlepar)
857 pain.fillRectangle(0, vi.y2, bv.workWidth(), bv.workHeight() - vi.y2, LColor::bottomarea);
861 void paintTextInset(LyXText const & text, PainterInfo & pi, int x, int y)
863 // lyxerr << " paintTextInset: y: " << y << endl;
865 y -= text.getPar(0).ascent();
866 for (int pit = 0; pit < int(text.paragraphs().size()); ++pit) {
867 y += text.getPar(pit).ascent();
868 paintPar(pi, text, pit, x, y, true);
869 y += text.getPar(pit).descent();