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"
17 #include "coordcache.h"
20 #include "bufferparams.h"
21 #include "BufferView.h"
28 #include "lyxrow_funcs.h"
29 #include "metricsinfo.h"
30 #include "paragraph.h"
31 #include "paragraph_funcs.h"
32 #include "ParagraphParameters.h"
35 #include "frontends/font_metrics.h"
36 #include "frontends/nullpainter.h"
37 #include "frontends/Painter.h"
39 #include "insets/insettext.h"
41 #include "support/textutils.h"
54 * A class used for painting an individual row of text.
58 /// initialise and run painter
59 RowPainter(BufferView const & bv, Painter & pain, LyXText const & text,
60 par_type pit, RowList::iterator rit, int y);
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_;
100 ParagraphList & pars_;
103 RowList::iterator const rit_;
108 Paragraph const & par_;
121 RowPainter::RowPainter(BufferView const & bv, Painter & pain,
122 LyXText const & text, par_type pit, RowList::iterator rit, int y)
123 : bv_(bv), pain_(pain), text_(text), pars_(text.paragraphs()),
124 rit_(rit), row_(*rit), pit_(pit), par_(text.paragraphs()[pit]),
125 xo_(text_.xo_), yo_(y), width_(text_.width())
127 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo << " yo: " << yo
128 // << " pit->y: " << pit_->y
129 // << " row: " << (par_.size() ? par_.getChar(row_.pos()) : 'X') << endl;
131 RowMetrics m = text_.computeRowMetrics(pit, row_);
134 separator_ = m.separator;
136 label_hfill_ = m.label_hfill;
138 // background has already been cleared.
139 if (&text_ == bv_.text())
142 // paint the selection background
143 if (bv_.cursor().selection() && &text_ == bv_.cursor().text())
146 // vertical lines for appendix
149 // environment depth brackets
158 if (row_.endpos() >= par_.size())
167 LyXFont const RowPainter::getFont(pos_type pos) const
169 return text_.getFont(par_, pos);
173 int RowPainter::singleWidth(lyx::pos_type pos) const
175 return text_.singleWidth(par_, pos);
179 int RowPainter::singleWidth(lyx::pos_type pos, char c) const
181 LyXFont const & font = text_.getFont(par_, pos);
182 return text_.singleWidth(par_, pos, c, font);
186 LyXFont const RowPainter::getLabelFont() const
188 return text_.getLabelFont(par_);
192 int RowPainter::leftMargin() const
194 return text_.leftMargin(pit_, row_.pos());
198 void RowPainter::paintInset(pos_type const pos)
200 InsetBase const * inset = par_.getInset(pos);
202 PainterInfo pi(const_cast<BufferView *>(&bv_), pain_);
203 pi.base.font = getFont(pos);
204 pi.ltr_pos = (text_.bidi.level(pos) % 2 == 0);
205 theCoords.insets_.add(inset, int(x_), yo_ + row_.baseline());
206 inset->drawSelection(pi, int(x_), yo_ + row_.baseline());
207 inset->draw(pi, int(x_), yo_ + row_.baseline());
208 x_ += inset->width();
212 void RowPainter::paintHebrewComposeChar(pos_type & vpos)
214 pos_type pos = text_.bidi.vis2log(vpos);
219 char c = par_.getChar(pos);
223 LyXFont const & font = getFont(pos);
224 int const width = font_metrics::width(c, font);
227 for (pos_type i = pos - 1; i >= 0; --i) {
229 if (!Encodings::IsComposeChar_hebrew(c)) {
230 if (IsPrintableNonspace(c)) {
231 int const width2 = singleWidth(i, c);
233 dx = (c == 'ø' || c == 'ã')
235 : (width2 - width) / 2;
242 pain_.text(int(x_) + dx, yo_ + row_.baseline(), str, font);
246 void RowPainter::paintArabicComposeChar(pos_type & vpos)
248 pos_type pos = text_.bidi.vis2log(vpos);
252 char c = par_.getChar(pos);
253 c = par_.transformChar(c, pos);
257 LyXFont const & font = getFont(pos);
258 int const width = font_metrics::width(c, font);
261 for (pos_type i = pos - 1; i >= 0; --i) {
263 if (!Encodings::IsComposeChar_arabic(c)) {
264 if (IsPrintableNonspace(c)) {
265 int const width2 = singleWidth(i, c);
266 dx = (width2 - width) / 2;
272 pain_.text(int(x_) + dx, yo_ + row_.baseline(), str, font);
276 void RowPainter::paintChars(pos_type & vpos, bool hebrew, bool arabic)
278 pos_type pos = text_.bidi.vis2log(vpos);
279 pos_type const end = row_.endpos();
280 LyXFont orig_font = getFont(pos);
284 str += par_.getChar(pos);
286 unsigned char c = str[0];
287 str[0] = par_.transformChar(c, pos);
290 bool prev_struckout = isDeletedText(par_, pos);
291 bool prev_newtext = isInsertedText(par_, pos);
293 // collect as much similar chars as we can
294 for (++vpos; vpos < end && (pos = text_.bidi.vis2log(vpos)) >= 0; ++vpos) {
295 char c = par_.getChar(pos);
297 if (!IsPrintableNonspace(c))
300 if (prev_struckout != isDeletedText(par_, pos))
303 if (prev_newtext != isInsertedText(par_, pos))
306 if (arabic && Encodings::IsComposeChar_arabic(c))
309 if (hebrew && Encodings::IsComposeChar_hebrew(c))
312 if (orig_font != getFont(pos))
316 c = par_.transformChar(c, pos);
322 orig_font.setColor(LColor::strikeout);
323 else if (prev_newtext)
324 orig_font.setColor(LColor::newtext);
326 // Draw text and set the new x position
327 //lyxerr << "paint row: yo_ " << yo_ << " baseline: " << row_.baseline()
329 pain_.text(int(x_), yo_ + row_.baseline(), str, orig_font);
330 x_ += font_metrics::width(str, orig_font);
334 void RowPainter::paintForeignMark(double orig_x, LyXFont const & orig_font)
336 if (!lyxrc.mark_foreign_language)
338 if (orig_font.language() == latex_language)
340 if (orig_font.language() == bv_.buffer()->params().language)
343 int const y = yo_ + row_.baseline() + 1;
344 pain_.line(int(orig_x), y, int(x_), y, LColor::language);
348 void RowPainter::paintFromPos(pos_type & vpos)
350 pos_type const pos = text_.bidi.vis2log(vpos);
352 LyXFont const & orig_font = getFont(pos);
354 double const orig_x = x_;
356 char const c = par_.getChar(pos);
358 if (c == Paragraph::META_INSET) {
361 paintForeignMark(orig_x, orig_font);
365 // usual characters, no insets
367 // special case languages
368 bool const hebrew = (orig_font.language()->lang() == "hebrew");
370 orig_font.language()->lang() == "arabic" &&
371 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
372 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
374 // draw as many chars as we can
375 if ((!hebrew && !arabic)
376 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
377 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
378 paintChars(vpos, hebrew, arabic);
380 paintHebrewComposeChar(vpos);
382 paintArabicComposeChar(vpos);
385 paintForeignMark(orig_x, orig_font);
389 void RowPainter::paintBackground()
391 int const x = int(xo_);
392 int const y = yo_ < 0 ? 0 : yo_;
393 int const h = yo_ < 0 ? row_.height() + yo_ : row_.height();
394 pain_.fillRectangle(x, y, width_, h, text_.backgroundColor());
398 void RowPainter::paintSelection()
400 bool const is_rtl = text_.isRTL(par_);
402 // the current selection
403 LCursor const & cur = bv_.cursor();
404 int const starty = text_.cursorY(cur.selBegin());
405 int const endy = text_.cursorY(cur.selEnd());
406 par_type startpit = cur.selBegin().par();
407 par_type endpit = cur.selEnd().par();
408 RowList::iterator startrow = pars_[startpit].getRow(cur.selBegin().pos());
409 RowList::iterator endrow = pars_[endpit].getRow(cur.selEnd().pos());
410 int const h = row_.height();
412 int const row_y = text_.yo_ + par_.y + row_.y_offset();
414 bool const sel_starts_here = startpit == pit_ && startrow == rit_;
415 bool const sel_ends_here = endpit == pit_ && endrow == rit_;
416 bool const sel_on_one_row = sel_starts_here && sel_ends_here;
418 if (text_.bidi.same_direction()) {
419 if (sel_on_one_row) {
420 int const startx = text_.cursorX(cur.selBegin());
421 int const endx = text_.cursorX(cur.selEnd());
422 int const x1 = is_rtl ? endx : startx;
423 int const x2 = is_rtl ? startx : endx;
424 pain_.fillRectangle(x1, yo_, x2 - x1, h, LColor::selection);
425 } else if (sel_starts_here) {
426 int const startx = text_.cursorX(cur.selBegin());
427 int const x1 = is_rtl ? int(xo_) : startx;
428 int const x2 = is_rtl ? startx : int(xo_) + width_;
429 pain_.fillRectangle(x1, yo_, x2 - x1, h, LColor::selection);
430 } else if (sel_ends_here) {
431 int const endx = text_.cursorX(cur.selEnd());
432 int const x1 = is_rtl ? endx : int(xo_);
433 int const x2 = is_rtl ? int(xo_) + width_ : endx;
434 pain_.fillRectangle(x1, yo_, x2 - x1, h, LColor::selection);
435 } else if (row_y > starty && row_y < endy) {
436 pain_.fillRectangle(int(xo_), yo_, width_, h, LColor::selection);
441 if ((startpit != pit_ && startrow != rit_ && !is_rtl)
442 || (endpit != pit_ && endrow != rit_ && is_rtl))
443 pain_.fillRectangle(int(xo_), yo_,
444 int(x_), h, LColor::selection);
446 pos_type const body_pos = par_.beginOfBody();
447 pos_type const end = row_.endpos();
450 for (pos_type vpos = row_.pos(); vpos < end; ++vpos) {
451 pos_type pos = text_.bidi.vis2log(vpos);
452 double const old_tmpx = tmpx;
453 if (body_pos > 0 && pos == body_pos - 1) {
454 LyXLayout_ptr const & layout = par_.layout();
455 LyXFont const lfont = getLabelFont();
457 tmpx += label_hfill_ + font_metrics::width(layout->labelsep, lfont);
459 if (par_.isLineSeparator(body_pos - 1))
460 tmpx -= singleWidth(body_pos - 1);
463 tmpx += singleWidth(pos);
465 if (hfillExpansion(par_, row_, pos)) {
469 tmpx += label_hfill_;
471 if (par_.isSeparator(pos) && pos >= body_pos)
475 if (((startpit != pit_ && startrow != rit_)
476 || cur.selBegin().pos() <= pos) &&
477 ((endpit != pit_ && endrow != rit_)
478 || pos < cur.selEnd().pos())) {
479 // Here we do not use x_ as xo_ was added to x_.
480 pain_.fillRectangle(int(old_tmpx), yo_,
481 int(tmpx - old_tmpx + 1), h, LColor::selection);
485 if ((startpit != pit_ && startrow != rit_ && is_rtl) ||
486 (endpit != pit_ && endrow != rit_ && !is_rtl)) {
487 pain_.fillRectangle(int(xo_ + tmpx),
488 yo_, int(width_ - tmpx), h, LColor::selection);
493 void RowPainter::paintChangeBar()
495 pos_type const start = row_.pos();
496 pos_type const end = row_.endpos();
498 if (start == end || !par_.isChanged(start, end - 1))
501 int const height = text_.isLastRow(pit_, row_)
503 : row_.height() + boost::next(rit_)->top_of_text();
505 pain_.fillRectangle(4, yo_, 5, height, LColor::changebar);
509 void RowPainter::paintAppendix()
511 if (!par_.params().appendix())
516 if (par_.params().startOfAppendix())
517 y += 2 * defaultRowHeight();
519 pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
520 pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), LColor::appendix);
524 void RowPainter::paintDepthBar()
526 Paragraph::depth_type const depth = par_.getDepth();
531 Paragraph::depth_type prev_depth = 0;
532 if (!text_.isFirstRow(pit_, row_)) {
533 par_type pit2 = pit_;
536 prev_depth = pars_[pit2].getDepth();
539 Paragraph::depth_type next_depth = 0;
540 if (!text_.isLastRow(pit_, row_)) {
541 par_type pit2 = pit_;
542 if (row_.endpos() >= pars_[pit2].size())
544 next_depth = pars_[pit2].getDepth();
547 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
548 int const w = nestMargin() / 5;
549 int x = int(xo_) + w * i;
550 // only consider the changebar space if we're drawing outermost text
551 if (text_.isMainText())
552 x += changebarMargin();
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 = par_.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 = par_.layout();
604 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
606 if (layout->latextype == LATEX_PARAGRAPH
607 && !par_.getDepth()) {
608 y_top += buffer.params().getDefSkip().inPixels(bv_);
610 LyXLayout_ptr const & playout = pars_[pit_ - 1].layout();
611 if (playout->latextype == LATEX_PARAGRAPH
612 && !pars_[pit_ - 1].getDepth()) {
613 // is it right to use defskip here, too? (AS)
614 y_top += buffer.params().getDefSkip().inPixels(bv_);
620 bool const is_rtl = text_.isRTL(par_);
621 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
622 //lyxerr << "paintFirst: " << par_.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 (!par_.getLabelstring().empty()) {
633 string const str = par_.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 = width_ - leftMargin() -
653 font_metrics::width(str, font);
657 yo_ + row_.baseline() -
658 row_.ascent_of_text() - maxdesc,
663 x = width_ - 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 (!par_.getLabelstring().empty()) {
682 string const str = par_.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 + width_ - text_.rightMargin(par_)) / 2;
698 x -= font_metrics::width(str, font) / 2;
700 x = width_ - leftMargin() -
701 font_metrics::width(str, font);
704 yo_ + row_.baseline() - row_.ascent_of_text() - maxdesc,
711 void RowPainter::paintLast()
713 bool const is_rtl = text_.isRTL(par_);
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 ? nestMargin() + changebarMargin() : width_ - size;
725 if (width_ - int(row_.width()) <= size)
726 x += (size - width_ + row_.width() + 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 = par_.layout()->endlabelstring();
738 double const x = is_rtl ?
739 x_ - font_metrics::width(str, font)
740 : - text_.rightMargin(par_) - row_.width();
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 = par_.beginOfBody();
756 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
760 LyXLayout_ptr const & layout = par_.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 const pos = text_.bidi.vis2log(vpos);
772 if (pos >= par_.size()) {
777 const int width_pos = singleWidth(pos);
778 if (x_ + width_pos < 0) {
784 is_struckout = isDeletedText(par_, pos);
786 if (is_struckout && !running_strikeout) {
787 running_strikeout = true;
788 last_strikeout_x = int(x_);
791 bool const highly_editable_inset = par_.isInset(pos)
792 && isHighlyEditableInset(par_.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 - width_pos;
810 if (par_.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(par_, 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 (par_.isSeparator(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, Painter & pain,
857 LyXText const & text, par_type pit, par_type end)
859 //lyxerr << " paintRows: pit: " << &*pit << endl;
860 ParagraphList & pars = text.paragraphs();
862 int y = pars[pit].y + text.yo_ - bv.top_y();
864 for (; pit != end; ++pit) {
865 RowList::iterator row = pars[pit].rows.begin();
866 RowList::iterator rend = pars[pit].rows.end();
868 // We draw full paragraphs to get the (xo, yo) cache of all
869 // contained insets right. This is needed for properly working
870 // editXY. And maybe not even sufficient.
871 // FIXME: Alfredo, please have a look at the coordinate business
873 for ( ; row != rend; ++row) {
874 RowPainter(bv, pain, text, pit, row, y);
885 void refreshPar(BufferView const & bv, LyXText const & text, par_type pit)
887 static NullPainter nop;
888 paintPars(bv, nop, text, pit, pit + 1);
892 int paintText(BufferView const & bv)
895 getParsInRange(bv.text()->paragraphs(), bv.top_y(),
896 bv.top_y() + bv.workHeight(), pit, end);
897 //lyxerr << "top_y: " << bv.top_y() << " y: " << pit->y << endl;
898 return paintPars(bv, bv.painter(), *bv.text(), pit, end);
902 void paintTextInset(LyXText const & text, PainterInfo & pi)
904 paintPars(*pi.base.bv, pi.pain, text, 0, text.paragraphs().size());