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.
15 #include "RowPainter.h"
18 #include "CoordCache.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
27 #include "MetricsInfo.h"
28 #include "Paragraph.h"
29 #include "ParagraphMetrics.h"
30 #include "ParagraphParameters.h"
31 #include "TextMetrics.h"
34 #include "frontends/FontMetrics.h"
35 #include "frontends/Painter.h"
37 #include "insets/InsetText.h"
39 #include "mathed/InsetMath.h"
41 #include "support/debug.h"
42 #include "support/gettext.h"
43 #include "support/textutils.h"
45 #include "support/lassert.h"
46 #include <boost/crc.hpp>
52 using frontend::Painter;
53 using frontend::FontMetrics;
56 RowPainter::RowPainter(PainterInfo & pi,
57 Text const & text, pit_type pit, Row const & row, int x, int y)
58 : pi_(pi), 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)), change_(pi_.change_),
63 xo_(x), yo_(y), width_(text_metrics_.width()),
64 solid_line_thickness_(1), solid_line_offset_(1),
65 dotted_line_thickness_(1), dotted_line_offset_(2)
67 if (lyxrc.zoom >= 200) {
68 // derive the line thickness from zoom factor
69 // the zoom is given in percent
70 // (increase thickness at 250%, 450% etc.)
71 solid_line_thickness_ = (lyxrc.zoom + 50) / 200;
72 // adjust line_offset_ too
73 solid_line_offset_ = 1 + solid_line_thickness_ / 2;
75 if (lyxrc.zoom >= 100) {
76 // derive the line thickness from zoom factor
77 // the zoom is given in percent
78 // (increase thickness at 150%, 250% etc.)
79 dotted_line_thickness_ = (lyxrc.zoom + 50) / 100;
80 // adjust line_offset_ too
81 dotted_line_offset_ = 1 + dotted_line_thickness_ / 2;
84 x_ = row_.left_margin + xo_;
86 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
90 LBUFERR(pit < int(text.paragraphs().size()));
94 FontInfo RowPainter::labelFont() const
96 FontInfo f = text_.labelFont(par_);
98 if (row_.begin_margin_sel || pi_.selected)
99 f.setPaintColor(Color_selectiontext);
104 // If you want to debug inset metrics uncomment the following line:
105 //#define DEBUG_METRICS
106 // This draws green lines around each inset.
109 void RowPainter::paintInset(Inset const * inset, Font const & font,
110 Change const & change,
114 bool const pi_selected = pi_.selected;
115 Cursor const & cur = pi_.base.bv->cursor();
116 if (cur.selection() && cur.text() == &text_
117 && cur.normalAnchor().text() == &text_)
118 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos;
120 LASSERT(inset, return);
121 // Backup full_repaint status because some insets (InsetTabular)
122 // requires a full repaint
123 bool const pi_full_repaint = pi_.full_repaint;
124 bool const pi_do_spellcheck = pi_.do_spellcheck;
125 Change const pi_change = pi_.change_;
127 pi_.base.font = inset->inheritFont() ? font.fontInfo() :
128 pi_.base.bv->buffer().params().getFont().fontInfo();
129 pi_.ltr_pos = !font.isVisibleRightToLeft();
130 pi_.change_ = change_.changed() ? change_ : change;
131 pi_.do_spellcheck &= inset->allowSpellCheck();
133 int const x1 = int(x_);
134 pi_.base.bv->coordCache().insets().add(inset, x1, yo_);
135 // insets are painted completely. Recursive
136 // FIXME: it is wrong to completely paint the background
137 // if we want to do single row painting.
138 inset->drawBackground(pi_, x1, yo_);
139 inset->drawSelection(pi_, x1, yo_);
140 inset->draw(pi_, x1, yo_);
142 Dimension const & dim = pm_.insetDimension(inset);
144 paintForeignMark(x_, font.language(), dim.descent());
148 // Restore full_repaint status.
149 pi_.full_repaint = pi_full_repaint;
150 pi_.change_ = pi_change;
151 pi_.do_spellcheck = pi_do_spellcheck;
152 pi_.selected = pi_selected;
155 int const x2 = x1 + dim.wid;
156 int const y1 = yo_ + dim.des;
157 int const y2 = yo_ - dim.asc;
158 pi_.pain.line(x1, y1, x1, y2, Color_green);
159 pi_.pain.line(x1, y1, x2, y1, Color_green);
160 pi_.pain.line(x2, y1, x2, y2, Color_green);
161 pi_.pain.line(x1, y2, x2, y2, Color_green);
166 void RowPainter::paintSeparator(double width, Font const & font)
168 pi_.pain.textDecoration(font.fontInfo(), int(x_), yo_, int(width));
173 void RowPainter::paintForeignMark(double orig_x, Language const * lang, int desc) const
175 if (!lyxrc.mark_foreign_language)
177 if (lang == latex_language)
179 if (lang == pi_.base.bv->buffer().params().language)
182 int const y = yo_ + solid_line_offset_ + desc + solid_line_thickness_ / 2;
183 pi_.pain.line(int(orig_x), y, int(x_), y, Color_language,
184 Painter::line_solid, solid_line_thickness_);
188 void RowPainter::paintMisspelledMark(double const orig_x,
189 docstring const & str, Font const & font,
190 pos_type const start_pos,
191 bool const changed) const
193 // if changed the misspelled marker gets placed slightly lower than normal
194 // to avoid drawing at the same vertical offset
195 int const y = yo_ + solid_line_offset_ + solid_line_thickness_
196 + (changed ? solid_line_thickness_ + 1 : 0)
197 + dotted_line_offset_;
199 //FIXME: this could be computed only once, it is probably not costly.
200 // check for cursor position
201 // don't draw misspelled marker for words at cursor position
202 // we don't want to disturb the process of text editing
203 DocIterator const nw = pi_.base.bv->cursor().newWord();
205 if (!nw.empty() && par_.id() == nw.paragraph().id()) {
207 if (cpos > 0 && cpos == par_.size() && !par_.isWordSeparator(cpos-1))
209 else if (cpos > 0 && par_.isWordSeparator(cpos))
213 pos_type pos = start_pos;
214 while (pos < start_pos + pos_type(str.length())) {
215 if (!par_.isMisspelled(pos)) {
220 FontSpan const & range = par_.getSpellRange(pos);
222 // Skip element which are being edited
223 if (range.contains(cpos)) {
224 // the range includes the last element
225 pos = range.last + 1;
229 FontMetrics const & fm = theFontMetrics(font);
230 int x1 = fm.pos2x(str, range.first - start_pos,
231 font.isVisibleRightToLeft());
232 int x2 = fm.pos2x(str, min(range.last - start_pos + 1,
233 pos_type(str.length())),
234 font.isVisibleRightToLeft());
238 pi_.pain.line(int(orig_x) + x1, y, int(orig_x) + x2, y,
240 Painter::line_onoffdash, dotted_line_thickness_);
241 pos = range.last + 1;
246 void RowPainter::paintStringAndSel(docstring const & str, Font const & font,
247 Change const & change,
248 pos_type start_pos, pos_type end_pos)
250 // at least part of text selected?
251 bool const some_sel = (end_pos >= row_.sel_beg && start_pos < row_.sel_end)
253 // all the text selected?
254 bool const all_sel = (start_pos >= row_.sel_beg && end_pos < row_.sel_end)
259 copy.fontInfo().setPaintColor(Color_selectiontext);
260 x_ += pi_.pain.text(int(x_), yo_, str, copy);
261 } else if (change.changed()) {
263 copy.fontInfo().setPaintColor(change.color());
264 x_ += pi_.pain.text(int(x_), yo_, str, copy);
265 } else if (!some_sel) {
266 x_ += pi_.pain.text(int(x_), yo_, str, font);
268 x_ += pi_.pain.text(int(x_), yo_, str, font, Color_selectiontext,
269 max(row_.sel_beg, start_pos) - start_pos,
270 min(row_.sel_end, end_pos) - start_pos);
275 void RowPainter::paintChange(double orig_x, Font const & font,
276 Change const & change) const
278 if (!change.changed())
280 // Calculate 1/3 height of font
281 FontMetrics const & fm = theFontMetrics(font);
282 int const y_bar = change.deleted() ? yo_ - fm.maxAscent() / 3
283 : yo_ + 2 * solid_line_offset_ + solid_line_thickness_;
284 pi_.pain.line(int(orig_x), y_bar, int(x_), y_bar,
285 change.color(), Painter::line_solid, solid_line_thickness_);
289 void RowPainter::paintChangeBar() const
291 pos_type const start = row_.pos();
292 pos_type end = row_.endpos();
294 if (par_.size() == end) {
295 // this is the last row of the paragraph;
296 // thus, we must also consider the imaginary end-of-par character
300 if (start == end || !par_.isChanged(start, end))
303 int const height = text_metrics_.isLastRow(pit_, row_)
307 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
311 void RowPainter::paintAppendix() const
313 // only draw the appendix frame once (for the main text)
314 if (!par_.params().appendix() || !text_.isMainText())
317 int y = yo_ - row_.ascent();
319 if (par_.params().startOfAppendix())
320 y += 2 * defaultRowHeight();
322 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
323 pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
327 void RowPainter::paintDepthBar() const
329 depth_type const depth = par_.getDepth();
334 depth_type prev_depth = 0;
335 if (!text_metrics_.isFirstRow(pit_, row_)) {
336 pit_type pit2 = pit_;
339 prev_depth = pars_[pit2].getDepth();
342 depth_type next_depth = 0;
343 if (!text_metrics_.isLastRow(pit_, row_)) {
344 pit_type pit2 = pit_;
345 if (row_.endpos() >= pars_[pit2].size())
347 next_depth = pars_[pit2].getDepth();
350 for (depth_type i = 1; i <= depth; ++i) {
351 int const w = nestMargin() / 5;
352 int x = int(xo_) + w * i;
353 // only consider the changebar space if we're drawing outermost text
354 if (text_.isMainText())
355 x += changebarMargin();
357 int const starty = yo_ - row_.ascent();
358 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
360 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
363 pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
365 pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
370 int RowPainter::paintAppendixStart(int y) const
372 FontInfo pb_font = sane_font;
373 pb_font.setColor(Color_appendix);
380 docstring const label = _("Appendix");
381 theFontMetrics(pb_font).rectText(label, w, a, d);
383 int const text_start = int(xo_ + (width_ - w) / 2);
384 int const text_end = text_start + w;
386 pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
388 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
389 pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
391 return 3 * defaultRowHeight();
395 void RowPainter::paintTooLargeMarks(bool const left, bool const right) const
398 pi_.pain.line(dotted_line_thickness_, yo_ - row_.ascent(),
399 dotted_line_thickness_, yo_ + row_.descent(),
401 Painter::line_onoffdash, dotted_line_thickness_);
403 int const wwidth = pi_.base.bv->workWidth() - dotted_line_thickness_;
404 pi_.pain.line(wwidth, yo_ - row_.ascent(),
405 wwidth, yo_ + row_.descent(),
407 Painter::line_onoffdash, dotted_line_thickness_);
412 void RowPainter::paintFirst() const
414 BufferParams const & bparams = pi_.base.bv->buffer().params();
415 Layout const & layout = par_.layout();
419 // start of appendix?
420 if (par_.params().startOfAppendix())
421 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
423 if (bparams.paragraph_separation == BufferParams::ParagraphSkipSeparation
425 if (layout.latextype == LATEX_PARAGRAPH
426 && !par_.getDepth()) {
427 y_top += bparams.getDefSkip().inPixels(*pi_.base.bv);
429 Layout const & playout = pars_[pit_ - 1].layout();
430 if (playout.latextype == LATEX_PARAGRAPH
431 && !pars_[pit_ - 1].getDepth()) {
432 // is it right to use defskip here, too? (AS)
433 y_top += bparams.getDefSkip().inPixels(*pi_.base.bv);
438 bool const is_first =
439 text_.isFirstInSequence(pit_) || !layout.isParagraphGroup();
440 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
442 if (layout.labelIsInline()
443 && (layout.labeltype != LABEL_STATIC || is_first)) {
445 } else if (is_first && layout.labelIsAbove()) {
446 paintTopLevelLabel();
451 void RowPainter::paintLabel() const
453 docstring const str = par_.labelString();
457 bool const is_rtl = text_.isRTL(par_);
458 Layout const & layout = par_.layout();
459 FontInfo const font = labelFont();
460 FontMetrics const & fm = theFontMetrics(font);
464 x = width_ - row_.left_margin
465 + fm.width(layout.labelsep);
467 x = x_ - fm.width(layout.labelsep)
471 pi_.pain.text(int(x), yo_, str, font);
475 void RowPainter::paintTopLevelLabel() const
477 BufferParams const & bparams = pi_.base.bv->buffer().params();
478 bool const is_rtl = text_.isRTL(par_);
479 ParagraphParameters const & pparams = par_.params();
480 Layout const & layout = par_.layout();
481 FontInfo const font = labelFont();
482 docstring const str = par_.labelString();
486 double spacing_val = 1.0;
487 if (!pparams.spacing().isDefault())
488 spacing_val = pparams.spacing().getValue();
490 spacing_val = bparams.spacing().getValue();
492 FontMetrics const & fm = theFontMetrics(font);
494 int const labeladdon = int(fm.maxHeight()
495 * layout.spacing.getValue() * spacing_val);
498 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
499 + (layout.labelbottomsep * defaultRowHeight()));
502 if (layout.labeltype == LABEL_CENTERED) {
504 x = row_.left_margin;
505 x += (width_ - text_metrics_.rightMargin(pm_) - row_.left_margin) / 2;
506 x -= fm.width(str) / 2;
508 x = width_ - row_.left_margin - fm.width(str);
510 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
514 /** Check if the current paragraph is the last paragraph in a
516 static int getEndLabel(pit_type p, Text const & text)
518 ParagraphList const & pars = text.paragraphs();
520 depth_type par_depth = pars[p].getDepth();
521 while (pit != pit_type(pars.size())) {
522 Layout const & layout = pars[pit].layout();
523 int const endlabeltype = layout.endlabeltype;
525 if (endlabeltype != END_LABEL_NO_LABEL) {
526 if (p + 1 == pit_type(pars.size()))
529 depth_type const next_depth =
530 pars[p + 1].getDepth();
531 if (par_depth > next_depth ||
532 (par_depth == next_depth && layout != pars[p + 1].layout()))
538 pit = text.outerHook(pit);
539 if (pit != pit_type(pars.size()))
540 par_depth = pars[pit].getDepth();
542 return END_LABEL_NO_LABEL;
546 void RowPainter::paintLast()
548 bool const is_rtl = text_.isRTL(par_);
549 int const endlabel = getEndLabel(pit_, text_);
551 // paint imaginary end-of-paragraph character
553 Change const & change = par_.lookupChange(par_.size());
554 if (change.changed()) {
555 FontMetrics const & fm =
556 theFontMetrics(pi_.base.bv->buffer().params().getFont());
557 int const length = fm.maxAscent() / 2;
558 Color col = change.color();
560 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
561 Painter::line_solid, 3);
563 if (change.deleted()) {
564 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
565 yo_ + 2, col, Painter::line_solid, 3);
567 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
568 yo_ + 2, col, Painter::line_solid, 3);
576 case END_LABEL_FILLED_BOX: {
577 FontInfo const font = labelFont();
578 FontMetrics const & fm = theFontMetrics(font);
579 int const size = int(0.75 * fm.maxAscent());
580 int const y = yo_ - size;
581 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
582 int x = is_rtl ? nestMargin() + changebarMargin()
583 : max_row_width - text_metrics_.rightMargin(pm_);
585 // If needed, move the box a bit to avoid overlapping with text.
586 int const rem = max_row_width - row_.width();
588 x += is_rtl ? rem : - rem;
590 if (endlabel == END_LABEL_BOX)
591 pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
593 pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
597 case END_LABEL_STATIC: {
598 FontInfo const font = labelFont();
599 FontMetrics const & fm = theFontMetrics(font);
600 docstring const & str = par_.layout().endlabelstring();
601 double const x = is_rtl ? x_ - fm.width(str) : x_;
602 pi_.pain.text(int(x), yo_, str, font);
606 case END_LABEL_NO_LABEL:
612 void RowPainter::paintOnlyInsets()
614 CoordCache const & cache = pi_.base.bv->coordCache();
615 pos_type const end = row_.endpos();
616 for (pos_type pos = row_.pos(); pos != end; ++pos) {
617 // If outer row has changed, nested insets are repaint completely.
618 Inset const * inset = par_.getInset(pos);
619 bool const nested_inset = inset &&
620 ((inset->asInsetMath() &&
621 !inset->asInsetMath()->asMacroTemplate())
622 || inset->asInsetText()
623 || inset->asInsetTabular());
626 if (x_ > pi_.base.bv->workWidth()
627 || !cache.getInsets().has(inset))
629 x_ = cache.getInsets().x(inset);
630 Font const font = text_metrics_.displayFont(pit_, pos);
631 paintInset(inset, font, par_.lookupChange(pos), pos);
635 void RowPainter::paintText()
637 Row::const_iterator cit = row_.begin();
638 Row::const_iterator const & end = row_.end();
639 for ( ; cit != end ; ++cit) {
640 double const orig_x = x_;
641 Row::Element const & e = *cit;
642 int foreign_descent = 0;
647 paintStringAndSel(e.str, e.font, e.change, e.pos, e.endpos);
649 // Paint the spelling mark if needed.
650 if (lyxrc.spellcheck_continuously && pi_.do_spellcheck
651 && par_.isMisspelled(e.pos)) {
652 paintMisspelledMark(orig_x, e.str, e.font, e.pos, e.change.changed());
656 // If outer row has changed, nested insets are repaint completely.
657 pi_.base.bv->coordCache().insets().add(e.inset, int(x_), yo_);
658 paintInset(e.inset, e.font, e.change, e.pos);
659 foreign_descent = e.dim.descent();
664 paintSeparator(e.full_width(), e.font);
667 // The line that indicates word in a different language
668 paintForeignMark(orig_x, e.font.language(), foreign_descent);
670 // change tracking (not for insets that track their own changes)
671 if (e.type != Row::INSET || ! e.inset->canTrackChanges())
672 paintChange(orig_x, e.font, e.change);
677 void RowPainter::paintSelection() const
679 if (!row_.selection())
681 Cursor const & curs = pi_.base.bv->cursor();
682 DocIterator beg = curs.selectionBegin();
684 beg.pos() = row_.sel_beg;
686 DocIterator end = curs.selectionEnd();
688 end.pos() = row_.sel_end;
690 bool const begin_boundary = beg.pos() >= row_.endpos();
691 bool const end_boundary = row_.sel_end == row_.endpos();
693 DocIterator cur = beg;
694 cur.boundary(begin_boundary);
695 int x1 = text_metrics_.cursorX(beg.top(), begin_boundary);
696 int x2 = text_metrics_.cursorX(end.top(), end_boundary);
697 int const y1 = yo_ - row_.ascent();
698 int const y2 = y1 + row_.height();
700 int const rm = text_.isMainText() ? pi_.base.bv->rightMargin() : 0;
701 int const lm = text_.isMainText() ? pi_.base.bv->leftMargin() : 0;
704 if (row_.begin_margin_sel) {
705 if (text_.isRTL(beg.paragraph())) {
706 pi_.pain.fillRectangle(int(xo_ + x1), y1,
707 text_metrics_.width() - rm - x1, y2 - y1, Color_selection);
709 pi_.pain.fillRectangle(int(xo_ + lm), y1, x1 - lm, y2 - y1,
714 if (row_.end_margin_sel) {
715 if (text_.isRTL(beg.paragraph())) {
716 pi_.pain.fillRectangle(int(xo_ + lm), y1, x2 - lm, y2 - y1,
719 pi_.pain.fillRectangle(int(xo_ + x2), y1, text_metrics_.width() - rm - x2,
720 y2 - y1, Color_selection);
724 // if we are on a boundary from the beginning, it's probably
725 // a RTL boundary and we jump to the other side directly as this
726 // segement is 0-size and confuses the logic below
730 // go through row and draw from RTL boundary to RTL boundary
732 bool draw_now = false;
734 // simplified cursorForward code below which does not
735 // descend into insets and which does not go into the
736 // next line. Compare the logic with the original cursorForward
738 // if left of boundary -> just jump to right side, but
739 // for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
740 if (cur.boundary()) {
742 } else if (text_metrics_.isRTLBoundary(cur.pit(), cur.pos() + 1)) {
743 // in front of RTL boundary -> Stay on this side of the boundary
744 // because: ab|cDDEEFFghi -> abc|DDEEFFghi
753 if (cur.pos() == row_.endpos())
758 // the previous segment was just drawn, now the next starts
759 x1 = text_metrics_.cursorX(cur.top(), cur.boundary());
762 if (!(cur < end) || draw_now) {
763 x2 = text_metrics_.cursorX(cur.top(), cur.boundary());
764 pi_.pain.fillRectangle(int(xo_ + min(x1, x2)), y1, abs(x2 - x1),
765 y2 - y1, Color_selection);
767 // reset x1, so it is set again next round (which will be on the
768 // right side of a boundary or at the selection end)