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 const & row, int x, int y);
63 // paint various parts
66 void paintChangeBar();
72 void paintForeignMark(double orig_x, LyXFont const & font);
73 void paintHebrewComposeChar(lyx::pos_type & vpos, LyXFont const & font);
74 void paintArabicComposeChar(lyx::pos_type & vpos, LyXFont const & font);
75 void paintChars(lyx::pos_type & vpos, LyXFont font,
76 bool hebrew, bool arabic);
77 int paintAppendixStart(int y);
78 void paintFromPos(lyx::pos_type & vpos);
79 void paintInset(lyx::pos_type const pos, LyXFont const & font);
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 const & pars_;
109 Paragraph const & par_;
113 int const yo_; // current baseline
122 RowPainter::RowPainter(PainterInfo & pi,
123 LyXText const & text, pit_type pit, Row const & row, int x, int y)
124 : bv_(*pi.base.bv), pain_(pi.pain), text_(text), pars_(text.paragraphs()),
125 row_(row), pit_(pit), par_(text.paragraphs()[pit]),
126 xo_(x), yo_(y), width_(text_.width())
128 RowMetrics m = text_.computeRowMetrics(pit, row_);
131 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
134 separator_ = m.separator;
136 label_hfill_ = m.label_hfill;
138 BOOST_ASSERT(pit >= 0);
139 BOOST_ASSERT(pit < int(text.paragraphs().size()));
144 LyXFont const RowPainter::getFont(pos_type pos) const
146 LyXFont pf(text_.getFont(par_, pos));
147 text_.applyOuterFont(pf);
152 int RowPainter::singleWidth(lyx::pos_type pos) const
154 return text_.singleWidth(par_, pos);
158 int RowPainter::singleWidth(lyx::pos_type pos, char c) const
160 LyXFont const & font = text_.getFont(par_, pos);
161 return text_.singleWidth(par_, pos, c, font);
165 LyXFont const RowPainter::getLabelFont() const
167 return text_.getLabelFont(par_);
171 int RowPainter::leftMargin() const
173 return text_.leftMargin(pit_, row_.pos());
177 void RowPainter::paintInset(pos_type const pos, LyXFont const & font)
179 InsetBase const * inset = par_.getInset(pos);
181 PainterInfo pi(const_cast<BufferView *>(&bv_), pain_);
183 pi.ltr_pos = (text_.bidi.level(pos) % 2 == 0);
184 theCoords.insets().add(inset, int(x_), yo_);
185 inset->drawSelection(pi, int(x_), yo_);
186 inset->draw(pi, int(x_), yo_);
187 x_ += inset->width();
191 void RowPainter::paintHebrewComposeChar(pos_type & vpos, LyXFont const & font)
193 pos_type pos = text_.bidi.vis2log(vpos);
198 char c = par_.getChar(pos);
202 int const width = font_metrics::width(c, font);
205 for (pos_type i = pos - 1; i >= 0; --i) {
207 if (!Encodings::IsComposeChar_hebrew(c)) {
208 if (IsPrintableNonspace(c)) {
209 int const width2 = singleWidth(i, c);
211 dx = (c == 'ø' || c == 'ã')
213 : (width2 - width) / 2;
220 pain_.text(int(x_) + dx, yo_, str, font);
224 void RowPainter::paintArabicComposeChar(pos_type & vpos, LyXFont const & font)
226 pos_type pos = text_.bidi.vis2log(vpos);
230 char c = par_.getChar(pos);
231 c = par_.transformChar(c, pos);
235 int const width = font_metrics::width(c, font);
238 for (pos_type i = pos - 1; i >= 0; --i) {
240 if (!Encodings::IsComposeChar_arabic(c)) {
241 if (IsPrintableNonspace(c)) {
242 int const width2 = singleWidth(i, c);
243 dx = (width2 - width) / 2;
249 pain_.text(int(x_) + dx, yo_, str, font);
253 void RowPainter::paintChars(pos_type & vpos, LyXFont font,
254 bool hebrew, bool arabic)
256 pos_type pos = text_.bidi.vis2log(vpos);
257 pos_type const end = row_.endpos();
258 FontSpan const font_span = par_.fontSpan(pos);
259 Change::Type const prev_change = par_.lookupChange(pos);
263 str += par_.getChar(pos);
265 unsigned char c = str[0];
266 str[0] = par_.transformChar(c, pos);
269 // collect as much similar chars as we can
270 for (++vpos ; vpos < end ; ++vpos) {
271 pos = text_.bidi.vis2log(vpos);
272 if (pos < font_span.first || pos > font_span.last)
275 if (prev_change != par_.lookupChange(pos))
278 char c = par_.getChar(pos);
280 if (!IsPrintableNonspace(c))
283 if (arabic && Encodings::IsComposeChar_arabic(c))
286 if (hebrew && Encodings::IsComposeChar_hebrew(c))
290 c = par_.transformChar(c, pos);
295 if (prev_change == Change::DELETED)
296 font.setColor(LColor::strikeout);
297 else if (prev_change == Change::INSERTED)
298 font.setColor(LColor::newtext);
300 // Draw text and set the new x position
301 //lyxerr << "paint row: yo_ " << yo_ << "\n";
302 pain_.text(int(x_), yo_, str, font);
303 x_ += font_metrics::width(str, font);
307 void RowPainter::paintForeignMark(double orig_x, LyXFont const & font)
309 if (!lyxrc.mark_foreign_language)
311 if (font.language() == latex_language)
313 if (font.language() == bv_.buffer()->params().language)
316 int const y = yo_ + 1;
317 pain_.line(int(orig_x), y, int(x_), y, LColor::language);
321 void RowPainter::paintFromPos(pos_type & vpos)
323 pos_type const pos = text_.bidi.vis2log(vpos);
325 LyXFont const & orig_font = getFont(pos);
327 double const orig_x = x_;
329 char const c = par_.getChar(pos);
331 if (c == Paragraph::META_INSET) {
332 paintInset(pos, orig_font);
334 paintForeignMark(orig_x, orig_font);
338 // usual characters, no insets
340 // special case languages
341 bool const hebrew = (orig_font.language()->lang() == "hebrew");
343 orig_font.language()->lang() == "arabic" &&
344 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
345 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
347 // draw as many chars as we can
348 if ((!hebrew && !arabic)
349 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
350 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
351 paintChars(vpos, orig_font, hebrew, arabic);
353 paintHebrewComposeChar(vpos, orig_font);
355 paintArabicComposeChar(vpos, orig_font);
358 paintForeignMark(orig_x, orig_font);
362 void RowPainter::paintChangeBar()
364 pos_type const start = row_.pos();
365 pos_type const end = row_.endpos();
367 if (start == end || !par_.isChanged(start, end - 1))
370 int const height = text_.isLastRow(pit_, row_)
374 pain_.fillRectangle(4, yo_ - row_.ascent(), 5, height, LColor::changebar);
378 void RowPainter::paintAppendix()
380 if (!par_.params().appendix())
383 int y = yo_ - row_.ascent();
385 if (par_.params().startOfAppendix())
386 y += 2 * defaultRowHeight();
388 pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
389 pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), LColor::appendix);
393 void RowPainter::paintDepthBar()
395 Paragraph::depth_type const depth = par_.getDepth();
400 Paragraph::depth_type prev_depth = 0;
401 if (!text_.isFirstRow(pit_, row_)) {
402 pit_type pit2 = pit_;
405 prev_depth = pars_[pit2].getDepth();
408 Paragraph::depth_type next_depth = 0;
409 if (!text_.isLastRow(pit_, row_)) {
410 pit_type pit2 = pit_;
411 if (row_.endpos() >= pars_[pit2].size())
413 next_depth = pars_[pit2].getDepth();
416 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
417 int const w = nestMargin() / 5;
418 int x = int(xo_) + w * i;
419 // only consider the changebar space if we're drawing outermost text
420 if (text_.isMainText())
421 x += changebarMargin();
423 int const starty = yo_ - row_.ascent();
424 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
426 pain_.line(x, starty, x, starty + h, LColor::depthbar);
429 pain_.fillRectangle(x, starty, w, 2, LColor::depthbar);
431 pain_.fillRectangle(x, starty + h, w, 2, LColor::depthbar);
436 int RowPainter::paintAppendixStart(int y)
439 pb_font.setColor(LColor::appendix);
442 string const label = _("Appendix");
446 font_metrics::rectText(label, pb_font, w, a, d);
448 int const text_start = int(xo_ + (width_ - w) / 2);
449 int const text_end = text_start + w;
451 pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
453 pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
454 pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
456 return 3 * defaultRowHeight();
460 void RowPainter::paintFirst()
462 ParagraphParameters const & parparams = par_.params();
466 // start of appendix?
467 if (parparams.startOfAppendix())
468 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
470 Buffer const & buffer = *bv_.buffer();
472 LyXLayout_ptr const & layout = par_.layout();
474 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
476 if (layout->latextype == LATEX_PARAGRAPH
477 && !par_.getDepth()) {
478 y_top += buffer.params().getDefSkip().inPixels(bv_);
480 LyXLayout_ptr const & playout = pars_[pit_ - 1].layout();
481 if (playout->latextype == LATEX_PARAGRAPH
482 && !pars_[pit_ - 1].getDepth()) {
483 // is it right to use defskip here, too? (AS)
484 y_top += buffer.params().getDefSkip().inPixels(bv_);
490 bool const is_rtl = text_.isRTL(par_);
491 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
492 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
494 // should we print a label?
495 if (layout->labeltype >= LABEL_STATIC
496 && (layout->labeltype != LABEL_STATIC
497 || layout->latextype != LATEX_ENVIRONMENT
500 LyXFont const font = getLabelFont();
501 string const str = par_.getLabelstring();
505 // this is special code for the chapter layout. This is
506 // printed in an extra row and has a pagebreak at
508 if (layout->counter == "chapter") {
509 double spacing_val = 1.0;
510 if (!parparams.spacing().isDefault()) {
511 spacing_val = parparams.spacing().getValue();
513 spacing_val = buffer.params().spacing().getValue();
516 #warning Look is this correct?
518 int const labeladdon = int(font_metrics::maxHeight(font) * layout->spacing.getValue() * spacing_val);
520 int const maxdesc = int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
521 + int(layout->parsep) * defaultRowHeight();
524 x = width_ - leftMargin() -
525 font_metrics::width(str, font);
528 pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
531 x = width_ - leftMargin()
532 + font_metrics::width(layout->labelsep, font);
534 x = x_ - font_metrics::width(layout->labelsep, font)
535 - font_metrics::width(str, font);
538 pain_.text(int(x), yo_, str, font);
542 // the labels at the top of an environment.
543 // More or less for bibliography
545 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
546 layout->labeltype == LABEL_BIBLIO ||
547 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
548 LyXFont font = getLabelFont();
549 if (!par_.getLabelstring().empty()) {
550 string const str = par_.getLabelstring();
551 double spacing_val = 1.0;
552 if (!parparams.spacing().isDefault()) {
553 spacing_val = parparams.spacing().getValue();
555 spacing_val = buffer.params().spacing().getValue();
559 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
560 + (layout->labelbottomsep * defaultRowHeight()));
563 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
564 x = ((is_rtl ? leftMargin() : x_)
565 + width_ - text_.rightMargin(par_)) / 2;
566 x -= font_metrics::width(str, font) / 2;
568 x = width_ - leftMargin() -
569 font_metrics::width(str, font);
571 pain_.text(int(x), yo_ - maxdesc, str, font);
577 void RowPainter::paintLast()
579 bool const is_rtl = text_.isRTL(par_);
580 int const endlabel = getEndLabel(pit_, text_.paragraphs());
585 case END_LABEL_FILLED_BOX: {
586 LyXFont const font = getLabelFont();
587 int const size = int(0.75 * font_metrics::maxAscent(font));
588 int const y = yo_ - size;
589 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
591 if (width_ - int(row_.width()) <= size)
592 x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
594 if (endlabel == END_LABEL_BOX)
595 pain_.rectangle(x, y, size, size, LColor::eolmarker);
597 pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
601 case END_LABEL_STATIC: {
602 LyXFont font = getLabelFont();
603 string const & str = par_.layout()->endlabelstring();
604 double const x = is_rtl ?
605 x_ - font_metrics::width(str, font)
606 : - text_.rightMargin(par_) - row_.width();
607 pain_.text(int(x), yo_, str, font);
611 case END_LABEL_NO_LABEL:
617 void RowPainter::paintText()
619 pos_type const end = row_.endpos();
620 pos_type body_pos = par_.beginOfBody();
622 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
626 LyXLayout_ptr const & layout = par_.layout();
628 bool running_strikeout = false;
629 bool is_struckout = false;
630 int last_strikeout_x = 0;
632 for (pos_type vpos = row_.pos(); vpos < end; ) {
633 if (x_ > bv_.workWidth())
636 pos_type const pos = text_.bidi.vis2log(vpos);
638 if (pos >= par_.size()) {
643 const int width_pos = singleWidth(pos);
644 if (x_ + width_pos < 0) {
650 is_struckout = isDeletedText(par_, pos);
652 if (is_struckout && !running_strikeout) {
653 running_strikeout = true;
654 last_strikeout_x = int(x_);
657 bool const highly_editable_inset = par_.isInset(pos)
658 && isHighlyEditableInset(par_.getInset(pos));
660 // if we reach the end of a struck out range, paint it
661 // we also don't paint across things like tables
662 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
663 // calculate 1/3 height of the buffer's default font
665 yo_ - font_metrics::maxAscent(text_.defaultfont_) / 3;
666 pain_.line(last_strikeout_x, middle, int(x_), middle,
667 LColor::strikeout, Painter::line_solid, Painter::line_thin);
668 running_strikeout = false;
671 if (body_pos > 0 && pos == body_pos - 1) {
672 int const lwidth = font_metrics::width(layout->labelsep,
675 x_ += label_hfill_ + lwidth - width_pos;
678 if (par_.isHfill(pos)) {
682 int const y1 = y0 - defaultRowHeight() / 2;
684 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
686 if (hfillExpansion(par_, row_, pos)) {
687 int const y2 = (y0 + y1) / 2;
689 if (pos >= body_pos) {
690 pain_.line(int(x_), y2, int(x_ + hfill_), y2,
692 Painter::line_onoffdash);
695 pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
697 Painter::line_onoffdash);
700 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
704 } else if (par_.isSeparator(pos)) {
714 // if we reach the end of a struck out range, paint it
715 if (running_strikeout) {
716 // calculate 1/3 height of the buffer's default font
718 yo_ - font_metrics::maxAscent(text_.defaultfont_) / 3;
719 pain_.line(last_strikeout_x, middle, int(x_), middle,
720 LColor::strikeout, Painter::line_solid, Painter::line_thin);
721 running_strikeout = false;
727 (PainterInfo & pi, LyXText const & text, pit_type pit, int x, int y)
729 // lyxerr << " paintPar: pit: " << pit << " at y: " << y << endl;
730 static NullPainter nop;
731 static PainterInfo nullpi(pi.base.bv, nop);
732 int const ww = pi.base.bv->workHeight();
734 Paragraph const & par = text.paragraphs()[pit];
736 RowList::const_iterator const rb = par.rows().begin();
737 RowList::const_iterator const re = par.rows().end();
738 theCoords.parPos()[&text][pit] = Point(x, y);
741 for (RowList::const_iterator rit = rb; rit != re; ++rit) {
743 bool const inside = (y + rit->descent() >= 0
744 && y - rit->ascent() < ww);
745 RowPainter rp(inside ? pi : nullpi, text, pit, *rit, x, y);
762 void paintText(BufferView const & bv, ViewMetricsInfo const & vi)
764 Painter & pain = bv.painter();
765 LyXText * const text = bv.text();
768 pain.fillRectangle(0, vi.y1, bv.workWidth(), vi.y2 - vi.y1,
772 PainterInfo pi(const_cast<BufferView *>(&bv), pain);
774 text->drawSelection(pi, 0, 0);
778 for (pit_type pit = vi.p1; pit <= vi.p2; ++pit) {
779 yy += text->getPar(pit).ascent();
780 paintPar(pi, *bv.text(), pit, 0, yy);
781 yy += text->getPar(pit).descent();
785 // paint one paragraph above and one below
786 // Note MV: this cannot be suppressed even for singlepar.
787 // Try viewing the User Guide Mobius figure
789 text->redoParagraph(vi.p1 - 1);
790 paintPar(pi, *bv.text(), vi.p1 - 1, 0,
791 vi.y1 - text->getPar(vi.p1 - 1).descent());
794 if (vi.p2 < lyx::pit_type(text->paragraphs().size()) - 1) {
795 text->redoParagraph(vi.p2 + 1);
796 paintPar(pi, *bv.text(), vi.p2 + 1, 0,
797 vi.y2 + text->getPar(vi.p2 + 1).ascent());
800 // and grey out above (should not happen later)
801 // lyxerr << "par ascent: " << text->getPar(vi.p1).ascent() << endl;
803 pain.fillRectangle(0, 0, bv.workWidth(), vi.y1, LColor::bottomarea);
805 // and possibly grey out below
806 // lyxerr << "par descent: " << text->getPar(vi.p1).ascent() << endl;
807 if (vi.y2 < bv.workHeight())
808 pain.fillRectangle(0, vi.y2, bv.workWidth(), bv.workHeight() - vi.y2, LColor::bottomarea);
812 void paintTextInset(LyXText const & text, PainterInfo & pi, int x, int y)
814 // lyxerr << " paintTextInset: y: " << y << endl;
816 y -= text.getPar(0).ascent();
817 for (int pit = 0; pit < int(text.paragraphs().size()); ++pit) {
818 y += text.getPar(pit).ascent();
819 paintPar(pi, text, pit, x, y);
820 y += text.getPar(pit).descent();