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"
41 #include "frontends/FontMetrics.h"
43 #include "insets/InsetBibitem.h"
44 #include "insets/InsetOptArg.h"
46 #include "support/lstrings.h"
47 #include "support/textutils.h"
48 #include "support/convert.h"
49 #include "support/unicode.h"
51 #include <boost/bind.hpp>
52 #include <boost/crc.hpp>
62 using lyx::support::contains;
63 using lyx::support::rsplit;
72 using std::ostringstream;
75 ParagraphMetrics::ParagraphMetrics(Paragraph const & par): position_(-1), par_(&par)
80 ParagraphMetrics & ParagraphMetrics::operator=(
81 ParagraphMetrics const & pm)
86 position_ = pm.position_;
91 void ParagraphMetrics::reset(Paragraph const & par)
99 void ParagraphMetrics::computeRowSignature(Row & row,
100 BufferParams const & bparams)
102 boost::crc_32_type crc;
103 for (pos_type i = row.pos(); i < row.endpos(); ++i) {
104 char_type const b[] = { par_->getChar(i) };
105 crc.process_bytes(b, 1);
106 if (bparams.trackChanges) {
107 Change change = par_->lookupChange(i);
108 char_type const b[] = { change.type };
109 crc.process_bytes(b, 1);
112 row.setCrc(crc.checksum());
116 void ParagraphMetrics::setPosition(int position)
118 position_ = position;
122 Dimension const & ParagraphMetrics::insetDimension(Inset const * inset) const
124 InsetDims::const_iterator it = inset_dims_.find(inset);
125 if (it != inset_dims_.end())
128 static Dimension dummy;
133 void ParagraphMetrics::setInsetDimension(Inset const * inset,
134 Dimension const & dim)
136 inset_dims_[inset] = dim;
140 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
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::iterator rit = rows_.end();
150 RowList::iterator const begin = rows_.begin();
152 for (--rit; rit != begin && rit->pos() > pos; --rit)
159 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
161 BOOST_ASSERT(!rows().empty());
163 // If boundary is set we should return the row on which
164 // the character before is inside.
165 if (pos > 0 && boundary)
168 RowList::const_iterator rit = rows_.end();
169 RowList::const_iterator const begin = rows_.begin();
171 for (--rit; rit != begin && rit->pos() > pos; --rit)
178 size_t ParagraphMetrics::pos2row(pos_type pos) const
180 BOOST_ASSERT(!rows().empty());
182 RowList::const_iterator rit = rows_.end();
183 RowList::const_iterator const begin = rows_.begin();
185 for (--rit; rit != begin && rit->pos() > pos; --rit)
192 void ParagraphMetrics::dump() const
194 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
195 for (size_t i = 0; i != rows_.size(); ++i) {
196 lyxerr << " row " << i << ": ";
201 int ParagraphMetrics::rightMargin(Buffer const & buffer) const
203 BufferParams const & params = buffer.params();
204 TextClass const & tclass = params.getTextClass();
205 frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
208 + fm.signedWidth(tclass.rightmargin())
209 + fm.signedWidth(par_->layout()->rightmargin)
210 * 4 / (par_->getDepth() + 4);
216 int ParagraphMetrics::singleWidth(pos_type pos, Font const & font) const
218 char_type c = par_->getChar(pos);
220 // The most special cases are handled first.
221 if (c == Paragraph::META_INSET)
222 return insetDimension(par_->getInset(pos)).wid;
225 return theFontMetrics(font).width(c);
227 Language const * language = font.language();
228 if (language->rightToLeft()) {
229 if (language->lang() == "arabic_arabtex" ||
230 language->lang() == "arabic_arabi" ||
231 language->lang() == "farsi") {
232 if (Encodings::isComposeChar_arabic(c))
234 c = par_->transformChar(c, pos);
235 } else if (language->lang() == "hebrew" &&
236 Encodings::isComposeChar_hebrew(c))
239 return theFontMetrics(font).width(c);
243 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
245 if (!par_->isHfill(pos))
248 BOOST_ASSERT(pos >= row.pos() && pos < row.endpos());
250 // expand at the end of a row only if there is another hfill on the same row
251 if (pos == row.endpos() - 1) {
252 for (pos_type i = row.pos(); i < pos; i++) {
253 if (par_->isHfill(i))
259 // expand at the beginning of a row only if it is the first row of a paragraph
260 if (pos == row.pos()) {
264 // do not expand in some labels
265 if (par_->layout()->margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
268 // if there is anything between the first char of the row and
269 // the specified position that is neither a newline nor an hfill,
270 // the hfill will be expanded, otherwise it won't
271 for (pos_type i = row.pos(); i < pos; i++) {
272 if (!par_->isNewline(i) && !par_->isHfill(i))