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 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
123 BOOST_ASSERT(!rows().empty());
125 // If boundary is set we should return the row on which
126 // the character before is inside.
127 if (pos > 0 && boundary)
130 RowList::iterator rit = rows_.end();
131 RowList::iterator const begin = rows_.begin();
133 for (--rit; rit != begin && rit->pos() > pos; --rit)
140 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
142 BOOST_ASSERT(!rows().empty());
144 // If boundary is set we should return the row on which
145 // the character before is inside.
146 if (pos > 0 && boundary)
149 RowList::const_iterator rit = rows_.end();
150 RowList::const_iterator const begin = rows_.begin();
152 for (--rit; rit != begin && rit->pos() > pos; --rit)
159 size_t ParagraphMetrics::pos2row(pos_type pos) const
161 BOOST_ASSERT(!rows().empty());
163 RowList::const_iterator rit = rows_.end();
164 RowList::const_iterator const begin = rows_.begin();
166 for (--rit; rit != begin && rit->pos() > pos; --rit)
173 void ParagraphMetrics::dump() const
175 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
176 for (size_t i = 0; i != rows_.size(); ++i) {
177 lyxerr << " row " << i << ": ";
182 int ParagraphMetrics::rightMargin(Buffer const & buffer) const
184 BufferParams const & params = buffer.params();
185 TextClass const & tclass = params.getTextClass();
186 frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
189 + fm.signedWidth(tclass.rightmargin())
190 + fm.signedWidth(par_->layout()->rightmargin)
191 * 4 / (par_->getDepth() + 4);
197 int ParagraphMetrics::singleWidth(pos_type pos, Font const & font) const
199 char_type c = par_->getChar(pos);
201 // The most special cases are handled first.
202 if (c == Paragraph::META_INSET)
203 return par_->getInset(pos)->width();
206 return theFontMetrics(font).width(c);
208 Language const * language = font.language();
209 if (language->rightToLeft()) {
210 if (language->lang() == "arabic_arabtex" ||
211 language->lang() == "arabic_arabi" ||
212 language->lang() == "farsi") {
213 if (Encodings::isComposeChar_arabic(c))
215 c = par_->transformChar(c, pos);
216 } else if (language->lang() == "hebrew" &&
217 Encodings::isComposeChar_hebrew(c))
220 return theFontMetrics(font).width(c);
224 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
226 if (!par_->isHfill(pos))
229 BOOST_ASSERT(pos >= row.pos() && pos < row.endpos());
231 // expand at the end of a row only if there is another hfill on the same row
232 if (pos == row.endpos() - 1) {
233 for (pos_type i = row.pos(); i < pos; i++) {
234 if (par_->isHfill(i))
240 // expand at the beginning of a row only if it is the first row of a paragraph
241 if (pos == row.pos()) {
245 // do not expand in some labels
246 if (par_->layout()->margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
249 // if there is anything between the first char of the row and
250 // the specified position that is neither a newline nor an hfill,
251 // the hfill will be expanded, otherwise it won't
252 for (pos_type i = row.pos(); i < pos; i++) {
253 if (!par_->isNewline(i) && !par_->isHfill(i))