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
119 // Hack to get 1.4cvs working
124 RowPainter::RowPainter(PainterInfo & pi,
125 LyXText const & text, pit_type pit, Row const & row, int x, int y)
126 : bv_(*pi.base.bv), pain_(pi.pain), text_(text), pars_(text.paragraphs()),
127 row_(row), pit_(pit), par_(text.paragraphs()[pit]),
128 xo_(x), yo_(y), width_(text_.width()), font_(pi.base.font)
130 RowMetrics m = text_.computeRowMetrics(pit, row_);
133 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
136 separator_ = m.separator;
138 label_hfill_ = m.label_hfill;
140 BOOST_ASSERT(pit >= 0);
141 BOOST_ASSERT(pit < int(text.paragraphs().size()));
146 LyXFont const RowPainter::getFont(pos_type pos) const
149 LyXFont pf(text_.getFont(par_, pos));
150 lf.reduce(LyXFont(LyXFont::ALL_SANE));
152 lf.setLanguage(pf.language());
157 int RowPainter::singleWidth(lyx::pos_type pos) const
159 return text_.singleWidth(par_, pos);
163 int RowPainter::singleWidth(lyx::pos_type pos, char c) const
165 LyXFont const & font = text_.getFont(par_, pos);
166 return text_.singleWidth(par_, pos, c, font);
170 LyXFont const RowPainter::getLabelFont() const
172 return text_.getLabelFont(par_);
176 int RowPainter::leftMargin() const
178 return text_.leftMargin(pit_, row_.pos());
182 void RowPainter::paintInset(pos_type const pos)
184 InsetBase const * inset = par_.getInset(pos);
186 PainterInfo pi(const_cast<BufferView *>(&bv_), pain_);
187 pi.base.font = getFont(pos);
188 pi.ltr_pos = (text_.bidi.level(pos) % 2 == 0);
189 theCoords.insets().add(inset, int(x_), yo_);
190 inset->drawSelection(pi, int(x_), yo_);
191 inset->draw(pi, int(x_), yo_);
192 x_ += inset->width();
196 void RowPainter::paintHebrewComposeChar(pos_type & vpos)
198 pos_type pos = text_.bidi.vis2log(vpos);
203 char c = par_.getChar(pos);
207 LyXFont const & font = getFont(pos);
208 int const width = font_metrics::width(c, font);
211 for (pos_type i = pos - 1; i >= 0; --i) {
213 if (!Encodings::IsComposeChar_hebrew(c)) {
214 if (IsPrintableNonspace(c)) {
215 int const width2 = singleWidth(i, c);
217 dx = (c == 'ø' || c == 'ã')
219 : (width2 - width) / 2;
226 pain_.text(int(x_) + dx, yo_, str, font);
230 void RowPainter::paintArabicComposeChar(pos_type & vpos)
232 pos_type pos = text_.bidi.vis2log(vpos);
236 char c = par_.getChar(pos);
237 c = par_.transformChar(c, pos);
241 LyXFont const & font = getFont(pos);
242 int const width = font_metrics::width(c, font);
245 for (pos_type i = pos - 1; i >= 0; --i) {
247 if (!Encodings::IsComposeChar_arabic(c)) {
248 if (IsPrintableNonspace(c)) {
249 int const width2 = singleWidth(i, c);
250 dx = (width2 - width) / 2;
256 pain_.text(int(x_) + dx, yo_, str, font);
260 void RowPainter::paintChars(pos_type & vpos, bool hebrew, bool arabic)
262 pos_type pos = text_.bidi.vis2log(vpos);
263 pos_type const end = row_.endpos();
264 LyXFont orig_font = getFont(pos);
268 str += par_.getChar(pos);
270 unsigned char c = str[0];
271 str[0] = par_.transformChar(c, pos);
274 bool prev_struckout = isDeletedText(par_, pos);
275 bool prev_newtext = isInsertedText(par_, pos);
277 // collect as much similar chars as we can
278 for (++vpos; vpos < end && (pos = text_.bidi.vis2log(vpos)) >= 0; ++vpos) {
279 char c = par_.getChar(pos);
281 if (!IsPrintableNonspace(c))
284 if (prev_struckout != isDeletedText(par_, pos))
287 if (prev_newtext != isInsertedText(par_, pos))
290 if (arabic && Encodings::IsComposeChar_arabic(c))
293 if (hebrew && Encodings::IsComposeChar_hebrew(c))
296 if (orig_font != getFont(pos))
300 c = par_.transformChar(c, pos);
306 orig_font.setColor(LColor::strikeout);
307 else if (prev_newtext)
308 orig_font.setColor(LColor::newtext);
310 // Draw text and set the new x position
311 //lyxerr << "paint row: yo_ " << yo_ << "\n";
312 pain_.text(int(x_), yo_, str, orig_font);
313 x_ += font_metrics::width(str, orig_font);
317 void RowPainter::paintForeignMark(double orig_x, LyXFont const & orig_font)
319 if (!lyxrc.mark_foreign_language)
321 if (orig_font.language() == latex_language)
323 if (orig_font.language() == bv_.buffer()->params().language)
326 int const y = yo_ + 1;
327 pain_.line(int(orig_x), y, int(x_), y, LColor::language);
331 void RowPainter::paintFromPos(pos_type & vpos)
333 pos_type const pos = text_.bidi.vis2log(vpos);
335 LyXFont const & orig_font = getFont(pos);
337 double const orig_x = x_;
339 char const c = par_.getChar(pos);
341 if (c == Paragraph::META_INSET) {
344 paintForeignMark(orig_x, orig_font);
348 // usual characters, no insets
350 // special case languages
351 bool const hebrew = (orig_font.language()->lang() == "hebrew");
353 orig_font.language()->lang() == "arabic" &&
354 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
355 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
357 // draw as many chars as we can
358 if ((!hebrew && !arabic)
359 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
360 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
361 paintChars(vpos, hebrew, arabic);
363 paintHebrewComposeChar(vpos);
365 paintArabicComposeChar(vpos);
368 paintForeignMark(orig_x, orig_font);
372 void RowPainter::paintChangeBar()
374 pos_type const start = row_.pos();
375 pos_type const end = row_.endpos();
377 if (start == end || !par_.isChanged(start, end - 1))
380 int const height = text_.isLastRow(pit_, row_)
384 pain_.fillRectangle(4, yo_ - row_.ascent(), 5, height, LColor::changebar);
388 void RowPainter::paintAppendix()
390 if (!par_.params().appendix())
393 int y = yo_ - row_.ascent();
395 if (par_.params().startOfAppendix())
396 y += 2 * defaultRowHeight();
398 pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
399 pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), LColor::appendix);
403 void RowPainter::paintDepthBar()
405 Paragraph::depth_type const depth = par_.getDepth();
410 Paragraph::depth_type prev_depth = 0;
411 if (!text_.isFirstRow(pit_, row_)) {
412 pit_type pit2 = pit_;
415 prev_depth = pars_[pit2].getDepth();
418 Paragraph::depth_type next_depth = 0;
419 if (!text_.isLastRow(pit_, row_)) {
420 pit_type pit2 = pit_;
421 if (row_.endpos() >= pars_[pit2].size())
423 next_depth = pars_[pit2].getDepth();
426 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
427 int const w = nestMargin() / 5;
428 int x = int(xo_) + w * i;
429 // only consider the changebar space if we're drawing outermost text
430 if (text_.isMainText())
431 x += changebarMargin();
433 int const starty = yo_ - row_.ascent();
434 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
436 pain_.line(x, starty, x, starty + h, LColor::depthbar);
439 pain_.fillRectangle(x, starty, w, 2, LColor::depthbar);
441 pain_.fillRectangle(x, starty + h, w, 2, LColor::depthbar);
446 int RowPainter::paintAppendixStart(int y)
449 pb_font.setColor(LColor::appendix);
452 string const label = _("Appendix");
456 font_metrics::rectText(label, pb_font, w, a, d);
458 int const text_start = int(xo_ + (width_ - w) / 2);
459 int const text_end = text_start + w;
461 pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
463 pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
464 pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
466 return 3 * defaultRowHeight();
470 void RowPainter::paintFirst()
472 ParagraphParameters const & parparams = par_.params();
476 // start of appendix?
477 if (parparams.startOfAppendix())
478 y_top += paintAppendixStart(yo_ + y_top + 2 * defaultRowHeight());
480 Buffer const & buffer = *bv_.buffer();
482 LyXLayout_ptr const & layout = par_.layout();
484 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
486 if (layout->latextype == LATEX_PARAGRAPH
487 && !par_.getDepth()) {
488 y_top += buffer.params().getDefSkip().inPixels(bv_);
490 LyXLayout_ptr const & playout = pars_[pit_ - 1].layout();
491 if (playout->latextype == LATEX_PARAGRAPH
492 && !pars_[pit_ - 1].getDepth()) {
493 // is it right to use defskip here, too? (AS)
494 y_top += buffer.params().getDefSkip().inPixels(bv_);
500 bool const is_rtl = text_.isRTL(par_);
501 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
502 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
504 // should we print a label?
505 if (layout->labeltype >= LABEL_STATIC
506 && (layout->labeltype != LABEL_STATIC
507 || layout->latextype != LATEX_ENVIRONMENT
510 LyXFont font = getLabelFont();
511 if (!par_.getLabelstring().empty()) {
513 string const str = par_.getLabelstring();
515 // this is special code for the chapter layout. This is
516 // printed in an extra row and has a pagebreak at
518 if (layout->counter == "chapter") {
519 if (buffer.params().secnumdepth >= 0) {
520 double spacing_val = 1.0;
521 if (!parparams.spacing().isDefault()) {
522 spacing_val = parparams.spacing().getValue();
524 spacing_val = buffer.params().spacing().getValue();
527 #warning Look is this correct?
529 int const labeladdon = int(font_metrics::maxHeight(font) * layout->spacing.getValue() * spacing_val);
531 int const maxdesc = int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
532 + int(layout->parsep) * defaultRowHeight();
535 x = width_ - leftMargin() -
536 font_metrics::width(str, font);
539 pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
543 x = width_ - leftMargin()
544 + font_metrics::width(layout->labelsep, font);
546 x = x_ - font_metrics::width(layout->labelsep, font)
547 - font_metrics::width(str, font);
550 pain_.text(int(x), yo_, str, font);
554 // the labels at the top of an environment.
555 // More or less for bibliography
557 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
558 layout->labeltype == LABEL_BIBLIO ||
559 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
560 LyXFont font = getLabelFont();
561 if (!par_.getLabelstring().empty()) {
562 string const str = par_.getLabelstring();
563 double spacing_val = 1.0;
564 if (!parparams.spacing().isDefault()) {
565 spacing_val = parparams.spacing().getValue();
567 spacing_val = buffer.params().spacing().getValue();
571 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
572 + (layout->labelbottomsep * defaultRowHeight()));
575 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
576 x = ((is_rtl ? leftMargin() : x_)
577 + width_ - text_.rightMargin(par_)) / 2;
578 x -= font_metrics::width(str, font) / 2;
580 x = width_ - leftMargin() -
581 font_metrics::width(str, font);
583 pain_.text(int(x), yo_ - maxdesc, str, font);
589 void RowPainter::paintLast()
591 bool const is_rtl = text_.isRTL(par_);
592 int const endlabel = getEndLabel(pit_, text_.paragraphs());
597 case END_LABEL_FILLED_BOX: {
598 LyXFont const font = getLabelFont();
599 int const size = int(0.75 * font_metrics::maxAscent(font));
600 int const y = yo_ - size;
601 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
603 if (width_ - int(row_.width()) <= size)
604 x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
606 if (endlabel == END_LABEL_BOX)
607 pain_.rectangle(x, y, size, size, LColor::eolmarker);
609 pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
613 case END_LABEL_STATIC: {
614 LyXFont font = getLabelFont();
615 string const & str = par_.layout()->endlabelstring();
616 double const x = is_rtl ?
617 x_ - font_metrics::width(str, font)
618 : - text_.rightMargin(par_) - row_.width();
619 pain_.text(int(x), yo_, str, font);
623 case END_LABEL_NO_LABEL:
629 void RowPainter::paintText()
631 pos_type const end = row_.endpos();
632 pos_type body_pos = par_.beginOfBody();
634 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
638 LyXLayout_ptr const & layout = par_.layout();
640 bool running_strikeout = false;
641 bool is_struckout = false;
642 int last_strikeout_x = 0;
644 for (pos_type vpos = row_.pos(); vpos < end; ) {
645 if (x_ > bv_.workWidth())
648 pos_type const pos = text_.bidi.vis2log(vpos);
650 if (pos >= par_.size()) {
655 const int width_pos = singleWidth(pos);
656 if (x_ + width_pos < 0) {
662 is_struckout = isDeletedText(par_, pos);
664 if (is_struckout && !running_strikeout) {
665 running_strikeout = true;
666 last_strikeout_x = int(x_);
669 bool const highly_editable_inset = par_.isInset(pos)
670 && isHighlyEditableInset(par_.getInset(pos));
672 // if we reach the end of a struck out range, paint it
673 // we also don't paint across things like tables
674 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
675 // calculate 1/3 height of the buffer's default font
677 yo_ - font_metrics::maxAscent(text_.defaultfont_) / 3;
678 pain_.line(last_strikeout_x, middle, int(x_), middle,
679 LColor::strikeout, Painter::line_solid, Painter::line_thin);
680 running_strikeout = false;
683 if (body_pos > 0 && pos == body_pos - 1) {
684 int const lwidth = font_metrics::width(layout->labelsep,
687 x_ += label_hfill_ + lwidth - width_pos;
690 if (par_.isHfill(pos)) {
694 int const y1 = y0 - defaultRowHeight() / 2;
696 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
698 if (hfillExpansion(par_, row_, pos)) {
699 int const y2 = (y0 + y1) / 2;
701 if (pos >= body_pos) {
702 pain_.line(int(x_), y2, int(x_ + hfill_), y2,
704 Painter::line_onoffdash);
707 pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
709 Painter::line_onoffdash);
712 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
716 } else if (par_.isSeparator(pos)) {
726 // if we reach the end of a struck out range, paint it
727 if (running_strikeout) {
728 // calculate 1/3 height of the buffer's default font
730 yo_ - font_metrics::maxAscent(text_.defaultfont_) / 3;
731 pain_.line(last_strikeout_x, middle, int(x_), middle,
732 LColor::strikeout, Painter::line_solid, Painter::line_thin);
733 running_strikeout = false;
739 (PainterInfo & pi, LyXText const & text, pit_type pit, int x, int y)
741 // lyxerr << " paintPar: pit: " << pit << " at y: " << y << endl;
742 static NullPainter nop;
743 static PainterInfo nullpi(pi.base.bv, nop);
744 int const ww = pi.base.bv->workHeight();
746 Paragraph & par = text.paragraphs()[pit];
748 RowList::const_iterator const rb = par.rows().begin();
749 RowList::const_iterator const re = par.rows().end();
750 theCoords.parPos()[&text][pit] = Point(x, y);
753 for (RowList::const_iterator rit = rb; rit != re; ++rit) {
755 bool const inside = (y + rit->descent() >= 0
756 && y - rit->ascent() < ww);
757 RowPainter rp(inside ? pi : nullpi, text, pit, *rit, x, y);
774 void paintText(BufferView const & bv, ViewMetricsInfo const & vi)
776 Painter & pain = bv.painter();
777 LyXText * const text = bv.text();
780 pain.fillRectangle(0, vi.y1, bv.workWidth(), vi.y2 - vi.y1,
784 PainterInfo pi(const_cast<BufferView *>(&bv), pain);
786 text->drawSelection(pi, 0, 0);
790 for (pit_type pit = vi.p1; pit <= vi.p2; ++pit) {
791 yy += text->getPar(pit).ascent();
792 paintPar(pi, *bv.text(), pit, 0, yy);
793 yy += text->getPar(pit).descent();
797 // paint one paragraph above and one below
799 text->redoParagraph(vi.p1 - 1);
800 paintPar(pi, *bv.text(), vi.p1 - 1, 0,
801 vi.y1 - text->getPar(vi.p1 - 1).descent());
804 if (vi.p2 < lyx::pit_type(text->paragraphs().size()) - 1) {
805 text->redoParagraph(vi.p2 + 1);
806 paintPar(pi, *bv.text(), vi.p2 + 1, 0,
807 vi.y2 + text->getPar(vi.p2 + 1).ascent());
810 // and grey out above (should not happen later)
811 // lyxerr << "par ascent: " << text->getPar(vi.p1).ascent() << endl;
813 pain.fillRectangle(0, 0, bv.workWidth(), vi.y1, LColor::bottomarea);
815 // and possibly grey out below
816 // lyxerr << "par descent: " << text->getPar(vi.p1).ascent() << endl;
817 if (vi.y2 < bv.workHeight())
818 pain.fillRectangle(0, vi.y2, bv.workWidth(), bv.workHeight() - vi.y2, LColor::bottomarea);
822 void paintTextInset(LyXText const & text, PainterInfo & pi, int x, int y)
824 // lyxerr << " paintTextInset: y: " << y << endl;
826 y -= text.getPar(0).ascent();
827 for (int pit = 0; pit < int(text.paragraphs().size()); ++pit) {
828 y += text.getPar(pit).ascent();
829 paintPar(pi, text, pit, x, y);
830 y += text.getPar(pit).descent();