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().innerText())
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 * inset = const_cast<InsetBase *>(pit_->getInset(pos));
198 PainterInfo pi(const_cast<BufferView *>(&bv_));
199 pi.base.font = getFont(pos);
200 inset->draw(pi, int(x_), yo_ + row_.baseline());
201 x_ += inset->width();
205 void RowPainter::paintHebrewComposeChar(pos_type & vpos)
207 pos_type pos = text_.bidi.vis2log(vpos);
212 char c = pit_->getChar(pos);
216 LyXFont const & font = getFont(pos);
217 int const width = font_metrics::width(c, font);
220 for (pos_type i = pos - 1; i >= 0; --i) {
221 c = pit_->getChar(i);
222 if (!Encodings::IsComposeChar_hebrew(c)) {
223 if (IsPrintableNonspace(c)) {
224 int const width2 = singleWidth(i, c);
226 dx = (c == 'ø' || c == 'ã')
228 : (width2 - width) / 2;
235 pain_.text(int(x_) + dx, yo_ + row_.baseline(), str, font);
239 void RowPainter::paintArabicComposeChar(pos_type & vpos)
241 pos_type pos = text_.bidi.vis2log(vpos);
245 char c = pit_->getChar(pos);
246 c = pit_->transformChar(c, pos);
250 LyXFont const & font = getFont(pos);
251 int const width = font_metrics::width(c, font);
254 for (pos_type i = pos - 1; i >= 0; --i) {
255 c = pit_->getChar(i);
256 if (!Encodings::IsComposeChar_arabic(c)) {
257 if (IsPrintableNonspace(c)) {
258 int const width2 = singleWidth(i, c);
259 dx = (width2 - width) / 2;
265 pain_.text(int(x_) + dx, yo_ + row_.baseline(), str, font);
269 void RowPainter::paintChars(pos_type & vpos, bool hebrew, bool arabic)
271 pos_type pos = text_.bidi.vis2log(vpos);
272 pos_type const end = row_.endpos();
273 LyXFont orig_font = getFont(pos);
277 str += pit_->getChar(pos);
279 unsigned char c = str[0];
280 str[0] = pit_->transformChar(c, pos);
283 bool prev_struckout = isDeletedText(*pit_, pos);
284 bool prev_newtext = isInsertedText(*pit_, pos);
286 // collect as much similar chars as we can
287 for (++vpos; vpos < end && (pos = text_.bidi.vis2log(vpos)) >= 0; ++vpos) {
288 char c = pit_->getChar(pos);
290 if (!IsPrintableNonspace(c))
293 if (prev_struckout != isDeletedText(*pit_, pos))
296 if (prev_newtext != isInsertedText(*pit_, pos))
299 if (arabic && Encodings::IsComposeChar_arabic(c))
302 if (hebrew && Encodings::IsComposeChar_hebrew(c))
305 if (orig_font != getFont(pos))
309 c = pit_->transformChar(c, pos);
315 orig_font.setColor(LColor::strikeout);
316 else if (prev_newtext)
317 orig_font.setColor(LColor::newtext);
319 // Draw text and set the new x position
320 //lyxerr << "paint row: yo_ " << yo_ << " baseline: " << row_.baseline()
322 pain_.text(int(x_), yo_ + row_.baseline(), str, orig_font);
323 x_ += font_metrics::width(str, orig_font);
327 void RowPainter::paintForeignMark(double orig_x, LyXFont const & orig_font)
329 if (!lyxrc.mark_foreign_language)
331 if (orig_font.language() == latex_language)
333 if (orig_font.language() == bv_.buffer()->params().language)
336 int const y = yo_ + row_.baseline() + 1;
337 pain_.line(int(orig_x), y, int(x_), y, LColor::language);
341 void RowPainter::paintFromPos(pos_type & vpos)
343 pos_type const pos = text_.bidi.vis2log(vpos);
345 LyXFont const & orig_font = getFont(pos);
347 double const orig_x = x_;
349 char const c = pit_->getChar(pos);
351 if (c == Paragraph::META_INSET) {
354 paintForeignMark(orig_x, orig_font);
358 // usual characters, no insets
360 // special case languages
361 bool const hebrew = (orig_font.language()->lang() == "hebrew");
363 orig_font.language()->lang() == "arabic" &&
364 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
365 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
367 // draw as many chars as we can
368 if ((!hebrew && !arabic)
369 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
370 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
371 paintChars(vpos, hebrew, arabic);
373 paintHebrewComposeChar(vpos);
375 paintArabicComposeChar(vpos);
378 paintForeignMark(orig_x, orig_font);
382 void RowPainter::paintBackground()
384 int const x = int(xo_);
385 int const y = yo_ < 0 ? 0 : yo_;
386 int const h = yo_ < 0 ? row_.height() + yo_ : row_.height();
387 pain_.fillRectangle(x, y, width_, h, text_.backgroundColor());
391 void RowPainter::paintSelection()
393 bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params());
395 // the current selection
396 LCursor const & cur = bv_.cursor();
397 int const startx = text_.cursorX(cur.selBegin());
398 int const endx = text_.cursorX(cur.selEnd());
399 int const starty = text_.cursorY(cur.selBegin());
400 int const endy = text_.cursorY(cur.selEnd());
401 ParagraphList::iterator startpit = text_.getPar(cur.selBegin());
402 ParagraphList::iterator endpit = text_.getPar(cur.selEnd());
403 RowList::iterator startrow = startpit->getRow(cur.selBegin().pos());
404 RowList::iterator endrow = endpit->getRow(cur.selEnd().pos());
405 int const h = row_.height();
407 int const row_y = pit_->y + row_.y_offset();
409 bool const sel_starts_here = startpit == pit_ && startrow == rit_;
410 bool const sel_ends_here = endpit == pit_ && endrow == rit_;
411 bool const sel_on_one_row = sel_starts_here && sel_ends_here;
413 if (text_.bidi.same_direction()) {
416 if (sel_on_one_row) {
424 pain_.fillRectangle(x, yo_, w, h, LColor::selection);
425 } else if (sel_starts_here) {
426 int const x = is_rtl ? 0 : startx;
427 int const w = is_rtl ? startx : (width_ - startx);
428 pain_.fillRectangle(x, yo_, w, h, LColor::selection);
429 } else if (sel_ends_here) {
430 int const x = is_rtl ? endx : 0;
431 int const w = is_rtl ? (width_ - endx) : endx;
432 pain_.fillRectangle(x, yo_, w, h, LColor::selection);
433 } else if (row_y > starty && row_y < endy) {
434 pain_.fillRectangle(int(xo_), yo_, width_, h, LColor::selection);
439 if ((startpit != pit_ && startrow != rit_ && !is_rtl)
440 || (endpit != pit_ && endrow != rit_ && is_rtl))
441 pain_.fillRectangle(int(xo_), yo_,
442 int(x_), h, LColor::selection);
444 pos_type const body_pos = pit_->beginOfBody();
445 pos_type const end = row_.endpos();
448 for (pos_type vpos = row_.pos(); vpos < end; ++vpos) {
449 pos_type pos = text_.bidi.vis2log(vpos);
450 double const old_tmpx = tmpx;
451 if (body_pos > 0 && pos == body_pos - 1) {
452 LyXLayout_ptr const & layout = pit_->layout();
453 LyXFont const lfont = getLabelFont();
455 tmpx += label_hfill_ + font_metrics::width(layout->labelsep, lfont);
457 if (pit_->isLineSeparator(body_pos - 1))
458 tmpx -= singleWidth(body_pos - 1);
461 tmpx += singleWidth(pos);
463 if (hfillExpansion(*pit_, row_, pos)) {
467 tmpx += label_hfill_;
469 if (pit_->isSeparator(pos) && pos >= body_pos)
473 if (((startpit != pit_ && startrow != rit_)
474 || cur.selBegin().pos() <= pos) &&
475 ((endpit != pit_ && endrow != rit_)
476 || pos < cur.selEnd().pos())) {
477 // Here we do not use x_ as xo_ was added to x_.
478 pain_.fillRectangle(int(old_tmpx), yo_,
479 int(tmpx - old_tmpx + 1), h, LColor::selection);
483 if ((startpit != pit_ && startrow != rit_ && is_rtl) ||
484 (endpit != pit_ && endrow != rit_ && !is_rtl)) {
485 pain_.fillRectangle(int(xo_ + tmpx),
486 yo_, int(width_ - tmpx), h, LColor::selection);
491 void RowPainter::paintChangeBar()
493 pos_type const start = row_.pos();
494 pos_type const end = row_.endpos();
496 if (start == end || !pit_->isChanged(start, end - 1))
499 int const height = text_.isLastRow(pit_, row_)
501 : row_.height() + boost::next(rit_)->top_of_text();
503 pain_.fillRectangle(4, yo_, 5, height, LColor::changebar);
507 void RowPainter::paintAppendix()
509 if (!pit_->params().appendix())
512 // FIXME: can be just width_ ?
513 int const ww = width_;
517 if (pit_->params().startOfAppendix())
518 y += 2 * defaultRowHeight();
520 pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
521 pain_.line(ww - 2, y, ww - 2, yo_ + row_.height(), LColor::appendix);
525 void RowPainter::paintDepthBar()
527 Paragraph::depth_type const depth = pit_->getDepth();
532 Paragraph::depth_type prev_depth = 0;
533 if (!text_.isFirstRow(pit_, row_)) {
534 ParagraphList::iterator pit2 = pit_;
537 prev_depth = pit2->getDepth();
540 Paragraph::depth_type next_depth = 0;
541 if (!text_.isLastRow(pit_, row_)) {
542 ParagraphList::iterator pit2 = pit_;
543 if (row_.endpos() >= pit2->size())
545 next_depth = pit2->getDepth();
548 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
549 int const w = NEST_MARGIN / 5;
550 int x = int(w * i + xo_);
551 // only consider the changebar space if we're drawing outer left
553 x += CHANGEBAR_MARGIN;
554 int const h = yo_ + row_.height() - 1 - (i - next_depth - 1) * 3;
556 pain_.line(x, yo_, x, h, LColor::depthbar);
559 pain_.fillRectangle(x, yo_, w, 2, LColor::depthbar);
561 pain_.fillRectangle(x, h, w, 2, LColor::depthbar);
566 int RowPainter::paintAppendixStart(int y)
569 pb_font.setColor(LColor::appendix);
572 string const label = _("Appendix");
576 font_metrics::rectText(label, pb_font, w, a, d);
578 int const text_start = int(xo_ + (width_ - w) / 2);
579 int const text_end = text_start + w;
581 pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
583 pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
584 pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
586 return 3 * defaultRowHeight();
590 void RowPainter::paintFirst()
592 ParagraphParameters const & parparams = pit_->params();
596 // start of appendix?
597 if (parparams.startOfAppendix())
598 y_top += paintAppendixStart(yo_ + y_top + 2 * defaultRowHeight());
600 Buffer const & buffer = *bv_.buffer();
602 LyXLayout_ptr const & layout = pit_->layout();
604 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
605 if (pit_ != text_.paragraphs().begin()) {
606 if (layout->latextype == LATEX_PARAGRAPH
607 && !pit_->getDepth()) {
608 y_top += buffer.params().getDefSkip().inPixels(bv_);
610 LyXLayout_ptr const & playout =
611 boost::prior(pit_)->layout();
612 if (playout->latextype == LATEX_PARAGRAPH
613 && !boost::prior(pit_)->getDepth()) {
614 // is it right to use defskip here, too? (AS)
615 y_top += buffer.params().getDefSkip().inPixels(bv_);
621 int const ww = bv_.workWidth();
623 bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params());
624 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
625 //lyxerr << "paintFirst: " << pit_->id() << " is_seq: " << is_seq << std::endl;
627 // should we print a label?
628 if (layout->labeltype >= LABEL_STATIC
629 && (layout->labeltype != LABEL_STATIC
630 || layout->latextype != LATEX_ENVIRONMENT
633 LyXFont font = getLabelFont();
634 if (!pit_->getLabelstring().empty()) {
636 string const str = pit_->getLabelstring();
638 // this is special code for the chapter layout. This is
639 // printed in an extra row and has a pagebreak at
641 if (layout->counter == "chapter") {
642 if (buffer.params().secnumdepth >= 0) {
643 float spacing_val = 1.0;
644 if (!parparams.spacing().isDefault()) {
645 spacing_val = parparams.spacing().getValue();
647 spacing_val = buffer.params().spacing().getValue();
651 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
652 + int(layout->parsep) * defaultRowHeight();
655 x = ww - leftMargin() -
656 font_metrics::width(str, font);
660 yo_ + row_.baseline() -
661 row_.ascent_of_text() - maxdesc,
666 x = ww - leftMargin()
667 + font_metrics::width(layout->labelsep, font);
669 x = x_ - font_metrics::width(layout->labelsep, font)
670 - font_metrics::width(str, font);
673 pain_.text(int(x), yo_ + row_.baseline(), str, font);
677 // the labels at the top of an environment.
678 // More or less for bibliography
680 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
681 layout->labeltype == LABEL_BIBLIO ||
682 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
683 LyXFont font = getLabelFont();
684 if (!pit_->getLabelstring().empty()) {
685 string const str = pit_->getLabelstring();
686 float spacing_val = 1.0;
687 if (!parparams.spacing().isDefault()) {
688 spacing_val = parparams.spacing().getValue();
690 spacing_val = buffer.params().spacing().getValue();
694 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
695 + (layout->labelbottomsep * defaultRowHeight()));
698 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
699 x = ((is_rtl ? leftMargin() : x_)
700 + ww - text_.rightMargin(*pit_)) / 2;
701 x -= font_metrics::width(str, font) / 2;
703 x = ww - leftMargin() -
704 font_metrics::width(str, font);
707 yo_ + row_.baseline() - row_.ascent_of_text() - maxdesc,
714 void RowPainter::paintLast()
716 int const ww = bv_.workWidth();
717 bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params());
718 int const endlabel = getEndLabel(pit_, text_.paragraphs());
723 case END_LABEL_FILLED_BOX: {
724 LyXFont const font = getLabelFont();
725 int const size = int(0.75 * font_metrics::maxAscent(font));
726 int const y = yo_ + row_.baseline() - size;
727 int x = is_rtl ? NEST_MARGIN + CHANGEBAR_MARGIN: ww - size;
729 if (row_.fill() <= size)
730 x += (size - row_.fill() + 1) * (is_rtl ? -1 : 1);
732 if (endlabel == END_LABEL_BOX)
733 pain_.rectangle(x, y, size, size, LColor::eolmarker);
735 pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
739 case END_LABEL_STATIC: {
740 LyXFont font = getLabelFont();
741 string const & str = pit_->layout()->endlabelstring();
742 double const x = is_rtl ?
743 x_ - font_metrics::width(str, font)
744 : ww - text_.rightMargin(*pit_) - row_.fill();
745 pain_.text(int(x), yo_ + row_.baseline(), str, font);
749 case END_LABEL_NO_LABEL:
755 void RowPainter::paintText()
757 pos_type const end = row_.endpos();
758 pos_type body_pos = pit_->beginOfBody();
760 (body_pos > end || !pit_->isLineSeparator(body_pos - 1))) {
764 LyXLayout_ptr const & layout = pit_->layout();
766 bool running_strikeout = false;
767 bool is_struckout = false;
768 int last_strikeout_x = 0;
770 for (pos_type vpos = row_.pos(); vpos < end; ) {
771 if (x_ > bv_.workWidth())
774 pos_type pos = text_.bidi.vis2log(vpos);
776 if (pos >= pit_->size()) {
781 if (x_ + singleWidth(pos) < 0) {
782 x_ += singleWidth(pos);
787 is_struckout = isDeletedText(*pit_, pos);
789 if (is_struckout && !running_strikeout) {
790 running_strikeout = true;
791 last_strikeout_x = int(x_);
794 bool const highly_editable_inset = pit_->isInset(pos)
795 && isHighlyEditableInset(pit_->getInset(pos));
797 // if we reach the end of a struck out range, paint it
798 // we also don't paint across things like tables
799 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
800 int const middle = yo_ + (row_.baseline() + row_.top_of_text()) / 2;
801 pain_.line(last_strikeout_x, middle, int(x_), middle,
802 LColor::strikeout, Painter::line_solid, Painter::line_thin);
803 running_strikeout = false;
806 if (body_pos > 0 && pos == body_pos - 1) {
807 int const lwidth = font_metrics::width(layout->labelsep,
810 x_ += label_hfill_ + lwidth - singleWidth(body_pos - 1);
813 if (pit_->isHfill(pos)) {
816 int const y0 = yo_ + row_.baseline();
817 int const y1 = y0 - defaultRowHeight() / 2;
819 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
821 if (hfillExpansion(*pit_, row_, pos)) {
822 int const y2 = (y0 + y1) / 2;
824 if (pos >= body_pos) {
825 pain_.line(int(x_), y2, int(x_ + hfill_), y2,
827 Painter::line_onoffdash);
830 pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
832 Painter::line_onoffdash);
835 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
839 } else if (pit_->isSeparator(pos)) {
840 x_ += singleWidth(pos);
849 // if we reach the end of a struck out range, paint it
850 if (running_strikeout) {
851 int const middle = yo_ + (row_.baseline() + row_.top_of_text()) / 2;
852 pain_.line(last_strikeout_x, middle, int(x_), middle,
853 LColor::strikeout, Painter::line_solid, Painter::line_thin);
854 running_strikeout = false;
859 int paintPars(BufferView const & bv, LyXText const & text,
860 ParagraphList::iterator pit, int xo, int yo, int y)
862 //lyxerr << " paintRows: pit: " << &*pit << endl;
863 int const y2 = bv.painter().paperHeight();
866 ParagraphList::iterator end = text.paragraphs().end();
868 for ( ; pit != end; ++pit) {
869 RowList::iterator row = pit->rows.begin();
870 RowList::iterator rend = pit->rows.end();
872 for ( ; row != rend; ++row) {
873 RowPainter(bv, text, pit, row, xo, y + yo);
886 int paintText(BufferView const & bv)
888 ParagraphList::iterator pit;
889 bv.text()->updateParPositions();
890 bv.text()->getRowNearY(0, pit);
891 lyxerr << "top_y: " << bv.top_y() << " y: " << pit->y << endl;
892 return paintPars(bv, *bv.text(), pit, 0, 0, pit->y);
896 void paintTextInset(LyXText const & text, PainterInfo & pi, int xo, int yo)
898 paintPars(*pi.base.bv, text, text.paragraphs().begin(), xo, yo, 0);