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_.right_margin + fm.width(layout.labelsep);
466 x = x_ - fm.width(layout.labelsep) - fm.width(str);
468 pi_.pain.text(int(x), yo_, str, font);
472 void RowPainter::paintTopLevelLabel() const
474 BufferParams const & bparams = pi_.base.bv->buffer().params();
475 bool const is_rtl = text_.isRTL(par_);
476 ParagraphParameters const & pparams = par_.params();
477 Layout const & layout = par_.layout();
478 FontInfo const font = labelFont();
479 docstring const str = par_.labelString();
483 double spacing_val = 1.0;
484 if (!pparams.spacing().isDefault())
485 spacing_val = pparams.spacing().getValue();
487 spacing_val = bparams.spacing().getValue();
489 FontMetrics const & fm = theFontMetrics(font);
491 int const labeladdon = int(fm.maxHeight()
492 * layout.spacing.getValue() * spacing_val);
495 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
496 + (layout.labelbottomsep * defaultRowHeight()));
499 if (layout.labeltype == LABEL_CENTERED) {
500 x = row_.left_margin + (width_ - row_.left_margin - row_.right_margin) / 2;
501 x -= fm.width(str) / 2;
503 x = width_ - row_.right_margin - fm.width(str);
505 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
509 /** Check if the current paragraph is the last paragraph in a
511 static int getEndLabel(pit_type p, Text const & text)
513 ParagraphList const & pars = text.paragraphs();
515 depth_type par_depth = pars[p].getDepth();
516 while (pit != pit_type(pars.size())) {
517 Layout const & layout = pars[pit].layout();
518 int const endlabeltype = layout.endlabeltype;
520 if (endlabeltype != END_LABEL_NO_LABEL) {
521 if (p + 1 == pit_type(pars.size()))
524 depth_type const next_depth =
525 pars[p + 1].getDepth();
526 if (par_depth > next_depth ||
527 (par_depth == next_depth && layout != pars[p + 1].layout()))
533 pit = text.outerHook(pit);
534 if (pit != pit_type(pars.size()))
535 par_depth = pars[pit].getDepth();
537 return END_LABEL_NO_LABEL;
541 void RowPainter::paintLast()
543 bool const is_rtl = text_.isRTL(par_);
544 int const endlabel = getEndLabel(pit_, text_);
546 // paint imaginary end-of-paragraph character
548 Change const & change = par_.lookupChange(par_.size());
549 if (change.changed()) {
550 FontMetrics const & fm =
551 theFontMetrics(pi_.base.bv->buffer().params().getFont());
552 int const length = fm.maxAscent() / 2;
553 Color col = change.color();
555 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
556 Painter::line_solid, 3);
558 if (change.deleted()) {
559 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
560 yo_ + 2, col, Painter::line_solid, 3);
562 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
563 yo_ + 2, col, Painter::line_solid, 3);
571 case END_LABEL_FILLED_BOX: {
572 FontInfo const font = labelFont();
573 FontMetrics const & fm = theFontMetrics(font);
574 int const size = int(0.75 * fm.maxAscent());
575 int const y = yo_ - size;
576 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
577 int x = is_rtl ? nestMargin() + changebarMargin()
578 : max_row_width - row_.right_margin;
580 // If needed, move the box a bit to avoid overlapping with text.
581 int const rem = max_row_width - row_.width();
583 x += is_rtl ? rem : - rem;
585 if (endlabel == END_LABEL_BOX)
586 pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
588 pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
592 case END_LABEL_STATIC: {
593 FontInfo const font = labelFont();
594 FontMetrics const & fm = theFontMetrics(font);
595 docstring const & str = par_.layout().endlabelstring();
596 double const x = is_rtl ? x_ - fm.width(str) : x_;
597 pi_.pain.text(int(x), yo_, str, font);
601 case END_LABEL_NO_LABEL:
607 void RowPainter::paintOnlyInsets()
609 Row::const_iterator cit = row_.begin();
610 Row::const_iterator const & end = row_.end();
611 for ( ; cit != end ; ++cit) {
612 Row::Element const & e = *cit;
613 if (e.type == Row::INSET) {
614 // If outer row has changed, nested insets are repainted completely.
615 pi_.base.bv->coordCache().insets().add(e.inset, int(x_), yo_);
616 bool const nested_inset =
617 (e.inset->asInsetMath() && !e.inset->asInsetMath()->asMacroTemplate())
618 || e.inset->asInsetText() || e.inset->asInsetTabular();
620 x_ += e.full_width();
623 paintInset(e.inset, e.font, e.change, e.pos);
625 x_ += e.full_width();
630 void RowPainter::paintText()
632 Row::const_iterator cit = row_.begin();
633 Row::const_iterator const & end = row_.end();
634 for ( ; cit != end ; ++cit) {
635 double const orig_x = x_;
636 Row::Element const & e = *cit;
637 int foreign_descent = 0;
642 paintStringAndSel(e.str, e.font, e.change, e.pos, e.endpos);
644 // Paint the spelling mark if needed.
645 if (lyxrc.spellcheck_continuously && pi_.do_spellcheck
646 && par_.isMisspelled(e.pos)) {
647 paintMisspelledMark(orig_x, e.str, e.font, e.pos, e.change.changed());
651 // If outer row has changed, nested insets are repaint completely.
652 pi_.base.bv->coordCache().insets().add(e.inset, int(x_), yo_);
653 paintInset(e.inset, e.font, e.change, e.pos);
654 foreign_descent = e.dim.descent();
659 paintSeparator(e.full_width(), e.font);
662 // The line that indicates word in a different language
663 paintForeignMark(orig_x, e.font.language(), foreign_descent);
665 // change tracking (not for insets that track their own changes)
666 if (e.type != Row::INSET || ! e.inset->canTrackChanges())
667 paintChange(orig_x, e.font, e.change);
672 void RowPainter::paintSelection() const
674 if (!row_.selection())
676 Cursor const & curs = pi_.base.bv->cursor();
677 DocIterator beg = curs.selectionBegin();
679 beg.pos() = row_.sel_beg;
681 DocIterator end = curs.selectionEnd();
683 end.pos() = row_.sel_end;
685 bool const begin_boundary = beg.pos() >= row_.endpos();
686 bool const end_boundary = row_.sel_end == row_.endpos();
688 DocIterator cur = beg;
689 cur.boundary(begin_boundary);
690 int x1 = text_metrics_.cursorX(beg.top(), begin_boundary);
691 int x2 = text_metrics_.cursorX(end.top(), end_boundary);
692 int const y1 = yo_ - row_.ascent();
693 int const y2 = y1 + row_.height();
695 int const rm = text_.isMainText() ? pi_.base.bv->rightMargin() : 0;
696 int const lm = text_.isMainText() ? pi_.base.bv->leftMargin() : 0;
699 if (row_.begin_margin_sel) {
700 if (text_.isRTL(beg.paragraph())) {
701 pi_.pain.fillRectangle(int(xo_ + x1), y1,
702 text_metrics_.width() - rm - x1, y2 - y1, Color_selection);
704 pi_.pain.fillRectangle(int(xo_ + lm), y1, x1 - lm, y2 - y1,
709 if (row_.end_margin_sel) {
710 if (text_.isRTL(beg.paragraph())) {
711 pi_.pain.fillRectangle(int(xo_ + lm), y1, x2 - lm, y2 - y1,
714 pi_.pain.fillRectangle(int(xo_ + x2), y1, text_metrics_.width() - rm - x2,
715 y2 - y1, Color_selection);
719 // if we are on a boundary from the beginning, it's probably
720 // a RTL boundary and we jump to the other side directly as this
721 // segement is 0-size and confuses the logic below
725 // go through row and draw from RTL boundary to RTL boundary
727 bool draw_now = false;
729 // simplified cursorForward code below which does not
730 // descend into insets and which does not go into the
731 // next line. Compare the logic with the original cursorForward
733 // if left of boundary -> just jump to right side, but
734 // for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
735 if (cur.boundary()) {
737 } else if (text_metrics_.isRTLBoundary(cur.pit(), cur.pos() + 1)) {
738 // in front of RTL boundary -> Stay on this side of the boundary
739 // because: ab|cDDEEFFghi -> abc|DDEEFFghi
748 if (cur.pos() == row_.endpos())
753 // the previous segment was just drawn, now the next starts
754 x1 = text_metrics_.cursorX(cur.top(), cur.boundary());
757 if (!(cur < end) || draw_now) {
758 x2 = text_metrics_.cursorX(cur.top(), cur.boundary());
759 pi_.pain.fillRectangle(int(xo_ + min(x1, x2)), y1, abs(x2 - x1),
760 y2 - y1, Color_selection);
762 // reset x1, so it is set again next round (which will be on the
763 // right side of a boundary or at the selection end)