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"
55 * A class used for painting an individual row of text.
59 /// initialise and run painter
60 RowPainter(PainterInfo & pi, LyXText const & text,
61 pit_type pit, Row & row, int x, int y);
63 // paint various parts
66 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);
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_;
108 Paragraph const & par_;
112 int const yo_; // current baseline
121 RowPainter::RowPainter(PainterInfo & pi,
122 LyXText const & text, pit_type pit, Row & row, int x, int y)
123 : bv_(*pi.base.bv), pain_(pi.pain), text_(text), pars_(text.paragraphs()),
124 row_(row), pit_(pit), par_(text.paragraphs()[pit]),
125 xo_(x), yo_(y), width_(text_.width())
127 RowMetrics m = text_.computeRowMetrics(pit, row_);
130 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
133 separator_ = m.separator;
135 label_hfill_ = m.label_hfill;
137 BOOST_ASSERT(pit >= 0);
138 BOOST_ASSERT(pit < int(text.paragraphs().size()));
143 LyXFont const RowPainter::getFont(pos_type pos) const
145 return text_.getFont(par_, pos);
149 int RowPainter::singleWidth(lyx::pos_type pos) const
151 return text_.singleWidth(par_, pos);
155 int RowPainter::singleWidth(lyx::pos_type pos, char c) const
157 LyXFont const & font = text_.getFont(par_, pos);
158 return text_.singleWidth(par_, pos, c, font);
162 LyXFont const RowPainter::getLabelFont() const
164 return text_.getLabelFont(par_);
168 int RowPainter::leftMargin() const
170 return text_.leftMargin(pit_, row_.pos());
174 void RowPainter::paintInset(pos_type const pos)
176 InsetBase const * inset = par_.getInset(pos);
178 PainterInfo pi(const_cast<BufferView *>(&bv_), pain_);
179 pi.base.font = getFont(pos);
180 pi.ltr_pos = (text_.bidi.level(pos) % 2 == 0);
181 theCoords.insets_.add(inset, int(x_), yo_);
182 inset->drawSelection(pi, int(x_), yo_);
183 inset->draw(pi, int(x_), yo_);
184 x_ += inset->width();
188 void RowPainter::paintHebrewComposeChar(pos_type & vpos)
190 pos_type pos = text_.bidi.vis2log(vpos);
195 char c = par_.getChar(pos);
199 LyXFont const & font = getFont(pos);
200 int const width = font_metrics::width(c, font);
203 for (pos_type i = pos - 1; i >= 0; --i) {
205 if (!Encodings::IsComposeChar_hebrew(c)) {
206 if (IsPrintableNonspace(c)) {
207 int const width2 = singleWidth(i, c);
209 dx = (c == 'ø' || c == 'ã')
211 : (width2 - width) / 2;
218 pain_.text(int(x_) + dx, yo_, str, font);
222 void RowPainter::paintArabicComposeChar(pos_type & vpos)
224 pos_type pos = text_.bidi.vis2log(vpos);
228 char c = par_.getChar(pos);
229 c = par_.transformChar(c, pos);
233 LyXFont const & font = getFont(pos);
234 int const width = font_metrics::width(c, font);
237 for (pos_type i = pos - 1; i >= 0; --i) {
239 if (!Encodings::IsComposeChar_arabic(c)) {
240 if (IsPrintableNonspace(c)) {
241 int const width2 = singleWidth(i, c);
242 dx = (width2 - width) / 2;
248 pain_.text(int(x_) + dx, yo_, str, font);
252 void RowPainter::paintChars(pos_type & vpos, bool hebrew, bool arabic)
254 pos_type pos = text_.bidi.vis2log(vpos);
255 pos_type const end = row_.endpos();
256 LyXFont orig_font = getFont(pos);
260 str += par_.getChar(pos);
262 unsigned char c = str[0];
263 str[0] = par_.transformChar(c, pos);
266 bool prev_struckout = isDeletedText(par_, pos);
267 bool prev_newtext = isInsertedText(par_, pos);
269 // collect as much similar chars as we can
270 for (++vpos; vpos < end && (pos = text_.bidi.vis2log(vpos)) >= 0; ++vpos) {
271 char c = par_.getChar(pos);
273 if (!IsPrintableNonspace(c))
276 if (prev_struckout != isDeletedText(par_, pos))
279 if (prev_newtext != isInsertedText(par_, pos))
282 if (arabic && Encodings::IsComposeChar_arabic(c))
285 if (hebrew && Encodings::IsComposeChar_hebrew(c))
288 if (orig_font != getFont(pos))
292 c = par_.transformChar(c, pos);
298 orig_font.setColor(LColor::strikeout);
299 else if (prev_newtext)
300 orig_font.setColor(LColor::newtext);
302 // Draw text and set the new x position
303 //lyxerr << "paint row: yo_ " << yo_ << "\n";
304 pain_.text(int(x_), yo_, str, orig_font);
305 x_ += font_metrics::width(str, orig_font);
309 void RowPainter::paintForeignMark(double orig_x, LyXFont const & orig_font)
311 if (!lyxrc.mark_foreign_language)
313 if (orig_font.language() == latex_language)
315 if (orig_font.language() == bv_.buffer()->params().language)
318 int const y = yo_ + 1;
319 pain_.line(int(orig_x), y, int(x_), y, LColor::language);
323 void RowPainter::paintFromPos(pos_type & vpos)
325 pos_type const pos = text_.bidi.vis2log(vpos);
327 LyXFont const & orig_font = getFont(pos);
329 double const orig_x = x_;
331 char const c = par_.getChar(pos);
333 if (c == Paragraph::META_INSET) {
336 paintForeignMark(orig_x, orig_font);
340 // usual characters, no insets
342 // special case languages
343 bool const hebrew = (orig_font.language()->lang() == "hebrew");
345 orig_font.language()->lang() == "arabic" &&
346 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
347 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
349 // draw as many chars as we can
350 if ((!hebrew && !arabic)
351 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
352 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
353 paintChars(vpos, hebrew, arabic);
355 paintHebrewComposeChar(vpos);
357 paintArabicComposeChar(vpos);
360 paintForeignMark(orig_x, orig_font);
364 void RowPainter::paintChangeBar()
366 pos_type const start = row_.pos();
367 pos_type const end = row_.endpos();
369 if (start == end || !par_.isChanged(start, end - 1))
372 int const height = text_.isLastRow(pit_, row_)
376 pain_.fillRectangle(4, yo_ - row_.ascent(), 5, height, LColor::changebar);
380 void RowPainter::paintAppendix()
382 if (!par_.params().appendix())
385 int y = yo_ - row_.ascent();
387 if (par_.params().startOfAppendix())
388 y += 2 * defaultRowHeight();
390 pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
391 pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), LColor::appendix);
395 void RowPainter::paintDepthBar()
397 Paragraph::depth_type const depth = par_.getDepth();
402 Paragraph::depth_type prev_depth = 0;
403 if (!text_.isFirstRow(pit_, row_)) {
404 pit_type pit2 = pit_;
407 prev_depth = pars_[pit2].getDepth();
410 Paragraph::depth_type next_depth = 0;
411 if (!text_.isLastRow(pit_, row_)) {
412 pit_type pit2 = pit_;
413 if (row_.endpos() >= pars_[pit2].size())
415 next_depth = pars_[pit2].getDepth();
418 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
419 int const w = nestMargin() / 5;
420 int x = int(xo_) + w * i;
421 // only consider the changebar space if we're drawing outermost text
422 if (text_.isMainText())
423 x += changebarMargin();
425 int const starty = yo_ - row_.ascent();
426 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
428 pain_.line(x, starty, x, starty + h, LColor::depthbar);
431 pain_.fillRectangle(x, starty, w, 2, LColor::depthbar);
433 pain_.fillRectangle(x, starty + h, w, 2, LColor::depthbar);
438 int RowPainter::paintAppendixStart(int y)
441 pb_font.setColor(LColor::appendix);
444 string const label = _("Appendix");
448 font_metrics::rectText(label, pb_font, w, a, d);
450 int const text_start = int(xo_ + (width_ - w) / 2);
451 int const text_end = text_start + w;
453 pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
455 pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
456 pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
458 return 3 * defaultRowHeight();
462 void RowPainter::paintFirst()
464 ParagraphParameters const & parparams = par_.params();
468 // start of appendix?
469 if (parparams.startOfAppendix())
470 y_top += paintAppendixStart(yo_ + y_top + 2 * defaultRowHeight());
472 Buffer const & buffer = *bv_.buffer();
474 LyXLayout_ptr const & layout = par_.layout();
476 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
478 if (layout->latextype == LATEX_PARAGRAPH
479 && !par_.getDepth()) {
480 y_top += buffer.params().getDefSkip().inPixels(bv_);
482 LyXLayout_ptr const & playout = pars_[pit_ - 1].layout();
483 if (playout->latextype == LATEX_PARAGRAPH
484 && !pars_[pit_ - 1].getDepth()) {
485 // is it right to use defskip here, too? (AS)
486 y_top += buffer.params().getDefSkip().inPixels(bv_);
492 bool const is_rtl = text_.isRTL(par_);
493 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
494 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
496 // should we print a label?
497 if (layout->labeltype >= LABEL_STATIC
498 && (layout->labeltype != LABEL_STATIC
499 || layout->latextype != LATEX_ENVIRONMENT
502 LyXFont font = getLabelFont();
503 if (!par_.getLabelstring().empty()) {
505 string const str = par_.getLabelstring();
507 // this is special code for the chapter layout. This is
508 // printed in an extra row and has a pagebreak at
510 if (layout->counter == "chapter") {
511 if (buffer.params().secnumdepth >= 0) {
512 double spacing_val = 1.0;
513 if (!parparams.spacing().isDefault()) {
514 spacing_val = parparams.spacing().getValue();
516 spacing_val = buffer.params().spacing().getValue();
519 #warning Look is this correct?
521 int const labeladdon = int(font_metrics::maxHeight(font) * layout->spacing.getValue() * spacing_val);
523 int const maxdesc = int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
524 + int(layout->parsep) * defaultRowHeight();
527 x = width_ - leftMargin() -
528 font_metrics::width(str, font);
531 pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
535 x = width_ - leftMargin()
536 + font_metrics::width(layout->labelsep, font);
538 x = x_ - font_metrics::width(layout->labelsep, font)
539 - font_metrics::width(str, font);
542 pain_.text(int(x), yo_, str, font);
546 // the labels at the top of an environment.
547 // More or less for bibliography
549 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
550 layout->labeltype == LABEL_BIBLIO ||
551 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
552 LyXFont font = getLabelFont();
553 if (!par_.getLabelstring().empty()) {
554 string const str = par_.getLabelstring();
555 double spacing_val = 1.0;
556 if (!parparams.spacing().isDefault()) {
557 spacing_val = parparams.spacing().getValue();
559 spacing_val = buffer.params().spacing().getValue();
563 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
564 + (layout->labelbottomsep * defaultRowHeight()));
567 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
568 x = ((is_rtl ? leftMargin() : x_)
569 + width_ - text_.rightMargin(par_)) / 2;
570 x -= font_metrics::width(str, font) / 2;
572 x = width_ - leftMargin() -
573 font_metrics::width(str, font);
575 pain_.text(int(x), yo_ - maxdesc, str, font);
581 void RowPainter::paintLast()
583 bool const is_rtl = text_.isRTL(par_);
584 int const endlabel = getEndLabel(pit_, text_.paragraphs());
589 case END_LABEL_FILLED_BOX: {
590 LyXFont const font = getLabelFont();
591 int const size = int(0.75 * font_metrics::maxAscent(font));
592 int const y = yo_ - size;
593 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
595 if (width_ - int(row_.width()) <= size)
596 x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
598 if (endlabel == END_LABEL_BOX)
599 pain_.rectangle(x, y, size, size, LColor::eolmarker);
601 pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
605 case END_LABEL_STATIC: {
606 LyXFont font = getLabelFont();
607 string const & str = par_.layout()->endlabelstring();
608 double const x = is_rtl ?
609 x_ - font_metrics::width(str, font)
610 : - text_.rightMargin(par_) - row_.width();
611 pain_.text(int(x), yo_, str, font);
615 case END_LABEL_NO_LABEL:
621 void RowPainter::paintText()
623 pos_type const end = row_.endpos();
624 pos_type body_pos = par_.beginOfBody();
626 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
630 LyXLayout_ptr const & layout = par_.layout();
632 bool running_strikeout = false;
633 bool is_struckout = false;
634 int last_strikeout_x = 0;
636 for (pos_type vpos = row_.pos(); vpos < end; ) {
637 if (x_ > bv_.workWidth())
640 pos_type const pos = text_.bidi.vis2log(vpos);
642 if (pos >= par_.size()) {
647 const int width_pos = singleWidth(pos);
648 if (x_ + width_pos < 0) {
654 is_struckout = isDeletedText(par_, pos);
656 if (is_struckout && !running_strikeout) {
657 running_strikeout = true;
658 last_strikeout_x = int(x_);
661 bool const highly_editable_inset = par_.isInset(pos)
662 && isHighlyEditableInset(par_.getInset(pos));
664 // if we reach the end of a struck out range, paint it
665 // we also don't paint across things like tables
666 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
667 // FIXME this should take real text height into account, not
668 // the whole row including padding whitespace
669 //int const middle = yo_ + (- row_.ascent() + row_.top_of_text()) / 2;
670 int const middle = yo_ - row_.ascent() / 2;
671 pain_.line(last_strikeout_x, middle, int(x_), middle,
672 LColor::strikeout, Painter::line_solid, Painter::line_thin);
673 running_strikeout = false;
676 if (body_pos > 0 && pos == body_pos - 1) {
677 int const lwidth = font_metrics::width(layout->labelsep,
680 x_ += label_hfill_ + lwidth - width_pos;
683 if (par_.isHfill(pos)) {
687 int const y1 = y0 - defaultRowHeight() / 2;
689 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
691 if (hfillExpansion(par_, row_, pos)) {
692 int const y2 = (y0 + y1) / 2;
694 if (pos >= body_pos) {
695 pain_.line(int(x_), y2, int(x_ + hfill_), y2,
697 Painter::line_onoffdash);
700 pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
702 Painter::line_onoffdash);
705 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
709 } else if (par_.isSeparator(pos)) {
719 // if we reach the end of a struck out range, paint it
720 if (running_strikeout) {
721 //top_of_text = font_metrics::maxAscent(font);
722 //int const middle = yo_ - top_of_text() / 2;
723 int const middle = yo_ - row_.ascent() / 2;
724 pain_.line(last_strikeout_x, middle, int(x_), middle,
725 LColor::strikeout, Painter::line_solid, Painter::line_thin);
726 running_strikeout = false;
732 (PainterInfo & pi, LyXText const & text, pit_type pit, int x, int y)
734 // lyxerr << " paintPar: pit: " << pit << " at y: " << y << endl;
735 static NullPainter nop;
736 static PainterInfo nullpi(pi.base.bv, nop);
737 int const ww = pi.base.bv->workHeight();
739 Paragraph & par = text.paragraphs()[pit];
741 RowList::iterator const rb = par.rows().begin();
742 RowList::iterator const re = par.rows().end();
743 theCoords.pars_[&text][pit] = Point(x, y);
746 for (RowList::iterator rit = rb; rit != re; ++rit) {
748 bool const inside = (y + rit->descent() >= 0
749 && y - rit->ascent() < ww);
750 RowPainter rp(inside ? pi : nullpi, text, pit, *rit, x, y);
767 void paintText(BufferView const & bv, ViewMetricsInfo const & vi)
769 Painter & pain = bv.painter();
770 LyXText * const text = bv.text();
773 pain.fillRectangle(0, vi.y1, bv.workWidth(), vi.y2 - vi.y1,
777 PainterInfo pi(const_cast<BufferView *>(&bv), pain);
779 text->drawSelection(pi, 0, 0);
783 for (pit_type pit = vi.p1; pit <= vi.p2; ++pit) {
784 yy += text->getPar(pit).ascent();
785 paintPar(pi, *bv.text(), pit, 0, yy);
786 yy += text->getPar(pit).descent();
790 // paint one paragraph above and one below
792 text->redoParagraph(vi.p1 - 1);
793 paintPar(pi, *bv.text(), vi.p1 - 1, 0,
794 vi.y1 - text->getPar(vi.p1 - 1).descent());
797 if (vi.p2 < text->paragraphs().size() - 1) {
798 text->redoParagraph(vi.p2 + 1);
799 paintPar(pi, *bv.text(), vi.p2 + 1, 0,
800 vi.y2 + text->getPar(vi.p2 + 1).ascent());
803 // and grey out above (should not happen later)
804 lyxerr << "par ascent: " << text->getPar(vi.p1).ascent() << endl;
806 pain.fillRectangle(0, 0, bv.workWidth(), vi.y1, LColor::bottomarea);
808 // and possibly grey out below
809 lyxerr << "par descent: " << text->getPar(vi.p1).ascent() << endl;
810 if (vi.y2 < bv.workHeight())
811 pain.fillRectangle(0, vi.y2, bv.workWidth(), bv.workHeight() - vi.y2, LColor::bottomarea);
815 void paintTextInset(LyXText const & text, PainterInfo & pi, int x, int y)
817 // lyxerr << " paintTextInset: y: " << y << endl;
819 y -= text.getPar(0).ascent();
820 for (int pit = 0; pit < int(text.paragraphs().size()); ++pit) {
821 y += text.getPar(pit).ascent();
822 paintPar(pi, text, pit, x, y);
823 y += text.getPar(pit).descent();