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"
28 #include "LaTeXFeatures.h"
33 #include "OutputParams.h"
35 #include "TextClass.h"
38 #include "frontends/FontMetrics.h"
40 #include "insets/InsetBibitem.h"
41 #include "insets/InsetArgument.h"
43 #include "support/lassert.h"
44 #include "support/debug.h"
45 #include "support/ExceptionMessage.h"
46 #include "support/gettext.h"
47 #include "support/lstrings.h"
48 #include "support/textutils.h"
50 #include "support/bind.h"
51 #include <boost/crc.hpp>
59 using namespace lyx::support;
64 ParagraphMetrics::ParagraphMetrics(Paragraph const & par) :
65 position_(-1), par_(&par)
69 ParagraphMetrics & ParagraphMetrics::operator=(
70 ParagraphMetrics const & pm)
75 position_ = pm.position_;
80 void ParagraphMetrics::reset(Paragraph const & par)
88 size_t ParagraphMetrics::computeRowSignature(Row const & row,
89 BufferParams const & bparams) const
91 boost::crc_32_type crc;
92 for (pos_type i = row.pos(); i < row.endpos(); ++i) {
93 char_type const b[] = { par_->getChar(i) };
94 crc.process_bytes(b, sizeof(char_type));
95 if (bparams.track_changes) {
96 Change change = par_->lookupChange(i);
97 char_type const b[] = { static_cast<char_type>(change.type) };
98 // 1 byte is enough to encode Change::Type
99 crc.process_bytes(b, 1);
103 pos_type const b1[] = { row.sel_beg, row.sel_end };
104 crc.process_bytes(b1, sizeof(b1));
106 Dimension const & d = row.dimension();
107 int const b2[] = { row.begin_margin_sel,
109 d.wid, d.asc, d.des };
110 crc.process_bytes(b2, sizeof(b2));
111 crc.process_bytes(&row.separator, sizeof(row.separator));
113 return crc.checksum();
117 void ParagraphMetrics::setPosition(int position)
119 position_ = position;
123 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
125 LBUFERR(!rows().empty());
127 // If boundary is set we should return the row on which
128 // the character before is inside.
129 if (pos > 0 && boundary)
132 RowList::iterator rit = rows_.end();
133 RowList::iterator const begin = rows_.begin();
135 for (--rit; rit != begin && rit->pos() > pos; --rit)
142 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
144 LBUFERR(!rows().empty());
146 // If boundary is set we should return the row on which
147 // the character before is inside.
148 if (pos > 0 && boundary)
151 RowList::const_iterator rit = rows_.end();
152 RowList::const_iterator const begin = rows_.begin();
154 for (--rit; rit != begin && rit->pos() > pos; --rit)
161 size_t ParagraphMetrics::pos2row(pos_type pos) const
163 LBUFERR(!rows().empty());
165 RowList::const_iterator rit = rows_.end();
166 RowList::const_iterator const begin = rows_.begin();
168 for (--rit; rit != begin && rit->pos() > pos; --rit)
175 void ParagraphMetrics::dump() const
177 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
178 for (size_t i = 0; i != rows_.size(); ++i) {
179 lyxerr << " row " << i << ": " << rows_[i];
183 int ParagraphMetrics::rightMargin(BufferView const & bv) const
185 BufferParams const & params = bv.buffer().params();
186 DocumentClass const & tclass = params.documentClass();
187 frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
190 + fm.signedWidth(tclass.rightmargin())
191 + fm.signedWidth(par_->layout().rightmargin)
192 * 4 / (par_->getDepth() + 4);
198 // FIXME: this code seems bogus. Audit and rewrite (see bug #9860).
199 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
201 if (!par_->isHfill(pos))
204 LASSERT(pos >= row.pos() && pos < row.endpos(), return false);
206 // expand at the end of a row only if there is another hfill on the same row
207 if (pos == row.endpos() - 1) {
208 for (pos_type i = row.pos(); i < pos; i++) {
209 if (par_->isHfill(i))
215 // expand at the beginning of a row only if it is the first row of a paragraph
216 if (pos == row.pos())
219 // do not expand in some labels
220 if (par_->layout().margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
223 // if there is anything between the first char of the row and
224 // the specified position that is neither a newline nor an hfill,
225 // the hfill will be expanded, otherwise it won't
226 for (pos_type i = row.pos(); i < pos; i++) {
227 if (!par_->isNewline(i) && !par_->isEnvSeparator(i) && !par_->isHfill(i))