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 "ParagraphParameters.h"
30 #include "TextMetrics.h"
33 #include "frontends/FontMetrics.h"
34 #include "frontends/Painter.h"
36 #include "insets/InsetText.h"
38 #include "mathed/InsetMath.h"
40 #include "support/debug.h"
41 #include "support/gettext.h"
42 #include "support/textutils.h"
44 #include "support/lassert.h"
45 #include <boost/crc.hpp>
53 using frontend::Painter;
54 using frontend::FontMetrics;
57 RowPainter::RowPainter(PainterInfo & pi,
58 Text const & text, Row const & row, int x, int y)
59 : pi_(pi), text_(text),
60 tm_(pi_.base.bv->textMetrics(&text)),
61 pars_(text.paragraphs()),
62 row_(row), par_(text.paragraphs()[row.pit()]),
66 x_ = row_.left_margin + xo_;
68 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
71 LBUFERR(row.pit() >= 0);
72 LBUFERR(row.pit() < int(text.paragraphs().size()));
76 FontInfo RowPainter::labelFont() const
78 FontInfo f = text_.labelFont(par_);
80 if (row_.begin_margin_sel || pi_.selected)
81 f.setPaintColor(Color_selectiontext);
86 // If you want to debug inset metrics uncomment the following line:
87 //#define DEBUG_METRICS
88 // This draws green lines around each inset.
91 void RowPainter::paintInset(Row::Element const & e) const
94 bool const pi_selected = pi_.selected;
95 Cursor const & cur = pi_.base.bv->cursor();
96 if (cur.selection() && cur.text() == &text_
97 && cur.normalAnchor().text() == &text_)
98 pi_.selected = row_.sel_beg <= e.pos && row_.sel_end > e.pos;
100 LASSERT(e.inset, return);
101 // Backup full_repaint status because some insets (InsetTabular)
102 // requires a full repaint
103 bool const pi_full_repaint = pi_.full_repaint;
104 bool const pi_do_spellcheck = pi_.do_spellcheck;
105 Change const pi_change = pi_.change_;
107 pi_.base.font = e.inset->inheritFont() ? e.font.fontInfo() :
108 pi_.base.bv->buffer().params().getFont().fontInfo();
109 pi_.ltr_pos = !e.font.isVisibleRightToLeft();
110 pi_.change_ = change_.changed() ? change_ : e.change;
111 pi_.do_spellcheck &= e.inset->allowSpellCheck();
113 int const x1 = int(x_);
114 pi_.base.bv->coordCache().insets().add(e.inset, x1, yo_);
115 // insets are painted completely. Recursive
116 // FIXME: it is wrong to completely paint the background
117 // if we want to do single row painting.
118 e.inset->drawBackground(pi_, x1, yo_);
119 e.inset->drawSelection(pi_, x1, yo_);
120 e.inset->draw(pi_, x1, yo_);
122 // Restore full_repaint status.
123 pi_.full_repaint = pi_full_repaint;
124 pi_.change_ = pi_change;
125 pi_.do_spellcheck = pi_do_spellcheck;
126 pi_.selected = pi_selected;
129 Dimension const & dim = pi_.base.bv->coordCache().insets().dim(e.inset);
130 int const x2 = x1 + dim.wid;
131 int const y1 = yo_ + dim.des;
132 int const y2 = yo_ - dim.asc;
133 pi_.pain.line(x1, y1, x1, y2, Color_green);
134 pi_.pain.line(x1, y1, x2, y1, Color_green);
135 pi_.pain.line(x2, y1, x2, y2, Color_green);
136 pi_.pain.line(x1, y2, x2, y2, Color_green);
141 void RowPainter::paintForeignMark(Row::Element const & e) const
143 Language const * lang = e.font.language();
144 if (!lyxrc.mark_foreign_language)
146 if (lang == latex_language)
148 if (lang == pi_.base.bv->buffer().params().language)
151 int const desc = e.inset ? e.dim.descent() : 0;
152 int const y = yo_ + pi_.base.solidLineOffset()
153 + desc + pi_.base.solidLineThickness() / 2;
154 pi_.pain.line(int(x_), y, int(x_ + e.full_width()), y, Color_language,
155 Painter::line_solid, pi_.base.solidLineThickness());
159 void RowPainter::paintMisspelledMark(Row::Element const & e) const
161 // if changed the misspelled marker gets placed slightly lower than normal
162 // to avoid drawing at the same vertical offset
163 FontMetrics const & fm = theFontMetrics(e.font);
164 int const thickness = max(fm.lineWidth(), 2);
165 int const y = yo_ + pi_.base.solidLineOffset() + pi_.base.solidLineThickness()
166 + (e.change.changed() ? pi_.base.solidLineThickness() + 1 : 0)
169 //FIXME: this could be computed only once, it is probably not costly.
170 // check for cursor position
171 // don't draw misspelled marker for words at cursor position
172 // we don't want to disturb the process of text editing
173 DocIterator const nw = pi_.base.bv->cursor().newWord();
175 if (!nw.empty() && par_.id() == nw.paragraph().id()) {
177 if (cpos > 0 && cpos == par_.size() && !par_.isWordSeparator(cpos-1))
179 else if (cpos > 0 && par_.isWordSeparator(cpos))
183 pos_type pos = e.pos;
184 while (pos < e.pos + pos_type(e.str.length())) {
185 if (!par_.isMisspelled(pos)) {
190 FontSpan const & range = par_.getSpellRange(pos);
192 // Skip element which are being edited
193 if (range.contains(cpos)) {
194 // the range includes the last element
195 pos = range.last + 1;
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 int const height = tm_.isLastRow(row_)
253 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
257 void RowPainter::paintAppendix() const
259 // only draw the appendix frame once (for the main text)
260 if (!par_.params().appendix() || !text_.isMainText())
263 int y = yo_ - row_.ascent();
265 if (par_.params().startOfAppendix())
266 y += 2 * defaultRowHeight();
268 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
269 pi_.pain.line(tm_.width() - 2, y, tm_.width() - 2, yo_ + row_.height(), Color_appendix);
273 void RowPainter::paintDepthBar() const
275 depth_type const depth = par_.getDepth();
280 depth_type prev_depth = 0;
281 if (!tm_.isFirstRow(row_)) {
282 pit_type pit2 = row_.pit();
285 prev_depth = pars_[pit2].getDepth();
288 depth_type next_depth = 0;
289 if (!tm_.isLastRow(row_)) {
290 pit_type pit2 = row_.pit();
291 if (row_.endpos() >= pars_[pit2].size())
293 next_depth = pars_[pit2].getDepth();
296 for (depth_type i = 1; i <= depth; ++i) {
297 int const w = nestMargin() / 5;
298 int x = int(xo_) + w * i;
299 // only consider the changebar space if we're drawing outermost text
300 if (text_.isMainText())
301 x += changebarMargin();
303 int const starty = yo_ - row_.ascent();
304 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
306 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
309 pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
311 pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
316 void RowPainter::paintAppendixStart(int y) const
318 FontInfo pb_font = sane_font;
319 pb_font.setColor(Color_appendix);
326 docstring const label = _("Appendix");
327 theFontMetrics(pb_font).rectText(label, w, a, d);
329 int const text_start = int(xo_ + (tm_.width() - w) / 2);
330 int const text_end = text_start + w;
332 pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
334 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
335 pi_.pain.line(text_end, y, int(xo_ + tm_.width() - 2), y, Color_appendix);
339 void RowPainter::paintTooLargeMarks(bool const left, bool const right) const
342 pi_.pain.line(pi_.base.dottedLineThickness(), yo_ - row_.ascent(),
343 pi_.base.dottedLineThickness(), yo_ + row_.descent(),
344 Color_scroll, Painter::line_onoffdash,
345 pi_.base.dottedLineThickness());
348 pi_.base.bv->workWidth() - pi_.base.dottedLineThickness();
349 pi_.pain.line(wwidth, yo_ - row_.ascent(),
350 wwidth, yo_ + row_.descent(),
351 Color_scroll, Painter::line_onoffdash,
352 pi_.base.dottedLineThickness());
357 void RowPainter::paintFirst() const
359 Layout const & layout = par_.layout();
361 // start of appendix?
362 if (par_.params().startOfAppendix())
363 paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
365 bool const is_first =
366 text_.isFirstInSequence(row_.pit()) || !layout.isParagraphGroup();
367 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
369 if (layout.labelIsInline()
370 && (layout.labeltype != LABEL_STATIC || is_first))
372 else if (is_first && layout.labelIsAbove())
373 paintTopLevelLabel();
377 void RowPainter::paintLabel() const
379 docstring const & str = par_.labelString();
383 Layout const & layout = par_.layout();
384 FontInfo const font = labelFont();
385 FontMetrics const & fm = theFontMetrics(font);
389 x = tm_.width() - row_.right_margin + fm.width(layout.labelsep);
391 x = x_ - fm.width(layout.labelsep) - fm.width(str);
393 pi_.pain.text(int(x), yo_, str, font);
397 void RowPainter::paintTopLevelLabel() const
399 BufferParams const & bparams = pi_.base.bv->buffer().params();
400 ParagraphParameters const & pparams = par_.params();
401 Layout const & layout = par_.layout();
402 FontInfo const font = labelFont();
403 docstring const str = par_.labelString();
407 double spacing_val = 1.0;
408 if (!pparams.spacing().isDefault())
409 spacing_val = pparams.spacing().getValue();
411 spacing_val = bparams.spacing().getValue();
413 FontMetrics const & fm = theFontMetrics(font);
415 int const labeladdon = int(fm.maxHeight()
416 * layout.spacing.getValue() * spacing_val);
419 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
420 + (layout.labelbottomsep * defaultRowHeight()));
423 if (layout.labeltype == LABEL_CENTERED) {
424 x = row_.left_margin + (tm_.width() - row_.left_margin - row_.right_margin) / 2;
425 x -= fm.width(str) / 2;
426 } else if (row_.isRTL()) {
427 x = tm_.width() - row_.right_margin - fm.width(str);
429 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
433 /** Check if the current paragraph is the last paragraph in a
435 static int getEndLabel(pit_type p, Text const & text)
437 ParagraphList const & pars = text.paragraphs();
439 depth_type par_depth = pars[p].getDepth();
440 while (pit != pit_type(pars.size())) {
441 Layout const & layout = pars[pit].layout();
442 int const endlabeltype = layout.endlabeltype;
444 if (endlabeltype != END_LABEL_NO_LABEL) {
445 if (p + 1 == pit_type(pars.size()))
448 depth_type const next_depth =
449 pars[p + 1].getDepth();
450 if (par_depth > next_depth ||
451 (par_depth == next_depth && layout != pars[p + 1].layout()))
457 pit = text.outerHook(pit);
458 if (pit != pit_type(pars.size()))
459 par_depth = pars[pit].getDepth();
461 return END_LABEL_NO_LABEL;
465 void RowPainter::paintLast() const
467 int const endlabel = getEndLabel(row_.pit(), text_);
469 // paint imaginary end-of-paragraph character
471 Change const & change = par_.lookupChange(par_.size());
472 if (change.changed()) {
473 FontMetrics const & fm =
474 theFontMetrics(pi_.base.bv->buffer().params().getFont());
475 int const length = fm.maxAscent() / 2;
476 Color col = change.color();
478 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
479 Painter::line_solid, 3);
481 if (change.deleted()) {
482 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
483 yo_ + 2, col, Painter::line_solid, 3);
485 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
486 yo_ + 2, col, Painter::line_solid, 3);
494 case END_LABEL_FILLED_BOX: {
495 FontInfo const font = labelFont();
496 FontMetrics const & fm = theFontMetrics(font);
497 int const size = int(0.75 * fm.maxAscent());
498 int const y = yo_ - size;
500 // If needed, move the box a bit to avoid overlapping with text.
503 int const normal_x = nestMargin() + changebarMargin();
504 x = min(normal_x, row_.left_margin - size - Inset::TEXT_TO_INSET_OFFSET);
506 int const normal_x = tm_.width() - row_.right_margin
507 - size - Inset::TEXT_TO_INSET_OFFSET;
508 x = max(normal_x, row_.width());
511 if (endlabel == END_LABEL_BOX)
512 pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
514 pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
518 case END_LABEL_STATIC: {
519 FontInfo const font = labelFont();
520 FontMetrics const & fm = theFontMetrics(font);
521 docstring const & str = par_.layout().endlabelstring();
522 double const x = row_.isRTL() ? x_ - fm.width(str) : x_;
523 pi_.pain.text(int(x), yo_, str, font);
527 case END_LABEL_NO_LABEL:
533 void RowPainter::paintOnlyInsets()
535 Row::const_iterator cit = row_.begin();
536 Row::const_iterator const & end = row_.end();
537 for ( ; cit != end ; ++cit) {
538 Row::Element const & e = *cit;
539 if (e.type == Row::INSET) {
541 // The line that indicates word in a different language
543 // change tracking (not for insets that handle it themselves)
544 if (!e.inset->canPaintChange(*pi_.base.bv))
548 x_ += e.full_width();
553 void RowPainter::paintText()
555 Row::const_iterator cit = row_.begin();
556 Row::const_iterator const & end = row_.end();
557 for ( ; cit != end ; ++cit) {
558 Row::Element const & e = *cit;
563 paintStringAndSel(e);
565 // Paint the spelling marks if enabled.
566 if (lyxrc.spellcheck_continuously && pi_.do_spellcheck && !pi_.pain.isNull())
567 paintMisspelledMark(e);
575 pi_.pain.textDecoration(e.font.fontInfo(), int(x_), yo_, int(e.full_width()));
578 // The line that indicates word in a different language
581 // change tracking (not for insets that handle it themselves)
582 if (e.type != Row::INSET || ! e.inset->canPaintChange(*pi_.base.bv))
585 x_ += e.full_width();
590 void RowPainter::paintSelection() const
592 if (!row_.selection())
595 int const y1 = yo_ - row_.ascent();
596 int const y2 = y1 + row_.height();
599 if (row_.isRTL() ? row_.end_margin_sel : row_.begin_margin_sel)
600 pi_.pain.fillRectangle(int(xo_), y1, row_.left_margin, y2 - y1,
603 // go through row and draw from RTL boundary to RTL boundary
604 double x = xo_ + row_.left_margin;
605 for (auto const & e : row_) {
606 // These are the same tests as in paintStringAndSel, except
607 // that all_sel has an additional clause that triggers for end
608 // of paragraph markers. The clause was not used in
609 // paintStringAndSel to avoid changing the drawing color.
610 // at least part of text selected?
611 bool const some_sel = (e.endpos >= row_.sel_beg && e.pos < row_.sel_end)
613 // all the text selected?
614 bool const all_sel = (e.pos >= row_.sel_beg && e.endpos < row_.sel_end)
615 || (e.isVirtual() && e.pos == row_.endpos() && row_.end_margin_sel)
619 // the 3rd argument is written like that to avoid rounding issues
620 pi_.pain.fillRectangle(int(x), y1,
621 int(x + e.full_width()) - int(x), y2 - y1,
623 } else if (some_sel) {
624 pos_type const from = min(max(row_.sel_beg, e.pos), e.endpos);
625 pos_type const to = max(min(row_.sel_end, e.endpos), e.pos);
626 double x1 = e.pos2x(from);
627 double x2 = e.pos2x(to);
630 // the 3rd argument is written like that to avoid rounding issues
631 pi_.pain.fillRectangle(int(x + x1), y1, int(x2 + x) - int(x1 + x),
632 y2 - y1, Color_selection);
637 if (row_.isRTL() ? row_.begin_margin_sel : row_.end_margin_sel)
638 pi_.pain.fillRectangle(int(x), y1,
639 int(xo_ + tm_.width()) - int(x), y2 - y1,