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"
34 #include "paragraph_funcs.h"
36 #include "TextClass.h"
40 #include "frontends/FontMetrics.h"
42 #include "insets/InsetBibitem.h"
43 #include "insets/InsetOptArg.h"
45 #include "support/lassert.h"
46 #include "support/debug.h"
47 #include "support/gettext.h"
48 #include "support/lstrings.h"
49 #include "support/textutils.h"
51 #include <boost/bind.hpp>
52 #include <boost/crc.hpp>
60 using namespace lyx::support;
65 ParagraphMetrics::ParagraphMetrics(Paragraph const & par) :
66 position_(-1), par_(&par)
70 ParagraphMetrics & ParagraphMetrics::operator=(
71 ParagraphMetrics const & pm)
76 position_ = pm.position_;
81 void ParagraphMetrics::reset(Paragraph const & par)
89 size_t ParagraphMetrics::computeRowSignature(Row const & row,
90 BufferParams const & bparams) const
92 boost::crc_32_type crc;
93 for (pos_type i = row.pos(); i < row.endpos(); ++i) {
94 char_type const b[] = { par_->getChar(i) };
95 crc.process_bytes(b, sizeof(char_type));
96 if (bparams.trackChanges) {
97 Change change = par_->lookupChange(i);
98 char_type const b[] = { change.type };
99 // 1 byte is enough to encode Change::Type
100 crc.process_bytes(b, 1);
104 Dimension const & d = row.dimension();
105 char_type const b[] = { row.sel_beg, row.sel_end,
106 row.begin_margin_sel, row.end_margin_sel, d.wid, d.asc, d.des};
107 // Each of the variable to process is 4 bytes: 4x7 = 28
108 crc.process_bytes(b, 28);
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 LASSERT(!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 LASSERT(!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 LASSERT(!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(BufferView const & bv) const
201 BufferParams const & params = bv.buffer().params();
202 DocumentClass const & tclass = params.documentClass();
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 (Inset const * inset = par_->getInset(pos))
218 return insetDimension(inset).wid;
220 char_type c = par_->getChar(pos);
223 return 4 * theFontMetrics(font).width(' ');
226 return theFontMetrics(font).width(c);
228 Language const * language = font.language();
229 if (language->rightToLeft()) {
230 if (language->lang() == "arabic_arabtex" ||
231 language->lang() == "arabic_arabi" ||
232 language->lang() == "farsi") {
233 if (Encodings::isArabicComposeChar(c))
235 c = par_->transformChar(c, pos);
236 } else if (language->lang() == "hebrew" &&
237 Encodings::isHebrewComposeChar(c)) {
241 return theFontMetrics(font).width(c);
245 bool ParagraphMetrics::hfillExpansion(Row const & row, pos_type pos) const
247 if (!par_->isHfill(pos))
250 LASSERT(pos >= row.pos() && pos < row.endpos(), /**/);
252 // expand at the end of a row only if there is another hfill on the same row
253 if (pos == row.endpos() - 1) {
254 for (pos_type i = row.pos(); i < pos; i++) {
255 if (par_->isHfill(i))
261 // expand at the beginning of a row only if it is the first row of a paragraph
262 if (pos == row.pos()) {
266 // do not expand in some labels
267 if (par_->layout().margintype != MARGIN_MANUAL && pos < par_->beginOfBody())
270 // if there is anything between the first char of the row and
271 // the specified position that is neither a newline nor an hfill,
272 // the hfill will be expanded, otherwise it won't
273 for (pos_type i = row.pos(); i < pos; i++) {
274 if (!par_->isNewline(i) && !par_->isHfill(i))