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;
53 * A class used for painting an individual row of text.
57 /// initialise and run painter
58 RowPainter(BufferView const & bv, LyXText const & text,
59 ParagraphList::iterator pit, RowList::iterator rit, int xo, int yo);
61 // paint various parts
62 void paintBackground();
63 void paintSelection();
66 void paintChangeBar();
69 void paintForeignMark(double orig_x, LyXFont const & orig_font);
70 void paintHebrewComposeChar(lyx::pos_type & vpos);
71 void paintArabicComposeChar(lyx::pos_type & vpos);
72 void paintChars(lyx::pos_type & vpos, bool hebrew, bool arabic);
73 int paintAppendixStart(int y);
75 void paintFromPos(lyx::pos_type & vpos);
76 void paintInset(lyx::pos_type const pos);
78 /// return left margin
79 int leftMargin() const;
81 /// return the font at the given pos
82 LyXFont const getFont(lyx::pos_type pos) const;
84 /// return the label font for this row
85 LyXFont const getLabelFont() const;
87 /// return pixel width for the given pos
88 int singleWidth(lyx::pos_type pos) const;
89 int singleWidth(lyx::pos_type pos, char c) const;
91 /// bufferview to paint on
92 BufferView const & bv_;
97 /// LyXText for the row
98 LyXText const & text_;
101 RowList::iterator const rit_;
105 mutable ParagraphList::iterator pit_;
118 RowPainter::RowPainter(BufferView const & bv, LyXText const & text,
119 ParagraphList::iterator pit, RowList::iterator rit,
121 : bv_(bv), pain_(bv_.painter()), text_(text), rit_(rit), row_(*rit),
122 pit_(pit), xo_(xo), yo_(yo), x_(row_.x()),
123 width_(text_.textWidth()),
124 separator_(row_.fill_separator()),
125 hfill_(row_.fill_hfill()),
126 label_hfill_(row_.fill_label_hfill())
130 // background has already been cleared.
131 if (&text_ == bv_.text())
134 // paint the selection background
135 if (bv_.cursor().selection() && &text_ == bv_.cursor().innerText())
138 // vertical lines for appendix
141 // environment depth brackets
150 if (row_.endpos() >= pit_->size())
159 LyXFont const RowPainter::getFont(pos_type pos) const
161 return text_.getFont(pit_, pos);
165 int RowPainter::singleWidth(lyx::pos_type pos) const
167 return text_.singleWidth(pit_, pos);
171 int RowPainter::singleWidth(lyx::pos_type pos, char c) const
173 LyXFont const & font = text_.getFont(pit_, pos);
174 return text_.singleWidth(pit_, pos, c, font);
178 LyXFont const RowPainter::getLabelFont() const
180 return text_.getLabelFont(pit_);
184 int RowPainter::leftMargin() const
186 return text_.leftMargin(pit_, row_.pos());
190 void RowPainter::paintInset(pos_type const pos)
192 InsetBase * inset = const_cast<InsetBase *>(pit_->getInset(pos));
194 PainterInfo pi(const_cast<BufferView *>(&bv_));
195 pi.base.font = getFont(pos);
196 inset->draw(pi, int(x_), yo_ + row_.baseline());
197 x_ += inset->width();
201 void RowPainter::paintHebrewComposeChar(pos_type & vpos)
203 pos_type pos = text_.bidi.vis2log(vpos);
208 char c = pit_->getChar(pos);
212 LyXFont const & font = getFont(pos);
213 int const width = font_metrics::width(c, font);
216 for (pos_type i = pos - 1; i >= 0; --i) {
217 c = pit_->getChar(i);
218 if (!Encodings::IsComposeChar_hebrew(c)) {
219 if (IsPrintableNonspace(c)) {
220 int const width2 = singleWidth(i, c);
222 dx = (c == 'ø' || c == 'ã')
224 : (width2 - width) / 2;
231 pain_.text(int(x_) + dx, yo_ + row_.baseline(), str, font);
235 void RowPainter::paintArabicComposeChar(pos_type & vpos)
237 pos_type pos = text_.bidi.vis2log(vpos);
241 char c = pit_->getChar(pos);
242 c = pit_->transformChar(c, pos);
246 LyXFont const & font = getFont(pos);
247 int const width = font_metrics::width(c, font);
250 for (pos_type i = pos - 1; i >= 0; --i) {
251 c = pit_->getChar(i);
252 if (!Encodings::IsComposeChar_arabic(c)) {
253 if (IsPrintableNonspace(c)) {
254 int const width2 = singleWidth(i, c);
255 dx = (width2 - width) / 2;
261 pain_.text(int(x_) + dx, yo_ + row_.baseline(), str, font);
265 void RowPainter::paintChars(pos_type & vpos, bool hebrew, bool arabic)
267 pos_type pos = text_.bidi.vis2log(vpos);
268 pos_type const end = row_.endpos();
269 LyXFont orig_font = getFont(pos);
273 str += pit_->getChar(pos);
275 unsigned char c = str[0];
276 str[0] = pit_->transformChar(c, pos);
279 bool prev_struckout = isDeletedText(*pit_, pos);
280 bool prev_newtext = isInsertedText(*pit_, pos);
282 // collect as much similar chars as we can
283 for (++vpos; vpos < end && (pos = text_.bidi.vis2log(vpos)) >= 0; ++vpos) {
284 char c = pit_->getChar(pos);
286 if (!IsPrintableNonspace(c))
289 if (prev_struckout != isDeletedText(*pit_, pos))
292 if (prev_newtext != isInsertedText(*pit_, pos))
295 if (arabic && Encodings::IsComposeChar_arabic(c))
298 if (hebrew && Encodings::IsComposeChar_hebrew(c))
301 if (orig_font != getFont(pos))
305 c = pit_->transformChar(c, pos);
311 orig_font.setColor(LColor::strikeout);
312 else if (prev_newtext)
313 orig_font.setColor(LColor::newtext);
315 // Draw text and set the new x position
316 //lyxerr << "paint row: yo_ " << yo_ << " baseline: " << row_.baseline()
318 pain_.text(int(x_), yo_ + row_.baseline(), str, orig_font);
319 x_ += font_metrics::width(str, orig_font);
323 void RowPainter::paintForeignMark(double orig_x, LyXFont const & orig_font)
325 if (!lyxrc.mark_foreign_language)
327 if (orig_font.language() == latex_language)
329 if (orig_font.language() == bv_.buffer()->params().language)
332 int const y = yo_ + row_.baseline() + 1;
333 pain_.line(int(orig_x), y, int(x_), y, LColor::language);
337 void RowPainter::paintFromPos(pos_type & vpos)
339 pos_type const pos = text_.bidi.vis2log(vpos);
341 LyXFont const & orig_font = getFont(pos);
343 double const orig_x = x_;
345 char const c = pit_->getChar(pos);
347 if (c == Paragraph::META_INSET) {
350 paintForeignMark(orig_x, orig_font);
354 // usual characters, no insets
356 // special case languages
357 bool const hebrew = (orig_font.language()->lang() == "hebrew");
359 orig_font.language()->lang() == "arabic" &&
360 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
361 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
363 // draw as many chars as we can
364 if ((!hebrew && !arabic)
365 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
366 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
367 paintChars(vpos, hebrew, arabic);
369 paintHebrewComposeChar(vpos);
371 paintArabicComposeChar(vpos);
374 paintForeignMark(orig_x, orig_font);
378 void RowPainter::paintBackground()
380 int const x = int(xo_);
381 int const y = yo_ < 0 ? 0 : yo_;
382 int const h = yo_ < 0 ? row_.height() + yo_ : row_.height();
383 pain_.fillRectangle(x, y, width_, h, text_.backgroundColor());
387 void RowPainter::paintSelection()
389 bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params());
391 // the current selection
392 LCursor const & cur = bv_.cursor();
393 int const startx = text_.cursorX(cur.selBegin());
394 int const endx = text_.cursorX(cur.selEnd());
395 int const starty = text_.cursorY(cur.selBegin());
396 int const endy = text_.cursorY(cur.selEnd());
397 ParagraphList::iterator startpit = text_.getPar(cur.selBegin());
398 ParagraphList::iterator endpit = text_.getPar(cur.selEnd());
399 RowList::iterator startrow = startpit->getRow(cur.selBegin().pos());
400 RowList::iterator endrow = endpit->getRow(cur.selEnd().pos());
401 int const h = row_.height();
403 int const row_y = pit_->y + row_.y_offset();
405 bool const sel_starts_here = startpit == pit_ && startrow == rit_;
406 bool const sel_ends_here = endpit == pit_ && endrow == rit_;
407 bool const sel_on_one_row = sel_starts_here && sel_ends_here;
409 if (text_.bidi.same_direction()) {
412 if (sel_on_one_row) {
414 x = int(xo_) + startx;
420 pain_.fillRectangle(x, yo_, w, h, LColor::selection);
421 } else if (sel_starts_here) {
422 int const x = is_rtl ? int(xo_) : int(xo_ + startx);
423 int const w = is_rtl ? startx : (width_ - startx);
424 pain_.fillRectangle(x, yo_, w, h, LColor::selection);
425 } else if (sel_ends_here) {
426 int const x = is_rtl ? int(xo_ + endx) : int(xo_);
427 int const w = is_rtl ? (width_ - endx) : endx;
428 pain_.fillRectangle(x, yo_, w, h, LColor::selection);
429 } else if (row_y > starty && row_y < endy) {
430 pain_.fillRectangle(int(xo_), yo_, width_, h, LColor::selection);
435 if ((startpit != pit_ && startrow != rit_ && !is_rtl)
436 || (endpit != pit_ && endrow != rit_ && is_rtl))
437 pain_.fillRectangle(int(xo_), yo_,
438 int(x_), h, LColor::selection);
440 pos_type const body_pos = pit_->beginOfBody();
441 pos_type const end = row_.endpos();
444 for (pos_type vpos = row_.pos(); vpos < end; ++vpos) {
445 pos_type pos = text_.bidi.vis2log(vpos);
446 double const old_tmpx = tmpx;
447 if (body_pos > 0 && pos == body_pos - 1) {
448 LyXLayout_ptr const & layout = pit_->layout();
449 LyXFont const lfont = getLabelFont();
451 tmpx += label_hfill_ + font_metrics::width(layout->labelsep, lfont);
453 if (pit_->isLineSeparator(body_pos - 1))
454 tmpx -= singleWidth(body_pos - 1);
457 tmpx += singleWidth(pos);
459 if (hfillExpansion(*pit_, row_, pos)) {
463 tmpx += label_hfill_;
465 if (pit_->isSeparator(pos) && pos >= body_pos)
469 if (((startpit != pit_ && startrow != rit_)
470 || cur.selBegin().pos() <= pos) &&
471 ((endpit != pit_ && endrow != rit_)
472 || pos < cur.selEnd().pos())) {
473 // Here we do not use x_ as xo_ was added to x_.
474 pain_.fillRectangle(int(old_tmpx), yo_,
475 int(tmpx - old_tmpx + 1), h, LColor::selection);
479 if ((startpit != pit_ && startrow != rit_ && is_rtl) ||
480 (endpit != pit_ && endrow != rit_ && !is_rtl)) {
481 pain_.fillRectangle(int(xo_ + tmpx),
482 yo_, int(width_ - tmpx), h, LColor::selection);
487 void RowPainter::paintChangeBar()
489 pos_type const start = row_.pos();
490 pos_type const end = row_.endpos();
492 if (start == end || !pit_->isChanged(start, end - 1))
495 int const height = text_.isLastRow(pit_, row_)
497 : row_.height() + boost::next(rit_)->top_of_text();
499 pain_.fillRectangle(4, yo_, 5, height, LColor::changebar);
503 void RowPainter::paintAppendix()
505 if (!pit_->params().appendix())
508 // FIXME: can be just width_ ?
509 int const ww = width_;
513 if (pit_->params().startOfAppendix())
514 y += 2 * defaultRowHeight();
516 pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
517 pain_.line(ww - 2, y, ww - 2, yo_ + row_.height(), LColor::appendix);
521 void RowPainter::paintDepthBar()
523 Paragraph::depth_type const depth = pit_->getDepth();
528 Paragraph::depth_type prev_depth = 0;
529 if (!text_.isFirstRow(pit_, row_)) {
530 ParagraphList::iterator pit2 = pit_;
533 prev_depth = pit2->getDepth();
536 Paragraph::depth_type next_depth = 0;
537 if (!text_.isLastRow(pit_, row_)) {
538 ParagraphList::iterator pit2 = pit_;
539 if (row_.endpos() >= pit2->size())
541 next_depth = pit2->getDepth();
544 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
545 int const w = NEST_MARGIN / 5;
546 int x = int(w * i + xo_);
547 // only consider the changebar space if we're drawing outer left
549 x += CHANGEBAR_MARGIN;
550 int const h = yo_ + row_.height() - 1 - (i - next_depth - 1) * 3;
552 pain_.line(x, yo_, x, h, LColor::depthbar);
555 pain_.fillRectangle(x, yo_, w, 2, LColor::depthbar);
557 pain_.fillRectangle(x, h, w, 2, LColor::depthbar);
562 int RowPainter::paintAppendixStart(int y)
565 pb_font.setColor(LColor::appendix);
568 string const label = _("Appendix");
572 font_metrics::rectText(label, pb_font, w, a, d);
574 int const text_start = int(xo_ + (width_ - w) / 2);
575 int const text_end = text_start + w;
577 pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
579 pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
580 pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
582 return 3 * defaultRowHeight();
586 void RowPainter::paintFirst()
588 ParagraphParameters const & parparams = pit_->params();
592 // start of appendix?
593 if (parparams.startOfAppendix())
594 y_top += paintAppendixStart(yo_ + y_top + 2 * defaultRowHeight());
596 Buffer const & buffer = *bv_.buffer();
598 LyXLayout_ptr const & layout = pit_->layout();
600 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
601 if (pit_ != text_.paragraphs().begin()) {
602 if (layout->latextype == LATEX_PARAGRAPH
603 && !pit_->getDepth()) {
604 y_top += buffer.params().getDefSkip().inPixels(bv_);
606 LyXLayout_ptr const & playout =
607 boost::prior(pit_)->layout();
608 if (playout->latextype == LATEX_PARAGRAPH
609 && !boost::prior(pit_)->getDepth()) {
610 // is it right to use defskip here, too? (AS)
611 y_top += buffer.params().getDefSkip().inPixels(bv_);
617 int const ww = bv_.workWidth();
619 bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params());
620 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
621 //lyxerr << "paintFirst: " << pit_->id() << " is_seq: " << is_seq << std::endl;
623 // should we print a label?
624 if (layout->labeltype >= LABEL_STATIC
625 && (layout->labeltype != LABEL_STATIC
626 || layout->latextype != LATEX_ENVIRONMENT
629 LyXFont font = getLabelFont();
630 if (!pit_->getLabelstring().empty()) {
632 string const str = pit_->getLabelstring();
634 // this is special code for the chapter layout. This is
635 // printed in an extra row and has a pagebreak at
637 if (layout->counter == "chapter") {
638 if (buffer.params().secnumdepth >= 0) {
639 float spacing_val = 1.0;
640 if (!parparams.spacing().isDefault()) {
641 spacing_val = parparams.spacing().getValue();
643 spacing_val = buffer.params().spacing().getValue();
647 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
648 + int(layout->parsep) * defaultRowHeight();
651 x = ww - leftMargin() -
652 font_metrics::width(str, font);
656 yo_ + row_.baseline() -
657 row_.ascent_of_text() - maxdesc,
662 x = ww - leftMargin()
663 + font_metrics::width(layout->labelsep, font);
665 x = x_ - font_metrics::width(layout->labelsep, font)
666 - font_metrics::width(str, font);
669 pain_.text(int(x), yo_ + row_.baseline(), str, font);
673 // the labels at the top of an environment.
674 // More or less for bibliography
676 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
677 layout->labeltype == LABEL_BIBLIO ||
678 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
679 LyXFont font = getLabelFont();
680 if (!pit_->getLabelstring().empty()) {
681 string const str = pit_->getLabelstring();
682 float spacing_val = 1.0;
683 if (!parparams.spacing().isDefault()) {
684 spacing_val = parparams.spacing().getValue();
686 spacing_val = buffer.params().spacing().getValue();
690 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
691 + (layout->labelbottomsep * defaultRowHeight()));
694 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
695 x = ((is_rtl ? leftMargin() : x_)
696 + ww - text_.rightMargin(*pit_)) / 2;
697 x -= font_metrics::width(str, font) / 2;
699 x = ww - leftMargin() -
700 font_metrics::width(str, font);
703 yo_ + row_.baseline() - row_.ascent_of_text() - maxdesc,
710 void RowPainter::paintLast()
712 int const ww = bv_.workWidth();
713 bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params());
714 int const endlabel = getEndLabel(pit_, text_.paragraphs());
719 case END_LABEL_FILLED_BOX: {
720 LyXFont const font = getLabelFont();
721 int const size = int(0.75 * font_metrics::maxAscent(font));
722 int const y = yo_ + row_.baseline() - size;
723 int x = is_rtl ? NEST_MARGIN + CHANGEBAR_MARGIN: ww - size;
725 if (row_.fill() <= size)
726 x += (size - row_.fill() + 1) * (is_rtl ? -1 : 1);
728 if (endlabel == END_LABEL_BOX)
729 pain_.rectangle(x, y, size, size, LColor::eolmarker);
731 pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
735 case END_LABEL_STATIC: {
736 LyXFont font = getLabelFont();
737 string const & str = pit_->layout()->endlabelstring();
738 double const x = is_rtl ?
739 x_ - font_metrics::width(str, font)
740 : ww - text_.rightMargin(*pit_) - row_.fill();
741 pain_.text(int(x), yo_ + row_.baseline(), str, font);
745 case END_LABEL_NO_LABEL:
751 void RowPainter::paintText()
753 pos_type const end = row_.endpos();
754 pos_type body_pos = pit_->beginOfBody();
756 (body_pos > end || !pit_->isLineSeparator(body_pos - 1))) {
760 LyXLayout_ptr const & layout = pit_->layout();
762 bool running_strikeout = false;
763 bool is_struckout = false;
764 int last_strikeout_x = 0;
766 for (pos_type vpos = row_.pos(); vpos < end; ) {
767 if (x_ > bv_.workWidth())
770 pos_type pos = text_.bidi.vis2log(vpos);
772 if (pos >= pit_->size()) {
777 if (x_ + singleWidth(pos) < 0) {
778 x_ += singleWidth(pos);
783 is_struckout = isDeletedText(*pit_, pos);
785 if (is_struckout && !running_strikeout) {
786 running_strikeout = true;
787 last_strikeout_x = int(x_);
790 bool const highly_editable_inset = pit_->isInset(pos)
791 && isHighlyEditableInset(pit_->getInset(pos));
793 // if we reach the end of a struck out range, paint it
794 // we also don't paint across things like tables
795 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
796 int const middle = yo_ + (row_.baseline() + row_.top_of_text()) / 2;
797 pain_.line(last_strikeout_x, middle, int(x_), middle,
798 LColor::strikeout, Painter::line_solid, Painter::line_thin);
799 running_strikeout = false;
802 if (body_pos > 0 && pos == body_pos - 1) {
803 int const lwidth = font_metrics::width(layout->labelsep,
806 x_ += label_hfill_ + lwidth - singleWidth(body_pos - 1);
809 if (pit_->isHfill(pos)) {
812 int const y0 = yo_ + row_.baseline();
813 int const y1 = y0 - defaultRowHeight() / 2;
815 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
817 if (hfillExpansion(*pit_, row_, pos)) {
818 int const y2 = (y0 + y1) / 2;
820 if (pos >= body_pos) {
821 pain_.line(int(x_), y2, int(x_ + hfill_), y2,
823 Painter::line_onoffdash);
826 pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
828 Painter::line_onoffdash);
831 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
835 } else if (pit_->isSeparator(pos)) {
836 x_ += singleWidth(pos);
845 // if we reach the end of a struck out range, paint it
846 if (running_strikeout) {
847 int const middle = yo_ + (row_.baseline() + row_.top_of_text()) / 2;
848 pain_.line(last_strikeout_x, middle, int(x_), middle,
849 LColor::strikeout, Painter::line_solid, Painter::line_thin);
850 running_strikeout = false;
855 int paintPars(BufferView const & bv, LyXText const & text,
856 ParagraphList::iterator pit, int xo, int yo, int y)
858 //lyxerr << " paintRows: pit: " << &*pit << endl;
859 int const y2 = bv.painter().paperHeight();
862 ParagraphList::iterator end = text.paragraphs().end();
864 for ( ; pit != end; ++pit) {
865 RowList::iterator row = pit->rows.begin();
866 RowList::iterator rend = pit->rows.end();
868 for ( ; row != rend; ++row) {
869 RowPainter(bv, text, pit, row, xo, y + yo);
882 int paintText(BufferView const & bv)
884 ParagraphList::iterator pit;
885 bv.text()->getRowNearY(bv.top_y(), pit);
886 return paintPars(bv, *bv.text(), pit, 0, 0, pit->y);
890 void paintTextInset(LyXText const & text, PainterInfo & pi, int xo, int yo)
892 paintPars(*pi.base.bv, text, text.paragraphs().begin(), xo, yo, 0);