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 (text_.selection.set() && &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 InsetOld * inset = const_cast<InsetOld*>(pit_->getInset(pos));
196 PainterInfo pi(const_cast<BufferView *>(&bv_));
197 pi.base.font = getFont(pos);
198 inset->draw(pi, int(x_), yo_ + row_.baseline());
199 x_ += inset->width();
203 void RowPainter::paintHebrewComposeChar(pos_type & vpos)
205 pos_type pos = text_.bidi.vis2log(vpos);
210 char c = pit_->getChar(pos);
214 LyXFont const & font = getFont(pos);
215 int const width = font_metrics::width(c, font);
218 for (pos_type i = pos - 1; i >= 0; --i) {
219 c = pit_->getChar(i);
220 if (!Encodings::IsComposeChar_hebrew(c)) {
221 if (IsPrintableNonspace(c)) {
222 int const width2 = singleWidth(i, c);
224 dx = (c == 'ø' || c == 'ã')
226 : (width2 - width) / 2;
233 pain_.text(int(x_) + dx, yo_ + row_.baseline(), str, font);
237 void RowPainter::paintArabicComposeChar(pos_type & vpos)
239 pos_type pos = text_.bidi.vis2log(vpos);
243 char c = pit_->getChar(pos);
244 c = pit_->transformChar(c, pos);
248 LyXFont const & font = getFont(pos);
249 int const width = font_metrics::width(c, font);
252 for (pos_type i = pos - 1; i >= 0; --i) {
253 c = pit_->getChar(i);
254 if (!Encodings::IsComposeChar_arabic(c)) {
255 if (IsPrintableNonspace(c)) {
256 int const width2 = singleWidth(i, c);
257 dx = (width2 - width) / 2;
263 pain_.text(int(x_) + dx, yo_ + row_.baseline(), str, font);
267 void RowPainter::paintChars(pos_type & vpos, bool hebrew, bool arabic)
269 pos_type pos = text_.bidi.vis2log(vpos);
270 pos_type const end = row_.endpos();
271 LyXFont orig_font = getFont(pos);
275 str += pit_->getChar(pos);
277 unsigned char c = str[0];
278 str[0] = pit_->transformChar(c, pos);
281 bool prev_struckout = isDeletedText(*pit_, pos);
282 bool prev_newtext = isInsertedText(*pit_, pos);
284 // collect as much similar chars as we can
285 for (++vpos; vpos < end && (pos = text_.bidi.vis2log(vpos)) >= 0; ++vpos) {
286 char c = pit_->getChar(pos);
288 if (!IsPrintableNonspace(c))
291 if (prev_struckout != isDeletedText(*pit_, pos))
294 if (prev_newtext != isInsertedText(*pit_, pos))
297 if (arabic && Encodings::IsComposeChar_arabic(c))
300 if (hebrew && Encodings::IsComposeChar_hebrew(c))
303 if (orig_font != getFont(pos))
307 c = pit_->transformChar(c, pos);
313 orig_font.setColor(LColor::strikeout);
314 else if (prev_newtext)
315 orig_font.setColor(LColor::newtext);
317 // Draw text and set the new x position
318 //lyxerr << "paint row: yo_ " << yo_ << " baseline: " << row_.baseline()
320 pain_.text(int(x_), yo_ + row_.baseline(), str, orig_font);
321 x_ += font_metrics::width(str, orig_font);
325 void RowPainter::paintForeignMark(double orig_x, LyXFont const & orig_font)
327 if (!lyxrc.mark_foreign_language)
329 if (orig_font.language() == latex_language)
331 if (orig_font.language() == bv_.buffer()->params().language)
334 int const y = yo_ + row_.baseline() + 1;
335 pain_.line(int(orig_x), y, int(x_), y, LColor::language);
339 void RowPainter::paintFromPos(pos_type & vpos)
341 pos_type const pos = text_.bidi.vis2log(vpos);
343 LyXFont const & orig_font = getFont(pos);
345 double const orig_x = x_;
347 char const c = pit_->getChar(pos);
349 if (c == Paragraph::META_INSET) {
352 paintForeignMark(orig_x, orig_font);
356 // usual characters, no insets
358 // special case languages
359 bool const hebrew = (orig_font.language()->lang() == "hebrew");
361 orig_font.language()->lang() == "arabic" &&
362 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
363 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
365 // draw as many chars as we can
366 if ((!hebrew && !arabic)
367 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
368 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
369 paintChars(vpos, hebrew, arabic);
371 paintHebrewComposeChar(vpos);
373 paintArabicComposeChar(vpos);
376 paintForeignMark(orig_x, orig_font);
380 void RowPainter::paintBackground()
382 int const x = int(xo_);
383 int const y = yo_ < 0 ? 0 : yo_;
384 int const h = yo_ < 0 ? row_.height() + yo_ : row_.height();
385 pain_.fillRectangle(x, y, width_, h, text_.backgroundColor());
389 void RowPainter::paintSelection()
391 bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params());
393 // the current selection
394 int const startx = text_.selection.start.x();
395 int const endx = text_.selection.end.x();
396 int const starty = text_.selection.start.y();
397 int const endy = text_.selection.end.y();
398 ParagraphList::iterator startpit = text_.getPar(text_.selection.start);
399 ParagraphList::iterator endpit = text_.getPar(text_.selection.end);
400 RowList::iterator startrow = startpit->getRow(text_.selection.start.pos());
401 RowList::iterator endrow = endpit->getRow(text_.selection.end.pos());
402 int const h = row_.height();
404 int const row_y = pit_->y + row_.y_offset();
406 bool const sel_starts_here = startpit == pit_ && startrow == rit_;
407 bool const sel_ends_here = endpit == pit_ && endrow == rit_;
408 bool const sel_on_one_row = sel_starts_here && sel_ends_here;
410 if (text_.bidi.same_direction()) {
413 if (sel_on_one_row) {
415 x = int(xo_) + startx;
421 pain_.fillRectangle(x, yo_, w, h, LColor::selection);
422 } else if (sel_starts_here) {
423 int const x = is_rtl ? int(xo_) : int(xo_ + startx);
424 int const w = is_rtl ? startx : (width_ - startx);
425 pain_.fillRectangle(x, yo_, w, h, LColor::selection);
426 } else if (sel_ends_here) {
427 int const x = is_rtl ? int(xo_ + endx) : int(xo_);
428 int const w = is_rtl ? (width_ - endx) : endx;
429 pain_.fillRectangle(x, yo_, w, h, LColor::selection);
430 } else if (row_y > starty && row_y < endy) {
431 pain_.fillRectangle(int(xo_), yo_, width_, h, LColor::selection);
436 if ((startpit != pit_ && startrow != rit_ && !is_rtl)
437 || (endpit != pit_ && endrow != rit_ && is_rtl))
438 pain_.fillRectangle(int(xo_), yo_,
439 int(x_), h, LColor::selection);
441 pos_type const body_pos = pit_->beginOfBody();
442 pos_type const end = row_.endpos();
445 for (pos_type vpos = row_.pos(); vpos < end; ++vpos) {
446 pos_type pos = text_.bidi.vis2log(vpos);
447 double const old_tmpx = tmpx;
448 if (body_pos > 0 && pos == body_pos - 1) {
449 LyXLayout_ptr const & layout = pit_->layout();
450 LyXFont const lfont = getLabelFont();
452 tmpx += label_hfill_ + font_metrics::width(layout->labelsep, lfont);
454 if (pit_->isLineSeparator(body_pos - 1))
455 tmpx -= singleWidth(body_pos - 1);
458 tmpx += singleWidth(pos);
460 if (hfillExpansion(*pit_, row_, pos)) {
464 tmpx += label_hfill_;
466 if (pit_->isSeparator(pos) && pos >= body_pos)
470 if (((startpit != pit_ && startrow != rit_)
471 || text_.selection.start.pos() <= pos) &&
472 ((endpit != pit_ && endrow != rit_)
473 || pos < text_.selection.end.pos())) {
474 // Here we do not use x_ as xo_ was added to x_.
475 pain_.fillRectangle(int(old_tmpx), yo_,
476 int(tmpx - old_tmpx + 1), h, LColor::selection);
480 if ((startpit != pit_ && startrow != rit_ && is_rtl) ||
481 (endpit != pit_ && endrow != rit_ && !is_rtl)) {
482 pain_.fillRectangle(int(xo_ + tmpx),
483 yo_, int(width_ - tmpx), h, LColor::selection);
488 void RowPainter::paintChangeBar()
490 pos_type const start = row_.pos();
491 pos_type const end = row_.endpos();
493 if (start == end || !pit_->isChanged(start, end - 1))
496 int const height = text_.isLastRow(pit_, row_)
498 : row_.height() + boost::next(rit_)->top_of_text();
500 pain_.fillRectangle(4, yo_, 5, height, LColor::changebar);
504 void RowPainter::paintAppendix()
506 if (!pit_->params().appendix())
509 // FIXME: can be just width_ ?
510 int const ww = width_;
514 if (pit_->params().startOfAppendix())
515 y += 2 * defaultRowHeight();
517 pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
518 pain_.line(ww - 2, y, ww - 2, yo_ + row_.height(), LColor::appendix);
522 void RowPainter::paintDepthBar()
524 Paragraph::depth_type const depth = pit_->getDepth();
529 Paragraph::depth_type prev_depth = 0;
530 if (!text_.isFirstRow(pit_, row_)) {
531 ParagraphList::iterator pit2 = pit_;
534 prev_depth = pit2->getDepth();
537 Paragraph::depth_type next_depth = 0;
538 if (!text_.isLastRow(pit_, row_)) {
539 ParagraphList::iterator pit2 = pit_;
540 if (row_.endpos() >= pit2->size())
542 next_depth = pit2->getDepth();
545 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
546 int const w = NEST_MARGIN / 5;
547 int x = int(w * i + xo_);
548 // only consider the changebar space if we're drawing outer left
550 x += CHANGEBAR_MARGIN;
551 int const h = yo_ + row_.height() - 1 - (i - next_depth - 1) * 3;
553 pain_.line(x, yo_, x, h, LColor::depthbar);
556 pain_.fillRectangle(x, yo_, w, 2, LColor::depthbar);
558 pain_.fillRectangle(x, h, w, 2, LColor::depthbar);
563 int RowPainter::paintAppendixStart(int y)
566 pb_font.setColor(LColor::appendix);
569 string const label = _("Appendix");
573 font_metrics::rectText(label, pb_font, w, a, d);
575 int const text_start = int(xo_ + (width_ - w) / 2);
576 int const text_end = text_start + w;
578 pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
580 pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
581 pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
583 return 3 * defaultRowHeight();
587 void RowPainter::paintFirst()
589 ParagraphParameters const & parparams = pit_->params();
593 // start of appendix?
594 if (parparams.startOfAppendix())
595 y_top += paintAppendixStart(yo_ + y_top + 2 * defaultRowHeight());
597 Buffer const & buffer = *bv_.buffer();
599 LyXLayout_ptr const & layout = pit_->layout();
601 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
602 if (pit_ != text_.ownerParagraphs().begin()) {
603 if (layout->latextype == LATEX_PARAGRAPH
604 && !pit_->getDepth()) {
605 y_top += buffer.params().getDefSkip().inPixels(bv_);
607 LyXLayout_ptr const & playout =
608 boost::prior(pit_)->layout();
609 if (playout->latextype == LATEX_PARAGRAPH
610 && !boost::prior(pit_)->getDepth()) {
611 // is it right to use defskip here, too? (AS)
612 y_top += buffer.params().getDefSkip().inPixels(bv_);
618 int const ww = bv_.workWidth();
620 bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params());
621 bool const is_seq = isFirstInSequence(pit_, text_.ownerParagraphs());
622 //lyxerr << "paintFirst: " << pit_->id() << " is_seq: " << is_seq << std::endl;
624 // should we print a label?
625 if (layout->labeltype >= LABEL_STATIC
626 && (layout->labeltype != LABEL_STATIC
627 || layout->latextype != LATEX_ENVIRONMENT
630 LyXFont font = getLabelFont();
631 if (!pit_->getLabelstring().empty()) {
633 string const str = pit_->getLabelstring();
635 // this is special code for the chapter layout. This is
636 // printed in an extra row and has a pagebreak at
638 if (layout->counter == "chapter") {
639 if (buffer.params().secnumdepth >= 0) {
640 float spacing_val = 1.0;
641 if (!parparams.spacing().isDefault()) {
642 spacing_val = parparams.spacing().getValue();
644 spacing_val = buffer.params().spacing().getValue();
648 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
649 + int(layout->parsep) * defaultRowHeight();
652 x = ww - leftMargin() -
653 font_metrics::width(str, font);
657 yo_ + row_.baseline() -
658 row_.ascent_of_text() - maxdesc,
663 x = ww - leftMargin()
664 + font_metrics::width(layout->labelsep, font);
666 x = x_ - font_metrics::width(layout->labelsep, font)
667 - font_metrics::width(str, font);
670 pain_.text(int(x), yo_ + row_.baseline(), str, font);
674 // the labels at the top of an environment.
675 // More or less for bibliography
677 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
678 layout->labeltype == LABEL_BIBLIO ||
679 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
680 LyXFont font = getLabelFont();
681 if (!pit_->getLabelstring().empty()) {
682 string const str = pit_->getLabelstring();
683 float spacing_val = 1.0;
684 if (!parparams.spacing().isDefault()) {
685 spacing_val = parparams.spacing().getValue();
687 spacing_val = buffer.params().spacing().getValue();
691 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
692 + (layout->labelbottomsep * defaultRowHeight()));
695 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
696 x = ((is_rtl ? leftMargin() : x_)
697 + ww - text_.rightMargin(*pit_)) / 2;
698 x -= font_metrics::width(str, font) / 2;
700 x = ww - leftMargin() -
701 font_metrics::width(str, font);
704 yo_ + row_.baseline() - row_.ascent_of_text() - maxdesc,
711 void RowPainter::paintLast()
713 int const ww = bv_.workWidth();
714 bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params());
715 int const endlabel = getEndLabel(pit_, text_.ownerParagraphs());
720 case END_LABEL_FILLED_BOX: {
721 LyXFont const font = getLabelFont();
722 int const size = int(0.75 * font_metrics::maxAscent(font));
723 int const y = yo_ + row_.baseline() - size;
724 int x = is_rtl ? NEST_MARGIN + CHANGEBAR_MARGIN: ww - size;
726 if (row_.fill() <= size)
727 x += (size - row_.fill() + 1) * (is_rtl ? -1 : 1);
729 if (endlabel == END_LABEL_BOX)
730 pain_.rectangle(x, y, size, size, LColor::eolmarker);
732 pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
736 case END_LABEL_STATIC: {
737 LyXFont font = getLabelFont();
738 string const & str = pit_->layout()->endlabelstring();
739 double const x = is_rtl ?
740 x_ - font_metrics::width(str, font)
741 : ww - text_.rightMargin(*pit_) - row_.fill();
742 pain_.text(int(x), yo_ + row_.baseline(), str, font);
746 case END_LABEL_NO_LABEL:
752 void RowPainter::paintText()
754 pos_type const end = row_.endpos();
755 pos_type body_pos = pit_->beginOfBody();
757 (body_pos > end || !pit_->isLineSeparator(body_pos - 1))) {
761 LyXLayout_ptr const & layout = pit_->layout();
763 bool running_strikeout = false;
764 bool is_struckout = false;
765 int last_strikeout_x = 0;
767 for (pos_type vpos = row_.pos(); vpos < end; ) {
768 if (x_ > bv_.workWidth())
771 pos_type pos = text_.bidi.vis2log(vpos);
773 if (pos >= pit_->size()) {
778 if (x_ + singleWidth(pos) < 0) {
779 x_ += singleWidth(pos);
784 is_struckout = isDeletedText(*pit_, pos);
786 if (is_struckout && !running_strikeout) {
787 running_strikeout = true;
788 last_strikeout_x = int(x_);
791 bool const highly_editable_inset = pit_->isInset(pos)
792 && isHighlyEditableInset(pit_->getInset(pos));
794 // if we reach the end of a struck out range, paint it
795 // we also don't paint across things like tables
796 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
797 int const middle = yo_ + (row_.baseline() + row_.top_of_text()) / 2;
798 pain_.line(last_strikeout_x, middle, int(x_), middle,
799 LColor::strikeout, Painter::line_solid, Painter::line_thin);
800 running_strikeout = false;
803 if (body_pos > 0 && pos == body_pos - 1) {
804 int const lwidth = font_metrics::width(layout->labelsep,
807 x_ += label_hfill_ + lwidth - singleWidth(body_pos - 1);
810 if (pit_->isHfill(pos)) {
813 int const y0 = yo_ + row_.baseline();
814 int const y1 = y0 - defaultRowHeight() / 2;
816 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
818 if (hfillExpansion(*pit_, row_, pos)) {
819 int const y2 = (y0 + y1) / 2;
821 if (pos >= body_pos) {
822 pain_.line(int(x_), y2, int(x_ + hfill_), y2,
824 Painter::line_onoffdash);
827 pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
829 Painter::line_onoffdash);
832 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
836 } else if (pit_->isSeparator(pos)) {
837 x_ += singleWidth(pos);
846 // if we reach the end of a struck out range, paint it
847 if (running_strikeout) {
848 int const middle = yo_ + (row_.baseline() + row_.top_of_text()) / 2;
849 pain_.line(last_strikeout_x, middle, int(x_), middle,
850 LColor::strikeout, Painter::line_solid, Painter::line_thin);
851 running_strikeout = false;
856 int paintPars(BufferView const & bv, LyXText const & text,
857 ParagraphList::iterator pit, int xo, int yo, int y)
859 //lyxerr << " paintRows: pit: " << &*pit << endl;
860 int const y2 = bv.painter().paperHeight();
863 ParagraphList::iterator end = text.ownerParagraphs().end();
865 for ( ; pit != end; ++pit) {
866 RowList::iterator row = pit->rows.begin();
867 RowList::iterator rend = pit->rows.end();
869 for ( ; row != rend; ++row) {
870 RowPainter(bv, text, pit, row, xo, y + yo);
883 int paintText(BufferView const & bv)
885 ParagraphList::iterator pit;
886 bv.text()->getRowNearY(bv.top_y(), pit);
887 return paintPars(bv, *bv.text(), pit, 0, 0, pit->y);
891 void paintTextInset(BufferView const & bv, LyXText const & text, int xo, int yo)
893 paintPars(bv, text, text.ownerParagraphs().begin(), xo, yo, 0);