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"
30 #include "TextClass.h"
32 #include "frontends/FontMetrics.h"
34 #include "support/lassert.h"
35 #include "support/debug.h"
40 using namespace lyx::support;
45 ParagraphMetrics::ParagraphMetrics(Paragraph const & par) :
46 position_(-1), par_(&par)
50 ParagraphMetrics & ParagraphMetrics::operator=(
51 ParagraphMetrics const & pm)
56 position_ = pm.position_;
61 void ParagraphMetrics::reset(Paragraph const & par)
69 void ParagraphMetrics::setPosition(int position)
75 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
77 LBUFERR(!rows().empty());
79 // If boundary is set we should return the row on which
80 // the character before is inside.
81 if (pos > 0 && boundary)
84 RowList::const_iterator rit = rows_.end();
85 RowList::const_iterator const begin = rows_.begin();
87 for (--rit; rit != begin && rit->pos() > pos; --rit)
94 size_t ParagraphMetrics::pos2row(pos_type pos) const
96 LBUFERR(!rows().empty());
98 RowList::const_iterator rit = rows_.end();
99 RowList::const_iterator const begin = rows_.begin();
101 for (--rit; rit != begin && rit->pos() > pos; --rit)
108 void ParagraphMetrics::dump() const
110 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
111 for (size_t i = 0; i != rows_.size(); ++i) {
112 lyxerr << " row " << i << ": " << rows_[i];
116 int ParagraphMetrics::rightMargin(BufferView const & bv) const
118 BufferParams const & params = bv.buffer().params();
119 DocumentClass const & tclass = params.documentClass();
120 frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
123 + fm.signedWidth(tclass.rightmargin())
124 + fm.signedWidth(par_->layout().rightmargin)
125 * 4 / (par_->getDepth() + 4);
131 // FIXME: this code seems bogus. Audit and rewrite (see bug #9860).
132 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
134 if (!par_->isHfill(pos))
137 LASSERT(pos >= row.pos() && pos < row.endpos(), return false);
139 // expand at the end of a row only if there is another hfill on the same row
140 if (pos == row.endpos() - 1) {
141 for (pos_type i = row.pos(); i < pos; i++) {
142 if (par_->isHfill(i))
148 // expand at the beginning of a row only if it is the first row of a paragraph
149 if (pos == row.pos())
152 // do not expand in some labels
153 if (par_->layout().margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
156 // if there is anything between the first char of the row and
157 // the specified position that is neither a newline nor an hfill,
158 // the hfill will be expanded, otherwise it won't
159 for (pos_type i = row.pos(); i < pos; i++) {
160 if (!par_->isNewline(i) && !par_->isEnvSeparator(i) && !par_->isHfill(i))