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 "CutAndPaste.h"
29 #include "FontIterator.h"
30 #include "FuncRequest.h"
31 #include "InsetList.h"
35 #include "MetricsInfo.h"
36 #include "paragraph_funcs.h"
37 #include "ParagraphParameters.h"
38 #include "ParIterator.h"
39 #include "rowpainter.h"
43 #include "mathed/MacroTable.h"
44 #include "mathed/MathMacroTemplate.h"
46 #include "frontends/FontMetrics.h"
47 #include "frontends/Painter.h"
49 #include <boost/current_function.hpp>
59 using frontend::FontMetrics;
63 int numberOfSeparators(Paragraph const & par, Row const & row)
65 pos_type const first = max(row.pos(), par.beginOfBody());
66 pos_type const last = row.endpos() - 1;
68 for (pos_type p = first; p < last; ++p) {
69 if (par.isSeparator(p))
76 int numberOfLabelHfills(Paragraph const & par, Row const & row)
78 pos_type last = row.endpos() - 1;
79 pos_type first = row.pos();
81 // hfill *DO* count at the beginning of paragraphs!
83 while (first < last && par.isHfill(first))
87 last = min(last, par.beginOfBody());
89 for (pos_type p = first; p < last; ++p) {
97 int numberOfHfills(Paragraph const & par, Row const & row)
99 pos_type const last = row.endpos();
100 pos_type first = row.pos();
102 // hfill *DO* count at the beginning of paragraphs!
104 while (first < last && par.isHfill(first))
108 first = max(first, par.beginOfBody());
111 for (pos_type p = first; p < last; ++p) {
120 TextMetrics::TextMetrics(BufferView * bv, Text * text)
121 : bv_(bv), text_(text)
124 max_width_ = bv_->workWidth();
125 dim_.wid = max_width_;
129 //text_->updateLabels(bv->buffer());
133 bool TextMetrics::has(pit_type pit) const
135 return par_metrics_.find(pit) != par_metrics_.end();
139 ParagraphMetrics const & TextMetrics::parMetrics(pit_type pit) const
141 return const_cast<TextMetrics *>(this)->parMetrics(pit, true);
146 pair<pit_type, ParagraphMetrics const *> TextMetrics::first() const
148 ParMetricsCache::const_iterator it = par_metrics_.begin();
149 return make_pair(it->first, &it->second);
153 pair<pit_type, ParagraphMetrics const *> TextMetrics::last() const
155 ParMetricsCache::const_reverse_iterator it = par_metrics_.rbegin();
156 return make_pair(it->first, &it->second);
160 ParagraphMetrics & TextMetrics::parMetrics(pit_type pit,
163 ParMetricsCache::iterator pmc_it = par_metrics_.find(pit);
164 if (pmc_it == par_metrics_.end()) {
165 pmc_it = par_metrics_.insert(
166 make_pair(pit, ParagraphMetrics(text_->getPar(pit)))).first;
168 if (pmc_it->second.rows().empty() && redo) {
171 return pmc_it->second;
175 int TextMetrics::parPosition(pit_type pit) const
177 if (pit < par_metrics_.begin()->first)
179 else if (pit > par_metrics_.rbegin()->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.fontInfo().reduce(bv_->buffer().params().getFont().fontInfo());
242 lf.fontInfo().realize(font.fontInfo());
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 FontInfo const & lf = lab ? layout->labelfont : layout->font;
268 FontInfo rlf = lab ? layout->reslabelfont : layout->resfont;
270 // In case the default family has been customized
271 if (lf.family() == INHERIT_FAMILY)
272 rlf.setFamily(params.getFont().fontInfo().family());
273 f.fontInfo().realize(rlf);
277 // The uncommon case need not be optimized as much
278 FontInfo const & layoutfont = pos < body_pos ?
279 layout->labelfont : layout->font;
281 Font font = par.getFontSettings(params, pos);
282 font.fontInfo().realize(layoutfont);
284 if (!text_->isMainText(buffer))
285 applyOuterFont(font);
287 // Realize against environment font information
288 // NOTE: the cast to pit_type should be removed when pit_type
289 // changes to a unsigned integer.
290 if (pit < pit_type(pars.size()))
291 font.fontInfo().realize(outerFont(pit, pars).fontInfo());
293 // Realize with the fonts of lesser depth.
294 font.fontInfo().realize(params.getFont().fontInfo());
300 bool TextMetrics::isRTL(CursorSlice const & sl, bool boundary) const
302 if (!lyxrc.rtl_support || !sl.text())
306 if (boundary && sl.pos() > 0)
309 return getDisplayFont(sl.pit(), sl.pos() + correction).isVisibleRightToLeft();
313 bool TextMetrics::isRTLBoundary(pit_type pit, pos_type pos) const
315 if (!lyxrc.rtl_support)
318 // no RTL boundary at line start
322 Paragraph const & par = text_->getPar(pit);
324 bool left = getDisplayFont(pit, pos - 1).isVisibleRightToLeft();
326 if (pos == par.size())
327 right = par.isRTL(bv_->buffer().params());
329 right = getDisplayFont(pit, pos).isVisibleRightToLeft();
330 return left != right;
334 bool TextMetrics::isRTLBoundary(pit_type pit, pos_type pos,
335 Font const & font) const
337 if (!lyxrc.rtl_support)
340 Paragraph const & par = text_->getPar(pit);
341 bool left = font.isVisibleRightToLeft();
343 if (pos == par.size())
344 right = par.isRTL(bv_->buffer().params());
346 right = getDisplayFont(pit, pos).isVisibleRightToLeft();
347 return left != right;
351 bool TextMetrics::redoParagraph(pit_type const pit)
353 Paragraph & par = text_->getPar(pit);
354 // IMPORTANT NOTE: We pass 'false' explicitely in order to not call
355 // redoParagraph() recursively inside parMetrics.
356 Dimension old_dim = parMetrics(pit, false).dim();
357 ParagraphMetrics & pm = par_metrics_[pit];
360 Buffer & buffer = bv_->buffer();
361 BufferParams const & bparams = buffer.params();
362 main_text_ = (text_ == &buffer.text());
363 bool changed = false;
365 // FIXME This check ought to be done somewhere else. It is the reason
366 // why text_ is not const. But then, where else to do it?
367 // Well, how can you end up with either (a) a biblio environment that
368 // has no InsetBibitem or (b) a biblio environment with more than one
369 // InsetBibitem? I think the answer is: when paragraphs are merged;
370 // when layout is set; when material is pasted.
371 int const moveCursor = par.checkBiblio(buffer.params().trackChanges);
373 const_cast<Cursor &>(bv_->cursor()).posRight();
374 else if (moveCursor < 0) {
375 Cursor & cursor = const_cast<Cursor &>(bv_->cursor());
376 if (cursor.pos() >= -moveCursor)
380 // Optimisation: this is used in the next two loops
381 // so better to calculate that once here.
382 int const right_margin = rightMargin(pm);
385 // FIXME: We should always use getFont(), see documentation of
386 // noFontChange() in Inset.h.
387 Font const bufferfont = buffer.params().getFont();
388 MacroContext mc(buffer, par);
389 InsetList::const_iterator ii = par.insetList().begin();
390 InsetList::const_iterator iend = par.insetList().end();
391 for (; ii != iend; ++ii) {
392 // the macro must come here, _before_ the metric call, because
393 // the macro should see itself to detect recursions. To find out
394 // whether the macro definition is a redefinition it will look
395 // at the MacroData::redefinition_. So it doesn't get confused
396 // by the already existing macro definition of itself in the
398 if (ii->inset->lyxCode() == MATHMACRO_CODE) {
400 MathMacroTemplate const & macroTemplate
401 = static_cast<MathMacroTemplate const &>(*ii->inset);
404 if (macroTemplate.validMacro()) {
405 MacroData macro = macroTemplate.asMacroData();
408 macro.setRedefinition(mc.has(macroTemplate.name()));
410 // register macro (possibly overwrite the previous one of this paragraph)
411 mc.insert(macroTemplate.name(), macro);
415 // do the metric calculation
417 int const w = max_width_ - leftMargin(max_width_, pit, ii->pos)
419 Font const & font = ii->inset->noFontChange() ?
420 bufferfont : getDisplayFont(pit, ii->pos);
421 MetricsInfo mi(bv_, font.fontInfo(), w, mc);
422 ii->inset->metrics(mi, dim);
423 Dimension const old_dim = pm.insetDimension(ii->inset);
424 pm.setInsetDimension(ii->inset, dim);
425 changed |= (old_dim != dim);
428 Cursor const & cur = bv_->cursor();
429 DocIterator sel_beg = cur.selectionBegin();
430 DocIterator sel_end = cur.selectionEnd();
431 bool selection = cur.selection()
433 && cur.text() == text_
434 // if the anchor is outside, this is not our selection
435 && cur.anchor().text() == text_
436 && pit >= sel_beg.pit() && pit <= sel_end.pit();
438 // We care only about visible selection.
440 if (pit != sel_beg.pit()) {
444 if (pit != sel_end.pit()) {
446 sel_end.pos() = sel_end.lastpos();
450 par.setBeginOfBody();
452 size_t row_index = 0;
453 // maximum pixel width of a row
454 int width = max_width_ - right_margin; // - leftMargin(max_width_, pit, row);
457 pos_type end = rowBreakPoint(width, pit, first);
458 if (row_index || end < par.size())
459 // If there is more than one row, expand the text to
460 // the full allowable width. This setting here is needed
461 // for the computeRowMetrics() below.
462 dim_.wid = max_width_;
464 dim.wid = rowWidth(right_margin, pit, first, end);
465 boost::tie(dim.asc, dim.des) = rowHeight(pit, first, end);
466 if (row_index == pm.rows().size())
467 pm.rows().push_back(Row());
468 Row & row = pm.rows()[row_index];
469 row.setChanged(false);
473 row.setSelection(sel_beg.pos(), sel_end.pos());
475 row.setSelection(-1, -1);
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);
483 pm.dim().wid = std::max(pm.dim().wid, dim.wid);
484 pm.dim().des += dim.height();
485 } while (first < par.size());
487 if (row_index < pm.rows().size())
488 pm.rows().resize(row_index);
490 // Make sure that if a par ends in newline, there is one more row
492 if (first > 0 && par.isNewline(first - 1)) {
494 dim.wid = rowWidth(right_margin, pit, first, first);
495 boost::tie(dim.asc, dim.des) = rowHeight(pit, first, first);
496 if (row_index == pm.rows().size())
497 pm.rows().push_back(Row());
498 Row & row = pm.rows()[row_index];
499 row.setChanged(false);
502 row.setDimension(dim);
503 int const max_row_width = max(dim_.wid, dim.wid);
504 computeRowMetrics(pit, row, max_row_width);
505 pm.computeRowSignature(row, bparams);
506 pm.dim().des += dim.height();
509 pm.dim().asc += pm.rows()[0].ascent();
510 pm.dim().des -= pm.rows()[0].ascent();
512 changed |= old_dim.height() != pm.dim().height();
518 void TextMetrics::computeRowMetrics(pit_type const pit,
519 Row & row, int width) const
526 Buffer & buffer = bv_->buffer();
527 Paragraph const & par = text_->getPar(pit);
529 double w = width - row.width();
530 // FIXME: put back this assertion when the crash on new doc is solved.
531 //BOOST_ASSERT(w >= 0);
533 //lyxerr << "\ndim_.wid " << dim_.wid << endl;
534 //lyxerr << "row.width() " << row.width() << endl;
535 //lyxerr << "w " << w << endl;
537 bool const is_rtl = text_->isRTL(buffer, par);
539 row.x = rightMargin(pit);
541 row.x = leftMargin(max_width_, pit, row.pos());
543 // is there a manual margin with a manual label
544 LayoutPtr const & layout = par.layout();
546 if (layout->margintype == MARGIN_MANUAL
547 && layout->labeltype == LABEL_MANUAL) {
548 /// We might have real hfills in the label part
549 int nlh = numberOfLabelHfills(par, row);
551 // A manual label par (e.g. List) has an auto-hfill
552 // between the label text and the body of the
554 // But we don't want to do this auto hfill if the par
559 if (nlh && !par.getLabelWidthString().empty())
560 row.label_hfill = labelFill(pit, row) / double(nlh);
563 // are there any hfills in the row?
564 int const nh = numberOfHfills(par, row);
569 // we don't have to look at the alignment if it is ALIGN_LEFT and
570 // if the row is already larger then the permitted width as then
571 // we force the LEFT_ALIGN'edness!
572 } else if (int(row.width()) < max_width_) {
573 // is it block, flushleft or flushright?
574 // set x how you need it
576 if (par.params().align() == LYX_ALIGN_LAYOUT)
577 align = layout->align;
579 align = par.params().align();
581 // Display-style insets should always be on a centred row
582 // The test on par.size() is to catch zero-size pars, which
583 // would trigger the assert in Paragraph::getInset().
584 //inset = par.size() ? par.getInset(row.pos()) : 0;
585 if (row.pos() < par.size()
586 && par.isInset(row.pos()))
588 switch(par.getInset(row.pos())->display()) {
589 case Inset::AlignLeft:
590 align = LYX_ALIGN_BLOCK;
592 case Inset::AlignCenter:
593 align = LYX_ALIGN_CENTER;
596 case Inset::AlignRight:
597 // unchanged (use align)
603 case LYX_ALIGN_BLOCK: {
604 int const ns = numberOfSeparators(par, row);
605 bool disp_inset = false;
606 if (row.endpos() < par.size()) {
607 Inset const * in = par.getInset(row.endpos());
609 disp_inset = in->display();
611 // If we have separators, this is not the last row of a
612 // par, does not end in newline, and is not row above a
613 // display inset... then stretch it
615 && row.endpos() < par.size()
616 && !par.isNewline(row.endpos() - 1)
619 row.separator = w / ns;
620 //lyxerr << "row.separator " << row.separator << endl;
621 //lyxerr << "ns " << ns << endl;
627 case LYX_ALIGN_RIGHT:
630 case LYX_ALIGN_CENTER:
637 pos_type body_pos = par.beginOfBody();
638 pos_type end = row.endpos();
641 && (body_pos > end || !par.isLineSeparator(body_pos - 1)))
643 row.x += theFontMetrics(text_->getLabelFont(buffer, par)).
644 width(layout->labelsep);
646 row.x += row.label_hfill;
652 int TextMetrics::labelFill(pit_type const pit, Row const & row) const
654 Buffer & buffer = bv_->buffer();
655 Paragraph const & par = text_->getPar(pit);
657 pos_type last = par.beginOfBody();
658 BOOST_ASSERT(last > 0);
660 // -1 because a label ends with a space that is in the label
663 // a separator at this end does not count
664 if (par.isLineSeparator(last))
668 for (pos_type i = row.pos(); i <= last; ++i)
669 w += singleWidth(pit, i);
671 docstring const & label = par.params().labelWidthString();
675 FontMetrics const & fm
676 = theFontMetrics(text_->getLabelFont(buffer, par));
678 return max(0, fm.width(label) - w);
684 // this needs special handling - only newlines count as a break point
685 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
687 pos_type const end = par.size();
690 if (par.isNewline(i))
699 int TextMetrics::labelEnd(pit_type const pit) const
701 // labelEnd is only needed if the layout fills a flushleft label.
702 if (text_->getPar(pit).layout()->margintype != MARGIN_MANUAL)
704 // return the beginning of the body
705 return leftMargin(max_width_, pit);
709 pit_type TextMetrics::rowBreakPoint(int width, pit_type const pit,
712 Buffer & buffer = bv_->buffer();
713 ParagraphMetrics const & pm = par_metrics_[pit];
714 Paragraph const & par = text_->getPar(pit);
715 pos_type const end = par.size();
716 if (pos == end || width < 0)
719 LayoutPtr const & layout = par.layout();
721 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX)
722 return addressBreakPoint(pos, par);
724 pos_type const body_pos = par.beginOfBody();
727 // Now we iterate through until we reach the right margin
728 // or the end of the par, then choose the possible break
731 int label_end = labelEnd(pit);
732 int const left = leftMargin(max_width_, pit, pos);
735 // pixel width since last breakpoint
738 FontIterator fi = FontIterator(*this, par, pit, pos);
739 pos_type point = end;
741 for ( ; i < end; ++i, ++fi) {
742 int thiswidth = pm.singleWidth(i, *fi);
744 // add the auto-hfill from label end to the body
745 if (body_pos && i == body_pos) {
746 FontMetrics const & fm = theFontMetrics(
747 text_->getLabelFont(buffer, par));
748 int add = fm.width(layout->labelsep);
749 if (par.isLineSeparator(i - 1))
750 add -= singleWidth(pit, i - 1);
752 add = std::max(add, label_end - x);
757 chunkwidth += thiswidth;
759 // break before a character that will fall off
760 // the right of the row
762 // if no break before, break here
763 if (point == end || chunkwidth >= width - left) {
770 // exit on last registered breakpoint:
774 if (par.isNewline(i)) {
780 if (par.isInset(i + 1) && par.getInset(i + 1)->display()) {
785 if (par.isInset(i) && par.getInset(i)->display()) {
791 if (!par.isInset(i) || par.getInset(i)->isChar()) {
792 // some insets are line separators too
793 if (par.isLineSeparator(i)) {
794 // register breakpoint:
801 // maybe found one, but the par is short enough.
802 if (i == end && x < width)
805 // manual labels cannot be broken in LaTeX. But we
806 // want to make our on-screen rendering of footnotes
808 if (body_pos && point < body_pos)
815 int TextMetrics::rowWidth(int right_margin, pit_type const pit,
816 pos_type const first, pos_type const end) const
818 Buffer & buffer = bv_->buffer();
819 // get the pure distance
820 ParagraphMetrics const & pm = par_metrics_[pit];
821 Paragraph const & par = text_->getPar(pit);
822 int w = leftMargin(max_width_, pit, first);
823 int label_end = labelEnd(pit);
825 pos_type const body_pos = par.beginOfBody();
829 FontIterator fi = FontIterator(*this, par, pit, i);
830 for ( ; i < end; ++i, ++fi) {
831 if (body_pos > 0 && i == body_pos) {
832 FontMetrics const & fm = theFontMetrics(
833 text_->getLabelFont(buffer, par));
834 w += fm.width(par.layout()->labelsep);
835 if (par.isLineSeparator(i - 1))
836 w -= singleWidth(pit, i - 1);
837 w = max(w, label_end);
839 w += pm.singleWidth(i, *fi);
843 if (body_pos > 0 && body_pos >= end) {
844 FontMetrics const & fm = theFontMetrics(
845 text_->getLabelFont(buffer, par));
846 w += fm.width(par.layout()->labelsep);
847 if (end > 0 && par.isLineSeparator(end - 1))
848 w -= singleWidth(pit, end - 1);
849 w = max(w, label_end);
852 return w + right_margin;
856 boost::tuple<int, int> TextMetrics::rowHeight(pit_type const pit, pos_type const first,
857 pos_type const end) const
859 Paragraph const & par = text_->getPar(pit);
860 // get the maximum ascent and the maximum descent
861 double layoutasc = 0;
862 double layoutdesc = 0;
863 double const dh = defaultRowHeight();
865 // ok, let us initialize the maxasc and maxdesc value.
866 // Only the fontsize count. The other properties
867 // are taken from the layoutfont. Nicer on the screen :)
868 LayoutPtr const & layout = par.layout();
870 // as max get the first character of this row then it can
871 // increase but not decrease the height. Just some point to
872 // start with so we don't have to do the assignment below too
874 Buffer const & buffer = bv_->buffer();
875 Font font = getDisplayFont(pit, first);
876 FontSize const tmpsize = font.fontInfo().size();
877 font.fontInfo() = text_->getLayoutFont(buffer, pit);
878 FontSize const size = font.fontInfo().size();
879 font.fontInfo().setSize(tmpsize);
881 FontInfo labelfont = text_->getLabelFont(buffer, par);
883 FontMetrics const & labelfont_metrics = theFontMetrics(labelfont);
884 FontMetrics const & fontmetrics = theFontMetrics(font);
886 // these are minimum values
887 double const spacing_val = layout->spacing.getValue()
888 * text_->spacing(buffer, par);
889 //lyxerr << "spacing_val = " << spacing_val << endl;
890 int maxasc = int(fontmetrics.maxAscent() * spacing_val);
891 int maxdesc = int(fontmetrics.maxDescent() * spacing_val);
893 // insets may be taller
894 ParagraphMetrics const & pm = par_metrics_[pit];
895 InsetList::const_iterator ii = par.insetList().begin();
896 InsetList::const_iterator iend = par.insetList().end();
897 for ( ; ii != iend; ++ii) {
898 Dimension const & dim = pm.insetDimension(ii->inset);
899 if (ii->pos >= first && ii->pos < end) {
900 maxasc = max(maxasc, dim.ascent());
901 maxdesc = max(maxdesc, dim.descent());
905 // Check if any custom fonts are larger (Asger)
906 // This is not completely correct, but we can live with the small,
907 // cosmetic error for now.
911 par.highestFontInRange(first, end, size);
912 if (maxsize > font.fontInfo().size()) {
913 // use standard paragraph font with the maximal size
914 FontInfo maxfont = font.fontInfo();
915 maxfont.setSize(maxsize);
916 FontMetrics const & maxfontmetrics = theFontMetrics(maxfont);
917 maxasc = max(maxasc, maxfontmetrics.maxAscent());
918 maxdesc = max(maxdesc, maxfontmetrics.maxDescent());
921 // This is nicer with box insets:
925 ParagraphList const & pars = text_->paragraphs();
929 BufferParams const & bufparams = buffer.params();
930 // some parskips VERY EASY IMPLEMENTATION
931 if (bufparams.paragraph_separation
932 == BufferParams::PARSEP_SKIP
933 && par.ownerCode() != ERT_CODE
934 && par.ownerCode() != LISTINGS_CODE
936 && ((layout->isParagraph() && par.getDepth() == 0)
937 || (pars[pit - 1].layout()->isParagraph()
938 && pars[pit - 1].getDepth() == 0)))
940 maxasc += bufparams.getDefSkip().inPixels(*bv_);
943 if (par.params().startOfAppendix())
944 maxasc += int(3 * dh);
946 // This is special code for the chapter, since the label of this
947 // layout is printed in an extra row
948 if (layout->counter == "chapter"
949 && !par.params().labelString().empty()) {
950 labeladdon = int(labelfont_metrics.maxHeight()
951 * layout->spacing.getValue()
952 * text_->spacing(buffer, par));
955 // special code for the top label
956 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
957 || layout->labeltype == LABEL_BIBLIO
958 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
959 && isFirstInSequence(pit, pars)
960 && !par.getLabelstring().empty())
963 labelfont_metrics.maxHeight()
964 * layout->spacing.getValue()
965 * text_->spacing(buffer, par)
966 + (layout->topsep + layout->labelbottomsep) * dh);
969 // Add the layout spaces, for example before and after
970 // a section, or between the items of a itemize or enumerate
973 pit_type prev = depthHook(pit, pars, par.getDepth());
974 Paragraph const & prevpar = pars[prev];
976 && prevpar.layout() == layout
977 && prevpar.getDepth() == par.getDepth()
978 && prevpar.getLabelWidthString()
979 == par.getLabelWidthString()) {
980 layoutasc = layout->itemsep * dh;
981 } else if (pit != 0 || first != 0) {
982 if (layout->topsep > 0)
983 layoutasc = layout->topsep * dh;
986 prev = outerHook(pit, pars);
987 if (prev != pit_type(pars.size())) {
988 maxasc += int(pars[prev].layout()->parsep * dh);
989 } else if (pit != 0) {
990 Paragraph const & prevpar = pars[pit - 1];
991 if (prevpar.getDepth() != 0 ||
992 prevpar.layout() == layout) {
993 maxasc += int(layout->parsep * dh);
998 // is it a bottom line?
999 if (end >= par.size()) {
1000 // add the layout spaces, for example before and after
1001 // a section, or between the items of a itemize or enumerate
1003 pit_type nextpit = pit + 1;
1004 if (nextpit != pit_type(pars.size())) {
1005 pit_type cpit = pit;
1009 if (pars[cpit].getDepth() > pars[nextpit].getDepth()) {
1010 usual = pars[cpit].layout()->bottomsep * dh;
1011 cpit = depthHook(cpit, pars, pars[nextpit].getDepth());
1012 if (pars[cpit].layout() != pars[nextpit].layout()
1013 || pars[nextpit].getLabelWidthString() != pars[cpit].getLabelWidthString())
1015 unusual = pars[cpit].layout()->bottomsep * dh;
1017 layoutdesc = max(unusual, usual);
1018 } else if (pars[cpit].getDepth() == pars[nextpit].getDepth()) {
1019 if (pars[cpit].layout() != pars[nextpit].layout()
1020 || pars[nextpit].getLabelWidthString() != pars[cpit].getLabelWidthString())
1021 layoutdesc = int(pars[cpit].layout()->bottomsep * dh);
1026 // incalculate the layout spaces
1027 maxasc += int(layoutasc * 2 / (2 + pars[pit].getDepth()));
1028 maxdesc += int(layoutdesc * 2 / (2 + pars[pit].getDepth()));
1030 // FIXME: the correct way is to do the following is to move the
1031 // following code in another method specially tailored for the
1032 // main Text. The following test is thus bogus.
1033 // Top and bottom margin of the document (only at top-level)
1035 if (pit == 0 && first == 0)
1037 if (pit + 1 == pit_type(pars.size()) &&
1038 end == par.size() &&
1039 !(end > 0 && par.isNewline(end - 1)))
1043 return boost::make_tuple(maxasc + labeladdon, maxdesc);
1047 // x is an absolute screen coord
1048 // returns the column near the specified x-coordinate of the row
1049 // x is set to the real beginning of this column
1050 pos_type TextMetrics::getColumnNearX(pit_type const pit,
1051 Row const & row, int & x, bool & boundary) const
1053 Buffer const & buffer = bv_->buffer();
1055 /// For the main Text, it is possible that this pit is not
1056 /// yet in the CoordCache when moving cursor up.
1057 /// x Paragraph coordinate is always 0 for main text anyway.
1058 int const xo = origin_.x_;
1060 Paragraph const & par = text_->getPar(pit);
1061 ParagraphMetrics const & pm = par_metrics_[pit];
1063 bidi.computeTables(par, buffer, row);
1065 pos_type vc = row.pos();
1066 pos_type end = row.endpos();
1068 LayoutPtr const & layout = par.layout();
1070 bool left_side = false;
1072 pos_type body_pos = par.beginOfBody();
1074 double tmpx = row.x;
1075 double last_tmpx = tmpx;
1078 (body_pos > end || !par.isLineSeparator(body_pos - 1)))
1081 // check for empty row
1087 while (vc < end && tmpx <= x) {
1088 c = bidi.vis2log(vc);
1090 if (body_pos > 0 && c == body_pos - 1) {
1091 FontMetrics const & fm = theFontMetrics(
1092 text_->getLabelFont(buffer, par));
1093 tmpx += row.label_hfill + fm.width(layout->labelsep);
1094 if (par.isLineSeparator(body_pos - 1))
1095 tmpx -= singleWidth(pit, body_pos - 1);
1098 if (pm.hfillExpansion(row, c)) {
1099 tmpx += singleWidth(pit, c);
1103 tmpx += row.label_hfill;
1104 } else if (par.isSeparator(c)) {
1105 tmpx += singleWidth(pit, c);
1107 tmpx += row.separator;
1109 tmpx += singleWidth(pit, c);
1114 if ((tmpx + last_tmpx) / 2 > x) {
1119 BOOST_ASSERT(vc <= end); // This shouldn't happen.
1122 // This (rtl_support test) is not needed, but gives
1123 // some speedup if rtl_support == false
1124 bool const lastrow = lyxrc.rtl_support && row.endpos() == par.size();
1126 // If lastrow is false, we don't need to compute
1127 // the value of rtl.
1128 bool const rtl = lastrow ? text_->isRTL(buffer, par) : false;
1130 ((rtl && left_side && vc == row.pos() && x < tmpx - 5) ||
1131 (!rtl && !left_side && vc == end && x > tmpx + 5)))
1133 else if (vc == row.pos()) {
1134 c = bidi.vis2log(vc);
1135 if (bidi.level(c) % 2 == 1)
1138 c = bidi.vis2log(vc - 1);
1139 bool const rtl = (bidi.level(c) % 2 == 1);
1140 if (left_side == rtl) {
1142 boundary = isRTLBoundary(pit, c);
1146 // I believe this code is not needed anymore (Jug 20050717)
1148 // The following code is necessary because the cursor position past
1149 // the last char in a row is logically equivalent to that before
1150 // the first char in the next row. That's why insets causing row
1151 // divisions -- Newline and display-style insets -- must be treated
1152 // specially, so cursor up/down doesn't get stuck in an air gap -- MV
1153 // Newline inset, air gap below:
1154 if (row.pos() < end && c >= end && par.isNewline(end - 1)) {
1155 if (bidi.level(end -1) % 2 == 0)
1156 tmpx -= singleWidth(pit, end - 1);
1158 tmpx += singleWidth(pit, end - 1);
1162 // Air gap above display inset:
1163 if (row.pos() < end && c >= end && end < par.size()
1164 && par.isInset(end) && par.getInset(end)->display()) {
1167 // Air gap below display inset:
1168 if (row.pos() < end && c >= end && par.isInset(end - 1)
1169 && par.getInset(end - 1)->display()) {
1175 pos_type const col = c - row.pos();
1177 if (!c || end == par.size())
1180 if (c==end && !par.isLineSeparator(c-1) && !par.isNewline(c-1)) {
1185 return min(col, end - 1 - row.pos());
1189 pos_type TextMetrics::x2pos(pit_type pit, int row, int x) const
1191 // We play safe and use parMetrics(pit) to make sure the
1192 // ParagraphMetrics will be redone and OK to use if needed.
1193 // Otherwise we would use an empty ParagraphMetrics in
1194 // upDownInText() while in selection mode.
1195 ParagraphMetrics const & pm = parMetrics(pit);
1197 BOOST_ASSERT(row < int(pm.rows().size()));
1199 Row const & r = pm.rows()[row];
1200 return r.pos() + getColumnNearX(pit, r, x, bound);
1204 void TextMetrics::newParMetricsDown()
1206 pair<pit_type, ParagraphMetrics> const & last = *par_metrics_.rbegin();
1207 pit_type const pit = last.first + 1;
1208 if (pit == int(text_->paragraphs().size()))
1211 // do it and update its position.
1213 par_metrics_[pit].setPosition(last.second.position()
1214 + last.second.descent());
1218 void TextMetrics::newParMetricsUp()
1220 pair<pit_type, ParagraphMetrics> const & first = *par_metrics_.begin();
1221 if (first.first == 0)
1224 pit_type const pit = first.first - 1;
1225 // do it and update its position.
1227 par_metrics_[pit].setPosition(first.second.position()
1228 - first.second.ascent());
1231 // y is screen coordinate
1232 pit_type TextMetrics::getPitNearY(int y)
1234 BOOST_ASSERT(!text_->paragraphs().empty());
1235 LYXERR(Debug::DEBUG)
1236 << BOOST_CURRENT_FUNCTION
1237 << ": y: " << y << " cache size: " << par_metrics_.size()
1240 // look for highest numbered paragraph with y coordinate less than given y
1243 ParMetricsCache::const_iterator it = par_metrics_.begin();
1244 ParMetricsCache::const_iterator et = par_metrics_.end();
1245 ParMetricsCache::const_iterator last = et; last--;
1247 ParagraphMetrics const & pm = it->second;
1249 // If we are off-screen (before the visible part)
1251 // and even before the first paragraph in the cache.
1252 && y < it->second.position() - int(pm.ascent())) {
1253 // and we are not at the first paragraph in the inset.
1256 // then this is the paragraph we are looking for.
1257 pit = it->first - 1;
1258 // rebreak it and update the CoordCache.
1260 par_metrics_[pit].setPosition(it->second.position() - pm.descent());
1264 ParagraphMetrics const & pm_last = par_metrics_[last->first];
1266 // If we are off-screen (after the visible part)
1267 if (y > bv_->workHeight()
1268 // and even after the first paragraph in the cache.
1269 && y >= last->second.position() + int(pm_last.descent())) {
1270 pit = last->first + 1;
1271 // and we are not at the last paragraph in the inset.
1272 if (pit == int(text_->paragraphs().size()))
1274 // then this is the paragraph we are looking for.
1275 // rebreak it and update the CoordCache.
1277 par_metrics_[pit].setPosition(last->second.position() + pm_last.ascent());
1281 for (; it != et; ++it) {
1282 LYXERR(Debug::DEBUG)
1283 << BOOST_CURRENT_FUNCTION
1284 << " examining: pit: " << it->first
1285 << " y: " << it->second.position()
1288 ParagraphMetrics const & pm = par_metrics_[it->first];
1290 if (it->first >= pit && int(it->second.position()) - int(pm.ascent()) <= y) {
1292 yy = it->second.position();
1296 LYXERR(Debug::DEBUG)
1297 << BOOST_CURRENT_FUNCTION
1298 << ": found best y: " << yy << " for pit: " << pit
1305 Row const & TextMetrics::getRowNearY(int y, pit_type pit) const
1307 ParagraphMetrics const & pm = par_metrics_[pit];
1309 int yy = pm.position() - pm.ascent();
1310 BOOST_ASSERT(!pm.rows().empty());
1311 RowList::const_iterator rit = pm.rows().begin();
1312 RowList::const_iterator rlast = pm.rows().end();
1314 for (; rit != rlast; yy += rit->height(), ++rit)
1315 if (yy + rit->height() > y)
1321 // x,y are absolute screen coordinates
1322 // sets cursor recursively descending into nested editable insets
1323 Inset * TextMetrics::editXY(Cursor & cur, int x, int y)
1325 if (lyxerr.debugging(Debug::WORKAREA)) {
1326 lyxerr << "TextMetrics::editXY(cur, " << x << ", " << y << ")" << std::endl;
1327 cur.bv().coordCache().dump();
1329 pit_type pit = getPitNearY(y);
1330 BOOST_ASSERT(pit != -1);
1332 Row const & row = getRowNearY(y, pit);
1335 int xx = x; // is modified by getColumnNearX
1336 pos_type const pos = row.pos()
1337 + getColumnNearX(pit, row, xx, bound);
1340 cur.boundary(bound);
1343 // try to descend into nested insets
1344 Inset * inset = checkInsetHit(x, y);
1345 //lyxerr << "inset " << inset << " hit at x: " << x << " y: " << y << endl;
1347 // Either we deconst editXY or better we move current_font
1348 // and real_current_font to Cursor
1349 // FIXME: what is needed now that current_font and real_current_font
1351 cur.setCurrentFont();
1355 ParagraphList const & pars = text_->paragraphs();
1356 Inset const * insetBefore = pos? pars[pit].getInset(pos - 1): 0;
1357 //Inset * insetBehind = pars[pit].getInset(pos);
1359 // This should be just before or just behind the
1360 // cursor position set above.
1361 BOOST_ASSERT((pos != 0 && inset == insetBefore)
1362 || inset == pars[pit].getInset(pos));
1364 // Make sure the cursor points to the position before
1366 if (inset == insetBefore) {
1368 cur.boundary(false);
1371 // Try to descend recursively inside the inset.
1372 inset = inset->editXY(cur, x, y);
1374 if (cur.top().text() == text_)
1375 cur.setCurrentFont();
1380 void TextMetrics::setCursorFromCoordinates(Cursor & cur, int const x, int const y)
1382 BOOST_ASSERT(text_ == cur.text());
1383 pit_type pit = getPitNearY(y);
1385 ParagraphMetrics const & pm = par_metrics_[pit];
1387 int yy = pm.position() - pm.ascent();
1388 LYXERR(Debug::DEBUG)
1389 << BOOST_CURRENT_FUNCTION
1393 << " yy: " << yy << endl;
1396 BOOST_ASSERT(pm.rows().size());
1397 for (; r < int(pm.rows().size()) - 1; ++r) {
1398 Row const & row = pm.rows()[r];
1399 if (int(yy + row.height()) > y)
1404 Row const & row = pm.rows()[r];
1406 LYXERR(Debug::DEBUG)
1407 << BOOST_CURRENT_FUNCTION
1409 << " from pos: " << row.pos()
1414 pos_type const pos = row.pos() + getColumnNearX(pit, row, xx, bound);
1416 LYXERR(Debug::DEBUG)
1417 << BOOST_CURRENT_FUNCTION
1418 << ": setting cursor pit: " << pit
1422 text_->setCursor(cur, pit, pos, true, bound);
1423 // remember new position.
1428 //takes screen x,y coordinates
1429 Inset * TextMetrics::checkInsetHit(int x, int y)
1431 pit_type pit = getPitNearY(y);
1432 BOOST_ASSERT(pit != -1);
1434 Paragraph const & par = text_->paragraphs()[pit];
1435 ParagraphMetrics const & pm = par_metrics_[pit];
1437 LYXERR(Debug::DEBUG)
1438 << BOOST_CURRENT_FUNCTION
1443 InsetList::const_iterator iit = par.insetList().begin();
1444 InsetList::const_iterator iend = par.insetList().end();
1445 for (; iit != iend; ++iit) {
1446 Inset * inset = iit->inset;
1448 LYXERR(Debug::DEBUG)
1449 << BOOST_CURRENT_FUNCTION
1450 << ": examining inset " << inset << endl;
1452 if (!bv_->coordCache().getInsets().has(inset)) {
1453 LYXERR(Debug::DEBUG)
1454 << BOOST_CURRENT_FUNCTION
1455 << ": inset has no cached position" << endl;
1459 Dimension const & dim = pm.insetDimension(inset);
1460 Point p = bv_->coordCache().getInsets().xy(inset);
1462 LYXERR(Debug::DEBUG)
1463 << BOOST_CURRENT_FUNCTION
1464 << ": xo: " << p.x_ << "..." << p.x_ + dim.wid
1465 << " yo: " << p.y_ - dim.asc << "..." << p.y_ + dim.des
1469 && x <= p.x_ + dim.wid
1470 && y >= p.y_ - dim.asc
1471 && y <= p.y_ + dim.des) {
1472 LYXERR(Debug::DEBUG)
1473 << BOOST_CURRENT_FUNCTION
1474 << ": Hit inset: " << inset << endl;
1479 LYXERR(Debug::DEBUG)
1480 << BOOST_CURRENT_FUNCTION
1481 << ": No inset hit. " << endl;
1486 int TextMetrics::cursorX(CursorSlice const & sl,
1487 bool boundary) const
1489 BOOST_ASSERT(sl.text() == text_);
1490 pit_type const pit = sl.pit();
1491 Paragraph const & par = text_->paragraphs()[pit];
1492 ParagraphMetrics const & pm = par_metrics_[pit];
1493 if (pm.rows().empty())
1496 pos_type ppos = sl.pos();
1497 // Correct position in front of big insets
1498 bool const boundary_correction = ppos != 0 && boundary;
1499 if (boundary_correction)
1502 Row const & row = pm.getRow(sl.pos(), boundary);
1504 pos_type cursor_vpos = 0;
1506 Buffer const & buffer = bv_->buffer();
1509 bidi.computeTables(par, buffer, row);
1511 pos_type const row_pos = row.pos();
1512 pos_type const end = row.endpos();
1513 // Spaces at logical line breaks in bidi text must be skipped during
1514 // cursor positioning. However, they may appear visually in the middle
1515 // of a row; they must be skipped, wherever they are...
1516 // * logically "abc_[HEBREW_\nHEBREW]"
1517 // * visually "abc_[_WERBEH\nWERBEH]"
1518 pos_type skipped_sep_vpos = -1;
1521 cursor_vpos = row_pos;
1522 else if (ppos >= end)
1523 cursor_vpos = text_->isRTL(buffer, par) ? row_pos : end;
1524 else if (ppos > row_pos && ppos >= end)
1525 // Place cursor after char at (logical) position pos - 1
1526 cursor_vpos = (bidi.level(ppos - 1) % 2 == 0)
1527 ? bidi.log2vis(ppos - 1) + 1 : bidi.log2vis(ppos - 1);
1529 // Place cursor before char at (logical) position ppos
1530 cursor_vpos = (bidi.level(ppos) % 2 == 0)
1531 ? bidi.log2vis(ppos) : bidi.log2vis(ppos) + 1;
1533 pos_type body_pos = par.beginOfBody();
1535 (body_pos > end || !par.isLineSeparator(body_pos - 1)))
1538 // Use font span to speed things up, see below
1542 // If the last logical character is a separator, skip it, unless
1543 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
1544 if (end > 0 && end < par.size() && par.isSeparator(end - 1))
1545 skipped_sep_vpos = bidi.log2vis(end - 1);
1547 for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
1548 // Skip the separator which is at the logical end of the row
1549 if (vpos == skipped_sep_vpos)
1551 pos_type pos = bidi.vis2log(vpos);
1552 if (body_pos > 0 && pos == body_pos - 1) {
1553 FontMetrics const & labelfm = theFontMetrics(
1554 text_->getLabelFont(buffer, par));
1555 x += row.label_hfill + labelfm.width(par.layout()->labelsep);
1556 if (par.isLineSeparator(body_pos - 1))
1557 x -= singleWidth(pit, body_pos - 1);
1560 // Use font span to speed things up, see above
1561 if (pos < font_span.first || pos > font_span.last) {
1562 font_span = par.fontSpan(pos);
1563 font = getDisplayFont(pit, pos);
1566 x += pm.singleWidth(pos, font);
1568 if (pm.hfillExpansion(row, pos))
1569 x += (pos >= body_pos) ? row.hfill : row.label_hfill;
1570 else if (par.isSeparator(pos) && pos >= body_pos)
1574 // see correction above
1575 if (boundary_correction) {
1576 if (isRTL(sl, boundary))
1577 x -= singleWidth(pit, ppos);
1579 x += singleWidth(pit, ppos);
1586 int TextMetrics::cursorY(CursorSlice const & sl, bool boundary) const
1588 //lyxerr << "TextMetrics::cursorY: boundary: " << boundary << std::endl;
1589 ParagraphMetrics const & pm = par_metrics_[sl.pit()];
1590 if (pm.rows().empty())
1594 h -= par_metrics_[0].rows()[0].ascent();
1595 for (pit_type pit = 0; pit < sl.pit(); ++pit) {
1596 h += par_metrics_[pit].height();
1599 if (pos && boundary)
1601 size_t const rend = pm.pos2row(pos);
1602 for (size_t rit = 0; rit != rend; ++rit)
1603 h += pm.rows()[rit].height();
1604 h += pm.rows()[rend].ascent();
1609 void TextMetrics::cursorPrevious(Cursor & cur)
1611 pos_type cpos = cur.pos();
1612 pit_type cpar = cur.pit();
1614 int x = cur.x_target();
1615 setCursorFromCoordinates(cur, x, 0);
1616 cur.dispatch(FuncRequest(cur.selection()? LFUN_UP_SELECT: LFUN_UP));
1618 if (cpar == cur.pit() && cpos == cur.pos())
1619 // we have a row which is taller than the workarea. The
1620 // simplest solution is to move to the previous row instead.
1621 cur.dispatch(FuncRequest(cur.selection()? LFUN_UP_SELECT: LFUN_UP));
1624 cur.updateFlags(Update::Force | Update::FitCursor);
1628 void TextMetrics::cursorNext(Cursor & cur)
1630 pos_type cpos = cur.pos();
1631 pit_type cpar = cur.pit();
1633 int x = cur.x_target();
1634 setCursorFromCoordinates(cur, x, cur.bv().workHeight() - 1);
1635 cur.dispatch(FuncRequest(cur.selection()? LFUN_DOWN_SELECT: LFUN_DOWN));
1637 if (cpar == cur.pit() && cpos == cur.pos())
1638 // we have a row which is taller than the workarea. The
1639 // simplest solution is to move to the next row instead.
1641 FuncRequest(cur.selection()? LFUN_DOWN_SELECT: LFUN_DOWN));
1644 cur.updateFlags(Update::Force | Update::FitCursor);
1648 // the cursor set functions have a special mechanism. When they
1649 // realize you left an empty paragraph, they will delete it.
1651 bool TextMetrics::cursorHome(Cursor & cur)
1653 BOOST_ASSERT(text_ == cur.text());
1654 ParagraphMetrics const & pm = par_metrics_[cur.pit()];
1655 Row const & row = pm.getRow(cur.pos(),cur.boundary());
1656 return text_->setCursor(cur, cur.pit(), row.pos());
1660 bool TextMetrics::cursorEnd(Cursor & cur)
1662 BOOST_ASSERT(text_ == cur.text());
1663 // if not on the last row of the par, put the cursor before
1664 // the final space exept if I have a spanning inset or one string
1665 // is so long that we force a break.
1666 pos_type end = cur.textRow().endpos();
1668 // empty text, end-1 is no valid position
1670 bool boundary = false;
1671 if (end != cur.lastpos()) {
1672 if (!cur.paragraph().isLineSeparator(end-1)
1673 && !cur.paragraph().isNewline(end-1))
1678 return text_->setCursor(cur, cur.pit(), end, true, boundary);
1682 void TextMetrics::deleteLineForward(Cursor & cur)
1684 BOOST_ASSERT(text_ == cur.text());
1685 if (cur.lastpos() == 0) {
1686 // Paragraph is empty, so we just go forward
1687 text_->cursorForward(cur);
1690 cur.selection() = true; // to avoid deletion
1693 // What is this test for ??? (JMarc)
1694 if (!cur.selection())
1695 text_->deleteWordForward(cur);
1697 cap::cutSelection(cur, true, false);
1698 checkBufferStructure(cur.buffer(), cur);
1703 bool TextMetrics::isLastRow(pit_type pit, Row const & row) const
1705 ParagraphList const & pars = text_->paragraphs();
1706 return row.endpos() >= pars[pit].size()
1707 && pit + 1 == pit_type(pars.size());
1711 bool TextMetrics::isFirstRow(pit_type pit, Row const & row) const
1713 return row.pos() == 0 && pit == 0;
1717 int TextMetrics::leftMargin(int max_width, pit_type pit) const
1719 BOOST_ASSERT(pit >= 0);
1720 BOOST_ASSERT(pit < int(text_->paragraphs().size()));
1721 return leftMargin(max_width, pit, text_->paragraphs()[pit].size());
1725 int TextMetrics::leftMargin(int max_width,
1726 pit_type const pit, pos_type const pos) const
1728 ParagraphList const & pars = text_->paragraphs();
1730 BOOST_ASSERT(pit >= 0);
1731 BOOST_ASSERT(pit < int(pars.size()));
1732 Paragraph const & par = pars[pit];
1733 BOOST_ASSERT(pos >= 0);
1734 BOOST_ASSERT(pos <= par.size());
1735 Buffer const & buffer = bv_->buffer();
1736 //lyxerr << "TextMetrics::leftMargin: pit: " << pit << " pos: " << pos << endl;
1737 TextClass const & tclass = buffer.params().getTextClass();
1738 LayoutPtr const & layout = par.layout();
1740 docstring parindent = layout->parindent;
1744 if (text_->isMainText(buffer))
1745 l_margin += changebarMargin();
1747 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(
1748 tclass.leftmargin());
1750 if (par.getDepth() != 0) {
1751 // find the next level paragraph
1752 pit_type newpar = outerHook(pit, pars);
1753 if (newpar != pit_type(pars.size())) {
1754 if (pars[newpar].layout()->isEnvironment()) {
1755 l_margin = leftMargin(max_width, newpar);
1757 if (par.layout() == tclass.defaultLayout()) {
1758 if (pars[newpar].params().noindent())
1761 parindent = pars[newpar].layout()->parindent;
1766 // This happens after sections in standard classes. The 1.3.x
1767 // code compared depths too, but it does not seem necessary
1769 if (par.layout() == tclass.defaultLayout()
1770 && pit > 0 && pars[pit - 1].layout()->nextnoindent)
1773 FontInfo const labelfont = text_->getLabelFont(buffer, par);
1774 FontMetrics const & labelfont_metrics = theFontMetrics(labelfont);
1776 switch (layout->margintype) {
1777 case MARGIN_DYNAMIC:
1778 if (!layout->leftmargin.empty()) {
1779 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(
1780 layout->leftmargin);
1782 if (!par.getLabelstring().empty()) {
1783 l_margin += labelfont_metrics.signedWidth(layout->labelindent);
1784 l_margin += labelfont_metrics.width(par.getLabelstring());
1785 l_margin += labelfont_metrics.width(layout->labelsep);
1789 case MARGIN_MANUAL: {
1790 l_margin += labelfont_metrics.signedWidth(layout->labelindent);
1791 // The width of an empty par, even with manual label, should be 0
1792 if (!par.empty() && pos >= par.beginOfBody()) {
1793 if (!par.getLabelWidthString().empty()) {
1794 docstring labstr = par.getLabelWidthString();
1795 l_margin += labelfont_metrics.width(labstr);
1796 l_margin += labelfont_metrics.width(layout->labelsep);
1802 case MARGIN_STATIC: {
1803 l_margin += theFontMetrics(buffer.params().getFont()).
1804 signedWidth(layout->leftmargin) * 4 / (par.getDepth() + 4);
1808 case MARGIN_FIRST_DYNAMIC:
1809 if (layout->labeltype == LABEL_MANUAL) {
1810 if (pos >= par.beginOfBody()) {
1811 l_margin += labelfont_metrics.signedWidth(layout->leftmargin);
1813 l_margin += labelfont_metrics.signedWidth(layout->labelindent);
1816 // Special case to fix problems with
1818 || (layout->labeltype == LABEL_STATIC
1819 && layout->latextype == LATEX_ENVIRONMENT
1820 && !isFirstInSequence(pit, pars))) {
1821 l_margin += labelfont_metrics.signedWidth(layout->leftmargin);
1822 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
1823 && layout->labeltype != LABEL_BIBLIO
1824 && layout->labeltype !=
1825 LABEL_CENTERED_TOP_ENVIRONMENT) {
1826 l_margin += labelfont_metrics.signedWidth(layout->labelindent);
1827 l_margin += labelfont_metrics.width(layout->labelsep);
1828 l_margin += labelfont_metrics.width(par.getLabelstring());
1832 case MARGIN_RIGHT_ADDRESS_BOX: {
1834 // ok, a terrible hack. The left margin depends on the widest
1835 // row in this paragraph.
1836 RowList::iterator rit = par.rows().begin();
1837 RowList::iterator end = par.rows().end();
1838 // FIXME: This is wrong.
1839 int minfill = max_width;
1840 for ( ; rit != end; ++rit)
1841 if (rit->fill() < minfill)
1842 minfill = rit->fill();
1843 l_margin += theFontMetrics(params.getFont()).signedWidth(layout->leftmargin);
1844 l_margin += minfill;
1846 // also wrong, but much shorter.
1847 l_margin += max_width / 2;
1852 if (!par.params().leftIndent().zero())
1853 l_margin += par.params().leftIndent().inPixels(max_width);
1857 if (par.params().align() == LYX_ALIGN_LAYOUT)
1858 align = layout->align;
1860 align = par.params().align();
1862 // set the correct parindent
1864 && (layout->labeltype == LABEL_NO_LABEL
1865 || layout->labeltype == LABEL_TOP_ENVIRONMENT
1866 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
1867 || (layout->labeltype == LABEL_STATIC
1868 && layout->latextype == LATEX_ENVIRONMENT
1869 && !isFirstInSequence(pit, pars)))
1870 && align == LYX_ALIGN_BLOCK
1871 && !par.params().noindent()
1872 // in some insets, paragraphs are never indented
1873 && !(par.inInset() && par.inInset()->neverIndent(buffer))
1874 // display style insets are always centered, omit indentation
1877 && par.getInset(pos)->display())
1878 && (par.layout() != tclass.defaultLayout()
1879 || buffer.params().paragraph_separation ==
1880 BufferParams::PARSEP_INDENT))
1882 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(
1890 int TextMetrics::singleWidth(pit_type pit, pos_type pos) const
1892 ParagraphMetrics const & pm = par_metrics_[pit];
1894 return pm.singleWidth(pos, getDisplayFont(pit, pos));
1898 // only used for inset right now. should also be used for main text
1899 void TextMetrics::draw(PainterInfo & pi, int x, int y) const
1901 if (par_metrics_.empty())
1907 ParMetricsCache::iterator it = par_metrics_.begin();
1908 ParMetricsCache::iterator const pm_end = par_metrics_.end();
1909 y -= it->second.ascent();
1910 for (; it != pm_end; ++it) {
1911 ParagraphMetrics const & pmi = it->second;
1913 pit_type const pit = it->first;
1914 // Save the paragraph position in the cache.
1915 it->second.setPosition(y);
1916 drawParagraph(pi, pit, x, y);
1922 void TextMetrics::drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const
1924 // lyxerr << " paintPar: pit: " << pit << " at y: " << y << endl;
1925 int const ww = bv_->workHeight();
1927 ParagraphMetrics const & pm = par_metrics_[pit];
1928 if (pm.rows().empty())
1931 RowList::const_iterator const rb = pm.rows().begin();
1932 RowList::const_iterator const re = pm.rows().end();
1937 for (RowList::const_iterator rit = rb; rit != re; ++rit) {
1940 bool const inside = (y + rit->descent() >= 0
1941 && y - rit->ascent() < ww);
1942 // it is not needed to draw on screen if we are not inside.
1943 pi.pain.setDrawingEnabled(inside);
1944 RowPainter rp(pi, *text_, pit, *rit, bidi, x, y);
1946 // Row signature; has row changed since last paint?
1947 bool row_has_changed = rit->changed();
1949 bool row_selection = rit->sel_beg != -1 && rit->sel_end != -1;
1951 if (!row_selection && !pi.full_repaint && !row_has_changed) {
1952 // Paint the only the insets if the text itself is
1954 rp.paintOnlyInsets();
1955 y += rit->descent();
1959 // Paint the row if a full repaint has been requested or it has
1961 // Clear background of this row
1962 // (if paragraph background was not cleared)
1963 if (row_selection || (!pi.full_repaint && row_has_changed)) {
1964 pi.pain.fillRectangle(x, y - rit->ascent(),
1965 width(), rit->height(), pi.background_color);
1967 if (row_selection) {
1968 DocIterator beg = bv_->cursor().selectionBegin();
1969 DocIterator end = bv_->cursor().selectionEnd();
1971 beg.pos() = rit->sel_beg;
1972 if (pit == end.pit()) {
1973 end.pos() = rit->sel_end;
1975 end.pit() = pit + 1;
1978 drawSelection(pi, beg, end, x);
1981 // Instrumentation for testing row cache (see also
1983 if (lyxerr.debugging(Debug::PAINTING)) {
1984 if (text_->isMainText(bv_->buffer()))
1985 LYXERR(Debug::PAINTING) << "\n{" << inside <<
1986 pi.full_repaint << row_has_changed << "}";
1988 LYXERR(Debug::PAINTING) << "[" << inside <<
1989 pi.full_repaint << row_has_changed << "]";
1992 // Backup full_repaint status and force full repaint
1993 // for inner insets as the Row has been cleared out.
1994 bool tmp = pi.full_repaint;
1995 pi.full_repaint = true;
1998 rp.paintChangeBar();
2004 y += rit->descent();
2005 // Restore full_repaint status.
2006 pi.full_repaint = tmp;
2008 // Re-enable screen drawing for future use of the painter.
2009 pi.pain.setDrawingEnabled(true);
2011 //LYXERR(Debug::PAINTING) << "." << endl;
2015 // FIXME: only take care of one row!
2016 void TextMetrics::drawSelection(PainterInfo & pi,
2017 DocIterator const & beg, DocIterator const & end, int x) const
2019 ParagraphMetrics const & pm1 = parMetrics(beg.pit());
2020 ParagraphMetrics const & pm2 = parMetrics(end.pit());
2021 Row const & row1 = pm1.getRow(beg.pos(), beg.boundary());
2022 Row const & row2 = pm2.getRow(end.pos(), end.boundary());
2026 bool const clipAbove = (bv_->cursorStatus(beg) == CUR_ABOVE);
2030 middleTop = bv_->getPos(beg, beg.boundary()).y_ + row1.descent();
2034 bool const clipBelow = (bv_->cursorStatus(end) == CUR_BELOW);
2036 middleBottom = bv_->workHeight();
2038 middleBottom = bv_->getPos(end, end.boundary()).y_ - row2.ascent();
2040 // start and end in the same line?
2041 if (!clipAbove && !clipBelow && &row1 == &row2)
2042 // then only draw this row's selection
2043 drawRowSelection(pi, x, row1, beg, end, false, false);
2047 DocIterator begRowEnd = beg;
2048 begRowEnd.pos() = row1.endpos();
2049 begRowEnd.boundary(true);
2051 // draw upper rectangle
2052 drawRowSelection(pi, x, row1, beg, begRowEnd, false, true);
2055 if (middleTop < middleBottom) {
2056 // draw middle rectangle
2057 pi.pain.fillRectangle(x, middleTop, width(), middleBottom - middleTop,
2063 DocIterator endRowBeg = end;
2064 endRowBeg.pos() = row2.pos();
2065 endRowBeg.boundary(false);
2067 // draw low rectangle
2068 drawRowSelection(pi, x, row2, endRowBeg, end, true, false);
2074 void TextMetrics::drawRowSelection(PainterInfo & pi, int x, Row const & row,
2075 DocIterator const & beg, DocIterator const & end,
2076 bool drawOnBegMargin, bool drawOnEndMargin) const
2078 Buffer & buffer = bv_->buffer();
2079 DocIterator cur = beg;
2080 int x1 = cursorX(beg.top(), beg.boundary());
2081 int x2 = cursorX(end.top(), end.boundary());
2082 int y1 = bv_->getPos(cur, cur.boundary()).y_ - row.ascent();
2083 int y2 = y1 + row.height();
2086 if (drawOnBegMargin) {
2087 if (text_->isRTL(buffer, beg.paragraph()))
2088 pi.pain.fillRectangle(x + x1, y1, width() - x1, y2 - y1, Color_selection);
2090 pi.pain.fillRectangle(x, y1, x1, y2 - y1, Color_selection);
2093 if (drawOnEndMargin) {
2094 if (text_->isRTL(buffer, beg.paragraph()))
2095 pi.pain.fillRectangle(x, y1, x2, y2 - y1, Color_selection);
2097 pi.pain.fillRectangle(x + x2, y1, width() - x2, y2 - y1, Color_selection);
2100 // if we are on a boundary from the beginning, it's probably
2101 // a RTL boundary and we jump to the other side directly as this
2102 // segement is 0-size and confuses the logic below
2104 cur.boundary(false);
2106 // go through row and draw from RTL boundary to RTL boundary
2108 bool drawNow = false;
2110 // simplified cursorForward code below which does not
2111 // descend into insets and which does not go into the
2112 // next line. Compare the logic with the original cursorForward
2114 // if left of boundary -> just jump to right side
2115 // but for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
2116 if (cur.boundary()) {
2117 cur.boundary(false);
2118 } else if (isRTLBoundary(cur.pit(), cur.pos() + 1)) {
2119 // in front of RTL boundary -> Stay on this side of the boundary because:
2120 // ab|cDDEEFFghi -> abc|DDEEFFghi
2129 if (cur.pos() == row.endpos())
2134 // the previous segment was just drawn, now the next starts
2135 x1 = cursorX(cur.top(), cur.boundary());
2138 if (!(cur < end) || drawNow) {
2139 x2 = cursorX(cur.top(), cur.boundary());
2140 pi.pain.fillRectangle(x + min(x1,x2), y1, abs(x2 - x1), y2 - y1,
2143 // reset x1, so it is set again next round (which will be on the
2144 // right side of a boundary or at the selection end)
2150 //int TextMetrics::pos2x(pit_type pit, pos_type pos) const
2152 // ParagraphMetrics const & pm = par_metrics_[pit];
2153 // Row const & r = pm.rows()[row];
2159 int defaultRowHeight()
2161 return int(theFontMetrics(sane_font).maxHeight() * 1.2);