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 label font for this row
85 LyXFont const getLabelFont() const;
87 /// bufferview to paint on
88 BufferView const & bv_;
93 /// LyXText for the row
94 LyXText const & text_;
95 ParagraphList const & pars_;
102 Paragraph const & par_;
104 /// is row erased? (change tracking)
109 int const yo_; // current baseline
118 RowPainter::RowPainter(PainterInfo & pi,
119 LyXText const & text, pit_type pit, Row const & row, int x, int y)
120 : bv_(*pi.base.bv), pain_(pi.pain), text_(text), pars_(text.paragraphs()),
121 row_(row), pit_(pit), par_(text.paragraphs()[pit]),
123 xo_(x), yo_(y), width_(text_.width())
125 RowMetrics m = text_.computeRowMetrics(pit, row_);
128 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
131 separator_ = m.separator;
133 label_hfill_ = m.label_hfill;
135 BOOST_ASSERT(pit >= 0);
136 BOOST_ASSERT(pit < int(text.paragraphs().size()));
140 LyXFont const RowPainter::getLabelFont() const
142 return text_.getLabelFont(par_);
146 int RowPainter::leftMargin() const
148 return text_.leftMargin(pit_, row_.pos());
152 void RowPainter::paintInset(pos_type const pos, LyXFont const & font)
154 InsetBase const * inset = par_.getInset(pos);
156 PainterInfo pi(const_cast<BufferView *>(&bv_), pain_);
158 pi.ltr_pos = (text_.bidi.level(pos) % 2 == 0);
159 pi.erased_ = erased_ || isDeletedText(par_, pos);
160 theCoords.insets().add(inset, int(x_), yo_);
161 inset->drawSelection(pi, int(x_), yo_);
162 inset->draw(pi, int(x_), yo_);
163 x_ += inset->width();
167 void RowPainter::paintHebrewComposeChar(pos_type & vpos, LyXFont const & font)
169 pos_type pos = text_.bidi.vis2log(vpos);
174 char c = par_.getChar(pos);
178 int const width = font_metrics::width(c, font);
181 for (pos_type i = pos - 1; i >= 0; --i) {
183 if (!Encodings::IsComposeChar_hebrew(c)) {
184 if (IsPrintableNonspace(c)) {
186 text_.singleWidth(par_, i, c, text_.getFont(par_, i));
188 dx = (c == 'ø' || c == 'ã')
190 : (width2 - width) / 2;
197 pain_.text(int(x_) + dx, yo_, str, font);
201 void RowPainter::paintArabicComposeChar(pos_type & vpos, LyXFont const & font)
203 pos_type pos = text_.bidi.vis2log(vpos);
207 char c = par_.getChar(pos);
208 c = par_.transformChar(c, pos);
212 int const width = font_metrics::width(c, font);
215 for (pos_type i = pos - 1; i >= 0; --i) {
217 if (!Encodings::IsComposeChar_arabic(c)) {
218 if (IsPrintableNonspace(c)) {
220 text_.singleWidth(par_, i, c, text_.getFont(par_, i));
221 dx = (width2 - width) / 2;
227 pain_.text(int(x_) + dx, yo_, str, font);
231 void RowPainter::paintChars(pos_type & vpos, LyXFont font,
232 bool hebrew, bool arabic)
234 pos_type pos = text_.bidi.vis2log(vpos);
235 pos_type const end = row_.endpos();
236 FontSpan const font_span = par_.fontSpan(pos);
237 Change::Type const prev_change = par_.lookupChange(pos);
241 str += par_.getChar(pos);
243 unsigned char c = str[0];
244 str[0] = par_.transformChar(c, pos);
247 // collect as much similar chars as we can
248 for (++vpos ; vpos < end ; ++vpos) {
249 pos = text_.bidi.vis2log(vpos);
250 if (pos < font_span.first || pos > font_span.last)
253 if (prev_change != par_.lookupChange(pos))
256 char c = par_.getChar(pos);
258 if (!IsPrintableNonspace(c))
261 if (arabic && Encodings::IsComposeChar_arabic(c))
264 if (hebrew && Encodings::IsComposeChar_hebrew(c))
268 c = par_.transformChar(c, pos);
273 if (prev_change == Change::DELETED)
274 font.setColor(LColor::strikeout);
275 else if (prev_change == Change::INSERTED)
276 font.setColor(LColor::newtext);
278 // Draw text and set the new x position
279 //lyxerr << "paint row: yo_ " << yo_ << "\n";
280 pain_.text(int(x_), yo_, str, font);
281 x_ += font_metrics::width(str, font);
285 void RowPainter::paintForeignMark(double orig_x, LyXFont const & font)
287 if (!lyxrc.mark_foreign_language)
289 if (font.language() == latex_language)
291 if (font.language() == bv_.buffer()->params().language)
294 int const y = yo_ + 1;
295 pain_.line(int(orig_x), y, int(x_), y, LColor::language);
299 void RowPainter::paintFromPos(pos_type & vpos)
301 pos_type const pos = text_.bidi.vis2log(vpos);
302 LyXFont orig_font = text_.getFont(par_, pos);
303 text_.applyOuterFont(orig_font);
305 double const orig_x = x_;
307 if (par_.isInset(pos)) {
308 paintInset(pos, orig_font);
310 paintForeignMark(orig_x, orig_font);
314 // usual characters, no insets
315 char const c = par_.getChar(pos);
317 // special case languages
318 std::string const & lang = orig_font.language()->lang();
319 bool const hebrew = lang == "hebrew";
320 bool const arabic = lang == "arabic" &&
321 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
322 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
324 // draw as many chars as we can
325 if ((!hebrew && !arabic)
326 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
327 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
328 paintChars(vpos, orig_font, hebrew, arabic);
330 paintHebrewComposeChar(vpos, orig_font);
332 paintArabicComposeChar(vpos, orig_font);
335 paintForeignMark(orig_x, orig_font);
339 void RowPainter::paintChangeBar()
341 pos_type const start = row_.pos();
342 pos_type const end = row_.endpos();
344 if (start == end || !par_.isChanged(start, end - 1))
347 int const height = text_.isLastRow(pit_, row_)
351 pain_.fillRectangle(4, yo_ - row_.ascent(), 5, height, LColor::changebar);
355 void RowPainter::paintAppendix()
357 if (!par_.params().appendix())
360 int y = yo_ - row_.ascent();
362 if (par_.params().startOfAppendix())
363 y += 2 * defaultRowHeight();
365 pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
366 pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), LColor::appendix);
370 void RowPainter::paintDepthBar()
372 Paragraph::depth_type const depth = par_.getDepth();
377 Paragraph::depth_type prev_depth = 0;
378 if (!text_.isFirstRow(pit_, row_)) {
379 pit_type pit2 = pit_;
382 prev_depth = pars_[pit2].getDepth();
385 Paragraph::depth_type next_depth = 0;
386 if (!text_.isLastRow(pit_, row_)) {
387 pit_type pit2 = pit_;
388 if (row_.endpos() >= pars_[pit2].size())
390 next_depth = pars_[pit2].getDepth();
393 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
394 int const w = nestMargin() / 5;
395 int x = int(xo_) + w * i;
396 // only consider the changebar space if we're drawing outermost text
397 if (text_.isMainText())
398 x += changebarMargin();
400 int const starty = yo_ - row_.ascent();
401 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
403 pain_.line(x, starty, x, starty + h, LColor::depthbar);
406 pain_.fillRectangle(x, starty, w, 2, LColor::depthbar);
408 pain_.fillRectangle(x, starty + h, w, 2, LColor::depthbar);
413 int RowPainter::paintAppendixStart(int y)
416 pb_font.setColor(LColor::appendix);
419 string const label = _("Appendix");
423 font_metrics::rectText(label, pb_font, w, a, d);
425 int const text_start = int(xo_ + (width_ - w) / 2);
426 int const text_end = text_start + w;
428 pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
430 pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
431 pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
433 return 3 * defaultRowHeight();
437 void RowPainter::paintFirst()
439 ParagraphParameters const & parparams = par_.params();
443 // start of appendix?
444 if (parparams.startOfAppendix())
445 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
447 Buffer const & buffer = *bv_.buffer();
449 LyXLayout_ptr const & layout = par_.layout();
451 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
453 if (layout->latextype == LATEX_PARAGRAPH
454 && !par_.getDepth()) {
455 y_top += buffer.params().getDefSkip().inPixels(bv_);
457 LyXLayout_ptr const & playout = pars_[pit_ - 1].layout();
458 if (playout->latextype == LATEX_PARAGRAPH
459 && !pars_[pit_ - 1].getDepth()) {
460 // is it right to use defskip here, too? (AS)
461 y_top += buffer.params().getDefSkip().inPixels(bv_);
467 bool const is_rtl = text_.isRTL(par_);
468 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
469 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
471 // should we print a label?
472 if (layout->labeltype >= LABEL_STATIC
473 && (layout->labeltype != LABEL_STATIC
474 || layout->latextype != LATEX_ENVIRONMENT
477 LyXFont const font = getLabelFont();
478 string const str = par_.getLabelstring();
482 // this is special code for the chapter layout. This is
483 // printed in an extra row and has a pagebreak at
485 if (layout->counter == "chapter") {
486 double spacing_val = 1.0;
487 if (!parparams.spacing().isDefault()) {
488 spacing_val = parparams.spacing().getValue();
490 spacing_val = buffer.params().spacing().getValue();
493 int const labeladdon = int(font_metrics::maxHeight(font) * layout->spacing.getValue() * spacing_val);
495 int const maxdesc = int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
496 + int(layout->parsep) * defaultRowHeight();
499 x = width_ - leftMargin() -
500 font_metrics::width(str, font);
503 pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
506 x = width_ - leftMargin()
507 + font_metrics::width(layout->labelsep, font);
509 x = x_ - font_metrics::width(layout->labelsep, font)
510 - font_metrics::width(str, font);
513 pain_.text(int(x), yo_, str, font);
517 // the labels at the top of an environment.
518 // More or less for bibliography
520 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
521 layout->labeltype == LABEL_BIBLIO ||
522 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
523 LyXFont font = getLabelFont();
524 if (!par_.getLabelstring().empty()) {
525 string const str = par_.getLabelstring();
526 double spacing_val = 1.0;
527 if (!parparams.spacing().isDefault())
528 spacing_val = parparams.spacing().getValue();
530 spacing_val = buffer.params().spacing().getValue();
532 int const labeladdon = int(font_metrics::maxHeight(font) * layout->spacing.getValue() * spacing_val);
535 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
536 + (layout->labelbottomsep * defaultRowHeight()));
539 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
542 x += (width_ - text_.rightMargin(par_) - leftMargin()) / 2;
543 x -= font_metrics::width(str, font) / 2;
545 x = width_ - leftMargin() -
546 font_metrics::width(str, font);
548 pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
554 void RowPainter::paintLast()
556 bool const is_rtl = text_.isRTL(par_);
557 int const endlabel = getEndLabel(pit_, text_.paragraphs());
562 case END_LABEL_FILLED_BOX: {
563 LyXFont const font = getLabelFont();
564 int const size = int(0.75 * font_metrics::maxAscent(font));
565 int const y = yo_ - size;
566 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
568 if (width_ - int(row_.width()) <= size)
569 x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
571 if (endlabel == END_LABEL_BOX)
572 pain_.rectangle(x, y, size, size, LColor::eolmarker);
574 pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
578 case END_LABEL_STATIC: {
579 LyXFont font = getLabelFont();
580 string const & str = par_.layout()->endlabelstring();
581 double const x = is_rtl ?
582 x_ - font_metrics::width(str, font)
583 : - text_.rightMargin(par_) - row_.width();
584 pain_.text(int(x), yo_, str, font);
588 case END_LABEL_NO_LABEL:
594 void RowPainter::paintText()
596 pos_type const end = row_.endpos();
597 pos_type body_pos = par_.beginOfBody();
599 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
603 LyXLayout_ptr const & layout = par_.layout();
605 bool running_strikeout = false;
606 bool is_struckout = false;
607 int last_strikeout_x = 0;
609 // Use font span to speed things up, see below
613 for (pos_type vpos = row_.pos(); vpos < end; ) {
614 if (x_ > bv_.workWidth())
617 pos_type const pos = text_.bidi.vis2log(vpos);
619 if (pos >= par_.size()) {
624 // Use font span to speed things up, see above
625 if (vpos < font_span.first || vpos > font_span.last) {
626 font_span = par_.fontSpan(vpos);
627 font = text_.getFont(par_, vpos);
630 const int width_pos =
631 text_.singleWidth(par_, pos, par_.getChar(pos), font);
633 if (x_ + width_pos < 0) {
639 is_struckout = isDeletedText(par_, pos);
641 if (is_struckout && !running_strikeout) {
642 running_strikeout = true;
643 last_strikeout_x = int(x_);
646 bool const highly_editable_inset = par_.isInset(pos)
647 && isHighlyEditableInset(par_.getInset(pos));
649 // If we reach the end of a struck out range, paint it.
650 // We also don't paint across things like tables
651 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
652 // Calculate 1/3 height of the buffer's default font
654 yo_ - font_metrics::maxAscent(text_.defaultfont_) / 3;
655 pain_.line(last_strikeout_x, middle, int(x_), middle,
656 LColor::strikeout, Painter::line_solid, Painter::line_thin);
657 running_strikeout = false;
660 if (body_pos > 0 && pos == body_pos - 1) {
661 int const lwidth = font_metrics::width(layout->labelsep,
664 x_ += label_hfill_ + lwidth - width_pos;
667 if (par_.isHfill(pos)) {
671 int const y1 = y0 - defaultRowHeight() / 2;
673 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
675 if (hfillExpansion(par_, row_, pos)) {
676 int const y2 = (y0 + y1) / 2;
678 if (pos >= body_pos) {
679 pain_.line(int(x_), y2, int(x_ + hfill_), y2,
681 Painter::line_onoffdash);
684 pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
686 Painter::line_onoffdash);
689 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
693 } else if (par_.isSeparator(pos)) {
703 // if we reach the end of a struck out range, paint it
704 if (running_strikeout) {
705 // calculate 1/3 height of the buffer's default font
707 yo_ - font_metrics::maxAscent(text_.defaultfont_) / 3;
708 pain_.line(last_strikeout_x, middle, int(x_), middle,
709 LColor::strikeout, Painter::line_solid, Painter::line_thin);
710 running_strikeout = false;
716 (PainterInfo & pi, LyXText const & text, pit_type pit, int x, int y)
718 // lyxerr << " paintPar: pit: " << pit << " at y: " << y << endl;
719 static NullPainter nop;
720 static PainterInfo nullpi(pi.base.bv, nop);
721 int const ww = pi.base.bv->workHeight();
723 Paragraph const & par = text.paragraphs()[pit];
725 RowList::const_iterator const rb = par.rows().begin();
726 RowList::const_iterator const re = par.rows().end();
727 theCoords.parPos()[&text][pit] = Point(x, y);
730 for (RowList::const_iterator rit = rb; rit != re; ++rit) {
732 bool const inside = (y + rit->descent() >= 0
733 && y - rit->ascent() < ww);
734 RowPainter rp(inside ? pi : nullpi, text, pit, *rit, x, y);
751 void paintText(BufferView const & bv, ViewMetricsInfo const & vi)
753 Painter & pain = bv.painter();
754 LyXText * const text = bv.text();
757 pain.fillRectangle(0, vi.y1, bv.workWidth(), vi.y2 - vi.y1,
761 PainterInfo pi(const_cast<BufferView *>(&bv), pain);
763 text->drawSelection(pi, 0, 0);
767 for (pit_type pit = vi.p1; pit <= vi.p2; ++pit) {
768 yy += text->getPar(pit).ascent();
769 paintPar(pi, *bv.text(), pit, 0, yy);
770 yy += text->getPar(pit).descent();
773 // Cache one paragraph above and one below
774 // Note MV: this cannot be suppressed even for singlepar.
775 // Try viewing the User Guide Mobius figure
778 text->redoParagraph(vi.p1 - 1);
779 theCoords.parPos()[bv.text()][vi.p1 - 1] =
780 Point(0, vi.y1 - text->getPar(vi.p1 - 1).descent());
783 if (vi.p2 < lyx::pit_type(text->paragraphs().size()) - 1) {
784 text->redoParagraph(vi.p2 + 1);
785 theCoords.parPos()[bv.text()][vi.p2 + 1] =
786 Point(0, vi.y2 + text->getPar(vi.p2 + 1).ascent());
789 // and grey out above (should not happen later)
790 // lyxerr << "par ascent: " << text->getPar(vi.p1).ascent() << endl;
791 if (vi.y1 > 0 && !vi.singlepar)
792 pain.fillRectangle(0, 0, bv.workWidth(), vi.y1, LColor::bottomarea);
794 // and possibly grey out below
795 // lyxerr << "par descent: " << text->getPar(vi.p1).ascent() << endl;
796 if (vi.y2 < bv.workHeight() && !vi.singlepar)
797 pain.fillRectangle(0, vi.y2, bv.workWidth(), bv.workHeight() - vi.y2, LColor::bottomarea);
801 void paintTextInset(LyXText const & text, PainterInfo & pi, int x, int y)
803 // lyxerr << " paintTextInset: y: " << y << endl;
805 y -= text.getPar(0).ascent();
806 for (int pit = 0; pit < int(text.paragraphs().size()); ++pit) {
807 y += text.getPar(pit).ascent();
808 paintPar(pi, text, pit, x, y);
809 y += text.getPar(pit).descent();