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"
28 #include "LaTeXFeatures.h"
33 #include "OutputParams.h"
35 #include "TextClass.h"
38 #include "frontends/FontMetrics.h"
40 #include "insets/InsetBibitem.h"
41 #include "insets/InsetArgument.h"
43 #include "support/lassert.h"
44 #include "support/debug.h"
45 #include "support/gettext.h"
46 #include "support/lstrings.h"
47 #include "support/textutils.h"
49 #include "support/bind.h"
50 #include <boost/crc.hpp>
58 using namespace lyx::support;
63 ParagraphMetrics::ParagraphMetrics(Paragraph const & par) :
64 position_(-1), par_(&par)
68 ParagraphMetrics & ParagraphMetrics::operator=(
69 ParagraphMetrics const & pm)
74 position_ = pm.position_;
79 void ParagraphMetrics::reset(Paragraph const & par)
87 size_t ParagraphMetrics::computeRowSignature(Row const & row,
88 BufferParams const & bparams) const
90 boost::crc_32_type crc;
91 for (pos_type i = row.pos(); i < row.endpos(); ++i) {
92 char_type const b[] = { par_->getChar(i) };
93 crc.process_bytes(b, sizeof(char_type));
94 if (bparams.trackChanges) {
95 Change change = par_->lookupChange(i);
96 char_type const b[] = { change.type };
97 // 1 byte is enough to encode Change::Type
98 crc.process_bytes(b, 1);
102 Dimension const & d = row.dimension();
103 char_type const b[] = { row.sel_beg, row.sel_end,
104 row.begin_margin_sel, row.end_margin_sel, d.wid, d.asc, d.des};
105 crc.process_bytes(b, sizeof(b));
107 return crc.checksum();
111 void ParagraphMetrics::setPosition(int position)
113 position_ = position;
117 Dimension const & ParagraphMetrics::insetDimension(Inset const * inset) const
119 InsetDims::const_iterator it = inset_dims_.find(inset);
120 if (it != inset_dims_.end())
123 static Dimension dummy;
128 void ParagraphMetrics::setInsetDimension(Inset const * inset,
129 Dimension const & dim)
131 inset_dims_[inset] = dim;
135 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
137 LASSERT(!rows().empty(), /**/);
139 // If boundary is set we should return the row on which
140 // the character before is inside.
141 if (pos > 0 && boundary)
144 RowList::iterator rit = rows_.end();
145 RowList::iterator const begin = rows_.begin();
147 for (--rit; rit != begin && rit->pos() > pos; --rit)
154 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
156 LASSERT(!rows().empty(), /**/);
158 // If boundary is set we should return the row on which
159 // the character before is inside.
160 if (pos > 0 && boundary)
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 size_t ParagraphMetrics::pos2row(pos_type pos) const
175 LASSERT(!rows().empty(), /**/);
177 RowList::const_iterator rit = rows_.end();
178 RowList::const_iterator const begin = rows_.begin();
180 for (--rit; rit != begin && rit->pos() > pos; --rit)
187 void ParagraphMetrics::dump() const
189 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
190 for (size_t i = 0; i != rows_.size(); ++i) {
191 lyxerr << " row " << i << ": ";
196 int ParagraphMetrics::rightMargin(BufferView const & bv) const
198 BufferParams const & params = bv.buffer().params();
199 DocumentClass const & tclass = params.documentClass();
200 frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
203 + fm.signedWidth(tclass.rightmargin())
204 + fm.signedWidth(par_->layout().rightmargin)
205 * 4 / (par_->getDepth() + 4);
211 int ParagraphMetrics::singleWidth(pos_type pos, Font const & font) const
213 // The most special cases are handled first.
214 if (Inset const * inset = par_->getInset(pos))
215 return insetDimension(inset).wid;
217 char_type c = par_->getChar(pos);
220 return 4 * theFontMetrics(font).width(' ');
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::isArabicComposeChar(c))
232 c = par_->transformChar(c, pos);
233 } else if (language->lang() == "hebrew" &&
234 Encodings::isHebrewComposeChar(c)) {
238 return theFontMetrics(font).width(c);
242 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
244 if (!par_->isHfill(pos))
247 LASSERT(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))