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): par_(&par)
79 ParagraphMetrics & ParagraphMetrics::operator=(
80 ParagraphMetrics const & pm)
89 void ParagraphMetrics::reset(Paragraph const & par)
97 size_type ParagraphMetrics::calculateRowSignature(Row const & row,
98 BufferParams const & bparams)
100 boost::crc_32_type crc;
101 for (pos_type i = row.pos(); i < row.endpos(); ++i) {
102 char_type const b[] = { par_->getChar(i) };
103 crc.process_bytes(b, 1);
104 if (bparams.trackChanges) {
105 Change change = par_->lookupChange(i);
106 char_type const b[] = { change.type };
107 crc.process_bytes(b, 1);
110 return crc.checksum();
114 void ParagraphMetrics::updateRowChangeStatus(BufferParams const & bparams)
116 size_t const size = rows_.size();
117 row_change_status_.resize(size);
118 row_signature_.resize(size);
120 for (size_t i = 0; i != size; ++i) {
121 // Row signature; has row changed since last update?
122 size_type const row_sig = calculateRowSignature(rows_[i], bparams);
123 row_change_status_[i] = row_signature_[i] != row_sig;
124 row_signature_[i] = row_sig;
129 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
131 BOOST_ASSERT(!rows().empty());
133 // If boundary is set we should return the row on which
134 // the character before is inside.
135 if (pos > 0 && boundary)
138 RowList::iterator rit = rows_.end();
139 RowList::iterator const begin = rows_.begin();
141 for (--rit; rit != begin && rit->pos() > pos; --rit)
148 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
150 BOOST_ASSERT(!rows().empty());
152 // If boundary is set we should return the row on which
153 // the character before is inside.
154 if (pos > 0 && boundary)
157 RowList::const_iterator rit = rows_.end();
158 RowList::const_iterator const begin = rows_.begin();
160 for (--rit; rit != begin && rit->pos() > pos; --rit)
167 size_t ParagraphMetrics::pos2row(pos_type pos) const
169 BOOST_ASSERT(!rows().empty());
171 RowList::const_iterator rit = rows_.end();
172 RowList::const_iterator const begin = rows_.begin();
174 for (--rit; rit != begin && rit->pos() > pos; --rit)
181 void ParagraphMetrics::dump() const
183 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
184 for (size_t i = 0; i != rows_.size(); ++i) {
185 lyxerr << " row " << i << ": ";
190 int ParagraphMetrics::rightMargin(Buffer const & buffer) const
192 BufferParams const & params = buffer.params();
193 TextClass const & tclass = params.getTextClass();
194 frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
197 + fm.signedWidth(tclass.rightmargin())
198 + fm.signedWidth(par_->layout()->rightmargin)
199 * 4 / (par_->getDepth() + 4);
205 int ParagraphMetrics::singleWidth(pos_type pos, Font const & font) const
207 char_type c = par_->getChar(pos);
209 // The most special cases are handled first.
210 if (c == Paragraph::META_INSET)
211 return par_->getInset(pos)->width();
214 return theFontMetrics(font).width(c);
216 Language const * language = font.language();
217 if (language->rightToLeft()) {
218 if (language->lang() == "arabic_arabtex" ||
219 language->lang() == "arabic_arabi" ||
220 language->lang() == "farsi") {
221 if (Encodings::isComposeChar_arabic(c))
223 c = par_->transformChar(c, pos);
224 } else if (language->lang() == "hebrew" &&
225 Encodings::isComposeChar_hebrew(c))
228 return theFontMetrics(font).width(c);