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"
19 #include "bufferparams.h"
20 #include "BufferView.h"
27 #include "lyxrow_funcs.h"
28 #include "metricsinfo.h"
29 #include "paragraph.h"
30 #include "paragraph_funcs.h"
31 #include "ParagraphParameters.h"
34 #include "frontends/font_metrics.h"
35 #include "frontends/Painter.h"
37 #include "insets/insettext.h"
39 #include "support/textutils.h"
47 extern int NEST_MARGIN;
48 extern int CHANGEBAR_MARGIN;
54 * A class used for painting an individual row of text.
58 /// initialise and run painter
59 RowPainter(BufferView const & bv, LyXText const & text,
60 ParagraphList::iterator pit, RowList::iterator rit, int xo, int yo);
62 // paint various parts
63 void paintBackground();
64 void paintSelection();
67 void paintChangeBar();
70 void paintForeignMark(double orig_x, LyXFont const & orig_font);
71 void paintHebrewComposeChar(lyx::pos_type & vpos);
72 void paintArabicComposeChar(lyx::pos_type & vpos);
73 void paintChars(lyx::pos_type & vpos, bool hebrew, bool arabic);
74 int paintAppendixStart(int y);
76 void paintFromPos(lyx::pos_type & vpos);
77 void paintInset(lyx::pos_type const pos);
79 /// return left margin
80 int leftMargin() const;
82 /// return the font at the given pos
83 LyXFont const getFont(lyx::pos_type pos) const;
85 /// return the label font for this row
86 LyXFont const getLabelFont() const;
88 /// return pixel width for the given pos
89 int singleWidth(lyx::pos_type pos) const;
90 int singleWidth(lyx::pos_type pos, char c) const;
92 /// bufferview to paint on
93 BufferView const & bv_;
98 /// LyXText for the row
99 LyXText const & text_;
102 RowList::iterator const rit_;
106 mutable ParagraphList::iterator pit_;
119 RowPainter::RowPainter(BufferView const & bv, LyXText const & text,
120 ParagraphList::iterator pit, RowList::iterator rit,
122 : bv_(bv), pain_(bv_.painter()), text_(text), rit_(rit), row_(*rit),
123 pit_(pit), xo_(xo), yo_(yo), width_(text_.width())
125 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo << " yo: " << yo
126 // << " pit->y: " << pit_->y
127 // << " row: " << (pit_->size() ? pit_->getChar(row_.pos()) : 'X') << endl;
128 RowMetrics m = text_.computeRowMetrics(pit, row_);
130 separator_ = m.separator;
132 label_hfill_ = m.label_hfill;
134 // background has already been cleared.
135 if (&text_ == bv_.text())
138 // paint the selection background
139 if (bv_.cursor().selection() && &text_ == bv_.cursor().text())
142 // vertical lines for appendix
145 // environment depth brackets
154 if (row_.endpos() >= pit_->size())
163 LyXFont const RowPainter::getFont(pos_type pos) const
165 return text_.getFont(pit_, pos);
169 int RowPainter::singleWidth(lyx::pos_type pos) const
171 return text_.singleWidth(pit_, pos);
175 int RowPainter::singleWidth(lyx::pos_type pos, char c) const
177 LyXFont const & font = text_.getFont(pit_, pos);
178 return text_.singleWidth(pit_, pos, c, font);
182 LyXFont const RowPainter::getLabelFont() const
184 return text_.getLabelFont(pit_);
188 int RowPainter::leftMargin() const
190 return text_.leftMargin(pit_, row_.pos());
194 void RowPainter::paintInset(pos_type const pos)
196 InsetBase const * inset = pit_->getInset(pos);
198 PainterInfo pi(const_cast<BufferView *>(&bv_));
199 pi.base.font = getFont(pos);
200 inset->drawSelection(pi, int(x_), yo_ + row_.baseline());
201 inset->draw(pi, int(x_), yo_ + row_.baseline());
202 x_ += inset->width();
206 void RowPainter::paintHebrewComposeChar(pos_type & vpos)
208 pos_type pos = text_.bidi.vis2log(vpos);
213 char c = pit_->getChar(pos);
217 LyXFont const & font = getFont(pos);
218 int const width = font_metrics::width(c, font);
221 for (pos_type i = pos - 1; i >= 0; --i) {
222 c = pit_->getChar(i);
223 if (!Encodings::IsComposeChar_hebrew(c)) {
224 if (IsPrintableNonspace(c)) {
225 int const width2 = singleWidth(i, c);
227 dx = (c == 'ø' || c == 'ã')
229 : (width2 - width) / 2;
236 pain_.text(int(x_) + dx, yo_ + row_.baseline(), str, font);
240 void RowPainter::paintArabicComposeChar(pos_type & vpos)
242 pos_type pos = text_.bidi.vis2log(vpos);
246 char c = pit_->getChar(pos);
247 c = pit_->transformChar(c, pos);
251 LyXFont const & font = getFont(pos);
252 int const width = font_metrics::width(c, font);
255 for (pos_type i = pos - 1; i >= 0; --i) {
256 c = pit_->getChar(i);
257 if (!Encodings::IsComposeChar_arabic(c)) {
258 if (IsPrintableNonspace(c)) {
259 int const width2 = singleWidth(i, c);
260 dx = (width2 - width) / 2;
266 pain_.text(int(x_) + dx, yo_ + row_.baseline(), str, font);
270 void RowPainter::paintChars(pos_type & vpos, bool hebrew, bool arabic)
272 pos_type pos = text_.bidi.vis2log(vpos);
273 pos_type const end = row_.endpos();
274 LyXFont orig_font = getFont(pos);
278 str += pit_->getChar(pos);
280 unsigned char c = str[0];
281 str[0] = pit_->transformChar(c, pos);
284 bool prev_struckout = isDeletedText(*pit_, pos);
285 bool prev_newtext = isInsertedText(*pit_, pos);
287 // collect as much similar chars as we can
288 for (++vpos; vpos < end && (pos = text_.bidi.vis2log(vpos)) >= 0; ++vpos) {
289 char c = pit_->getChar(pos);
291 if (!IsPrintableNonspace(c))
294 if (prev_struckout != isDeletedText(*pit_, pos))
297 if (prev_newtext != isInsertedText(*pit_, pos))
300 if (arabic && Encodings::IsComposeChar_arabic(c))
303 if (hebrew && Encodings::IsComposeChar_hebrew(c))
306 if (orig_font != getFont(pos))
310 c = pit_->transformChar(c, pos);
316 orig_font.setColor(LColor::strikeout);
317 else if (prev_newtext)
318 orig_font.setColor(LColor::newtext);
320 // Draw text and set the new x position
321 //lyxerr << "paint row: yo_ " << yo_ << " baseline: " << row_.baseline()
323 pain_.text(int(x_), yo_ + row_.baseline(), str, orig_font);
324 x_ += font_metrics::width(str, orig_font);
328 void RowPainter::paintForeignMark(double orig_x, LyXFont const & orig_font)
330 if (!lyxrc.mark_foreign_language)
332 if (orig_font.language() == latex_language)
334 if (orig_font.language() == bv_.buffer()->params().language)
337 int const y = yo_ + row_.baseline() + 1;
338 pain_.line(int(orig_x), y, int(x_), y, LColor::language);
342 void RowPainter::paintFromPos(pos_type & vpos)
344 pos_type const pos = text_.bidi.vis2log(vpos);
346 LyXFont const & orig_font = getFont(pos);
348 double const orig_x = x_;
350 char const c = pit_->getChar(pos);
352 if (c == Paragraph::META_INSET) {
355 paintForeignMark(orig_x, orig_font);
359 // usual characters, no insets
361 // special case languages
362 bool const hebrew = (orig_font.language()->lang() == "hebrew");
364 orig_font.language()->lang() == "arabic" &&
365 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
366 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
368 // draw as many chars as we can
369 if ((!hebrew && !arabic)
370 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
371 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
372 paintChars(vpos, hebrew, arabic);
374 paintHebrewComposeChar(vpos);
376 paintArabicComposeChar(vpos);
379 paintForeignMark(orig_x, orig_font);
383 void RowPainter::paintBackground()
385 int const x = int(xo_);
386 int const y = yo_ < 0 ? 0 : yo_;
387 int const h = yo_ < 0 ? row_.height() + yo_ : row_.height();
388 pain_.fillRectangle(x, y, width_, h, text_.backgroundColor());
392 void RowPainter::paintSelection()
394 bool const is_rtl = text_.isRTL(*pit_);
396 // the current selection
397 LCursor const & cur = bv_.cursor();
398 int const startx = text_.cursorX(cur.selBegin());
399 int const endx = text_.cursorX(cur.selEnd());
400 int const starty = text_.cursorY(cur.selBegin());
401 int const endy = text_.cursorY(cur.selEnd());
402 ParagraphList::iterator startpit = text_.getPar(cur.selBegin());
403 ParagraphList::iterator endpit = text_.getPar(cur.selEnd());
404 RowList::iterator startrow = startpit->getRow(cur.selBegin().pos());
405 RowList::iterator endrow = endpit->getRow(cur.selEnd().pos());
406 int const h = row_.height();
408 int const row_y = text_.yo_ + pit_->y + row_.y_offset();
410 bool const sel_starts_here = startpit == pit_ && startrow == rit_;
411 bool const sel_ends_here = endpit == pit_ && endrow == rit_;
412 bool const sel_on_one_row = sel_starts_here && sel_ends_here;
414 if (text_.bidi.same_direction()) {
415 if (sel_on_one_row) {
416 int const x1 = is_rtl ? endx : startx;
417 int const x2 = is_rtl ? startx : endx;
418 pain_.fillRectangle(x1, yo_, x2 - x1, h, LColor::selection);
419 } else if (sel_starts_here) {
420 int const x1 = is_rtl ? int(xo_) : startx;
421 int const x2 = is_rtl ? startx : int(xo_) + width_;
422 pain_.fillRectangle(x1, yo_, x2 - x1, h, LColor::selection);
423 } else if (sel_ends_here) {
424 int const x1 = is_rtl ? endx : int(xo_);
425 int const x2 = is_rtl ? int(xo_) + width_ : endx;
426 pain_.fillRectangle(x1, yo_, x2 - x1, h, LColor::selection);
427 } else if (row_y > starty && row_y < endy) {
428 pain_.fillRectangle(int(xo_), yo_, width_, h, LColor::selection);
433 if ((startpit != pit_ && startrow != rit_ && !is_rtl)
434 || (endpit != pit_ && endrow != rit_ && is_rtl))
435 pain_.fillRectangle(int(xo_), yo_,
436 int(x_), h, LColor::selection);
438 pos_type const body_pos = pit_->beginOfBody();
439 pos_type const end = row_.endpos();
442 for (pos_type vpos = row_.pos(); vpos < end; ++vpos) {
443 pos_type pos = text_.bidi.vis2log(vpos);
444 double const old_tmpx = tmpx;
445 if (body_pos > 0 && pos == body_pos - 1) {
446 LyXLayout_ptr const & layout = pit_->layout();
447 LyXFont const lfont = getLabelFont();
449 tmpx += label_hfill_ + font_metrics::width(layout->labelsep, lfont);
451 if (pit_->isLineSeparator(body_pos - 1))
452 tmpx -= singleWidth(body_pos - 1);
455 tmpx += singleWidth(pos);
457 if (hfillExpansion(*pit_, row_, pos)) {
461 tmpx += label_hfill_;
463 if (pit_->isSeparator(pos) && pos >= body_pos)
467 if (((startpit != pit_ && startrow != rit_)
468 || cur.selBegin().pos() <= pos) &&
469 ((endpit != pit_ && endrow != rit_)
470 || pos < cur.selEnd().pos())) {
471 // Here we do not use x_ as xo_ was added to x_.
472 pain_.fillRectangle(int(old_tmpx), yo_,
473 int(tmpx - old_tmpx + 1), h, LColor::selection);
477 if ((startpit != pit_ && startrow != rit_ && is_rtl) ||
478 (endpit != pit_ && endrow != rit_ && !is_rtl)) {
479 pain_.fillRectangle(int(xo_ + tmpx),
480 yo_, int(width_ - tmpx), h, LColor::selection);
485 void RowPainter::paintChangeBar()
487 pos_type const start = row_.pos();
488 pos_type const end = row_.endpos();
490 if (start == end || !pit_->isChanged(start, end - 1))
493 int const height = text_.isLastRow(pit_, row_)
495 : row_.height() + boost::next(rit_)->top_of_text();
497 pain_.fillRectangle(4, yo_, 5, height, LColor::changebar);
501 void RowPainter::paintAppendix()
503 if (!pit_->params().appendix())
508 if (pit_->params().startOfAppendix())
509 y += 2 * defaultRowHeight();
511 pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
512 pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), LColor::appendix);
516 void RowPainter::paintDepthBar()
518 Paragraph::depth_type const depth = pit_->getDepth();
523 Paragraph::depth_type prev_depth = 0;
524 if (!text_.isFirstRow(pit_, row_)) {
525 ParagraphList::iterator pit2 = pit_;
528 prev_depth = pit2->getDepth();
531 Paragraph::depth_type next_depth = 0;
532 if (!text_.isLastRow(pit_, row_)) {
533 ParagraphList::iterator pit2 = pit_;
534 if (row_.endpos() >= pit2->size())
536 next_depth = pit2->getDepth();
539 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
540 int const w = NEST_MARGIN / 5;
541 int x = int(w * i + xo_);
542 // only consider the changebar space if we're drawing outer left
544 x += CHANGEBAR_MARGIN;
545 int const h = yo_ + row_.height() - 1 - (i - next_depth - 1) * 3;
547 pain_.line(x, yo_, x, h, LColor::depthbar);
550 pain_.fillRectangle(x, yo_, w, 2, LColor::depthbar);
552 pain_.fillRectangle(x, h, w, 2, LColor::depthbar);
557 int RowPainter::paintAppendixStart(int y)
560 pb_font.setColor(LColor::appendix);
563 string const label = _("Appendix");
567 font_metrics::rectText(label, pb_font, w, a, d);
569 int const text_start = int(xo_ + (width_ - w) / 2);
570 int const text_end = text_start + w;
572 pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
574 pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
575 pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
577 return 3 * defaultRowHeight();
581 void RowPainter::paintFirst()
583 ParagraphParameters const & parparams = pit_->params();
587 // start of appendix?
588 if (parparams.startOfAppendix())
589 y_top += paintAppendixStart(yo_ + y_top + 2 * defaultRowHeight());
591 Buffer const & buffer = *bv_.buffer();
593 LyXLayout_ptr const & layout = pit_->layout();
595 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
596 if (pit_ != text_.paragraphs().begin()) {
597 if (layout->latextype == LATEX_PARAGRAPH
598 && !pit_->getDepth()) {
599 y_top += buffer.params().getDefSkip().inPixels(bv_);
601 LyXLayout_ptr const & playout =
602 boost::prior(pit_)->layout();
603 if (playout->latextype == LATEX_PARAGRAPH
604 && !boost::prior(pit_)->getDepth()) {
605 // is it right to use defskip here, too? (AS)
606 y_top += buffer.params().getDefSkip().inPixels(bv_);
612 bool const is_rtl = text_.isRTL(*pit_);
613 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
614 //lyxerr << "paintFirst: " << pit_->id() << " is_seq: " << is_seq << std::endl;
616 // should we print a label?
617 if (layout->labeltype >= LABEL_STATIC
618 && (layout->labeltype != LABEL_STATIC
619 || layout->latextype != LATEX_ENVIRONMENT
622 LyXFont font = getLabelFont();
623 if (!pit_->getLabelstring().empty()) {
625 string const str = pit_->getLabelstring();
627 // this is special code for the chapter layout. This is
628 // printed in an extra row and has a pagebreak at
630 if (layout->counter == "chapter") {
631 if (buffer.params().secnumdepth >= 0) {
632 float spacing_val = 1.0;
633 if (!parparams.spacing().isDefault()) {
634 spacing_val = parparams.spacing().getValue();
636 spacing_val = buffer.params().spacing().getValue();
640 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
641 + int(layout->parsep) * defaultRowHeight();
644 x = width_ - leftMargin() -
645 font_metrics::width(str, font);
649 yo_ + row_.baseline() -
650 row_.ascent_of_text() - maxdesc,
655 x = width_ - leftMargin()
656 + font_metrics::width(layout->labelsep, font);
658 x = x_ - font_metrics::width(layout->labelsep, font)
659 - font_metrics::width(str, font);
662 pain_.text(int(x), yo_ + row_.baseline(), str, font);
666 // the labels at the top of an environment.
667 // More or less for bibliography
669 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
670 layout->labeltype == LABEL_BIBLIO ||
671 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
672 LyXFont font = getLabelFont();
673 if (!pit_->getLabelstring().empty()) {
674 string const str = pit_->getLabelstring();
675 float spacing_val = 1.0;
676 if (!parparams.spacing().isDefault()) {
677 spacing_val = parparams.spacing().getValue();
679 spacing_val = buffer.params().spacing().getValue();
683 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
684 + (layout->labelbottomsep * defaultRowHeight()));
687 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
688 x = ((is_rtl ? leftMargin() : x_)
689 + width_ - text_.rightMargin(*pit_)) / 2;
690 x -= font_metrics::width(str, font) / 2;
692 x = width_ - leftMargin() -
693 font_metrics::width(str, font);
696 yo_ + row_.baseline() - row_.ascent_of_text() - maxdesc,
703 void RowPainter::paintLast()
705 bool const is_rtl = text_.isRTL(*pit_);
706 int const endlabel = getEndLabel(pit_, text_.paragraphs());
711 case END_LABEL_FILLED_BOX: {
712 LyXFont const font = getLabelFont();
713 int const size = int(0.75 * font_metrics::maxAscent(font));
714 int const y = yo_ + row_.baseline() - size;
715 int x = is_rtl ? NEST_MARGIN + CHANGEBAR_MARGIN: width_ - size;
717 if (width_ - int(row_.width()) <= size)
718 x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
720 if (endlabel == END_LABEL_BOX)
721 pain_.rectangle(x, y, size, size, LColor::eolmarker);
723 pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
727 case END_LABEL_STATIC: {
728 LyXFont font = getLabelFont();
729 string const & str = pit_->layout()->endlabelstring();
730 double const x = is_rtl ?
731 x_ - font_metrics::width(str, font)
732 : - text_.rightMargin(*pit_) - row_.width();
733 pain_.text(int(x), yo_ + row_.baseline(), str, font);
737 case END_LABEL_NO_LABEL:
743 void RowPainter::paintText()
745 pos_type const end = row_.endpos();
746 pos_type body_pos = pit_->beginOfBody();
748 (body_pos > end || !pit_->isLineSeparator(body_pos - 1))) {
752 LyXLayout_ptr const & layout = pit_->layout();
754 bool running_strikeout = false;
755 bool is_struckout = false;
756 int last_strikeout_x = 0;
758 for (pos_type vpos = row_.pos(); vpos < end; ) {
759 if (x_ > bv_.workWidth())
762 pos_type pos = text_.bidi.vis2log(vpos);
764 if (pos >= pit_->size()) {
769 if (x_ + singleWidth(pos) < 0) {
770 x_ += singleWidth(pos);
775 is_struckout = isDeletedText(*pit_, pos);
777 if (is_struckout && !running_strikeout) {
778 running_strikeout = true;
779 last_strikeout_x = int(x_);
782 bool const highly_editable_inset = pit_->isInset(pos)
783 && isHighlyEditableInset(pit_->getInset(pos));
785 // if we reach the end of a struck out range, paint it
786 // we also don't paint across things like tables
787 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
788 int const middle = yo_ + (row_.baseline() + row_.top_of_text()) / 2;
789 pain_.line(last_strikeout_x, middle, int(x_), middle,
790 LColor::strikeout, Painter::line_solid, Painter::line_thin);
791 running_strikeout = false;
794 if (body_pos > 0 && pos == body_pos - 1) {
795 int const lwidth = font_metrics::width(layout->labelsep,
798 x_ += label_hfill_ + lwidth - singleWidth(body_pos - 1);
801 if (pit_->isHfill(pos)) {
804 int const y0 = yo_ + row_.baseline();
805 int const y1 = y0 - defaultRowHeight() / 2;
807 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
809 if (hfillExpansion(*pit_, row_, pos)) {
810 int const y2 = (y0 + y1) / 2;
812 if (pos >= body_pos) {
813 pain_.line(int(x_), y2, int(x_ + hfill_), y2,
815 Painter::line_onoffdash);
818 pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
820 Painter::line_onoffdash);
823 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
827 } else if (pit_->isSeparator(pos)) {
828 x_ += singleWidth(pos);
837 // if we reach the end of a struck out range, paint it
838 if (running_strikeout) {
839 int const middle = yo_ + (row_.baseline() + row_.top_of_text()) / 2;
840 pain_.line(last_strikeout_x, middle, int(x_), middle,
841 LColor::strikeout, Painter::line_solid, Painter::line_thin);
842 running_strikeout = false;
847 int paintPars(BufferView const & bv, LyXText const & text,
848 ParagraphList::iterator pit, int xo, int yo, int y)
850 //lyxerr << " paintRows: pit: " << &*pit << endl;
851 int const y2 = bv.painter().paperHeight();
854 ParagraphList::iterator end = text.paragraphs().end();
856 for ( ; pit != end; ++pit) {
857 RowList::iterator row = pit->rows.begin();
858 RowList::iterator rend = pit->rows.end();
860 for ( ; row != rend; ++row) {
861 RowPainter(bv, text, pit, row, xo, y + yo);
874 int paintText(BufferView const & bv)
876 ParagraphList::iterator pit;
877 bv.text()->updateParPositions();
878 bv.text()->getRowNearY(bv.top_y(), pit);
879 //lyxerr << "top_y: " << bv.top_y() << " y: " << pit->y << endl;
880 return paintPars(bv, *bv.text(), pit, 0, 0, pit->y);
884 void paintTextInset(LyXText const & text, PainterInfo & pi, int xo, int yo)
886 paintPars(*pi.base.bv, text, text.paragraphs().begin(), xo, yo, 0);