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 = pi_.base.bv->coordCache().insets().dim(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::paintForeignMark(double orig_x, Language const * lang, int desc) const
168 if (!lyxrc.mark_foreign_language)
170 if (lang == latex_language)
172 if (lang == pi_.base.bv->buffer().params().language)
175 int const y = yo_ + solid_line_offset_ + desc + solid_line_thickness_ / 2;
176 pi_.pain.line(int(orig_x), y, int(x_), y, Color_language,
177 Painter::line_solid, solid_line_thickness_);
181 void RowPainter::paintMisspelledMark(double const orig_x,
182 Row::Element const & e) const
184 // if changed the misspelled marker gets placed slightly lower than normal
185 // to avoid drawing at the same vertical offset
186 int const y = yo_ + solid_line_offset_ + solid_line_thickness_
187 + (e.change.changed() ? solid_line_thickness_ + 1 : 0)
188 + dotted_line_offset_;
190 //FIXME: this could be computed only once, it is probably not costly.
191 // check for cursor position
192 // don't draw misspelled marker for words at cursor position
193 // we don't want to disturb the process of text editing
194 DocIterator const nw = pi_.base.bv->cursor().newWord();
196 if (!nw.empty() && par_.id() == nw.paragraph().id()) {
198 if (cpos > 0 && cpos == par_.size() && !par_.isWordSeparator(cpos-1))
200 else if (cpos > 0 && par_.isWordSeparator(cpos))
204 pos_type pos = e.pos;
205 while (pos < e.pos + pos_type(e.str.length())) {
206 if (!par_.isMisspelled(pos)) {
211 FontSpan const & range = par_.getSpellRange(pos);
213 // Skip element which are being edited
214 if (range.contains(cpos)) {
215 // the range includes the last element
216 pos = range.last + 1;
220 FontMetrics const & fm = theFontMetrics(e.font);
221 int x1 = fm.pos2x(e.str, range.first - e.pos,
223 int x2 = fm.pos2x(e.str, min(range.last - e.pos + 1,
224 pos_type(e.str.length())),
229 pi_.pain.line(int(orig_x) + x1, y, int(orig_x) + x2, y,
231 Painter::line_onoffdash, dotted_line_thickness_);
232 pos = range.last + 1;
237 void RowPainter::paintStringAndSel(Row::Element const & e)
239 // at least part of text selected?
240 bool const some_sel = (e.endpos >= row_.sel_beg && e.pos < row_.sel_end)
242 // all the text selected?
243 bool const all_sel = (e.pos >= row_.sel_beg && e.endpos < row_.sel_end)
248 copy.fontInfo().setPaintColor(Color_selectiontext);
249 pi_.pain.text(int(x_), yo_, e.str, copy, e.extra);
250 } else if (e.change.changed()) {
252 copy.fontInfo().setPaintColor(e.change.color());
253 pi_.pain.text(int(x_), yo_, e.str, copy, e.extra);
254 } else if (!some_sel) {
255 pi_.pain.text(int(x_), yo_, e.str, e.font, e.extra);
257 pi_.pain.text(int(x_), yo_, e.str, e.font, Color_selectiontext,
258 max(row_.sel_beg, e.pos) - e.pos,
259 min(row_.sel_end, e.endpos) - e.pos, e.extra);
261 x_ += e.full_width();
265 void RowPainter::paintChange(double orig_x, Font const & font,
266 Change const & change) const
268 if (!change.changed())
270 // Calculate 1/3 height of font
271 FontMetrics const & fm = theFontMetrics(font);
272 int const y_bar = change.deleted() ? yo_ - fm.maxAscent() / 3
273 : yo_ + 2 * solid_line_offset_ + solid_line_thickness_;
274 pi_.pain.line(int(orig_x), y_bar, int(x_), y_bar,
275 change.color(), Painter::line_solid, solid_line_thickness_);
279 void RowPainter::paintChangeBar() const
281 pos_type const start = row_.pos();
282 pos_type end = row_.endpos();
284 if (par_.size() == end) {
285 // this is the last row of the paragraph;
286 // thus, we must also consider the imaginary end-of-par character
290 if (start == end || !par_.isChanged(start, end))
293 int const height = text_metrics_.isLastRow(pit_, row_)
297 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
301 void RowPainter::paintAppendix() const
303 // only draw the appendix frame once (for the main text)
304 if (!par_.params().appendix() || !text_.isMainText())
307 int y = yo_ - row_.ascent();
309 if (par_.params().startOfAppendix())
310 y += 2 * defaultRowHeight();
312 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
313 pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
317 void RowPainter::paintDepthBar() const
319 depth_type const depth = par_.getDepth();
324 depth_type prev_depth = 0;
325 if (!text_metrics_.isFirstRow(pit_, row_)) {
326 pit_type pit2 = pit_;
329 prev_depth = pars_[pit2].getDepth();
332 depth_type next_depth = 0;
333 if (!text_metrics_.isLastRow(pit_, row_)) {
334 pit_type pit2 = pit_;
335 if (row_.endpos() >= pars_[pit2].size())
337 next_depth = pars_[pit2].getDepth();
340 for (depth_type i = 1; i <= depth; ++i) {
341 int const w = nestMargin() / 5;
342 int x = int(xo_) + w * i;
343 // only consider the changebar space if we're drawing outermost text
344 if (text_.isMainText())
345 x += changebarMargin();
347 int const starty = yo_ - row_.ascent();
348 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
350 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
353 pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
355 pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
360 void RowPainter::paintAppendixStart(int y) const
362 FontInfo pb_font = sane_font;
363 pb_font.setColor(Color_appendix);
370 docstring const label = _("Appendix");
371 theFontMetrics(pb_font).rectText(label, w, a, d);
373 int const text_start = int(xo_ + (width_ - w) / 2);
374 int const text_end = text_start + w;
376 pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
378 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
379 pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
383 void RowPainter::paintTooLargeMarks(bool const left, bool const right) const
386 pi_.pain.line(dotted_line_thickness_, yo_ - row_.ascent(),
387 dotted_line_thickness_, yo_ + row_.descent(),
389 Painter::line_onoffdash, dotted_line_thickness_);
391 int const wwidth = pi_.base.bv->workWidth() - dotted_line_thickness_;
392 pi_.pain.line(wwidth, yo_ - row_.ascent(),
393 wwidth, yo_ + row_.descent(),
395 Painter::line_onoffdash, dotted_line_thickness_);
400 void RowPainter::paintFirst() const
402 Layout const & layout = par_.layout();
404 // start of appendix?
405 if (par_.params().startOfAppendix())
406 paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
408 bool const is_first =
409 text_.isFirstInSequence(pit_) || !layout.isParagraphGroup();
410 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
412 if (layout.labelIsInline()
413 && (layout.labeltype != LABEL_STATIC || is_first))
415 else if (is_first && layout.labelIsAbove())
416 paintTopLevelLabel();
420 void RowPainter::paintLabel() const
422 docstring const str = par_.labelString();
426 bool const is_rtl = text_.isRTL(par_);
427 Layout const & layout = par_.layout();
428 FontInfo const font = labelFont();
429 FontMetrics const & fm = theFontMetrics(font);
433 x = width_ - row_.right_margin + fm.width(layout.labelsep);
435 x = x_ - fm.width(layout.labelsep) - fm.width(str);
437 pi_.pain.text(int(x), yo_, str, font);
441 void RowPainter::paintTopLevelLabel() const
443 BufferParams const & bparams = pi_.base.bv->buffer().params();
444 bool const is_rtl = text_.isRTL(par_);
445 ParagraphParameters const & pparams = par_.params();
446 Layout const & layout = par_.layout();
447 FontInfo const font = labelFont();
448 docstring const str = par_.labelString();
452 double spacing_val = 1.0;
453 if (!pparams.spacing().isDefault())
454 spacing_val = pparams.spacing().getValue();
456 spacing_val = bparams.spacing().getValue();
458 FontMetrics const & fm = theFontMetrics(font);
460 int const labeladdon = int(fm.maxHeight()
461 * layout.spacing.getValue() * spacing_val);
464 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
465 + (layout.labelbottomsep * defaultRowHeight()));
468 if (layout.labeltype == LABEL_CENTERED) {
469 x = row_.left_margin + (width_ - row_.left_margin - row_.right_margin) / 2;
470 x -= fm.width(str) / 2;
472 x = width_ - row_.right_margin - fm.width(str);
474 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
478 /** Check if the current paragraph is the last paragraph in a
480 static int getEndLabel(pit_type p, Text const & text)
482 ParagraphList const & pars = text.paragraphs();
484 depth_type par_depth = pars[p].getDepth();
485 while (pit != pit_type(pars.size())) {
486 Layout const & layout = pars[pit].layout();
487 int const endlabeltype = layout.endlabeltype;
489 if (endlabeltype != END_LABEL_NO_LABEL) {
490 if (p + 1 == pit_type(pars.size()))
493 depth_type const next_depth =
494 pars[p + 1].getDepth();
495 if (par_depth > next_depth ||
496 (par_depth == next_depth && layout != pars[p + 1].layout()))
502 pit = text.outerHook(pit);
503 if (pit != pit_type(pars.size()))
504 par_depth = pars[pit].getDepth();
506 return END_LABEL_NO_LABEL;
510 void RowPainter::paintLast()
512 bool const is_rtl = text_.isRTL(par_);
513 int const endlabel = getEndLabel(pit_, text_);
515 // paint imaginary end-of-paragraph character
517 Change const & change = par_.lookupChange(par_.size());
518 if (change.changed()) {
519 FontMetrics const & fm =
520 theFontMetrics(pi_.base.bv->buffer().params().getFont());
521 int const length = fm.maxAscent() / 2;
522 Color col = change.color();
524 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
525 Painter::line_solid, 3);
527 if (change.deleted()) {
528 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
529 yo_ + 2, col, Painter::line_solid, 3);
531 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
532 yo_ + 2, col, Painter::line_solid, 3);
540 case END_LABEL_FILLED_BOX: {
541 FontInfo const font = labelFont();
542 FontMetrics const & fm = theFontMetrics(font);
543 int const size = int(0.75 * fm.maxAscent());
544 int const y = yo_ - size;
545 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
546 int x = is_rtl ? nestMargin() + changebarMargin()
547 : max_row_width - row_.right_margin;
549 // If needed, move the box a bit to avoid overlapping with text.
550 int const rem = max_row_width - row_.width();
552 x += is_rtl ? rem : - rem;
554 if (endlabel == END_LABEL_BOX)
555 pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
557 pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
561 case END_LABEL_STATIC: {
562 FontInfo const font = labelFont();
563 FontMetrics const & fm = theFontMetrics(font);
564 docstring const & str = par_.layout().endlabelstring();
565 double const x = is_rtl ? x_ - fm.width(str) : x_;
566 pi_.pain.text(int(x), yo_, str, font);
570 case END_LABEL_NO_LABEL:
576 void RowPainter::paintOnlyInsets()
578 Row::const_iterator cit = row_.begin();
579 Row::const_iterator const & end = row_.end();
580 for ( ; cit != end ; ++cit) {
581 Row::Element const & e = *cit;
582 if (e.type == Row::INSET) {
583 // If outer row has changed, nested insets are repainted completely.
584 pi_.base.bv->coordCache().insets().add(e.inset, int(x_), yo_);
585 bool const nested_inset =
586 (e.inset->asInsetMath() && !e.inset->asInsetMath()->asMacroTemplate())
587 || e.inset->asInsetText() || e.inset->asInsetTabular();
589 x_ += e.full_width();
592 paintInset(e.inset, e.font, e.change, e.pos);
594 x_ += e.full_width();
599 void RowPainter::paintText()
601 Row::const_iterator cit = row_.begin();
602 Row::const_iterator const & end = row_.end();
603 for ( ; cit != end ; ++cit) {
604 double const orig_x = x_;
605 Row::Element const & e = *cit;
606 int foreign_descent = 0;
611 paintStringAndSel(e);
613 // Paint the spelling marks if enabled.
614 if (lyxrc.spellcheck_continuously && pi_.do_spellcheck)
615 paintMisspelledMark(orig_x, e);
618 // If outer row has changed, nested insets are repaint completely.
619 pi_.base.bv->coordCache().insets().add(e.inset, int(x_), yo_);
620 paintInset(e.inset, e.font, e.change, e.pos);
621 foreign_descent = e.dim.descent();
625 pi_.pain.textDecoration(e.font.fontInfo(), int(x_), yo_, int(e.full_width()));
626 x_ += e.full_width();
629 // The line that indicates word in a different language
630 paintForeignMark(orig_x, e.font.language(), foreign_descent);
632 // change tracking (not for insets that track their own changes)
633 if (e.type != Row::INSET || ! e.inset->canTrackChanges())
634 paintChange(orig_x, e.font, e.change);
639 void RowPainter::paintSelection() const
641 if (!row_.selection())
643 Cursor const & curs = pi_.base.bv->cursor();
644 DocIterator beg = curs.selectionBegin();
646 beg.pos() = row_.sel_beg;
648 DocIterator end = curs.selectionEnd();
650 end.pos() = row_.sel_end;
652 bool const begin_boundary = beg.pos() >= row_.endpos();
653 bool const end_boundary = row_.sel_end == row_.endpos();
655 DocIterator cur = beg;
656 cur.boundary(begin_boundary);
657 int x1 = text_metrics_.cursorX(beg.top(), begin_boundary);
658 int x2 = text_metrics_.cursorX(end.top(), end_boundary);
659 int const y1 = yo_ - row_.ascent();
660 int const y2 = y1 + row_.height();
662 int const rm = text_.isMainText() ? pi_.base.bv->rightMargin() : 0;
663 int const lm = text_.isMainText() ? pi_.base.bv->leftMargin() : 0;
666 if (row_.begin_margin_sel) {
667 if (text_.isRTL(beg.paragraph())) {
668 pi_.pain.fillRectangle(int(xo_ + x1), y1,
669 text_metrics_.width() - rm - x1, y2 - y1, Color_selection);
671 pi_.pain.fillRectangle(int(xo_ + lm), y1, x1 - lm, y2 - y1,
676 if (row_.end_margin_sel) {
677 if (text_.isRTL(beg.paragraph())) {
678 pi_.pain.fillRectangle(int(xo_ + lm), y1, x2 - lm, y2 - y1,
681 pi_.pain.fillRectangle(int(xo_ + x2), y1, text_metrics_.width() - rm - x2,
682 y2 - y1, Color_selection);
686 // if we are on a boundary from the beginning, it's probably
687 // a RTL boundary and we jump to the other side directly as this
688 // segement is 0-size and confuses the logic below
692 // go through row and draw from RTL boundary to RTL boundary
694 bool draw_now = false;
696 // simplified cursorForward code below which does not
697 // descend into insets and which does not go into the
698 // next line. Compare the logic with the original cursorForward
700 // if left of boundary -> just jump to right side, but
701 // for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
702 if (cur.boundary()) {
704 } else if (text_metrics_.isRTLBoundary(cur.pit(), cur.pos() + 1)) {
705 // in front of RTL boundary -> Stay on this side of the boundary
706 // because: ab|cDDEEFFghi -> abc|DDEEFFghi
715 if (cur.pos() == row_.endpos())
720 // the previous segment was just drawn, now the next starts
721 x1 = text_metrics_.cursorX(cur.top(), cur.boundary());
724 if (!(cur < end) || draw_now) {
725 x2 = text_metrics_.cursorX(cur.top(), cur.boundary());
726 pi_.pain.fillRectangle(int(xo_ + min(x1, x2)), y1, abs(x2 - x1),
727 y2 - y1, Color_selection);
729 // reset x1, so it is set again next round (which will be on the
730 // right side of a boundary or at the selection end)