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 & 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 const & 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 LyXFont pf(text_.getFont(par_, pos));
146 text_.applyOuterFont(pf);
151 int RowPainter::singleWidth(lyx::pos_type pos) const
153 return text_.singleWidth(par_, pos);
157 int RowPainter::singleWidth(lyx::pos_type pos, char c) const
159 LyXFont const & font = text_.getFont(par_, pos);
160 return text_.singleWidth(par_, pos, c, font);
164 LyXFont const RowPainter::getLabelFont() const
166 return text_.getLabelFont(par_);
170 int RowPainter::leftMargin() const
172 return text_.leftMargin(pit_, row_.pos());
176 void RowPainter::paintInset(pos_type const pos)
178 InsetBase const * inset = par_.getInset(pos);
180 PainterInfo pi(const_cast<BufferView *>(&bv_), pain_);
181 pi.base.font = getFont(pos);
182 pi.ltr_pos = (text_.bidi.level(pos) % 2 == 0);
183 theCoords.insets().add(inset, int(x_), yo_);
184 inset->drawSelection(pi, int(x_), yo_);
185 inset->draw(pi, int(x_), yo_);
186 x_ += inset->width();
190 void RowPainter::paintHebrewComposeChar(pos_type & vpos)
192 pos_type pos = text_.bidi.vis2log(vpos);
197 char c = par_.getChar(pos);
201 LyXFont const & font = getFont(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)
226 pos_type pos = text_.bidi.vis2log(vpos);
230 char c = par_.getChar(pos);
231 c = par_.transformChar(c, pos);
235 LyXFont const & font = getFont(pos);
236 int const width = font_metrics::width(c, font);
239 for (pos_type i = pos - 1; i >= 0; --i) {
241 if (!Encodings::IsComposeChar_arabic(c)) {
242 if (IsPrintableNonspace(c)) {
243 int const width2 = singleWidth(i, c);
244 dx = (width2 - width) / 2;
250 pain_.text(int(x_) + dx, yo_, str, font);
254 void RowPainter::paintChars(pos_type & vpos, bool hebrew, bool arabic)
256 pos_type pos = text_.bidi.vis2log(vpos);
257 pos_type const end = row_.endpos();
258 LyXFont orig_font = getFont(pos);
262 str += par_.getChar(pos);
264 unsigned char c = str[0];
265 str[0] = par_.transformChar(c, pos);
268 bool prev_struckout = isDeletedText(par_, pos);
269 bool prev_newtext = isInsertedText(par_, pos);
271 // collect as much similar chars as we can
272 for (++vpos; vpos < end && (pos = text_.bidi.vis2log(vpos)) >= 0; ++vpos) {
273 char c = par_.getChar(pos);
275 if (!IsPrintableNonspace(c))
278 if (prev_struckout != isDeletedText(par_, pos))
281 if (prev_newtext != isInsertedText(par_, pos))
284 if (arabic && Encodings::IsComposeChar_arabic(c))
287 if (hebrew && Encodings::IsComposeChar_hebrew(c))
290 if (orig_font != getFont(pos))
294 c = par_.transformChar(c, pos);
300 orig_font.setColor(LColor::strikeout);
301 else if (prev_newtext)
302 orig_font.setColor(LColor::newtext);
304 // Draw text and set the new x position
305 //lyxerr << "paint row: yo_ " << yo_ << "\n";
306 pain_.text(int(x_), yo_, str, orig_font);
307 x_ += font_metrics::width(str, orig_font);
311 void RowPainter::paintForeignMark(double orig_x, LyXFont const & orig_font)
313 if (!lyxrc.mark_foreign_language)
315 if (orig_font.language() == latex_language)
317 if (orig_font.language() == bv_.buffer()->params().language)
320 int const y = yo_ + 1;
321 pain_.line(int(orig_x), y, int(x_), y, LColor::language);
325 void RowPainter::paintFromPos(pos_type & vpos)
327 pos_type const pos = text_.bidi.vis2log(vpos);
329 LyXFont const & orig_font = getFont(pos);
331 double const orig_x = x_;
333 char const c = par_.getChar(pos);
335 if (c == Paragraph::META_INSET) {
338 paintForeignMark(orig_x, orig_font);
342 // usual characters, no insets
344 // special case languages
345 bool const hebrew = (orig_font.language()->lang() == "hebrew");
347 orig_font.language()->lang() == "arabic" &&
348 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
349 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
351 // draw as many chars as we can
352 if ((!hebrew && !arabic)
353 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
354 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
355 paintChars(vpos, hebrew, arabic);
357 paintHebrewComposeChar(vpos);
359 paintArabicComposeChar(vpos);
362 paintForeignMark(orig_x, orig_font);
366 void RowPainter::paintChangeBar()
368 pos_type const start = row_.pos();
369 pos_type const end = row_.endpos();
371 if (start == end || !par_.isChanged(start, end - 1))
374 int const height = text_.isLastRow(pit_, row_)
378 pain_.fillRectangle(4, yo_ - row_.ascent(), 5, height, LColor::changebar);
382 void RowPainter::paintAppendix()
384 if (!par_.params().appendix())
387 int y = yo_ - row_.ascent();
389 if (par_.params().startOfAppendix())
390 y += 2 * defaultRowHeight();
392 pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
393 pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), LColor::appendix);
397 void RowPainter::paintDepthBar()
399 Paragraph::depth_type const depth = par_.getDepth();
404 Paragraph::depth_type prev_depth = 0;
405 if (!text_.isFirstRow(pit_, row_)) {
406 pit_type pit2 = pit_;
409 prev_depth = pars_[pit2].getDepth();
412 Paragraph::depth_type next_depth = 0;
413 if (!text_.isLastRow(pit_, row_)) {
414 pit_type pit2 = pit_;
415 if (row_.endpos() >= pars_[pit2].size())
417 next_depth = pars_[pit2].getDepth();
420 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
421 int const w = nestMargin() / 5;
422 int x = int(xo_) + w * i;
423 // only consider the changebar space if we're drawing outermost text
424 if (text_.isMainText())
425 x += changebarMargin();
427 int const starty = yo_ - row_.ascent();
428 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
430 pain_.line(x, starty, x, starty + h, LColor::depthbar);
433 pain_.fillRectangle(x, starty, w, 2, LColor::depthbar);
435 pain_.fillRectangle(x, starty + h, w, 2, LColor::depthbar);
440 int RowPainter::paintAppendixStart(int y)
443 pb_font.setColor(LColor::appendix);
446 string const label = _("Appendix");
450 font_metrics::rectText(label, pb_font, w, a, d);
452 int const text_start = int(xo_ + (width_ - w) / 2);
453 int const text_end = text_start + w;
455 pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
457 pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
458 pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
460 return 3 * defaultRowHeight();
464 void RowPainter::paintFirst()
466 ParagraphParameters const & parparams = par_.params();
470 // start of appendix?
471 if (parparams.startOfAppendix())
472 y_top += paintAppendixStart(yo_ + y_top + 2 * defaultRowHeight());
474 Buffer const & buffer = *bv_.buffer();
476 LyXLayout_ptr const & layout = par_.layout();
478 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
480 if (layout->latextype == LATEX_PARAGRAPH
481 && !par_.getDepth()) {
482 y_top += buffer.params().getDefSkip().inPixels(bv_);
484 LyXLayout_ptr const & playout = pars_[pit_ - 1].layout();
485 if (playout->latextype == LATEX_PARAGRAPH
486 && !pars_[pit_ - 1].getDepth()) {
487 // is it right to use defskip here, too? (AS)
488 y_top += buffer.params().getDefSkip().inPixels(bv_);
494 bool const is_rtl = text_.isRTL(par_);
495 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
496 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
498 // should we print a label?
499 if (layout->labeltype >= LABEL_STATIC
500 && (layout->labeltype != LABEL_STATIC
501 || layout->latextype != LATEX_ENVIRONMENT
504 LyXFont const font = getLabelFont();
505 string const str = par_.getLabelstring();
509 // this is special code for the chapter layout. This is
510 // printed in an extra row and has a pagebreak at
512 if (layout->counter == "chapter") {
513 double spacing_val = 1.0;
514 if (!parparams.spacing().isDefault()) {
515 spacing_val = parparams.spacing().getValue();
517 spacing_val = buffer.params().spacing().getValue();
520 #warning Look is this correct?
522 int const labeladdon = int(font_metrics::maxHeight(font) * layout->spacing.getValue() * spacing_val);
524 int const maxdesc = int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
525 + int(layout->parsep) * defaultRowHeight();
528 x = width_ - leftMargin() -
529 font_metrics::width(str, font);
532 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 // calculate 1/3 height of the buffer's default font
669 yo_ - font_metrics::maxAscent(text_.defaultfont_) / 3;
670 pain_.line(last_strikeout_x, middle, int(x_), middle,
671 LColor::strikeout, Painter::line_solid, Painter::line_thin);
672 running_strikeout = false;
675 if (body_pos > 0 && pos == body_pos - 1) {
676 int const lwidth = font_metrics::width(layout->labelsep,
679 x_ += label_hfill_ + lwidth - width_pos;
682 if (par_.isHfill(pos)) {
686 int const y1 = y0 - defaultRowHeight() / 2;
688 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
690 if (hfillExpansion(par_, row_, pos)) {
691 int const y2 = (y0 + y1) / 2;
693 if (pos >= body_pos) {
694 pain_.line(int(x_), y2, int(x_ + hfill_), y2,
696 Painter::line_onoffdash);
699 pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
701 Painter::line_onoffdash);
704 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
708 } else if (par_.isSeparator(pos)) {
718 // if we reach the end of a struck out range, paint it
719 if (running_strikeout) {
720 // calculate 1/3 height of the buffer's default font
722 yo_ - font_metrics::maxAscent(text_.defaultfont_) / 3;
723 pain_.line(last_strikeout_x, middle, int(x_), middle,
724 LColor::strikeout, Painter::line_solid, Painter::line_thin);
725 running_strikeout = false;
731 (PainterInfo & pi, LyXText const & text, pit_type pit, int x, int y)
733 // lyxerr << " paintPar: pit: " << pit << " at y: " << y << endl;
734 static NullPainter nop;
735 static PainterInfo nullpi(pi.base.bv, nop);
736 int const ww = pi.base.bv->workHeight();
738 Paragraph & par = text.paragraphs()[pit];
740 RowList::const_iterator const rb = par.rows().begin();
741 RowList::const_iterator const re = par.rows().end();
742 theCoords.parPos()[&text][pit] = Point(x, y);
745 for (RowList::const_iterator rit = rb; rit != re; ++rit) {
747 bool const inside = (y + rit->descent() >= 0
748 && y - rit->ascent() < ww);
749 RowPainter rp(inside ? pi : nullpi, text, pit, *rit, x, y);
766 void paintText(BufferView const & bv, ViewMetricsInfo const & vi)
768 Painter & pain = bv.painter();
769 LyXText * const text = bv.text();
772 pain.fillRectangle(0, vi.y1, bv.workWidth(), vi.y2 - vi.y1,
776 PainterInfo pi(const_cast<BufferView *>(&bv), pain);
778 text->drawSelection(pi, 0, 0);
782 for (pit_type pit = vi.p1; pit <= vi.p2; ++pit) {
783 yy += text->getPar(pit).ascent();
784 paintPar(pi, *bv.text(), pit, 0, yy);
785 yy += text->getPar(pit).descent();
789 // paint one paragraph above and one below
791 text->redoParagraph(vi.p1 - 1);
792 paintPar(pi, *bv.text(), vi.p1 - 1, 0,
793 vi.y1 - text->getPar(vi.p1 - 1).descent());
796 if (vi.p2 < lyx::pit_type(text->paragraphs().size()) - 1) {
797 text->redoParagraph(vi.p2 + 1);
798 paintPar(pi, *bv.text(), vi.p2 + 1, 0,
799 vi.y2 + text->getPar(vi.p2 + 1).ascent());
802 // and grey out above (should not happen later)
803 // lyxerr << "par ascent: " << text->getPar(vi.p1).ascent() << endl;
805 pain.fillRectangle(0, 0, bv.workWidth(), vi.y1, LColor::bottomarea);
807 // and possibly grey out below
808 // lyxerr << "par descent: " << text->getPar(vi.p1).ascent() << endl;
809 if (vi.y2 < bv.workHeight())
810 pain.fillRectangle(0, vi.y2, bv.workWidth(), bv.workHeight() - vi.y2, LColor::bottomarea);
814 void paintTextInset(LyXText const & text, PainterInfo & pi, int x, int y)
816 // lyxerr << " paintTextInset: y: " << y << endl;
818 y -= text.getPar(0).ascent();
819 for (int pit = 0; pit < int(text.paragraphs().size()); ++pit) {
820 y += text.getPar(pit).ascent();
821 paintPar(pi, text, pit, x, y);
822 y += text.getPar(pit).descent();