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/nullpainter.h"
36 #include "frontends/Painter.h"
38 #include "insets/insettext.h"
40 #include "support/textutils.h"
49 extern int NEST_MARGIN;
50 extern int CHANGEBAR_MARGIN;
56 * A class used for painting an individual row of text.
60 /// initialise and run painter
61 RowPainter(BufferView const & bv, Painter & pain, LyXText const & text,
62 par_type pit, RowList::iterator rit, int y);
64 // paint various parts
65 void paintBackground();
66 void paintSelection();
69 void paintChangeBar();
72 void paintForeignMark(double orig_x, LyXFont const & orig_font);
73 void paintHebrewComposeChar(lyx::pos_type & vpos);
74 void paintArabicComposeChar(lyx::pos_type & vpos);
75 void paintChars(lyx::pos_type & vpos, bool hebrew, bool arabic);
76 int paintAppendixStart(int y);
78 void paintFromPos(lyx::pos_type & vpos);
79 void paintInset(lyx::pos_type const pos);
81 /// return left margin
82 int leftMargin() const;
84 /// return the font at the given pos
85 LyXFont const getFont(lyx::pos_type pos) const;
87 /// return the label font for this row
88 LyXFont const getLabelFont() const;
90 /// return pixel width for the given pos
91 int singleWidth(lyx::pos_type pos) const;
92 int singleWidth(lyx::pos_type pos, char c) const;
94 /// bufferview to paint on
95 BufferView const & bv_;
100 /// LyXText for the row
101 LyXText const & text_;
102 ParagraphList & pars_;
105 RowList::iterator const rit_;
109 mutable par_type pit_;
122 RowPainter::RowPainter(BufferView const & bv, Painter & pain,
123 LyXText const & text, par_type pit, RowList::iterator rit, int y)
124 : bv_(bv), pain_(pain), text_(text), pars_(text.paragraphs()),
125 rit_(rit), row_(*rit), pit_(pit),
126 xo_(text_.xo_), yo_(y), width_(text_.width())
128 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo << " yo: " << yo
129 // << " pit->y: " << pit_->y
130 // << " row: " << (pars_[pit_].size() ? pars_[pit_].getChar(row_.pos()) : 'X') << endl;
131 RowMetrics m = text_.computeRowMetrics(pit, row_);
133 separator_ = m.separator;
135 label_hfill_ = m.label_hfill;
137 // background has already been cleared.
138 if (&text_ == bv_.text())
141 // paint the selection background
142 if (bv_.cursor().selection() && &text_ == bv_.cursor().text())
145 // vertical lines for appendix
148 // environment depth brackets
157 if (row_.endpos() >= pars_[pit_].size())
166 LyXFont const RowPainter::getFont(pos_type pos) const
168 return text_.getFont(pit_, pos);
172 int RowPainter::singleWidth(lyx::pos_type pos) const
174 return text_.singleWidth(pit_, pos);
178 int RowPainter::singleWidth(lyx::pos_type pos, char c) const
180 LyXFont const & font = text_.getFont(pit_, pos);
181 return text_.singleWidth(pit_, pos, c, font);
185 LyXFont const RowPainter::getLabelFont() const
187 return text_.getLabelFont(pit_);
191 int RowPainter::leftMargin() const
193 return text_.leftMargin(pit_, row_.pos());
197 void RowPainter::paintInset(pos_type const pos)
199 InsetBase const * inset = pars_[pit_].getInset(pos);
201 PainterInfo pi(const_cast<BufferView *>(&bv_), pain_);
202 pi.base.font = getFont(pos);
203 inset->drawSelection(pi, int(x_), yo_ + row_.baseline());
204 inset->draw(pi, int(x_), yo_ + row_.baseline());
205 x_ += inset->width();
209 void RowPainter::paintHebrewComposeChar(pos_type & vpos)
211 pos_type pos = text_.bidi.vis2log(vpos);
216 char c = pars_[pit_].getChar(pos);
220 LyXFont const & font = getFont(pos);
221 int const width = font_metrics::width(c, font);
224 for (pos_type i = pos - 1; i >= 0; --i) {
225 c = pars_[pit_].getChar(i);
226 if (!Encodings::IsComposeChar_hebrew(c)) {
227 if (IsPrintableNonspace(c)) {
228 int const width2 = singleWidth(i, c);
230 dx = (c == 'ø' || c == 'ã')
232 : (width2 - width) / 2;
239 pain_.text(int(x_) + dx, yo_ + row_.baseline(), str, font);
243 void RowPainter::paintArabicComposeChar(pos_type & vpos)
245 pos_type pos = text_.bidi.vis2log(vpos);
249 char c = pars_[pit_].getChar(pos);
250 c = pars_[pit_].transformChar(c, pos);
254 LyXFont const & font = getFont(pos);
255 int const width = font_metrics::width(c, font);
258 for (pos_type i = pos - 1; i >= 0; --i) {
259 c = pars_[pit_].getChar(i);
260 if (!Encodings::IsComposeChar_arabic(c)) {
261 if (IsPrintableNonspace(c)) {
262 int const width2 = singleWidth(i, c);
263 dx = (width2 - width) / 2;
269 pain_.text(int(x_) + dx, yo_ + row_.baseline(), str, font);
273 void RowPainter::paintChars(pos_type & vpos, bool hebrew, bool arabic)
275 pos_type pos = text_.bidi.vis2log(vpos);
276 pos_type const end = row_.endpos();
277 LyXFont orig_font = getFont(pos);
281 str += pars_[pit_].getChar(pos);
283 unsigned char c = str[0];
284 str[0] = pars_[pit_].transformChar(c, pos);
287 bool prev_struckout = isDeletedText(pars_[pit_], pos);
288 bool prev_newtext = isInsertedText(pars_[pit_], pos);
290 // collect as much similar chars as we can
291 for (++vpos; vpos < end && (pos = text_.bidi.vis2log(vpos)) >= 0; ++vpos) {
292 char c = pars_[pit_].getChar(pos);
294 if (!IsPrintableNonspace(c))
297 if (prev_struckout != isDeletedText(pars_[pit_], pos))
300 if (prev_newtext != isInsertedText(pars_[pit_], pos))
303 if (arabic && Encodings::IsComposeChar_arabic(c))
306 if (hebrew && Encodings::IsComposeChar_hebrew(c))
309 if (orig_font != getFont(pos))
313 c = pars_[pit_].transformChar(c, pos);
319 orig_font.setColor(LColor::strikeout);
320 else if (prev_newtext)
321 orig_font.setColor(LColor::newtext);
323 // Draw text and set the new x position
324 //lyxerr << "paint row: yo_ " << yo_ << " baseline: " << row_.baseline()
326 pain_.text(int(x_), yo_ + row_.baseline(), str, orig_font);
327 x_ += font_metrics::width(str, orig_font);
331 void RowPainter::paintForeignMark(double orig_x, LyXFont const & orig_font)
333 if (!lyxrc.mark_foreign_language)
335 if (orig_font.language() == latex_language)
337 if (orig_font.language() == bv_.buffer()->params().language)
340 int const y = yo_ + row_.baseline() + 1;
341 pain_.line(int(orig_x), y, int(x_), y, LColor::language);
345 void RowPainter::paintFromPos(pos_type & vpos)
347 pos_type const pos = text_.bidi.vis2log(vpos);
349 LyXFont const & orig_font = getFont(pos);
351 double const orig_x = x_;
353 char const c = pars_[pit_].getChar(pos);
355 if (c == Paragraph::META_INSET) {
358 paintForeignMark(orig_x, orig_font);
362 // usual characters, no insets
364 // special case languages
365 bool const hebrew = (orig_font.language()->lang() == "hebrew");
367 orig_font.language()->lang() == "arabic" &&
368 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
369 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
371 // draw as many chars as we can
372 if ((!hebrew && !arabic)
373 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
374 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
375 paintChars(vpos, hebrew, arabic);
377 paintHebrewComposeChar(vpos);
379 paintArabicComposeChar(vpos);
382 paintForeignMark(orig_x, orig_font);
386 void RowPainter::paintBackground()
388 int const x = int(xo_);
389 int const y = yo_ < 0 ? 0 : yo_;
390 int const h = yo_ < 0 ? row_.height() + yo_ : row_.height();
391 pain_.fillRectangle(x, y, width_, h, text_.backgroundColor());
395 void RowPainter::paintSelection()
397 bool const is_rtl = text_.isRTL(pars_[pit_]);
399 // the current selection
400 LCursor const & cur = bv_.cursor();
401 int const starty = text_.cursorY(cur.selBegin());
402 int const endy = text_.cursorY(cur.selEnd());
403 par_type startpit = cur.selBegin().par();
404 par_type endpit = cur.selEnd().par();
405 RowList::iterator startrow = pars_[startpit].getRow(cur.selBegin().pos());
406 RowList::iterator endrow = pars_[endpit].getRow(cur.selEnd().pos());
407 int const h = row_.height();
409 int const row_y = text_.yo_ + pars_[pit_].y + row_.y_offset();
411 bool const sel_starts_here = startpit == pit_ && startrow == rit_;
412 bool const sel_ends_here = endpit == pit_ && endrow == rit_;
413 bool const sel_on_one_row = sel_starts_here && sel_ends_here;
415 if (text_.bidi.same_direction()) {
416 if (sel_on_one_row) {
417 int const startx = text_.cursorX(cur.selBegin());
418 int const endx = text_.cursorX(cur.selEnd());
419 int const x1 = is_rtl ? endx : startx;
420 int const x2 = is_rtl ? startx : endx;
421 pain_.fillRectangle(x1, yo_, x2 - x1, h, LColor::selection);
422 } else if (sel_starts_here) {
423 int const startx = text_.cursorX(cur.selBegin());
424 int const x1 = is_rtl ? int(xo_) : startx;
425 int const x2 = is_rtl ? startx : int(xo_) + width_;
426 pain_.fillRectangle(x1, yo_, x2 - x1, h, LColor::selection);
427 } else if (sel_ends_here) {
428 int const endx = text_.cursorX(cur.selEnd());
429 int const x1 = is_rtl ? endx : int(xo_);
430 int const x2 = is_rtl ? int(xo_) + width_ : endx;
431 pain_.fillRectangle(x1, yo_, x2 - x1, h, LColor::selection);
432 } else if (row_y > starty && row_y < endy) {
433 pain_.fillRectangle(int(xo_), yo_, width_, h, LColor::selection);
438 if ((startpit != pit_ && startrow != rit_ && !is_rtl)
439 || (endpit != pit_ && endrow != rit_ && is_rtl))
440 pain_.fillRectangle(int(xo_), yo_,
441 int(x_), h, LColor::selection);
443 pos_type const body_pos = pars_[pit_].beginOfBody();
444 pos_type const end = row_.endpos();
447 for (pos_type vpos = row_.pos(); vpos < end; ++vpos) {
448 pos_type pos = text_.bidi.vis2log(vpos);
449 double const old_tmpx = tmpx;
450 if (body_pos > 0 && pos == body_pos - 1) {
451 LyXLayout_ptr const & layout = pars_[pit_].layout();
452 LyXFont const lfont = getLabelFont();
454 tmpx += label_hfill_ + font_metrics::width(layout->labelsep, lfont);
456 if (pars_[pit_].isLineSeparator(body_pos - 1))
457 tmpx -= singleWidth(body_pos - 1);
460 tmpx += singleWidth(pos);
462 if (hfillExpansion(pars_[pit_], row_, pos)) {
466 tmpx += label_hfill_;
468 if (pars_[pit_].isSeparator(pos) && pos >= body_pos)
472 if (((startpit != pit_ && startrow != rit_)
473 || cur.selBegin().pos() <= pos) &&
474 ((endpit != pit_ && endrow != rit_)
475 || pos < cur.selEnd().pos())) {
476 // Here we do not use x_ as xo_ was added to x_.
477 pain_.fillRectangle(int(old_tmpx), yo_,
478 int(tmpx - old_tmpx + 1), h, LColor::selection);
482 if ((startpit != pit_ && startrow != rit_ && is_rtl) ||
483 (endpit != pit_ && endrow != rit_ && !is_rtl)) {
484 pain_.fillRectangle(int(xo_ + tmpx),
485 yo_, int(width_ - tmpx), h, LColor::selection);
490 void RowPainter::paintChangeBar()
492 pos_type const start = row_.pos();
493 pos_type const end = row_.endpos();
495 if (start == end || !pars_[pit_].isChanged(start, end - 1))
498 int const height = text_.isLastRow(pit_, row_)
500 : row_.height() + boost::next(rit_)->top_of_text();
502 pain_.fillRectangle(4, yo_, 5, height, LColor::changebar);
506 void RowPainter::paintAppendix()
508 if (!pars_[pit_].params().appendix())
513 if (pars_[pit_].params().startOfAppendix())
514 y += 2 * defaultRowHeight();
516 pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
517 pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), LColor::appendix);
521 void RowPainter::paintDepthBar()
523 Paragraph::depth_type const depth = pars_[pit_].getDepth();
528 Paragraph::depth_type prev_depth = 0;
529 if (!text_.isFirstRow(pit_, row_)) {
530 par_type pit2 = pit_;
533 prev_depth = pars_[pit2].getDepth();
536 Paragraph::depth_type next_depth = 0;
537 if (!text_.isLastRow(pit_, row_)) {
538 par_type pit2 = pit_;
539 if (row_.endpos() >= pars_[pit2].size())
541 next_depth = pars_[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 = pars_[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 = pars_[pit_].layout();
600 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
602 if (layout->latextype == LATEX_PARAGRAPH
603 && !pars_[pit_].getDepth()) {
604 y_top += buffer.params().getDefSkip().inPixels(bv_);
606 LyXLayout_ptr const & playout = pars_[pit_ - 1].layout();
607 if (playout->latextype == LATEX_PARAGRAPH
608 && !pars_[pit_ - 1].getDepth()) {
609 // is it right to use defskip here, too? (AS)
610 y_top += buffer.params().getDefSkip().inPixels(bv_);
616 bool const is_rtl = text_.isRTL(pars_[pit_]);
617 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
618 //lyxerr << "paintFirst: " << pars_[pit_].id() << " is_seq: " << is_seq << std::endl;
620 // should we print a label?
621 if (layout->labeltype >= LABEL_STATIC
622 && (layout->labeltype != LABEL_STATIC
623 || layout->latextype != LATEX_ENVIRONMENT
626 LyXFont font = getLabelFont();
627 if (!pars_[pit_].getLabelstring().empty()) {
629 string const str = pars_[pit_].getLabelstring();
631 // this is special code for the chapter layout. This is
632 // printed in an extra row and has a pagebreak at
634 if (layout->counter == "chapter") {
635 if (buffer.params().secnumdepth >= 0) {
636 float spacing_val = 1.0;
637 if (!parparams.spacing().isDefault()) {
638 spacing_val = parparams.spacing().getValue();
640 spacing_val = buffer.params().spacing().getValue();
644 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
645 + int(layout->parsep) * defaultRowHeight();
648 x = width_ - leftMargin() -
649 font_metrics::width(str, font);
653 yo_ + row_.baseline() -
654 row_.ascent_of_text() - maxdesc,
659 x = width_ - leftMargin()
660 + font_metrics::width(layout->labelsep, font);
662 x = x_ - font_metrics::width(layout->labelsep, font)
663 - font_metrics::width(str, font);
666 pain_.text(int(x), yo_ + row_.baseline(), str, font);
670 // the labels at the top of an environment.
671 // More or less for bibliography
673 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
674 layout->labeltype == LABEL_BIBLIO ||
675 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
676 LyXFont font = getLabelFont();
677 if (!pars_[pit_].getLabelstring().empty()) {
678 string const str = pars_[pit_].getLabelstring();
679 float spacing_val = 1.0;
680 if (!parparams.spacing().isDefault()) {
681 spacing_val = parparams.spacing().getValue();
683 spacing_val = buffer.params().spacing().getValue();
687 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
688 + (layout->labelbottomsep * defaultRowHeight()));
691 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
692 x = ((is_rtl ? leftMargin() : x_)
693 + width_ - text_.rightMargin(pars_[pit_])) / 2;
694 x -= font_metrics::width(str, font) / 2;
696 x = width_ - leftMargin() -
697 font_metrics::width(str, font);
700 yo_ + row_.baseline() - row_.ascent_of_text() - maxdesc,
707 void RowPainter::paintLast()
709 bool const is_rtl = text_.isRTL(pars_[pit_]);
710 int const endlabel = getEndLabel(pit_, text_.paragraphs());
715 case END_LABEL_FILLED_BOX: {
716 LyXFont const font = getLabelFont();
717 int const size = int(0.75 * font_metrics::maxAscent(font));
718 int const y = yo_ + row_.baseline() - size;
719 int x = is_rtl ? NEST_MARGIN + CHANGEBAR_MARGIN: width_ - size;
721 if (width_ - int(row_.width()) <= size)
722 x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
724 if (endlabel == END_LABEL_BOX)
725 pain_.rectangle(x, y, size, size, LColor::eolmarker);
727 pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
731 case END_LABEL_STATIC: {
732 LyXFont font = getLabelFont();
733 string const & str = pars_[pit_].layout()->endlabelstring();
734 double const x = is_rtl ?
735 x_ - font_metrics::width(str, font)
736 : - text_.rightMargin(pars_[pit_]) - row_.width();
737 pain_.text(int(x), yo_ + row_.baseline(), str, font);
741 case END_LABEL_NO_LABEL:
747 void RowPainter::paintText()
749 pos_type const end = row_.endpos();
750 pos_type body_pos = pars_[pit_].beginOfBody();
752 (body_pos > end || !pars_[pit_].isLineSeparator(body_pos - 1))) {
756 LyXLayout_ptr const & layout = pars_[pit_].layout();
758 bool running_strikeout = false;
759 bool is_struckout = false;
760 int last_strikeout_x = 0;
762 for (pos_type vpos = row_.pos(); vpos < end; ) {
763 if (x_ > bv_.workWidth())
766 pos_type pos = text_.bidi.vis2log(vpos);
768 if (pos >= pars_[pit_].size()) {
773 if (x_ + singleWidth(pos) < 0) {
774 x_ += singleWidth(pos);
779 is_struckout = isDeletedText(pars_[pit_], pos);
781 if (is_struckout && !running_strikeout) {
782 running_strikeout = true;
783 last_strikeout_x = int(x_);
786 bool const highly_editable_inset = pars_[pit_].isInset(pos)
787 && isHighlyEditableInset(pars_[pit_].getInset(pos));
789 // if we reach the end of a struck out range, paint it
790 // we also don't paint across things like tables
791 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
792 int const middle = yo_ + (row_.baseline() + row_.top_of_text()) / 2;
793 pain_.line(last_strikeout_x, middle, int(x_), middle,
794 LColor::strikeout, Painter::line_solid, Painter::line_thin);
795 running_strikeout = false;
798 if (body_pos > 0 && pos == body_pos - 1) {
799 int const lwidth = font_metrics::width(layout->labelsep,
802 x_ += label_hfill_ + lwidth - singleWidth(body_pos - 1);
805 if (pars_[pit_].isHfill(pos)) {
808 int const y0 = yo_ + row_.baseline();
809 int const y1 = y0 - defaultRowHeight() / 2;
811 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
813 if (hfillExpansion(pars_[pit_], row_, pos)) {
814 int const y2 = (y0 + y1) / 2;
816 if (pos >= body_pos) {
817 pain_.line(int(x_), y2, int(x_ + hfill_), y2,
819 Painter::line_onoffdash);
822 pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
824 Painter::line_onoffdash);
827 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
831 } else if (pars_[pit_].isSeparator(pos)) {
832 x_ += singleWidth(pos);
841 // if we reach the end of a struck out range, paint it
842 if (running_strikeout) {
843 int const middle = yo_ + (row_.baseline() + row_.top_of_text()) / 2;
844 pain_.line(last_strikeout_x, middle, int(x_), middle,
845 LColor::strikeout, Painter::line_solid, Painter::line_thin);
846 running_strikeout = false;
851 int paintPars(BufferView const & bv, Painter & pain,
852 LyXText const & text, par_type pit, par_type end)
854 //lyxerr << " paintRows: pit: " << &*pit << endl;
855 ParagraphList & pars = text.paragraphs();
857 int y = pars[pit].y + text.yo_ - bv.top_y();
858 int const y2 = pain.paperHeight();
860 for (; pit != end; ++pit) {
861 RowList::iterator row = pars[pit].rows.begin();
862 RowList::iterator rend = pars[pit].rows.end();
864 for ( ; row != rend; ++row) {
865 RowPainter(bv, pain, text, pit, row, y);
879 void refreshPar(BufferView const & bv, LyXText const & text, par_type pit)
882 static NullPainter nop;
883 paintPars(bv, nop, text, pit, pit + 1);
887 int paintText(BufferView const & bv)
890 getParsInRange(bv.text()->paragraphs(), bv.top_y(),
891 bv.top_y() + bv.workHeight(), pit, end);
892 //lyxerr << "top_y: " << bv.top_y() << " y: " << pit->y << endl;
893 return paintPars(bv, bv.painter(), *bv.text(), pit, end);
897 void paintTextInset(LyXText const & text, PainterInfo & pi)
899 paintPars(*pi.base.bv, pi.pain, text, 0, text.paragraphs().size());