]> git.lyx.org Git - lyx.git/blob - src/RowPainter.cpp
2216b2107781046cf0548952de937380871add98
[lyx.git] / src / RowPainter.cpp
1 /**
2  * \file RowPainter.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author various
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13 #include <algorithm>
14
15 #include "RowPainter.h"
16
17 #include "Buffer.h"
18 #include "CoordCache.h"
19 #include "Cursor.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
22 #include "Changes.h"
23 #include "Language.h"
24 #include "Layout.h"
25 #include "LyXRC.h"
26 #include "Row.h"
27 #include "MetricsInfo.h"
28 #include "Paragraph.h"
29 #include "ParagraphParameters.h"
30 #include "TextMetrics.h"
31 #include "VSpace.h"
32
33 #include "frontends/FontMetrics.h"
34 #include "frontends/Painter.h"
35
36 #include "insets/InsetText.h"
37
38 #include "mathed/InsetMath.h"
39
40 #include "support/debug.h"
41 #include "support/gettext.h"
42 #include "support/textutils.h"
43
44 #include "support/lassert.h"
45 #include <boost/crc.hpp>
46
47 #include <stdlib.h>
48
49 using namespace std;
50
51 namespace lyx {
52
53 using frontend::Painter;
54 using frontend::FontMetrics;
55
56
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()]),
63           change_(pi_.change_),
64           xo_(x), yo_(y)
65 {
66         x_ = row_.left_margin + xo_;
67
68         //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
69         //row_.dump();
70
71         LBUFERR(row.pit() >= 0);
72         LBUFERR(row.pit() < int(text.paragraphs().size()));
73 }
74
75
76 FontInfo RowPainter::labelFont(bool end) const
77 {
78         FontInfo f = text_.labelFont(par_);
79         // selected text?
80         if ((end ? row_.end_margin_sel : row_.begin_margin_sel)
81             || pi_.selected)
82                 f.setPaintColor(Color_selectiontext);
83         return f;
84 }
85
86
87 // If you want to debug inset metrics uncomment the following line:
88 //#define DEBUG_METRICS
89 // This draws green lines around each inset.
90
91
92 void RowPainter::paintInset(Row::Element const & e) const
93 {
94         // Handle selection
95         bool const pi_selected = pi_.selected;
96         Cursor const & cur = pi_.base.bv->cursor();
97         if (cur.selection() && cur.text() == &text_
98                 && cur.normalAnchor().text() == &text_)
99                 pi_.selected = row_.sel_beg <= e.pos && row_.sel_end > e.pos;
100
101         LASSERT(e.inset, return);
102         // Backup full_repaint status because some insets (InsetTabular)
103         // requires a full repaint
104         bool const pi_full_repaint = pi_.full_repaint;
105         bool const pi_do_spellcheck = pi_.do_spellcheck;
106         Change const pi_change = pi_.change_;
107
108         pi_.base.font = e.inset->inheritFont() ? e.font.fontInfo() :
109                 pi_.base.bv->buffer().params().getFont().fontInfo();
110         pi_.ltr_pos = !e.font.isVisibleRightToLeft();
111         pi_.change_ = change_.changed() ? change_ : e.change;
112         pi_.do_spellcheck &= e.inset->allowSpellCheck();
113
114         int const x1 = int(x_);
115         pi_.base.bv->coordCache().insets().add(e.inset, x1, yo_);
116         // insets are painted completely. Recursive
117         // FIXME: it is wrong to completely paint the background
118         // if we want to do single row painting.
119         e.inset->drawBackground(pi_, x1, yo_);
120         e.inset->drawSelection(pi_, x1, yo_);
121         e.inset->draw(pi_, x1, yo_);
122         paintTextDecoration(e);
123
124         // Restore full_repaint status.
125         pi_.full_repaint = pi_full_repaint;
126         pi_.change_ = pi_change;
127         pi_.do_spellcheck = pi_do_spellcheck;
128         pi_.selected = pi_selected;
129
130 #ifdef DEBUG_METRICS
131         Dimension const & dim = pi_.base.bv->coordCache().insets().dim(e.inset);
132         int const x2 = x1 + dim.wid;
133         int const y1 = yo_ + dim.des;
134         int const y2 = yo_ - dim.asc;
135         pi_.pain.line(x1, y1, x1, y2, Color_green);
136         pi_.pain.line(x1, y1, x2, y1, Color_green);
137         pi_.pain.line(x2, y1, x2, y2, Color_green);
138         pi_.pain.line(x1, y2, x2, y2, Color_green);
139 #endif
140 }
141
142
143 void RowPainter::paintLanguageMarkings(Row::Element const & e) const
144 {
145         paintForeignMark(e);
146         paintNoSpellingMark(e);
147 }
148
149
150 void RowPainter::paintForeignMark(Row::Element const & e) const
151 {
152         Language const * lang = e.font.language();
153         if (!lyxrc.mark_foreign_language)
154                 return;
155         if (lang == latex_language)
156                 return;
157         if (lang == pi_.base.bv->buffer().params().language)
158                 return;
159
160         int const desc = e.inset ? e.dim.descent() : 0;
161         int const y = yo_ + min(3 * pi_.base.solidLineOffset() / 2 + desc,
162                                 row_.descent() - 1);
163         pi_.pain.line(int(x_), y, int(x_ + e.full_width() - 1), y, Color_language,
164                       Painter::line_solid, pi_.base.solidLineThickness());
165 }
166
167
168 void RowPainter::paintNoSpellingMark(Row::Element const & e) const
169 {
170         //if (!lyxrc.mark_no_spelling)
171         //      return;
172         if (e.font.language() == latex_language)
173                 return;
174         if (e.font.fontInfo().nospellcheck() != FONT_ON)
175                 return;
176
177         // We at the same voffset than the misspelled mark, since
178         // these two are mutually exclusive
179         int const desc = e.inset ? e.dim.descent() : 0;
180         int const y = yo_ + pi_.base.solidLineOffset() + desc
181                 + pi_.base.solidLineThickness()
182                 + (e.change.changed() ? pi_.base.solidLineThickness() + 1 : 0)
183                 + 1;
184         pi_.pain.line(int(x_), y, int(x_ + e.full_width()), y, Color_language,
185                       Painter::line_onoffdash, pi_.base.solidLineThickness());
186 }
187
188
189 void RowPainter::paintMisspelledMark(Row::Element const & e) const
190 {
191         if (e.font.fontInfo().nospellcheck() == FONT_ON)
192                 return;
193         // if changed the misspelled marker gets placed slightly lower than normal
194         // to avoid drawing at the same vertical offset
195         FontMetrics const & fm = theFontMetrics(e.font);
196         int const thickness = max(fm.lineWidth(), 2);
197         int const y = yo_ + pi_.base.solidLineOffset() + pi_.base.solidLineThickness()
198                 + (e.change.changed() ? pi_.base.solidLineThickness() + 1 : 0)
199                 + 1 + thickness / 2;
200
201         //FIXME: this could be computed only once, it is probably not costly.
202         // check for cursor position
203         // don't draw misspelled marker for words at cursor position
204         // we don't want to disturb the process of text editing
205         DocIterator const nw = pi_.base.bv->cursor().newWord();
206         pos_type cpos = -1;
207         if (!nw.empty() && par_.id() == nw.paragraph().id()) {
208                 cpos = nw.pos();
209                 if (cpos > 0 && cpos == par_.size() && !par_.isWordSeparator(cpos-1))
210                         --cpos;
211                 else if (cpos > 0 && par_.isWordSeparator(cpos))
212                         --cpos;
213         }
214
215         pos_type pos = e.pos;
216         while (pos < e.pos + pos_type(e.str.length())) {
217                 if (!par_.isMisspelled(pos)) {
218                         ++pos;
219                         continue;
220                 }
221
222                 FontSpan const & range = par_.getSpellRange(pos);
223
224                 // Skip element which are being edited
225                 if (range.contains(cpos)) {
226                         // the range includes the last element
227                         pos = range.last + 1;
228                         continue;
229                 }
230
231                 int x1 = fm.pos2x(e.str, range.first - e.pos,
232                                   e.isRTL(), e.extra);
233                 int x2 = fm.pos2x(e.str, min(range.last - e.pos + 1,
234                                                                          pos_type(e.str.length())),
235                                                                          e.isRTL(), e.extra);
236                 if (x1 > x2)
237                         swap(x1, x2);
238
239                 pi_.pain.line(int(x_ + x1), y, int(x_ + x2), y,
240                               Color_error,
241                               Painter::line_onoffdash, thickness);
242                 pos = range.last + 1;
243         }
244 }
245
246
247 void RowPainter::paintStringAndSel(Row::Element const & e) const
248 {
249         // at least part of text selected?
250         bool const some_sel = (e.endpos >= row_.sel_beg && e.pos < row_.sel_end)
251                 || pi_.selected;
252         // all the text selected?
253         bool const all_sel = (e.pos >= row_.sel_beg && e.endpos < row_.sel_end)
254                 || pi_.selected;
255
256         if (all_sel || e.change.changed()) {
257                 Font copy = e.font;
258                 Color const col = e.change.changed() ? e.change.color()
259                                                      : Color_selectiontext;
260                 copy.fontInfo().setPaintColor(col);
261                 pi_.pain.text(int(x_), yo_, e.str, copy, e.extra, e.full_width());
262         } else if (!some_sel) {
263                 pi_.pain.text(int(x_), yo_, e.str, e.font, e.extra, e.full_width());
264         } else {
265                 pi_.pain.text(int(x_), yo_, e.str, e.font, Color_selectiontext,
266                               max(row_.sel_beg, e.pos) - e.pos,
267                               min(row_.sel_end, e.endpos) - e.pos,
268                               e.extra, e.full_width());
269         }
270 }
271
272
273 void RowPainter::paintTextDecoration(Row::Element const & e) const
274 {
275         // element selected?
276         bool const sel = (e.pos >= row_.sel_beg && e.endpos <= row_.sel_end)
277                 || pi_.selected;
278         FontInfo copy = e.font.fontInfo();
279         if (sel || e.change.changed()) {
280                 Color const col = e.change.changed() ? e.change.color()
281                                                      : Color_selectiontext;
282                 copy.setPaintColor(col);
283         }
284         pi_.pain.textDecoration(copy, int(x_), yo_, int(e.full_width()));
285 }
286
287
288 void RowPainter::paintChange(Row::Element const & e) const
289 {
290         e.change.paintCue(pi_, x_, yo_, x_ + e.full_width(), e.font.fontInfo());
291 }
292
293
294 void RowPainter::paintChangeBar() const
295 {
296         pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, row_.height(), Color_changebar);
297 }
298
299
300 void RowPainter::paintAppendix() const
301 {
302         // only draw the appendix frame once (for the main text)
303         if (!par_.params().appendix() || !text_.isMainText())
304                 return;
305
306         int y = yo_ - row_.ascent();
307
308         if (par_.params().startOfAppendix())
309                 y += 2 * defaultRowHeight();
310
311         pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
312         pi_.pain.line(tm_.width() - 2, y, tm_.width() - 2, yo_ + row_.height(), Color_appendix);
313 }
314
315
316 void RowPainter::paintDepthBar() const
317 {
318         depth_type const depth = par_.getDepth();
319
320         if (depth <= 0)
321                 return;
322
323         depth_type prev_depth = 0;
324         if (!tm_.isFirstRow(row_)) {
325                 pit_type pit2 = row_.pit();
326                 if (row_.pos() == 0)
327                         --pit2;
328                 prev_depth = pars_[pit2].getDepth();
329         }
330
331         depth_type next_depth = 0;
332         if (!tm_.isLastRow(row_)) {
333                 pit_type pit2 = row_.pit();
334                 if (row_.endpos() >= pars_[pit2].size())
335                         ++pit2;
336                 next_depth = pars_[pit2].getDepth();
337         }
338
339         for (depth_type i = 1; i <= depth; ++i) {
340                 int const w = nestMargin() / 5;
341                 int x = int(xo_) + w * i;
342                 // consider the bufferview left margin if we're drawing outermost text
343                 if (text_.isMainText())
344                         x += pi_.base.bv->leftMargin();
345
346                 int const starty = yo_ - row_.ascent();
347                 int const h =  row_.height() - 1 - (i - next_depth - 1) * 3;
348
349                 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
350
351                 if (i > prev_depth)
352                         pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
353                 if (i > next_depth)
354                         pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
355         }
356 }
357
358
359 void RowPainter::paintAppendixStart(int y) const
360 {
361         FontInfo pb_font = sane_font;
362         pb_font.setColor(Color_appendix);
363         pb_font.decSize();
364
365         int w = 0;
366         int a = 0;
367         int d = 0;
368
369         docstring const label = _("Appendix");
370         theFontMetrics(pb_font).rectText(label, w, a, d);
371
372         int const text_start = int(xo_ + (tm_.width() - w) / 2);
373         int const text_end = text_start + w;
374
375         pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
376
377         pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
378         pi_.pain.line(text_end, y, int(xo_ + tm_.width() - 2), y, Color_appendix);
379 }
380
381
382 void RowPainter::paintTooLargeMarks(bool const left, bool const right) const
383 {
384         if (left)
385                 pi_.pain.line(pi_.base.dottedLineThickness(), yo_ - row_.ascent(),
386                                           pi_.base.dottedLineThickness(), yo_ + row_.descent(),
387                                           Color_scroll, Painter::line_onoffdash,
388                               pi_.base.dottedLineThickness());
389         if (right) {
390                 int const wwidth =
391                         pi_.base.bv->workWidth() - pi_.base.dottedLineThickness();
392                 pi_.pain.line(wwidth, yo_ - row_.ascent(),
393                                           wwidth, yo_ + row_.descent(),
394                                           Color_scroll, Painter::line_onoffdash,
395                               pi_.base.dottedLineThickness());
396         }
397 }
398
399
400 void RowPainter::paintFirst() const
401 {
402         Layout const & layout = par_.layout();
403
404         // start of appendix?
405         if (par_.params().startOfAppendix())
406             paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
407
408         bool const is_first =
409                 text_.isFirstInSequence(row_.pit()) || !layout.isParagraphGroup();
410         //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
411
412         if (layout.labelIsInline()
413             && (layout.labeltype != LABEL_STATIC || is_first))
414                 paintLabel();
415         else if (is_first && layout.labelIsAbove())
416                 paintTopLevelLabel();
417 }
418
419
420 void RowPainter::paintLabel() const
421 {
422         docstring const & str = par_.labelString();
423         if (str.empty())
424                 return;
425
426         Layout const & layout = par_.layout();
427         FontInfo const font = labelFont(false);
428         FontMetrics const & fm = theFontMetrics(font);
429         int const x = row_.isRTL() ? row_.width() + fm.width(layout.labelsep)
430                                    : row_.left_margin - fm.width(layout.labelsep) - fm.width(str);
431
432         pi_.pain.text(int(xo_) + x, yo_, str, font);
433 }
434
435
436 void RowPainter::paintTopLevelLabel() const
437 {
438         BufferParams const & bparams = pi_.base.bv->buffer().params();
439         ParagraphParameters const & pparams = par_.params();
440         Layout const & layout = par_.layout();
441         FontInfo const font = labelFont(false);
442         docstring const str = par_.labelString();
443         if (str.empty())
444                 return;
445
446         double spacing_val = 1.0;
447         if (!pparams.spacing().isDefault())
448                 spacing_val = pparams.spacing().getValue();
449         else
450                 spacing_val = bparams.spacing().getValue();
451
452         FontMetrics const & fm = theFontMetrics(font);
453
454         int const labeladdon = int(fm.maxHeight()
455                 * layout.spacing.getValue() * spacing_val);
456
457         int maxdesc =
458                 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
459                 + (layout.labelbottomsep * defaultRowHeight()));
460
461         double x = x_;
462         if (layout.labeltype == LABEL_CENTERED) {
463                 x += (tm_.width() - row_.left_margin - row_.right_margin) / 2;
464                 x -= fm.width(str) / 2;
465         } else if (row_.isRTL()) {
466                 x = xo_ + tm_.width() - row_.right_margin - fm.width(str);
467         }
468         pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
469 }
470
471
472 void RowPainter::paintLast() const
473 {
474         int const endlabel = text_.getEndLabel(row_.pit());
475         switch (endlabel) {
476         case END_LABEL_BOX:
477         case END_LABEL_FILLED_BOX: {
478                 FontInfo font = labelFont(true);
479                 if (font.realColor() != Color_selectiontext)
480                         font.setPaintColor(Color_eolmarker);
481                 FontMetrics const & fm = theFontMetrics(font);
482                 int const size = int(0.75 * fm.maxAscent());
483                 int const y = yo_ - size;
484
485                 // If needed, move the box a bit to avoid overlapping with text.
486                 int x = 0;
487                 if (row_.isRTL()) {
488                         int const normal_x = nestMargin() + changebarMargin();
489                         x = min(normal_x, row_.left_margin - size - Inset::TEXT_TO_INSET_OFFSET);
490                 } else {
491                         int const normal_x = tm_.width() - row_.right_margin
492                                 - size - Inset::TEXT_TO_INSET_OFFSET;
493                         x = max(normal_x, row_.width());
494                 }
495
496                 if (endlabel == END_LABEL_BOX)
497                         pi_.pain.rectangle(int(xo_) + x, y, size, size, font.realColor());
498                 else
499                         pi_.pain.fillRectangle(int(xo_) + x, y, size, size, font.realColor());
500                 break;
501         }
502
503         case END_LABEL_STATIC: {
504                 FontInfo const font = labelFont(true);
505                 FontMetrics const & fm = theFontMetrics(font);
506                 docstring const & str = par_.layout().endlabelstring();
507                 double const x = row_.isRTL() ? x_ - fm.width(str) : x_;
508                 pi_.pain.text(int(x), yo_, str, font);
509                 break;
510         }
511
512         case END_LABEL_NO_LABEL:
513                 break;
514         }
515 }
516
517
518 void RowPainter::paintOnlyInsets()
519 {
520         for (Row::Element const & e : row_) {
521                 if (e.type == Row::INSET) {
522                         paintInset(e);
523                         // The markings of foreign languages
524                         // and of text ignored for spellchecking
525                         paintLanguageMarkings(e);
526                         // change tracking (not for insets that handle it themselves)
527                         if (!e.inset->canPaintChange(*pi_.base.bv))
528                                 paintChange(e);
529                 }
530
531                 x_ += e.full_width();
532         }
533 }
534
535
536 void RowPainter::paintText()
537 {
538         for (Row::Element const & e : row_) {
539                 switch (e.type) {
540                 case Row::STRING:
541                 case Row::VIRTUAL:
542                         paintStringAndSel(e);
543
544                         // Paint the spelling marks if enabled.
545                         if (lyxrc.spellcheck_continuously && pi_.do_spellcheck && !pi_.pain.isNull())
546                                 paintMisspelledMark(e);
547                         break;
548
549                 case Row::INSET:
550                         paintInset(e);
551                         break;
552
553                 case Row::SPACE:
554                         paintTextDecoration(e);
555                 }
556
557                 // The markings of foreign languages
558                 // and of text ignored for spellchecking
559                 paintLanguageMarkings(e);
560
561                 // change tracking (not for insets that handle it themselves)
562                 if (e.type != Row::INSET || ! e.inset->canPaintChange(*pi_.base.bv))
563                         paintChange(e);
564
565                 x_ += e.full_width();
566         }
567 }
568
569
570 void RowPainter::paintSelection() const
571 {
572         if (!row_.selection())
573                 return;
574
575         int const y1 = yo_ - row_.ascent();
576         int const y2 = y1 + row_.height();
577
578         // draw the margins
579         if (row_.isRTL() ? row_.end_margin_sel : row_.begin_margin_sel)
580                 pi_.pain.fillRectangle(int(xo_), y1, row_.left_margin, y2 - y1,
581                                        Color_selection);
582
583         // go through row and draw from RTL boundary to RTL boundary
584         double x = xo_ + row_.left_margin;
585         for (auto const & e : row_) {
586                 // These are the same tests as in paintStringAndSel, except
587                 // that all_sel has an additional clause that triggers for end
588                 // of paragraph markers. The clause was not used in
589                 // paintStringAndSel to avoid changing the drawing color.
590                 // at least part of text selected?
591                 bool const some_sel = (e.endpos >= row_.sel_beg && e.pos < row_.sel_end)
592                         || pi_.selected;
593                 // all the text selected?
594                 bool const all_sel = (e.pos >= row_.sel_beg && e.endpos < row_.sel_end)
595                     || (e.isVirtual() && e.pos == row_.endpos() && row_.end_margin_sel)
596                     || pi_.selected;
597
598                 if (all_sel) {
599                         // the 3rd argument is written like that to avoid rounding issues
600                         pi_.pain.fillRectangle(int(x), y1,
601                                                int(x + e.full_width()) - int(x), y2 - y1,
602                                                Color_selection);
603                 } else if (some_sel) {
604                         pos_type const from = min(max(row_.sel_beg, e.pos), e.endpos);
605                         pos_type const to = max(min(row_.sel_end, e.endpos), e.pos);
606                         double x1 = e.pos2x(from);
607                         double x2 = e.pos2x(to);
608                         if (x1 > x2)
609                                 swap(x1, x2);
610                         // the 3rd argument is written like that to avoid rounding issues
611                         pi_.pain.fillRectangle(int(x + x1), y1, int(x2 + x) - int(x1 + x),
612                                                y2 - y1, Color_selection);
613                 }
614                 x += e.full_width();
615         }
616
617         if (row_.isRTL() ? row_.begin_margin_sel : row_.end_margin_sel)
618                 pi_.pain.fillRectangle(int(x), y1,
619                                        int(xo_ + tm_.width()) - int(x), y2 - y1,
620                                        Color_selection);
621
622 }
623
624
625 } // namespace lyx