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"
34 #include "OutputParams.h"
35 #include "paragraph_funcs.h"
40 #include "frontends/FontMetrics.h"
42 #include "insets/InsetBibitem.h"
43 #include "insets/InsetOptArg.h"
45 #include "support/lstrings.h"
46 #include "support/textutils.h"
47 #include "support/convert.h"
48 #include "support/unicode.h"
50 #include <boost/bind.hpp>
51 #include <boost/crc.hpp>
61 using lyx::support::contains;
62 using lyx::support::rsplit;
71 using std::ostringstream;
74 ParagraphMetrics::ParagraphMetrics(Paragraph const & par): position_(-1), par_(&par)
79 ParagraphMetrics & ParagraphMetrics::operator=(
80 ParagraphMetrics const & pm)
85 position_ = pm.position_;
90 void ParagraphMetrics::reset(Paragraph const & par)
98 void ParagraphMetrics::computeRowSignature(Row & row,
99 BufferParams const & bparams)
101 boost::crc_32_type crc;
102 for (pos_type i = row.pos(); i < row.endpos(); ++i) {
103 char_type const b[] = { par_->getChar(i) };
104 crc.process_bytes(b, 1);
105 if (bparams.trackChanges) {
106 Change change = par_->lookupChange(i);
107 char_type const b[] = { change.type };
108 crc.process_bytes(b, 1);
111 row.setCrc(crc.checksum());
115 void ParagraphMetrics::setPosition(int position)
117 position_ = position;
121 Dimension const & ParagraphMetrics::insetDimension(Inset const * inset) const
123 InsetDims::const_iterator it = inset_dims_.find(inset);
124 if (it != inset_dims_.end())
127 static Dimension dummy;
132 void ParagraphMetrics::setInsetDimension(Inset const * inset,
133 Dimension const & dim)
135 inset_dims_[inset] = dim;
139 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
141 BOOST_ASSERT(!rows().empty());
143 // If boundary is set we should return the row on which
144 // the character before is inside.
145 if (pos > 0 && boundary)
148 RowList::iterator rit = rows_.end();
149 RowList::iterator const begin = rows_.begin();
151 for (--rit; rit != begin && rit->pos() > pos; --rit)
158 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
160 BOOST_ASSERT(!rows().empty());
162 // If boundary is set we should return the row on which
163 // the character before is inside.
164 if (pos > 0 && boundary)
167 RowList::const_iterator rit = rows_.end();
168 RowList::const_iterator const begin = rows_.begin();
170 for (--rit; rit != begin && rit->pos() > pos; --rit)
177 size_t ParagraphMetrics::pos2row(pos_type pos) const
179 BOOST_ASSERT(!rows().empty());
181 RowList::const_iterator rit = rows_.end();
182 RowList::const_iterator const begin = rows_.begin();
184 for (--rit; rit != begin && rit->pos() > pos; --rit)
191 void ParagraphMetrics::dump() const
193 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
194 for (size_t i = 0; i != rows_.size(); ++i) {
195 lyxerr << " row " << i << ": ";
200 int ParagraphMetrics::rightMargin(Buffer const & buffer) const
202 BufferParams const & params = buffer.params();
203 TextClass const & tclass = params.getTextClass();
204 frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
207 + fm.signedWidth(tclass.rightmargin())
208 + fm.signedWidth(par_->layout()->rightmargin)
209 * 4 / (par_->getDepth() + 4);
215 int ParagraphMetrics::singleWidth(pos_type pos, Font const & font) const
217 char_type c = par_->getChar(pos);
219 // The most special cases are handled first.
220 if (c == Paragraph::META_INSET)
221 return insetDimension(par_->getInset(pos)).wid;
224 return theFontMetrics(font).width(c);
226 Language const * language = font.language();
227 if (language->rightToLeft()) {
228 if (language->lang() == "arabic_arabtex" ||
229 language->lang() == "arabic_arabi" ||
230 language->lang() == "farsi") {
231 if (Encodings::isComposeChar_arabic(c))
233 c = par_->transformChar(c, pos);
234 } else if (language->lang() == "hebrew" &&
235 Encodings::isComposeChar_hebrew(c))
238 return theFontMetrics(font).width(c);
242 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
244 if (!par_->isHfill(pos))
247 BOOST_ASSERT(pos >= row.pos() && pos < row.endpos());
249 // expand at the end of a row only if there is another hfill on the same row
250 if (pos == row.endpos() - 1) {
251 for (pos_type i = row.pos(); i < pos; i++) {
252 if (par_->isHfill(i))
258 // expand at the beginning of a row only if it is the first row of a paragraph
259 if (pos == row.pos()) {
263 // do not expand in some labels
264 if (par_->layout()->margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
267 // if there is anything between the first char of the row and
268 // the specified position that is neither a newline nor an hfill,
269 // the hfill will be expanded, otherwise it won't
270 for (pos_type i = row.pos(); i < pos; i++) {
271 if (!par_->isNewline(i) && !par_->isHfill(i))