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"
41 #include "frontends/FontMetrics.h"
42 #include "frontends/Painter.h"
44 #include <boost/current_function.hpp>
52 using frontend::FontMetrics;
56 int numberOfSeparators(Paragraph const & par, Row const & row)
58 pos_type const first = max(row.pos(), par.beginOfBody());
59 pos_type const last = row.endpos() - 1;
61 for (pos_type p = first; p < last; ++p) {
62 if (par.isSeparator(p))
69 int numberOfLabelHfills(Paragraph const & par, Row const & row)
71 pos_type last = row.endpos() - 1;
72 pos_type first = row.pos();
74 // hfill *DO* count at the beginning of paragraphs!
76 while (first < last && par.isHfill(first))
80 last = min(last, par.beginOfBody());
82 for (pos_type p = first; p < last; ++p) {
90 int numberOfHfills(Paragraph const & par, Row const & row)
92 pos_type const last = row.endpos();
93 pos_type first = row.pos();
95 // hfill *DO* count at the beginning of paragraphs!
97 while (first < last && par.isHfill(first))
101 first = max(first, par.beginOfBody());
104 for (pos_type p = first; p < last; ++p) {
113 TextMetrics::TextMetrics(BufferView * bv, Text * text)
114 : bv_(bv), text_(text)
117 max_width_ = bv_->workWidth();
118 dim_.wid = max_width_;
122 //text_->updateLabels(bv->buffer());
126 ParagraphMetrics const & TextMetrics::parMetrics(pit_type pit) const
128 return const_cast<TextMetrics *>(this)->parMetrics(pit, true);
132 ParagraphMetrics & TextMetrics::parMetrics(pit_type pit,
135 ParMetricsCache::iterator pmc_it = par_metrics_.find(pit);
136 if (pmc_it == par_metrics_.end()) {
137 pmc_it = par_metrics_.insert(
138 std::make_pair(pit, ParagraphMetrics(text_->getPar(pit)))).first;
140 if (pmc_it->second.rows().empty() && redo) {
143 return pmc_it->second;
147 bool TextMetrics::metrics(MetricsInfo & mi, Dimension & dim)
149 BOOST_ASSERT(mi.base.textwidth);
150 max_width_ = mi.base.textwidth;
151 // backup old dimension.
152 Dimension const old_dim = dim_;
155 size_t npar = text_->paragraphs().size();
157 // If there is more than one row, expand the text to
158 // the full allowable width.
159 dim_.wid = max_width_;
161 //lyxerr << "Text::metrics: width: " << mi.base.textwidth
162 // << " maxWidth: " << max_width_ << "\nfont: " << mi.base.font << endl;
164 bool changed = false;
166 for (pit_type pit = 0; pit != npar; ++pit) {
167 changed |= redoParagraph(pit);
168 ParagraphMetrics const & pm = par_metrics_[pit];
170 if (dim_.wid < pm.width())
171 dim_.wid = pm.width();
174 dim_.asc = par_metrics_[0].ascent();
175 dim_.des = h - dim_.asc;
176 //lyxerr << "dim_.wid " << dim_.wid << endl;
177 //lyxerr << "dim_.asc " << dim_.asc << endl;
178 //lyxerr << "dim_.des " << dim_.des << endl;
180 changed |= dim_ != old_dim;
186 int TextMetrics::rightMargin(ParagraphMetrics const & pm) const
188 return main_text_? pm.rightMargin(bv_->buffer()) : 0;
192 int TextMetrics::rightMargin(pit_type const pit) const
194 return main_text_? par_metrics_[pit].rightMargin(bv_->buffer()) : 0;
198 bool TextMetrics::redoParagraph(pit_type const pit)
200 Paragraph & par = text_->getPar(pit);
201 // IMPORTANT NOTE: We pass 'false' explicitely in order to not call
202 // redoParagraph() recursively inside parMetrics.
203 Dimension old_dim = parMetrics(pit, false).dim();
204 ParagraphMetrics & pm = par_metrics_[pit];
207 Buffer & buffer = bv_->buffer();
208 BufferParams const & bparams = buffer.params();
209 main_text_ = (text_ == &buffer.text());
210 bool changed = false;
212 // FIXME This check ought to be done somewhere else. It is the reason
213 // why text_ is not const. But then, where else to do it?
214 // Well, how can you end up with either (a) a biblio environment that
215 // has no InsetBibitem or (b) a biblio environment with more than one
216 // InsetBibitem? I think the answer is: when paragraphs are merged;
217 // when layout is set; when material is pasted.
218 int const moveCursor = par.checkBiblio(buffer.params().trackChanges);
220 const_cast<Cursor &>(bv_->cursor()).posRight();
221 else if (moveCursor < 0) {
222 Cursor & cursor = const_cast<Cursor &>(bv_->cursor());
223 if (cursor.pos() >= -moveCursor)
227 // Optimisation: this is used in the next two loops
228 // so better to calculate that once here.
229 int const right_margin = rightMargin(pm);
232 // FIXME: We should always use getFont(), see documentation of
233 // noFontChange() in Inset.h.
234 Font const bufferfont = buffer.params().getFont();
235 InsetList::const_iterator ii = par.insetlist.begin();
236 InsetList::const_iterator iend = par.insetlist.end();
237 for (; ii != iend; ++ii) {
238 Dimension old_dim = ii->inset->dimension();
240 int const w = max_width_ - text_->leftMargin(buffer, max_width_, pit, ii->pos)
242 Font const & font = ii->inset->noFontChange() ?
243 bufferfont : text_->getFont(buffer, par, ii->pos);
244 MetricsInfo mi(bv_, font, w);
245 changed |= ii->inset->metrics(mi, dim);
246 changed |= (old_dim != dim);
249 par.setBeginOfBody();
251 size_t row_index = 0;
252 // maximum pixel width of a row
253 int width = max_width_ - right_margin; // - leftMargin(buffer, max_width_, pit, row);
256 pos_type end = rowBreakPoint(width, pit, first);
257 if (row_index || end < par.size())
258 // If there is more than one row, expand the text to
259 // the full allowable width. This setting here is needed
260 // for the computeRowMetrics below().
261 dim_.wid = max_width_;
263 dim.wid = rowWidth(right_margin, pit, first, end);
264 boost::tie(dim.asc, dim.des) = rowHeight(pit, first, end);
265 if (row_index == pm.rows().size())
266 pm.rows().push_back(Row());
267 Row & row = pm.rows()[row_index];
268 row.setChanged(false);
271 row.setDimension(dim);
272 computeRowMetrics(pit, row);
273 pm.computeRowSignature(row, bparams);
277 pm.dim().wid = std::max(pm.dim().wid, dim.wid);
278 pm.dim().des += dim.height();
279 } while (first < par.size());
281 if (row_index < pm.rows().size())
282 pm.rows().resize(row_index);
284 // Make sure that if a par ends in newline, there is one more row
286 if (first > 0 && par.isNewline(first - 1)) {
288 dim.wid = rowWidth(right_margin, pit, first, first);
289 boost::tie(dim.asc, dim.des) = rowHeight(pit, first, first);
290 if (row_index == pm.rows().size())
291 pm.rows().push_back(Row());
292 Row & row = pm.rows()[row_index];
293 row.setChanged(false);
296 row.setDimension(dim);
297 computeRowMetrics(pit, row);
298 pm.computeRowSignature(row, bparams);
299 pm.dim().des += dim.height();
302 pm.dim().asc += pm.rows()[0].ascent();
303 pm.dim().des -= pm.rows()[0].ascent();
305 changed |= old_dim.height() != pm.dim().height();
311 void TextMetrics::computeRowMetrics(pit_type const pit,
314 Buffer & buffer = bv_->buffer();
315 Paragraph const & par = text_->getPar(pit);
317 double w = dim_.wid - row.width();
318 // FIXME: put back this assertion when the crash on new doc is solved.
319 //BOOST_ASSERT(w >= 0);
321 //lyxerr << "\ndim_.wid " << dim_.wid << endl;
322 //lyxerr << "row.width() " << row.width() << endl;
323 //lyxerr << "w " << w << endl;
325 bool const is_rtl = text_->isRTL(buffer, par);
327 row.x = rightMargin(pit);
329 row.x = text_->leftMargin(buffer, max_width_, pit, row.pos());
331 // is there a manual margin with a manual label
332 LayoutPtr const & layout = par.layout();
334 if (layout->margintype == MARGIN_MANUAL
335 && layout->labeltype == LABEL_MANUAL) {
336 /// We might have real hfills in the label part
337 int nlh = numberOfLabelHfills(par, row);
339 // A manual label par (e.g. List) has an auto-hfill
340 // between the label text and the body of the
342 // But we don't want to do this auto hfill if the par
347 if (nlh && !par.getLabelWidthString().empty())
348 row.label_hfill = labelFill(pit, row) / double(nlh);
351 // are there any hfills in the row?
352 int const nh = numberOfHfills(par, row);
357 // we don't have to look at the alignment if it is ALIGN_LEFT and
358 // if the row is already larger then the permitted width as then
359 // we force the LEFT_ALIGN'edness!
360 } else if (int(row.width()) < max_width_) {
361 // is it block, flushleft or flushright?
362 // set x how you need it
364 if (par.params().align() == LYX_ALIGN_LAYOUT)
365 align = layout->align;
367 align = par.params().align();
369 // Display-style insets should always be on a centred row
370 // The test on par.size() is to catch zero-size pars, which
371 // would trigger the assert in Paragraph::getInset().
372 //inset = par.size() ? par.getInset(row.pos()) : 0;
373 if (row.pos() < par.size()
374 && par.isInset(row.pos()))
376 switch(par.getInset(row.pos())->display()) {
377 case Inset::AlignLeft:
378 align = LYX_ALIGN_BLOCK;
380 case Inset::AlignCenter:
381 align = LYX_ALIGN_CENTER;
384 case Inset::AlignRight:
385 // unchanged (use align)
391 case LYX_ALIGN_BLOCK: {
392 int const ns = numberOfSeparators(par, row);
393 bool disp_inset = false;
394 if (row.endpos() < par.size()) {
395 Inset const * in = par.getInset(row.endpos());
397 disp_inset = in->display();
399 // If we have separators, this is not the last row of a
400 // par, does not end in newline, and is not row above a
401 // display inset... then stretch it
403 && row.endpos() < par.size()
404 && !par.isNewline(row.endpos() - 1)
407 row.separator = w / ns;
408 //lyxerr << "row.separator " << row.separator << endl;
409 //lyxerr << "ns " << ns << endl;
415 case LYX_ALIGN_RIGHT:
418 case LYX_ALIGN_CENTER:
425 pos_type body_pos = par.beginOfBody();
426 pos_type end = row.endpos();
429 && (body_pos > end || !par.isLineSeparator(body_pos - 1)))
431 row.x += theFontMetrics(text_->getLabelFont(buffer, par)).
432 width(layout->labelsep);
434 row.x += row.label_hfill;
440 int TextMetrics::labelFill(pit_type const pit, Row const & row) const
442 Buffer & buffer = bv_->buffer();
443 Paragraph const & par = text_->getPar(pit);
445 pos_type last = par.beginOfBody();
446 BOOST_ASSERT(last > 0);
448 // -1 because a label ends with a space that is in the label
451 // a separator at this end does not count
452 if (par.isLineSeparator(last))
456 for (pos_type i = row.pos(); i <= last; ++i)
457 w += singleWidth(pit, i);
459 docstring const & label = par.params().labelWidthString();
463 FontMetrics const & fm
464 = theFontMetrics(text_->getLabelFont(buffer, par));
466 return max(0, fm.width(label) - w);
472 // this needs special handling - only newlines count as a break point
473 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
475 pos_type const end = par.size();
478 if (par.isNewline(i))
487 int TextMetrics::labelEnd(pit_type const pit) const
489 // labelEnd is only needed if the layout fills a flushleft label.
490 if (text_->getPar(pit).layout()->margintype != MARGIN_MANUAL)
492 // return the beginning of the body
493 return text_->leftMargin(bv_->buffer(), max_width_, pit);
497 pit_type TextMetrics::rowBreakPoint(int width, pit_type const pit,
500 Buffer & buffer = bv_->buffer();
501 ParagraphMetrics const & pm = par_metrics_[pit];
502 Paragraph const & par = text_->getPar(pit);
503 pos_type const end = par.size();
504 if (pos == end || width < 0)
507 LayoutPtr const & layout = par.layout();
509 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX)
510 return addressBreakPoint(pos, par);
512 pos_type const body_pos = par.beginOfBody();
515 // Now we iterate through until we reach the right margin
516 // or the end of the par, then choose the possible break
519 int label_end = labelEnd(pit);
520 int const left = text_->leftMargin(buffer, max_width_, pit, pos);
523 // pixel width since last breakpoint
526 FontIterator fi = FontIterator(buffer, *text_, par, pos);
527 pos_type point = end;
529 for ( ; i < end; ++i, ++fi) {
530 int thiswidth = pm.singleWidth(i, *fi);
532 // add the auto-hfill from label end to the body
533 if (body_pos && i == body_pos) {
534 FontMetrics const & fm = theFontMetrics(
535 text_->getLabelFont(buffer, par));
536 int add = fm.width(layout->labelsep);
537 if (par.isLineSeparator(i - 1))
538 add -= singleWidth(pit, i - 1);
540 add = std::max(add, label_end - x);
545 chunkwidth += thiswidth;
547 // break before a character that will fall off
548 // the right of the row
550 // if no break before, break here
551 if (point == end || chunkwidth >= width - left) {
558 // exit on last registered breakpoint:
562 if (par.isNewline(i)) {
568 if (par.isInset(i + 1) && par.getInset(i + 1)->display()) {
573 if (par.isInset(i) && par.getInset(i)->display()) {
579 if (!par.isInset(i) || par.getInset(i)->isChar()) {
580 // some insets are line separators too
581 if (par.isLineSeparator(i)) {
582 // register breakpoint:
589 // maybe found one, but the par is short enough.
590 if (i == end && x < width)
593 // manual labels cannot be broken in LaTeX. But we
594 // want to make our on-screen rendering of footnotes
596 if (body_pos && point < body_pos)
603 int TextMetrics::rowWidth(int right_margin, pit_type const pit,
604 pos_type const first, pos_type const end) const
606 Buffer & buffer = bv_->buffer();
607 // get the pure distance
608 ParagraphMetrics const & pm = par_metrics_[pit];
609 Paragraph const & par = text_->getPar(pit);
610 int w = text_->leftMargin(buffer, max_width_, pit, first);
611 int label_end = labelEnd(pit);
613 pos_type const body_pos = par.beginOfBody();
617 FontIterator fi = FontIterator(buffer, *text_, par, i);
618 for ( ; i < end; ++i, ++fi) {
619 if (body_pos > 0 && i == body_pos) {
620 FontMetrics const & fm = theFontMetrics(
621 text_->getLabelFont(buffer, par));
622 w += fm.width(par.layout()->labelsep);
623 if (par.isLineSeparator(i - 1))
624 w -= singleWidth(pit, i - 1);
625 w = max(w, label_end);
627 w += pm.singleWidth(i, *fi);
631 if (body_pos > 0 && body_pos >= end) {
632 FontMetrics const & fm = theFontMetrics(
633 text_->getLabelFont(buffer, par));
634 w += fm.width(par.layout()->labelsep);
635 if (end > 0 && par.isLineSeparator(end - 1))
636 w -= singleWidth(pit, end - 1);
637 w = max(w, label_end);
640 return w + right_margin;
644 boost::tuple<int, int> TextMetrics::rowHeight(pit_type const pit, pos_type const first,
645 pos_type const end) const
647 Paragraph const & par = text_->getPar(pit);
648 // get the maximum ascent and the maximum descent
649 double layoutasc = 0;
650 double layoutdesc = 0;
651 double const dh = defaultRowHeight();
653 // ok, let us initialize the maxasc and maxdesc value.
654 // Only the fontsize count. The other properties
655 // are taken from the layoutfont. Nicer on the screen :)
656 LayoutPtr const & layout = par.layout();
658 // as max get the first character of this row then it can
659 // increase but not decrease the height. Just some point to
660 // start with so we don't have to do the assignment below too
662 Buffer const & buffer = bv_->buffer();
663 Font font = text_->getFont(buffer, par, first);
664 Font::FONT_SIZE const tmpsize = font.size();
665 font = text_->getLayoutFont(buffer, pit);
666 Font::FONT_SIZE const size = font.size();
667 font.setSize(tmpsize);
669 Font labelfont = text_->getLabelFont(buffer, par);
671 FontMetrics const & labelfont_metrics = theFontMetrics(labelfont);
672 FontMetrics const & fontmetrics = theFontMetrics(font);
674 // these are minimum values
675 double const spacing_val = layout->spacing.getValue()
676 * text_->spacing(buffer, par);
677 //lyxerr << "spacing_val = " << spacing_val << endl;
678 int maxasc = int(fontmetrics.maxAscent() * spacing_val);
679 int maxdesc = int(fontmetrics.maxDescent() * spacing_val);
681 // insets may be taller
682 InsetList::const_iterator ii = par.insetlist.begin();
683 InsetList::const_iterator iend = par.insetlist.end();
684 for ( ; ii != iend; ++ii) {
685 if (ii->pos >= first && ii->pos < end) {
686 maxasc = max(maxasc, ii->inset->ascent());
687 maxdesc = max(maxdesc, ii->inset->descent());
691 // Check if any custom fonts are larger (Asger)
692 // This is not completely correct, but we can live with the small,
693 // cosmetic error for now.
696 Font::FONT_SIZE maxsize =
697 par.highestFontInRange(first, end, size);
698 if (maxsize > font.size()) {
699 // use standard paragraph font with the maximal size
701 maxfont.setSize(maxsize);
702 FontMetrics const & maxfontmetrics = theFontMetrics(maxfont);
703 maxasc = max(maxasc, maxfontmetrics.maxAscent());
704 maxdesc = max(maxdesc, maxfontmetrics.maxDescent());
707 // This is nicer with box insets:
711 ParagraphList const & pars = text_->paragraphs();
715 BufferParams const & bufparams = buffer.params();
716 // some parskips VERY EASY IMPLEMENTATION
717 if (bufparams.paragraph_separation
718 == BufferParams::PARSEP_SKIP
719 && par.ownerCode() != Inset::ERT_CODE
720 && par.ownerCode() != Inset::LISTINGS_CODE
722 && ((layout->isParagraph() && par.getDepth() == 0)
723 || (pars[pit - 1].layout()->isParagraph()
724 && pars[pit - 1].getDepth() == 0)))
726 maxasc += bufparams.getDefSkip().inPixels(*bv_);
729 if (par.params().startOfAppendix())
730 maxasc += int(3 * dh);
732 // This is special code for the chapter, since the label of this
733 // layout is printed in an extra row
734 if (layout->counter == "chapter"
735 && !par.params().labelString().empty()) {
736 labeladdon = int(labelfont_metrics.maxHeight()
737 * layout->spacing.getValue()
738 * text_->spacing(buffer, par));
741 // special code for the top label
742 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
743 || layout->labeltype == LABEL_BIBLIO
744 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
745 && isFirstInSequence(pit, pars)
746 && !par.getLabelstring().empty())
749 labelfont_metrics.maxHeight()
750 * layout->spacing.getValue()
751 * text_->spacing(buffer, par)
752 + (layout->topsep + layout->labelbottomsep) * dh);
755 // Add the layout spaces, for example before and after
756 // a section, or between the items of a itemize or enumerate
759 pit_type prev = depthHook(pit, pars, par.getDepth());
760 Paragraph const & prevpar = pars[prev];
762 && prevpar.layout() == layout
763 && prevpar.getDepth() == par.getDepth()
764 && prevpar.getLabelWidthString()
765 == par.getLabelWidthString()) {
766 layoutasc = layout->itemsep * dh;
767 } else if (pit != 0 || first != 0) {
768 if (layout->topsep > 0)
769 layoutasc = layout->topsep * dh;
772 prev = outerHook(pit, pars);
773 if (prev != pit_type(pars.size())) {
774 maxasc += int(pars[prev].layout()->parsep * dh);
775 } else if (pit != 0) {
776 Paragraph const & prevpar = pars[pit - 1];
777 if (prevpar.getDepth() != 0 ||
778 prevpar.layout() == layout) {
779 maxasc += int(layout->parsep * dh);
784 // is it a bottom line?
785 if (end >= par.size()) {
786 // add the layout spaces, for example before and after
787 // a section, or between the items of a itemize or enumerate
789 pit_type nextpit = pit + 1;
790 if (nextpit != pit_type(pars.size())) {
795 if (pars[cpit].getDepth() > pars[nextpit].getDepth()) {
796 usual = pars[cpit].layout()->bottomsep * dh;
797 cpit = depthHook(cpit, pars, pars[nextpit].getDepth());
798 if (pars[cpit].layout() != pars[nextpit].layout()
799 || pars[nextpit].getLabelWidthString() != pars[cpit].getLabelWidthString())
801 unusual = pars[cpit].layout()->bottomsep * dh;
803 layoutdesc = max(unusual, usual);
804 } else if (pars[cpit].getDepth() == pars[nextpit].getDepth()) {
805 if (pars[cpit].layout() != pars[nextpit].layout()
806 || pars[nextpit].getLabelWidthString() != pars[cpit].getLabelWidthString())
807 layoutdesc = int(pars[cpit].layout()->bottomsep * dh);
812 // incalculate the layout spaces
813 maxasc += int(layoutasc * 2 / (2 + pars[pit].getDepth()));
814 maxdesc += int(layoutdesc * 2 / (2 + pars[pit].getDepth()));
816 // FIXME: the correct way is to do the following is to move the
817 // following code in another method specially tailored for the
818 // main Text. The following test is thus bogus.
819 // Top and bottom margin of the document (only at top-level)
821 if (pit == 0 && first == 0)
823 if (pit + 1 == pit_type(pars.size()) &&
825 !(end > 0 && par.isNewline(end - 1)))
829 return boost::make_tuple(maxasc + labeladdon, maxdesc);
833 // x is an absolute screen coord
834 // returns the column near the specified x-coordinate of the row
835 // x is set to the real beginning of this column
836 pos_type TextMetrics::getColumnNearX(pit_type const pit,
837 Row const & row, int & x, bool & boundary) const
839 Buffer const & buffer = bv_->buffer();
841 /// For the main Text, it is possible that this pit is not
842 /// yet in the CoordCache when moving cursor up.
843 /// x Paragraph coordinate is always 0 for main text anyway.
844 int const xo = main_text_? 0 : bv_->coordCache().get(text_, pit).x_;
846 Paragraph const & par = text_->getPar(pit);
847 ParagraphMetrics const & pm = par_metrics_[pit];
849 bidi.computeTables(par, buffer, row);
851 pos_type vc = row.pos();
852 pos_type end = row.endpos();
854 LayoutPtr const & layout = par.layout();
856 bool left_side = false;
858 pos_type body_pos = par.beginOfBody();
861 double last_tmpx = tmpx;
864 (body_pos > end || !par.isLineSeparator(body_pos - 1)))
867 // check for empty row
873 while (vc < end && tmpx <= x) {
874 c = bidi.vis2log(vc);
876 if (body_pos > 0 && c == body_pos - 1) {
877 FontMetrics const & fm = theFontMetrics(
878 text_->getLabelFont(buffer, par));
879 tmpx += row.label_hfill + fm.width(layout->labelsep);
880 if (par.isLineSeparator(body_pos - 1))
881 tmpx -= singleWidth(pit, body_pos - 1);
884 if (pm.hfillExpansion(row, c)) {
885 tmpx += singleWidth(pit, c);
889 tmpx += row.label_hfill;
890 } else if (par.isSeparator(c)) {
891 tmpx += singleWidth(pit, c);
893 tmpx += row.separator;
895 tmpx += singleWidth(pit, c);
900 if ((tmpx + last_tmpx) / 2 > x) {
905 BOOST_ASSERT(vc <= end); // This shouldn't happen.
908 // This (rtl_support test) is not needed, but gives
909 // some speedup if rtl_support == false
910 bool const lastrow = lyxrc.rtl_support && row.endpos() == par.size();
912 // If lastrow is false, we don't need to compute
914 bool const rtl = lastrow ? text_->isRTL(buffer, par) : false;
916 ((rtl && left_side && vc == row.pos() && x < tmpx - 5) ||
917 (!rtl && !left_side && vc == end && x > tmpx + 5)))
919 else if (vc == row.pos()) {
920 c = bidi.vis2log(vc);
921 if (bidi.level(c) % 2 == 1)
924 c = bidi.vis2log(vc - 1);
925 bool const rtl = (bidi.level(c) % 2 == 1);
926 if (left_side == rtl) {
928 boundary = text_->isRTLBoundary(buffer, par, c);
932 // I believe this code is not needed anymore (Jug 20050717)
934 // The following code is necessary because the cursor position past
935 // the last char in a row is logically equivalent to that before
936 // the first char in the next row. That's why insets causing row
937 // divisions -- Newline and display-style insets -- must be treated
938 // specially, so cursor up/down doesn't get stuck in an air gap -- MV
939 // Newline inset, air gap below:
940 if (row.pos() < end && c >= end && par.isNewline(end - 1)) {
941 if (bidi.level(end -1) % 2 == 0)
942 tmpx -= singleWidth(pit, end - 1);
944 tmpx += singleWidth(pit, end - 1);
948 // Air gap above display inset:
949 if (row.pos() < end && c >= end && end < par.size()
950 && par.isInset(end) && par.getInset(end)->display()) {
953 // Air gap below display inset:
954 if (row.pos() < end && c >= end && par.isInset(end - 1)
955 && par.getInset(end - 1)->display()) {
961 pos_type const col = c - row.pos();
963 if (!c || end == par.size())
966 if (c==end && !par.isLineSeparator(c-1) && !par.isNewline(c-1)) {
971 return min(col, end - 1 - row.pos());
975 pos_type TextMetrics::x2pos(pit_type pit, int row, int x) const
977 ParagraphMetrics const & pm = par_metrics_[pit];
978 BOOST_ASSERT(!pm.rows().empty());
979 BOOST_ASSERT(row < int(pm.rows().size()));
981 Row const & r = pm.rows()[row];
982 return r.pos() + getColumnNearX(pit, r, x, bound);
986 int TextMetrics::singleWidth(pit_type pit, pos_type pos) const
988 Buffer const & buffer = bv_->buffer();
989 ParagraphMetrics const & pm = par_metrics_[pit];
991 return pm.singleWidth(pos, text_->getFont(buffer, text_->getPar(pit), pos));
995 // only used for inset right now. should also be used for main text
996 void TextMetrics::draw(PainterInfo & pi, int x, int y) const
998 if (par_metrics_.empty())
1001 ParMetricsCache::const_iterator it = par_metrics_.begin();
1002 ParMetricsCache::const_iterator const end = par_metrics_.end();
1003 y -= it->second.ascent();
1004 for (; it != end; ++it) {
1005 ParagraphMetrics const & pmi = it->second;
1007 drawParagraph(pi, it->first, x, y);
1013 void TextMetrics::drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const
1015 // lyxerr << " paintPar: pit: " << pit << " at y: " << y << endl;
1016 int const ww = bv_->workHeight();
1018 bv_->coordCache().parPos()[text_][pit] = Point(x, y);
1020 ParagraphMetrics const & pm = par_metrics_[pit];
1021 if (pm.rows().empty())
1024 RowList::const_iterator const rb = pm.rows().begin();
1025 RowList::const_iterator const re = pm.rows().end();
1030 for (RowList::const_iterator rit = rb; rit != re; ++rit) {
1033 bool const inside = (y + rit->descent() >= 0
1034 && y - rit->ascent() < ww);
1035 // it is not needed to draw on screen if we are not inside.
1036 pi.pain.setDrawingEnabled(inside);
1037 RowPainter rp(pi, *text_, pit, *rit, bidi, x, y);
1039 // Row signature; has row changed since last paint?
1040 bool row_has_changed = rit->changed();
1042 if (!pi.full_repaint && !row_has_changed) {
1043 // Paint the only the insets if the text itself is
1045 rp.paintOnlyInsets();
1046 y += rit->descent();
1050 // Paint the row if a full repaint has been requested or it has
1052 // Clear background of this row
1053 // (if paragraph background was not cleared)
1054 if (!pi.full_repaint && row_has_changed) {
1055 pi.pain.fillRectangle(x, y - rit->ascent(),
1056 width(), rit->height(),
1057 text_->backgroundColor());
1060 // Instrumentation for testing row cache (see also
1062 if (lyxerr.debugging(Debug::PAINTING)) {
1063 if (text_->isMainText(bv_->buffer()))
1064 LYXERR(Debug::PAINTING) << "\n{" <<
1065 pi.full_repaint << row_has_changed << "}";
1067 LYXERR(Debug::PAINTING) << "[" <<
1068 pi.full_repaint << row_has_changed << "]";
1071 // Backup full_repaint status and force full repaint
1072 // for inner insets as the Row has been cleared out.
1073 bool tmp = pi.full_repaint;
1074 pi.full_repaint = true;
1077 rp.paintChangeBar();
1083 y += rit->descent();
1084 // Restore full_repaint status.
1085 pi.full_repaint = tmp;
1087 // Re-enable screen drawing for future use of the painter.
1088 pi.pain.setDrawingEnabled(true);
1090 //LYXERR(Debug::PAINTING) << "." << endl;
1094 // only used for inset right now. should also be used for main text
1095 void TextMetrics::drawSelection(PainterInfo & pi, int x, int) const
1097 Cursor & cur = bv_->cursor();
1098 if (!cur.selection())
1100 if (!ptr_cmp(cur.text(), text_))
1103 LYXERR(Debug::DEBUG)
1104 << BOOST_CURRENT_FUNCTION
1105 << "draw selection at " << x
1108 DocIterator beg = cur.selectionBegin();
1109 DocIterator end = cur.selectionEnd();
1111 // the selection doesn't touch the visible screen?
1112 if (bv_funcs::status(bv_, beg) == bv_funcs::CUR_BELOW
1113 || bv_funcs::status(bv_, end) == bv_funcs::CUR_ABOVE)
1116 ParagraphMetrics const & pm1 = par_metrics_[beg.pit()];
1117 ParagraphMetrics const & pm2 = par_metrics_[end.pit()];
1118 Row const & row1 = pm1.getRow(beg.pos(), beg.boundary());
1119 Row const & row2 = pm2.getRow(end.pos(), end.boundary());
1123 bool const clipAbove =
1124 (bv_funcs::status(bv_, beg) == bv_funcs::CUR_ABOVE);
1128 middleTop = bv_funcs::getPos(*bv_, beg, beg.boundary()).y_ + row1.descent();
1132 bool const clipBelow =
1133 (bv_funcs::status(bv_, end) == bv_funcs::CUR_BELOW);
1135 middleBottom = bv_->workHeight();
1137 middleBottom = bv_funcs::getPos(*bv_, end, end.boundary()).y_ - row2.ascent();
1139 // start and end in the same line?
1140 if (!(clipAbove || clipBelow) && &row1 == &row2)
1141 // then only draw this row's selection
1142 drawRowSelection(pi, x, row1, beg, end, false, false);
1146 DocIterator begRowEnd = beg;
1147 begRowEnd.pos() = row1.endpos();
1148 begRowEnd.boundary(true);
1150 // draw upper rectangle
1151 drawRowSelection(pi, x, row1, beg, begRowEnd, false, true);
1154 if (middleTop < middleBottom) {
1155 // draw middle rectangle
1156 pi.pain.fillRectangle(x, middleTop, width(), middleBottom - middleTop,
1162 DocIterator endRowBeg = end;
1163 endRowBeg.pos() = row2.pos();
1164 endRowBeg.boundary(false);
1166 // draw low rectangle
1167 drawRowSelection(pi, x, row2, endRowBeg, end, true, false);
1173 void TextMetrics::drawRowSelection(PainterInfo & pi, int x, Row const & row,
1174 DocIterator const & beg, DocIterator const & end,
1175 bool drawOnBegMargin, bool drawOnEndMargin) const
1177 Buffer & buffer = bv_->buffer();
1178 DocIterator cur = beg;
1179 int x1 = text_->cursorX(*bv_, beg.top(), beg.boundary());
1180 int x2 = text_->cursorX(*bv_, end.top(), end.boundary());
1181 int y1 = bv_funcs::getPos(*bv_, cur, cur.boundary()).y_ - row.ascent();
1182 int y2 = y1 + row.height();
1185 if (drawOnBegMargin) {
1186 if (text_->isRTL(buffer, beg.paragraph()))
1187 pi.pain.fillRectangle(x + x1, y1, width() - x1, y2 - y1, Color::selection);
1189 pi.pain.fillRectangle(x, y1, x1, y2 - y1, Color::selection);
1192 if (drawOnEndMargin) {
1193 if (text_->isRTL(buffer, beg.paragraph()))
1194 pi.pain.fillRectangle(x, y1, x2, y2 - y1, Color::selection);
1196 pi.pain.fillRectangle(x + x2, y1, width() - x2, y2 - y1, Color::selection);
1199 // if we are on a boundary from the beginning, it's probably
1200 // a RTL boundary and we jump to the other side directly as this
1201 // segement is 0-size and confuses the logic below
1203 cur.boundary(false);
1205 // go through row and draw from RTL boundary to RTL boundary
1207 bool drawNow = false;
1209 // simplified cursorRight code below which does not
1210 // descend into insets and which does not go into the
1211 // next line. Compare the logic with the original cursorRight
1213 // if left of boundary -> just jump to right side
1214 // but for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
1215 if (cur.boundary()) {
1216 cur.boundary(false);
1217 } else if (text_->isRTLBoundary(buffer, cur.paragraph(), cur.pos() + 1)) {
1218 // in front of RTL boundary -> Stay on this side of the boundary because:
1219 // ab|cDDEEFFghi -> abc|DDEEFFghi
1228 if (cur.pos() == row.endpos())
1233 // the previous segment was just drawn, now the next starts
1234 x1 = text_->cursorX(*bv_, cur.top(), cur.boundary());
1237 if (!(cur < end) || drawNow) {
1238 x2 = text_->cursorX(*bv_, cur.top(), cur.boundary());
1239 pi.pain.fillRectangle(x + min(x1,x2), y1, abs(x2 - x1), y2 - y1,
1242 // reset x1, so it is set again next round (which will be on the
1243 // right side of a boundary or at the selection end)
1249 //int Text::pos2x(pit_type pit, pos_type pos) const
1251 // ParagraphMetrics const & pm = par_metrics_[pit];
1252 // Row const & r = pm.rows()[row];
1258 int defaultRowHeight()
1260 return int(theFontMetrics(Font(Font::ALL_SANE)).maxHeight() * 1.2);