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 RowMetrics m = text_metrics_.computeRowMetrics(pit_, row_);
67 bidi_.computeTables(par_, bv_.buffer(), row_);
70 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
73 separator_ = m.separator;
75 label_hfill_ = m.label_hfill;
77 BOOST_ASSERT(pit >= 0);
78 BOOST_ASSERT(pit < int(text.paragraphs().size()));
82 Font const RowPainter::getLabelFont() const
84 return text_.getLabelFont(bv_.buffer(), par_);
88 int RowPainter::leftMargin() const
90 return text_.leftMargin(bv_.buffer(), text_metrics_.width(), pit_,
95 // If you want to debug inset metrics uncomment the following line:
96 // #define DEBUG_METRICS
97 // This draws green lines around each inset.
100 void RowPainter::paintInset(pos_type const pos, Font const & font)
102 Inset const * inset = par_.getInset(pos);
104 PainterInfo pi(const_cast<BufferView *>(&bv_), pain_);
105 // FIXME: We should always use font, see documentation of
106 // noFontChange() in Inset.h.
107 pi.base.font = inset->noFontChange() ?
108 bv_.buffer().params().getFont() :
110 pi.ltr_pos = (bidi_.level(pos) % 2 == 0);
111 pi.erased_ = erased_ || par_.isDeleted(pos);
113 int const x1 = int(x_);
115 bv_.coordCache().insets().add(inset, int(x_), yo_);
116 // insets are painted completely. Recursive
117 inset->drawSelection(pi, int(x_), yo_);
118 inset->draw(pi, int(x_), yo_);
119 x_ += inset->width();
122 BOOST_ASSERT(max_witdh_ > 0);
123 int right_margin = text_metrics_.rightMargin(pm_);
124 int const w = max_witdh_ - leftMargin() - right_margin;
125 MetricsInfo mi(&bv_, font, w);
126 inset->metrics(mi, dim);
127 if (inset->width() > dim.wid)
128 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
129 << " draw width " << inset->width()
130 << "> metrics width " << dim.wid << "." << std::endl;
131 if (inset->ascent() > dim.asc)
132 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
133 << " draw ascent " << inset->ascent()
134 << "> metrics ascent " << dim.asc << "." << std::endl;
135 if (inset->descent() > dim.des)
136 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
137 << " draw ascent " << inset->descent()
138 << "> metrics descent " << dim.des << "." << std::endl;
139 BOOST_ASSERT(inset->width() <= dim.wid);
140 BOOST_ASSERT(inset->ascent() <= dim.asc);
141 BOOST_ASSERT(inset->descent() <= dim.des);
142 int const x2 = x1 + dim.wid;
143 int const y1 = yo_ + dim.des;
144 int const y2 = yo_ - dim.asc;
145 pi.pain.line(x1, y1, x1, y2, Color::green);
146 pi.pain.line(x1, y1, x2, y1, Color::green);
147 pi.pain.line(x2, y1, x2, y2, Color::green);
148 pi.pain.line(x1, y2, x2, y2, Color::green);
153 void RowPainter::paintHebrewComposeChar(pos_type & vpos, Font const & font)
155 pos_type pos = bidi_.vis2log(vpos);
160 char_type c = par_.getChar(pos);
164 int const width = theFontMetrics(font).width(c);
167 for (pos_type i = pos - 1; i >= 0; --i) {
169 if (!Encodings::isComposeChar_hebrew(c)) {
170 if (isPrintableNonspace(c)) {
171 int const width2 = pm_.singleWidth(i,
172 text_.getFont(bv_.buffer(), par_, i));
173 dx = (c == 0x05e8 || // resh
174 c == 0x05d3) // dalet
176 : (width2 - width) / 2;
183 pain_.text(int(x_) + dx, yo_, str, font);
187 void RowPainter::paintArabicComposeChar(pos_type & vpos, Font const & font)
189 pos_type pos = bidi_.vis2log(vpos);
193 char_type c = par_.getChar(pos);
194 c = par_.transformChar(c, pos);
198 int const width = theFontMetrics(font).width(c);
201 for (pos_type i = pos - 1; i >= 0; --i) {
203 if (!Encodings::isComposeChar_arabic(c)) {
204 if (isPrintableNonspace(c)) {
205 int const width2 = pm_.singleWidth(i,
206 text_.getFont(bv_.buffer(), par_, i));
207 dx = (width2 - width) / 2;
213 pain_.text(int(x_) + dx, yo_, str, font);
217 void RowPainter::paintChars(pos_type & vpos, Font const & font,
218 bool hebrew, bool arabic)
220 // This method takes up 70% of time when typing
221 pos_type pos = bidi_.vis2log(vpos);
222 pos_type const end = row_.endpos();
223 FontSpan const font_span = par_.fontSpan(pos);
224 Change::Type const prev_change = par_.lookupChange(pos).type;
227 std::vector<char_type> str;
229 str.push_back(par_.getChar(pos));
232 char_type c = str[0];
237 str[0] = par_.transformChar(c, pos);
240 // collect as much similar chars as we can
241 for (++vpos ; vpos < end ; ++vpos) {
242 pos = bidi_.vis2log(vpos);
243 if (pos < font_span.first || pos > font_span.last)
246 if (prev_change != par_.lookupChange(pos).type)
249 char_type c = par_.getChar(pos);
251 if (!isPrintableNonspace(c))
254 /* Because we do our own bidi, at this point the strings are
255 * already in visual order. However, Qt also applies its own
256 * bidi algorithm to strings that it paints to the screen.
257 * Therefore, if we were to paint Hebrew/Arabic words as a
258 * single string, the letters in the words would get reversed
259 * again. In order to avoid that, we don't collect Hebrew/
260 * Arabic characters, but rather paint them one at a time.
261 * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
266 /* FIXME: these checks are irrelevant, since 'arabic' and
267 * 'hebrew' alone are already going to trigger a break.
268 * However, this should not be removed completely, because
269 * if an alternative solution is found which allows grouping
270 * of arabic and hebrew characters, then these breaks may have
273 if (arabic && Encodings::isComposeChar_arabic(c))
276 if (hebrew && Encodings::isComposeChar_hebrew(c))
285 c = par_.transformChar(c, pos);
286 /* see comment in hebrew, explaining why we break */
293 docstring s(&str[0], str.size());
295 if (prev_change != Change::UNCHANGED) {
297 if (prev_change == Change::DELETED) {
298 copy.setColor(Color::deletedtext);
299 } else if (prev_change == Change::INSERTED) {
300 copy.setColor(Color::addedtext);
302 x_ += pain_.text(int(x_), yo_, s, copy);
304 x_ += pain_.text(int(x_), yo_, s, font);
309 void RowPainter::paintForeignMark(double orig_x, Font const & font, int desc)
311 if (!lyxrc.mark_foreign_language)
313 if (font.language() == latex_language)
315 if (font.language() == bv_.buffer().params().language)
318 int const y = yo_ + 1 + desc;
319 pain_.line(int(orig_x), y, int(x_), y, Color::language);
323 void RowPainter::paintFromPos(pos_type & vpos)
325 pos_type const pos = bidi_.vis2log(vpos);
326 Font orig_font = text_.getFont(bv_.buffer(), par_, pos);
328 double const orig_x = x_;
330 if (par_.isInset(pos)) {
331 // If outer row has changed, nested insets are repaint completely.
332 paintInset(pos, orig_font);
334 paintForeignMark(orig_x, orig_font,
335 par_.getInset(pos)->descent());
339 // usual characters, no insets
340 char_type const c = par_.getChar(pos);
342 // special case languages
343 std::string const & lang = orig_font.language()->lang();
344 bool const hebrew = lang == "hebrew";
345 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" ||
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 end = row_.endpos();
368 if (par_.size() == end) {
369 // this is the last row of the paragraph;
370 // thus, we must also consider the imaginary end-of-par character
374 if (start == end || !par_.isChanged(start, end))
377 int const height = text_.isLastRow(pit_, row_)
381 pain_.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color::changebar);
385 void RowPainter::paintAppendix()
387 // only draw the appendix frame once (for the main text)
388 if (!par_.params().appendix() || !text_.isMainText(bv_.buffer()))
391 int y = yo_ - row_.ascent();
393 if (par_.params().startOfAppendix())
394 y += 2 * defaultRowHeight();
396 pain_.line(1, y, 1, yo_ + row_.height(), Color::appendix);
397 pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color::appendix);
401 void RowPainter::paintDepthBar()
403 depth_type const depth = par_.getDepth();
408 depth_type prev_depth = 0;
409 if (!text_.isFirstRow(pit_, row_)) {
410 pit_type pit2 = pit_;
413 prev_depth = pars_[pit2].getDepth();
416 depth_type next_depth = 0;
417 if (!text_.isLastRow(pit_, row_)) {
418 pit_type pit2 = pit_;
419 if (row_.endpos() >= pars_[pit2].size())
421 next_depth = pars_[pit2].getDepth();
424 for (depth_type i = 1; i <= depth; ++i) {
425 int const w = nestMargin() / 5;
426 int x = int(xo_) + w * i;
427 // only consider the changebar space if we're drawing outermost text
428 if (text_.isMainText(bv_.buffer()))
429 x += changebarMargin();
431 int const starty = yo_ - row_.ascent();
432 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
434 pain_.line(x, starty, x, starty + h, Color::depthbar);
437 pain_.fillRectangle(x, starty, w, 2, Color::depthbar);
439 pain_.fillRectangle(x, starty + h, w, 2, Color::depthbar);
444 int RowPainter::paintAppendixStart(int y)
447 pb_font.setColor(Color::appendix);
454 docstring const label = _("Appendix");
455 theFontMetrics(pb_font).rectText(label, w, a, d);
457 int const text_start = int(xo_ + (width_ - w) / 2);
458 int const text_end = text_start + w;
460 pain_.rectText(text_start, y + d, label, pb_font, Color::none, Color::none);
462 pain_.line(int(xo_ + 1), y, text_start, y, Color::appendix);
463 pain_.line(text_end, y, int(xo_ + width_ - 2), y, Color::appendix);
465 return 3 * defaultRowHeight();
469 void RowPainter::paintFirst()
471 ParagraphParameters const & parparams = par_.params();
475 // start of appendix?
476 if (parparams.startOfAppendix())
477 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
479 Buffer const & buffer = bv_.buffer();
481 LayoutPtr const & layout = par_.layout();
483 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
485 if (layout->latextype == LATEX_PARAGRAPH
486 && !par_.getDepth()) {
487 y_top += buffer.params().getDefSkip().inPixels(bv_);
489 LayoutPtr const & playout = pars_[pit_ - 1].layout();
490 if (playout->latextype == LATEX_PARAGRAPH
491 && !pars_[pit_ - 1].getDepth()) {
492 // is it right to use defskip here, too? (AS)
493 y_top += buffer.params().getDefSkip().inPixels(bv_);
499 bool const is_rtl = text_.isRTL(buffer, par_);
500 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
501 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
503 // should we print a label?
504 if (layout->labeltype >= LABEL_STATIC
505 && (layout->labeltype != LABEL_STATIC
506 || layout->latextype != LATEX_ENVIRONMENT
509 Font const font = getLabelFont();
510 FontMetrics const & fm = theFontMetrics(font);
512 docstring const str = par_.getLabelstring();
516 // this is special code for the chapter layout. This is
517 // printed in an extra row and has a pagebreak at
519 if (layout->counter == "chapter") {
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 int const labeladdon = int(fm.maxHeight() * layout->spacing.getValue() * spacing_val);
529 int const maxdesc = int(fm.maxDescent() * layout->spacing.getValue() * spacing_val)
530 + int(layout->parsep) * defaultRowHeight();
533 x = width_ - leftMargin() -
537 pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
540 x = width_ - leftMargin()
541 + fm.width(layout->labelsep);
543 x = x_ - fm.width(layout->labelsep)
547 pain_.text(int(x), yo_, str, font);
551 // the labels at the top of an environment.
552 // More or less for bibliography
554 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
555 layout->labeltype == LABEL_BIBLIO ||
556 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
557 Font font = getLabelFont();
558 if (!par_.getLabelstring().empty()) {
559 docstring const str = par_.getLabelstring();
560 double spacing_val = 1.0;
561 if (!parparams.spacing().isDefault())
562 spacing_val = parparams.spacing().getValue();
564 spacing_val = buffer.params().spacing().getValue();
566 FontMetrics const & fm = theFontMetrics(font);
568 int const labeladdon = int(fm.maxHeight()
569 * layout->spacing.getValue() * spacing_val);
572 int(fm.maxDescent() * layout->spacing.getValue() * spacing_val
573 + (layout->labelbottomsep * defaultRowHeight()));
576 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
579 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
580 x -= fm.width(str) / 2;
582 x = width_ - leftMargin() - fm.width(str);
584 pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
590 void RowPainter::paintLast()
592 bool const is_rtl = text_.isRTL(bv_.buffer(), par_);
593 int const endlabel = getEndLabel(pit_, text_.paragraphs());
595 // paint imaginary end-of-paragraph character
597 if (par_.isInserted(par_.size()) || par_.isDeleted(par_.size())) {
598 FontMetrics const & fm = theFontMetrics(bv_.buffer().params().getFont());
599 int const length = fm.maxAscent() / 2;
600 Color::color col = par_.isInserted(par_.size()) ? Color::addedtext : Color::deletedtext;
602 pain_.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
603 Painter::line_solid, Painter::line_thick);
604 pain_.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1, yo_ + 2, col,
605 Painter::line_solid, Painter::line_thick);
612 case END_LABEL_FILLED_BOX: {
613 Font const font = getLabelFont();
614 FontMetrics const & fm = theFontMetrics(font);
615 int const size = int(0.75 * fm.maxAscent());
616 int const y = yo_ - size;
617 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
619 if (width_ - int(row_.width()) <= size)
620 x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
622 if (endlabel == END_LABEL_BOX)
623 pain_.rectangle(x, y, size, size, Color::eolmarker);
625 pain_.fillRectangle(x, y, size, size, Color::eolmarker);
629 case END_LABEL_STATIC: {
630 Font font = getLabelFont();
631 FontMetrics const & fm = theFontMetrics(font);
632 docstring const & str = par_.layout()->endlabelstring();
633 double const x = is_rtl ?
635 : - text_metrics_.rightMargin(pm_) - row_.width();
636 pain_.text(int(x), yo_, str, font);
640 case END_LABEL_NO_LABEL:
646 void RowPainter::paintText()
648 pos_type const end = row_.endpos();
649 // Spaces at logical line breaks in bidi text must be skipped during
650 // painting. However, they may appear visually in the middle
651 // of a row; they must be skipped, wherever they are...
652 // * logically "abc_[HEBREW_\nHEBREW]"
653 // * visually "abc_[_WERBEH\nWERBEH]"
654 pos_type skipped_sep_vpos = -1;
655 pos_type body_pos = par_.beginOfBody();
657 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
661 LayoutPtr const & layout = par_.layout();
663 bool running_strikeout = false;
664 bool is_struckout = false;
665 int last_strikeout_x = 0;
667 // Use font span to speed things up, see below
670 Buffer const & buffer = bv_.buffer();
672 // If the last logical character is a separator, don't paint it, unless
673 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
674 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
675 skipped_sep_vpos = bidi_.log2vis(end - 1);
677 for (pos_type vpos = row_.pos(); vpos < end; ) {
678 if (x_ > bv_.workWidth())
681 // Skip the separator at the logical end of the row
682 if (vpos == skipped_sep_vpos) {
687 pos_type const pos = bidi_.vis2log(vpos);
689 if (pos >= par_.size()) {
694 // Use font span to speed things up, see above
695 if (vpos < font_span.first || vpos > font_span.last) {
696 font_span = par_.fontSpan(vpos);
697 font = text_.getFont(buffer, par_, vpos);
700 const int width_pos = pm_.singleWidth(pos, font);
702 if (x_ + width_pos < 0) {
708 is_struckout = par_.isDeleted(pos);
710 if (is_struckout && !running_strikeout) {
711 running_strikeout = true;
712 last_strikeout_x = int(x_);
715 bool const highly_editable_inset = par_.isInset(pos)
716 && isHighlyEditableInset(par_.getInset(pos));
718 // If we reach the end of a struck out range, paint it.
719 // We also don't paint across things like tables
720 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
721 // Calculate 1/3 height of the buffer's default font
722 FontMetrics const & fm
723 = theFontMetrics(bv_.buffer().params().getFont());
724 int const middle = yo_ - fm.maxAscent() / 3;
725 pain_.line(last_strikeout_x, middle, int(x_), middle,
726 Color::deletedtext, Painter::line_solid, Painter::line_thin);
727 running_strikeout = false;
730 if (body_pos > 0 && pos == body_pos - 1) {
731 int const lwidth = theFontMetrics(getLabelFont())
732 .width(layout->labelsep);
734 x_ += label_hfill_ + lwidth - width_pos;
737 if (par_.isHfill(pos)) {
741 int const y1 = y0 - defaultRowHeight() / 2;
743 pain_.line(int(x_), y1, int(x_), y0, Color::added_space);
745 if (par_.hfillExpansion(row_, pos)) {
746 int const y2 = (y0 + y1) / 2;
748 if (pos >= body_pos) {
749 pain_.line(int(x_), y2, int(x_ + hfill_), y2,
751 Painter::line_onoffdash);
754 pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
756 Painter::line_onoffdash);
759 pain_.line(int(x_), y1, int(x_), y0, Color::added_space);
763 } else if (par_.isSeparator(pos)) {
764 Font orig_font = text_.getFont(bv_.buffer(), par_, pos);
765 double const orig_x = x_;
770 paintForeignMark(orig_x, orig_font);
776 // if we reach the end of a struck out range, paint it
777 if (running_strikeout) {
778 // calculate 1/3 height of the buffer's default font
779 FontMetrics const & fm
780 = theFontMetrics(bv_.buffer().params().getFont());
781 int const middle = yo_ - fm.maxAscent() / 3;
782 pain_.line(last_strikeout_x, middle, int(x_), middle,
783 Color::deletedtext, Painter::line_solid, Painter::line_thin);
784 running_strikeout = false;