2 * \file src/TextMetrics.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
10 * \author André Pönitz
12 * \author Jürgen Vigna
13 * \author Abdelrazak Younes
15 * Full author contact details are available in file CREDITS.
20 #include "TextMetrics.h"
23 #include "BufferParams.h"
24 #include "BufferView.h"
25 #include "bufferview_funcs.h"
27 #include "CoordCache.h"
29 #include "FontIterator.h"
30 #include "FuncRequest.h"
33 #include "MetricsInfo.h"
34 #include "paragraph_funcs.h"
35 #include "ParagraphParameters.h"
36 #include "ParIterator.h"
37 #include "rowpainter.h"
42 #include "frontends/FontMetrics.h"
43 #include "frontends/Painter.h"
45 #include <boost/current_function.hpp>
53 using frontend::FontMetrics;
57 int numberOfSeparators(Paragraph const & par, Row const & row)
59 pos_type const first = max(row.pos(), par.beginOfBody());
60 pos_type const last = row.endpos() - 1;
62 for (pos_type p = first; p < last; ++p) {
63 if (par.isSeparator(p))
70 int numberOfLabelHfills(Paragraph const & par, Row const & row)
72 pos_type last = row.endpos() - 1;
73 pos_type first = row.pos();
75 // hfill *DO* count at the beginning of paragraphs!
77 while (first < last && par.isHfill(first))
81 last = min(last, par.beginOfBody());
83 for (pos_type p = first; p < last; ++p) {
91 int numberOfHfills(Paragraph const & par, Row const & row)
93 pos_type const last = row.endpos();
94 pos_type first = row.pos();
96 // hfill *DO* count at the beginning of paragraphs!
98 while (first < last && par.isHfill(first))
102 first = max(first, par.beginOfBody());
105 for (pos_type p = first; p < last; ++p) {
114 TextMetrics::TextMetrics(BufferView * bv, Text * text)
115 : bv_(bv), text_(text)
118 max_width_ = bv_->workWidth();
119 dim_.wid = max_width_;
123 //text_->updateLabels(bv->buffer());
127 ParagraphMetrics const & TextMetrics::parMetrics(pit_type pit) const
129 return const_cast<TextMetrics *>(this)->parMetrics(pit, true);
133 ParagraphMetrics & TextMetrics::parMetrics(pit_type pit,
136 ParMetricsCache::iterator pmc_it = par_metrics_.find(pit);
137 if (pmc_it == par_metrics_.end()) {
138 pmc_it = par_metrics_.insert(
139 std::make_pair(pit, ParagraphMetrics(text_->getPar(pit)))).first;
141 if (pmc_it->second.rows().empty() && redo) {
144 return pmc_it->second;
148 bool TextMetrics::metrics(MetricsInfo & mi, Dimension & dim)
150 BOOST_ASSERT(mi.base.textwidth);
151 max_width_ = mi.base.textwidth;
152 // backup old dimension.
153 Dimension const old_dim = dim_;
156 size_t npar = text_->paragraphs().size();
158 // If there is more than one row, expand the text to
159 // the full allowable width.
160 dim_.wid = max_width_;
162 //lyxerr << "TextMetrics::metrics: width: " << mi.base.textwidth
163 // << " maxWidth: " << max_width_ << "\nfont: " << mi.base.font << endl;
165 bool changed = false;
167 for (pit_type pit = 0; pit != npar; ++pit) {
168 changed |= redoParagraph(pit);
169 ParagraphMetrics const & pm = par_metrics_[pit];
171 if (dim_.wid < pm.width())
172 dim_.wid = pm.width();
175 dim_.asc = par_metrics_[0].ascent();
176 dim_.des = h - dim_.asc;
177 //lyxerr << "dim_.wid " << dim_.wid << endl;
178 //lyxerr << "dim_.asc " << dim_.asc << endl;
179 //lyxerr << "dim_.des " << dim_.des << endl;
181 changed |= dim_ != old_dim;
187 int TextMetrics::rightMargin(ParagraphMetrics const & pm) const
189 return main_text_? pm.rightMargin(bv_->buffer()) : 0;
193 int TextMetrics::rightMargin(pit_type const pit) const
195 return main_text_? par_metrics_[pit].rightMargin(bv_->buffer()) : 0;
199 bool TextMetrics::redoParagraph(pit_type const pit)
201 Paragraph & par = text_->getPar(pit);
202 // IMPORTANT NOTE: We pass 'false' explicitely in order to not call
203 // redoParagraph() recursively inside parMetrics.
204 Dimension old_dim = parMetrics(pit, false).dim();
205 ParagraphMetrics & pm = par_metrics_[pit];
208 Buffer & buffer = bv_->buffer();
209 BufferParams const & bparams = buffer.params();
210 main_text_ = (text_ == &buffer.text());
211 bool changed = false;
213 // FIXME This check ought to be done somewhere else. It is the reason
214 // why text_ is not const. But then, where else to do it?
215 // Well, how can you end up with either (a) a biblio environment that
216 // has no InsetBibitem or (b) a biblio environment with more than one
217 // InsetBibitem? I think the answer is: when paragraphs are merged;
218 // when layout is set; when material is pasted.
219 int const moveCursor = par.checkBiblio(buffer.params().trackChanges);
221 const_cast<Cursor &>(bv_->cursor()).posRight();
222 else if (moveCursor < 0) {
223 Cursor & cursor = const_cast<Cursor &>(bv_->cursor());
224 if (cursor.pos() >= -moveCursor)
228 // Optimisation: this is used in the next two loops
229 // so better to calculate that once here.
230 int const right_margin = rightMargin(pm);
233 // FIXME: We should always use getFont(), see documentation of
234 // noFontChange() in Inset.h.
235 Font const bufferfont = buffer.params().getFont();
236 InsetList::const_iterator ii = par.insetlist.begin();
237 InsetList::const_iterator iend = par.insetlist.end();
238 for (; ii != iend; ++ii) {
239 Dimension old_dim = ii->inset->dimension();
241 int const w = max_width_ - leftMargin(max_width_, pit, ii->pos)
243 Font const & font = ii->inset->noFontChange() ?
244 bufferfont : text_->getFont(buffer, par, ii->pos);
245 MetricsInfo mi(bv_, font, w);
246 changed |= ii->inset->metrics(mi, dim);
247 changed |= (old_dim != dim);
250 par.setBeginOfBody();
252 size_t row_index = 0;
253 // maximum pixel width of a row
254 int width = max_width_ - right_margin; // - leftMargin(max_width_, pit, row);
257 pos_type end = rowBreakPoint(width, pit, first);
258 if (row_index || end < par.size())
259 // If there is more than one row, expand the text to
260 // the full allowable width. This setting here is needed
261 // for the computeRowMetrics below().
262 dim_.wid = max_width_;
264 dim.wid = rowWidth(right_margin, pit, first, end);
265 boost::tie(dim.asc, dim.des) = rowHeight(pit, first, end);
266 if (row_index == pm.rows().size())
267 pm.rows().push_back(Row());
268 Row & row = pm.rows()[row_index];
269 row.setChanged(false);
272 row.setDimension(dim);
273 computeRowMetrics(pit, row);
274 pm.computeRowSignature(row, bparams);
278 pm.dim().wid = std::max(pm.dim().wid, dim.wid);
279 pm.dim().des += dim.height();
280 } while (first < par.size());
282 if (row_index < pm.rows().size())
283 pm.rows().resize(row_index);
285 // Make sure that if a par ends in newline, there is one more row
287 if (first > 0 && par.isNewline(first - 1)) {
289 dim.wid = rowWidth(right_margin, pit, first, first);
290 boost::tie(dim.asc, dim.des) = rowHeight(pit, first, first);
291 if (row_index == pm.rows().size())
292 pm.rows().push_back(Row());
293 Row & row = pm.rows()[row_index];
294 row.setChanged(false);
297 row.setDimension(dim);
298 computeRowMetrics(pit, row);
299 pm.computeRowSignature(row, bparams);
300 pm.dim().des += dim.height();
303 pm.dim().asc += pm.rows()[0].ascent();
304 pm.dim().des -= pm.rows()[0].ascent();
306 changed |= old_dim.height() != pm.dim().height();
312 void TextMetrics::computeRowMetrics(pit_type const pit,
320 Buffer & buffer = bv_->buffer();
321 Paragraph const & par = text_->getPar(pit);
323 double w = dim_.wid - row.width();
324 // FIXME: put back this assertion when the crash on new doc is solved.
325 //BOOST_ASSERT(w >= 0);
327 //lyxerr << "\ndim_.wid " << dim_.wid << endl;
328 //lyxerr << "row.width() " << row.width() << endl;
329 //lyxerr << "w " << w << endl;
331 bool const is_rtl = text_->isRTL(buffer, par);
333 row.x = rightMargin(pit);
335 row.x = leftMargin(max_width_, pit, row.pos());
337 // is there a manual margin with a manual label
338 LayoutPtr const & layout = par.layout();
340 if (layout->margintype == MARGIN_MANUAL
341 && layout->labeltype == LABEL_MANUAL) {
342 /// We might have real hfills in the label part
343 int nlh = numberOfLabelHfills(par, row);
345 // A manual label par (e.g. List) has an auto-hfill
346 // between the label text and the body of the
348 // But we don't want to do this auto hfill if the par
353 if (nlh && !par.getLabelWidthString().empty())
354 row.label_hfill = labelFill(pit, row) / double(nlh);
357 // are there any hfills in the row?
358 int const nh = numberOfHfills(par, row);
363 // we don't have to look at the alignment if it is ALIGN_LEFT and
364 // if the row is already larger then the permitted width as then
365 // we force the LEFT_ALIGN'edness!
366 } else if (int(row.width()) < max_width_) {
367 // is it block, flushleft or flushright?
368 // set x how you need it
370 if (par.params().align() == LYX_ALIGN_LAYOUT)
371 align = layout->align;
373 align = par.params().align();
375 // Display-style insets should always be on a centred row
376 // The test on par.size() is to catch zero-size pars, which
377 // would trigger the assert in Paragraph::getInset().
378 //inset = par.size() ? par.getInset(row.pos()) : 0;
379 if (row.pos() < par.size()
380 && par.isInset(row.pos()))
382 switch(par.getInset(row.pos())->display()) {
383 case Inset::AlignLeft:
384 align = LYX_ALIGN_BLOCK;
386 case Inset::AlignCenter:
387 align = LYX_ALIGN_CENTER;
390 case Inset::AlignRight:
391 // unchanged (use align)
397 case LYX_ALIGN_BLOCK: {
398 int const ns = numberOfSeparators(par, row);
399 bool disp_inset = false;
400 if (row.endpos() < par.size()) {
401 Inset const * in = par.getInset(row.endpos());
403 disp_inset = in->display();
405 // If we have separators, this is not the last row of a
406 // par, does not end in newline, and is not row above a
407 // display inset... then stretch it
409 && row.endpos() < par.size()
410 && !par.isNewline(row.endpos() - 1)
413 row.separator = w / ns;
414 //lyxerr << "row.separator " << row.separator << endl;
415 //lyxerr << "ns " << ns << endl;
421 case LYX_ALIGN_RIGHT:
424 case LYX_ALIGN_CENTER:
431 pos_type body_pos = par.beginOfBody();
432 pos_type end = row.endpos();
435 && (body_pos > end || !par.isLineSeparator(body_pos - 1)))
437 row.x += theFontMetrics(text_->getLabelFont(buffer, par)).
438 width(layout->labelsep);
440 row.x += row.label_hfill;
446 int TextMetrics::labelFill(pit_type const pit, Row const & row) const
448 Buffer & buffer = bv_->buffer();
449 Paragraph const & par = text_->getPar(pit);
451 pos_type last = par.beginOfBody();
452 BOOST_ASSERT(last > 0);
454 // -1 because a label ends with a space that is in the label
457 // a separator at this end does not count
458 if (par.isLineSeparator(last))
462 for (pos_type i = row.pos(); i <= last; ++i)
463 w += singleWidth(pit, i);
465 docstring const & label = par.params().labelWidthString();
469 FontMetrics const & fm
470 = theFontMetrics(text_->getLabelFont(buffer, par));
472 return max(0, fm.width(label) - w);
478 // this needs special handling - only newlines count as a break point
479 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
481 pos_type const end = par.size();
484 if (par.isNewline(i))
493 int TextMetrics::labelEnd(pit_type const pit) const
495 // labelEnd is only needed if the layout fills a flushleft label.
496 if (text_->getPar(pit).layout()->margintype != MARGIN_MANUAL)
498 // return the beginning of the body
499 return leftMargin(max_width_, pit);
503 pit_type TextMetrics::rowBreakPoint(int width, pit_type const pit,
506 Buffer & buffer = bv_->buffer();
507 ParagraphMetrics const & pm = par_metrics_[pit];
508 Paragraph const & par = text_->getPar(pit);
509 pos_type const end = par.size();
510 if (pos == end || width < 0)
513 LayoutPtr const & layout = par.layout();
515 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX)
516 return addressBreakPoint(pos, par);
518 pos_type const body_pos = par.beginOfBody();
521 // Now we iterate through until we reach the right margin
522 // or the end of the par, then choose the possible break
525 int label_end = labelEnd(pit);
526 int const left = leftMargin(max_width_, pit, pos);
529 // pixel width since last breakpoint
532 FontIterator fi = FontIterator(buffer, *text_, par, pos);
533 pos_type point = end;
535 for ( ; i < end; ++i, ++fi) {
536 int thiswidth = pm.singleWidth(i, *fi);
538 // add the auto-hfill from label end to the body
539 if (body_pos && i == body_pos) {
540 FontMetrics const & fm = theFontMetrics(
541 text_->getLabelFont(buffer, par));
542 int add = fm.width(layout->labelsep);
543 if (par.isLineSeparator(i - 1))
544 add -= singleWidth(pit, i - 1);
546 add = std::max(add, label_end - x);
551 chunkwidth += thiswidth;
553 // break before a character that will fall off
554 // the right of the row
556 // if no break before, break here
557 if (point == end || chunkwidth >= width - left) {
564 // exit on last registered breakpoint:
568 if (par.isNewline(i)) {
574 if (par.isInset(i + 1) && par.getInset(i + 1)->display()) {
579 if (par.isInset(i) && par.getInset(i)->display()) {
585 if (!par.isInset(i) || par.getInset(i)->isChar()) {
586 // some insets are line separators too
587 if (par.isLineSeparator(i)) {
588 // register breakpoint:
595 // maybe found one, but the par is short enough.
596 if (i == end && x < width)
599 // manual labels cannot be broken in LaTeX. But we
600 // want to make our on-screen rendering of footnotes
602 if (body_pos && point < body_pos)
609 int TextMetrics::rowWidth(int right_margin, pit_type const pit,
610 pos_type const first, pos_type const end) const
612 Buffer & buffer = bv_->buffer();
613 // get the pure distance
614 ParagraphMetrics const & pm = par_metrics_[pit];
615 Paragraph const & par = text_->getPar(pit);
616 int w = leftMargin(max_width_, pit, first);
617 int label_end = labelEnd(pit);
619 pos_type const body_pos = par.beginOfBody();
623 FontIterator fi = FontIterator(buffer, *text_, par, i);
624 for ( ; i < end; ++i, ++fi) {
625 if (body_pos > 0 && i == body_pos) {
626 FontMetrics const & fm = theFontMetrics(
627 text_->getLabelFont(buffer, par));
628 w += fm.width(par.layout()->labelsep);
629 if (par.isLineSeparator(i - 1))
630 w -= singleWidth(pit, i - 1);
631 w = max(w, label_end);
633 w += pm.singleWidth(i, *fi);
637 if (body_pos > 0 && body_pos >= end) {
638 FontMetrics const & fm = theFontMetrics(
639 text_->getLabelFont(buffer, par));
640 w += fm.width(par.layout()->labelsep);
641 if (end > 0 && par.isLineSeparator(end - 1))
642 w -= singleWidth(pit, end - 1);
643 w = max(w, label_end);
646 return w + right_margin;
650 boost::tuple<int, int> TextMetrics::rowHeight(pit_type const pit, pos_type const first,
651 pos_type const end) const
653 Paragraph const & par = text_->getPar(pit);
654 // get the maximum ascent and the maximum descent
655 double layoutasc = 0;
656 double layoutdesc = 0;
657 double const dh = defaultRowHeight();
659 // ok, let us initialize the maxasc and maxdesc value.
660 // Only the fontsize count. The other properties
661 // are taken from the layoutfont. Nicer on the screen :)
662 LayoutPtr const & layout = par.layout();
664 // as max get the first character of this row then it can
665 // increase but not decrease the height. Just some point to
666 // start with so we don't have to do the assignment below too
668 Buffer const & buffer = bv_->buffer();
669 Font font = text_->getFont(buffer, par, first);
670 Font::FONT_SIZE const tmpsize = font.size();
671 font = text_->getLayoutFont(buffer, pit);
672 Font::FONT_SIZE const size = font.size();
673 font.setSize(tmpsize);
675 Font labelfont = text_->getLabelFont(buffer, par);
677 FontMetrics const & labelfont_metrics = theFontMetrics(labelfont);
678 FontMetrics const & fontmetrics = theFontMetrics(font);
680 // these are minimum values
681 double const spacing_val = layout->spacing.getValue()
682 * text_->spacing(buffer, par);
683 //lyxerr << "spacing_val = " << spacing_val << endl;
684 int maxasc = int(fontmetrics.maxAscent() * spacing_val);
685 int maxdesc = int(fontmetrics.maxDescent() * spacing_val);
687 // insets may be taller
688 InsetList::const_iterator ii = par.insetlist.begin();
689 InsetList::const_iterator iend = par.insetlist.end();
690 for ( ; ii != iend; ++ii) {
691 if (ii->pos >= first && ii->pos < end) {
692 maxasc = max(maxasc, ii->inset->ascent());
693 maxdesc = max(maxdesc, ii->inset->descent());
697 // Check if any custom fonts are larger (Asger)
698 // This is not completely correct, but we can live with the small,
699 // cosmetic error for now.
702 Font::FONT_SIZE maxsize =
703 par.highestFontInRange(first, end, size);
704 if (maxsize > font.size()) {
705 // use standard paragraph font with the maximal size
707 maxfont.setSize(maxsize);
708 FontMetrics const & maxfontmetrics = theFontMetrics(maxfont);
709 maxasc = max(maxasc, maxfontmetrics.maxAscent());
710 maxdesc = max(maxdesc, maxfontmetrics.maxDescent());
713 // This is nicer with box insets:
717 ParagraphList const & pars = text_->paragraphs();
721 BufferParams const & bufparams = buffer.params();
722 // some parskips VERY EASY IMPLEMENTATION
723 if (bufparams.paragraph_separation
724 == BufferParams::PARSEP_SKIP
725 && par.ownerCode() != Inset::ERT_CODE
726 && par.ownerCode() != Inset::LISTINGS_CODE
728 && ((layout->isParagraph() && par.getDepth() == 0)
729 || (pars[pit - 1].layout()->isParagraph()
730 && pars[pit - 1].getDepth() == 0)))
732 maxasc += bufparams.getDefSkip().inPixels(*bv_);
735 if (par.params().startOfAppendix())
736 maxasc += int(3 * dh);
738 // This is special code for the chapter, since the label of this
739 // layout is printed in an extra row
740 if (layout->counter == "chapter"
741 && !par.params().labelString().empty()) {
742 labeladdon = int(labelfont_metrics.maxHeight()
743 * layout->spacing.getValue()
744 * text_->spacing(buffer, par));
747 // special code for the top label
748 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
749 || layout->labeltype == LABEL_BIBLIO
750 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
751 && isFirstInSequence(pit, pars)
752 && !par.getLabelstring().empty())
755 labelfont_metrics.maxHeight()
756 * layout->spacing.getValue()
757 * text_->spacing(buffer, par)
758 + (layout->topsep + layout->labelbottomsep) * dh);
761 // Add the layout spaces, for example before and after
762 // a section, or between the items of a itemize or enumerate
765 pit_type prev = depthHook(pit, pars, par.getDepth());
766 Paragraph const & prevpar = pars[prev];
768 && prevpar.layout() == layout
769 && prevpar.getDepth() == par.getDepth()
770 && prevpar.getLabelWidthString()
771 == par.getLabelWidthString()) {
772 layoutasc = layout->itemsep * dh;
773 } else if (pit != 0 || first != 0) {
774 if (layout->topsep > 0)
775 layoutasc = layout->topsep * dh;
778 prev = outerHook(pit, pars);
779 if (prev != pit_type(pars.size())) {
780 maxasc += int(pars[prev].layout()->parsep * dh);
781 } else if (pit != 0) {
782 Paragraph const & prevpar = pars[pit - 1];
783 if (prevpar.getDepth() != 0 ||
784 prevpar.layout() == layout) {
785 maxasc += int(layout->parsep * dh);
790 // is it a bottom line?
791 if (end >= par.size()) {
792 // add the layout spaces, for example before and after
793 // a section, or between the items of a itemize or enumerate
795 pit_type nextpit = pit + 1;
796 if (nextpit != pit_type(pars.size())) {
801 if (pars[cpit].getDepth() > pars[nextpit].getDepth()) {
802 usual = pars[cpit].layout()->bottomsep * dh;
803 cpit = depthHook(cpit, pars, pars[nextpit].getDepth());
804 if (pars[cpit].layout() != pars[nextpit].layout()
805 || pars[nextpit].getLabelWidthString() != pars[cpit].getLabelWidthString())
807 unusual = pars[cpit].layout()->bottomsep * dh;
809 layoutdesc = max(unusual, usual);
810 } else if (pars[cpit].getDepth() == pars[nextpit].getDepth()) {
811 if (pars[cpit].layout() != pars[nextpit].layout()
812 || pars[nextpit].getLabelWidthString() != pars[cpit].getLabelWidthString())
813 layoutdesc = int(pars[cpit].layout()->bottomsep * dh);
818 // incalculate the layout spaces
819 maxasc += int(layoutasc * 2 / (2 + pars[pit].getDepth()));
820 maxdesc += int(layoutdesc * 2 / (2 + pars[pit].getDepth()));
822 // FIXME: the correct way is to do the following is to move the
823 // following code in another method specially tailored for the
824 // main Text. The following test is thus bogus.
825 // Top and bottom margin of the document (only at top-level)
827 if (pit == 0 && first == 0)
829 if (pit + 1 == pit_type(pars.size()) &&
831 !(end > 0 && par.isNewline(end - 1)))
835 return boost::make_tuple(maxasc + labeladdon, maxdesc);
839 // x is an absolute screen coord
840 // returns the column near the specified x-coordinate of the row
841 // x is set to the real beginning of this column
842 pos_type TextMetrics::getColumnNearX(pit_type const pit,
843 Row const & row, int & x, bool & boundary) const
845 Buffer const & buffer = bv_->buffer();
847 /// For the main Text, it is possible that this pit is not
848 /// yet in the CoordCache when moving cursor up.
849 /// x Paragraph coordinate is always 0 for main text anyway.
850 int const xo = main_text_? 0 : bv_->coordCache().get(text_, pit).x_;
852 Paragraph const & par = text_->getPar(pit);
853 ParagraphMetrics const & pm = par_metrics_[pit];
855 bidi.computeTables(par, buffer, row);
857 pos_type vc = row.pos();
858 pos_type end = row.endpos();
860 LayoutPtr const & layout = par.layout();
862 bool left_side = false;
864 pos_type body_pos = par.beginOfBody();
867 double last_tmpx = tmpx;
870 (body_pos > end || !par.isLineSeparator(body_pos - 1)))
873 // check for empty row
879 while (vc < end && tmpx <= x) {
880 c = bidi.vis2log(vc);
882 if (body_pos > 0 && c == body_pos - 1) {
883 FontMetrics const & fm = theFontMetrics(
884 text_->getLabelFont(buffer, par));
885 tmpx += row.label_hfill + fm.width(layout->labelsep);
886 if (par.isLineSeparator(body_pos - 1))
887 tmpx -= singleWidth(pit, body_pos - 1);
890 if (pm.hfillExpansion(row, c)) {
891 tmpx += singleWidth(pit, c);
895 tmpx += row.label_hfill;
896 } else if (par.isSeparator(c)) {
897 tmpx += singleWidth(pit, c);
899 tmpx += row.separator;
901 tmpx += singleWidth(pit, c);
906 if ((tmpx + last_tmpx) / 2 > x) {
911 BOOST_ASSERT(vc <= end); // This shouldn't happen.
914 // This (rtl_support test) is not needed, but gives
915 // some speedup if rtl_support == false
916 bool const lastrow = lyxrc.rtl_support && row.endpos() == par.size();
918 // If lastrow is false, we don't need to compute
920 bool const rtl = lastrow ? text_->isRTL(buffer, par) : false;
922 ((rtl && left_side && vc == row.pos() && x < tmpx - 5) ||
923 (!rtl && !left_side && vc == end && x > tmpx + 5)))
925 else if (vc == row.pos()) {
926 c = bidi.vis2log(vc);
927 if (bidi.level(c) % 2 == 1)
930 c = bidi.vis2log(vc - 1);
931 bool const rtl = (bidi.level(c) % 2 == 1);
932 if (left_side == rtl) {
934 boundary = text_->isRTLBoundary(buffer, par, c);
938 // I believe this code is not needed anymore (Jug 20050717)
940 // The following code is necessary because the cursor position past
941 // the last char in a row is logically equivalent to that before
942 // the first char in the next row. That's why insets causing row
943 // divisions -- Newline and display-style insets -- must be treated
944 // specially, so cursor up/down doesn't get stuck in an air gap -- MV
945 // Newline inset, air gap below:
946 if (row.pos() < end && c >= end && par.isNewline(end - 1)) {
947 if (bidi.level(end -1) % 2 == 0)
948 tmpx -= singleWidth(pit, end - 1);
950 tmpx += singleWidth(pit, end - 1);
954 // Air gap above display inset:
955 if (row.pos() < end && c >= end && end < par.size()
956 && par.isInset(end) && par.getInset(end)->display()) {
959 // Air gap below display inset:
960 if (row.pos() < end && c >= end && par.isInset(end - 1)
961 && par.getInset(end - 1)->display()) {
967 pos_type const col = c - row.pos();
969 if (!c || end == par.size())
972 if (c==end && !par.isLineSeparator(c-1) && !par.isNewline(c-1)) {
977 return min(col, end - 1 - row.pos());
981 pos_type TextMetrics::x2pos(pit_type pit, int row, int x) const
983 ParagraphMetrics const & pm = par_metrics_[pit];
984 BOOST_ASSERT(!pm.rows().empty());
985 BOOST_ASSERT(row < int(pm.rows().size()));
987 Row const & r = pm.rows()[row];
988 return r.pos() + getColumnNearX(pit, r, x, bound);
992 // y is screen coordinate
993 pit_type TextMetrics::getPitNearY(int y)
995 BOOST_ASSERT(!text_->paragraphs().empty());
996 BOOST_ASSERT(bv_->coordCache().getParPos().find(text_) != bv_->coordCache().getParPos().end());
997 CoordCache::InnerParPosCache const & cc = bv_->coordCache().getParPos().find(text_)->second;
999 << BOOST_CURRENT_FUNCTION
1000 << ": y: " << y << " cache size: " << cc.size()
1003 // look for highest numbered paragraph with y coordinate less than given y
1006 CoordCache::InnerParPosCache::const_iterator it = cc.begin();
1007 CoordCache::InnerParPosCache::const_iterator et = cc.end();
1008 CoordCache::InnerParPosCache::const_iterator last = et; last--;
1010 ParagraphMetrics const & pm = par_metrics_[it->first];
1012 // If we are off-screen (before the visible part)
1014 // and even before the first paragraph in the cache.
1015 && y < it->second.y_ - int(pm.ascent())) {
1016 // and we are not at the first paragraph in the inset.
1019 // then this is the paragraph we are looking for.
1020 pit = it->first - 1;
1021 // rebreak it and update the CoordCache.
1023 bv_->coordCache().parPos()[text_][pit] =
1024 Point(0, it->second.y_ - pm.descent());
1028 ParagraphMetrics const & pm_last = par_metrics_[last->first];
1030 // If we are off-screen (after the visible part)
1031 if (y > bv_->workHeight()
1032 // and even after the first paragraph in the cache.
1033 && y >= last->second.y_ + int(pm_last.descent())) {
1034 pit = last->first + 1;
1035 // and we are not at the last paragraph in the inset.
1036 if (pit == int(text_->paragraphs().size()))
1038 // then this is the paragraph we are looking for.
1039 // rebreak it and update the CoordCache.
1041 bv_->coordCache().parPos()[text_][pit] =
1042 Point(0, last->second.y_ + pm_last.ascent());
1046 for (; it != et; ++it) {
1047 LYXERR(Debug::DEBUG)
1048 << BOOST_CURRENT_FUNCTION
1049 << " examining: pit: " << it->first
1050 << " y: " << it->second.y_
1053 ParagraphMetrics const & pm = par_metrics_[it->first];
1055 if (it->first >= pit && int(it->second.y_) - int(pm.ascent()) <= y) {
1061 LYXERR(Debug::DEBUG)
1062 << BOOST_CURRENT_FUNCTION
1063 << ": found best y: " << yy << " for pit: " << pit
1070 Row const & TextMetrics::getRowNearY(int y, pit_type pit) const
1072 ParagraphMetrics const & pm = par_metrics_[pit];
1074 int yy = bv_->coordCache().get(text_, pit).y_ - pm.ascent();
1075 BOOST_ASSERT(!pm.rows().empty());
1076 RowList::const_iterator rit = pm.rows().begin();
1077 RowList::const_iterator const rlast = boost::prior(pm.rows().end());
1078 for (; rit != rlast; yy += rit->height(), ++rit)
1079 if (yy + rit->height() > y)
1085 // x,y are absolute screen coordinates
1086 // sets cursor recursively descending into nested editable insets
1087 Inset * TextMetrics::editXY(Cursor & cur, int x, int y)
1089 if (lyxerr.debugging(Debug::WORKAREA)) {
1090 lyxerr << "TextMetrics::editXY(cur, " << x << ", " << y << ")" << std::endl;
1091 cur.bv().coordCache().dump();
1093 pit_type pit = getPitNearY(y);
1094 BOOST_ASSERT(pit != -1);
1096 Row const & row = getRowNearY(y, pit);
1099 int xx = x; // is modified by getColumnNearX
1100 pos_type const pos = row.pos()
1101 + getColumnNearX(pit, row, xx, bound);
1104 cur.boundary(bound);
1107 // try to descend into nested insets
1108 Inset * inset = checkInsetHit(x, y);
1109 //lyxerr << "inset " << inset << " hit at x: " << x << " y: " << y << endl;
1111 // Either we deconst editXY or better we move current_font
1112 // and real_current_font to Cursor
1113 text_->setCurrentFont(cur);
1117 ParagraphList const & pars = text_->paragraphs();
1118 Inset const * insetBefore = pos? pars[pit].getInset(pos - 1): 0;
1119 //Inset * insetBehind = pars[pit].getInset(pos);
1121 // This should be just before or just behind the
1122 // cursor position set above.
1123 BOOST_ASSERT((pos != 0 && inset == insetBefore)
1124 || inset == pars[pit].getInset(pos));
1126 // Make sure the cursor points to the position before
1128 if (inset == insetBefore) {
1130 cur.boundary(false);
1133 // Try to descend recursively inside the inset.
1134 inset = inset->editXY(cur, x, y);
1136 if (cur.top().text() == text_)
1137 text_->setCurrentFont(cur);
1142 void TextMetrics::setCursorFromCoordinates(Cursor & cur, int const x, int const y)
1144 BOOST_ASSERT(text_ == cur.text());
1145 pit_type pit = getPitNearY(y);
1147 ParagraphMetrics const & pm = par_metrics_[pit];
1149 int yy = bv_->coordCache().get(text_, pit).y_ - pm.ascent();
1150 LYXERR(Debug::DEBUG)
1151 << BOOST_CURRENT_FUNCTION
1155 << " yy: " << yy << endl;
1158 BOOST_ASSERT(pm.rows().size());
1159 for (; r < int(pm.rows().size()) - 1; ++r) {
1160 Row const & row = pm.rows()[r];
1161 if (int(yy + row.height()) > y)
1166 Row const & row = pm.rows()[r];
1168 LYXERR(Debug::DEBUG)
1169 << BOOST_CURRENT_FUNCTION
1171 << " from pos: " << row.pos()
1176 pos_type const pos = row.pos() + getColumnNearX(pit, row, xx, bound);
1178 LYXERR(Debug::DEBUG)
1179 << BOOST_CURRENT_FUNCTION
1180 << ": setting cursor pit: " << pit
1184 text_->setCursor(cur, pit, pos, true, bound);
1185 // remember new position.
1190 //takes screen x,y coordinates
1191 Inset * TextMetrics::checkInsetHit(int x, int y)
1193 pit_type pit = getPitNearY(y);
1194 BOOST_ASSERT(pit != -1);
1196 Paragraph const & par = text_->paragraphs()[pit];
1198 LYXERR(Debug::DEBUG)
1199 << BOOST_CURRENT_FUNCTION
1204 InsetList::const_iterator iit = par.insetlist.begin();
1205 InsetList::const_iterator iend = par.insetlist.end();
1206 for (; iit != iend; ++iit) {
1207 Inset * inset = iit->inset;
1209 LYXERR(Debug::DEBUG)
1210 << BOOST_CURRENT_FUNCTION
1211 << ": examining inset " << inset << endl;
1213 if (bv_->coordCache().getInsets().has(inset))
1214 LYXERR(Debug::DEBUG)
1215 << BOOST_CURRENT_FUNCTION
1216 << ": xo: " << inset->xo(*bv_) << "..."
1217 << inset->xo(*bv_) + inset->width()
1218 << " yo: " << inset->yo(*bv_) - inset->ascent()
1220 << inset->yo(*bv_) + inset->descent()
1223 LYXERR(Debug::DEBUG)
1224 << BOOST_CURRENT_FUNCTION
1225 << ": inset has no cached position" << endl;
1227 if (inset->covers(*bv_, x, y)) {
1228 LYXERR(Debug::DEBUG)
1229 << BOOST_CURRENT_FUNCTION
1230 << ": Hit inset: " << inset << endl;
1234 LYXERR(Debug::DEBUG)
1235 << BOOST_CURRENT_FUNCTION
1236 << ": No inset hit. " << endl;
1241 int TextMetrics::cursorX(CursorSlice const & sl,
1242 bool boundary) const
1244 BOOST_ASSERT(sl.text() == text_);
1245 pit_type const pit = sl.pit();
1246 Paragraph const & par = text_->paragraphs()[pit];
1247 ParagraphMetrics const & pm = par_metrics_[pit];
1248 if (pm.rows().empty())
1251 pos_type ppos = sl.pos();
1252 // Correct position in front of big insets
1253 bool const boundary_correction = ppos != 0 && boundary;
1254 if (boundary_correction)
1257 Row const & row = pm.getRow(sl.pos(), boundary);
1259 pos_type cursor_vpos = 0;
1261 Buffer const & buffer = bv_->buffer();
1264 bidi.computeTables(par, buffer, row);
1266 pos_type const row_pos = row.pos();
1267 pos_type const end = row.endpos();
1268 // Spaces at logical line breaks in bidi text must be skipped during
1269 // cursor positioning. However, they may appear visually in the middle
1270 // of a row; they must be skipped, wherever they are...
1271 // * logically "abc_[HEBREW_\nHEBREW]"
1272 // * visually "abc_[_WERBEH\nWERBEH]"
1273 pos_type skipped_sep_vpos = -1;
1276 cursor_vpos = row_pos;
1277 else if (ppos >= end)
1278 cursor_vpos = text_->isRTL(buffer, par) ? row_pos : end;
1279 else if (ppos > row_pos && ppos >= end)
1280 // Place cursor after char at (logical) position pos - 1
1281 cursor_vpos = (bidi.level(ppos - 1) % 2 == 0)
1282 ? bidi.log2vis(ppos - 1) + 1 : bidi.log2vis(ppos - 1);
1284 // Place cursor before char at (logical) position ppos
1285 cursor_vpos = (bidi.level(ppos) % 2 == 0)
1286 ? bidi.log2vis(ppos) : bidi.log2vis(ppos) + 1;
1288 pos_type body_pos = par.beginOfBody();
1290 (body_pos > end || !par.isLineSeparator(body_pos - 1)))
1293 // Use font span to speed things up, see below
1297 // If the last logical character is a separator, skip it, unless
1298 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
1299 if (end > 0 && end < par.size() && par.isSeparator(end - 1))
1300 skipped_sep_vpos = bidi.log2vis(end - 1);
1302 for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
1303 // Skip the separator which is at the logical end of the row
1304 if (vpos == skipped_sep_vpos)
1306 pos_type pos = bidi.vis2log(vpos);
1307 if (body_pos > 0 && pos == body_pos - 1) {
1308 FontMetrics const & labelfm = theFontMetrics(
1309 text_->getLabelFont(buffer, par));
1310 x += row.label_hfill + labelfm.width(par.layout()->labelsep);
1311 if (par.isLineSeparator(body_pos - 1))
1312 x -= singleWidth(pit, body_pos - 1);
1315 // Use font span to speed things up, see above
1316 if (pos < font_span.first || pos > font_span.last) {
1317 font_span = par.fontSpan(pos);
1318 font = text_->getFont(buffer, par, pos);
1321 x += pm.singleWidth(pos, font);
1323 if (pm.hfillExpansion(row, pos))
1324 x += (pos >= body_pos) ? row.hfill : row.label_hfill;
1325 else if (par.isSeparator(pos) && pos >= body_pos)
1329 // see correction above
1330 if (boundary_correction) {
1331 if (text_->isRTL(buffer, sl, boundary))
1332 x -= singleWidth(pit, ppos);
1334 x += singleWidth(pit, ppos);
1341 int TextMetrics::cursorY(CursorSlice const & sl, bool boundary) const
1343 //lyxerr << "TextMetrics::cursorY: boundary: " << boundary << std::endl;
1344 ParagraphMetrics const & pm = par_metrics_[sl.pit()];
1345 if (pm.rows().empty())
1349 h -= par_metrics_[0].rows()[0].ascent();
1350 for (pit_type pit = 0; pit < sl.pit(); ++pit) {
1351 h += par_metrics_[pit].height();
1354 if (pos && boundary)
1356 size_t const rend = pm.pos2row(pos);
1357 for (size_t rit = 0; rit != rend; ++rit)
1358 h += pm.rows()[rit].height();
1359 h += pm.rows()[rend].ascent();
1364 void TextMetrics::cursorPrevious(Cursor & cur)
1366 pos_type cpos = cur.pos();
1367 pit_type cpar = cur.pit();
1369 int x = cur.x_target();
1370 setCursorFromCoordinates(cur, x, 0);
1371 cur.dispatch(FuncRequest(cur.selection()? LFUN_UP_SELECT: LFUN_UP));
1373 if (cpar == cur.pit() && cpos == cur.pos())
1374 // we have a row which is taller than the workarea. The
1375 // simplest solution is to move to the previous row instead.
1376 cur.dispatch(FuncRequest(cur.selection()? LFUN_UP_SELECT: LFUN_UP));
1379 cur.updateFlags(Update::Force | Update::FitCursor);
1383 void TextMetrics::cursorNext(Cursor & cur)
1385 pos_type cpos = cur.pos();
1386 pit_type cpar = cur.pit();
1388 int x = cur.x_target();
1389 setCursorFromCoordinates(cur, x, cur.bv().workHeight() - 1);
1390 cur.dispatch(FuncRequest(cur.selection()? LFUN_DOWN_SELECT: LFUN_DOWN));
1392 if (cpar == cur.pit() && cpos == cur.pos())
1393 // we have a row which is taller than the workarea. The
1394 // simplest solution is to move to the next row instead.
1396 FuncRequest(cur.selection()? LFUN_DOWN_SELECT: LFUN_DOWN));
1399 cur.updateFlags(Update::Force | Update::FitCursor);
1403 int TextMetrics::leftMargin(int max_width, pit_type pit) const
1405 BOOST_ASSERT(pit >= 0);
1406 BOOST_ASSERT(pit < int(text_->paragraphs().size()));
1407 return leftMargin(max_width, pit, text_->paragraphs()[pit].size());
1411 int TextMetrics::leftMargin(int max_width,
1412 pit_type const pit, pos_type const pos) const
1414 ParagraphList const & pars = text_->paragraphs();
1416 BOOST_ASSERT(pit >= 0);
1417 BOOST_ASSERT(pit < int(pars.size()));
1418 Paragraph const & par = pars[pit];
1419 BOOST_ASSERT(pos >= 0);
1420 BOOST_ASSERT(pos <= par.size());
1421 Buffer const & buffer = bv_->buffer();
1422 //lyxerr << "TextMetrics::leftMargin: pit: " << pit << " pos: " << pos << endl;
1423 TextClass const & tclass = buffer.params().getTextClass();
1424 LayoutPtr const & layout = par.layout();
1426 docstring parindent = layout->parindent;
1430 if (text_->isMainText(buffer))
1431 l_margin += changebarMargin();
1433 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(
1434 tclass.leftmargin());
1436 if (par.getDepth() != 0) {
1437 // find the next level paragraph
1438 pit_type newpar = outerHook(pit, pars);
1439 if (newpar != pit_type(pars.size())) {
1440 if (pars[newpar].layout()->isEnvironment()) {
1441 l_margin = leftMargin(max_width, newpar);
1443 if (par.layout() == tclass.defaultLayout()) {
1444 if (pars[newpar].params().noindent())
1447 parindent = pars[newpar].layout()->parindent;
1452 // This happens after sections in standard classes. The 1.3.x
1453 // code compared depths too, but it does not seem necessary
1455 if (par.layout() == tclass.defaultLayout()
1456 && pit > 0 && pars[pit - 1].layout()->nextnoindent)
1459 Font const labelfont = text_->getLabelFont(buffer, par);
1460 FontMetrics const & labelfont_metrics = theFontMetrics(labelfont);
1462 switch (layout->margintype) {
1463 case MARGIN_DYNAMIC:
1464 if (!layout->leftmargin.empty()) {
1465 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(
1466 layout->leftmargin);
1468 if (!par.getLabelstring().empty()) {
1469 l_margin += labelfont_metrics.signedWidth(layout->labelindent);
1470 l_margin += labelfont_metrics.width(par.getLabelstring());
1471 l_margin += labelfont_metrics.width(layout->labelsep);
1475 case MARGIN_MANUAL: {
1476 l_margin += labelfont_metrics.signedWidth(layout->labelindent);
1477 // The width of an empty par, even with manual label, should be 0
1478 if (!par.empty() && pos >= par.beginOfBody()) {
1479 if (!par.getLabelWidthString().empty()) {
1480 docstring labstr = par.getLabelWidthString();
1481 l_margin += labelfont_metrics.width(labstr);
1482 l_margin += labelfont_metrics.width(layout->labelsep);
1488 case MARGIN_STATIC: {
1489 l_margin += theFontMetrics(buffer.params().getFont()).
1490 signedWidth(layout->leftmargin) * 4 / (par.getDepth() + 4);
1494 case MARGIN_FIRST_DYNAMIC:
1495 if (layout->labeltype == LABEL_MANUAL) {
1496 if (pos >= par.beginOfBody()) {
1497 l_margin += labelfont_metrics.signedWidth(layout->leftmargin);
1499 l_margin += labelfont_metrics.signedWidth(layout->labelindent);
1502 // Special case to fix problems with
1504 || (layout->labeltype == LABEL_STATIC
1505 && layout->latextype == LATEX_ENVIRONMENT
1506 && !isFirstInSequence(pit, pars))) {
1507 l_margin += labelfont_metrics.signedWidth(layout->leftmargin);
1508 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
1509 && layout->labeltype != LABEL_BIBLIO
1510 && layout->labeltype !=
1511 LABEL_CENTERED_TOP_ENVIRONMENT) {
1512 l_margin += labelfont_metrics.signedWidth(layout->labelindent);
1513 l_margin += labelfont_metrics.width(layout->labelsep);
1514 l_margin += labelfont_metrics.width(par.getLabelstring());
1518 case MARGIN_RIGHT_ADDRESS_BOX: {
1520 // ok, a terrible hack. The left margin depends on the widest
1521 // row in this paragraph.
1522 RowList::iterator rit = par.rows().begin();
1523 RowList::iterator end = par.rows().end();
1524 // FIXME: This is wrong.
1525 int minfill = max_width;
1526 for ( ; rit != end; ++rit)
1527 if (rit->fill() < minfill)
1528 minfill = rit->fill();
1529 l_margin += theFontMetrics(params.getFont()).signedWidth(layout->leftmargin);
1530 l_margin += minfill;
1532 // also wrong, but much shorter.
1533 l_margin += max_width / 2;
1538 if (!par.params().leftIndent().zero())
1539 l_margin += par.params().leftIndent().inPixels(max_width);
1543 if (par.params().align() == LYX_ALIGN_LAYOUT)
1544 align = layout->align;
1546 align = par.params().align();
1548 // set the correct parindent
1550 && (layout->labeltype == LABEL_NO_LABEL
1551 || layout->labeltype == LABEL_TOP_ENVIRONMENT
1552 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
1553 || (layout->labeltype == LABEL_STATIC
1554 && layout->latextype == LATEX_ENVIRONMENT
1555 && !isFirstInSequence(pit, pars)))
1556 && align == LYX_ALIGN_BLOCK
1557 && !par.params().noindent()
1558 // in some insets, paragraphs are never indented
1559 && !(par.inInset() && par.inInset()->neverIndent(buffer))
1560 // display style insets are always centered, omit indentation
1563 && par.getInset(pos)->display())
1564 && (par.layout() != tclass.defaultLayout()
1565 || buffer.params().paragraph_separation ==
1566 BufferParams::PARSEP_INDENT))
1568 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(
1576 int TextMetrics::singleWidth(pit_type pit, pos_type pos) const
1578 Buffer const & buffer = bv_->buffer();
1579 ParagraphMetrics const & pm = par_metrics_[pit];
1581 return pm.singleWidth(pos, text_->getFont(buffer, text_->getPar(pit), pos));
1585 // only used for inset right now. should also be used for main text
1586 void TextMetrics::draw(PainterInfo & pi, int x, int y) const
1588 if (par_metrics_.empty())
1591 ParMetricsCache::const_iterator it = par_metrics_.begin();
1592 ParMetricsCache::const_iterator const end = par_metrics_.end();
1593 y -= it->second.ascent();
1594 for (; it != end; ++it) {
1595 ParagraphMetrics const & pmi = it->second;
1597 drawParagraph(pi, it->first, x, y);
1603 void TextMetrics::drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const
1605 // lyxerr << " paintPar: pit: " << pit << " at y: " << y << endl;
1606 int const ww = bv_->workHeight();
1608 bv_->coordCache().parPos()[text_][pit] = Point(x, y);
1610 ParagraphMetrics const & pm = par_metrics_[pit];
1611 if (pm.rows().empty())
1614 RowList::const_iterator const rb = pm.rows().begin();
1615 RowList::const_iterator const re = pm.rows().end();
1620 for (RowList::const_iterator rit = rb; rit != re; ++rit) {
1623 bool const inside = (y + rit->descent() >= 0
1624 && y - rit->ascent() < ww);
1625 // it is not needed to draw on screen if we are not inside.
1626 pi.pain.setDrawingEnabled(inside);
1627 RowPainter rp(pi, *text_, pit, *rit, bidi, x, y);
1629 // Row signature; has row changed since last paint?
1630 bool row_has_changed = rit->changed();
1632 if (!pi.full_repaint && !row_has_changed) {
1633 // Paint the only the insets if the text itself is
1635 rp.paintOnlyInsets();
1636 y += rit->descent();
1640 // Paint the row if a full repaint has been requested or it has
1642 // Clear background of this row
1643 // (if paragraph background was not cleared)
1644 if (!pi.full_repaint && row_has_changed) {
1645 pi.pain.fillRectangle(x, y - rit->ascent(),
1646 width(), rit->height(),
1647 text_->backgroundColor());
1650 // Instrumentation for testing row cache (see also
1652 if (lyxerr.debugging(Debug::PAINTING)) {
1653 if (text_->isMainText(bv_->buffer()))
1654 LYXERR(Debug::PAINTING) << "\n{" <<
1655 pi.full_repaint << row_has_changed << "}";
1657 LYXERR(Debug::PAINTING) << "[" <<
1658 pi.full_repaint << row_has_changed << "]";
1661 // Backup full_repaint status and force full repaint
1662 // for inner insets as the Row has been cleared out.
1663 bool tmp = pi.full_repaint;
1664 pi.full_repaint = true;
1667 rp.paintChangeBar();
1673 y += rit->descent();
1674 // Restore full_repaint status.
1675 pi.full_repaint = tmp;
1677 // Re-enable screen drawing for future use of the painter.
1678 pi.pain.setDrawingEnabled(true);
1680 //LYXERR(Debug::PAINTING) << "." << endl;
1684 // only used for inset right now. should also be used for main text
1685 void TextMetrics::drawSelection(PainterInfo & pi, int x, int) const
1687 Cursor & cur = bv_->cursor();
1688 if (!cur.selection())
1690 if (!ptr_cmp(cur.text(), text_))
1693 LYXERR(Debug::DEBUG)
1694 << BOOST_CURRENT_FUNCTION
1695 << "draw selection at " << x
1698 DocIterator beg = cur.selectionBegin();
1699 DocIterator end = cur.selectionEnd();
1701 // the selection doesn't touch the visible screen?
1702 if (bv_funcs::status(bv_, beg) == bv_funcs::CUR_BELOW
1703 || bv_funcs::status(bv_, end) == bv_funcs::CUR_ABOVE)
1706 ParagraphMetrics const & pm1 = par_metrics_[beg.pit()];
1707 ParagraphMetrics const & pm2 = par_metrics_[end.pit()];
1708 Row const & row1 = pm1.getRow(beg.pos(), beg.boundary());
1709 Row const & row2 = pm2.getRow(end.pos(), end.boundary());
1713 bool const clipAbove =
1714 (bv_funcs::status(bv_, beg) == bv_funcs::CUR_ABOVE);
1718 middleTop = bv_funcs::getPos(*bv_, beg, beg.boundary()).y_ + row1.descent();
1722 bool const clipBelow =
1723 (bv_funcs::status(bv_, end) == bv_funcs::CUR_BELOW);
1725 middleBottom = bv_->workHeight();
1727 middleBottom = bv_funcs::getPos(*bv_, end, end.boundary()).y_ - row2.ascent();
1729 // start and end in the same line?
1730 if (!(clipAbove || clipBelow) && &row1 == &row2)
1731 // then only draw this row's selection
1732 drawRowSelection(pi, x, row1, beg, end, false, false);
1736 DocIterator begRowEnd = beg;
1737 begRowEnd.pos() = row1.endpos();
1738 begRowEnd.boundary(true);
1740 // draw upper rectangle
1741 drawRowSelection(pi, x, row1, beg, begRowEnd, false, true);
1744 if (middleTop < middleBottom) {
1745 // draw middle rectangle
1746 pi.pain.fillRectangle(x, middleTop, width(), middleBottom - middleTop,
1752 DocIterator endRowBeg = end;
1753 endRowBeg.pos() = row2.pos();
1754 endRowBeg.boundary(false);
1756 // draw low rectangle
1757 drawRowSelection(pi, x, row2, endRowBeg, end, true, false);
1763 void TextMetrics::drawRowSelection(PainterInfo & pi, int x, Row const & row,
1764 DocIterator const & beg, DocIterator const & end,
1765 bool drawOnBegMargin, bool drawOnEndMargin) const
1767 Buffer & buffer = bv_->buffer();
1768 DocIterator cur = beg;
1769 int x1 = cursorX(beg.top(), beg.boundary());
1770 int x2 = cursorX(end.top(), end.boundary());
1771 int y1 = bv_funcs::getPos(*bv_, cur, cur.boundary()).y_ - row.ascent();
1772 int y2 = y1 + row.height();
1775 if (drawOnBegMargin) {
1776 if (text_->isRTL(buffer, beg.paragraph()))
1777 pi.pain.fillRectangle(x + x1, y1, width() - x1, y2 - y1, Color::selection);
1779 pi.pain.fillRectangle(x, y1, x1, y2 - y1, Color::selection);
1782 if (drawOnEndMargin) {
1783 if (text_->isRTL(buffer, beg.paragraph()))
1784 pi.pain.fillRectangle(x, y1, x2, y2 - y1, Color::selection);
1786 pi.pain.fillRectangle(x + x2, y1, width() - x2, y2 - y1, Color::selection);
1789 // if we are on a boundary from the beginning, it's probably
1790 // a RTL boundary and we jump to the other side directly as this
1791 // segement is 0-size and confuses the logic below
1793 cur.boundary(false);
1795 // go through row and draw from RTL boundary to RTL boundary
1797 bool drawNow = false;
1799 // simplified cursorRight code below which does not
1800 // descend into insets and which does not go into the
1801 // next line. Compare the logic with the original cursorRight
1803 // if left of boundary -> just jump to right side
1804 // but for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
1805 if (cur.boundary()) {
1806 cur.boundary(false);
1807 } else if (text_->isRTLBoundary(buffer, cur.paragraph(), cur.pos() + 1)) {
1808 // in front of RTL boundary -> Stay on this side of the boundary because:
1809 // ab|cDDEEFFghi -> abc|DDEEFFghi
1818 if (cur.pos() == row.endpos())
1823 // the previous segment was just drawn, now the next starts
1824 x1 = cursorX(cur.top(), cur.boundary());
1827 if (!(cur < end) || drawNow) {
1828 x2 = cursorX(cur.top(), cur.boundary());
1829 pi.pain.fillRectangle(x + min(x1,x2), y1, abs(x2 - x1), y2 - y1,
1832 // reset x1, so it is set again next round (which will be on the
1833 // right side of a boundary or at the selection end)
1839 //int TextMetrics::pos2x(pit_type pit, pos_type pos) const
1841 // ParagraphMetrics const & pm = par_metrics_[pit];
1842 // Row const & r = pm.rows()[row];
1848 int defaultRowHeight()
1850 return int(theFontMetrics(Font(Font::ALL_SANE)).maxHeight() * 1.2);