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;
44 ParagraphMetrics::ParagraphMetrics(Paragraph const & par) :
45 position_(-1), par_(&par)
49 ParagraphMetrics & ParagraphMetrics::operator=(
50 ParagraphMetrics const & pm)
55 position_ = pm.position_;
60 void ParagraphMetrics::reset(Paragraph const & par)
68 void ParagraphMetrics::setPosition(int position)
74 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
76 LBUFERR(!rows().empty());
78 // If boundary is set we should return the row on which
79 // the character before is inside.
80 if (pos > 0 && boundary)
83 RowList::const_iterator rit = rows_.end();
84 RowList::const_iterator const begin = rows_.begin();
86 for (--rit; rit != begin && rit->pos() > pos; --rit)
93 size_t ParagraphMetrics::pos2row(pos_type pos) const
95 LBUFERR(!rows().empty());
97 RowList::const_iterator rit = rows_.end();
98 RowList::const_iterator const begin = rows_.begin();
100 for (--rit; rit != begin && rit->pos() > pos; --rit)
107 void ParagraphMetrics::dump() const
109 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
110 for (size_t i = 0; i != rows_.size(); ++i) {
111 lyxerr << " row " << i << ": " << rows_[i];
115 int ParagraphMetrics::rightMargin(BufferView const & bv) const
117 BufferParams const & params = bv.buffer().params();
118 DocumentClass const & tclass = params.documentClass();
119 frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
122 + fm.signedWidth(tclass.rightmargin())
123 + fm.signedWidth(par_->layout().rightmargin)
124 * 4 / (par_->getDepth() + 4);
130 // FIXME: this code seems bogus. Audit and rewrite (see bug #9860).
131 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
133 if (!par_->isHfill(pos))
136 LASSERT(pos >= row.pos() && pos < row.endpos(), return false);
138 // expand at the end of a row only if there is another hfill on the same row
139 if (pos == row.endpos() - 1) {
140 for (pos_type i = row.pos(); i < pos; i++) {
141 if (par_->isHfill(i))
147 // expand at the beginning of a row only if it is the first row of a paragraph
148 if (pos == row.pos())
151 // do not expand in some labels
152 if (par_->layout().margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
155 // if there is anything between the first char of the row and
156 // the specified position that is neither a newline nor an hfill,
157 // the hfill will be expanded, otherwise it won't
158 for (pos_type i = row.pos(); i < pos; i++) {
159 if (!par_->isNewline(i) && !par_->isEnvSeparator(i) && !par_->isHfill(i))