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"
36 #include "rowpainter.h"
42 #include "frontends/FontMetrics.h"
44 #include "insets/InsetBibitem.h"
45 #include "insets/InsetOptArg.h"
47 #include "support/lstrings.h"
48 #include "support/textutils.h"
49 #include "support/convert.h"
50 #include "support/unicode.h"
52 #include <boost/bind.hpp>
53 #include <boost/crc.hpp>
63 using lyx::support::contains;
64 using lyx::support::rsplit;
73 using std::ostringstream;
76 ParagraphMetrics::ParagraphMetrics(Paragraph const & par): par_(&par)
81 ParagraphMetrics & ParagraphMetrics::operator=(
82 ParagraphMetrics const & pm)
91 void ParagraphMetrics::reset(Paragraph const & par)
99 size_type ParagraphMetrics::calculateRowSignature(Row const & row)
101 boost::crc_32_type crc;
102 for (pos_type i = row.pos(); i < row.endpos(); ++i) {
103 char_type const b[] = { par_->getChar(i) };
104 crc.process_bytes(b, 1);
106 return crc.checksum();
110 void ParagraphMetrics::updateRowChangeStatus()
112 size_t const size = rows_.size();
113 row_change_status_.resize(size);
114 row_signature_.resize(size);
116 for (size_t i = 0; i != size; ++i) {
117 // Row signature; has row changed since last update?
118 size_type const row_sig = calculateRowSignature(rows_[i]);
119 row_change_status_[i] = row_signature_[i] != row_sig;
120 row_signature_[i] = row_sig;
125 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
127 BOOST_ASSERT(!rows().empty());
129 // If boundary is set we should return the row on which
130 // the character before is inside.
131 if (pos > 0 && boundary)
134 RowList::iterator rit = rows_.end();
135 RowList::iterator const begin = rows_.begin();
137 for (--rit; rit != begin && rit->pos() > pos; --rit)
144 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
146 BOOST_ASSERT(!rows().empty());
148 // If boundary is set we should return the row on which
149 // the character before is inside.
150 if (pos > 0 && boundary)
153 RowList::const_iterator rit = rows_.end();
154 RowList::const_iterator const begin = rows_.begin();
156 for (--rit; rit != begin && rit->pos() > pos; --rit)
163 size_t ParagraphMetrics::pos2row(pos_type pos) const
165 BOOST_ASSERT(!rows().empty());
167 RowList::const_iterator rit = rows_.end();
168 RowList::const_iterator const begin = rows_.begin();
170 for (--rit; rit != begin && rit->pos() > pos; --rit)
177 void ParagraphMetrics::dump() const
179 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
180 for (size_t i = 0; i != rows_.size(); ++i) {
181 lyxerr << " row " << i << ": ";
186 int ParagraphMetrics::rightMargin(Buffer const & buffer) const
188 BufferParams const & params = buffer.params();
189 TextClass const & tclass = params.getTextClass();
190 frontend::FontMetrics const & fm = theFontMetrics(params.getFont());
193 + fm.signedWidth(tclass.rightmargin())
194 + fm.signedWidth(par_->layout()->rightmargin)
195 * 4 / (par_->getDepth() + 4);
201 int ParagraphMetrics::singleWidth(pos_type pos, Font const & font) const
203 char_type c = par_->getChar(pos);
205 // The most special cases are handled first.
206 if (c == Paragraph::META_INSET)
207 return par_->getInset(pos)->width();
210 return theFontMetrics(font).width(c);
212 Language const * language = font.language();
213 if (language->rightToLeft()) {
214 if (language->lang() == "arabic_arabtex" ||
215 language->lang() == "arabic_arabi" ||
216 language->lang() == "farsi") {
217 if (Encodings::isComposeChar_arabic(c))
219 c = par_->transformChar(c, pos);
220 } else if (language->lang() == "hebrew" &&
221 Encodings::isComposeChar_hebrew(c))
224 return theFontMetrics(font).width(c);