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.
14 #include "rowpainter.h"
17 #include "coordcache.h"
20 #include "bufferparams.h"
21 #include "BufferView.h"
28 #include "lyxrow_funcs.h"
29 #include "metricsinfo.h"
30 #include "paragraph.h"
31 #include "paragraph_funcs.h"
32 #include "ParagraphParameters.h"
35 #include "frontends/font_metrics.h"
36 #include "frontends/nullpainter.h"
37 #include "frontends/Painter.h"
39 #include "insets/insettext.h"
41 #include "support/textutils.h"
55 * A class used for painting an individual row of text.
59 /// initialise and run painter
60 RowPainter(PainterInfo & pi, LyXText const & text,
61 pit_type pit, Row const & row, int x, int y);
63 // paint various parts
66 void paintChangeBar();
72 void paintForeignMark(double orig_x, LyXFont const & orig_font);
73 void paintHebrewComposeChar(lyx::pos_type & vpos);
74 void paintArabicComposeChar(lyx::pos_type & vpos);
75 void paintChars(lyx::pos_type & vpos, bool hebrew, bool arabic);
76 int paintAppendixStart(int y);
77 void paintFromPos(lyx::pos_type & vpos);
78 void paintInset(lyx::pos_type const pos);
80 /// return left margin
81 int leftMargin() const;
83 /// return the font at the given pos
84 LyXFont const getFont(lyx::pos_type pos) const;
86 /// return the label font for this row
87 LyXFont const getLabelFont() const;
89 /// return pixel width for the given pos
90 int singleWidth(lyx::pos_type pos) const;
91 int singleWidth(lyx::pos_type pos, char c) const;
93 /// bufferview to paint on
94 BufferView const & bv_;
99 /// LyXText for the row
100 LyXText const & text_;
101 ParagraphList & pars_;
108 Paragraph const & par_;
112 int const yo_; // current baseline
119 // Hack to get 1.4cvs working
124 RowPainter::RowPainter(PainterInfo & pi,
125 LyXText const & text, pit_type pit, Row const & row, int x, int y)
126 : bv_(*pi.base.bv), pain_(pi.pain), text_(text), pars_(text.paragraphs()),
127 row_(row), pit_(pit), par_(text.paragraphs()[pit]),
128 xo_(x), yo_(y), width_(text_.width()), font_(pi.base.font)
130 RowMetrics m = text_.computeRowMetrics(pit, row_);
133 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
136 separator_ = m.separator;
138 label_hfill_ = m.label_hfill;
140 BOOST_ASSERT(pit >= 0);
141 BOOST_ASSERT(pit < int(text.paragraphs().size()));
146 LyXFont const RowPainter::getFont(pos_type pos) const
149 lf.reduce(LyXFont(LyXFont::ALL_SANE));
150 lf.realize(text_.getFont(par_, pos));
155 int RowPainter::singleWidth(lyx::pos_type pos) const
157 return text_.singleWidth(par_, pos);
161 int RowPainter::singleWidth(lyx::pos_type pos, char c) const
163 LyXFont const & font = text_.getFont(par_, pos);
164 return text_.singleWidth(par_, pos, c, font);
168 LyXFont const RowPainter::getLabelFont() const
170 return text_.getLabelFont(par_);
174 int RowPainter::leftMargin() const
176 return text_.leftMargin(pit_, row_.pos());
180 void RowPainter::paintInset(pos_type const pos)
182 InsetBase const * inset = par_.getInset(pos);
184 PainterInfo pi(const_cast<BufferView *>(&bv_), pain_);
185 pi.base.font = getFont(pos);
186 pi.ltr_pos = (text_.bidi.level(pos) % 2 == 0);
187 theCoords.insets().add(inset, int(x_), yo_);
188 inset->drawSelection(pi, int(x_), yo_);
189 inset->draw(pi, int(x_), yo_);
190 x_ += inset->width();
194 void RowPainter::paintHebrewComposeChar(pos_type & vpos)
196 pos_type pos = text_.bidi.vis2log(vpos);
201 char c = par_.getChar(pos);
205 LyXFont const & font = getFont(pos);
206 int const width = font_metrics::width(c, font);
209 for (pos_type i = pos - 1; i >= 0; --i) {
211 if (!Encodings::IsComposeChar_hebrew(c)) {
212 if (IsPrintableNonspace(c)) {
213 int const width2 = singleWidth(i, c);
215 dx = (c == 'ø' || c == 'ã')
217 : (width2 - width) / 2;
224 pain_.text(int(x_) + dx, yo_, str, font);
228 void RowPainter::paintArabicComposeChar(pos_type & vpos)
230 pos_type pos = text_.bidi.vis2log(vpos);
234 char c = par_.getChar(pos);
235 c = par_.transformChar(c, pos);
239 LyXFont const & font = getFont(pos);
240 int const width = font_metrics::width(c, font);
243 for (pos_type i = pos - 1; i >= 0; --i) {
245 if (!Encodings::IsComposeChar_arabic(c)) {
246 if (IsPrintableNonspace(c)) {
247 int const width2 = singleWidth(i, c);
248 dx = (width2 - width) / 2;
254 pain_.text(int(x_) + dx, yo_, str, font);
258 void RowPainter::paintChars(pos_type & vpos, bool hebrew, bool arabic)
260 pos_type pos = text_.bidi.vis2log(vpos);
261 pos_type const end = row_.endpos();
262 LyXFont orig_font = getFont(pos);
266 str += par_.getChar(pos);
268 unsigned char c = str[0];
269 str[0] = par_.transformChar(c, pos);
272 bool prev_struckout = isDeletedText(par_, pos);
273 bool prev_newtext = isInsertedText(par_, pos);
275 // collect as much similar chars as we can
276 for (++vpos; vpos < end && (pos = text_.bidi.vis2log(vpos)) >= 0; ++vpos) {
277 char c = par_.getChar(pos);
279 if (!IsPrintableNonspace(c))
282 if (prev_struckout != isDeletedText(par_, pos))
285 if (prev_newtext != isInsertedText(par_, pos))
288 if (arabic && Encodings::IsComposeChar_arabic(c))
291 if (hebrew && Encodings::IsComposeChar_hebrew(c))
294 if (orig_font != getFont(pos))
298 c = par_.transformChar(c, pos);
304 orig_font.setColor(LColor::strikeout);
305 else if (prev_newtext)
306 orig_font.setColor(LColor::newtext);
308 // Draw text and set the new x position
309 //lyxerr << "paint row: yo_ " << yo_ << "\n";
310 pain_.text(int(x_), yo_, str, orig_font);
311 x_ += font_metrics::width(str, orig_font);
315 void RowPainter::paintForeignMark(double orig_x, LyXFont const & orig_font)
317 if (!lyxrc.mark_foreign_language)
319 if (orig_font.language() == latex_language)
321 if (orig_font.language() == bv_.buffer()->params().language)
324 int const y = yo_ + 1;
325 pain_.line(int(orig_x), y, int(x_), y, LColor::language);
329 void RowPainter::paintFromPos(pos_type & vpos)
331 pos_type const pos = text_.bidi.vis2log(vpos);
333 LyXFont const & orig_font = getFont(pos);
335 double const orig_x = x_;
337 char const c = par_.getChar(pos);
339 if (c == Paragraph::META_INSET) {
342 paintForeignMark(orig_x, orig_font);
346 // usual characters, no insets
348 // special case languages
349 bool const hebrew = (orig_font.language()->lang() == "hebrew");
351 orig_font.language()->lang() == "arabic" &&
352 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
353 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
355 // draw as many chars as we can
356 if ((!hebrew && !arabic)
357 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
358 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
359 paintChars(vpos, hebrew, arabic);
361 paintHebrewComposeChar(vpos);
363 paintArabicComposeChar(vpos);
366 paintForeignMark(orig_x, orig_font);
370 void RowPainter::paintChangeBar()
372 pos_type const start = row_.pos();
373 pos_type const end = row_.endpos();
375 if (start == end || !par_.isChanged(start, end - 1))
378 int const height = text_.isLastRow(pit_, row_)
382 pain_.fillRectangle(4, yo_ - row_.ascent(), 5, height, LColor::changebar);
386 void RowPainter::paintAppendix()
388 if (!par_.params().appendix())
391 int y = yo_ - row_.ascent();
393 if (par_.params().startOfAppendix())
394 y += 2 * defaultRowHeight();
396 pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
397 pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), LColor::appendix);
401 void RowPainter::paintDepthBar()
403 Paragraph::depth_type const depth = par_.getDepth();
408 Paragraph::depth_type prev_depth = 0;
409 if (!text_.isFirstRow(pit_, row_)) {
410 pit_type pit2 = pit_;
413 prev_depth = pars_[pit2].getDepth();
416 Paragraph::depth_type next_depth = 0;
417 if (!text_.isLastRow(pit_, row_)) {
418 pit_type pit2 = pit_;
419 if (row_.endpos() >= pars_[pit2].size())
421 next_depth = pars_[pit2].getDepth();
424 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
425 int const w = nestMargin() / 5;
426 int x = int(xo_) + w * i;
427 // only consider the changebar space if we're drawing outermost text
428 if (text_.isMainText())
429 x += changebarMargin();
431 int const starty = yo_ - row_.ascent();
432 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
434 pain_.line(x, starty, x, starty + h, LColor::depthbar);
437 pain_.fillRectangle(x, starty, w, 2, LColor::depthbar);
439 pain_.fillRectangle(x, starty + h, w, 2, LColor::depthbar);
444 int RowPainter::paintAppendixStart(int y)
447 pb_font.setColor(LColor::appendix);
450 string const label = _("Appendix");
454 font_metrics::rectText(label, pb_font, w, a, d);
456 int const text_start = int(xo_ + (width_ - w) / 2);
457 int const text_end = text_start + w;
459 pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
461 pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
462 pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
464 return 3 * defaultRowHeight();
468 void RowPainter::paintFirst()
470 ParagraphParameters const & parparams = par_.params();
474 // start of appendix?
475 if (parparams.startOfAppendix())
476 y_top += paintAppendixStart(yo_ + y_top + 2 * defaultRowHeight());
478 Buffer const & buffer = *bv_.buffer();
480 LyXLayout_ptr const & layout = par_.layout();
482 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
484 if (layout->latextype == LATEX_PARAGRAPH
485 && !par_.getDepth()) {
486 y_top += buffer.params().getDefSkip().inPixels(bv_);
488 LyXLayout_ptr const & playout = pars_[pit_ - 1].layout();
489 if (playout->latextype == LATEX_PARAGRAPH
490 && !pars_[pit_ - 1].getDepth()) {
491 // is it right to use defskip here, too? (AS)
492 y_top += buffer.params().getDefSkip().inPixels(bv_);
498 bool const is_rtl = text_.isRTL(par_);
499 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
500 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
502 // should we print a label?
503 if (layout->labeltype >= LABEL_STATIC
504 && (layout->labeltype != LABEL_STATIC
505 || layout->latextype != LATEX_ENVIRONMENT
508 LyXFont font = getLabelFont();
509 if (!par_.getLabelstring().empty()) {
511 string const str = par_.getLabelstring();
513 // this is special code for the chapter layout. This is
514 // printed in an extra row and has a pagebreak at
516 if (layout->counter == "chapter") {
517 if (buffer.params().secnumdepth >= 0) {
518 double spacing_val = 1.0;
519 if (!parparams.spacing().isDefault()) {
520 spacing_val = parparams.spacing().getValue();
522 spacing_val = buffer.params().spacing().getValue();
525 #warning Look is this correct?
527 int const labeladdon = int(font_metrics::maxHeight(font) * layout->spacing.getValue() * spacing_val);
529 int const maxdesc = int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
530 + int(layout->parsep) * defaultRowHeight();
533 x = width_ - leftMargin() -
534 font_metrics::width(str, font);
537 pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
541 x = width_ - leftMargin()
542 + font_metrics::width(layout->labelsep, font);
544 x = x_ - font_metrics::width(layout->labelsep, font)
545 - font_metrics::width(str, font);
548 pain_.text(int(x), yo_, str, font);
552 // the labels at the top of an environment.
553 // More or less for bibliography
555 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
556 layout->labeltype == LABEL_BIBLIO ||
557 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
558 LyXFont font = getLabelFont();
559 if (!par_.getLabelstring().empty()) {
560 string const str = par_.getLabelstring();
561 double spacing_val = 1.0;
562 if (!parparams.spacing().isDefault()) {
563 spacing_val = parparams.spacing().getValue();
565 spacing_val = buffer.params().spacing().getValue();
569 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
570 + (layout->labelbottomsep * defaultRowHeight()));
573 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
574 x = ((is_rtl ? leftMargin() : x_)
575 + width_ - text_.rightMargin(par_)) / 2;
576 x -= font_metrics::width(str, font) / 2;
578 x = width_ - leftMargin() -
579 font_metrics::width(str, font);
581 pain_.text(int(x), yo_ - maxdesc, str, font);
587 void RowPainter::paintLast()
589 bool const is_rtl = text_.isRTL(par_);
590 int const endlabel = getEndLabel(pit_, text_.paragraphs());
595 case END_LABEL_FILLED_BOX: {
596 LyXFont const font = getLabelFont();
597 int const size = int(0.75 * font_metrics::maxAscent(font));
598 int const y = yo_ - size;
599 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
601 if (width_ - int(row_.width()) <= size)
602 x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
604 if (endlabel == END_LABEL_BOX)
605 pain_.rectangle(x, y, size, size, LColor::eolmarker);
607 pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
611 case END_LABEL_STATIC: {
612 LyXFont font = getLabelFont();
613 string const & str = par_.layout()->endlabelstring();
614 double const x = is_rtl ?
615 x_ - font_metrics::width(str, font)
616 : - text_.rightMargin(par_) - row_.width();
617 pain_.text(int(x), yo_, str, font);
621 case END_LABEL_NO_LABEL:
627 void RowPainter::paintText()
629 pos_type const end = row_.endpos();
630 pos_type body_pos = par_.beginOfBody();
632 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
636 LyXLayout_ptr const & layout = par_.layout();
638 bool running_strikeout = false;
639 bool is_struckout = false;
640 int last_strikeout_x = 0;
642 for (pos_type vpos = row_.pos(); vpos < end; ) {
643 if (x_ > bv_.workWidth())
646 pos_type const pos = text_.bidi.vis2log(vpos);
648 if (pos >= par_.size()) {
653 const int width_pos = singleWidth(pos);
654 if (x_ + width_pos < 0) {
660 is_struckout = isDeletedText(par_, pos);
662 if (is_struckout && !running_strikeout) {
663 running_strikeout = true;
664 last_strikeout_x = int(x_);
667 bool const highly_editable_inset = par_.isInset(pos)
668 && isHighlyEditableInset(par_.getInset(pos));
670 // if we reach the end of a struck out range, paint it
671 // we also don't paint across things like tables
672 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
673 // calculate 1/3 height of the buffer's default font
675 yo_ - font_metrics::maxAscent(text_.defaultfont_) / 3;
676 pain_.line(last_strikeout_x, middle, int(x_), middle,
677 LColor::strikeout, Painter::line_solid, Painter::line_thin);
678 running_strikeout = false;
681 if (body_pos > 0 && pos == body_pos - 1) {
682 int const lwidth = font_metrics::width(layout->labelsep,
685 x_ += label_hfill_ + lwidth - width_pos;
688 if (par_.isHfill(pos)) {
692 int const y1 = y0 - defaultRowHeight() / 2;
694 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
696 if (hfillExpansion(par_, row_, pos)) {
697 int const y2 = (y0 + y1) / 2;
699 if (pos >= body_pos) {
700 pain_.line(int(x_), y2, int(x_ + hfill_), y2,
702 Painter::line_onoffdash);
705 pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
707 Painter::line_onoffdash);
710 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
714 } else if (par_.isSeparator(pos)) {
724 // if we reach the end of a struck out range, paint it
725 if (running_strikeout) {
726 // calculate 1/3 height of the buffer's default font
728 yo_ - font_metrics::maxAscent(text_.defaultfont_) / 3;
729 pain_.line(last_strikeout_x, middle, int(x_), middle,
730 LColor::strikeout, Painter::line_solid, Painter::line_thin);
731 running_strikeout = false;
737 (PainterInfo & pi, LyXText const & text, pit_type pit, int x, int y)
739 // lyxerr << " paintPar: pit: " << pit << " at y: " << y << endl;
740 static NullPainter nop;
741 static PainterInfo nullpi(pi.base.bv, nop);
742 int const ww = pi.base.bv->workHeight();
744 Paragraph & par = text.paragraphs()[pit];
746 RowList::const_iterator const rb = par.rows().begin();
747 RowList::const_iterator const re = par.rows().end();
748 theCoords.parPos()[&text][pit] = Point(x, y);
751 for (RowList::const_iterator rit = rb; rit != re; ++rit) {
753 bool const inside = (y + rit->descent() >= 0
754 && y - rit->ascent() < ww);
755 RowPainter rp(inside ? pi : nullpi, text, pit, *rit, x, y);
772 void paintText(BufferView const & bv, ViewMetricsInfo const & vi)
774 Painter & pain = bv.painter();
775 LyXText * const text = bv.text();
778 pain.fillRectangle(0, vi.y1, bv.workWidth(), vi.y2 - vi.y1,
782 PainterInfo pi(const_cast<BufferView *>(&bv), pain);
784 text->drawSelection(pi, 0, 0);
788 for (pit_type pit = vi.p1; pit <= vi.p2; ++pit) {
789 yy += text->getPar(pit).ascent();
790 paintPar(pi, *bv.text(), pit, 0, yy);
791 yy += text->getPar(pit).descent();
795 // paint one paragraph above and one below
797 text->redoParagraph(vi.p1 - 1);
798 paintPar(pi, *bv.text(), vi.p1 - 1, 0,
799 vi.y1 - text->getPar(vi.p1 - 1).descent());
802 if (vi.p2 < lyx::pit_type(text->paragraphs().size()) - 1) {
803 text->redoParagraph(vi.p2 + 1);
804 paintPar(pi, *bv.text(), vi.p2 + 1, 0,
805 vi.y2 + text->getPar(vi.p2 + 1).ascent());
808 // and grey out above (should not happen later)
809 // lyxerr << "par ascent: " << text->getPar(vi.p1).ascent() << endl;
811 pain.fillRectangle(0, 0, bv.workWidth(), vi.y1, LColor::bottomarea);
813 // and possibly grey out below
814 // lyxerr << "par descent: " << text->getPar(vi.p1).ascent() << endl;
815 if (vi.y2 < bv.workHeight())
816 pain.fillRectangle(0, vi.y2, bv.workWidth(), bv.workHeight() - vi.y2, LColor::bottomarea);
820 void paintTextInset(LyXText const & text, PainterInfo & pi, int x, int y)
822 // lyxerr << " paintTextInset: y: " << y << endl;
824 y -= text.getPar(0).ascent();
825 for (int pit = 0; pit < int(text.paragraphs().size()); ++pit) {
826 y += text.getPar(pit).ascent();
827 paintPar(pi, text, pit, x, y);
828 y += text.getPar(pit).descent();