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, 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), par_(text.paragraphs()[row.pit()]),
62 pm_(text_metrics_.parMetrics(row.pit())), change_(pi_.change_),
63 xo_(x), yo_(y), width_(text_metrics_.width())
65 x_ = row_.left_margin + xo_;
67 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
70 LBUFERR(row.pit() >= 0);
71 LBUFERR(row.pit() < int(text.paragraphs().size()));
75 FontInfo RowPainter::labelFont() const
77 FontInfo f = text_.labelFont(par_);
79 if (row_.begin_margin_sel || pi_.selected)
80 f.setPaintColor(Color_selectiontext);
85 // If you want to debug inset metrics uncomment the following line:
86 //#define DEBUG_METRICS
87 // This draws green lines around each inset.
90 void RowPainter::paintInset(Row::Element const & e) const
93 bool const pi_selected = pi_.selected;
94 Cursor const & cur = pi_.base.bv->cursor();
95 if (cur.selection() && cur.text() == &text_
96 && cur.normalAnchor().text() == &text_)
97 pi_.selected = row_.sel_beg <= e.pos && row_.sel_end > e.pos;
99 LASSERT(e.inset, return);
100 // Backup full_repaint status because some insets (InsetTabular)
101 // requires a full repaint
102 bool const pi_full_repaint = pi_.full_repaint;
103 bool const pi_do_spellcheck = pi_.do_spellcheck;
104 Change const pi_change = pi_.change_;
106 pi_.base.font = e.inset->inheritFont() ? e.font.fontInfo() :
107 pi_.base.bv->buffer().params().getFont().fontInfo();
108 pi_.ltr_pos = !e.font.isVisibleRightToLeft();
109 pi_.change_ = change_.changed() ? change_ : e.change;
110 pi_.do_spellcheck &= e.inset->allowSpellCheck();
112 int const x1 = int(x_);
113 pi_.base.bv->coordCache().insets().add(e.inset, x1, yo_);
114 // insets are painted completely. Recursive
115 // FIXME: it is wrong to completely paint the background
116 // if we want to do single row painting.
117 e.inset->drawBackground(pi_, x1, yo_);
118 e.inset->drawSelection(pi_, x1, yo_);
119 e.inset->draw(pi_, x1, yo_);
121 // Restore full_repaint status.
122 pi_.full_repaint = pi_full_repaint;
123 pi_.change_ = pi_change;
124 pi_.do_spellcheck = pi_do_spellcheck;
125 pi_.selected = pi_selected;
128 Dimension const & dim = pi_.base.bv->coordCache().insets().dim(e.inset);
129 int const x2 = x1 + dim.wid;
130 int const y1 = yo_ + dim.des;
131 int const y2 = yo_ - dim.asc;
132 pi_.pain.line(x1, y1, x1, y2, Color_green);
133 pi_.pain.line(x1, y1, x2, y1, Color_green);
134 pi_.pain.line(x2, y1, x2, y2, Color_green);
135 pi_.pain.line(x1, y2, x2, y2, Color_green);
140 void RowPainter::paintForeignMark(Row::Element const & e) const
142 Language const * lang = e.font.language();
143 if (!lyxrc.mark_foreign_language)
145 if (lang == latex_language)
147 if (lang == pi_.base.bv->buffer().params().language)
150 int const desc = e.inset ? e.dim.descent() : 0;
151 int const y = yo_ + pi_.base.solidLineOffset()
152 + desc + pi_.base.solidLineThickness() / 2;
153 pi_.pain.line(int(x_), y, int(x_ + e.full_width()), y, Color_language,
154 Painter::line_solid, pi_.base.solidLineThickness());
158 void RowPainter::paintMisspelledMark(Row::Element const & e) const
160 // if changed the misspelled marker gets placed slightly lower than normal
161 // to avoid drawing at the same vertical offset
162 FontMetrics const & fm = theFontMetrics(e.font);
163 int const thickness = max(fm.lineWidth(), 2);
164 int const y = yo_ + pi_.base.solidLineOffset() + pi_.base.solidLineThickness()
165 + (e.change.changed() ? pi_.base.solidLineThickness() + 1 : 0)
168 //FIXME: this could be computed only once, it is probably not costly.
169 // check for cursor position
170 // don't draw misspelled marker for words at cursor position
171 // we don't want to disturb the process of text editing
172 DocIterator const nw = pi_.base.bv->cursor().newWord();
174 if (!nw.empty() && par_.id() == nw.paragraph().id()) {
176 if (cpos > 0 && cpos == par_.size() && !par_.isWordSeparator(cpos-1))
178 else if (cpos > 0 && par_.isWordSeparator(cpos))
182 pos_type pos = e.pos;
183 while (pos < e.pos + pos_type(e.str.length())) {
184 if (!par_.isMisspelled(pos)) {
189 FontSpan const & range = par_.getSpellRange(pos);
191 // Skip element which are being edited
192 if (range.contains(cpos)) {
193 // the range includes the last element
194 pos = range.last + 1;
198 FontMetrics const & fm = theFontMetrics(e.font);
199 int x1 = fm.pos2x(e.str, range.first - e.pos,
201 int x2 = fm.pos2x(e.str, min(range.last - e.pos + 1,
202 pos_type(e.str.length())),
207 pi_.pain.line(int(x_ + x1), y, int(x_ + x2), y,
209 Painter::line_onoffdash, thickness);
210 pos = range.last + 1;
215 void RowPainter::paintStringAndSel(Row::Element const & e) const
217 // at least part of text selected?
218 bool const some_sel = (e.endpos >= row_.sel_beg && e.pos < row_.sel_end)
220 // all the text selected?
221 bool const all_sel = (e.pos >= row_.sel_beg && e.endpos < row_.sel_end)
224 if (all_sel || e.change.changed()) {
226 Color const col = e.change.changed() ? e.change.color()
227 : Color_selectiontext;
228 copy.fontInfo().setPaintColor(col);
229 pi_.pain.text(int(x_), yo_, e.str, copy, e.extra, e.full_width());
230 } else if (!some_sel) {
231 pi_.pain.text(int(x_), yo_, e.str, e.font, e.extra, e.full_width());
233 pi_.pain.text(int(x_), yo_, e.str, e.font, Color_selectiontext,
234 max(row_.sel_beg, e.pos) - e.pos,
235 min(row_.sel_end, e.endpos) - e.pos,
236 e.extra, e.full_width());
241 void RowPainter::paintChange(Row::Element const & e) const
243 e.change.paintCue(pi_, x_, yo_, x_ + e.full_width(), e.font.fontInfo());
247 void RowPainter::paintChangeBar() const
249 pos_type const start = row_.pos();
250 pos_type end = row_.endpos();
252 if (par_.size() == end) {
253 // this is the last row of the paragraph;
254 // thus, we must also consider the imaginary end-of-par character
258 if (start == end || !par_.isChanged(start, end))
261 int const height = text_metrics_.isLastRow(row_)
265 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
269 void RowPainter::paintAppendix() const
271 // only draw the appendix frame once (for the main text)
272 if (!par_.params().appendix() || !text_.isMainText())
275 int y = yo_ - row_.ascent();
277 if (par_.params().startOfAppendix())
278 y += 2 * defaultRowHeight();
280 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
281 pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
285 void RowPainter::paintDepthBar() const
287 depth_type const depth = par_.getDepth();
292 depth_type prev_depth = 0;
293 if (!text_metrics_.isFirstRow(row_)) {
294 pit_type pit2 = row_.pit();
297 prev_depth = pars_[pit2].getDepth();
300 depth_type next_depth = 0;
301 if (!text_metrics_.isLastRow(row_)) {
302 pit_type pit2 = row_.pit();
303 if (row_.endpos() >= pars_[pit2].size())
305 next_depth = pars_[pit2].getDepth();
308 for (depth_type i = 1; i <= depth; ++i) {
309 int const w = nestMargin() / 5;
310 int x = int(xo_) + w * i;
311 // only consider the changebar space if we're drawing outermost text
312 if (text_.isMainText())
313 x += changebarMargin();
315 int const starty = yo_ - row_.ascent();
316 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
318 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
321 pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
323 pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
328 void RowPainter::paintAppendixStart(int y) const
330 FontInfo pb_font = sane_font;
331 pb_font.setColor(Color_appendix);
338 docstring const label = _("Appendix");
339 theFontMetrics(pb_font).rectText(label, w, a, d);
341 int const text_start = int(xo_ + (width_ - w) / 2);
342 int const text_end = text_start + w;
344 pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
346 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
347 pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
351 void RowPainter::paintTooLargeMarks(bool const left, bool const right) const
354 pi_.pain.line(pi_.base.dottedLineThickness(), yo_ - row_.ascent(),
355 pi_.base.dottedLineThickness(), yo_ + row_.descent(),
356 Color_scroll, Painter::line_onoffdash,
357 pi_.base.dottedLineThickness());
360 pi_.base.bv->workWidth() - pi_.base.dottedLineThickness();
361 pi_.pain.line(wwidth, yo_ - row_.ascent(),
362 wwidth, yo_ + row_.descent(),
363 Color_scroll, Painter::line_onoffdash,
364 pi_.base.dottedLineThickness());
369 void RowPainter::paintFirst() const
371 Layout const & layout = par_.layout();
373 // start of appendix?
374 if (par_.params().startOfAppendix())
375 paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
377 bool const is_first =
378 text_.isFirstInSequence(row_.pit()) || !layout.isParagraphGroup();
379 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
381 if (layout.labelIsInline()
382 && (layout.labeltype != LABEL_STATIC || is_first))
384 else if (is_first && layout.labelIsAbove())
385 paintTopLevelLabel();
389 void RowPainter::paintLabel() const
391 docstring const & str = par_.labelString();
395 bool const is_rtl = text_.isRTL(par_);
396 Layout const & layout = par_.layout();
397 FontInfo const font = labelFont();
398 FontMetrics const & fm = theFontMetrics(font);
402 x = width_ - row_.right_margin + fm.width(layout.labelsep);
404 x = x_ - fm.width(layout.labelsep) - fm.width(str);
406 pi_.pain.text(int(x), yo_, str, font);
410 void RowPainter::paintTopLevelLabel() const
412 BufferParams const & bparams = pi_.base.bv->buffer().params();
413 bool const is_rtl = text_.isRTL(par_);
414 ParagraphParameters const & pparams = par_.params();
415 Layout const & layout = par_.layout();
416 FontInfo const font = labelFont();
417 docstring const str = par_.labelString();
421 double spacing_val = 1.0;
422 if (!pparams.spacing().isDefault())
423 spacing_val = pparams.spacing().getValue();
425 spacing_val = bparams.spacing().getValue();
427 FontMetrics const & fm = theFontMetrics(font);
429 int const labeladdon = int(fm.maxHeight()
430 * layout.spacing.getValue() * spacing_val);
433 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
434 + (layout.labelbottomsep * defaultRowHeight()));
437 if (layout.labeltype == LABEL_CENTERED) {
438 x = row_.left_margin + (width_ - row_.left_margin - row_.right_margin) / 2;
439 x -= fm.width(str) / 2;
441 x = width_ - row_.right_margin - fm.width(str);
443 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
447 /** Check if the current paragraph is the last paragraph in a
449 static int getEndLabel(pit_type p, Text const & text)
451 ParagraphList const & pars = text.paragraphs();
453 depth_type par_depth = pars[p].getDepth();
454 while (pit != pit_type(pars.size())) {
455 Layout const & layout = pars[pit].layout();
456 int const endlabeltype = layout.endlabeltype;
458 if (endlabeltype != END_LABEL_NO_LABEL) {
459 if (p + 1 == pit_type(pars.size()))
462 depth_type const next_depth =
463 pars[p + 1].getDepth();
464 if (par_depth > next_depth ||
465 (par_depth == next_depth && layout != pars[p + 1].layout()))
471 pit = text.outerHook(pit);
472 if (pit != pit_type(pars.size()))
473 par_depth = pars[pit].getDepth();
475 return END_LABEL_NO_LABEL;
479 void RowPainter::paintLast() const
481 bool const is_rtl = text_.isRTL(par_);
482 int const endlabel = getEndLabel(row_.pit(), text_);
484 // paint imaginary end-of-paragraph character
486 Change const & change = par_.lookupChange(par_.size());
487 if (change.changed()) {
488 FontMetrics const & fm =
489 theFontMetrics(pi_.base.bv->buffer().params().getFont());
490 int const length = fm.maxAscent() / 2;
491 Color col = change.color();
493 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
494 Painter::line_solid, 3);
496 if (change.deleted()) {
497 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
498 yo_ + 2, col, Painter::line_solid, 3);
500 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
501 yo_ + 2, col, Painter::line_solid, 3);
509 case END_LABEL_FILLED_BOX: {
510 FontInfo const font = labelFont();
511 FontMetrics const & fm = theFontMetrics(font);
512 int const size = int(0.75 * fm.maxAscent());
513 int const y = yo_ - size;
514 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
515 int x = is_rtl ? nestMargin() + changebarMargin()
516 : max_row_width - row_.right_margin;
518 // If needed, move the box a bit to avoid overlapping with text.
519 int const rem = max_row_width - row_.width();
521 x += is_rtl ? rem : - rem;
523 if (endlabel == END_LABEL_BOX)
524 pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
526 pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
530 case END_LABEL_STATIC: {
531 FontInfo const font = labelFont();
532 FontMetrics const & fm = theFontMetrics(font);
533 docstring const & str = par_.layout().endlabelstring();
534 double const x = is_rtl ? x_ - fm.width(str) : x_;
535 pi_.pain.text(int(x), yo_, str, font);
539 case END_LABEL_NO_LABEL:
545 void RowPainter::paintOnlyInsets()
547 Row::const_iterator cit = row_.begin();
548 Row::const_iterator const & end = row_.end();
549 for ( ; cit != end ; ++cit) {
550 Row::Element const & e = *cit;
551 if (e.type == Row::INSET) {
552 // If outer row has changed, nested insets are repainted completely.
553 // FIXME: check what this really does. The test is weird.
554 bool const nested_inset =
555 (e.inset->asInsetMath() && !e.inset->asInsetMath()->asMacroTemplate())
556 || e.inset->asInsetText() || e.inset->asInsetTabular();
560 x_ += e.full_width();
565 void RowPainter::paintText()
567 Row::const_iterator cit = row_.begin();
568 Row::const_iterator const & end = row_.end();
569 for ( ; cit != end ; ++cit) {
570 Row::Element const & e = *cit;
575 paintStringAndSel(e);
577 // Paint the spelling marks if enabled.
578 if (lyxrc.spellcheck_continuously && pi_.do_spellcheck && pi_.pain.isDrawingEnabled())
579 paintMisspelledMark(e);
587 pi_.pain.textDecoration(e.font.fontInfo(), int(x_), yo_, int(e.full_width()));
590 // The line that indicates word in a different language
593 // change tracking (not for insets that handle it themselves)
594 if (e.type != Row::INSET || ! e.inset->canPaintChange(*pi_.base.bv))
597 x_ += e.full_width();
602 void RowPainter::paintSelection() const
604 if (!row_.selection())
606 Cursor const & curs = pi_.base.bv->cursor();
607 DocIterator beg = curs.selectionBegin();
608 beg.pit() = row_.pit();
609 beg.pos() = row_.sel_beg;
611 DocIterator end = curs.selectionEnd();
612 end.pit() = row_.pit();
613 end.pos() = row_.sel_end;
615 bool const begin_boundary = beg.pos() >= row_.endpos();
616 bool const end_boundary = row_.sel_end == row_.endpos();
618 DocIterator cur = beg;
619 cur.boundary(begin_boundary);
620 int x1 = text_metrics_.cursorX(beg.top(), begin_boundary);
621 int x2 = text_metrics_.cursorX(end.top(), end_boundary);
622 int const y1 = yo_ - row_.ascent();
623 int const y2 = y1 + row_.height();
625 int const rm = text_.isMainText() ? pi_.base.bv->rightMargin() : 0;
626 int const lm = text_.isMainText() ? pi_.base.bv->leftMargin() : 0;
629 if (row_.begin_margin_sel) {
630 if (text_.isRTL(beg.paragraph())) {
631 pi_.pain.fillRectangle(int(xo_ + x1), y1,
632 text_metrics_.width() - rm - x1, y2 - y1, Color_selection);
634 pi_.pain.fillRectangle(int(xo_ + lm), y1, x1 - lm, y2 - y1,
639 if (row_.end_margin_sel) {
640 if (text_.isRTL(beg.paragraph())) {
641 pi_.pain.fillRectangle(int(xo_ + lm), y1, x2 - lm, y2 - y1,
644 pi_.pain.fillRectangle(int(xo_ + x2), y1, text_metrics_.width() - rm - x2,
645 y2 - y1, Color_selection);
649 // if we are on a boundary from the beginning, it's probably
650 // a RTL boundary and we jump to the other side directly as this
651 // segement is 0-size and confuses the logic below
655 // go through row and draw from RTL boundary to RTL boundary
657 bool draw_now = false;
659 // simplified cursorForward code below which does not
660 // descend into insets and which does not go into the
661 // next line. Compare the logic with the original cursorForward
663 // if left of boundary -> just jump to right side, but
664 // for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
665 if (cur.boundary()) {
667 } else if (text_metrics_.isRTLBoundary(cur.pit(), cur.pos() + 1)) {
668 // in front of RTL boundary -> Stay on this side of the boundary
669 // because: ab|cDDEEFFghi -> abc|DDEEFFghi
678 if (cur.pos() == row_.endpos())
683 // the previous segment was just drawn, now the next starts
684 x1 = text_metrics_.cursorX(cur.top(), cur.boundary());
687 if (!(cur < end) || draw_now) {
688 x2 = text_metrics_.cursorX(cur.top(), cur.boundary());
689 pi_.pain.fillRectangle(int(xo_ + min(x1, x2)), y1, abs(x2 - x1),
690 y2 - y1, Color_selection);
692 // reset x1, so it is set again next round (which will be on the
693 // right side of a boundary or at the selection end)