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"
27 #include "support/debug.h"
28 #include "support/gettext.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>
61 using namespace lyx::support;
66 ParagraphMetrics::ParagraphMetrics(Paragraph const & par): position_(-1), par_(&par)
71 ParagraphMetrics & ParagraphMetrics::operator=(
72 ParagraphMetrics const & pm)
77 position_ = pm.position_;
82 void ParagraphMetrics::reset(Paragraph const & par)
90 size_t ParagraphMetrics::computeRowSignature(Row const & row,
91 BufferParams const & bparams) const
93 boost::crc_32_type crc;
94 for (pos_type i = row.pos(); i < row.endpos(); ++i) {
95 char_type const b[] = { par_->getChar(i) };
96 crc.process_bytes(b, sizeof(char_type));
97 if (bparams.trackChanges) {
98 Change change = par_->lookupChange(i);
99 char_type const b[] = { change.type };
100 // 1 byte is enough to encode Change::Type
101 crc.process_bytes(b, 1);
105 Dimension const & d = row.dimension();
106 char_type const b[] = { row.sel_beg, row.sel_end, d.wid, d.asc, d.des};
107 // Each of the variable to process is 4 bytes: 4x5 = 20
108 crc.process_bytes(b, 20);
110 return crc.checksum();
114 void ParagraphMetrics::setPosition(int position)
116 position_ = position;
120 Dimension const & ParagraphMetrics::insetDimension(Inset const * inset) const
122 InsetDims::const_iterator it = inset_dims_.find(inset);
123 if (it != inset_dims_.end())
126 static Dimension dummy;
131 void ParagraphMetrics::setInsetDimension(Inset const * inset,
132 Dimension const & dim)
134 inset_dims_[inset] = dim;
138 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
140 BOOST_ASSERT(!rows().empty());
142 // If boundary is set we should return the row on which
143 // the character before is inside.
144 if (pos > 0 && boundary)
147 RowList::iterator rit = rows_.end();
148 RowList::iterator const begin = rows_.begin();
150 for (--rit; rit != begin && rit->pos() > pos; --rit)
157 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
159 BOOST_ASSERT(!rows().empty());
161 // If boundary is set we should return the row on which
162 // the character before is inside.
163 if (pos > 0 && boundary)
166 RowList::const_iterator rit = rows_.end();
167 RowList::const_iterator const begin = rows_.begin();
169 for (--rit; rit != begin && rit->pos() > pos; --rit)
176 size_t ParagraphMetrics::pos2row(pos_type pos) const
178 BOOST_ASSERT(!rows().empty());
180 RowList::const_iterator rit = rows_.end();
181 RowList::const_iterator const begin = rows_.begin();
183 for (--rit; rit != begin && rit->pos() > pos; --rit)
190 void ParagraphMetrics::dump() const
192 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
193 for (size_t i = 0; i != rows_.size(); ++i) {
194 lyxerr << " row " << i << ": ";
199 int ParagraphMetrics::rightMargin(Buffer const & buffer) const
201 BufferParams const & params = buffer.params();
202 TextClass const & tclass = params.getTextClass();
203 frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
206 + fm.signedWidth(tclass.rightmargin())
207 + fm.signedWidth(par_->layout()->rightmargin)
208 * 4 / (par_->getDepth() + 4);
214 int ParagraphMetrics::singleWidth(pos_type pos, Font const & font) const
216 // The most special cases are handled first.
217 if (par_->isInset(pos))
218 return insetDimension(par_->getInset(pos)).wid;
220 char_type c = par_->getChar(pos);
223 return theFontMetrics(font).width(c);
225 Language const * language = font.language();
226 if (language->rightToLeft()) {
227 if (language->lang() == "arabic_arabtex" ||
228 language->lang() == "arabic_arabi" ||
229 language->lang() == "farsi") {
230 if (Encodings::isComposeChar_arabic(c))
232 c = par_->transformChar(c, pos);
233 } else if (language->lang() == "hebrew" &&
234 Encodings::isComposeChar_hebrew(c))
237 return theFontMetrics(font).width(c);
241 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
243 if (!par_->isHfill(pos))
246 BOOST_ASSERT(pos >= row.pos() && pos < row.endpos());
248 // expand at the end of a row only if there is another hfill on the same row
249 if (pos == row.endpos() - 1) {
250 for (pos_type i = row.pos(); i < pos; i++) {
251 if (par_->isHfill(i))
257 // expand at the beginning of a row only if it is the first row of a paragraph
258 if (pos == row.pos()) {
262 // do not expand in some labels
263 if (par_->layout()->margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
266 // if there is anything between the first char of the row and
267 // the specified position that is neither a newline nor an hfill,
268 // the hfill will be expanded, otherwise it won't
269 for (pos_type i = row.pos(); i < pos; i++) {
270 if (!par_->isNewline(i) && !par_->isHfill(i))