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"
29 #include "LaTeXFeatures.h"
33 #include "outputparams.h"
34 #include "paragraph_funcs.h"
35 #include "ParagraphList_fwd.h"
37 #include "rowpainter.h"
43 #include "frontends/FontMetrics.h"
45 #include "insets/insetbibitem.h"
46 #include "insets/insetoptarg.h"
48 #include "support/lstrings.h"
49 #include "support/textutils.h"
50 #include "support/convert.h"
51 #include "support/unicode.h"
53 #include <boost/bind.hpp>
54 #include <boost/crc.hpp>
64 using lyx::support::contains;
65 using lyx::support::rsplit;
74 using std::ostringstream;
77 ParagraphMetrics::ParagraphMetrics(Paragraph const & par): par_(&par)
82 ParagraphMetrics & ParagraphMetrics::operator=(
83 ParagraphMetrics const & pm)
92 size_type ParagraphMetrics::calculateRowSignature(Row const & row)
94 boost::crc_32_type crc;
95 for (pos_type i = row.pos(); i < row.endpos(); ++i) {
96 char_type const b[] = { par_->getChar(i) };
97 crc.process_bytes(b, 1);
99 return crc.checksum();
103 void ParagraphMetrics::updateRowChangeStatus()
105 size_t const size = rows_.size();
106 row_change_status_.resize(size);
107 row_signature_.resize(size);
109 for (size_t i = 0; i != size; ++i) {
110 // Row signature; has row changed since last update?
111 size_type const row_sig = calculateRowSignature(rows_[i]);
112 row_change_status_[i] = row_signature_[i] != row_sig;
113 row_signature_[i] = row_sig;
118 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
120 BOOST_ASSERT(!rows().empty());
122 // If boundary is set we should return the row on which
123 // the character before is inside.
124 if (pos > 0 && boundary)
127 RowList::iterator rit = rows_.end();
128 RowList::iterator const begin = rows_.begin();
130 for (--rit; rit != begin && rit->pos() > pos; --rit)
137 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
139 BOOST_ASSERT(!rows().empty());
141 // If boundary is set we should return the row on which
142 // the character before is inside.
143 if (pos > 0 && boundary)
146 RowList::const_iterator rit = rows_.end();
147 RowList::const_iterator const begin = rows_.begin();
149 for (--rit; rit != begin && rit->pos() > pos; --rit)
156 size_t ParagraphMetrics::pos2row(pos_type pos) const
158 BOOST_ASSERT(!rows().empty());
160 RowList::const_iterator rit = rows_.end();
161 RowList::const_iterator const begin = rows_.begin();
163 for (--rit; rit != begin && rit->pos() > pos; --rit)
170 void ParagraphMetrics::dump() const
172 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
173 for (size_t i = 0; i != rows_.size(); ++i) {
174 lyxerr << " row " << i << ": ";
179 int ParagraphMetrics::rightMargin(Buffer const & buffer) const
181 BufferParams const & params = buffer.params();
182 LyXTextClass const & tclass = params.getLyXTextClass();
183 docstring trmarg = from_utf8(tclass.rightmargin());
184 docstring lrmarg = from_utf8(par_->layout()->rightmargin);
185 frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
188 + fm.signedWidth(trmarg)
189 + fm.signedWidth(lrmarg)
190 * 4 / (par_->getDepth() + 4);