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"
30 #include "MetricsInfo.h"
31 #include "Paragraph.h"
32 #include "ParagraphMetrics.h"
33 #include "paragraph_funcs.h"
34 #include "ParagraphParameters.h"
35 #include "TextMetrics.h"
38 #include "frontends/FontMetrics.h"
39 #include "frontends/Painter.h"
41 #include "insets/InsetText.h"
43 #include "support/textutils.h"
45 #include <boost/crc.hpp>
54 using frontend::Painter;
55 using frontend::FontMetrics;
57 RowPainter::RowPainter(PainterInfo & pi,
58 Text const & text, pit_type pit, Row const & row, Bidi & bidi, int x, int y)
59 : pi_(pi), text_(text),
60 text_metrics_(pi_.base.bv->textMetrics(&text)),
61 pars_(text.paragraphs()),
62 row_(row), pit_(pit), par_(text.paragraphs()[pit]),
63 pm_(text_metrics_.parMetrics(pit)),
64 bidi_(bidi), erased_(pi_.erased_),
65 xo_(x), yo_(y), width_(text_metrics_.width())
67 bidi_.computeTables(par_, pi_.base.bv->buffer(), row_);
70 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
73 BOOST_ASSERT(pit >= 0);
74 BOOST_ASSERT(pit < int(text.paragraphs().size()));
78 FontInfo const RowPainter::getLabelFont() const
80 return text_.getLabelFont(pi_.base.bv->buffer(), par_);
84 int RowPainter::leftMargin() const
86 return text_metrics_.leftMargin(text_metrics_.width(), pit_,
91 void RowPainter::paintHfill(pos_type const pos, pos_type const body_pos)
96 int const y1 = y0 - defaultRowHeight() / 2;
98 pi_.pain.line(int(x_), y1, int(x_), y0, Color_added_space);
100 if (pm_.hfillExpansion(row_, pos)) {
101 int const y2 = (y0 + y1) / 2;
103 if (pos >= body_pos) {
104 pi_.pain.line(int(x_), y2, int(x_ + row_.hfill), y2,
106 Painter::line_onoffdash);
109 pi_.pain.line(int(x_), y2, int(x_ + row_.label_hfill), y2,
111 Painter::line_onoffdash);
112 x_ += row_.label_hfill;
114 pi_.pain.line(int(x_), y1, int(x_), y0, Color_added_space);
120 // If you want to debug inset metrics uncomment the following line:
121 //#define DEBUG_METRICS
122 // This draws green lines around each inset.
125 void RowPainter::paintInset(Inset const * inset, pos_type const pos)
127 Font const font = text_metrics_.getDisplayFont(pit_, pos);
130 // FIXME: We should always use font, see documentation of
131 // noFontChange() in Inset.h.
132 pi_.base.font = inset->noFontChange() ?
133 pi_.base.bv->buffer().params().getFont().fontInfo() :
135 pi_.ltr_pos = (bidi_.level(pos) % 2 == 0);
136 pi_.erased_ = erased_ || par_.isDeleted(pos);
137 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
138 // insets are painted completely. Recursive
139 inset->drawSelection(pi_, int(x_), yo_);
140 inset->draw(pi_, int(x_), yo_);
142 Dimension const & dim = pm_.insetDimension(inset);
144 paintForeignMark(x_, font.language(), dim.descent());
149 int const x1 = int(x_ - dim.width());
151 BOOST_ASSERT(max_witdh_ > 0);
152 int right_margin = text_metrics_.rightMargin(pm_);
153 int const w = max_witdh_ - leftMargin() - right_margin;
154 MetricsInfo mi(pi_.base.bv, font.fontInfo(), w);
155 inset->metrics(mi, dim2);
156 if (dim.wid != dim2.wid)
157 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
158 << " draw width " << dim.width()
159 << "> metrics width " << dim2.wid << "." << std::endl;
160 if (dim->asc != dim2.asc)
161 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
162 << " draw ascent " << dim.ascent()
163 << "> metrics ascent " << dim2.asc << "." << std::endl;
164 if (dim2.descent() != dim.des)
165 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
166 << " draw ascent " << dim.descent()
167 << "> metrics descent " << dim2.des << "." << std::endl;
168 BOOST_ASSERT(dim2.wid == dim.wid);
169 BOOST_ASSERT(dim2.asc == dim.asc);
170 BOOST_ASSERT(dim2.des == dim.des);
171 int const x2 = x1 + dim.wid;
172 int const y1 = yo_ + dim.des;
173 int const y2 = yo_ - dim.asc;
174 pi_.pain.line(x1, y1, x1, y2, Color_green);
175 pi_.pain.line(x1, y1, x2, y1, Color_green);
176 pi_.pain.line(x2, y1, x2, y2, Color_green);
177 pi_.pain.line(x1, y2, x2, y2, Color_green);
182 void RowPainter::paintHebrewComposeChar(pos_type & vpos, FontInfo const & font)
184 pos_type pos = bidi_.vis2log(vpos);
189 char_type c = par_.getChar(pos);
193 int const width = theFontMetrics(font).width(c);
196 for (pos_type i = pos - 1; i >= 0; --i) {
198 if (!Encodings::isComposeChar_hebrew(c)) {
199 if (isPrintableNonspace(c)) {
200 int const width2 = pm_.singleWidth(i,
201 text_metrics_.getDisplayFont(pit_, i));
202 dx = (c == 0x05e8 || // resh
203 c == 0x05d3) // dalet
205 : (width2 - width) / 2;
212 pi_.pain.text(int(x_) + dx, yo_, str, font);
216 void RowPainter::paintArabicComposeChar(pos_type & vpos, FontInfo const & font)
218 pos_type pos = bidi_.vis2log(vpos);
222 char_type c = par_.getChar(pos);
223 c = par_.transformChar(c, pos);
227 int const width = theFontMetrics(font).width(c);
230 for (pos_type i = pos - 1; i >= 0; --i) {
232 if (!Encodings::isComposeChar_arabic(c)) {
233 if (isPrintableNonspace(c)) {
234 int const width2 = pm_.singleWidth(i,
235 text_metrics_.getDisplayFont(pit_, i));
236 dx = (width2 - width) / 2;
242 pi_.pain.text(int(x_) + dx, yo_, str, font);
246 void RowPainter::paintChars(pos_type & vpos, FontInfo const & font,
247 bool hebrew, bool arabic)
249 // This method takes up 70% of time when typing
250 pos_type pos = bidi_.vis2log(vpos);
251 pos_type const end = row_.endpos();
252 FontSpan const font_span = par_.fontSpan(pos);
253 Change::Type const prev_change = par_.lookupChange(pos).type;
256 std::vector<char_type> str;
258 str.push_back(par_.getChar(pos));
261 char_type c = str[0];
266 str[0] = par_.transformChar(c, pos);
269 // collect as much similar chars as we can
270 for (++vpos ; vpos < end ; ++vpos) {
271 pos = bidi_.vis2log(vpos);
272 if (pos < font_span.first || pos > font_span.last)
275 if (prev_change != par_.lookupChange(pos).type)
278 char_type c = par_.getChar(pos);
280 if (!isPrintableNonspace(c))
283 /* Because we do our own bidi, at this point the strings are
284 * already in visual order. However, Qt also applies its own
285 * bidi algorithm to strings that it paints to the screen.
286 * Therefore, if we were to paint Hebrew/Arabic words as a
287 * single string, the letters in the words would get reversed
288 * again. In order to avoid that, we don't collect Hebrew/
289 * Arabic characters, but rather paint them one at a time.
290 * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
295 /* FIXME: these checks are irrelevant, since 'arabic' and
296 * 'hebrew' alone are already going to trigger a break.
297 * However, this should not be removed completely, because
298 * if an alternative solution is found which allows grouping
299 * of arabic and hebrew characters, then these breaks may have
302 if (arabic && Encodings::isComposeChar_arabic(c))
305 if (hebrew && Encodings::isComposeChar_hebrew(c))
314 c = par_.transformChar(c, pos);
315 /* see comment in hebrew, explaining why we break */
322 docstring s(&str[0], str.size());
324 if (prev_change != Change::UNCHANGED) {
325 FontInfo copy = font;
326 if (prev_change == Change::DELETED) {
327 copy.setColor(Color_deletedtext);
328 } else if (prev_change == Change::INSERTED) {
329 copy.setColor(Color_addedtext);
331 x_ += pi_.pain.text(int(x_), yo_, s, copy);
333 x_ += pi_.pain.text(int(x_), yo_, s, font);
338 void RowPainter::paintForeignMark(double orig_x, Language const * lang,
341 if (!lyxrc.mark_foreign_language)
343 if (lang == latex_language)
345 if (lang == pi_.base.bv->buffer().params().language)
348 int const y = yo_ + 1 + desc;
349 pi_.pain.line(int(orig_x), y, int(x_), y, Color_language);
353 void RowPainter::paintFromPos(pos_type & vpos)
355 pos_type const pos = bidi_.vis2log(vpos);
356 Font const orig_font = text_metrics_.getDisplayFont(pit_, pos);
357 double const orig_x = x_;
359 // usual characters, no insets
360 char_type const c = par_.getChar(pos);
362 // special case languages
363 std::string const & lang = orig_font.language()->lang();
364 bool const hebrew = lang == "hebrew";
365 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" ||
368 // draw as many chars as we can
369 if ((!hebrew && !arabic)
370 || (hebrew && !Encodings::isComposeChar_hebrew(c))
371 || (arabic && !Encodings::isComposeChar_arabic(c))) {
372 paintChars(vpos, orig_font.fontInfo(), hebrew, arabic);
374 paintHebrewComposeChar(vpos, orig_font.fontInfo());
376 paintArabicComposeChar(vpos, orig_font.fontInfo());
379 paintForeignMark(orig_x, orig_font.language());
383 void RowPainter::paintChangeBar()
385 pos_type const start = row_.pos();
386 pos_type end = row_.endpos();
388 if (par_.size() == end) {
389 // this is the last row of the paragraph;
390 // thus, we must also consider the imaginary end-of-par character
394 if (start == end || !par_.isChanged(start, end))
397 int const height = text_metrics_.isLastRow(pit_, row_)
401 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
405 void RowPainter::paintAppendix()
407 // only draw the appendix frame once (for the main text)
408 if (!par_.params().appendix() || !text_.isMainText(pi_.base.bv->buffer()))
411 int y = yo_ - row_.ascent();
413 if (par_.params().startOfAppendix())
414 y += 2 * defaultRowHeight();
416 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
417 pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
421 void RowPainter::paintDepthBar()
423 depth_type const depth = par_.getDepth();
428 depth_type prev_depth = 0;
429 if (!text_metrics_.isFirstRow(pit_, row_)) {
430 pit_type pit2 = pit_;
433 prev_depth = pars_[pit2].getDepth();
436 depth_type next_depth = 0;
437 if (!text_metrics_.isLastRow(pit_, row_)) {
438 pit_type pit2 = pit_;
439 if (row_.endpos() >= pars_[pit2].size())
441 next_depth = pars_[pit2].getDepth();
444 for (depth_type i = 1; i <= depth; ++i) {
445 int const w = nestMargin() / 5;
446 int x = int(xo_) + w * i;
447 // only consider the changebar space if we're drawing outermost text
448 if (text_.isMainText(pi_.base.bv->buffer()))
449 x += changebarMargin();
451 int const starty = yo_ - row_.ascent();
452 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
454 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
457 pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
459 pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
464 int RowPainter::paintAppendixStart(int y)
466 FontInfo pb_font = sane_font;
467 pb_font.setColor(Color_appendix);
474 docstring const label = _("Appendix");
475 theFontMetrics(pb_font).rectText(label, w, a, d);
477 int const text_start = int(xo_ + (width_ - w) / 2);
478 int const text_end = text_start + w;
480 pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
482 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
483 pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
485 return 3 * defaultRowHeight();
489 void RowPainter::paintFirst()
491 ParagraphParameters const & parparams = par_.params();
495 // start of appendix?
496 if (parparams.startOfAppendix())
497 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
499 Buffer const & buffer = pi_.base.bv->buffer();
501 LayoutPtr const & layout = par_.layout();
503 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
505 if (layout->latextype == LATEX_PARAGRAPH
506 && !par_.getDepth()) {
507 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
509 LayoutPtr const & playout = pars_[pit_ - 1].layout();
510 if (playout->latextype == LATEX_PARAGRAPH
511 && !pars_[pit_ - 1].getDepth()) {
512 // is it right to use defskip here, too? (AS)
513 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
519 bool const is_rtl = text_.isRTL(buffer, par_);
520 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
521 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
523 // should we print a label?
524 if (layout->labeltype >= LABEL_STATIC
525 && (layout->labeltype != LABEL_STATIC
526 || layout->latextype != LATEX_ENVIRONMENT
529 FontInfo const font = getLabelFont();
530 FontMetrics const & fm = theFontMetrics(font);
532 docstring const str = par_.getLabelstring();
536 // this is special code for the chapter layout. This is
537 // printed in an extra row and has a pagebreak at
539 if (layout->counter == "chapter") {
540 double spacing_val = 1.0;
541 if (!parparams.spacing().isDefault()) {
542 spacing_val = parparams.spacing().getValue();
544 spacing_val = buffer.params().spacing().getValue();
547 int const labeladdon = int(fm.maxHeight() * layout->spacing.getValue() * spacing_val);
549 int const maxdesc = int(fm.maxDescent() * layout->spacing.getValue() * spacing_val)
550 + int(layout->parsep) * defaultRowHeight();
553 x = width_ - leftMargin() -
557 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
560 x = width_ - leftMargin()
561 + fm.width(layout->labelsep);
563 x = x_ - fm.width(layout->labelsep)
567 pi_.pain.text(int(x), yo_, str, font);
571 // the labels at the top of an environment.
572 // More or less for bibliography
574 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
575 layout->labeltype == LABEL_BIBLIO ||
576 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
577 FontInfo const font = getLabelFont();
578 if (!par_.getLabelstring().empty()) {
579 docstring const str = par_.getLabelstring();
580 double spacing_val = 1.0;
581 if (!parparams.spacing().isDefault())
582 spacing_val = parparams.spacing().getValue();
584 spacing_val = buffer.params().spacing().getValue();
586 FontMetrics const & fm = theFontMetrics(font);
588 int const labeladdon = int(fm.maxHeight()
589 * layout->spacing.getValue() * spacing_val);
592 int(fm.maxDescent() * layout->spacing.getValue() * spacing_val
593 + (layout->labelbottomsep * defaultRowHeight()));
596 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
599 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
600 x -= fm.width(str) / 2;
602 x = width_ - leftMargin() - fm.width(str);
604 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
610 void RowPainter::paintLast()
612 bool const is_rtl = text_.isRTL(pi_.base.bv->buffer(), par_);
613 int const endlabel = getEndLabel(pit_, text_.paragraphs());
615 // paint imaginary end-of-paragraph character
617 if (par_.isInserted(par_.size()) || par_.isDeleted(par_.size())) {
618 FontMetrics const & fm = theFontMetrics(pi_.base.bv->buffer().params().getFont());
619 int const length = fm.maxAscent() / 2;
620 ColorCode col = par_.isInserted(par_.size()) ? Color_addedtext : Color_deletedtext;
622 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
623 Painter::line_solid, Painter::line_thick);
624 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1, yo_ + 2, col,
625 Painter::line_solid, Painter::line_thick);
632 case END_LABEL_FILLED_BOX: {
633 FontInfo const font = getLabelFont();
634 FontMetrics const & fm = theFontMetrics(font);
635 int const size = int(0.75 * fm.maxAscent());
636 int const y = yo_ - size;
637 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
639 if (width_ - int(row_.width()) <= size)
640 x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
642 if (endlabel == END_LABEL_BOX)
643 pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
645 pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
649 case END_LABEL_STATIC: {
650 FontInfo const font = getLabelFont();
651 FontMetrics const & fm = theFontMetrics(font);
652 docstring const & str = par_.layout()->endlabelstring();
653 double const x = is_rtl ?
655 : - text_metrics_.rightMargin(pm_) - row_.width();
656 pi_.pain.text(int(x), yo_, str, font);
660 case END_LABEL_NO_LABEL:
666 void RowPainter::paintOnlyInsets()
668 pos_type const end = row_.endpos();
669 for (pos_type pos = row_.pos(); pos != end; ++pos) {
670 if (!par_.isInset(pos))
673 // If outer row has changed, nested insets are repaint completely.
674 Inset const * inset = par_.getInset(pos);
676 if (x_ > pi_.base.bv->workWidth())
679 x_ = pi_.base.bv->coordCache().getInsets().x(inset);
680 paintInset(inset, pos);
685 void RowPainter::paintText()
687 pos_type const end = row_.endpos();
688 // Spaces at logical line breaks in bidi text must be skipped during
689 // painting. However, they may appear visually in the middle
690 // of a row; they must be skipped, wherever they are...
691 // * logically "abc_[HEBREW_\nHEBREW]"
692 // * visually "abc_[_WERBEH\nWERBEH]"
693 pos_type skipped_sep_vpos = -1;
694 pos_type body_pos = par_.beginOfBody();
696 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
700 LayoutPtr const & layout = par_.layout();
702 bool running_strikeout = false;
703 bool is_struckout = false;
704 int last_strikeout_x = 0;
706 // Use font span to speed things up, see below
710 // If the last logical character is a separator, don't paint it, unless
711 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
712 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
713 skipped_sep_vpos = bidi_.log2vis(end - 1);
715 for (pos_type vpos = row_.pos(); vpos < end; ) {
716 if (x_ > pi_.base.bv->workWidth())
719 // Skip the separator at the logical end of the row
720 if (vpos == skipped_sep_vpos) {
725 pos_type const pos = bidi_.vis2log(vpos);
727 if (pos >= par_.size()) {
732 // Use font span to speed things up, see above
733 if (vpos < font_span.first || vpos > font_span.last) {
734 font_span = par_.fontSpan(vpos);
735 font = text_metrics_.getDisplayFont(pit_, vpos);
738 const int width_pos = pm_.singleWidth(pos, font);
740 if (x_ + width_pos < 0) {
746 is_struckout = par_.isDeleted(pos);
748 if (is_struckout && !running_strikeout) {
749 running_strikeout = true;
750 last_strikeout_x = int(x_);
753 bool const highly_editable_inset = par_.isInset(pos)
754 && par_.getInset(pos)->editable() == Inset::HIGHLY_EDITABLE;
756 // If we reach the end of a struck out range, paint it.
757 // We also don't paint across things like tables
758 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
759 // Calculate 1/3 height of the buffer's default font
760 FontMetrics const & fm
761 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
762 int const middle = yo_ - fm.maxAscent() / 3;
763 pi_.pain.line(last_strikeout_x, middle, int(x_), middle,
764 Color_deletedtext, Painter::line_solid, Painter::line_thin);
765 running_strikeout = false;
768 if (body_pos > 0 && pos == body_pos - 1) {
769 int const lwidth = theFontMetrics(getLabelFont())
770 .width(layout->labelsep);
772 x_ += row_.label_hfill + lwidth - width_pos;
775 if (par_.isHfill(pos)) {
776 Inset const * inset = par_.getInset(pos);
777 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
778 paintHfill(pos, body_pos);
781 } else if (par_.isSeparator(pos)) {
782 Font const orig_font = text_metrics_.getDisplayFont(pit_, pos);
783 double const orig_x = x_;
786 x_ += row_.separator;
787 paintForeignMark(orig_x, orig_font.language());
790 } else if (par_.isInset(pos)) {
791 // If outer row has changed, nested insets are repaint completely.
792 Inset const * inset = par_.getInset(pos);
793 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
794 paintInset(inset, pos);
798 // paint as many characters as possible.
803 // if we reach the end of a struck out range, paint it
804 if (running_strikeout) {
805 // calculate 1/3 height of the buffer's default font
806 FontMetrics const & fm
807 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
808 int const middle = yo_ - fm.maxAscent() / 3;
809 pi_.pain.line(last_strikeout_x, middle, int(x_), middle,
810 Color_deletedtext, Painter::line_solid, Painter::line_thin);
811 running_strikeout = false;