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 void ParagraphMetrics::computeRowSignature(Row & row,
101 BufferParams const & bparams)
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);
113 row.setCrc(crc.checksum());
117 void ParagraphMetrics::setPosition(int position)
119 position_ = position;
123 Dimension const & ParagraphMetrics::insetDimension(Inset const * inset) const
125 InsetDims::const_iterator it = inset_dims_.find(inset);
126 if (it != inset_dims_.end())
129 static Dimension dummy;
134 void ParagraphMetrics::setInsetDimension(Inset const * inset,
135 Dimension const & dim)
137 inset_dims_[inset] = dim;
141 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
143 BOOST_ASSERT(!rows().empty());
145 // If boundary is set we should return the row on which
146 // the character before is inside.
147 if (pos > 0 && boundary)
150 RowList::iterator rit = rows_.end();
151 RowList::iterator const begin = rows_.begin();
153 for (--rit; rit != begin && rit->pos() > pos; --rit)
160 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
162 BOOST_ASSERT(!rows().empty());
164 // If boundary is set we should return the row on which
165 // the character before is inside.
166 if (pos > 0 && boundary)
169 RowList::const_iterator rit = rows_.end();
170 RowList::const_iterator const begin = rows_.begin();
172 for (--rit; rit != begin && rit->pos() > pos; --rit)
179 size_t ParagraphMetrics::pos2row(pos_type pos) const
181 BOOST_ASSERT(!rows().empty());
183 RowList::const_iterator rit = rows_.end();
184 RowList::const_iterator const begin = rows_.begin();
186 for (--rit; rit != begin && rit->pos() > pos; --rit)
193 void ParagraphMetrics::dump() const
195 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
196 for (size_t i = 0; i != rows_.size(); ++i) {
197 lyxerr << " row " << i << ": ";
202 int ParagraphMetrics::rightMargin(Buffer const & buffer) const
204 BufferParams const & params = buffer.params();
205 TextClass const & tclass = params.getTextClass();
206 frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
209 + fm.signedWidth(tclass.rightmargin())
210 + fm.signedWidth(par_->layout()->rightmargin)
211 * 4 / (par_->getDepth() + 4);
217 int ParagraphMetrics::singleWidth(pos_type pos, Font const & font) const
219 // The most special cases are handled first.
220 if (par_->isInset(pos))
221 return insetDimension(par_->getInset(pos)).wid;
223 char_type c = par_->getChar(pos);
226 return theFontMetrics(font).width(c);
228 Language const * language = font.language();
229 if (language->rightToLeft()) {
230 if (language->lang() == "arabic_arabtex" ||
231 language->lang() == "arabic_arabi" ||
232 language->lang() == "farsi") {
233 if (Encodings::isComposeChar_arabic(c))
235 c = par_->transformChar(c, pos);
236 } else if (language->lang() == "hebrew" &&
237 Encodings::isComposeChar_hebrew(c))
240 return theFontMetrics(font).width(c);
244 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
246 if (!par_->isHfill(pos))
249 BOOST_ASSERT(pos >= row.pos() && pos < row.endpos());
251 // expand at the end of a row only if there is another hfill on the same row
252 if (pos == row.endpos() - 1) {
253 for (pos_type i = row.pos(); i < pos; i++) {
254 if (par_->isHfill(i))
260 // expand at the beginning of a row only if it is the first row of a paragraph
261 if (pos == row.pos()) {
265 // do not expand in some labels
266 if (par_->layout()->margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
269 // if there is anything between the first char of the row and
270 // the specified position that is neither a newline nor an hfill,
271 // the hfill will be expanded, otherwise it won't
272 for (pos_type i = row.pos(); i < pos; i++) {
273 if (!par_->isNewline(i) && !par_->isHfill(i))