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"
18 #include "CoordCache.h"
21 #include "BufferParams.h"
22 #include "BufferView.h"
29 #include "MetricsInfo.h"
30 #include "Paragraph.h"
31 #include "ParagraphMetrics.h"
32 #include "paragraph_funcs.h"
33 #include "ParagraphParameters.h"
34 #include "TextMetrics.h"
37 #include "frontends/FontMetrics.h"
38 #include "frontends/Painter.h"
40 #include "insets/InsetText.h"
42 #include "support/textutils.h"
44 #include <boost/crc.hpp>
53 using frontend::Painter;
54 using frontend::FontMetrics;
56 RowPainter::RowPainter(PainterInfo & pi,
57 Text const & text, pit_type pit, Row const & row, Bidi & bidi, int x, int y)
58 : bv_(*pi.base.bv), pain_(pi.pain), text_(text),
59 text_metrics_(pi.base.bv->textMetrics(&text)),
60 pars_(text.paragraphs()),
61 row_(row), pit_(pit), par_(text.paragraphs()[pit]),
62 pm_(text_metrics_.parMetrics(pit)),
63 bidi_(bidi), erased_(pi.erased_),
64 xo_(x), yo_(y), width_(text_metrics_.width())
66 bidi_.computeTables(par_, bv_.buffer(), row_);
69 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
72 BOOST_ASSERT(pit >= 0);
73 BOOST_ASSERT(pit < int(text.paragraphs().size()));
77 Font const RowPainter::getLabelFont() const
79 return text_.getLabelFont(bv_.buffer(), par_);
83 int RowPainter::leftMargin() const
85 return text_.leftMargin(bv_.buffer(), text_metrics_.width(), pit_,
90 // If you want to debug inset metrics uncomment the following line:
91 // #define DEBUG_METRICS
92 // This draws green lines around each inset.
95 void RowPainter::paintInset(pos_type const pos, Font const & font)
97 Inset const * inset = par_.getInset(pos);
99 PainterInfo pi(const_cast<BufferView *>(&bv_), pain_);
100 // FIXME: We should always use font, see documentation of
101 // noFontChange() in Inset.h.
102 pi.base.font = inset->noFontChange() ?
103 bv_.buffer().params().getFont() :
105 pi.ltr_pos = (bidi_.level(pos) % 2 == 0);
106 pi.erased_ = erased_ || par_.isDeleted(pos);
108 int const x1 = int(x_);
110 bv_.coordCache().insets().add(inset, int(x_), yo_);
111 // insets are painted completely. Recursive
112 inset->drawSelection(pi, int(x_), yo_);
113 inset->draw(pi, int(x_), yo_);
114 x_ += inset->width();
117 BOOST_ASSERT(max_witdh_ > 0);
118 int right_margin = text_metrics_.rightMargin(pm_);
119 int const w = max_witdh_ - leftMargin() - right_margin;
120 MetricsInfo mi(&bv_, font, w);
121 inset->metrics(mi, dim);
122 if (inset->width() > dim.wid)
123 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
124 << " draw width " << inset->width()
125 << "> metrics width " << dim.wid << "." << std::endl;
126 if (inset->ascent() > dim.asc)
127 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
128 << " draw ascent " << inset->ascent()
129 << "> metrics ascent " << dim.asc << "." << std::endl;
130 if (inset->descent() > dim.des)
131 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
132 << " draw ascent " << inset->descent()
133 << "> metrics descent " << dim.des << "." << std::endl;
134 BOOST_ASSERT(inset->width() <= dim.wid);
135 BOOST_ASSERT(inset->ascent() <= dim.asc);
136 BOOST_ASSERT(inset->descent() <= dim.des);
137 int const x2 = x1 + dim.wid;
138 int const y1 = yo_ + dim.des;
139 int const y2 = yo_ - dim.asc;
140 pi.pain.line(x1, y1, x1, y2, Color::green);
141 pi.pain.line(x1, y1, x2, y1, Color::green);
142 pi.pain.line(x2, y1, x2, y2, Color::green);
143 pi.pain.line(x1, y2, x2, y2, Color::green);
148 void RowPainter::paintHebrewComposeChar(pos_type & vpos, Font const & font)
150 pos_type pos = bidi_.vis2log(vpos);
155 char_type c = par_.getChar(pos);
159 int const width = theFontMetrics(font).width(c);
162 for (pos_type i = pos - 1; i >= 0; --i) {
164 if (!Encodings::isComposeChar_hebrew(c)) {
165 if (isPrintableNonspace(c)) {
166 int const width2 = pm_.singleWidth(i,
167 text_.getFont(bv_.buffer(), par_, i));
168 dx = (c == 0x05e8 || // resh
169 c == 0x05d3) // dalet
171 : (width2 - width) / 2;
178 pain_.text(int(x_) + dx, yo_, str, font);
182 void RowPainter::paintArabicComposeChar(pos_type & vpos, Font const & font)
184 pos_type pos = bidi_.vis2log(vpos);
188 char_type c = par_.getChar(pos);
189 c = par_.transformChar(c, pos);
193 int const width = theFontMetrics(font).width(c);
196 for (pos_type i = pos - 1; i >= 0; --i) {
198 if (!Encodings::isComposeChar_arabic(c)) {
199 if (isPrintableNonspace(c)) {
200 int const width2 = pm_.singleWidth(i,
201 text_.getFont(bv_.buffer(), par_, i));
202 dx = (width2 - width) / 2;
208 pain_.text(int(x_) + dx, yo_, str, font);
212 void RowPainter::paintChars(pos_type & vpos, Font const & font,
213 bool hebrew, bool arabic)
215 // This method takes up 70% of time when typing
216 pos_type pos = bidi_.vis2log(vpos);
217 pos_type const end = row_.endpos();
218 FontSpan const font_span = par_.fontSpan(pos);
219 Change::Type const prev_change = par_.lookupChange(pos).type;
222 std::vector<char_type> str;
224 str.push_back(par_.getChar(pos));
227 char_type c = str[0];
232 str[0] = par_.transformChar(c, pos);
235 // collect as much similar chars as we can
236 for (++vpos ; vpos < end ; ++vpos) {
237 pos = bidi_.vis2log(vpos);
238 if (pos < font_span.first || pos > font_span.last)
241 if (prev_change != par_.lookupChange(pos).type)
244 char_type c = par_.getChar(pos);
246 if (!isPrintableNonspace(c))
249 /* Because we do our own bidi, at this point the strings are
250 * already in visual order. However, Qt also applies its own
251 * bidi algorithm to strings that it paints to the screen.
252 * Therefore, if we were to paint Hebrew/Arabic words as a
253 * single string, the letters in the words would get reversed
254 * again. In order to avoid that, we don't collect Hebrew/
255 * Arabic characters, but rather paint them one at a time.
256 * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
261 /* FIXME: these checks are irrelevant, since 'arabic' and
262 * 'hebrew' alone are already going to trigger a break.
263 * However, this should not be removed completely, because
264 * if an alternative solution is found which allows grouping
265 * of arabic and hebrew characters, then these breaks may have
268 if (arabic && Encodings::isComposeChar_arabic(c))
271 if (hebrew && Encodings::isComposeChar_hebrew(c))
280 c = par_.transformChar(c, pos);
281 /* see comment in hebrew, explaining why we break */
288 docstring s(&str[0], str.size());
290 if (prev_change != Change::UNCHANGED) {
292 if (prev_change == Change::DELETED) {
293 copy.setColor(Color::deletedtext);
294 } else if (prev_change == Change::INSERTED) {
295 copy.setColor(Color::addedtext);
297 x_ += pain_.text(int(x_), yo_, s, copy);
299 x_ += pain_.text(int(x_), yo_, s, font);
304 void RowPainter::paintForeignMark(double orig_x, Font const & font, int desc)
306 if (!lyxrc.mark_foreign_language)
308 if (font.language() == latex_language)
310 if (font.language() == bv_.buffer().params().language)
313 int const y = yo_ + 1 + desc;
314 pain_.line(int(orig_x), y, int(x_), y, Color::language);
318 void RowPainter::paintFromPos(pos_type & vpos)
320 pos_type const pos = bidi_.vis2log(vpos);
321 Font orig_font = text_.getFont(bv_.buffer(), par_, pos);
323 double const orig_x = x_;
325 if (par_.isInset(pos)) {
326 // If outer row has changed, nested insets are repaint completely.
327 paintInset(pos, orig_font);
329 paintForeignMark(orig_x, orig_font,
330 par_.getInset(pos)->descent());
334 // usual characters, no insets
335 char_type const c = par_.getChar(pos);
337 // special case languages
338 std::string const & lang = orig_font.language()->lang();
339 bool const hebrew = lang == "hebrew";
340 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" ||
343 // draw as many chars as we can
344 if ((!hebrew && !arabic)
345 || (hebrew && !Encodings::isComposeChar_hebrew(c))
346 || (arabic && !Encodings::isComposeChar_arabic(c))) {
347 paintChars(vpos, orig_font, hebrew, arabic);
349 paintHebrewComposeChar(vpos, orig_font);
351 paintArabicComposeChar(vpos, orig_font);
354 paintForeignMark(orig_x, orig_font);
358 void RowPainter::paintChangeBar()
360 pos_type const start = row_.pos();
361 pos_type end = row_.endpos();
363 if (par_.size() == end) {
364 // this is the last row of the paragraph;
365 // thus, we must also consider the imaginary end-of-par character
369 if (start == end || !par_.isChanged(start, end))
372 int const height = text_.isLastRow(pit_, row_)
376 pain_.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color::changebar);
380 void RowPainter::paintAppendix()
382 // only draw the appendix frame once (for the main text)
383 if (!par_.params().appendix() || !text_.isMainText(bv_.buffer()))
386 int y = yo_ - row_.ascent();
388 if (par_.params().startOfAppendix())
389 y += 2 * defaultRowHeight();
391 pain_.line(1, y, 1, yo_ + row_.height(), Color::appendix);
392 pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color::appendix);
396 void RowPainter::paintDepthBar()
398 depth_type const depth = par_.getDepth();
403 depth_type prev_depth = 0;
404 if (!text_.isFirstRow(pit_, row_)) {
405 pit_type pit2 = pit_;
408 prev_depth = pars_[pit2].getDepth();
411 depth_type next_depth = 0;
412 if (!text_.isLastRow(pit_, row_)) {
413 pit_type pit2 = pit_;
414 if (row_.endpos() >= pars_[pit2].size())
416 next_depth = pars_[pit2].getDepth();
419 for (depth_type i = 1; i <= depth; ++i) {
420 int const w = nestMargin() / 5;
421 int x = int(xo_) + w * i;
422 // only consider the changebar space if we're drawing outermost text
423 if (text_.isMainText(bv_.buffer()))
424 x += changebarMargin();
426 int const starty = yo_ - row_.ascent();
427 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
429 pain_.line(x, starty, x, starty + h, Color::depthbar);
432 pain_.fillRectangle(x, starty, w, 2, Color::depthbar);
434 pain_.fillRectangle(x, starty + h, w, 2, Color::depthbar);
439 int RowPainter::paintAppendixStart(int y)
442 pb_font.setColor(Color::appendix);
449 docstring const label = _("Appendix");
450 theFontMetrics(pb_font).rectText(label, 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, Color::none, Color::none);
457 pain_.line(int(xo_ + 1), y, text_start, y, Color::appendix);
458 pain_.line(text_end, y, int(xo_ + width_ - 2), y, Color::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_ - row_.ascent() + 2 * defaultRowHeight());
474 Buffer const & buffer = bv_.buffer();
476 LayoutPtr 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 LayoutPtr 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(buffer, 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 Font const font = getLabelFont();
505 FontMetrics const & fm = theFontMetrics(font);
507 docstring const str = par_.getLabelstring();
511 // this is special code for the chapter layout. This is
512 // printed in an extra row and has a pagebreak at
514 if (layout->counter == "chapter") {
515 double spacing_val = 1.0;
516 if (!parparams.spacing().isDefault()) {
517 spacing_val = parparams.spacing().getValue();
519 spacing_val = buffer.params().spacing().getValue();
522 int const labeladdon = int(fm.maxHeight() * layout->spacing.getValue() * spacing_val);
524 int const maxdesc = int(fm.maxDescent() * layout->spacing.getValue() * spacing_val)
525 + int(layout->parsep) * defaultRowHeight();
528 x = width_ - leftMargin() -
532 pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
535 x = width_ - leftMargin()
536 + fm.width(layout->labelsep);
538 x = x_ - fm.width(layout->labelsep)
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 Font font = getLabelFont();
553 if (!par_.getLabelstring().empty()) {
554 docstring 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();
561 FontMetrics const & fm = theFontMetrics(font);
563 int const labeladdon = int(fm.maxHeight()
564 * layout->spacing.getValue() * spacing_val);
567 int(fm.maxDescent() * layout->spacing.getValue() * spacing_val
568 + (layout->labelbottomsep * defaultRowHeight()));
571 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
574 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
575 x -= fm.width(str) / 2;
577 x = width_ - leftMargin() - fm.width(str);
579 pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
585 void RowPainter::paintLast()
587 bool const is_rtl = text_.isRTL(bv_.buffer(), par_);
588 int const endlabel = getEndLabel(pit_, text_.paragraphs());
590 // paint imaginary end-of-paragraph character
592 if (par_.isInserted(par_.size()) || par_.isDeleted(par_.size())) {
593 FontMetrics const & fm = theFontMetrics(bv_.buffer().params().getFont());
594 int const length = fm.maxAscent() / 2;
595 Color::color col = par_.isInserted(par_.size()) ? Color::addedtext : Color::deletedtext;
597 pain_.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
598 Painter::line_solid, Painter::line_thick);
599 pain_.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1, yo_ + 2, col,
600 Painter::line_solid, Painter::line_thick);
607 case END_LABEL_FILLED_BOX: {
608 Font const font = getLabelFont();
609 FontMetrics const & fm = theFontMetrics(font);
610 int const size = int(0.75 * fm.maxAscent());
611 int const y = yo_ - size;
612 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
614 if (width_ - int(row_.width()) <= size)
615 x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
617 if (endlabel == END_LABEL_BOX)
618 pain_.rectangle(x, y, size, size, Color::eolmarker);
620 pain_.fillRectangle(x, y, size, size, Color::eolmarker);
624 case END_LABEL_STATIC: {
625 Font font = getLabelFont();
626 FontMetrics const & fm = theFontMetrics(font);
627 docstring const & str = par_.layout()->endlabelstring();
628 double const x = is_rtl ?
630 : - text_metrics_.rightMargin(pm_) - row_.width();
631 pain_.text(int(x), yo_, str, font);
635 case END_LABEL_NO_LABEL:
641 void RowPainter::paintText()
643 pos_type const end = row_.endpos();
644 // Spaces at logical line breaks in bidi text must be skipped during
645 // painting. However, they may appear visually in the middle
646 // of a row; they must be skipped, wherever they are...
647 // * logically "abc_[HEBREW_\nHEBREW]"
648 // * visually "abc_[_WERBEH\nWERBEH]"
649 pos_type skipped_sep_vpos = -1;
650 pos_type body_pos = par_.beginOfBody();
652 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
656 LayoutPtr const & layout = par_.layout();
658 bool running_strikeout = false;
659 bool is_struckout = false;
660 int last_strikeout_x = 0;
662 // Use font span to speed things up, see below
665 Buffer const & buffer = bv_.buffer();
667 // If the last logical character is a separator, don't paint it, unless
668 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
669 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
670 skipped_sep_vpos = bidi_.log2vis(end - 1);
672 for (pos_type vpos = row_.pos(); vpos < end; ) {
673 if (x_ > bv_.workWidth())
676 // Skip the separator at the logical end of the row
677 if (vpos == skipped_sep_vpos) {
682 pos_type const pos = bidi_.vis2log(vpos);
684 if (pos >= par_.size()) {
689 // Use font span to speed things up, see above
690 if (vpos < font_span.first || vpos > font_span.last) {
691 font_span = par_.fontSpan(vpos);
692 font = text_.getFont(buffer, par_, vpos);
695 const int width_pos = pm_.singleWidth(pos, font);
697 if (x_ + width_pos < 0) {
703 is_struckout = par_.isDeleted(pos);
705 if (is_struckout && !running_strikeout) {
706 running_strikeout = true;
707 last_strikeout_x = int(x_);
710 bool const highly_editable_inset = par_.isInset(pos)
711 && isHighlyEditableInset(par_.getInset(pos));
713 // If we reach the end of a struck out range, paint it.
714 // We also don't paint across things like tables
715 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
716 // Calculate 1/3 height of the buffer's default font
717 FontMetrics const & fm
718 = theFontMetrics(bv_.buffer().params().getFont());
719 int const middle = yo_ - fm.maxAscent() / 3;
720 pain_.line(last_strikeout_x, middle, int(x_), middle,
721 Color::deletedtext, Painter::line_solid, Painter::line_thin);
722 running_strikeout = false;
725 if (body_pos > 0 && pos == body_pos - 1) {
726 int const lwidth = theFontMetrics(getLabelFont())
727 .width(layout->labelsep);
729 x_ += row_.label_hfill + lwidth - width_pos;
732 if (par_.isHfill(pos)) {
736 int const y1 = y0 - defaultRowHeight() / 2;
738 pain_.line(int(x_), y1, int(x_), y0, Color::added_space);
740 if (par_.hfillExpansion(row_, pos)) {
741 int const y2 = (y0 + y1) / 2;
743 if (pos >= body_pos) {
744 pain_.line(int(x_), y2, int(x_ + row_.hfill), y2,
746 Painter::line_onoffdash);
749 pain_.line(int(x_), y2, int(x_ + row_.label_hfill), y2,
751 Painter::line_onoffdash);
752 x_ += row_.label_hfill;
754 pain_.line(int(x_), y1, int(x_), y0, Color::added_space);
758 } else if (par_.isSeparator(pos)) {
759 Font orig_font = text_.getFont(bv_.buffer(), par_, pos);
760 double const orig_x = x_;
763 x_ += row_.separator;
765 paintForeignMark(orig_x, orig_font);
771 // if we reach the end of a struck out range, paint it
772 if (running_strikeout) {
773 // calculate 1/3 height of the buffer's default font
774 FontMetrics const & fm
775 = theFontMetrics(bv_.buffer().params().getFont());
776 int const middle = yo_ - fm.maxAscent() / 3;
777 pain_.line(last_strikeout_x, middle, int(x_), middle,
778 Color::deletedtext, Painter::line_solid, Painter::line_thin);
779 running_strikeout = false;