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
9 * \author Angus Leeming
11 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
20 #include "ParagraphMetrics.h"
23 #include "BufferParams.h"
24 #include "BufferView.h"
27 #include "LayoutEnums.h"
28 #include "Paragraph.h"
29 #include "TextClass.h"
31 #include "frontends/FontMetrics.h"
33 #include "support/lassert.h"
34 #include "support/debug.h"
39 using namespace lyx::support;
43 const int pm_npos = -10000;
45 ParagraphMetrics::ParagraphMetrics(Paragraph const & par) :
46 position_(pm_npos), id_(par.id()), par_(&par)
50 ParagraphMetrics & ParagraphMetrics::operator=(
51 ParagraphMetrics const & pm)
56 position_ = pm.position_;
61 void ParagraphMetrics::reset(Paragraph const & par)
65 //position_ = pm_npos;
69 int ParagraphMetrics::position() const
71 LASSERT(hasPosition(), return pm_npos);
76 void ParagraphMetrics::setPosition(int position)
82 void ParagraphMetrics::resetPosition()
88 bool ParagraphMetrics::hasPosition() const
90 return position_ != pm_npos;
94 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
96 LBUFERR(!rows().empty());
98 // If boundary is set we should return the row on which
99 // the character before is inside.
100 if (pos > 0 && boundary)
103 RowList::const_iterator rit = rows_.end();
104 RowList::const_iterator const begin = rows_.begin();
106 for (--rit; rit != begin && rit->pos() > pos; --rit)
113 size_t ParagraphMetrics::pos2row(pos_type pos) const
115 LBUFERR(!rows().empty());
117 RowList::const_iterator rit = rows_.end();
118 RowList::const_iterator const begin = rows_.begin();
120 for (--rit; rit != begin && rit->pos() > pos; --rit)
127 void ParagraphMetrics::dump() const
129 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
130 for (size_t i = 0; i != rows_.size(); ++i) {
131 lyxerr << " row " << i << ": " << rows_[i];
135 int ParagraphMetrics::rightMargin(BufferView const & bv) const
137 BufferParams const & params = bv.buffer().params();
138 DocumentClass const & tclass = params.documentClass();
139 frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
142 + fm.signedWidth(tclass.rightmargin())
143 + fm.signedWidth(par_->layout().rightmargin)
144 * 4 / (par_->getDepth() + 4);
150 // FIXME: this code seems bogus. Audit and rewrite (see bug #9860).
151 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
153 if (!par_->isHfill(pos))
156 LASSERT(pos >= row.pos() && pos < row.endpos(), return false);
158 // expand at the end of a row only if there is another hfill on the same row
159 if (pos == row.endpos() - 1) {
160 for (pos_type i = row.pos(); i < pos; i++) {
161 if (par_->isHfill(i))
167 // expand at the beginning of a row only if it is the first row of a paragraph
168 if (pos == row.pos())
171 // do not expand in some labels
172 if (par_->layout().margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
175 // if there is anything between the first char of the row and
176 // the specified position that is neither a newline nor an hfill,
177 // the hfill will be expanded, otherwise it won't
178 for (pos_type i = row.pos(); i < pos; i++) {
179 if (!par_->isNewline(i) && !par_->isEnvSeparator(i) && !par_->isHfill(i))