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"
24 #include "buffer_funcs.h"
25 #include "BufferParams.h"
26 #include "BufferView.h"
27 #include "bufferview_funcs.h"
29 #include "CutAndPaste.h"
31 #include "FontIterator.h"
32 #include "FuncRequest.h"
35 #include "MetricsInfo.h"
36 #include "paragraph_funcs.h"
37 #include "ParagraphParameters.h"
38 #include "ParIterator.h"
39 #include "rowpainter.h"
44 #include "frontends/FontMetrics.h"
45 #include "frontends/Painter.h"
47 #include <boost/current_function.hpp>
57 using frontend::FontMetrics;
61 int numberOfSeparators(Paragraph const & par, Row const & row)
63 pos_type const first = max(row.pos(), par.beginOfBody());
64 pos_type const last = row.endpos() - 1;
66 for (pos_type p = first; p < last; ++p) {
67 if (par.isSeparator(p))
74 int numberOfLabelHfills(Paragraph const & par, Row const & row)
76 pos_type last = row.endpos() - 1;
77 pos_type first = row.pos();
79 // hfill *DO* count at the beginning of paragraphs!
81 while (first < last && par.isHfill(first))
85 last = min(last, par.beginOfBody());
87 for (pos_type p = first; p < last; ++p) {
95 int numberOfHfills(Paragraph const & par, Row const & row)
97 pos_type const last = row.endpos();
98 pos_type first = row.pos();
100 // hfill *DO* count at the beginning of paragraphs!
102 while (first < last && par.isHfill(first))
106 first = max(first, par.beginOfBody());
109 for (pos_type p = first; p < last; ++p) {
118 TextMetrics::TextMetrics(BufferView * bv, Text * text)
119 : bv_(bv), text_(text)
122 max_width_ = bv_->workWidth();
123 dim_.wid = max_width_;
127 //text_->updateLabels(bv->buffer());
131 bool TextMetrics::has(pit_type pit) const
133 return par_metrics_.find(pit) != par_metrics_.end();
137 ParagraphMetrics const & TextMetrics::parMetrics(pit_type pit) const
139 return const_cast<TextMetrics *>(this)->parMetrics(pit, true);
144 pair<pit_type, ParagraphMetrics const *> TextMetrics::first() const
146 ParMetricsCache::const_iterator it = par_metrics_.begin();
147 return make_pair(it->first, &it->second);
151 pair<pit_type, ParagraphMetrics const *> TextMetrics::last() const
153 ParMetricsCache::const_reverse_iterator it = par_metrics_.rbegin();
154 return make_pair(it->first, &it->second);
158 ParagraphMetrics & TextMetrics::parMetrics(pit_type pit,
161 ParMetricsCache::iterator pmc_it = par_metrics_.find(pit);
162 if (pmc_it == par_metrics_.end()) {
163 pmc_it = par_metrics_.insert(
164 make_pair(pit, ParagraphMetrics(text_->getPar(pit)))).first;
166 if (pmc_it->second.rows().empty() && redo) {
169 return pmc_it->second;
173 int TextMetrics::parPosition(pit_type pit) const
175 pair<pit_type, ParagraphMetrics> first = *par_metrics_.begin();
176 pair<pit_type, ParagraphMetrics> last = *par_metrics_.rbegin();
177 if (pit < first.first)
179 else if (pit > last.first)
182 return par_metrics_[pit].position();
186 bool TextMetrics::metrics(MetricsInfo & mi, Dimension & dim, int min_width)
188 BOOST_ASSERT(mi.base.textwidth);
189 max_width_ = mi.base.textwidth;
190 // backup old dimension.
191 Dimension const old_dim = dim_;
194 dim_.wid = min_width;
195 pit_type const npar = text_->paragraphs().size();
197 // If there is more than one row, expand the text to
198 // the full allowable width.
199 dim_.wid = max_width_;
201 //lyxerr << "TextMetrics::metrics: width: " << mi.base.textwidth
202 // << " maxWidth: " << max_width_ << "\nfont: " << mi.base.font << endl;
204 bool changed = false;
206 for (pit_type pit = 0; pit != npar; ++pit) {
207 changed |= redoParagraph(pit);
208 ParagraphMetrics const & pm = par_metrics_[pit];
210 if (dim_.wid < pm.width())
211 dim_.wid = pm.width();
214 dim_.asc = par_metrics_[0].ascent();
215 dim_.des = h - dim_.asc;
216 //lyxerr << "dim_.wid " << dim_.wid << endl;
217 //lyxerr << "dim_.asc " << dim_.asc << endl;
218 //lyxerr << "dim_.des " << dim_.des << endl;
220 changed |= dim_ != old_dim;
226 int TextMetrics::rightMargin(ParagraphMetrics const & pm) const
228 return main_text_? pm.rightMargin(bv_->buffer()) : 0;
232 int TextMetrics::rightMargin(pit_type const pit) const
234 return main_text_? par_metrics_[pit].rightMargin(bv_->buffer()) : 0;
238 void TextMetrics::applyOuterFont(Font & font) const
241 lf.reduce(bv_->buffer().params().getFont());
243 lf.setLanguage(font.language());
248 Font TextMetrics::getDisplayFont(pit_type pit, pos_type pos) const
250 BOOST_ASSERT(pos >= 0);
252 ParagraphList const & pars = text_->paragraphs();
253 Paragraph const & par = pars[pit];
254 LayoutPtr const & layout = par.layout();
255 Buffer const & buffer = bv_->buffer();
257 BufferParams const & params = buffer.params();
258 pos_type const body_pos = par.beginOfBody();
260 // We specialize the 95% common case:
261 if (!par.getDepth()) {
262 Font f = par.getFontSettings(params, pos);
263 if (!text_->isMainText(buffer))
265 bool lab = layout->labeltype == LABEL_MANUAL && pos < body_pos;
267 Font const & lf = lab ? layout->labelfont : layout->font;
268 Font rlf = lab ? layout->reslabelfont : layout->resfont;
270 // In case the default family has been customized
271 if (lf.family() == Font::INHERIT_FAMILY)
272 rlf.setFamily(params.getFont().family());
273 return f.realize(rlf);
276 // The uncommon case need not be optimized as much
277 Font const & layoutfont = pos < body_pos ?
278 layout->labelfont : layout->font;
280 Font font = par.getFontSettings(params, pos);
281 font.realize(layoutfont);
283 if (!text_->isMainText(buffer))
284 applyOuterFont(font);
286 // Realize against environment font information
287 // NOTE: the cast to pit_type should be removed when pit_type
288 // changes to a unsigned integer.
289 if (pit < pit_type(pars.size()))
290 font.realize(outerFont(pit, pars));
292 // Realize with the fonts of lesser depth.
293 font.realize(params.getFont());
299 bool TextMetrics::isRTL(CursorSlice const & sl, bool boundary) const
301 if (!lyxrc.rtl_support || !sl.text())
305 if (boundary && sl.pos() > 0)
308 return getDisplayFont(sl.pit(), sl.pos() + correction).isVisibleRightToLeft();
312 bool TextMetrics::isRTLBoundary(pit_type pit, pos_type pos) const
314 if (!lyxrc.rtl_support)
317 // no RTL boundary at line start
321 Paragraph const & par = text_->getPar(pit);
323 bool left = getDisplayFont(pit, pos - 1).isVisibleRightToLeft();
325 if (pos == par.size())
326 right = par.isRTL(bv_->buffer().params());
328 right = getDisplayFont(pit, pos).isVisibleRightToLeft();
329 return left != right;
333 bool TextMetrics::isRTLBoundary(pit_type pit, pos_type pos,
334 Font const & font) const
336 if (!lyxrc.rtl_support)
339 Paragraph const & par = text_->getPar(pit);
340 bool left = font.isVisibleRightToLeft();
342 if (pos == par.size())
343 right = par.isRTL(bv_->buffer().params());
345 right = getDisplayFont(pit, pos).isVisibleRightToLeft();
346 return left != right;
350 bool TextMetrics::redoParagraph(pit_type const pit)
352 Paragraph & par = text_->getPar(pit);
353 // IMPORTANT NOTE: We pass 'false' explicitely in order to not call
354 // redoParagraph() recursively inside parMetrics.
355 Dimension old_dim = parMetrics(pit, false).dim();
356 ParagraphMetrics & pm = par_metrics_[pit];
359 Buffer & buffer = bv_->buffer();
360 BufferParams const & bparams = buffer.params();
361 main_text_ = (text_ == &buffer.text());
362 bool changed = false;
364 // FIXME This check ought to be done somewhere else. It is the reason
365 // why text_ is not const. But then, where else to do it?
366 // Well, how can you end up with either (a) a biblio environment that
367 // has no InsetBibitem or (b) a biblio environment with more than one
368 // InsetBibitem? I think the answer is: when paragraphs are merged;
369 // when layout is set; when material is pasted.
370 int const moveCursor = par.checkBiblio(buffer.params().trackChanges);
372 const_cast<Cursor &>(bv_->cursor()).posRight();
373 else if (moveCursor < 0) {
374 Cursor & cursor = const_cast<Cursor &>(bv_->cursor());
375 if (cursor.pos() >= -moveCursor)
379 // Optimisation: this is used in the next two loops
380 // so better to calculate that once here.
381 int const right_margin = rightMargin(pm);
384 // FIXME: We should always use getFont(), see documentation of
385 // noFontChange() in Inset.h.
386 Font const bufferfont = buffer.params().getFont();
387 InsetList::const_iterator ii = par.insetlist.begin();
388 InsetList::const_iterator iend = par.insetlist.end();
389 for (; ii != iend; ++ii) {
391 int const w = max_width_ - leftMargin(max_width_, pit, ii->pos)
393 Font const & font = ii->inset->noFontChange() ?
394 bufferfont : getDisplayFont(pit, ii->pos);
395 MetricsInfo mi(bv_, font, w);
396 ii->inset->metrics(mi, dim);
397 Dimension const old_dim = pm.insetDimension(ii->inset);
398 pm.setInsetDimension(ii->inset, dim);
399 changed |= (old_dim != dim);
402 Cursor const & cur = bv_->cursor();
403 DocIterator sel_beg = cur.selectionBegin();
404 DocIterator sel_end = cur.selectionEnd();
405 bool selection = cur.selection()
407 && cur.text() == text_
408 // if the anchor is outside, this is not our selection
409 && cur.anchor().text() == text_
410 && pit >= sel_beg.pit() && pit <= sel_end.pit();
412 // We care only about visible selection.
414 if (pit != sel_beg.pit()) {
418 if (pit != sel_end.pit()) {
420 sel_end.pos() = sel_end.lastpos();
424 par.setBeginOfBody();
426 size_t row_index = 0;
427 // maximum pixel width of a row
428 int width = max_width_ - right_margin; // - leftMargin(max_width_, pit, row);
431 pos_type end = rowBreakPoint(width, pit, first);
432 if (row_index || end < par.size())
433 // If there is more than one row, expand the text to
434 // the full allowable width. This setting here is needed
435 // for the computeRowMetrics() below.
436 dim_.wid = max_width_;
438 dim.wid = rowWidth(right_margin, pit, first, end);
439 boost::tie(dim.asc, dim.des) = rowHeight(pit, first, end);
440 if (row_index == pm.rows().size())
441 pm.rows().push_back(Row());
442 Row & row = pm.rows()[row_index];
443 row.setChanged(false);
447 row.setSelection(sel_beg.pos(), sel_end.pos());
449 row.setSelection(-1, -1);
450 row.setDimension(dim);
451 int const max_row_width = max(dim_.wid, dim.wid);
452 computeRowMetrics(pit, row, max_row_width);
453 pm.computeRowSignature(row, bparams);
457 pm.dim().wid = std::max(pm.dim().wid, dim.wid);
458 pm.dim().des += dim.height();
459 } while (first < par.size());
461 if (row_index < pm.rows().size())
462 pm.rows().resize(row_index);
464 // Make sure that if a par ends in newline, there is one more row
466 if (first > 0 && par.isNewline(first - 1)) {
468 dim.wid = rowWidth(right_margin, pit, first, first);
469 boost::tie(dim.asc, dim.des) = rowHeight(pit, first, first);
470 if (row_index == pm.rows().size())
471 pm.rows().push_back(Row());
472 Row & row = pm.rows()[row_index];
473 row.setChanged(false);
476 row.setDimension(dim);
477 int const max_row_width = max(dim_.wid, dim.wid);
478 computeRowMetrics(pit, row, max_row_width);
479 pm.computeRowSignature(row, bparams);
480 pm.dim().des += dim.height();
483 pm.dim().asc += pm.rows()[0].ascent();
484 pm.dim().des -= pm.rows()[0].ascent();
486 changed |= old_dim.height() != pm.dim().height();
492 void TextMetrics::computeRowMetrics(pit_type const pit,
493 Row & row, int width) const
500 Buffer & buffer = bv_->buffer();
501 Paragraph const & par = text_->getPar(pit);
503 double w = width - row.width();
504 // FIXME: put back this assertion when the crash on new doc is solved.
505 //BOOST_ASSERT(w >= 0);
507 //lyxerr << "\ndim_.wid " << dim_.wid << endl;
508 //lyxerr << "row.width() " << row.width() << endl;
509 //lyxerr << "w " << w << endl;
511 bool const is_rtl = text_->isRTL(buffer, par);
513 row.x = rightMargin(pit);
515 row.x = leftMargin(max_width_, pit, row.pos());
517 // is there a manual margin with a manual label
518 LayoutPtr const & layout = par.layout();
520 if (layout->margintype == MARGIN_MANUAL
521 && layout->labeltype == LABEL_MANUAL) {
522 /// We might have real hfills in the label part
523 int nlh = numberOfLabelHfills(par, row);
525 // A manual label par (e.g. List) has an auto-hfill
526 // between the label text and the body of the
528 // But we don't want to do this auto hfill if the par
533 if (nlh && !par.getLabelWidthString().empty())
534 row.label_hfill = labelFill(pit, row) / double(nlh);
537 // are there any hfills in the row?
538 int const nh = numberOfHfills(par, row);
543 // we don't have to look at the alignment if it is ALIGN_LEFT and
544 // if the row is already larger then the permitted width as then
545 // we force the LEFT_ALIGN'edness!
546 } else if (int(row.width()) < max_width_) {
547 // is it block, flushleft or flushright?
548 // set x how you need it
550 if (par.params().align() == LYX_ALIGN_LAYOUT)
551 align = layout->align;
553 align = par.params().align();
555 // Display-style insets should always be on a centred row
556 // The test on par.size() is to catch zero-size pars, which
557 // would trigger the assert in Paragraph::getInset().
558 //inset = par.size() ? par.getInset(row.pos()) : 0;
559 if (row.pos() < par.size()
560 && par.isInset(row.pos()))
562 switch(par.getInset(row.pos())->display()) {
563 case Inset::AlignLeft:
564 align = LYX_ALIGN_BLOCK;
566 case Inset::AlignCenter:
567 align = LYX_ALIGN_CENTER;
570 case Inset::AlignRight:
571 // unchanged (use align)
577 case LYX_ALIGN_BLOCK: {
578 int const ns = numberOfSeparators(par, row);
579 bool disp_inset = false;
580 if (row.endpos() < par.size()) {
581 Inset const * in = par.getInset(row.endpos());
583 disp_inset = in->display();
585 // If we have separators, this is not the last row of a
586 // par, does not end in newline, and is not row above a
587 // display inset... then stretch it
589 && row.endpos() < par.size()
590 && !par.isNewline(row.endpos() - 1)
593 row.separator = w / ns;
594 //lyxerr << "row.separator " << row.separator << endl;
595 //lyxerr << "ns " << ns << endl;
601 case LYX_ALIGN_RIGHT:
604 case LYX_ALIGN_CENTER:
611 pos_type body_pos = par.beginOfBody();
612 pos_type end = row.endpos();
615 && (body_pos > end || !par.isLineSeparator(body_pos - 1)))
617 row.x += theFontMetrics(text_->getLabelFont(buffer, par)).
618 width(layout->labelsep);
620 row.x += row.label_hfill;
626 int TextMetrics::labelFill(pit_type const pit, Row const & row) const
628 Buffer & buffer = bv_->buffer();
629 Paragraph const & par = text_->getPar(pit);
631 pos_type last = par.beginOfBody();
632 BOOST_ASSERT(last > 0);
634 // -1 because a label ends with a space that is in the label
637 // a separator at this end does not count
638 if (par.isLineSeparator(last))
642 for (pos_type i = row.pos(); i <= last; ++i)
643 w += singleWidth(pit, i);
645 docstring const & label = par.params().labelWidthString();
649 FontMetrics const & fm
650 = theFontMetrics(text_->getLabelFont(buffer, par));
652 return max(0, fm.width(label) - w);
658 // this needs special handling - only newlines count as a break point
659 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
661 pos_type const end = par.size();
664 if (par.isNewline(i))
673 int TextMetrics::labelEnd(pit_type const pit) const
675 // labelEnd is only needed if the layout fills a flushleft label.
676 if (text_->getPar(pit).layout()->margintype != MARGIN_MANUAL)
678 // return the beginning of the body
679 return leftMargin(max_width_, pit);
683 pit_type TextMetrics::rowBreakPoint(int width, pit_type const pit,
686 Buffer & buffer = bv_->buffer();
687 ParagraphMetrics const & pm = par_metrics_[pit];
688 Paragraph const & par = text_->getPar(pit);
689 pos_type const end = par.size();
690 if (pos == end || width < 0)
693 LayoutPtr const & layout = par.layout();
695 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX)
696 return addressBreakPoint(pos, par);
698 pos_type const body_pos = par.beginOfBody();
701 // Now we iterate through until we reach the right margin
702 // or the end of the par, then choose the possible break
705 int label_end = labelEnd(pit);
706 int const left = leftMargin(max_width_, pit, pos);
709 // pixel width since last breakpoint
712 FontIterator fi = FontIterator(*this, par, pit, pos);
713 pos_type point = end;
715 for ( ; i < end; ++i, ++fi) {
716 int thiswidth = pm.singleWidth(i, *fi);
718 // add the auto-hfill from label end to the body
719 if (body_pos && i == body_pos) {
720 FontMetrics const & fm = theFontMetrics(
721 text_->getLabelFont(buffer, par));
722 int add = fm.width(layout->labelsep);
723 if (par.isLineSeparator(i - 1))
724 add -= singleWidth(pit, i - 1);
726 add = std::max(add, label_end - x);
731 chunkwidth += thiswidth;
733 // break before a character that will fall off
734 // the right of the row
736 // if no break before, break here
737 if (point == end || chunkwidth >= width - left) {
744 // exit on last registered breakpoint:
748 if (par.isNewline(i)) {
754 if (par.isInset(i + 1) && par.getInset(i + 1)->display()) {
759 if (par.isInset(i) && par.getInset(i)->display()) {
765 if (!par.isInset(i) || par.getInset(i)->isChar()) {
766 // some insets are line separators too
767 if (par.isLineSeparator(i)) {
768 // register breakpoint:
775 // maybe found one, but the par is short enough.
776 if (i == end && x < width)
779 // manual labels cannot be broken in LaTeX. But we
780 // want to make our on-screen rendering of footnotes
782 if (body_pos && point < body_pos)
789 int TextMetrics::rowWidth(int right_margin, pit_type const pit,
790 pos_type const first, pos_type const end) const
792 Buffer & buffer = bv_->buffer();
793 // get the pure distance
794 ParagraphMetrics const & pm = par_metrics_[pit];
795 Paragraph const & par = text_->getPar(pit);
796 int w = leftMargin(max_width_, pit, first);
797 int label_end = labelEnd(pit);
799 pos_type const body_pos = par.beginOfBody();
803 FontIterator fi = FontIterator(*this, par, pit, i);
804 for ( ; i < end; ++i, ++fi) {
805 if (body_pos > 0 && i == body_pos) {
806 FontMetrics const & fm = theFontMetrics(
807 text_->getLabelFont(buffer, par));
808 w += fm.width(par.layout()->labelsep);
809 if (par.isLineSeparator(i - 1))
810 w -= singleWidth(pit, i - 1);
811 w = max(w, label_end);
813 w += pm.singleWidth(i, *fi);
817 if (body_pos > 0 && body_pos >= end) {
818 FontMetrics const & fm = theFontMetrics(
819 text_->getLabelFont(buffer, par));
820 w += fm.width(par.layout()->labelsep);
821 if (end > 0 && par.isLineSeparator(end - 1))
822 w -= singleWidth(pit, end - 1);
823 w = max(w, label_end);
826 return w + right_margin;
830 boost::tuple<int, int> TextMetrics::rowHeight(pit_type const pit, pos_type const first,
831 pos_type const end) const
833 Paragraph const & par = text_->getPar(pit);
834 // get the maximum ascent and the maximum descent
835 double layoutasc = 0;
836 double layoutdesc = 0;
837 double const dh = defaultRowHeight();
839 // ok, let us initialize the maxasc and maxdesc value.
840 // Only the fontsize count. The other properties
841 // are taken from the layoutfont. Nicer on the screen :)
842 LayoutPtr const & layout = par.layout();
844 // as max get the first character of this row then it can
845 // increase but not decrease the height. Just some point to
846 // start with so we don't have to do the assignment below too
848 Buffer const & buffer = bv_->buffer();
849 Font font = getDisplayFont(pit, first);
850 Font::FONT_SIZE const tmpsize = font.size();
851 font = text_->getLayoutFont(buffer, pit);
852 Font::FONT_SIZE const size = font.size();
853 font.setSize(tmpsize);
855 Font labelfont = text_->getLabelFont(buffer, par);
857 FontMetrics const & labelfont_metrics = theFontMetrics(labelfont);
858 FontMetrics const & fontmetrics = theFontMetrics(font);
860 // these are minimum values
861 double const spacing_val = layout->spacing.getValue()
862 * text_->spacing(buffer, par);
863 //lyxerr << "spacing_val = " << spacing_val << endl;
864 int maxasc = int(fontmetrics.maxAscent() * spacing_val);
865 int maxdesc = int(fontmetrics.maxDescent() * spacing_val);
867 // insets may be taller
868 ParagraphMetrics const & pm = par_metrics_[pit];
869 InsetList::const_iterator ii = par.insetlist.begin();
870 InsetList::const_iterator iend = par.insetlist.end();
871 for ( ; ii != iend; ++ii) {
872 Dimension const & dim = pm.insetDimension(ii->inset);
873 if (ii->pos >= first && ii->pos < end) {
874 maxasc = max(maxasc, dim.ascent());
875 maxdesc = max(maxdesc, dim.descent());
879 // Check if any custom fonts are larger (Asger)
880 // This is not completely correct, but we can live with the small,
881 // cosmetic error for now.
884 Font::FONT_SIZE maxsize =
885 par.highestFontInRange(first, end, size);
886 if (maxsize > font.size()) {
887 // use standard paragraph font with the maximal size
889 maxfont.setSize(maxsize);
890 FontMetrics const & maxfontmetrics = theFontMetrics(maxfont);
891 maxasc = max(maxasc, maxfontmetrics.maxAscent());
892 maxdesc = max(maxdesc, maxfontmetrics.maxDescent());
895 // This is nicer with box insets:
899 ParagraphList const & pars = text_->paragraphs();
903 BufferParams const & bufparams = buffer.params();
904 // some parskips VERY EASY IMPLEMENTATION
905 if (bufparams.paragraph_separation
906 == BufferParams::PARSEP_SKIP
907 && par.ownerCode() != Inset::ERT_CODE
908 && par.ownerCode() != Inset::LISTINGS_CODE
910 && ((layout->isParagraph() && par.getDepth() == 0)
911 || (pars[pit - 1].layout()->isParagraph()
912 && pars[pit - 1].getDepth() == 0)))
914 maxasc += bufparams.getDefSkip().inPixels(*bv_);
917 if (par.params().startOfAppendix())
918 maxasc += int(3 * dh);
920 // This is special code for the chapter, since the label of this
921 // layout is printed in an extra row
922 if (layout->counter == "chapter"
923 && !par.params().labelString().empty()) {
924 labeladdon = int(labelfont_metrics.maxHeight()
925 * layout->spacing.getValue()
926 * text_->spacing(buffer, par));
929 // special code for the top label
930 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
931 || layout->labeltype == LABEL_BIBLIO
932 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
933 && isFirstInSequence(pit, pars)
934 && !par.getLabelstring().empty())
937 labelfont_metrics.maxHeight()
938 * layout->spacing.getValue()
939 * text_->spacing(buffer, par)
940 + (layout->topsep + layout->labelbottomsep) * dh);
943 // Add the layout spaces, for example before and after
944 // a section, or between the items of a itemize or enumerate
947 pit_type prev = depthHook(pit, pars, par.getDepth());
948 Paragraph const & prevpar = pars[prev];
950 && prevpar.layout() == layout
951 && prevpar.getDepth() == par.getDepth()
952 && prevpar.getLabelWidthString()
953 == par.getLabelWidthString()) {
954 layoutasc = layout->itemsep * dh;
955 } else if (pit != 0 || first != 0) {
956 if (layout->topsep > 0)
957 layoutasc = layout->topsep * dh;
960 prev = outerHook(pit, pars);
961 if (prev != pit_type(pars.size())) {
962 maxasc += int(pars[prev].layout()->parsep * dh);
963 } else if (pit != 0) {
964 Paragraph const & prevpar = pars[pit - 1];
965 if (prevpar.getDepth() != 0 ||
966 prevpar.layout() == layout) {
967 maxasc += int(layout->parsep * dh);
972 // is it a bottom line?
973 if (end >= par.size()) {
974 // add the layout spaces, for example before and after
975 // a section, or between the items of a itemize or enumerate
977 pit_type nextpit = pit + 1;
978 if (nextpit != pit_type(pars.size())) {
983 if (pars[cpit].getDepth() > pars[nextpit].getDepth()) {
984 usual = pars[cpit].layout()->bottomsep * dh;
985 cpit = depthHook(cpit, pars, pars[nextpit].getDepth());
986 if (pars[cpit].layout() != pars[nextpit].layout()
987 || pars[nextpit].getLabelWidthString() != pars[cpit].getLabelWidthString())
989 unusual = pars[cpit].layout()->bottomsep * dh;
991 layoutdesc = max(unusual, usual);
992 } else if (pars[cpit].getDepth() == pars[nextpit].getDepth()) {
993 if (pars[cpit].layout() != pars[nextpit].layout()
994 || pars[nextpit].getLabelWidthString() != pars[cpit].getLabelWidthString())
995 layoutdesc = int(pars[cpit].layout()->bottomsep * dh);
1000 // incalculate the layout spaces
1001 maxasc += int(layoutasc * 2 / (2 + pars[pit].getDepth()));
1002 maxdesc += int(layoutdesc * 2 / (2 + pars[pit].getDepth()));
1004 // FIXME: the correct way is to do the following is to move the
1005 // following code in another method specially tailored for the
1006 // main Text. The following test is thus bogus.
1007 // Top and bottom margin of the document (only at top-level)
1009 if (pit == 0 && first == 0)
1011 if (pit + 1 == pit_type(pars.size()) &&
1012 end == par.size() &&
1013 !(end > 0 && par.isNewline(end - 1)))
1017 return boost::make_tuple(maxasc + labeladdon, maxdesc);
1021 // x is an absolute screen coord
1022 // returns the column near the specified x-coordinate of the row
1023 // x is set to the real beginning of this column
1024 pos_type TextMetrics::getColumnNearX(pit_type const pit,
1025 Row const & row, int & x, bool & boundary) const
1027 Buffer const & buffer = bv_->buffer();
1029 /// For the main Text, it is possible that this pit is not
1030 /// yet in the CoordCache when moving cursor up.
1031 /// x Paragraph coordinate is always 0 for main text anyway.
1032 int const xo = origin_.x_;
1034 Paragraph const & par = text_->getPar(pit);
1035 ParagraphMetrics const & pm = par_metrics_[pit];
1037 bidi.computeTables(par, buffer, row);
1039 pos_type vc = row.pos();
1040 pos_type end = row.endpos();
1042 LayoutPtr const & layout = par.layout();
1044 bool left_side = false;
1046 pos_type body_pos = par.beginOfBody();
1048 double tmpx = row.x;
1049 double last_tmpx = tmpx;
1052 (body_pos > end || !par.isLineSeparator(body_pos - 1)))
1055 // check for empty row
1061 while (vc < end && tmpx <= x) {
1062 c = bidi.vis2log(vc);
1064 if (body_pos > 0 && c == body_pos - 1) {
1065 FontMetrics const & fm = theFontMetrics(
1066 text_->getLabelFont(buffer, par));
1067 tmpx += row.label_hfill + fm.width(layout->labelsep);
1068 if (par.isLineSeparator(body_pos - 1))
1069 tmpx -= singleWidth(pit, body_pos - 1);
1072 if (pm.hfillExpansion(row, c)) {
1073 tmpx += singleWidth(pit, c);
1077 tmpx += row.label_hfill;
1078 } else if (par.isSeparator(c)) {
1079 tmpx += singleWidth(pit, c);
1081 tmpx += row.separator;
1083 tmpx += singleWidth(pit, c);
1088 if ((tmpx + last_tmpx) / 2 > x) {
1093 BOOST_ASSERT(vc <= end); // This shouldn't happen.
1096 // This (rtl_support test) is not needed, but gives
1097 // some speedup if rtl_support == false
1098 bool const lastrow = lyxrc.rtl_support && row.endpos() == par.size();
1100 // If lastrow is false, we don't need to compute
1101 // the value of rtl.
1102 bool const rtl = lastrow ? text_->isRTL(buffer, par) : false;
1104 ((rtl && left_side && vc == row.pos() && x < tmpx - 5) ||
1105 (!rtl && !left_side && vc == end && x > tmpx + 5)))
1107 else if (vc == row.pos()) {
1108 c = bidi.vis2log(vc);
1109 if (bidi.level(c) % 2 == 1)
1112 c = bidi.vis2log(vc - 1);
1113 bool const rtl = (bidi.level(c) % 2 == 1);
1114 if (left_side == rtl) {
1116 boundary = isRTLBoundary(pit, c);
1120 // I believe this code is not needed anymore (Jug 20050717)
1122 // The following code is necessary because the cursor position past
1123 // the last char in a row is logically equivalent to that before
1124 // the first char in the next row. That's why insets causing row
1125 // divisions -- Newline and display-style insets -- must be treated
1126 // specially, so cursor up/down doesn't get stuck in an air gap -- MV
1127 // Newline inset, air gap below:
1128 if (row.pos() < end && c >= end && par.isNewline(end - 1)) {
1129 if (bidi.level(end -1) % 2 == 0)
1130 tmpx -= singleWidth(pit, end - 1);
1132 tmpx += singleWidth(pit, end - 1);
1136 // Air gap above display inset:
1137 if (row.pos() < end && c >= end && end < par.size()
1138 && par.isInset(end) && par.getInset(end)->display()) {
1141 // Air gap below display inset:
1142 if (row.pos() < end && c >= end && par.isInset(end - 1)
1143 && par.getInset(end - 1)->display()) {
1149 pos_type const col = c - row.pos();
1151 if (!c || end == par.size())
1154 if (c==end && !par.isLineSeparator(c-1) && !par.isNewline(c-1)) {
1159 return min(col, end - 1 - row.pos());
1163 pos_type TextMetrics::x2pos(pit_type pit, int row, int x) const
1165 // We play safe and use parMetrics(pit) to make sure the
1166 // ParagraphMetrics will be redone and OK to use if needed.
1167 // Otherwise we would use an empty ParagraphMetrics in
1168 // upDownInText() while in selection mode.
1169 ParagraphMetrics const & pm = parMetrics(pit);
1171 BOOST_ASSERT(row < int(pm.rows().size()));
1173 Row const & r = pm.rows()[row];
1174 return r.pos() + getColumnNearX(pit, r, x, bound);
1178 void TextMetrics::newParMetricsDown()
1180 pair<pit_type, ParagraphMetrics> const & last = *par_metrics_.rbegin();
1181 pit_type const pit = last.first + 1;
1182 if (pit == text_->paragraphs().size())
1185 // do it and update its position.
1187 par_metrics_[pit].setPosition(last.second.position()
1188 + last.second.descent());
1192 void TextMetrics::newParMetricsUp()
1194 pair<pit_type, ParagraphMetrics> const & first = *par_metrics_.begin();
1195 if (first.first == 0)
1198 pit_type const pit = first.first - 1;
1199 // do it and update its position.
1201 par_metrics_[pit].setPosition(first.second.position()
1202 - first.second.ascent());
1205 // y is screen coordinate
1206 pit_type TextMetrics::getPitNearY(int y)
1208 BOOST_ASSERT(!text_->paragraphs().empty());
1209 LYXERR(Debug::DEBUG)
1210 << BOOST_CURRENT_FUNCTION
1211 << ": y: " << y << " cache size: " << par_metrics_.size()
1214 // look for highest numbered paragraph with y coordinate less than given y
1217 ParMetricsCache::const_iterator it = par_metrics_.begin();
1218 ParMetricsCache::const_iterator et = par_metrics_.end();
1219 ParMetricsCache::const_iterator last = et; last--;
1221 ParagraphMetrics const & pm = it->second;
1223 // If we are off-screen (before the visible part)
1225 // and even before the first paragraph in the cache.
1226 && y < it->second.position() - int(pm.ascent())) {
1227 // and we are not at the first paragraph in the inset.
1230 // then this is the paragraph we are looking for.
1231 pit = it->first - 1;
1232 // rebreak it and update the CoordCache.
1234 par_metrics_[pit].setPosition(it->second.position() - pm.descent());
1238 ParagraphMetrics const & pm_last = par_metrics_[last->first];
1240 // If we are off-screen (after the visible part)
1241 if (y > bv_->workHeight()
1242 // and even after the first paragraph in the cache.
1243 && y >= last->second.position() + int(pm_last.descent())) {
1244 pit = last->first + 1;
1245 // and we are not at the last paragraph in the inset.
1246 if (pit == int(text_->paragraphs().size()))
1248 // then this is the paragraph we are looking for.
1249 // rebreak it and update the CoordCache.
1251 par_metrics_[pit].setPosition(last->second.position() + pm_last.ascent());
1255 for (; it != et; ++it) {
1256 LYXERR(Debug::DEBUG)
1257 << BOOST_CURRENT_FUNCTION
1258 << " examining: pit: " << it->first
1259 << " y: " << it->second.position()
1262 ParagraphMetrics const & pm = par_metrics_[it->first];
1264 if (it->first >= pit && int(it->second.position()) - int(pm.ascent()) <= y) {
1266 yy = it->second.position();
1270 LYXERR(Debug::DEBUG)
1271 << BOOST_CURRENT_FUNCTION
1272 << ": found best y: " << yy << " for pit: " << pit
1279 Row const & TextMetrics::getRowNearY(int y, pit_type pit) const
1281 ParagraphMetrics const & pm = par_metrics_[pit];
1283 int yy = pm.position() - pm.ascent();
1284 BOOST_ASSERT(!pm.rows().empty());
1285 RowList::const_iterator rit = pm.rows().begin();
1286 RowList::const_iterator const rlast = boost::prior(pm.rows().end());
1287 for (; rit != rlast; yy += rit->height(), ++rit)
1288 if (yy + rit->height() > y)
1294 // x,y are absolute screen coordinates
1295 // sets cursor recursively descending into nested editable insets
1296 Inset * TextMetrics::editXY(Cursor & cur, int x, int y)
1298 if (lyxerr.debugging(Debug::WORKAREA)) {
1299 lyxerr << "TextMetrics::editXY(cur, " << x << ", " << y << ")" << std::endl;
1300 cur.bv().coordCache().dump();
1302 pit_type pit = getPitNearY(y);
1303 BOOST_ASSERT(pit != -1);
1305 Row const & row = getRowNearY(y, pit);
1308 int xx = x; // is modified by getColumnNearX
1309 pos_type const pos = row.pos()
1310 + getColumnNearX(pit, row, xx, bound);
1313 cur.boundary(bound);
1316 // try to descend into nested insets
1317 Inset * inset = checkInsetHit(x, y);
1318 //lyxerr << "inset " << inset << " hit at x: " << x << " y: " << y << endl;
1320 // Either we deconst editXY or better we move current_font
1321 // and real_current_font to Cursor
1322 // FIXME: what is needed now that current_font and real_current_font
1324 cur.setCurrentFont();
1328 ParagraphList const & pars = text_->paragraphs();
1329 Inset const * insetBefore = pos? pars[pit].getInset(pos - 1): 0;
1330 //Inset * insetBehind = pars[pit].getInset(pos);
1332 // This should be just before or just behind the
1333 // cursor position set above.
1334 BOOST_ASSERT((pos != 0 && inset == insetBefore)
1335 || inset == pars[pit].getInset(pos));
1337 // Make sure the cursor points to the position before
1339 if (inset == insetBefore) {
1341 cur.boundary(false);
1344 // Try to descend recursively inside the inset.
1345 inset = inset->editXY(cur, x, y);
1347 if (cur.top().text() == text_)
1348 cur.setCurrentFont();
1353 void TextMetrics::setCursorFromCoordinates(Cursor & cur, int const x, int const y)
1355 BOOST_ASSERT(text_ == cur.text());
1356 pit_type pit = getPitNearY(y);
1358 ParagraphMetrics const & pm = par_metrics_[pit];
1360 int yy = pm.position() - pm.ascent();
1361 LYXERR(Debug::DEBUG)
1362 << BOOST_CURRENT_FUNCTION
1366 << " yy: " << yy << endl;
1369 BOOST_ASSERT(pm.rows().size());
1370 for (; r < int(pm.rows().size()) - 1; ++r) {
1371 Row const & row = pm.rows()[r];
1372 if (int(yy + row.height()) > y)
1377 Row const & row = pm.rows()[r];
1379 LYXERR(Debug::DEBUG)
1380 << BOOST_CURRENT_FUNCTION
1382 << " from pos: " << row.pos()
1387 pos_type const pos = row.pos() + getColumnNearX(pit, row, xx, bound);
1389 LYXERR(Debug::DEBUG)
1390 << BOOST_CURRENT_FUNCTION
1391 << ": setting cursor pit: " << pit
1395 text_->setCursor(cur, pit, pos, true, bound);
1396 // remember new position.
1401 //takes screen x,y coordinates
1402 Inset * TextMetrics::checkInsetHit(int x, int y)
1404 pit_type pit = getPitNearY(y);
1405 BOOST_ASSERT(pit != -1);
1407 Paragraph const & par = text_->paragraphs()[pit];
1408 ParagraphMetrics const & pm = par_metrics_[pit];
1410 LYXERR(Debug::DEBUG)
1411 << BOOST_CURRENT_FUNCTION
1416 InsetList::const_iterator iit = par.insetlist.begin();
1417 InsetList::const_iterator iend = par.insetlist.end();
1418 for (; iit != iend; ++iit) {
1419 Inset * inset = iit->inset;
1421 LYXERR(Debug::DEBUG)
1422 << BOOST_CURRENT_FUNCTION
1423 << ": examining inset " << inset << endl;
1425 if (!bv_->coordCache().getInsets().has(inset)) {
1426 LYXERR(Debug::DEBUG)
1427 << BOOST_CURRENT_FUNCTION
1428 << ": inset has no cached position" << endl;
1432 Dimension const & dim = pm.insetDimension(inset);
1433 Point p = bv_->coordCache().getInsets().xy(inset);
1435 LYXERR(Debug::DEBUG)
1436 << BOOST_CURRENT_FUNCTION
1437 << ": xo: " << p.x_ << "..." << p.x_ + dim.wid
1438 << " yo: " << p.y_ - dim.asc << "..." << p.y_ + dim.des
1442 && x <= p.x_ + dim.wid
1443 && y >= p.y_ - dim.asc
1444 && y <= p.y_ + dim.des) {
1445 LYXERR(Debug::DEBUG)
1446 << BOOST_CURRENT_FUNCTION
1447 << ": Hit inset: " << inset << endl;
1452 LYXERR(Debug::DEBUG)
1453 << BOOST_CURRENT_FUNCTION
1454 << ": No inset hit. " << endl;
1459 int TextMetrics::cursorX(CursorSlice const & sl,
1460 bool boundary) const
1462 BOOST_ASSERT(sl.text() == text_);
1463 pit_type const pit = sl.pit();
1464 Paragraph const & par = text_->paragraphs()[pit];
1465 ParagraphMetrics const & pm = par_metrics_[pit];
1466 if (pm.rows().empty())
1469 pos_type ppos = sl.pos();
1470 // Correct position in front of big insets
1471 bool const boundary_correction = ppos != 0 && boundary;
1472 if (boundary_correction)
1475 Row const & row = pm.getRow(sl.pos(), boundary);
1477 pos_type cursor_vpos = 0;
1479 Buffer const & buffer = bv_->buffer();
1482 bidi.computeTables(par, buffer, row);
1484 pos_type const row_pos = row.pos();
1485 pos_type const end = row.endpos();
1486 // Spaces at logical line breaks in bidi text must be skipped during
1487 // cursor positioning. However, they may appear visually in the middle
1488 // of a row; they must be skipped, wherever they are...
1489 // * logically "abc_[HEBREW_\nHEBREW]"
1490 // * visually "abc_[_WERBEH\nWERBEH]"
1491 pos_type skipped_sep_vpos = -1;
1494 cursor_vpos = row_pos;
1495 else if (ppos >= end)
1496 cursor_vpos = text_->isRTL(buffer, par) ? row_pos : end;
1497 else if (ppos > row_pos && ppos >= end)
1498 // Place cursor after char at (logical) position pos - 1
1499 cursor_vpos = (bidi.level(ppos - 1) % 2 == 0)
1500 ? bidi.log2vis(ppos - 1) + 1 : bidi.log2vis(ppos - 1);
1502 // Place cursor before char at (logical) position ppos
1503 cursor_vpos = (bidi.level(ppos) % 2 == 0)
1504 ? bidi.log2vis(ppos) : bidi.log2vis(ppos) + 1;
1506 pos_type body_pos = par.beginOfBody();
1508 (body_pos > end || !par.isLineSeparator(body_pos - 1)))
1511 // Use font span to speed things up, see below
1515 // If the last logical character is a separator, skip it, unless
1516 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
1517 if (end > 0 && end < par.size() && par.isSeparator(end - 1))
1518 skipped_sep_vpos = bidi.log2vis(end - 1);
1520 for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
1521 // Skip the separator which is at the logical end of the row
1522 if (vpos == skipped_sep_vpos)
1524 pos_type pos = bidi.vis2log(vpos);
1525 if (body_pos > 0 && pos == body_pos - 1) {
1526 FontMetrics const & labelfm = theFontMetrics(
1527 text_->getLabelFont(buffer, par));
1528 x += row.label_hfill + labelfm.width(par.layout()->labelsep);
1529 if (par.isLineSeparator(body_pos - 1))
1530 x -= singleWidth(pit, body_pos - 1);
1533 // Use font span to speed things up, see above
1534 if (pos < font_span.first || pos > font_span.last) {
1535 font_span = par.fontSpan(pos);
1536 font = getDisplayFont(pit, pos);
1539 x += pm.singleWidth(pos, font);
1541 if (pm.hfillExpansion(row, pos))
1542 x += (pos >= body_pos) ? row.hfill : row.label_hfill;
1543 else if (par.isSeparator(pos) && pos >= body_pos)
1547 // see correction above
1548 if (boundary_correction) {
1549 if (isRTL(sl, boundary))
1550 x -= singleWidth(pit, ppos);
1552 x += singleWidth(pit, ppos);
1559 int TextMetrics::cursorY(CursorSlice const & sl, bool boundary) const
1561 //lyxerr << "TextMetrics::cursorY: boundary: " << boundary << std::endl;
1562 ParagraphMetrics const & pm = par_metrics_[sl.pit()];
1563 if (pm.rows().empty())
1567 h -= par_metrics_[0].rows()[0].ascent();
1568 for (pit_type pit = 0; pit < sl.pit(); ++pit) {
1569 h += par_metrics_[pit].height();
1572 if (pos && boundary)
1574 size_t const rend = pm.pos2row(pos);
1575 for (size_t rit = 0; rit != rend; ++rit)
1576 h += pm.rows()[rit].height();
1577 h += pm.rows()[rend].ascent();
1582 void TextMetrics::cursorPrevious(Cursor & cur)
1584 pos_type cpos = cur.pos();
1585 pit_type cpar = cur.pit();
1587 int x = cur.x_target();
1588 setCursorFromCoordinates(cur, x, 0);
1589 cur.dispatch(FuncRequest(cur.selection()? LFUN_UP_SELECT: LFUN_UP));
1591 if (cpar == cur.pit() && cpos == cur.pos())
1592 // we have a row which is taller than the workarea. The
1593 // simplest solution is to move to the previous row instead.
1594 cur.dispatch(FuncRequest(cur.selection()? LFUN_UP_SELECT: LFUN_UP));
1597 cur.updateFlags(Update::Force | Update::FitCursor);
1601 void TextMetrics::cursorNext(Cursor & cur)
1603 pos_type cpos = cur.pos();
1604 pit_type cpar = cur.pit();
1606 int x = cur.x_target();
1607 setCursorFromCoordinates(cur, x, cur.bv().workHeight() - 1);
1608 cur.dispatch(FuncRequest(cur.selection()? LFUN_DOWN_SELECT: LFUN_DOWN));
1610 if (cpar == cur.pit() && cpos == cur.pos())
1611 // we have a row which is taller than the workarea. The
1612 // simplest solution is to move to the next row instead.
1614 FuncRequest(cur.selection()? LFUN_DOWN_SELECT: LFUN_DOWN));
1617 cur.updateFlags(Update::Force | Update::FitCursor);
1621 // the cursor set functions have a special mechanism. When they
1622 // realize you left an empty paragraph, they will delete it.
1624 bool TextMetrics::cursorHome(Cursor & cur)
1626 BOOST_ASSERT(text_ == cur.text());
1627 ParagraphMetrics const & pm = par_metrics_[cur.pit()];
1628 Row const & row = pm.getRow(cur.pos(),cur.boundary());
1629 return text_->setCursor(cur, cur.pit(), row.pos());
1633 bool TextMetrics::cursorEnd(Cursor & cur)
1635 BOOST_ASSERT(text_ == cur.text());
1636 // if not on the last row of the par, put the cursor before
1637 // the final space exept if I have a spanning inset or one string
1638 // is so long that we force a break.
1639 pos_type end = cur.textRow().endpos();
1641 // empty text, end-1 is no valid position
1643 bool boundary = false;
1644 if (end != cur.lastpos()) {
1645 if (!cur.paragraph().isLineSeparator(end-1)
1646 && !cur.paragraph().isNewline(end-1))
1651 return text_->setCursor(cur, cur.pit(), end, true, boundary);
1655 void TextMetrics::deleteLineForward(Cursor & cur)
1657 BOOST_ASSERT(text_ == cur.text());
1658 if (cur.lastpos() == 0) {
1659 // Paragraph is empty, so we just go to the right
1660 text_->cursorRight(cur);
1663 cur.selection() = true; // to avoid deletion
1666 // What is this test for ??? (JMarc)
1667 if (!cur.selection())
1668 text_->deleteWordForward(cur);
1670 cap::cutSelection(cur, true, false);
1671 checkBufferStructure(cur.buffer(), cur);
1676 bool TextMetrics::isLastRow(pit_type pit, Row const & row) const
1678 ParagraphList const & pars = text_->paragraphs();
1679 return row.endpos() >= pars[pit].size()
1680 && pit + 1 == pit_type(pars.size());
1684 bool TextMetrics::isFirstRow(pit_type pit, Row const & row) const
1686 return row.pos() == 0 && pit == 0;
1690 int TextMetrics::leftMargin(int max_width, pit_type pit) const
1692 BOOST_ASSERT(pit >= 0);
1693 BOOST_ASSERT(pit < int(text_->paragraphs().size()));
1694 return leftMargin(max_width, pit, text_->paragraphs()[pit].size());
1698 int TextMetrics::leftMargin(int max_width,
1699 pit_type const pit, pos_type const pos) const
1701 ParagraphList const & pars = text_->paragraphs();
1703 BOOST_ASSERT(pit >= 0);
1704 BOOST_ASSERT(pit < int(pars.size()));
1705 Paragraph const & par = pars[pit];
1706 BOOST_ASSERT(pos >= 0);
1707 BOOST_ASSERT(pos <= par.size());
1708 Buffer const & buffer = bv_->buffer();
1709 //lyxerr << "TextMetrics::leftMargin: pit: " << pit << " pos: " << pos << endl;
1710 TextClass const & tclass = buffer.params().getTextClass();
1711 LayoutPtr const & layout = par.layout();
1713 docstring parindent = layout->parindent;
1717 if (text_->isMainText(buffer))
1718 l_margin += changebarMargin();
1720 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(
1721 tclass.leftmargin());
1723 if (par.getDepth() != 0) {
1724 // find the next level paragraph
1725 pit_type newpar = outerHook(pit, pars);
1726 if (newpar != pit_type(pars.size())) {
1727 if (pars[newpar].layout()->isEnvironment()) {
1728 l_margin = leftMargin(max_width, newpar);
1730 if (par.layout() == tclass.defaultLayout()) {
1731 if (pars[newpar].params().noindent())
1734 parindent = pars[newpar].layout()->parindent;
1739 // This happens after sections in standard classes. The 1.3.x
1740 // code compared depths too, but it does not seem necessary
1742 if (par.layout() == tclass.defaultLayout()
1743 && pit > 0 && pars[pit - 1].layout()->nextnoindent)
1746 Font const labelfont = text_->getLabelFont(buffer, par);
1747 FontMetrics const & labelfont_metrics = theFontMetrics(labelfont);
1749 switch (layout->margintype) {
1750 case MARGIN_DYNAMIC:
1751 if (!layout->leftmargin.empty()) {
1752 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(
1753 layout->leftmargin);
1755 if (!par.getLabelstring().empty()) {
1756 l_margin += labelfont_metrics.signedWidth(layout->labelindent);
1757 l_margin += labelfont_metrics.width(par.getLabelstring());
1758 l_margin += labelfont_metrics.width(layout->labelsep);
1762 case MARGIN_MANUAL: {
1763 l_margin += labelfont_metrics.signedWidth(layout->labelindent);
1764 // The width of an empty par, even with manual label, should be 0
1765 if (!par.empty() && pos >= par.beginOfBody()) {
1766 if (!par.getLabelWidthString().empty()) {
1767 docstring labstr = par.getLabelWidthString();
1768 l_margin += labelfont_metrics.width(labstr);
1769 l_margin += labelfont_metrics.width(layout->labelsep);
1775 case MARGIN_STATIC: {
1776 l_margin += theFontMetrics(buffer.params().getFont()).
1777 signedWidth(layout->leftmargin) * 4 / (par.getDepth() + 4);
1781 case MARGIN_FIRST_DYNAMIC:
1782 if (layout->labeltype == LABEL_MANUAL) {
1783 if (pos >= par.beginOfBody()) {
1784 l_margin += labelfont_metrics.signedWidth(layout->leftmargin);
1786 l_margin += labelfont_metrics.signedWidth(layout->labelindent);
1789 // Special case to fix problems with
1791 || (layout->labeltype == LABEL_STATIC
1792 && layout->latextype == LATEX_ENVIRONMENT
1793 && !isFirstInSequence(pit, pars))) {
1794 l_margin += labelfont_metrics.signedWidth(layout->leftmargin);
1795 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
1796 && layout->labeltype != LABEL_BIBLIO
1797 && layout->labeltype !=
1798 LABEL_CENTERED_TOP_ENVIRONMENT) {
1799 l_margin += labelfont_metrics.signedWidth(layout->labelindent);
1800 l_margin += labelfont_metrics.width(layout->labelsep);
1801 l_margin += labelfont_metrics.width(par.getLabelstring());
1805 case MARGIN_RIGHT_ADDRESS_BOX: {
1807 // ok, a terrible hack. The left margin depends on the widest
1808 // row in this paragraph.
1809 RowList::iterator rit = par.rows().begin();
1810 RowList::iterator end = par.rows().end();
1811 // FIXME: This is wrong.
1812 int minfill = max_width;
1813 for ( ; rit != end; ++rit)
1814 if (rit->fill() < minfill)
1815 minfill = rit->fill();
1816 l_margin += theFontMetrics(params.getFont()).signedWidth(layout->leftmargin);
1817 l_margin += minfill;
1819 // also wrong, but much shorter.
1820 l_margin += max_width / 2;
1825 if (!par.params().leftIndent().zero())
1826 l_margin += par.params().leftIndent().inPixels(max_width);
1830 if (par.params().align() == LYX_ALIGN_LAYOUT)
1831 align = layout->align;
1833 align = par.params().align();
1835 // set the correct parindent
1837 && (layout->labeltype == LABEL_NO_LABEL
1838 || layout->labeltype == LABEL_TOP_ENVIRONMENT
1839 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
1840 || (layout->labeltype == LABEL_STATIC
1841 && layout->latextype == LATEX_ENVIRONMENT
1842 && !isFirstInSequence(pit, pars)))
1843 && align == LYX_ALIGN_BLOCK
1844 && !par.params().noindent()
1845 // in some insets, paragraphs are never indented
1846 && !(par.inInset() && par.inInset()->neverIndent(buffer))
1847 // display style insets are always centered, omit indentation
1850 && par.getInset(pos)->display())
1851 && (par.layout() != tclass.defaultLayout()
1852 || buffer.params().paragraph_separation ==
1853 BufferParams::PARSEP_INDENT))
1855 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(
1863 int TextMetrics::singleWidth(pit_type pit, pos_type pos) const
1865 ParagraphMetrics const & pm = par_metrics_[pit];
1867 return pm.singleWidth(pos, getDisplayFont(pit, pos));
1871 // only used for inset right now. should also be used for main text
1872 void TextMetrics::draw(PainterInfo & pi, int x, int y) const
1874 if (par_metrics_.empty())
1880 ParMetricsCache::iterator it = par_metrics_.begin();
1881 ParMetricsCache::iterator const pm_end = par_metrics_.end();
1882 y -= it->second.ascent();
1883 for (; it != pm_end; ++it) {
1884 ParagraphMetrics const & pmi = it->second;
1886 pit_type const pit = it->first;
1887 // Save the paragraph position in the cache.
1888 it->second.setPosition(y);
1889 drawParagraph(pi, pit, x, y);
1895 void TextMetrics::drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const
1897 // lyxerr << " paintPar: pit: " << pit << " at y: " << y << endl;
1898 int const ww = bv_->workHeight();
1900 ParagraphMetrics const & pm = par_metrics_[pit];
1901 if (pm.rows().empty())
1904 RowList::const_iterator const rb = pm.rows().begin();
1905 RowList::const_iterator const re = pm.rows().end();
1910 for (RowList::const_iterator rit = rb; rit != re; ++rit) {
1913 bool const inside = (y + rit->descent() >= 0
1914 && y - rit->ascent() < ww);
1915 // it is not needed to draw on screen if we are not inside.
1916 pi.pain.setDrawingEnabled(inside);
1917 RowPainter rp(pi, *text_, pit, *rit, bidi, x, y);
1919 // Row signature; has row changed since last paint?
1920 bool row_has_changed = rit->changed();
1922 bool row_selection = rit->sel_beg != -1 && rit->sel_end != -1;
1924 if (!row_selection && !pi.full_repaint && !row_has_changed) {
1925 // Paint the only the insets if the text itself is
1927 rp.paintOnlyInsets();
1928 y += rit->descent();
1932 // Paint the row if a full repaint has been requested or it has
1934 // Clear background of this row
1935 // (if paragraph background was not cleared)
1936 if (row_selection || (!pi.full_repaint && row_has_changed)) {
1937 pi.pain.fillRectangle(x, y - rit->ascent(),
1938 width(), rit->height(),
1939 Color_color(Color::color(pi.background_color)));
1941 if (row_selection) {
1942 DocIterator beg = bv_->cursor().selectionBegin();
1943 DocIterator end = bv_->cursor().selectionEnd();
1945 beg.pos() = rit->sel_beg;
1946 if (pit == end.pit()) {
1947 end.pos() = rit->sel_end;
1949 end.pit() = pit + 1;
1952 drawSelection(pi, beg, end, x);
1955 // Instrumentation for testing row cache (see also
1957 if (lyxerr.debugging(Debug::PAINTING)) {
1958 if (text_->isMainText(bv_->buffer()))
1959 LYXERR(Debug::PAINTING) << "\n{" <<
1960 pi.full_repaint << row_has_changed << "}";
1962 LYXERR(Debug::PAINTING) << "[" <<
1963 pi.full_repaint << row_has_changed << "]";
1966 // Backup full_repaint status and force full repaint
1967 // for inner insets as the Row has been cleared out.
1968 bool tmp = pi.full_repaint;
1969 pi.full_repaint = true;
1972 rp.paintChangeBar();
1978 y += rit->descent();
1979 // Restore full_repaint status.
1980 pi.full_repaint = tmp;
1982 // Re-enable screen drawing for future use of the painter.
1983 pi.pain.setDrawingEnabled(true);
1985 //LYXERR(Debug::PAINTING) << "." << endl;
1989 // FIXME: only take care of one row!
1990 void TextMetrics::drawSelection(PainterInfo & pi,
1991 DocIterator const & beg, DocIterator const & end, int x) const
1993 ParagraphMetrics const & pm1 = parMetrics(beg.pit());
1994 ParagraphMetrics const & pm2 = parMetrics(end.pit());
1995 Row const & row1 = pm1.getRow(beg.pos(), beg.boundary());
1996 Row const & row2 = pm2.getRow(end.pos(), end.boundary());
2000 bool const clipAbove =
2001 (bv_funcs::status(bv_, beg) == bv_funcs::CUR_ABOVE);
2005 middleTop = bv_funcs::getPos(*bv_, beg, beg.boundary()).y_ + row1.descent();
2009 bool const clipBelow =
2010 (bv_funcs::status(bv_, end) == bv_funcs::CUR_BELOW);
2012 middleBottom = bv_->workHeight();
2014 middleBottom = bv_funcs::getPos(*bv_, end, end.boundary()).y_ - row2.ascent();
2016 // start and end in the same line?
2017 if (!(clipAbove || clipBelow) && &row1 == &row2)
2018 // then only draw this row's selection
2019 drawRowSelection(pi, x, row1, beg, end, false, false);
2023 DocIterator begRowEnd = beg;
2024 begRowEnd.pos() = row1.endpos();
2025 begRowEnd.boundary(true);
2027 // draw upper rectangle
2028 drawRowSelection(pi, x, row1, beg, begRowEnd, false, true);
2031 if (middleTop < middleBottom) {
2032 // draw middle rectangle
2033 pi.pain.fillRectangle(x, middleTop, width(), middleBottom - middleTop,
2039 DocIterator endRowBeg = end;
2040 endRowBeg.pos() = row2.pos();
2041 endRowBeg.boundary(false);
2043 // draw low rectangle
2044 drawRowSelection(pi, x, row2, endRowBeg, end, true, false);
2050 void TextMetrics::drawRowSelection(PainterInfo & pi, int x, Row const & row,
2051 DocIterator const & beg, DocIterator const & end,
2052 bool drawOnBegMargin, bool drawOnEndMargin) const
2054 Buffer & buffer = bv_->buffer();
2055 DocIterator cur = beg;
2056 int x1 = cursorX(beg.top(), beg.boundary());
2057 int x2 = cursorX(end.top(), end.boundary());
2058 int y1 = bv_funcs::getPos(*bv_, cur, cur.boundary()).y_ - row.ascent();
2059 int y2 = y1 + row.height();
2062 if (drawOnBegMargin) {
2063 if (text_->isRTL(buffer, beg.paragraph()))
2064 pi.pain.fillRectangle(x + x1, y1, width() - x1, y2 - y1, Color::selection);
2066 pi.pain.fillRectangle(x, y1, x1, y2 - y1, Color::selection);
2069 if (drawOnEndMargin) {
2070 if (text_->isRTL(buffer, beg.paragraph()))
2071 pi.pain.fillRectangle(x, y1, x2, y2 - y1, Color::selection);
2073 pi.pain.fillRectangle(x + x2, y1, width() - x2, y2 - y1, Color::selection);
2076 // if we are on a boundary from the beginning, it's probably
2077 // a RTL boundary and we jump to the other side directly as this
2078 // segement is 0-size and confuses the logic below
2080 cur.boundary(false);
2082 // go through row and draw from RTL boundary to RTL boundary
2084 bool drawNow = false;
2086 // simplified cursorRight code below which does not
2087 // descend into insets and which does not go into the
2088 // next line. Compare the logic with the original cursorRight
2090 // if left of boundary -> just jump to right side
2091 // but for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
2092 if (cur.boundary()) {
2093 cur.boundary(false);
2094 } else if (isRTLBoundary(cur.pit(), cur.pos() + 1)) {
2095 // in front of RTL boundary -> Stay on this side of the boundary because:
2096 // ab|cDDEEFFghi -> abc|DDEEFFghi
2105 if (cur.pos() == row.endpos())
2110 // the previous segment was just drawn, now the next starts
2111 x1 = cursorX(cur.top(), cur.boundary());
2114 if (!(cur < end) || drawNow) {
2115 x2 = cursorX(cur.top(), cur.boundary());
2116 pi.pain.fillRectangle(x + min(x1,x2), y1, abs(x2 - x1), y2 - y1,
2119 // reset x1, so it is set again next round (which will be on the
2120 // right side of a boundary or at the selection end)
2126 //int TextMetrics::pos2x(pit_type pit, pos_type pos) const
2128 // ParagraphMetrics const & pm = par_metrics_[pit];
2129 // Row const & r = pm.rows()[row];
2135 int defaultRowHeight()
2137 return int(theFontMetrics(Font(Font::ALL_SANE)).maxHeight() * 1.2);