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"
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, 1);
107 if (bparams.trackChanges) {
108 Change change = par_->lookupChange(i);
109 char_type const b[] = { change.type };
110 crc.process_bytes(b, 1);
114 Dimension const & d = row.dimension();
115 char_type const b[] = { row.sel_beg, row.sel_end, d.wid, d.asc, d.des};
116 crc.process_bytes(b, 5);
118 return crc.checksum();
122 void ParagraphMetrics::setPosition(int position)
124 position_ = position;
128 Dimension const & ParagraphMetrics::insetDimension(Inset const * inset) const
130 InsetDims::const_iterator it = inset_dims_.find(inset);
131 if (it != inset_dims_.end())
134 static Dimension dummy;
139 void ParagraphMetrics::setInsetDimension(Inset const * inset,
140 Dimension const & dim)
142 inset_dims_[inset] = dim;
146 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
148 BOOST_ASSERT(!rows().empty());
150 // If boundary is set we should return the row on which
151 // the character before is inside.
152 if (pos > 0 && boundary)
155 RowList::iterator rit = rows_.end();
156 RowList::iterator const begin = rows_.begin();
158 for (--rit; rit != begin && rit->pos() > pos; --rit)
165 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
167 BOOST_ASSERT(!rows().empty());
169 // If boundary is set we should return the row on which
170 // the character before is inside.
171 if (pos > 0 && boundary)
174 RowList::const_iterator rit = rows_.end();
175 RowList::const_iterator const begin = rows_.begin();
177 for (--rit; rit != begin && rit->pos() > pos; --rit)
184 size_t ParagraphMetrics::pos2row(pos_type pos) const
186 BOOST_ASSERT(!rows().empty());
188 RowList::const_iterator rit = rows_.end();
189 RowList::const_iterator const begin = rows_.begin();
191 for (--rit; rit != begin && rit->pos() > pos; --rit)
198 void ParagraphMetrics::dump() const
200 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
201 for (size_t i = 0; i != rows_.size(); ++i) {
202 lyxerr << " row " << i << ": ";
207 int ParagraphMetrics::rightMargin(Buffer const & buffer) const
209 BufferParams const & params = buffer.params();
210 TextClass const & tclass = params.getTextClass();
211 frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
214 + fm.signedWidth(tclass.rightmargin())
215 + fm.signedWidth(par_->layout()->rightmargin)
216 * 4 / (par_->getDepth() + 4);
222 int ParagraphMetrics::singleWidth(pos_type pos, Font const & font) const
224 // The most special cases are handled first.
225 if (par_->isInset(pos))
226 return insetDimension(par_->getInset(pos)).wid;
228 char_type c = par_->getChar(pos);
231 return theFontMetrics(font).width(c);
233 Language const * language = font.language();
234 if (language->rightToLeft()) {
235 if (language->lang() == "arabic_arabtex" ||
236 language->lang() == "arabic_arabi" ||
237 language->lang() == "farsi") {
238 if (Encodings::isComposeChar_arabic(c))
240 c = par_->transformChar(c, pos);
241 } else if (language->lang() == "hebrew" &&
242 Encodings::isComposeChar_hebrew(c))
245 return theFontMetrics(font).width(c);
249 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
251 if (!par_->isHfill(pos))
254 BOOST_ASSERT(pos >= row.pos() && pos < row.endpos());
256 // expand at the end of a row only if there is another hfill on the same row
257 if (pos == row.endpos() - 1) {
258 for (pos_type i = row.pos(); i < pos; i++) {
259 if (par_->isHfill(i))
265 // expand at the beginning of a row only if it is the first row of a paragraph
266 if (pos == row.pos()) {
270 // do not expand in some labels
271 if (par_->layout()->margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
274 // if there is anything between the first char of the row and
275 // the specified position that is neither a newline nor an hfill,
276 // the hfill will be expanded, otherwise it won't
277 for (pos_type i = row.pos(); i < pos; i++) {
278 if (!par_->isNewline(i) && !par_->isHfill(i))