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)
99 boost::crc_32_type crc;
100 for (pos_type i = row.pos(); i < row.endpos(); ++i) {
101 char_type const b[] = { par_->getChar(i) };
102 crc.process_bytes(b, 1);
104 return crc.checksum();
108 void ParagraphMetrics::updateRowChangeStatus()
110 size_t const size = rows_.size();
111 row_change_status_.resize(size);
112 row_signature_.resize(size);
114 for (size_t i = 0; i != size; ++i) {
115 // Row signature; has row changed since last update?
116 size_type const row_sig = calculateRowSignature(rows_[i]);
117 row_change_status_[i] = row_signature_[i] != row_sig;
118 row_signature_[i] = row_sig;
123 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
125 BOOST_ASSERT(!rows().empty());
127 // If boundary is set we should return the row on which
128 // the character before is inside.
129 if (pos > 0 && boundary)
132 RowList::iterator rit = rows_.end();
133 RowList::iterator const begin = rows_.begin();
135 for (--rit; rit != begin && rit->pos() > pos; --rit)
142 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
144 BOOST_ASSERT(!rows().empty());
146 // If boundary is set we should return the row on which
147 // the character before is inside.
148 if (pos > 0 && boundary)
151 RowList::const_iterator rit = rows_.end();
152 RowList::const_iterator const begin = rows_.begin();
154 for (--rit; rit != begin && rit->pos() > pos; --rit)
161 size_t ParagraphMetrics::pos2row(pos_type pos) const
163 BOOST_ASSERT(!rows().empty());
165 RowList::const_iterator rit = rows_.end();
166 RowList::const_iterator const begin = rows_.begin();
168 for (--rit; rit != begin && rit->pos() > pos; --rit)
175 void ParagraphMetrics::dump() const
177 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
178 for (size_t i = 0; i != rows_.size(); ++i) {
179 lyxerr << " row " << i << ": ";
184 int ParagraphMetrics::rightMargin(Buffer const & buffer) const
186 BufferParams const & params = buffer.params();
187 TextClass const & tclass = params.getTextClass();
188 frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
191 + fm.signedWidth(tclass.rightmargin())
192 + fm.signedWidth(par_->layout()->rightmargin)
193 * 4 / (par_->getDepth() + 4);
199 int ParagraphMetrics::singleWidth(pos_type pos, Font const & font) const
201 char_type c = par_->getChar(pos);
203 // The most special cases are handled first.
204 if (c == Paragraph::META_INSET)
205 return par_->getInset(pos)->width();
208 return theFontMetrics(font).width(c);
210 Language const * language = font.language();
211 if (language->rightToLeft()) {
212 if (language->lang() == "arabic_arabtex" ||
213 language->lang() == "arabic_arabi" ||
214 language->lang() == "farsi") {
215 if (Encodings::isComposeChar_arabic(c))
217 c = par_->transformChar(c, pos);
218 } else if (language->lang() == "hebrew" &&
219 Encodings::isComposeChar_hebrew(c))
222 return theFontMetrics(font).width(c);