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;
152 //lyxerr << "Text::metrics: width: " << mi.base.textwidth
153 // << " maxWidth: " << max_width_ << "\nfont: " << mi.base.font << endl;
155 bool changed = false;
159 for (pit_type pit = 0, n = text_->paragraphs().size(); pit != n; ++pit) {
160 changed |= redoParagraph(pit);
161 ParagraphMetrics const & pm = par_metrics_[pit];
168 dim.asc = par_metrics_[0].ascent();
169 dim.des = h - dim.asc;
171 changed |= dim_ != dim;
177 int TextMetrics::rightMargin(ParagraphMetrics const & pm) const
179 return main_text_? pm.rightMargin(bv_->buffer()) : 0;
183 int TextMetrics::rightMargin(pit_type const pit) const
185 return main_text_? par_metrics_[pit].rightMargin(bv_->buffer()) : 0;
189 bool TextMetrics::redoParagraph(pit_type const pit)
191 Paragraph & par = text_->getPar(pit);
192 // IMPORTANT NOTE: We pass 'false' explicitely in order to not call
193 // redoParagraph() recursively inside parMetrics.
194 Dimension old_dim = parMetrics(pit, false).dim();
195 ParagraphMetrics & pm = par_metrics_[pit];
198 Buffer & buffer = bv_->buffer();
199 BufferParams const & bparams = buffer.params();
200 main_text_ = (text_ == &buffer.text());
201 bool changed = false;
203 // FIXME This check ought to be done somewhere else. It is the reason
204 // why text_ is not const. But then, where else to do it?
205 // Well, how can you end up with either (a) a biblio environment that
206 // has no InsetBibitem or (b) a biblio environment with more than one
207 // InsetBibitem? I think the answer is: when paragraphs are merged;
208 // when layout is set; when material is pasted.
209 int const moveCursor = par.checkBiblio(buffer.params().trackChanges);
211 const_cast<Cursor &>(bv_->cursor()).posRight();
212 else if (moveCursor < 0) {
213 Cursor & cursor = const_cast<Cursor &>(bv_->cursor());
214 if (cursor.pos() >= -moveCursor)
218 // Optimisation: this is used in the next two loops
219 // so better to calculate that once here.
220 int const right_margin = rightMargin(pm);
223 // FIXME: We should always use getFont(), see documentation of
224 // noFontChange() in Inset.h.
225 Font const bufferfont = buffer.params().getFont();
226 InsetList::const_iterator ii = par.insetlist.begin();
227 InsetList::const_iterator iend = par.insetlist.end();
228 for (; ii != iend; ++ii) {
229 Dimension old_dim = ii->inset->dimension();
231 int const w = max_width_ - text_->leftMargin(buffer, max_width_, pit, ii->pos)
233 Font const & font = ii->inset->noFontChange() ?
234 bufferfont : text_->getFont(buffer, par, ii->pos);
235 MetricsInfo mi(bv_, font, w);
236 changed |= ii->inset->metrics(mi, dim);
237 changed |= (old_dim != dim);
240 par.setBeginOfBody();
242 size_t row_index = 0;
243 // maximum pixel width of a row
244 int width = max_width_ - right_margin; // - leftMargin(buffer, max_width_, pit, row);
247 pos_type end = rowBreakPoint(width, pit, first);
248 dim.wid = rowWidth(right_margin, pit, first, end);
249 boost::tie(dim.asc, dim.des) = rowHeight(pit, first, end);
250 if (row_index == pm.rows().size())
251 pm.rows().push_back(Row());
252 Row & row = pm.rows()[row_index];
253 row.setChanged(false);
256 row.setDimension(dim);
257 computeRowMetrics(pit, row);
258 pm.computeRowSignature(row, bparams);
262 pm.dim().wid = std::max(pm.dim().wid, dim.wid);
263 pm.dim().des += dim.height();
264 } while (first < par.size());
266 if (row_index < pm.rows().size())
267 pm.rows().resize(row_index);
269 // Make sure that if a par ends in newline, there is one more row
271 if (first > 0 && par.isNewline(first - 1)) {
273 dim.wid = rowWidth(right_margin, pit, first, first);
274 boost::tie(dim.asc, dim.des) = rowHeight(pit, first, first);
275 if (row_index == pm.rows().size())
276 pm.rows().push_back(Row());
277 Row & row = pm.rows()[row_index];
278 row.setChanged(false);
281 row.setDimension(dim);
282 computeRowMetrics(pit, row);
283 pm.computeRowSignature(row, bparams);
284 pm.dim().des += dim.height();
287 pm.dim().asc += pm.rows()[0].ascent();
288 pm.dim().des -= pm.rows()[0].ascent();
290 changed |= old_dim.height() != pm.dim().height();
295 void TextMetrics::computeRowMetrics(pit_type const pit,
298 Buffer & buffer = bv_->buffer();
299 Paragraph const & par = text_->getPar(pit);
301 double w = dim_.wid - row.width();
303 bool const is_rtl = text_->isRTL(buffer, par);
305 row.x = rightMargin(pit);
307 row.x = text_->leftMargin(buffer, max_width_, pit, row.pos());
309 // is there a manual margin with a manual label
310 LayoutPtr const & layout = par.layout();
312 if (layout->margintype == MARGIN_MANUAL
313 && layout->labeltype == LABEL_MANUAL) {
314 /// We might have real hfills in the label part
315 int nlh = numberOfLabelHfills(par, row);
317 // A manual label par (e.g. List) has an auto-hfill
318 // between the label text and the body of the
320 // But we don't want to do this auto hfill if the par
325 if (nlh && !par.getLabelWidthString().empty())
326 row.label_hfill = labelFill(pit, row) / double(nlh);
329 // are there any hfills in the row?
330 int const nh = numberOfHfills(par, row);
335 // we don't have to look at the alignment if it is ALIGN_LEFT and
336 // if the row is already larger then the permitted width as then
337 // we force the LEFT_ALIGN'edness!
338 } else if (int(row.width()) < max_width_) {
339 // is it block, flushleft or flushright?
340 // set x how you need it
342 if (par.params().align() == LYX_ALIGN_LAYOUT)
343 align = layout->align;
345 align = par.params().align();
347 // Display-style insets should always be on a centred row
348 // The test on par.size() is to catch zero-size pars, which
349 // would trigger the assert in Paragraph::getInset().
350 //inset = par.size() ? par.getInset(row.pos()) : 0;
351 if (row.pos() < par.size()
352 && par.isInset(row.pos()))
354 switch(par.getInset(row.pos())->display()) {
355 case Inset::AlignLeft:
356 align = LYX_ALIGN_BLOCK;
358 case Inset::AlignCenter:
359 align = LYX_ALIGN_CENTER;
362 case Inset::AlignRight:
363 // unchanged (use align)
369 case LYX_ALIGN_BLOCK: {
370 int const ns = numberOfSeparators(par, row);
371 bool disp_inset = false;
372 if (row.endpos() < par.size()) {
373 Inset const * in = par.getInset(row.endpos());
375 disp_inset = in->display();
377 // If we have separators, this is not the last row of a
378 // par, does not end in newline, and is not row above a
379 // display inset... then stretch it
381 && row.endpos() < par.size()
382 && !par.isNewline(row.endpos() - 1)
385 row.separator = w / ns;
391 case LYX_ALIGN_RIGHT:
394 case LYX_ALIGN_CENTER:
401 pos_type body_pos = par.beginOfBody();
402 pos_type end = row.endpos();
405 && (body_pos > end || !par.isLineSeparator(body_pos - 1)))
407 row.x += theFontMetrics(text_->getLabelFont(buffer, par)).
408 width(layout->labelsep);
410 row.x += row.label_hfill;
416 int TextMetrics::labelFill(pit_type const pit, Row const & row) const
418 Buffer & buffer = bv_->buffer();
419 Paragraph const & par = text_->getPar(pit);
421 pos_type last = par.beginOfBody();
422 BOOST_ASSERT(last > 0);
424 // -1 because a label ends with a space that is in the label
427 // a separator at this end does not count
428 if (par.isLineSeparator(last))
432 for (pos_type i = row.pos(); i <= last; ++i)
433 w += singleWidth(pit, i);
435 docstring const & label = par.params().labelWidthString();
439 FontMetrics const & fm
440 = theFontMetrics(text_->getLabelFont(buffer, par));
442 return max(0, fm.width(label) - w);
448 // this needs special handling - only newlines count as a break point
449 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
451 pos_type const end = par.size();
454 if (par.isNewline(i))
463 int TextMetrics::labelEnd(pit_type const pit) const
465 // labelEnd is only needed if the layout fills a flushleft label.
466 if (text_->getPar(pit).layout()->margintype != MARGIN_MANUAL)
468 // return the beginning of the body
469 return text_->leftMargin(bv_->buffer(), max_width_, pit);
473 pit_type TextMetrics::rowBreakPoint(int width, pit_type const pit,
476 Buffer & buffer = bv_->buffer();
477 ParagraphMetrics const & pm = par_metrics_[pit];
478 Paragraph const & par = text_->getPar(pit);
479 pos_type const end = par.size();
480 if (pos == end || width < 0)
483 LayoutPtr const & layout = par.layout();
485 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX)
486 return addressBreakPoint(pos, par);
488 pos_type const body_pos = par.beginOfBody();
491 // Now we iterate through until we reach the right margin
492 // or the end of the par, then choose the possible break
495 int label_end = labelEnd(pit);
496 int const left = text_->leftMargin(buffer, max_width_, pit, pos);
499 // pixel width since last breakpoint
502 FontIterator fi = FontIterator(buffer, *text_, par, pos);
503 pos_type point = end;
505 for ( ; i < end; ++i, ++fi) {
506 int thiswidth = pm.singleWidth(i, *fi);
508 // add the auto-hfill from label end to the body
509 if (body_pos && i == body_pos) {
510 FontMetrics const & fm = theFontMetrics(
511 text_->getLabelFont(buffer, par));
512 int add = fm.width(layout->labelsep);
513 if (par.isLineSeparator(i - 1))
514 add -= singleWidth(pit, i - 1);
516 add = std::max(add, label_end - x);
521 chunkwidth += thiswidth;
523 // break before a character that will fall off
524 // the right of the row
526 // if no break before, break here
527 if (point == end || chunkwidth >= width - left) {
534 // exit on last registered breakpoint:
538 if (par.isNewline(i)) {
544 if (par.isInset(i + 1) && par.getInset(i + 1)->display()) {
549 if (par.isInset(i) && par.getInset(i)->display()) {
555 if (!par.isInset(i) || par.getInset(i)->isChar()) {
556 // some insets are line separators too
557 if (par.isLineSeparator(i)) {
558 // register breakpoint:
565 // maybe found one, but the par is short enough.
566 if (i == end && x < width)
569 // manual labels cannot be broken in LaTeX. But we
570 // want to make our on-screen rendering of footnotes
572 if (body_pos && point < body_pos)
579 int TextMetrics::rowWidth(int right_margin, pit_type const pit,
580 pos_type const first, pos_type const end) const
582 Buffer & buffer = bv_->buffer();
583 // get the pure distance
584 ParagraphMetrics const & pm = par_metrics_[pit];
585 Paragraph const & par = text_->getPar(pit);
586 int w = text_->leftMargin(buffer, max_width_, pit, first);
587 int label_end = labelEnd(pit);
589 pos_type const body_pos = par.beginOfBody();
593 FontIterator fi = FontIterator(buffer, *text_, par, i);
594 for ( ; i < end; ++i, ++fi) {
595 if (body_pos > 0 && i == body_pos) {
596 FontMetrics const & fm = theFontMetrics(
597 text_->getLabelFont(buffer, par));
598 w += fm.width(par.layout()->labelsep);
599 if (par.isLineSeparator(i - 1))
600 w -= singleWidth(pit, i - 1);
601 w = max(w, label_end);
603 w += pm.singleWidth(i, *fi);
607 if (body_pos > 0 && body_pos >= end) {
608 FontMetrics const & fm = theFontMetrics(
609 text_->getLabelFont(buffer, par));
610 w += fm.width(par.layout()->labelsep);
611 if (end > 0 && par.isLineSeparator(end - 1))
612 w -= singleWidth(pit, end - 1);
613 w = max(w, label_end);
616 return w + right_margin;
620 boost::tuple<int, int> TextMetrics::rowHeight(pit_type const pit, pos_type const first,
621 pos_type const end) const
623 Paragraph const & par = text_->getPar(pit);
624 // get the maximum ascent and the maximum descent
625 double layoutasc = 0;
626 double layoutdesc = 0;
627 double const dh = defaultRowHeight();
629 // ok, let us initialize the maxasc and maxdesc value.
630 // Only the fontsize count. The other properties
631 // are taken from the layoutfont. Nicer on the screen :)
632 LayoutPtr const & layout = par.layout();
634 // as max get the first character of this row then it can
635 // increase but not decrease the height. Just some point to
636 // start with so we don't have to do the assignment below too
638 Buffer const & buffer = bv_->buffer();
639 Font font = text_->getFont(buffer, par, first);
640 Font::FONT_SIZE const tmpsize = font.size();
641 font = text_->getLayoutFont(buffer, pit);
642 Font::FONT_SIZE const size = font.size();
643 font.setSize(tmpsize);
645 Font labelfont = text_->getLabelFont(buffer, par);
647 FontMetrics const & labelfont_metrics = theFontMetrics(labelfont);
648 FontMetrics const & fontmetrics = theFontMetrics(font);
650 // these are minimum values
651 double const spacing_val = layout->spacing.getValue()
652 * text_->spacing(buffer, par);
653 //lyxerr << "spacing_val = " << spacing_val << endl;
654 int maxasc = int(fontmetrics.maxAscent() * spacing_val);
655 int maxdesc = int(fontmetrics.maxDescent() * spacing_val);
657 // insets may be taller
658 InsetList::const_iterator ii = par.insetlist.begin();
659 InsetList::const_iterator iend = par.insetlist.end();
660 for ( ; ii != iend; ++ii) {
661 if (ii->pos >= first && ii->pos < end) {
662 maxasc = max(maxasc, ii->inset->ascent());
663 maxdesc = max(maxdesc, ii->inset->descent());
667 // Check if any custom fonts are larger (Asger)
668 // This is not completely correct, but we can live with the small,
669 // cosmetic error for now.
672 Font::FONT_SIZE maxsize =
673 par.highestFontInRange(first, end, size);
674 if (maxsize > font.size()) {
675 // use standard paragraph font with the maximal size
677 maxfont.setSize(maxsize);
678 FontMetrics const & maxfontmetrics = theFontMetrics(maxfont);
679 maxasc = max(maxasc, maxfontmetrics.maxAscent());
680 maxdesc = max(maxdesc, maxfontmetrics.maxDescent());
683 // This is nicer with box insets:
687 ParagraphList const & pars = text_->paragraphs();
691 BufferParams const & bufparams = buffer.params();
692 // some parskips VERY EASY IMPLEMENTATION
693 if (bufparams.paragraph_separation
694 == BufferParams::PARSEP_SKIP
695 && par.ownerCode() != Inset::ERT_CODE
696 && par.ownerCode() != Inset::LISTINGS_CODE
698 && ((layout->isParagraph() && par.getDepth() == 0)
699 || (pars[pit - 1].layout()->isParagraph()
700 && pars[pit - 1].getDepth() == 0)))
702 maxasc += bufparams.getDefSkip().inPixels(*bv_);
705 if (par.params().startOfAppendix())
706 maxasc += int(3 * dh);
708 // This is special code for the chapter, since the label of this
709 // layout is printed in an extra row
710 if (layout->counter == "chapter"
711 && !par.params().labelString().empty()) {
712 labeladdon = int(labelfont_metrics.maxHeight()
713 * layout->spacing.getValue()
714 * text_->spacing(buffer, par));
717 // special code for the top label
718 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
719 || layout->labeltype == LABEL_BIBLIO
720 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
721 && isFirstInSequence(pit, pars)
722 && !par.getLabelstring().empty())
725 labelfont_metrics.maxHeight()
726 * layout->spacing.getValue()
727 * text_->spacing(buffer, par)
728 + (layout->topsep + layout->labelbottomsep) * dh);
731 // Add the layout spaces, for example before and after
732 // a section, or between the items of a itemize or enumerate
735 pit_type prev = depthHook(pit, pars, par.getDepth());
736 Paragraph const & prevpar = pars[prev];
738 && prevpar.layout() == layout
739 && prevpar.getDepth() == par.getDepth()
740 && prevpar.getLabelWidthString()
741 == par.getLabelWidthString()) {
742 layoutasc = layout->itemsep * dh;
743 } else if (pit != 0 || first != 0) {
744 if (layout->topsep > 0)
745 layoutasc = layout->topsep * dh;
748 prev = outerHook(pit, pars);
749 if (prev != pit_type(pars.size())) {
750 maxasc += int(pars[prev].layout()->parsep * dh);
751 } else if (pit != 0) {
752 Paragraph const & prevpar = pars[pit - 1];
753 if (prevpar.getDepth() != 0 ||
754 prevpar.layout() == layout) {
755 maxasc += int(layout->parsep * dh);
760 // is it a bottom line?
761 if (end >= par.size()) {
762 // add the layout spaces, for example before and after
763 // a section, or between the items of a itemize or enumerate
765 pit_type nextpit = pit + 1;
766 if (nextpit != pit_type(pars.size())) {
771 if (pars[cpit].getDepth() > pars[nextpit].getDepth()) {
772 usual = pars[cpit].layout()->bottomsep * dh;
773 cpit = depthHook(cpit, pars, pars[nextpit].getDepth());
774 if (pars[cpit].layout() != pars[nextpit].layout()
775 || pars[nextpit].getLabelWidthString() != pars[cpit].getLabelWidthString())
777 unusual = pars[cpit].layout()->bottomsep * dh;
779 layoutdesc = max(unusual, usual);
780 } else if (pars[cpit].getDepth() == pars[nextpit].getDepth()) {
781 if (pars[cpit].layout() != pars[nextpit].layout()
782 || pars[nextpit].getLabelWidthString() != pars[cpit].getLabelWidthString())
783 layoutdesc = int(pars[cpit].layout()->bottomsep * dh);
788 // incalculate the layout spaces
789 maxasc += int(layoutasc * 2 / (2 + pars[pit].getDepth()));
790 maxdesc += int(layoutdesc * 2 / (2 + pars[pit].getDepth()));
792 // FIXME: the correct way is to do the following is to move the
793 // following code in another method specially tailored for the
794 // main Text. The following test is thus bogus.
795 // Top and bottom margin of the document (only at top-level)
797 if (pit == 0 && first == 0)
799 if (pit + 1 == pit_type(pars.size()) &&
801 !(end > 0 && par.isNewline(end - 1)))
805 return boost::make_tuple(maxasc + labeladdon, maxdesc);
809 // x is an absolute screen coord
810 // returns the column near the specified x-coordinate of the row
811 // x is set to the real beginning of this column
812 pos_type TextMetrics::getColumnNearX(pit_type const pit,
813 Row const & row, int & x, bool & boundary) const
815 Buffer const & buffer = bv_->buffer();
817 /// For the main Text, it is possible that this pit is not
818 /// yet in the CoordCache when moving cursor up.
819 /// x Paragraph coordinate is always 0 for main text anyway.
820 int const xo = main_text_? 0 : bv_->coordCache().get(text_, pit).x_;
822 Paragraph const & par = text_->getPar(pit);
823 ParagraphMetrics const & pm = par_metrics_[pit];
825 bidi.computeTables(par, buffer, row);
827 pos_type vc = row.pos();
828 pos_type end = row.endpos();
830 LayoutPtr const & layout = par.layout();
832 bool left_side = false;
834 pos_type body_pos = par.beginOfBody();
837 double last_tmpx = tmpx;
840 (body_pos > end || !par.isLineSeparator(body_pos - 1)))
843 // check for empty row
849 while (vc < end && tmpx <= x) {
850 c = bidi.vis2log(vc);
852 if (body_pos > 0 && c == body_pos - 1) {
853 FontMetrics const & fm = theFontMetrics(
854 text_->getLabelFont(buffer, par));
855 tmpx += row.label_hfill + fm.width(layout->labelsep);
856 if (par.isLineSeparator(body_pos - 1))
857 tmpx -= singleWidth(pit, body_pos - 1);
860 if (pm.hfillExpansion(row, c)) {
861 tmpx += singleWidth(pit, c);
865 tmpx += row.label_hfill;
866 } else if (par.isSeparator(c)) {
867 tmpx += singleWidth(pit, c);
869 tmpx += row.separator;
871 tmpx += singleWidth(pit, c);
876 if ((tmpx + last_tmpx) / 2 > x) {
881 BOOST_ASSERT(vc <= end); // This shouldn't happen.
884 // This (rtl_support test) is not needed, but gives
885 // some speedup if rtl_support == false
886 bool const lastrow = lyxrc.rtl_support && row.endpos() == par.size();
888 // If lastrow is false, we don't need to compute
890 bool const rtl = lastrow ? text_->isRTL(buffer, par) : false;
892 ((rtl && left_side && vc == row.pos() && x < tmpx - 5) ||
893 (!rtl && !left_side && vc == end && x > tmpx + 5)))
895 else if (vc == row.pos()) {
896 c = bidi.vis2log(vc);
897 if (bidi.level(c) % 2 == 1)
900 c = bidi.vis2log(vc - 1);
901 bool const rtl = (bidi.level(c) % 2 == 1);
902 if (left_side == rtl) {
904 boundary = text_->isRTLBoundary(buffer, par, c);
908 // I believe this code is not needed anymore (Jug 20050717)
910 // The following code is necessary because the cursor position past
911 // the last char in a row is logically equivalent to that before
912 // the first char in the next row. That's why insets causing row
913 // divisions -- Newline and display-style insets -- must be treated
914 // specially, so cursor up/down doesn't get stuck in an air gap -- MV
915 // Newline inset, air gap below:
916 if (row.pos() < end && c >= end && par.isNewline(end - 1)) {
917 if (bidi.level(end -1) % 2 == 0)
918 tmpx -= singleWidth(pit, end - 1);
920 tmpx += singleWidth(pit, end - 1);
924 // Air gap above display inset:
925 if (row.pos() < end && c >= end && end < par.size()
926 && par.isInset(end) && par.getInset(end)->display()) {
929 // Air gap below display inset:
930 if (row.pos() < end && c >= end && par.isInset(end - 1)
931 && par.getInset(end - 1)->display()) {
937 pos_type const col = c - row.pos();
939 if (!c || end == par.size())
942 if (c==end && !par.isLineSeparator(c-1) && !par.isNewline(c-1)) {
947 return min(col, end - 1 - row.pos());
951 pos_type TextMetrics::x2pos(pit_type pit, int row, int x) const
953 ParagraphMetrics const & pm = par_metrics_[pit];
954 BOOST_ASSERT(!pm.rows().empty());
955 BOOST_ASSERT(row < int(pm.rows().size()));
957 Row const & r = pm.rows()[row];
958 return r.pos() + getColumnNearX(pit, r, x, bound);
962 int TextMetrics::singleWidth(pit_type pit, pos_type pos) const
964 Buffer const & buffer = bv_->buffer();
965 ParagraphMetrics const & pm = par_metrics_[pit];
967 return pm.singleWidth(pos, text_->getFont(buffer, text_->getPar(pit), pos));
971 // only used for inset right now. should also be used for main text
972 void TextMetrics::draw(PainterInfo & pi, int x, int y) const
974 if (par_metrics_.empty())
977 ParMetricsCache::const_iterator it = par_metrics_.begin();
978 ParMetricsCache::const_iterator const end = par_metrics_.end();
979 y -= it->second.ascent();
980 for (; it != end; ++it) {
981 ParagraphMetrics const & pmi = it->second;
983 drawParagraph(pi, it->first, x, y);
989 void TextMetrics::drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const
991 // lyxerr << " paintPar: pit: " << pit << " at y: " << y << endl;
992 int const ww = bv_->workHeight();
994 bv_->coordCache().parPos()[text_][pit] = Point(x, y);
996 ParagraphMetrics const & pm = par_metrics_[pit];
997 if (pm.rows().empty())
1000 RowList::const_iterator const rb = pm.rows().begin();
1001 RowList::const_iterator const re = pm.rows().end();
1006 for (RowList::const_iterator rit = rb; rit != re; ++rit) {
1009 bool const inside = (y + rit->descent() >= 0
1010 && y - rit->ascent() < ww);
1011 // it is not needed to draw on screen if we are not inside.
1012 pi.pain.setDrawingEnabled(inside);
1013 RowPainter rp(pi, *text_, pit, *rit, bidi, x, y);
1015 // Row signature; has row changed since last paint?
1016 bool row_has_changed = rit->changed();
1018 if (!pi.full_repaint && !row_has_changed) {
1019 // Paint the only the insets if the text itself is
1021 rp.paintOnlyInsets();
1022 y += rit->descent();
1026 // Paint the row if a full repaint has been requested or it has
1028 // Clear background of this row
1029 // (if paragraph background was not cleared)
1030 if (!pi.full_repaint && row_has_changed) {
1031 pi.pain.fillRectangle(x, y - rit->ascent(),
1032 width(), rit->height(),
1033 text_->backgroundColor());
1036 // Instrumentation for testing row cache (see also
1038 if (lyxerr.debugging(Debug::PAINTING)) {
1039 if (text_->isMainText(bv_->buffer()))
1040 LYXERR(Debug::PAINTING) << "\n{" <<
1041 pi.full_repaint << row_has_changed << "}";
1043 LYXERR(Debug::PAINTING) << "[" <<
1044 pi.full_repaint << row_has_changed << "]";
1047 // Backup full_repaint status and force full repaint
1048 // for inner insets as the Row has been cleared out.
1049 bool tmp = pi.full_repaint;
1050 pi.full_repaint = true;
1053 rp.paintChangeBar();
1059 y += rit->descent();
1060 // Restore full_repaint status.
1061 pi.full_repaint = tmp;
1063 // Re-enable screen drawing for future use of the painter.
1064 pi.pain.setDrawingEnabled(true);
1066 //LYXERR(Debug::PAINTING) << "." << endl;
1070 // only used for inset right now. should also be used for main text
1071 void TextMetrics::drawSelection(PainterInfo & pi, int x, int) const
1073 Cursor & cur = bv_->cursor();
1074 if (!cur.selection())
1076 if (!ptr_cmp(cur.text(), text_))
1079 LYXERR(Debug::DEBUG)
1080 << BOOST_CURRENT_FUNCTION
1081 << "draw selection at " << x
1084 DocIterator beg = cur.selectionBegin();
1085 DocIterator end = cur.selectionEnd();
1087 // the selection doesn't touch the visible screen?
1088 if (bv_funcs::status(bv_, beg) == bv_funcs::CUR_BELOW
1089 || bv_funcs::status(bv_, end) == bv_funcs::CUR_ABOVE)
1092 ParagraphMetrics const & pm1 = par_metrics_[beg.pit()];
1093 ParagraphMetrics const & pm2 = par_metrics_[end.pit()];
1094 Row const & row1 = pm1.getRow(beg.pos(), beg.boundary());
1095 Row const & row2 = pm2.getRow(end.pos(), end.boundary());
1099 bool const clipAbove =
1100 (bv_funcs::status(bv_, beg) == bv_funcs::CUR_ABOVE);
1104 middleTop = bv_funcs::getPos(*bv_, beg, beg.boundary()).y_ + row1.descent();
1108 bool const clipBelow =
1109 (bv_funcs::status(bv_, end) == bv_funcs::CUR_BELOW);
1111 middleBottom = bv_->workHeight();
1113 middleBottom = bv_funcs::getPos(*bv_, end, end.boundary()).y_ - row2.ascent();
1115 // start and end in the same line?
1116 if (!(clipAbove || clipBelow) && &row1 == &row2)
1117 // then only draw this row's selection
1118 drawRowSelection(pi, x, row1, beg, end, false, false);
1122 DocIterator begRowEnd = beg;
1123 begRowEnd.pos() = row1.endpos();
1124 begRowEnd.boundary(true);
1126 // draw upper rectangle
1127 drawRowSelection(pi, x, row1, beg, begRowEnd, false, true);
1130 if (middleTop < middleBottom) {
1131 // draw middle rectangle
1132 pi.pain.fillRectangle(x, middleTop, width(), middleBottom - middleTop,
1138 DocIterator endRowBeg = end;
1139 endRowBeg.pos() = row2.pos();
1140 endRowBeg.boundary(false);
1142 // draw low rectangle
1143 drawRowSelection(pi, x, row2, endRowBeg, end, true, false);
1149 void TextMetrics::drawRowSelection(PainterInfo & pi, int x, Row const & row,
1150 DocIterator const & beg, DocIterator const & end,
1151 bool drawOnBegMargin, bool drawOnEndMargin) const
1153 Buffer & buffer = bv_->buffer();
1154 DocIterator cur = beg;
1155 int x1 = text_->cursorX(*bv_, beg.top(), beg.boundary());
1156 int x2 = text_->cursorX(*bv_, end.top(), end.boundary());
1157 int y1 = bv_funcs::getPos(*bv_, cur, cur.boundary()).y_ - row.ascent();
1158 int y2 = y1 + row.height();
1161 if (drawOnBegMargin) {
1162 if (text_->isRTL(buffer, beg.paragraph()))
1163 pi.pain.fillRectangle(x + x1, y1, width() - x1, y2 - y1, Color::selection);
1165 pi.pain.fillRectangle(x, y1, x1, y2 - y1, Color::selection);
1168 if (drawOnEndMargin) {
1169 if (text_->isRTL(buffer, beg.paragraph()))
1170 pi.pain.fillRectangle(x, y1, x2, y2 - y1, Color::selection);
1172 pi.pain.fillRectangle(x + x2, y1, width() - x2, y2 - y1, Color::selection);
1175 // if we are on a boundary from the beginning, it's probably
1176 // a RTL boundary and we jump to the other side directly as this
1177 // segement is 0-size and confuses the logic below
1179 cur.boundary(false);
1181 // go through row and draw from RTL boundary to RTL boundary
1183 bool drawNow = false;
1185 // simplified cursorRight code below which does not
1186 // descend into insets and which does not go into the
1187 // next line. Compare the logic with the original cursorRight
1189 // if left of boundary -> just jump to right side
1190 // but for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
1191 if (cur.boundary()) {
1192 cur.boundary(false);
1193 } else if (text_->isRTLBoundary(buffer, cur.paragraph(), cur.pos() + 1)) {
1194 // in front of RTL boundary -> Stay on this side of the boundary because:
1195 // ab|cDDEEFFghi -> abc|DDEEFFghi
1204 if (cur.pos() == row.endpos())
1209 // the previous segment was just drawn, now the next starts
1210 x1 = text_->cursorX(*bv_, cur.top(), cur.boundary());
1213 if (!(cur < end) || drawNow) {
1214 x2 = text_->cursorX(*bv_, cur.top(), cur.boundary());
1215 pi.pain.fillRectangle(x + min(x1,x2), y1, abs(x2 - x1), y2 - y1,
1218 // reset x1, so it is set again next round (which will be on the
1219 // right side of a boundary or at the selection end)
1225 //int Text::pos2x(pit_type pit, pos_type pos) const
1227 // ParagraphMetrics const & pm = par_metrics_[pit];
1228 // Row const & r = pm.rows()[row];
1234 int defaultRowHeight()
1236 return int(theFontMetrics(Font(Font::ALL_SANE)).maxHeight() * 1.2);