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"
48 extern int NEST_MARGIN;
49 extern int CHANGEBAR_MARGIN;
55 * A class used for painting an individual row of text.
59 /// initialise and run painter
60 RowPainter(BufferView const & bv, LyXText const & text,
61 par_type pit, RowList::iterator rit, int xo, int yo);
63 // paint various parts
64 void paintBackground();
65 void paintSelection();
68 void paintChangeBar();
71 void paintForeignMark(double orig_x, LyXFont const & orig_font);
72 void paintHebrewComposeChar(lyx::pos_type & vpos);
73 void paintArabicComposeChar(lyx::pos_type & vpos);
74 void paintChars(lyx::pos_type & vpos, bool hebrew, bool arabic);
75 int paintAppendixStart(int y);
77 void paintFromPos(lyx::pos_type & vpos);
78 void paintInset(lyx::pos_type const pos);
80 /// return left margin
81 int leftMargin() const;
83 /// return the font at the given pos
84 LyXFont const getFont(lyx::pos_type pos) const;
86 /// return the label font for this row
87 LyXFont const getLabelFont() const;
89 /// return pixel width for the given pos
90 int singleWidth(lyx::pos_type pos) const;
91 int singleWidth(lyx::pos_type pos, char c) const;
93 /// bufferview to paint on
94 BufferView const & bv_;
99 /// LyXText for the row
100 LyXText const & text_;
101 ParagraphList & pars_;
104 RowList::iterator const rit_;
108 mutable par_type pit_;
121 RowPainter::RowPainter(BufferView const & bv, LyXText const & text,
122 par_type pit, RowList::iterator rit, int xo, int yo)
123 : bv_(bv), pain_(bv_.painter()), text_(text), pars_(text.paragraphs()),
124 rit_(rit), row_(*rit), pit_(pit), xo_(xo), yo_(yo), width_(text_.width())
126 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo << " yo: " << yo
127 // << " pit->y: " << pit_->y
128 // << " row: " << (pars_[pit_].size() ? pars_[pit_].getChar(row_.pos()) : 'X') << endl;
129 RowMetrics m = text_.computeRowMetrics(pit, row_);
131 separator_ = m.separator;
133 label_hfill_ = m.label_hfill;
135 // background has already been cleared.
136 if (&text_ == bv_.text())
139 // paint the selection background
140 if (bv_.cursor().selection() && &text_ == bv_.cursor().text())
143 // vertical lines for appendix
146 // environment depth brackets
155 if (row_.endpos() >= pars_[pit_].size())
164 LyXFont const RowPainter::getFont(pos_type pos) const
166 return text_.getFont(pit_, pos);
170 int RowPainter::singleWidth(lyx::pos_type pos) const
172 return text_.singleWidth(pit_, pos);
176 int RowPainter::singleWidth(lyx::pos_type pos, char c) const
178 LyXFont const & font = text_.getFont(pit_, pos);
179 return text_.singleWidth(pit_, pos, c, font);
183 LyXFont const RowPainter::getLabelFont() const
185 return text_.getLabelFont(pit_);
189 int RowPainter::leftMargin() const
191 return text_.leftMargin(pit_, row_.pos());
195 void RowPainter::paintInset(pos_type const pos)
197 InsetBase const * inset = pars_[pit_].getInset(pos);
199 PainterInfo pi(const_cast<BufferView *>(&bv_));
200 pi.base.font = getFont(pos);
201 inset->drawSelection(pi, int(x_), yo_ + row_.baseline());
202 inset->draw(pi, int(x_), yo_ + row_.baseline());
203 x_ += inset->width();
207 void RowPainter::paintHebrewComposeChar(pos_type & vpos)
209 pos_type pos = text_.bidi.vis2log(vpos);
214 char c = pars_[pit_].getChar(pos);
218 LyXFont const & font = getFont(pos);
219 int const width = font_metrics::width(c, font);
222 for (pos_type i = pos - 1; i >= 0; --i) {
223 c = pars_[pit_].getChar(i);
224 if (!Encodings::IsComposeChar_hebrew(c)) {
225 if (IsPrintableNonspace(c)) {
226 int const width2 = singleWidth(i, c);
228 dx = (c == 'ø' || c == 'ã')
230 : (width2 - width) / 2;
237 pain_.text(int(x_) + dx, yo_ + row_.baseline(), str, font);
241 void RowPainter::paintArabicComposeChar(pos_type & vpos)
243 pos_type pos = text_.bidi.vis2log(vpos);
247 char c = pars_[pit_].getChar(pos);
248 c = pars_[pit_].transformChar(c, pos);
252 LyXFont const & font = getFont(pos);
253 int const width = font_metrics::width(c, font);
256 for (pos_type i = pos - 1; i >= 0; --i) {
257 c = pars_[pit_].getChar(i);
258 if (!Encodings::IsComposeChar_arabic(c)) {
259 if (IsPrintableNonspace(c)) {
260 int const width2 = singleWidth(i, c);
261 dx = (width2 - width) / 2;
267 pain_.text(int(x_) + dx, yo_ + row_.baseline(), str, font);
271 void RowPainter::paintChars(pos_type & vpos, bool hebrew, bool arabic)
273 pos_type pos = text_.bidi.vis2log(vpos);
274 pos_type const end = row_.endpos();
275 LyXFont orig_font = getFont(pos);
279 str += pars_[pit_].getChar(pos);
281 unsigned char c = str[0];
282 str[0] = pars_[pit_].transformChar(c, pos);
285 bool prev_struckout = isDeletedText(pars_[pit_], pos);
286 bool prev_newtext = isInsertedText(pars_[pit_], pos);
288 // collect as much similar chars as we can
289 for (++vpos; vpos < end && (pos = text_.bidi.vis2log(vpos)) >= 0; ++vpos) {
290 char c = pars_[pit_].getChar(pos);
292 if (!IsPrintableNonspace(c))
295 if (prev_struckout != isDeletedText(pars_[pit_], pos))
298 if (prev_newtext != isInsertedText(pars_[pit_], pos))
301 if (arabic && Encodings::IsComposeChar_arabic(c))
304 if (hebrew && Encodings::IsComposeChar_hebrew(c))
307 if (orig_font != getFont(pos))
311 c = pars_[pit_].transformChar(c, pos);
317 orig_font.setColor(LColor::strikeout);
318 else if (prev_newtext)
319 orig_font.setColor(LColor::newtext);
321 // Draw text and set the new x position
322 //lyxerr << "paint row: yo_ " << yo_ << " baseline: " << row_.baseline()
324 pain_.text(int(x_), yo_ + row_.baseline(), str, orig_font);
325 x_ += font_metrics::width(str, orig_font);
329 void RowPainter::paintForeignMark(double orig_x, LyXFont const & orig_font)
331 if (!lyxrc.mark_foreign_language)
333 if (orig_font.language() == latex_language)
335 if (orig_font.language() == bv_.buffer()->params().language)
338 int const y = yo_ + row_.baseline() + 1;
339 pain_.line(int(orig_x), y, int(x_), y, LColor::language);
343 void RowPainter::paintFromPos(pos_type & vpos)
345 pos_type const pos = text_.bidi.vis2log(vpos);
347 LyXFont const & orig_font = getFont(pos);
349 double const orig_x = x_;
351 char const c = pars_[pit_].getChar(pos);
353 if (c == Paragraph::META_INSET) {
356 paintForeignMark(orig_x, orig_font);
360 // usual characters, no insets
362 // special case languages
363 bool const hebrew = (orig_font.language()->lang() == "hebrew");
365 orig_font.language()->lang() == "arabic" &&
366 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
367 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
369 // draw as many chars as we can
370 if ((!hebrew && !arabic)
371 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
372 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
373 paintChars(vpos, hebrew, arabic);
375 paintHebrewComposeChar(vpos);
377 paintArabicComposeChar(vpos);
380 paintForeignMark(orig_x, orig_font);
384 void RowPainter::paintBackground()
386 int const x = int(xo_);
387 int const y = yo_ < 0 ? 0 : yo_;
388 int const h = yo_ < 0 ? row_.height() + yo_ : row_.height();
389 pain_.fillRectangle(x, y, width_, h, text_.backgroundColor());
393 void RowPainter::paintSelection()
395 bool const is_rtl = text_.isRTL(pars_[pit_]);
397 // the current selection
398 LCursor const & cur = bv_.cursor();
399 int const startx = text_.cursorX(cur.selBegin());
400 int const endx = text_.cursorX(cur.selEnd());
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 x1 = is_rtl ? endx : startx;
418 int const x2 = is_rtl ? startx : endx;
419 pain_.fillRectangle(x1, yo_, x2 - x1, h, LColor::selection);
420 } else if (sel_starts_here) {
421 int const x1 = is_rtl ? int(xo_) : startx;
422 int const x2 = is_rtl ? startx : int(xo_) + width_;
423 pain_.fillRectangle(x1, yo_, x2 - x1, h, LColor::selection);
424 } else if (sel_ends_here) {
425 int const x1 = is_rtl ? endx : int(xo_);
426 int const x2 = is_rtl ? int(xo_) + width_ : endx;
427 pain_.fillRectangle(x1, yo_, x2 - x1, h, LColor::selection);
428 } else if (row_y > starty && row_y < endy) {
429 pain_.fillRectangle(int(xo_), yo_, width_, h, LColor::selection);
434 if ((startpit != pit_ && startrow != rit_ && !is_rtl)
435 || (endpit != pit_ && endrow != rit_ && is_rtl))
436 pain_.fillRectangle(int(xo_), yo_,
437 int(x_), h, LColor::selection);
439 pos_type const body_pos = pars_[pit_].beginOfBody();
440 pos_type const end = row_.endpos();
443 for (pos_type vpos = row_.pos(); vpos < end; ++vpos) {
444 pos_type pos = text_.bidi.vis2log(vpos);
445 double const old_tmpx = tmpx;
446 if (body_pos > 0 && pos == body_pos - 1) {
447 LyXLayout_ptr const & layout = pars_[pit_].layout();
448 LyXFont const lfont = getLabelFont();
450 tmpx += label_hfill_ + font_metrics::width(layout->labelsep, lfont);
452 if (pars_[pit_].isLineSeparator(body_pos - 1))
453 tmpx -= singleWidth(body_pos - 1);
456 tmpx += singleWidth(pos);
458 if (hfillExpansion(pars_[pit_], row_, pos)) {
462 tmpx += label_hfill_;
464 if (pars_[pit_].isSeparator(pos) && pos >= body_pos)
468 if (((startpit != pit_ && startrow != rit_)
469 || cur.selBegin().pos() <= pos) &&
470 ((endpit != pit_ && endrow != rit_)
471 || pos < cur.selEnd().pos())) {
472 // Here we do not use x_ as xo_ was added to x_.
473 pain_.fillRectangle(int(old_tmpx), yo_,
474 int(tmpx - old_tmpx + 1), h, LColor::selection);
478 if ((startpit != pit_ && startrow != rit_ && is_rtl) ||
479 (endpit != pit_ && endrow != rit_ && !is_rtl)) {
480 pain_.fillRectangle(int(xo_ + tmpx),
481 yo_, int(width_ - tmpx), h, LColor::selection);
486 void RowPainter::paintChangeBar()
488 pos_type const start = row_.pos();
489 pos_type const end = row_.endpos();
491 if (start == end || !pars_[pit_].isChanged(start, end - 1))
494 int const height = text_.isLastRow(pit_, row_)
496 : row_.height() + boost::next(rit_)->top_of_text();
498 pain_.fillRectangle(4, yo_, 5, height, LColor::changebar);
502 void RowPainter::paintAppendix()
504 if (!pars_[pit_].params().appendix())
509 if (pars_[pit_].params().startOfAppendix())
510 y += 2 * defaultRowHeight();
512 pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
513 pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), LColor::appendix);
517 void RowPainter::paintDepthBar()
519 Paragraph::depth_type const depth = pars_[pit_].getDepth();
524 Paragraph::depth_type prev_depth = 0;
525 if (!text_.isFirstRow(pit_, row_)) {
526 par_type pit2 = pit_;
529 prev_depth = pars_[pit2].getDepth();
532 Paragraph::depth_type next_depth = 0;
533 if (!text_.isLastRow(pit_, row_)) {
534 par_type pit2 = pit_;
535 if (row_.endpos() >= pars_[pit2].size())
537 next_depth = pars_[pit2].getDepth();
540 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
541 int const w = NEST_MARGIN / 5;
542 int x = int(w * i + xo_);
543 // only consider the changebar space if we're drawing outer left
545 x += CHANGEBAR_MARGIN;
546 int const h = yo_ + row_.height() - 1 - (i - next_depth - 1) * 3;
548 pain_.line(x, yo_, x, h, LColor::depthbar);
551 pain_.fillRectangle(x, yo_, w, 2, LColor::depthbar);
553 pain_.fillRectangle(x, h, w, 2, LColor::depthbar);
558 int RowPainter::paintAppendixStart(int y)
561 pb_font.setColor(LColor::appendix);
564 string const label = _("Appendix");
568 font_metrics::rectText(label, pb_font, w, a, d);
570 int const text_start = int(xo_ + (width_ - w) / 2);
571 int const text_end = text_start + w;
573 pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
575 pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
576 pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
578 return 3 * defaultRowHeight();
582 void RowPainter::paintFirst()
584 ParagraphParameters const & parparams = pars_[pit_].params();
588 // start of appendix?
589 if (parparams.startOfAppendix())
590 y_top += paintAppendixStart(yo_ + y_top + 2 * defaultRowHeight());
592 Buffer const & buffer = *bv_.buffer();
594 LyXLayout_ptr const & layout = pars_[pit_].layout();
596 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
598 if (layout->latextype == LATEX_PARAGRAPH
599 && !pars_[pit_].getDepth()) {
600 y_top += buffer.params().getDefSkip().inPixels(bv_);
602 LyXLayout_ptr const & playout = pars_[pit_ - 1].layout();
603 if (playout->latextype == LATEX_PARAGRAPH
604 && !pars_[pit_ - 1].getDepth()) {
605 // is it right to use defskip here, too? (AS)
606 y_top += buffer.params().getDefSkip().inPixels(bv_);
612 bool const is_rtl = text_.isRTL(pars_[pit_]);
613 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
614 //lyxerr << "paintFirst: " << pars_[pit_].id() << " is_seq: " << is_seq << std::endl;
616 // should we print a label?
617 if (layout->labeltype >= LABEL_STATIC
618 && (layout->labeltype != LABEL_STATIC
619 || layout->latextype != LATEX_ENVIRONMENT
622 LyXFont font = getLabelFont();
623 if (!pars_[pit_].getLabelstring().empty()) {
625 string const str = pars_[pit_].getLabelstring();
627 // this is special code for the chapter layout. This is
628 // printed in an extra row and has a pagebreak at
630 if (layout->counter == "chapter") {
631 if (buffer.params().secnumdepth >= 0) {
632 float spacing_val = 1.0;
633 if (!parparams.spacing().isDefault()) {
634 spacing_val = parparams.spacing().getValue();
636 spacing_val = buffer.params().spacing().getValue();
640 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
641 + int(layout->parsep) * defaultRowHeight();
644 x = width_ - leftMargin() -
645 font_metrics::width(str, font);
649 yo_ + row_.baseline() -
650 row_.ascent_of_text() - maxdesc,
655 x = width_ - leftMargin()
656 + font_metrics::width(layout->labelsep, font);
658 x = x_ - font_metrics::width(layout->labelsep, font)
659 - font_metrics::width(str, font);
662 pain_.text(int(x), yo_ + row_.baseline(), str, font);
666 // the labels at the top of an environment.
667 // More or less for bibliography
669 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
670 layout->labeltype == LABEL_BIBLIO ||
671 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
672 LyXFont font = getLabelFont();
673 if (!pars_[pit_].getLabelstring().empty()) {
674 string const str = pars_[pit_].getLabelstring();
675 float spacing_val = 1.0;
676 if (!parparams.spacing().isDefault()) {
677 spacing_val = parparams.spacing().getValue();
679 spacing_val = buffer.params().spacing().getValue();
683 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
684 + (layout->labelbottomsep * defaultRowHeight()));
687 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
688 x = ((is_rtl ? leftMargin() : x_)
689 + width_ - text_.rightMargin(pars_[pit_])) / 2;
690 x -= font_metrics::width(str, font) / 2;
692 x = width_ - leftMargin() -
693 font_metrics::width(str, font);
696 yo_ + row_.baseline() - row_.ascent_of_text() - maxdesc,
703 void RowPainter::paintLast()
705 bool const is_rtl = text_.isRTL(pars_[pit_]);
706 int const endlabel = getEndLabel(pit_, text_.paragraphs());
711 case END_LABEL_FILLED_BOX: {
712 LyXFont const font = getLabelFont();
713 int const size = int(0.75 * font_metrics::maxAscent(font));
714 int const y = yo_ + row_.baseline() - size;
715 int x = is_rtl ? NEST_MARGIN + CHANGEBAR_MARGIN: width_ - size;
717 if (width_ - int(row_.width()) <= size)
718 x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
720 if (endlabel == END_LABEL_BOX)
721 pain_.rectangle(x, y, size, size, LColor::eolmarker);
723 pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
727 case END_LABEL_STATIC: {
728 LyXFont font = getLabelFont();
729 string const & str = pars_[pit_].layout()->endlabelstring();
730 double const x = is_rtl ?
731 x_ - font_metrics::width(str, font)
732 : - text_.rightMargin(pars_[pit_]) - row_.width();
733 pain_.text(int(x), yo_ + row_.baseline(), str, font);
737 case END_LABEL_NO_LABEL:
743 void RowPainter::paintText()
745 pos_type const end = row_.endpos();
746 pos_type body_pos = pars_[pit_].beginOfBody();
748 (body_pos > end || !pars_[pit_].isLineSeparator(body_pos - 1))) {
752 LyXLayout_ptr const & layout = pars_[pit_].layout();
754 bool running_strikeout = false;
755 bool is_struckout = false;
756 int last_strikeout_x = 0;
758 for (pos_type vpos = row_.pos(); vpos < end; ) {
759 if (x_ > bv_.workWidth())
762 pos_type pos = text_.bidi.vis2log(vpos);
764 if (pos >= pars_[pit_].size()) {
769 if (x_ + singleWidth(pos) < 0) {
770 x_ += singleWidth(pos);
775 is_struckout = isDeletedText(pars_[pit_], pos);
777 if (is_struckout && !running_strikeout) {
778 running_strikeout = true;
779 last_strikeout_x = int(x_);
782 bool const highly_editable_inset = pars_[pit_].isInset(pos)
783 && isHighlyEditableInset(pars_[pit_].getInset(pos));
785 // if we reach the end of a struck out range, paint it
786 // we also don't paint across things like tables
787 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
788 int const middle = yo_ + (row_.baseline() + row_.top_of_text()) / 2;
789 pain_.line(last_strikeout_x, middle, int(x_), middle,
790 LColor::strikeout, Painter::line_solid, Painter::line_thin);
791 running_strikeout = false;
794 if (body_pos > 0 && pos == body_pos - 1) {
795 int const lwidth = font_metrics::width(layout->labelsep,
798 x_ += label_hfill_ + lwidth - singleWidth(body_pos - 1);
801 if (pars_[pit_].isHfill(pos)) {
804 int const y0 = yo_ + row_.baseline();
805 int const y1 = y0 - defaultRowHeight() / 2;
807 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
809 if (hfillExpansion(pars_[pit_], row_, pos)) {
810 int const y2 = (y0 + y1) / 2;
812 if (pos >= body_pos) {
813 pain_.line(int(x_), y2, int(x_ + hfill_), y2,
815 Painter::line_onoffdash);
818 pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
820 Painter::line_onoffdash);
823 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
827 } else if (pars_[pit_].isSeparator(pos)) {
828 x_ += singleWidth(pos);
837 // if we reach the end of a struck out range, paint it
838 if (running_strikeout) {
839 int const middle = yo_ + (row_.baseline() + row_.top_of_text()) / 2;
840 pain_.line(last_strikeout_x, middle, int(x_), middle,
841 LColor::strikeout, Painter::line_solid, Painter::line_thin);
842 running_strikeout = false;
847 int paintPars(BufferView const & bv, LyXText const & text,
848 par_type pit, int xo, int yo, int y)
850 //lyxerr << " paintRows: pit: " << &*pit << endl;
851 int const y2 = bv.painter().paperHeight();
854 ParagraphList & pars = text.paragraphs();
855 for ( ; pit != pars.size(); ++pit) {
856 RowList::iterator row = pars[pit].rows.begin();
857 RowList::iterator rend = pars[pit].rows.end();
859 for ( ; row != rend; ++row) {
860 RowPainter(bv, text, pit, row, xo, y + yo);
873 int paintText(BufferView const & bv)
876 bv.text()->updateParPositions();
877 bv.text()->getRowNearY(bv.top_y(), pit);
878 //lyxerr << "top_y: " << bv.top_y() << " y: " << pit->y << endl;
879 return paintPars(bv, *bv.text(), pit, 0, 0, bv.text()->paragraphs()[pit].y);
883 void paintTextInset(LyXText const & text, PainterInfo & pi, int xo, int yo)
885 paintPars(*pi.base.bv, text, 0, xo, yo, 0);