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), x_(row_.x()),
124 width_(text_.textWidth()),
125 separator_(row_.fill_separator()),
126 hfill_(row_.fill_hfill()),
127 label_hfill_(row_.fill_label_hfill())
129 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo << " yo: " << yo
130 // << " pit->y: " << pit_->y
131 // << " row: " << (pit_->size() ? pit_->getChar(row_.pos()) : 'X') << endl;
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()) {
417 if (sel_on_one_row) {
425 pain_.fillRectangle(x, yo_, w, h, LColor::selection);
426 } else if (sel_starts_here) {
427 int const x = is_rtl ? 0 : startx;
428 int const w = is_rtl ? startx : (width_ - startx);
429 pain_.fillRectangle(x, yo_, w, h, LColor::selection);
430 } else if (sel_ends_here) {
431 int const x = is_rtl ? endx : 0;
432 int const w = is_rtl ? (width_ - endx) : endx;
433 pain_.fillRectangle(x, yo_, w, h, LColor::selection);
434 } else if (row_y > starty && row_y < endy) {
435 pain_.fillRectangle(int(xo_), yo_, width_, h, LColor::selection);
440 if ((startpit != pit_ && startrow != rit_ && !is_rtl)
441 || (endpit != pit_ && endrow != rit_ && is_rtl))
442 pain_.fillRectangle(int(xo_), yo_,
443 int(x_), h, LColor::selection);
445 pos_type const body_pos = pit_->beginOfBody();
446 pos_type const end = row_.endpos();
449 for (pos_type vpos = row_.pos(); vpos < end; ++vpos) {
450 pos_type pos = text_.bidi.vis2log(vpos);
451 double const old_tmpx = tmpx;
452 if (body_pos > 0 && pos == body_pos - 1) {
453 LyXLayout_ptr const & layout = pit_->layout();
454 LyXFont const lfont = getLabelFont();
456 tmpx += label_hfill_ + font_metrics::width(layout->labelsep, lfont);
458 if (pit_->isLineSeparator(body_pos - 1))
459 tmpx -= singleWidth(body_pos - 1);
462 tmpx += singleWidth(pos);
464 if (hfillExpansion(*pit_, row_, pos)) {
468 tmpx += label_hfill_;
470 if (pit_->isSeparator(pos) && pos >= body_pos)
474 if (((startpit != pit_ && startrow != rit_)
475 || cur.selBegin().pos() <= pos) &&
476 ((endpit != pit_ && endrow != rit_)
477 || pos < cur.selEnd().pos())) {
478 // Here we do not use x_ as xo_ was added to x_.
479 pain_.fillRectangle(int(old_tmpx), yo_,
480 int(tmpx - old_tmpx + 1), h, LColor::selection);
484 if ((startpit != pit_ && startrow != rit_ && is_rtl) ||
485 (endpit != pit_ && endrow != rit_ && !is_rtl)) {
486 pain_.fillRectangle(int(xo_ + tmpx),
487 yo_, int(width_ - tmpx), h, LColor::selection);
492 void RowPainter::paintChangeBar()
494 pos_type const start = row_.pos();
495 pos_type const end = row_.endpos();
497 if (start == end || !pit_->isChanged(start, end - 1))
500 int const height = text_.isLastRow(pit_, row_)
502 : row_.height() + boost::next(rit_)->top_of_text();
504 pain_.fillRectangle(4, yo_, 5, height, LColor::changebar);
508 void RowPainter::paintAppendix()
510 if (!pit_->params().appendix())
513 // FIXME: can be just width_ ?
514 int const ww = width_;
518 if (pit_->params().startOfAppendix())
519 y += 2 * defaultRowHeight();
521 pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
522 pain_.line(ww - 2, y, ww - 2, yo_ + row_.height(), LColor::appendix);
526 void RowPainter::paintDepthBar()
528 Paragraph::depth_type const depth = pit_->getDepth();
533 Paragraph::depth_type prev_depth = 0;
534 if (!text_.isFirstRow(pit_, row_)) {
535 ParagraphList::iterator pit2 = pit_;
538 prev_depth = pit2->getDepth();
541 Paragraph::depth_type next_depth = 0;
542 if (!text_.isLastRow(pit_, row_)) {
543 ParagraphList::iterator pit2 = pit_;
544 if (row_.endpos() >= pit2->size())
546 next_depth = pit2->getDepth();
549 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
550 int const w = NEST_MARGIN / 5;
551 int x = int(w * i + xo_);
552 // only consider the changebar space if we're drawing outer left
554 x += CHANGEBAR_MARGIN;
555 int const h = yo_ + row_.height() - 1 - (i - next_depth - 1) * 3;
557 pain_.line(x, yo_, x, h, LColor::depthbar);
560 pain_.fillRectangle(x, yo_, w, 2, LColor::depthbar);
562 pain_.fillRectangle(x, h, w, 2, LColor::depthbar);
567 int RowPainter::paintAppendixStart(int y)
570 pb_font.setColor(LColor::appendix);
573 string const label = _("Appendix");
577 font_metrics::rectText(label, pb_font, w, a, d);
579 int const text_start = int(xo_ + (width_ - w) / 2);
580 int const text_end = text_start + w;
582 pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
584 pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
585 pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
587 return 3 * defaultRowHeight();
591 void RowPainter::paintFirst()
593 ParagraphParameters const & parparams = pit_->params();
597 // start of appendix?
598 if (parparams.startOfAppendix())
599 y_top += paintAppendixStart(yo_ + y_top + 2 * defaultRowHeight());
601 Buffer const & buffer = *bv_.buffer();
603 LyXLayout_ptr const & layout = pit_->layout();
605 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
606 if (pit_ != text_.paragraphs().begin()) {
607 if (layout->latextype == LATEX_PARAGRAPH
608 && !pit_->getDepth()) {
609 y_top += buffer.params().getDefSkip().inPixels(bv_);
611 LyXLayout_ptr const & playout =
612 boost::prior(pit_)->layout();
613 if (playout->latextype == LATEX_PARAGRAPH
614 && !boost::prior(pit_)->getDepth()) {
615 // is it right to use defskip here, too? (AS)
616 y_top += buffer.params().getDefSkip().inPixels(bv_);
622 int const ww = bv_.workWidth();
624 bool const is_rtl = text_.isRTL(*pit_);
625 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
626 //lyxerr << "paintFirst: " << pit_->id() << " is_seq: " << is_seq << std::endl;
628 // should we print a label?
629 if (layout->labeltype >= LABEL_STATIC
630 && (layout->labeltype != LABEL_STATIC
631 || layout->latextype != LATEX_ENVIRONMENT
634 LyXFont font = getLabelFont();
635 if (!pit_->getLabelstring().empty()) {
637 string const str = pit_->getLabelstring();
639 // this is special code for the chapter layout. This is
640 // printed in an extra row and has a pagebreak at
642 if (layout->counter == "chapter") {
643 if (buffer.params().secnumdepth >= 0) {
644 float spacing_val = 1.0;
645 if (!parparams.spacing().isDefault()) {
646 spacing_val = parparams.spacing().getValue();
648 spacing_val = buffer.params().spacing().getValue();
652 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
653 + int(layout->parsep) * defaultRowHeight();
656 x = ww - leftMargin() -
657 font_metrics::width(str, font);
661 yo_ + row_.baseline() -
662 row_.ascent_of_text() - maxdesc,
667 x = ww - leftMargin()
668 + font_metrics::width(layout->labelsep, font);
670 x = x_ - font_metrics::width(layout->labelsep, font)
671 - font_metrics::width(str, font);
674 pain_.text(int(x), yo_ + row_.baseline(), str, font);
678 // the labels at the top of an environment.
679 // More or less for bibliography
681 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
682 layout->labeltype == LABEL_BIBLIO ||
683 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
684 LyXFont font = getLabelFont();
685 if (!pit_->getLabelstring().empty()) {
686 string const str = pit_->getLabelstring();
687 float spacing_val = 1.0;
688 if (!parparams.spacing().isDefault()) {
689 spacing_val = parparams.spacing().getValue();
691 spacing_val = buffer.params().spacing().getValue();
695 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
696 + (layout->labelbottomsep * defaultRowHeight()));
699 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
700 x = ((is_rtl ? leftMargin() : x_)
701 + ww - text_.rightMargin(*pit_)) / 2;
702 x -= font_metrics::width(str, font) / 2;
704 x = ww - leftMargin() -
705 font_metrics::width(str, font);
708 yo_ + row_.baseline() - row_.ascent_of_text() - maxdesc,
715 void RowPainter::paintLast()
717 int const ww = bv_.workWidth();
718 bool const is_rtl = text_.isRTL(*pit_);
719 int const endlabel = getEndLabel(pit_, text_.paragraphs());
724 case END_LABEL_FILLED_BOX: {
725 LyXFont const font = getLabelFont();
726 int const size = int(0.75 * font_metrics::maxAscent(font));
727 int const y = yo_ + row_.baseline() - size;
728 int x = is_rtl ? NEST_MARGIN + CHANGEBAR_MARGIN: ww - size;
730 if (row_.fill() <= size)
731 x += (size - row_.fill() + 1) * (is_rtl ? -1 : 1);
733 if (endlabel == END_LABEL_BOX)
734 pain_.rectangle(x, y, size, size, LColor::eolmarker);
736 pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
740 case END_LABEL_STATIC: {
741 LyXFont font = getLabelFont();
742 string const & str = pit_->layout()->endlabelstring();
743 double const x = is_rtl ?
744 x_ - font_metrics::width(str, font)
745 : ww - text_.rightMargin(*pit_) - row_.fill();
746 pain_.text(int(x), yo_ + row_.baseline(), str, font);
750 case END_LABEL_NO_LABEL:
756 void RowPainter::paintText()
758 pos_type const end = row_.endpos();
759 pos_type body_pos = pit_->beginOfBody();
761 (body_pos > end || !pit_->isLineSeparator(body_pos - 1))) {
765 LyXLayout_ptr const & layout = pit_->layout();
767 bool running_strikeout = false;
768 bool is_struckout = false;
769 int last_strikeout_x = 0;
771 for (pos_type vpos = row_.pos(); vpos < end; ) {
772 if (x_ > bv_.workWidth())
775 pos_type pos = text_.bidi.vis2log(vpos);
777 if (pos >= pit_->size()) {
782 if (x_ + singleWidth(pos) < 0) {
783 x_ += singleWidth(pos);
788 is_struckout = isDeletedText(*pit_, pos);
790 if (is_struckout && !running_strikeout) {
791 running_strikeout = true;
792 last_strikeout_x = int(x_);
795 bool const highly_editable_inset = pit_->isInset(pos)
796 && isHighlyEditableInset(pit_->getInset(pos));
798 // if we reach the end of a struck out range, paint it
799 // we also don't paint across things like tables
800 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
801 int const middle = yo_ + (row_.baseline() + row_.top_of_text()) / 2;
802 pain_.line(last_strikeout_x, middle, int(x_), middle,
803 LColor::strikeout, Painter::line_solid, Painter::line_thin);
804 running_strikeout = false;
807 if (body_pos > 0 && pos == body_pos - 1) {
808 int const lwidth = font_metrics::width(layout->labelsep,
811 x_ += label_hfill_ + lwidth - singleWidth(body_pos - 1);
814 if (pit_->isHfill(pos)) {
817 int const y0 = yo_ + row_.baseline();
818 int const y1 = y0 - defaultRowHeight() / 2;
820 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
822 if (hfillExpansion(*pit_, row_, pos)) {
823 int const y2 = (y0 + y1) / 2;
825 if (pos >= body_pos) {
826 pain_.line(int(x_), y2, int(x_ + hfill_), y2,
828 Painter::line_onoffdash);
831 pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
833 Painter::line_onoffdash);
836 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
840 } else if (pit_->isSeparator(pos)) {
841 x_ += singleWidth(pos);
850 // if we reach the end of a struck out range, paint it
851 if (running_strikeout) {
852 int const middle = yo_ + (row_.baseline() + row_.top_of_text()) / 2;
853 pain_.line(last_strikeout_x, middle, int(x_), middle,
854 LColor::strikeout, Painter::line_solid, Painter::line_thin);
855 running_strikeout = false;
860 int paintPars(BufferView const & bv, LyXText const & text,
861 ParagraphList::iterator pit, int xo, int yo, int y)
863 //lyxerr << " paintRows: pit: " << &*pit << endl;
864 int const y2 = bv.painter().paperHeight();
867 ParagraphList::iterator end = text.paragraphs().end();
869 for ( ; pit != end; ++pit) {
870 RowList::iterator row = pit->rows.begin();
871 RowList::iterator rend = pit->rows.end();
873 for ( ; row != rend; ++row) {
874 RowPainter(bv, text, pit, row, xo, y + yo);
887 int paintText(BufferView const & bv)
889 ParagraphList::iterator pit;
890 bv.text()->updateParPositions();
891 bv.text()->getRowNearY(bv.top_y(), pit);
892 //lyxerr << "top_y: " << bv.top_y() << " y: " << pit->y << endl;
893 return paintPars(bv, *bv.text(), pit, 0, 0, pit->y);
897 void paintTextInset(LyXText const & text, PainterInfo & pi, int xo, int yo)
899 paintPars(*pi.base.bv, text, text.paragraphs().begin(), xo, yo, 0);