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 "support/debug.h"
28 #include "support/gettext.h"
30 #include "LaTeXFeatures.h"
35 #include "OutputParams.h"
36 #include "paragraph_funcs.h"
38 #include "TextClass.h"
42 #include "frontends/FontMetrics.h"
44 #include "insets/InsetBibitem.h"
45 #include "insets/InsetOptArg.h"
47 #include "support/lstrings.h"
48 #include "support/textutils.h"
49 #include "support/convert.h"
50 #include "support/unicode.h"
52 #include <boost/bind.hpp>
53 #include <boost/crc.hpp>
63 using lyx::support::contains;
64 using lyx::support::rsplit;
73 using std::ostringstream;
76 ParagraphMetrics::ParagraphMetrics(Paragraph const & par): position_(-1), par_(&par)
81 ParagraphMetrics & ParagraphMetrics::operator=(
82 ParagraphMetrics const & pm)
87 position_ = pm.position_;
92 void ParagraphMetrics::reset(Paragraph const & par)
100 size_t ParagraphMetrics::computeRowSignature(Row const & row,
101 BufferParams const & bparams) const
103 boost::crc_32_type crc;
104 for (pos_type i = row.pos(); i < row.endpos(); ++i) {
105 char_type const b[] = { par_->getChar(i) };
106 crc.process_bytes(b, sizeof(char_type));
107 if (bparams.trackChanges) {
108 Change change = par_->lookupChange(i);
109 char_type const b[] = { change.type };
110 // 1 byte is enough to encode Change::Type
111 crc.process_bytes(b, 1);
115 Dimension const & d = row.dimension();
116 char_type const b[] = { row.sel_beg, row.sel_end, d.wid, d.asc, d.des};
117 // Each of the variable to process is 4 bytes: 4x5 = 20
118 crc.process_bytes(b, 20);
120 return crc.checksum();
124 void ParagraphMetrics::setPosition(int position)
126 position_ = position;
130 Dimension const & ParagraphMetrics::insetDimension(Inset const * inset) const
132 InsetDims::const_iterator it = inset_dims_.find(inset);
133 if (it != inset_dims_.end())
136 static Dimension dummy;
141 void ParagraphMetrics::setInsetDimension(Inset const * inset,
142 Dimension const & dim)
144 inset_dims_[inset] = dim;
148 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
150 BOOST_ASSERT(!rows().empty());
152 // If boundary is set we should return the row on which
153 // the character before is inside.
154 if (pos > 0 && boundary)
157 RowList::iterator rit = rows_.end();
158 RowList::iterator const begin = rows_.begin();
160 for (--rit; rit != begin && rit->pos() > pos; --rit)
167 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
169 BOOST_ASSERT(!rows().empty());
171 // If boundary is set we should return the row on which
172 // the character before is inside.
173 if (pos > 0 && boundary)
176 RowList::const_iterator rit = rows_.end();
177 RowList::const_iterator const begin = rows_.begin();
179 for (--rit; rit != begin && rit->pos() > pos; --rit)
186 size_t ParagraphMetrics::pos2row(pos_type pos) const
188 BOOST_ASSERT(!rows().empty());
190 RowList::const_iterator rit = rows_.end();
191 RowList::const_iterator const begin = rows_.begin();
193 for (--rit; rit != begin && rit->pos() > pos; --rit)
200 void ParagraphMetrics::dump() const
202 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
203 for (size_t i = 0; i != rows_.size(); ++i) {
204 lyxerr << " row " << i << ": ";
209 int ParagraphMetrics::rightMargin(Buffer const & buffer) const
211 BufferParams const & params = buffer.params();
212 TextClass const & tclass = params.getTextClass();
213 frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
216 + fm.signedWidth(tclass.rightmargin())
217 + fm.signedWidth(par_->layout()->rightmargin)
218 * 4 / (par_->getDepth() + 4);
224 int ParagraphMetrics::singleWidth(pos_type pos, Font const & font) const
226 // The most special cases are handled first.
227 if (par_->isInset(pos))
228 return insetDimension(par_->getInset(pos)).wid;
230 char_type c = par_->getChar(pos);
233 return theFontMetrics(font).width(c);
235 Language const * language = font.language();
236 if (language->rightToLeft()) {
237 if (language->lang() == "arabic_arabtex" ||
238 language->lang() == "arabic_arabi" ||
239 language->lang() == "farsi") {
240 if (Encodings::isComposeChar_arabic(c))
242 c = par_->transformChar(c, pos);
243 } else if (language->lang() == "hebrew" &&
244 Encodings::isComposeChar_hebrew(c))
247 return theFontMetrics(font).width(c);
251 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
253 if (!par_->isHfill(pos))
256 BOOST_ASSERT(pos >= row.pos() && pos < row.endpos());
258 // expand at the end of a row only if there is another hfill on the same row
259 if (pos == row.endpos() - 1) {
260 for (pos_type i = row.pos(); i < pos; i++) {
261 if (par_->isHfill(i))
267 // expand at the beginning of a row only if it is the first row of a paragraph
268 if (pos == row.pos()) {
272 // do not expand in some labels
273 if (par_->layout()->margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
276 // if there is anything between the first char of the row and
277 // the specified position that is neither a newline nor an hfill,
278 // the hfill will be expanded, otherwise it won't
279 for (pos_type i = row.pos(); i < pos; i++) {
280 if (!par_->isNewline(i) && !par_->isHfill(i))