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 & 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 std::pair<lyx::pos_type, lyx::pos_type> const font_span
259 = par_.getFontSpan(pos);
260 Change::Type const prev_change = par_.lookupChange(pos);
264 str += par_.getChar(pos);
266 unsigned char c = str[0];
267 str[0] = par_.transformChar(c, pos);
270 // collect as much similar chars as we can
271 for (++vpos ; vpos < end ; ++vpos) {
272 pos = text_.bidi.vis2log(vpos);
273 if (pos < font_span.first || pos > font_span.second)
276 if (prev_change != par_.lookupChange(pos))
279 char c = par_.getChar(pos);
281 if (!IsPrintableNonspace(c))
284 if (arabic && Encodings::IsComposeChar_arabic(c))
287 if (hebrew && Encodings::IsComposeChar_hebrew(c))
291 c = par_.transformChar(c, pos);
296 if (prev_change == Change::DELETED)
297 font.setColor(LColor::strikeout);
298 else if (prev_change == Change::INSERTED)
299 font.setColor(LColor::newtext);
301 // Draw text and set the new x position
302 //lyxerr << "paint row: yo_ " << yo_ << "\n";
303 pain_.text(int(x_), yo_, str, font);
304 x_ += font_metrics::width(str, font);
308 void RowPainter::paintForeignMark(double orig_x, LyXFont const & font)
310 if (!lyxrc.mark_foreign_language)
312 if (font.language() == latex_language)
314 if (font.language() == bv_.buffer()->params().language)
317 int const y = yo_ + 1;
318 pain_.line(int(orig_x), y, int(x_), y, LColor::language);
322 void RowPainter::paintFromPos(pos_type & vpos)
324 pos_type const pos = text_.bidi.vis2log(vpos);
326 LyXFont const & orig_font = getFont(pos);
328 double const orig_x = x_;
330 char const c = par_.getChar(pos);
332 if (c == Paragraph::META_INSET) {
333 paintInset(pos, orig_font);
335 paintForeignMark(orig_x, orig_font);
339 // usual characters, no insets
341 // special case languages
342 bool const hebrew = (orig_font.language()->lang() == "hebrew");
344 orig_font.language()->lang() == "arabic" &&
345 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
346 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
348 // draw as many chars as we can
349 if ((!hebrew && !arabic)
350 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
351 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
352 paintChars(vpos, orig_font, hebrew, arabic);
354 paintHebrewComposeChar(vpos, orig_font);
356 paintArabicComposeChar(vpos, orig_font);
359 paintForeignMark(orig_x, orig_font);
363 void RowPainter::paintChangeBar()
365 pos_type const start = row_.pos();
366 pos_type const end = row_.endpos();
368 if (start == end || !par_.isChanged(start, end - 1))
371 int const height = text_.isLastRow(pit_, row_)
375 pain_.fillRectangle(4, yo_ - row_.ascent(), 5, height, LColor::changebar);
379 void RowPainter::paintAppendix()
381 if (!par_.params().appendix())
384 int y = yo_ - row_.ascent();
386 if (par_.params().startOfAppendix())
387 y += 2 * defaultRowHeight();
389 pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
390 pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), LColor::appendix);
394 void RowPainter::paintDepthBar()
396 Paragraph::depth_type const depth = par_.getDepth();
401 Paragraph::depth_type prev_depth = 0;
402 if (!text_.isFirstRow(pit_, row_)) {
403 pit_type pit2 = pit_;
406 prev_depth = pars_[pit2].getDepth();
409 Paragraph::depth_type next_depth = 0;
410 if (!text_.isLastRow(pit_, row_)) {
411 pit_type pit2 = pit_;
412 if (row_.endpos() >= pars_[pit2].size())
414 next_depth = pars_[pit2].getDepth();
417 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
418 int const w = nestMargin() / 5;
419 int x = int(xo_) + w * i;
420 // only consider the changebar space if we're drawing outermost text
421 if (text_.isMainText())
422 x += changebarMargin();
424 int const starty = yo_ - row_.ascent();
425 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
427 pain_.line(x, starty, x, starty + h, LColor::depthbar);
430 pain_.fillRectangle(x, starty, w, 2, LColor::depthbar);
432 pain_.fillRectangle(x, starty + h, w, 2, LColor::depthbar);
437 int RowPainter::paintAppendixStart(int y)
440 pb_font.setColor(LColor::appendix);
443 string const label = _("Appendix");
447 font_metrics::rectText(label, pb_font, w, a, d);
449 int const text_start = int(xo_ + (width_ - w) / 2);
450 int const text_end = text_start + w;
452 pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
454 pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
455 pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
457 return 3 * defaultRowHeight();
461 void RowPainter::paintFirst()
463 ParagraphParameters const & parparams = par_.params();
467 // start of appendix?
468 if (parparams.startOfAppendix())
469 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
471 Buffer const & buffer = *bv_.buffer();
473 LyXLayout_ptr const & layout = par_.layout();
475 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
477 if (layout->latextype == LATEX_PARAGRAPH
478 && !par_.getDepth()) {
479 y_top += buffer.params().getDefSkip().inPixels(bv_);
481 LyXLayout_ptr const & playout = pars_[pit_ - 1].layout();
482 if (playout->latextype == LATEX_PARAGRAPH
483 && !pars_[pit_ - 1].getDepth()) {
484 // is it right to use defskip here, too? (AS)
485 y_top += buffer.params().getDefSkip().inPixels(bv_);
491 bool const is_rtl = text_.isRTL(par_);
492 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
493 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
495 // should we print a label?
496 if (layout->labeltype >= LABEL_STATIC
497 && (layout->labeltype != LABEL_STATIC
498 || layout->latextype != LATEX_ENVIRONMENT
501 LyXFont const font = getLabelFont();
502 string const str = par_.getLabelstring();
506 // this is special code for the chapter layout. This is
507 // printed in an extra row and has a pagebreak at
509 if (layout->counter == "chapter") {
510 double spacing_val = 1.0;
511 if (!parparams.spacing().isDefault()) {
512 spacing_val = parparams.spacing().getValue();
514 spacing_val = buffer.params().spacing().getValue();
517 #warning Look is this correct?
519 int const labeladdon = int(font_metrics::maxHeight(font) * layout->spacing.getValue() * spacing_val);
521 int const maxdesc = int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
522 + int(layout->parsep) * defaultRowHeight();
525 x = width_ - leftMargin() -
526 font_metrics::width(str, font);
529 pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
532 x = width_ - leftMargin()
533 + font_metrics::width(layout->labelsep, font);
535 x = x_ - font_metrics::width(layout->labelsep, font)
536 - font_metrics::width(str, font);
539 pain_.text(int(x), yo_, str, font);
543 // the labels at the top of an environment.
544 // More or less for bibliography
546 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
547 layout->labeltype == LABEL_BIBLIO ||
548 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
549 LyXFont font = getLabelFont();
550 if (!par_.getLabelstring().empty()) {
551 string const str = par_.getLabelstring();
552 double spacing_val = 1.0;
553 if (!parparams.spacing().isDefault()) {
554 spacing_val = parparams.spacing().getValue();
556 spacing_val = buffer.params().spacing().getValue();
560 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
561 + (layout->labelbottomsep * defaultRowHeight()));
564 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
565 x = ((is_rtl ? leftMargin() : x_)
566 + width_ - text_.rightMargin(par_)) / 2;
567 x -= font_metrics::width(str, font) / 2;
569 x = width_ - leftMargin() -
570 font_metrics::width(str, font);
572 pain_.text(int(x), yo_ - maxdesc, str, font);
578 void RowPainter::paintLast()
580 bool const is_rtl = text_.isRTL(par_);
581 int const endlabel = getEndLabel(pit_, text_.paragraphs());
586 case END_LABEL_FILLED_BOX: {
587 LyXFont const font = getLabelFont();
588 int const size = int(0.75 * font_metrics::maxAscent(font));
589 int const y = yo_ - size;
590 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
592 if (width_ - int(row_.width()) <= size)
593 x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
595 if (endlabel == END_LABEL_BOX)
596 pain_.rectangle(x, y, size, size, LColor::eolmarker);
598 pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
602 case END_LABEL_STATIC: {
603 LyXFont font = getLabelFont();
604 string const & str = par_.layout()->endlabelstring();
605 double const x = is_rtl ?
606 x_ - font_metrics::width(str, font)
607 : - text_.rightMargin(par_) - row_.width();
608 pain_.text(int(x), yo_, str, font);
612 case END_LABEL_NO_LABEL:
618 void RowPainter::paintText()
620 pos_type const end = row_.endpos();
621 pos_type body_pos = par_.beginOfBody();
623 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
627 LyXLayout_ptr const & layout = par_.layout();
629 bool running_strikeout = false;
630 bool is_struckout = false;
631 int last_strikeout_x = 0;
633 for (pos_type vpos = row_.pos(); vpos < end; ) {
634 if (x_ > bv_.workWidth())
637 pos_type const pos = text_.bidi.vis2log(vpos);
639 if (pos >= par_.size()) {
644 const int width_pos = singleWidth(pos);
645 if (x_ + width_pos < 0) {
651 is_struckout = isDeletedText(par_, pos);
653 if (is_struckout && !running_strikeout) {
654 running_strikeout = true;
655 last_strikeout_x = int(x_);
658 bool const highly_editable_inset = par_.isInset(pos)
659 && isHighlyEditableInset(par_.getInset(pos));
661 // if we reach the end of a struck out range, paint it
662 // we also don't paint across things like tables
663 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
664 // calculate 1/3 height of the buffer's default font
666 yo_ - font_metrics::maxAscent(text_.defaultfont_) / 3;
667 pain_.line(last_strikeout_x, middle, int(x_), middle,
668 LColor::strikeout, Painter::line_solid, Painter::line_thin);
669 running_strikeout = false;
672 if (body_pos > 0 && pos == body_pos - 1) {
673 int const lwidth = font_metrics::width(layout->labelsep,
676 x_ += label_hfill_ + lwidth - width_pos;
679 if (par_.isHfill(pos)) {
683 int const y1 = y0 - defaultRowHeight() / 2;
685 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
687 if (hfillExpansion(par_, row_, pos)) {
688 int const y2 = (y0 + y1) / 2;
690 if (pos >= body_pos) {
691 pain_.line(int(x_), y2, int(x_ + hfill_), y2,
693 Painter::line_onoffdash);
696 pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
698 Painter::line_onoffdash);
701 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
705 } else if (par_.isSeparator(pos)) {
715 // if we reach the end of a struck out range, paint it
716 if (running_strikeout) {
717 // calculate 1/3 height of the buffer's default font
719 yo_ - font_metrics::maxAscent(text_.defaultfont_) / 3;
720 pain_.line(last_strikeout_x, middle, int(x_), middle,
721 LColor::strikeout, Painter::line_solid, Painter::line_thin);
722 running_strikeout = false;
728 (PainterInfo & pi, LyXText const & text, pit_type pit, int x, int y)
730 // lyxerr << " paintPar: pit: " << pit << " at y: " << y << endl;
731 static NullPainter nop;
732 static PainterInfo nullpi(pi.base.bv, nop);
733 int const ww = pi.base.bv->workHeight();
735 Paragraph & par = text.paragraphs()[pit];
737 RowList::const_iterator const rb = par.rows().begin();
738 RowList::const_iterator const re = par.rows().end();
739 theCoords.parPos()[&text][pit] = Point(x, y);
742 for (RowList::const_iterator rit = rb; rit != re; ++rit) {
744 bool const inside = (y + rit->descent() >= 0
745 && y - rit->ascent() < ww);
746 RowPainter rp(inside ? pi : nullpi, text, pit, *rit, x, y);
763 void paintText(BufferView const & bv, ViewMetricsInfo const & vi)
765 Painter & pain = bv.painter();
766 LyXText * const text = bv.text();
769 pain.fillRectangle(0, vi.y1, bv.workWidth(), vi.y2 - vi.y1,
773 PainterInfo pi(const_cast<BufferView *>(&bv), pain);
775 text->drawSelection(pi, 0, 0);
779 for (pit_type pit = vi.p1; pit <= vi.p2; ++pit) {
780 yy += text->getPar(pit).ascent();
781 paintPar(pi, *bv.text(), pit, 0, yy);
782 yy += text->getPar(pit).descent();
786 // paint one paragraph above and one below
787 // Note MV: this cannot be suppressed even for singlepar.
788 // Try viewing the User Guide Mobius figure
790 text->redoParagraph(vi.p1 - 1);
791 paintPar(pi, *bv.text(), vi.p1 - 1, 0,
792 vi.y1 - text->getPar(vi.p1 - 1).descent());
795 if (vi.p2 < lyx::pit_type(text->paragraphs().size()) - 1) {
796 text->redoParagraph(vi.p2 + 1);
797 paintPar(pi, *bv.text(), vi.p2 + 1, 0,
798 vi.y2 + text->getPar(vi.p2 + 1).ascent());
801 // and grey out above (should not happen later)
802 // lyxerr << "par ascent: " << text->getPar(vi.p1).ascent() << endl;
804 pain.fillRectangle(0, 0, bv.workWidth(), vi.y1, LColor::bottomarea);
806 // and possibly grey out below
807 // lyxerr << "par descent: " << text->getPar(vi.p1).ascent() << endl;
808 if (vi.y2 < bv.workHeight())
809 pain.fillRectangle(0, vi.y2, bv.workWidth(), bv.workHeight() - vi.y2, LColor::bottomarea);
813 void paintTextInset(LyXText const & text, PainterInfo & pi, int x, int y)
815 // lyxerr << " paintTextInset: y: " << y << endl;
817 y -= text.getPar(0).ascent();
818 for (int pit = 0; pit < int(text.paragraphs().size()); ++pit) {
819 y += text.getPar(pit).ascent();
820 paintPar(pi, text, pit, x, y);
821 y += text.getPar(pit).descent();